Batteries Included: Things you can do with standard libraries

🏦

Imagine you are working in a bank...

Everything is highly secure, you cannot:

- install anything without permission

- limited access for internet

- no phone, no laptops

- only given python 3.6

Make the best out of the 

 

Standard Libraries

But what are they?

Standard libraries comes with official Python installation

1) built-in modules (written in C) that provide access to system functionality

e.g. sys, os

 

2) modules that are written in Python that provide standardized solutions for many problems

e.g. json, math, datetime

create a virtual environment?

Pyenv Virtualenv

python3 -m venv tutorial-env
source tutorial-env/bin/activate

What I can do if I want to...

check a csv file?

pandas

csv

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))

What I can do if I want to...

work with a sql database?

SQLAlchemy

import sqlite3
con = sqlite3.connect('example.db')
cur = con.cursor()
cur.execute('''CREATE TABLE stocks
               (date text, trans text, symbol text, qty real, price real)''')
cur.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
con.commit()
con.close()

What I can do if I want to...

make an API call?

requests

>>> import urllib.request
>>> import urllib.parse
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
>>> with urllib.request.urlopen(url) as f:
...     print(f.read().decode('utf-8'))

What I can do if I want to...

urllib is quite powerful

write unit tests?

pytest

class MyTestCase(unittest.TestCase):
    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")
    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        pass
    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        pass

What I can do if I want to...

Other hidden gems

Pretty printer in Python that makes checking complex dictionary output possible

[(0, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (1, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (2, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'})]
[(0,
  {'a': 'A',
   'b': 'B',
   'c': 'C',
   'd': 'D',
   'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H'}),
 (1,
  {'a': 'A',
   'b': 'B',
   'c': 'C',
   'd': 'D',
   'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H'}),
 (2,
  {'a': 'A',
   'b': 'B',
   'c': 'C',
   'd': 'D',
   'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H'})]

You can also format the output

Run the Python code in your docstrings (or text file)

$ python example.py -v
Trying:
    factorial(5)
Expecting:
    120
ok
Trying:
    [factorial(n) for n in range(6)]
Expecting:
    [1, 1, 2, 6, 24, 120]
ok
Trying:
    factorial(1e100)
Expecting:
    Traceback (most recent call last):
        ...
    OverflowError: n too large
ok
2 items passed all tests:
   1 tests in __main__
   8 tests in __main__.factorial
9 tests in 2 items.
9 passed and 0 failed.
Test passed.
$

Python debugger, much better than using `print` all the time

Adding the following at the point of checking

import pdb; pdb.set_trace()

pdb

> <string>(0)?()
(Pdb) continue
> <string>(1)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)

Interactive debugging

Turtle graphics is a popular way for introducing programming to kids.

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

Other stuff that you may have used before

timeit (time the process),

subprocess (run shell stuff),

xml (r/w/xml files),

asyncio (async),

importlib (dynamic import a module),

re (regex),

filecmp (compare files)

Other stuff I want to try

GUI tool

tkinter

Parallelism

threading, multiprocessing

Which one is your favourite?

Batteries Included: Things you can do with standard libraries

By Cheuk Ting Ho

Batteries Included: Things you can do with standard libraries

  • 113