python – intermediate cheatsheet

__repr__ and __str__

Python has two different ways to convert an object to a string: str() and repr(). Printing an object uses str(); printing a list containing an object uses str() for the list itself, but the implementation of list.__str__() calls repr() for the individual items.

So you should also overwrite __repr__(). A simple

__repr__ = __str__

New style class vs old style class

  • low-level constructors named new()

  • descriptors, a generalized way to customize attribute access

  • static methods and class methods

  • properties (computed attributes)

  • decorators (introduced in Python 2.4)

  • slots

  • a new Method Resolution Order (MRO)

now is Class(object): instead of Class():

@property

getter and setter in python

@property -> getter

@property.setter -> setter

good article: http://www.programiz.com/python-programming/property

Read More

LEDs on cubietruck / armboards

It can be configured, it’s just not enabled by default (mostly).

Try entering ls /sys/class/leds/ to check what LEDs are available,

cat /sys/class/leds//trigger to see available triggers, for example

cat /sys/class/leds/white:ph11:led3/trigger

[none] battery-charging-or-full battery-charging battery-full battery-charging-blink-full-solid ac-online usb-online mmc0 mmc1 timer disk-activity heartbeat backlight cpu0 cpu1 default-on rfkill0 rfkill1

and setting them manually, example:

echo “mmc0″ > /sys/class/leds/blue:ph21:led1/trigger

echo “heartbeat” > /sys/class/leds/green:ph07:led4/trigger

echo “cpu0″ > /sys/class/leds/orange:ph20:led2/trigger

echo “cpu1″ > /sys/class/leds/white:ph11:led3/trigger

You need to use root shell to test setting triggers, using sudo from non-privileged user won’t work.

To enable them at every boot, you can edit .fex file (and recompile it to bin afterwards) on old kernel, .dts file with dtc (it’s a little bit tricky) on mainline, or just put echo commands to /etc/rc.local.

Read More

ctags

ctags -R *

Ctrl+] – go to definition

Ctrl+T – Jump back from the definition.

Ctrl+W Ctrl+] – Open the definition in a horizontal split

Add these lines in vimrc

map <C-> :tab split:exec(“tag “.expand(““))

map <A-]> :vsp :exec(“tag “.expand(““))

Ctrl+\ – Open the definition in a new tab

Alt+] – Open the definition in a vertical split

Read More