Python
main
You can use this to detect if a script was imported or ran standalone. An example with two scripts a.y:
and b.py
| main | |
|---|---|
If you run a.py you get :
Note: inside a.py the name is main but once b is imported name becomes b. And so the test will see that and detect it is imported.
Running b.py will give :
Of course there is never a.py involved so it is top level main all along, and the standalone version is detected.
glob
Globbing is pattern matching based on wildcard patterns, a glob is used to refer to the pattern used so : the "glob" to match all header is *.h
Read all html files in the current directory :
| glob | |
|---|---|
dictionary dump
You can print all variables of a dictionary with the aid of the vars function:
| dump dict | |
|---|---|
If also want it pretty printed, you can import pprint and use :
| pretty print | |
|---|---|
mongodb
debian installation
On debian at least, the package python-pymongo is in the aptitude list, but it is sadly broken.
If this little program :
| pymongo | |
|---|---|
Fails with :
| error | |
|---|---|
This is most likely the problem. Uninstall it and reinstall it using pip:
This gives me :
Probably always prefer pip over apt Or use easy_install/eggs (see above)
terse example
programming
lists
These are like arrays, you can fill them directly :
initialization
| lists | |
|---|---|
Or with a range, note that range() already gives a list so this:
| range | |
|---|---|
Would give you :
And this :
| array from range | |
|---|---|
Would result in
| output | |
|---|---|
Also you can use a format string to fill and format the contents :
Result
| output | |
|---|---|
This works like a printf in c, you need to provide as much arguments for % () as there are format codes (like c) and it does not matter if they appear twice(like c). Th ex can then be provided by a for loop or again a range :
Gives :
| output | |
|---|---|
Also you can have named format strings, for example :
| named format string | |
|---|---|
Will substitute the tuple value of key 'hi' and print :
| output | |
|---|---|
manipulating manually
Later..
threading
You can use the threading module for that.
UnboundLocalError: local variable referenced before assignment
This occurs whenever you do something like this :
| UnboundLocalError | |
|---|---|
The problem is that within the function, x = initialized (again) as a local variable, and thus has no value. You can fix it by using the global keyword.
import
importing modules can take a number of forms :
However when you want to use sleep without having to put the module in front of it you will get :
| output | |
|---|---|
To use sleep by itself import time like this :
Note however that you now have only imported sleep !!, for instance time.sleep(2) does not work anymore, because module time is NOT imported anymore. You could do this to get both versions working :
| now you can't use time.sleep() | |
|---|---|
The last way is import 'as' a local name.
Note that in this case, sleep() and time.sleep() are not recognized, only mysleep() and mytime.sleep()
modules
Simply import a file by it's name and use a function :
module.py
caller.py
Or if you want to use modfunc without the module name :
| named import | |
|---|---|
Call :
annotations
You've seen them before as a line starting with @, like in autobahn's exportRpc:
From : http://legacy.python.org/dev/peps/pep-3107/
- function annotations, both for parameters and return values, are completely optional.
- Function annotations are nothing more than a way of associating arbitrary Python expressions with various parts of a function at compile-time. The only way that annotations take on meaning is when they are interpreted by third-party libraries.
- Following from point 2, this PEP makes no attempt to introduce any kind of standard semantics, even for the built-in types. This work will be left to third-party libraries.
So... python defines the syntax but attached no meaning to annotations. In the autobahn example the third party library @exportRpc indeed exports the function below it as an Rpc call to the server. For instance :
| annotations | |
|---|---|
json pretty print
Also possible with standard python install. For instance pretty printing the output from klopt server :
| json pp | |
|---|---|
Or of course
| json pp | |
|---|---|