Often times, I need to know what modules are currently installed and available in my python VM. Here are two commands for this…
1. run “help(‘modules’)” within your python interpreter.
This will list all the modules including the standard packages that comes with Python
2. If you need to know only the modules installed by you then you can use the pip for this.
Run “pip freeze” on your command prompt to list all the packages installed by you.
Here is my python version of the bowling game kata with unit tests
Unit Test: http://pastebin.com/g4RW1wUJ
I don’t know if there is an easy or “standard” way of doing this. Did not bother to google and i do so using the sys module in the interpreter…
64 bit ==> 9223372036854775807
32 bit ==> 2147483647
Normally I use my own indexing when looping through list (or any sequence type) as in other languages. In python you can use enumerate function for the same.
Often times we need to check the version of a python library installed. I see there is no standard attribute convention for it. Most often it is __version__ or Version or version or ver or Ver etc., So I use the dir command on the module to find out the exact attribute name for the version.
For example after installing pymongo…
1. Launch python shell
2. import pymongo
This would provide all the methods, functions and attributes of pymongo module
If you know of a better way let me know.
Often times you may notice python codes having function signatures with both *args and **kwargs. This nice article explains how to use these both when calling functions and within function body.
The following steps assumes that you already have python installed. To make your installation easy the steps recommend using easy_install tool.
1. Download and install easy_install tool from cheeseshop .
2. Easy install “zope.interface”
3. Download and install Twisted 10.2.0 from Twisted site
4. Easy install “lxml”
5. Easy install “pyOpenSSL”
6. Easy install “Scrapy”
Test your Scrapy installation
1. Within python interpreter, import the following batteries and there should not be any errors
1. import zope.interface
2. import twisted
3. import lxml
4. import OpenSSL
5. import scrapy
2. If you need to find the version of a particular battery then use module attribute .version or .__version__ as below
print twisted.version ==> [twisted, version 10.2.0]
I just found this trick. Say if you want to split a list with i number of elements into lists with j number of elements. It can easily be done with one liner using the python zip function. You just need to unzip the list specifying the “j”.
See python documentation on zip for more information on this.
People love python because it has solutions built in for problems most frequently faced by developers. Python’s zip is one such thing.
You would most probably need this in scenarios like csv file reading where the first row in the file is list of column names and subsequent are values. You know well that it is better to use map to read/write the values in a row like row[colName] = x or x = row[colName] rather than index like row etc.,
Note: In case of csv file reading/writing, python does have a out-of-the-box solution: DictReader and so you don’t even have to do” even” this one liner 🙂 above…
I’m one of the guy who needs to know the internals of a machine before using it. I can’t help it but that is my very nature. The following article that I refer to is not for faint heart (meaning beginners to programming or python) and is by far the best i’ve found ever that describes the internals of python(at least 2.x) and i’ve to admit that i never ever used python 3 or found a need for using 3.x.
If you are curious about how python types, object or descriptors or attributes or methods work internally then read this .
My recommendation is to save the pdf version since this guy never wrote a book (which would have been an excellent one for experts or intermediates) and not sure how long his/her site will be up.