Python: Difference between revisions
Jump to navigation
Jump to search
(→Shell) |
(→Traps) |
||
Line 39: | Line 39: | ||
More available at http://docs.python.org/2/library/internet.html (see ''BaseHTTPServer'' and ''CGIHTTPServer''). |
More available at http://docs.python.org/2/library/internet.html (see ''BaseHTTPServer'' and ''CGIHTTPServer''). |
||
== Traps == |
|||
Frequent mistakes. Beware the snake can bite you! |
|||
; Confuse a method and a property in a test |
|||
:SOLUTION: Stick to a convention. Like always define methods like <code>isxyyz()</code> or <code>hasabc()</code> as methods. Note that defining them as property would raise an exception if used as a function, and hence might be safer. |
|||
<source lang=python> |
|||
if A.isdummy(): # This will fail isdummy is a property |
|||
if A.isdummy: # Always True if isdummy is a method |
|||
</source> |
|||
; Mix <code>0</code> with <code>None</code> in a sequence |
|||
: Testing whether an element is defined is more difficult. |
|||
<source lang=python> |
|||
a = [0,None,None,None] |
|||
bool(a[0]) # --> False |
|||
bool(a[1]) # --> False !!! How can we tell them apart? |
|||
a[1] == None # --> True This works, but is unusual and likely bad practice |
|||
</source> |
|||
; Mixing property and normal getter |
|||
:SOLUTION: prefix all getter method with <tt>get</tt>, like <code>getvalue()</code> |
|||
<source lang=python> |
|||
b = a.prop # Using a property, OR |
|||
b = a.getprop() # Using a getter |
|||
</source> |
Revision as of 08:42, 8 April 2014
Links
- Nice example of generating / testing regex in Python (with nice / small test framework) [1]
Shell
Use iPy (ipython) to get an interactive shell with auto-completion, instant help...
%magic # Get help on %magic commands (%run,...)
?run # Get help on %run magic
%run script.py # Run given script
%run -i script.py # ... with inspect mode on
%run -i -e script.py # ... ... and ignore sys.exit() call
!cmd # Run shell command 'cmd', for instance ...
!ls # ... List file in current directory
Reference
Basic
for i in range(10):
print i # carriage return
for i in range(10):
print i, # no carriage return
Tips
Simple HTTP Server
It's very easy to setup an ad-hoc HTTP server with Python. Just open a shell in a folder with some contents to share, and type:
python -m SimpleHTTPServer
More available at http://docs.python.org/2/library/internet.html (see BaseHTTPServer and CGIHTTPServer).
Traps
Frequent mistakes. Beware the snake can bite you!
- Confuse a method and a property in a test
- SOLUTION: Stick to a convention. Like always define methods like
isxyyz()
orhasabc()
as methods. Note that defining them as property would raise an exception if used as a function, and hence might be safer.
if A.isdummy(): # This will fail isdummy is a property
if A.isdummy: # Always True if isdummy is a method
- Mix
0
withNone
in a sequence - Testing whether an element is defined is more difficult.
a = [0,None,None,None]
bool(a[0]) # --> False
bool(a[1]) # --> False !!! How can we tell them apart?
a[1] == None # --> True This works, but is unusual and likely bad practice
- Mixing property and normal getter
- SOLUTION: prefix all getter method with get, like
getvalue()
b = a.prop # Using a property, OR
b = a.getprop() # Using a getter