diff --git a/humansize.py b/humansize.py index cd92778..2f40c82 100644 --- a/humansize.py +++ b/humansize.py @@ -12,8 +12,8 @@ Examples: """ -SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'), - 1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')} +SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], + 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']} def approximate_size(size, a_kilobyte_is_1024_bytes=True): """Convert a file size to human-readable form. diff --git a/native-datatypes.html b/native-datatypes.html index 0edb74f..7d7582a 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -195,24 +195,28 @@ body{counter-reset:h1 2}
  • If both slice indices are left out, all items of the list are included. But this is not the same as the original a_list variable. It is a new list that happens to have all the same items. a_list[:] is shorthand for making a complete copy of a list.

    Adding items to a list

    -

    Lists are implemented as classes. “Creating” a list is really instantiating a class. As such, a list has methods that operate on it. -

    >>> a_list
    -['a', 'b', 'mpilgrim', 'z', 'example']
    ->>> a_list.append('new')             
    +

    There are four ways to add items to a list. +

    +>>> a_list = ['a']
    +>>> a_list = a_list + [2.0, 3]    
     >>> a_list
    -['a', 'b', 'mpilgrim', 'z', 'example', 'new']
    ->>> a_list.insert(2, 'new')          
    +['a', 2.0, 3]
    +>>> a_list.append(True)           
     >>> a_list
    -['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
    ->>> a_list.extend(['two', 'items'])  
    +['a', 2.0, 3, True]
    +>>> a_list.extend(['four', 'e'])  
     >>> a_list
    -['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'items']
    +['a', 2.0, 3, True, 'four', 'e'] +>>> a_list.insert(1, 'a') +>>> a_list +['a', 'a', 2.0, 3, True, 'four', 'e']
      -
    1. The append() method adds a single item to the end of the list. -
    2. The insert() method inserts a single item into a list. The numeric argument is the index of the first item that gets bumped out of position. List items do not need to be unique; for example, there are now two separate items with the value 'new', a_list[2] and a_list[6]. -
    3. The extend() method concatenates lists. You do not call extend() with multiple arguments; you call it with one argument, a list. In this case, that list has two items. +
    4. The + operator concatenates lists. A list can contain any number of items; there is no size limit (other than available memory). A list can contain items of any datatype; they don't all need to be the same type. Here we have a list containing a string, a floating point number, and an integer. +
    5. The append() method adds a single item to the end of the list. (Now we have four different datatypes in the list!) +
    6. Lists are implemented as classes. “Creating” a list is really instantiating a class. As such, a list has methods that operate on it. The extend() method takes one argument, a list, and appends each of the items of the argument to the original list. +
    7. The insert() method inserts a single item into a list. The first argument is the index of the first item in the list that will get bumped out of position. List items do not need to be unique; for example, there are now two separate items with the value 'a', a_list[0] and a_list[1].
    -

    That last bit is subtle but important. Let's look closer at the difference between extend() and append(). +

    Let's look closer at the difference between append() and extend().

     >>> a_list = ['a', 'b', 'c']
     >>> a_list.extend(['d', 'e', 'f'])  
    @@ -257,207 +261,6 @@ ValueError: list.index(x): x not in list
  • The index() method finds the first occurrence of a value in the list. In this case, 'new' occurs twice in the list, in a_list[2] and a_list[4], but the index() method will return only the index of the first occurrence.
  • As you might not expect, if the value is not found in the list, Python raises an exception. This is notably different from most languages, which will return some invalid index (like -1). While this may seem annoying at first, I think you will come to appreciate it. It means your program will crash at the source of the problem instead of failing strangely and silently later. -

    Sets

    FIXME

    Dictionaries

    @@ -513,21 +316,21 @@ KeyError: 'db.diveintopython3.org' 1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}

    Let's tear that apart in the interactive shell.

    ->>> SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
    -...             1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}
    +>>> SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
    +...             1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}
     >>> len(SUFFIXES)      
     2
     >>> SUFFIXES[1000]     
    -('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
    +['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
     >>> SUFFIXES[1024]     
    -('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
    +['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
     >>> SUFFIXES[1000][3]  
     'TB'
    1. As with lists and sets, the len() function gives you the number of items in a dictionary. -
    2. 1000 is a key in the SUFFIXES dictionary; its value is a tuple of eight items (eight strings, to be precise). -
    3. Similarly, 1024 is a key in the SUFFIXES dictionary; its value is also a tuple of eight items. -
    4. Since SUFFIXES[1000] is a tuple, you can address individual items in the tuple by their 0-based index. +
    5. 1000 is a key in the SUFFIXES dictionary; its value is a list of eight items (eight strings, to be precise). +
    6. Similarly, 1024 is a key in the SUFFIXES dictionary; its value is also a list of eight items. +
    7. Since SUFFIXES[1000] is a list, you can address individual items in the list by their 0-based index.

    None

    None is a special constant in Python. It is a null value. None is not False; it is not 0; it is not an empty string. Comparing None to anything other than None will always return False. @@ -550,7 +353,7 @@ KeyError: 'db.diveintopython3.org' >>> x == y True -

    Further reading

    +

    Further reading