Python libraries cheatsheet

These are the libraries that came pre-imported with my most recent project. In order to understand what is available to me, I’m going through and writing a fast summary of what it does in a way I understand. Comments welcome if I am inaccurate. It’s a first stab at this, I am 100% confident there will be room for expansion. Consider this a rough draft that I’m publishing anyway.

CGI : A support module for Common Gateway Interface scripts. These CGI scripts are invoked by an HTTP server, usually to process data that a user submitted through a HTML form. The HTTP server puts a whole bunch o’ info in the CGI script, the script runs, and it returns a bunch of info back to the client. This library helps organize this info for Python apps, as well as providing debugging tools (which I haven’t looked into yet). The output of a CGI script is two sections, the first, being about what sort of data is coming, and the second, the data itself.

Inspect: Gives you tools to look at live objects. You can look at contents of a class, find the source code of a method, extract and format the arguments of a function (super helpful!), look at the interpreter stack, and much more.

Operator: To me, this seems like a great way to shorthand intrinsic Python functions. Instead of, for example, having to remember how to write a modulo operator in python, you can say:

operator.__mod__(a, b)

That will return the same as if you had typed

a % b

Upfront, maybe it looks like more typing, but it’s actually less over the long term if you have a program with a lot of math and/or true/false comparisions, because you don’t have to be typing in mathematical or boolean operations all the dang time.

OS: come back to this one

Time: Yay! Time! Provides time-related functions. Remember Y2K? There’s provisions for that in here :)

Types: This gives you names for standard Python types, but not those that are from other libraries. It seems weird and unnecessary to me at first, however, its great for methods that need to examine the type of the data you’re using as method arguments, and do things to the data based on its type. For example:

from types import *
def delete (users, email):
if type(email) is IntType:
del users[email]

sys: come back to this one

json: Allows you to convert python objects to Json. I imported two functions from this library as well, loads and dumps.

Dumps serializes (encodes) an object into a JSON formatted string using a conversion table. It looks a lot like a hash when its done. Essential, dumps turns Python to Json…

JSON conversion table for dumps

Loads does the opposite, taking a JSON formatted string and turning it into Python objects. Essentially it turns JSON into Python. It does so using this table:

JSON conversion table for loads

copy: used to create, well, copies of objects. there’s shallow and deep, shallow makes a copy of the object, but if the object that has been copied is a container, all its “containments” will still point to the original object. With a deep copy, however, all objects inside of it are copied as well, recursively. The O’Reilly online book has a good example:

urlparse: This is the good stuff. It breaks up url strings into components!! Some are scheme, port, path, params, and a few others. It’s amazing. I highly recommend playing around with it in your terminal. Here’s a brief sampling of me doing so in my terminal. Notice how so much information is intrinsically stored now, and I can use it later.

I also used parse_qs, which parses a query string as a string and is returned as a dictionary. Parse_qsl parses a query string and returns name, value pairs. Come back to this in a bit.

Urlencode: This lives in the Urllib library, and it encodes Urls. Why? Because if you’re access a remote API, any path parameter or query string needs to be properly encoded. This is an awesome tutorial I found, it uses Python 3.

HTTPClient: this lives in the geventhttpclient library, and it *PARSES HTTP*, both requests and responses. It also allows for concurrent (multiple) HTTP requests to be sent to the server I’m building at once. I cannot stress enough to go to the README of the software and do the examples as well as mess around with it!

My biggest takeaway from the project overall is the importance of taking the time upfront to understand the libraries you’re given to work with and to spend time playing with them in your terminal, as well as doing your best to understand what it does, not just what it means. Of course, there has to be a balance between *doing* and researching, but it’s important to put both in the mix. Spending time in the libraries has increased my Python literacy more than stumbling totally in the dark, as well as keeping me motivated and excited by showing me cool tools available to me. I don’t think it necessarily will eliminate confusion as I’m writing, but I think having a basic knowledge of the libraries can take away *unnecessary* confusion. As for myself, I will dedicate time to delving into the given libraries in a program to the very beginning of my process.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store