Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Record Calls - Software Debugging

Get Embed Code
2 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. At this point we have seen how to reproduce static data
  2. simply by shipping the files over as the data is required by the program.
  3. We have seen how to record and replay user interaction.
  4. However, all of these are actually instances of the same class of input
  5. that is interaction between the program and its environment.
  6. The question is whether it would be possible to record
  7. and replay this interaction as a whole.
  8. A program interactions with its environment
  9. by means of function calls.
  10. We use function calls to access data.
  11. We use function calls to access user interaction
  12. or generally anything that comes form the environment.
  13. So, what we could do is to record and replay function calls.
  14. This way we would have a single mechanism to capture
  15. all sorts of interaction between the program and its environment.
  16. Our plan would be to record every function call with parameters in a log
  17. then later be able to replay them.
  18. Let's first do the recording.
  19. We've already seen our tracing functions where we can access
  20. the individual execution frames of a run.
  21. We know that each frame contains the function name
  22. as well as all local variables.
  23. When a method gets called this list of local variables
  24. is actually the list of arguments for the function.
  25. If, for instance, I have set up the traceit function,
  26. which gets called for every line as always.
  27. It also gets called when a function is called.
  28. In this case the event variable up here has a value of call.
  29. If this happens then we print out the name of the current function,
  30. and we print out the local variables,
  31. which should be the arguments.
  32. Let's try out whether this works.
  33. What we see here is we have a call to remove html markup function
  34. and s as precisely the value as here in our code.
  35. We would now like to turn the function name as well as the argument list
  36. into something that can be translated back into code.
  37. We want it to look precisely like this.
  38. If we turn this into a string that looks exactly like a function call,
  39. then we can pass this to the Python eval function,
  40. which takes a string and interprets it as code.
  41. This is something we will use later for replaying.
  42. Here you can see how the string is interpreted, and the result is printed.
  43. Here's the function that takes care of the printing of the local variables
  44. that is the arguments.
  45. Since we don't know the position of the individual parameters in the function,
  46. what we use is we print out named arguments instead.
  47. That is, we print out name equals value.
  48. This will be separated by commas.
  49. Now again we will remove html markup with this parameter.
  50. If everything goes well,
  51. the traceit function to print out this very call with the right argument.
  52. This is precisely what happens.
  53. You see, we print out remove html markup with the parameter s
  54. being the string foo exactly as over here
  55. except that we do have a named parameter
  56. and not a positional parameter.
  57. The same mechanism works for tracing or recalls
  58. as long as these are Python functions.
  59. So if we call square root of 2, for instance
  60. This would also get traced and reported in our output.
  61. As you see here, here is the count of square root and x has a value of 2.
  62. Now let's assume that we have a variable,
  63. which actually has stored all these calls.
  64. We could do so by recording it right away while the code is executing,
  65. or we could also say store this in a file and read this back from a file.
  66. Your job now is to write a piece of code that evaluates all these calls.
  67. First this one, and then the other one,
  68. and which prints out the appropriate results for each of these calls
  69. in the form function argument equals return value.
  70. Over to you.