Python
Generic features and syntax
class.__mro__= Contains all the ancestors of the class, up tillobject, the Python base class_= At Python prompt, it represents the last value. Mostly like$?in Bash__file__= Contains the name of the file loaded (a module if importing or the filename of the script)locals()= Returns a dictionary with the local namespaceglobals()= Returns a dictionary with the global module namespace. Inside a class or function returns the module where this is declared, not where it’s being executedenumerate()= Returns a list’s elements with numeric indexes. For example,0, abc, 1, defetc.[::2]= Slice of a list, iterating every 2 elements. Works also by selecting a range ([1:10:2])[::-1]= Iterate over a list backwardprint("The {foo} is a {bar}".format(foo='answer', bar='42'))= String substitution in print using an alphanumerical indexfirst,*rest,last = (1,2,3,4,5,6,7)= Assign first element of the tuple tofirst, the last tolast, and all those in between torest. Python 3 onlyfirst,second,*rest = 1,2,3,4,5,6= Similarly to above, assign 1 to first, 2 to second and from 3 onward to rest. Rest will result in a list ([3,4,5,6]). Python 3 only__qualname__= Attribute of classes and functions showing the “path” from the module top-level to their definition. Practically shows all the parents functions/classes of a function/class. Only in Python >=3.3python -m pydoc -k keyword= Search Python documentation for “keyword”python -m this= The Zen of Python ;)a[-3]= Negative indexing, returns the third to last element of a listlastthree = slice(-3, None)= Saving a slice in a variable
Decorators
Write a function like this:
def func1(func):
def wrapper():
# code to execute before func()
func()
# code to execute after func()
return wrapper
and then pass to a function this way:
@func1
def func():
# function code
Metaclasses
Syntax:
ClassName = type(
'ClassName',
(object, ),
{'abc': def, 'esse': erre}
)
where ClassName is the name of the metaclass, (object,) is a tuple with all the classes to inherit and the following dictionary is the namespace of the metaclass. To refer to an instance of a metaclasse, use cls instead of self
Then to instantiate a metaclass:
MyClass = ClassName('MyClass', (object, ), {})
or it’s possible to instantiate the metaclass directly when creating a class, like:
class MyClass(object):
__metaclass__ = MyMetaClass
Set operations
various operations possible over sets. Examples:
A = {1, 2, 3, 3,}
B = {3, 4, 5, 6, 7}
A | B->set([1, 2, 3, 4, 5, 6, 7])A & B->set([3])A - B->set([1, 2])B - A->set([4, 5, 6, 7])A ^ B->set([1, 2, 4, 5, 6, 7])
Modules
Bisect
bisect.insort(l, e)= Insertein listl, automatically sorting the list. For huge lists it should be quicker than using.sorted()
Calendar
class HTMLCalendar= Format to html a calendar. After instantiating, use it likei.formatmonth(2012,11)to obtain the html code for november 2012python -m calendar 2013= Outputs the calendar for the 2013. It can be customized to print in html or text format, with or without a css and by width, lines, spacing etc. (seepython -m calendar -h)
Cgitb
cgitb.enable()= Enable a very verbose traceback. With argumentformat="html"the output is html
Collections
class Counter= A class to count the occurrences of a character or integer inside a list, dictionary or string. Returns a dictionary like{'a': 3, 'b': 2, 'c': 2}collections.deque= A different kind of queue, where values can be pushed or popped from both sides and not only one like in normal queuescollections.OrderedDict= A dictionary that remembers the order in which values have been inserted and when iterated upon, returns value in that exact order. Consequently, 2 OrderedDict with the same values but in a different order will not be considered equals, whereas a 2 normal dictionaries would
Named tuples
Point = collections.namedtuple('Point', ['x', 'y'])
p = Point(x=1.0, y=2.0)
Dis
dis.dis(code_object)= Disassemble un code_object
Itertools
Permutations
for p in itertools.permutations([1, 2, 3, 4]):
print(''.join(str(x) for x in p))
prints all the possible combinations
1234
1243
1324
1342
etc.
Json
python -m json.tool < file.json= Pretty JSON printer
Os.Path
os.path.sep= Variable containing the path separator of the current OS (’' or ‘/’)os.path.extsep= Variable containing the filename/extension separator (usually, ‘.’)os.path.curdir= Variable containing the symbol of the current directory (’.’ in Unix-like sytems)os.path.pardir= Variable containing the representation of the parent directory (’..’ in Unix-like systems)
Platform
Module to obtain various informations on the system. For example:
platform.system()= The OS name (like “Linux” or “Windows”)platform.node()= The hostnameplatform.release()= The release number of the OS (on Linux is usually the kernel version)platform.machine()= The kind of hardware of the machine (like ‘i386’)platform.architecture()= The architecture of the machine (nearly always nowadays, 32 or 64bit). Giving a file as argument, returns the architecture of it instead of the machine
Socket
socket.gethostname()= Returns the hostnamesocket.gethostbyname(host)= Returns the IP of hostsocket.gethostbyname_ex(host)= Returns all the IPs and aliases that belong to host
String
string.capwords(s)= Capitalize strings
Sys
sys.getrefcount(object)= Returns the number of times object is used. Includes a spurious copy created bygetrefcountitself. Object can be a variable, a class or a functionsys.getsizeof(object)= Returns how much memory is occupying object. For classes, doesn’t include also all the attributessys.modules= Contains the currently loaded modules (recursively)
Time
time.clock()= Returns the cpu time, the number of clocks from the execution of the current program. Useful to measure performances becauseclock()doesn’t increases during idle time but only when actually running
Timeit
python -m timeit '".".join(str(n) for n in range(100))'= Quick measuring of speed of code snippets