import batteries

The modules you never knew you needed

Ivana Kellyerova | June 8, 2018

Batteries Included

The Python standard library can do anything.

abstrusegoose.com/81 (license)

Okay, maybe not anything.

The Noteworthy

(for all sorts of reasons)

keyword

Determine if a string is a Python keyword.


            >>> import keyword

            >>> keyword.iskeyword('djangopony')
            False
            >>> keyword.iskeyword('while')
            True

            >>> keyword.kwlist
            ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
            'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
            'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
            'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
            'while', 'with', 'yield']
          

... that's it.

tabnanny

Check files for ambiguous indentation.


            >>> import tabnanny

            >>> tabnanny.check('input/perfect_python_file.py')
            >>> # ^ Internally raises tabnanny.NannyNag
          

turtle

Simple visual programming language.

docs.python.org/3/library/turtle.html


            $ python3 -m turtle
          

webbrowser

Your partner for all your browser-opening needs.


            >>> import webbrowser

            >>> webbrowser.open('https://www.amarion.net')
            True
          

sndhdr

Determine what kind of data there is in a sound file.

            >>> import sndhdr

            >>> # We like descriptive function names
            >>> sndhdr.what('path/to/music/file.mp3')
          

calendar
General calendar-related functions

colorsys
RGB, HSV, HLS, YIQ color conversion

cmd
Build your custom interactive shell

aifc
Read and write AIFF and AIFC files

doctest
Test code examples in docstrings

html
HTML manipulation

sunau
Support for the Sun AU sound format

telnet
Telnet client

zipfile
Read and write ZIP files

binhex4
Encode and decode binhex4

dis
Disassemble Python bytecode

grp
Support for Unix groups

mailbox
Manipulate mailboxes in various formats

Also Noteworthy

(this time for different reasons)

2to3

Automatic Python 2 to 3 code translation.


            def greet(name):
                print "Hello, {0}!".format(name)
                print "What's your name?"
                name = raw_input()
                greet(name)
          

            $ 2to3 example.py
          

            def greet(name):
                print("Hello, {0}!".format(name))
                print("What's your name?")
                name = input()
                greet(name)
          

builtins

Access builtin objects.


            >>> open
            <built-in function open>

            >>> def open(args):
            ...     do_something()

            >>> open
            <function open at 0x7f1216cfa950>
            >>> # Hmm, can't use the builtin open anymore
          

            >>> import builtins

            >>> with builtins.open('filename', 'r'):
            ...     do_something_else()
          

textwrap

Paragraph wrapping & indentation management.


            >>> import textwrap

            >>> long_string = """\
            ...     This is a long paragraph spanning multiple lines.
            ...     A very good candidate for a triple-quoted string.
            ... """
            >>> long_string
            "    This is a long paragraph spanning multiple lines.\n    A very good
            candidate for a triple-quoted string.\n"
          

textwrap


            >>> # Get rid of leading whitespace
            >>> long_string = textwrap.dedent(long_string)
            >>> long_string
            "This is a long paragraph spanning multiple lines.\nA very good
            candidate for a triple-quoted string.\n"
          

            >>> # Wrap to a specific number of characters
            >>> textwrap.fill(long_string, width=40)
            "This is a long paragraph spanning\nmultiple lines. A very good
            candidate\nfor a triple-quoted string.\n"
          

            >>> # Cut at the last possible word boundary to fill width
            >>> textwrap.shorten(long_string, width=10, placeholder='...')
            'This is...'
          

timeit


            $ python3 -m timeit '"-".join(str(n) for n in range(100))'
            10000 loops, best of 3: 30.2 usec per loop
            $ python3 -m timeit '"-".join([str(n) for n in range(100)])'
            10000 loops, best of 3: 27.5 usec per loop
            $ python3 -m timeit '"-".join(map(str, range(100)))'
            10000 loops, best of 3: 23.2 usec per loop
          

            >>> import timeit
            >>> timeit.timeit('"-".join(str(n) for n in range(100))',
            >>> number=10000)
            0.3018611848820001
            >>> timeit.timeit('"-".join([str(n) for n in range(100)])',
            >>> number=10000)
            0.2727368790656328
            >>> timeit.timeit('"-".join(map(str, range(100)))', number=10000)
            0.23702679807320237
          

pprint

String representation for humans.


            >>> games = {'aaa': {'fps': ['Counter Strike', 'Unreal Tournament'],
            ... 'rpg': ['Dragon Age', 'Mass Effect', 'Neverwinter Nights']},
            ... 'oldies': {'pointnclick': ['Legend of Kyrandia'],
            ... 'rpg': {'dungeon': ['Lands of Lore']}}, 'indie': {'stealth':
            ... ['Mark of the Ninja', 'Invisible Inc.'], 'roguelike': ['FTL',
            ... 'One More Dungeon'], 'puzzle': ['Braid'], 'action':
            ... ['Transistor', 'Bastion'], 'platformers': ['Hollow Knight', 'Eets
            ... Munchies'], 'dungeon': {'puzzle': ['Legend of Grimrock'],
            ... 'rhythm': ['Crypt of the Necrodancer']}}}
          

pprint

String representation for humans.


            >>> from pprint import pprint

            >>> pprint(games)
            {'aaa': {'fps': ['Counter Strike', 'Unreal Tournament'],
                     'rpg': ['Dragon Age', 'Mass Effect', 'Neverwinter Nights']},
             'indie': {'action': ['Transistor', 'Bastion'],
                       'dungeon': {'puzzle': ['Legend of Grimrock'],
                                   'rhythm': ['Crypt of the Necrodancer']},
                       'platformer': ['Hollow Knight', 'Eets Munchies'],
                       'puzzle': ['Braid'],
                       'roguelike': ['FTL', 'One More Dungeon'],
                       'stealth': ['Mark of the Ninja', 'Invisible Inc.']},
             'oldies': {'pointnclick': ['Legend of Kyrandia'],
                        'rpg': {'dungeon': ['Lands of Lore']}}}
          

itertools

Advanced iterators.

            >>> import itertools

            >>> seq1 = 'Today is Friday.'
            >>> seq2 = [42, 47, 12, 9, 4, 0, 64]
          

            >>> # Chain iterables
            >>> for elem in itertools.chain(seq1, seq2):
            ...     print(elem, end=' ')
            T o d a y   i s   F r i d a y . 42 47 12 9 4 0 64
          

            >>> list(itertools.dropwhile(lambda x: x > 30, seq2))
            [12, 9, 4, 0, 64]
          

            >>> [''.join(x) for x in itertools.permutations('abc', 2)]
            ['ab', 'ac', 'ba', 'bc', 'ca', 'cb']
          

functools

Higher order functions.


            >>> import functools

            >>> functools.reduce(
            ...     lambda x, y: x + y,
            ...     ['lab', 'rad', 'oo', 'dle']
            ... )
            'labradoodle'
          

            >>> print_word = functools.partial(print, end='')
            >>> for i in range(4):
            ...     print_word('cous')
            couscouscouscous
          

traceback

Extract, format, print stack traces.


            >>> def frobnicate():
            ...     foobarify()

            >>> def foobarify():
            ...     bazificate()

            >>> def bazificate():
            ...     import traceback
            ...     traceback.print_stack()

            >>> frobnicate()
            File "<stdin>", line 1, in <module>
            File "<stdin>", line 2, in frobnicate
            File "<stdin>", line 2, in foobarify
            File "<stdin>", line 3, in bazificate
          

bisect

Efficient searching and maintenance of sorted lists.


          >>> import bisect

          >>> seq = [1, 4, 67, 89, 123, 200, 345, 678, 1274]

          >>> # Use binary search to find out where an element is located:
          >>> bisect.bisect_left(seq, 67)
          2
          >>> # Insert an element, keeping seq sorted:
          >>> bisect.insort(seq, 470)
          >>> seq
          [1, 4, 67, 89, 123, 200, 345, 470, 678, 1274]
          

collections

Practical data structures.


          >>> import collections

          >>> counter = collections.Counter(['a', 'b', 'c', 'b', 'c', 'b'])
          >>> counter
          Counter({'b': 3, 'c': 2, 'a': 1})  # a dict with extra features

          >>> counter['c']
          2
          >>> counter['notthere']  # 0 for any nonexistent value
          0
          

collections

Practical data structures.


          >>> releases = {'Braid': '2008', 'Hollow Knight': '2017'}
          >>> releases['Pyre']
          Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
          KeyError: 'Pyre'

          >>> releases = collections.defaultdict(lambda: 'n/a')
          >>> releases['Braid'] = '2008'
          >>> releases['Hollow Knight'] = '2017'

          >>> releases['Pyre']
          'n/a'
          

The Python standard library can do almost anything.

bacon


          >>> import bacon     # ???
          

abstrusegoose.com/81 (license)

For the rest, there is the Python Package Index.

Twitter | @aerinthenniel

GitLab | jenx

GitHub | Eruraina

References