Return to Video

Data Invariants - Software Debugging

  • 0:00 - 0:08
    So, at this point we have seen that writing perfect assertions that cover all bugs would be pretty hard,
  • 0:08 - 0:13
    but this shouldn't keep us from writing assertion that catch as many bugs as possible,
  • 0:13 - 0:16
    and this is particularly useful in debugging.
  • 0:16 - 0:19
    Since assertions are automated, they can check
  • 0:19 - 0:24
    several executions of a function at once--actually all executions.
  • 0:24 - 0:29
    However, they can also check large portions of data at the same time and all automated.
  • 0:29 - 0:34
    What do I mean by large portions of data? Let me illustrate this by an example.
  • 0:34 - 0:37
    Let's assume we want to implement a time class.
  • 0:37 - 0:42
    A time that consists of hours, minutes, and seconds and we want to use assertions
  • 0:42 - 0:46
    to automatically check whether a time object is consistent.
  • 0:46 - 0:50
    That is whether the hours are in the right range, minutes are in the right range,
  • 0:50 - 0:52
    and seconds are also in the right range.
  • 0:52 - 0:57
    Here is our time class--we start with an initializer or a constructor that takes
  • 0:57 - 1:00
    three arguments: the hour, the minute, and the seconds,
  • 1:00 - 1:05
    with default values of 0 for each and we assign these to individual attributes
  • 1:05 - 1:09
    named hours, minutes, and seconds within a time object.
  • 1:09 - 1:13
    Here's a number of inspector method that gives us access to the internal attributes--
  • 1:13 - 1:15
    hours, minutes, and seconds.
  • 1:15 - 1:20
    After adding inspector methods that gives us the hours, the minutes, and the seconds,
  • 1:20 - 1:23
    we now need a way to print out the time object.
  • 1:23 - 1:30
    For this, Python provides a special method. It is called the repr for representation method,
  • 1:30 - 1:35
    the internal method with two underscores before and after and what this thing does is
  • 1:35 - 1:40
    this method is called whenever an object of the particular class is to be printed,
  • 1:40 - 1:44
    and this returns a string representation of the object.
  • 1:44 - 1:50
    So what does it do, we're using the string format method here, which takes a number of arguments,
  • 1:50 - 1:55
    and formats each of these arguments given to the format specification written here in that string.
  • 1:55 - 2:02
    So for instance :2d prints the numerical argument here with two digits--
  • 2:02 - 2:07
    self minutes also comes in two digits, self seconds also comes in two digits.
  • 2:07 - 2:09
    This format is not perfect yet.
  • 2:09 - 2:11
    We actually want leading zeros in here
  • 2:11 - 2:16
    and we want hours, minutes, and seconds separated by colons.
  • 2:16 - 2:22
    So here's a string, first the hours two digits leading zero, then the minutes two digits leading zero,
  • 2:22 - 2:25
    and then the seconds two digits leading zero.
  • 2:25 - 2:27
    Let's try out how this works
  • 2:27 - 2:32
    I'm initializing a time object here 13 hours, 0 minutes, 0 seconds.
  • 2:32 - 2:36
    We're using a 24-hour clock here and I'm printing this out.
  • 2:36 - 2:40
    In printing this out should now invoke this repr method, which would automatically
  • 2:40 - 2:43
    provide a nice string representation of our time.
  • 2:43 - 2:46
    Let's run this whole thing and we get nice representation
  • 2:46 - 2:50
    13 hours, 0 minutes, and 0 seconds, 1 pm in the afternoon.
Title:
Data Invariants - Software Debugging
Video Language:
English
Team:
Udacity
Project:
CS259 - Software Debugging
Duration:
02:53
Udacity Robot edited English subtitles for Data Invariants - Software Debugging
fabio edited English subtitles for Data Invariants - Software Debugging
Amara Bot added a translation

English subtitles

Revisions Compare revisions