Python Tips: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
Python takes some getting used to. The clean syntax is great, but it has some things which a Java or Ruby programmer might not guess. | Python takes some getting used to. The clean syntax is great, but it has some things which a Java or Ruby programmer might not guess. | ||
== Problem with deadline after upgrade == | |||
When starting python you get an IO error: | |||
<geshi lang="bash"> | |||
$ python | |||
Python 2.6.7 (r267:88850, Feb 3 2012, 20:21:47) | |||
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin | |||
Type "help", "copyright", "credits" or "license" for more information. | |||
/Users/brianegge/.pyhistory | |||
Traceback (most recent call last): | |||
File "/Users/brianegge/.pystartup", line 24, in <module> | |||
readline.read_history_file(historyPath) | |||
IOError: [Errno 2] No such file or directory | |||
>>> | |||
</geshi> | |||
Your ~/.pystartup looks like: | |||
<geshi lang="python"> | |||
import atexit | |||
import os | |||
import readline | |||
import rlcompleter | |||
historyPath = os.path.expanduser("~/.pyhistory") | |||
def save_history(historyPath=historyPath): | |||
import readline | |||
readline.write_history_file(historyPath) | |||
if os.path.exists(historyPath): | |||
readline.read_history_file(historyPath) | |||
</geshi> | |||
The solution is to remove your old history file '''rm /Users/brianegge/.pyhistory''' | |||
After you run python again, you will see new magic at the top of your readline file: | |||
<geshi lang="bash"> | |||
cat ~/.pyhistory | |||
_HiStOrY_V2_ | |||
</geshi> | |||
== How to tell if an object is None == | == How to tell if an object is None == |
Latest revision as of 02:33, 4 February 2012
Python takes some getting used to. The clean syntax is great, but it has some things which a Java or Ruby programmer might not guess.
Problem with deadline after upgrade
When starting python you get an IO error: <geshi lang="bash"> $ python Python 2.6.7 (r267:88850, Feb 3 2012, 20:21:47) [GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin Type "help", "copyright", "credits" or "license" for more information. /Users/brianegge/.pyhistory Traceback (most recent call last):
File "/Users/brianegge/.pystartup", line 24, in <module> readline.read_history_file(historyPath)
IOError: [Errno 2] No such file or directory >>> </geshi>
Your ~/.pystartup looks like: <geshi lang="python"> import atexit import os import readline import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
</geshi>
The solution is to remove your old history file rm /Users/brianegge/.pyhistory
After you run python again, you will see new magic at the top of your readline file: <geshi lang="bash"> cat ~/.pyhistory _HiStOrY_V2_ </geshi>
How to tell if an object is None
This won't parse: <geshi lang="python"> user = users.get_current_user() if !user:
gologin()
</geshi>
This works: <geshi lang="python"> if not bool(user):
gologin()
</geshi>
As does: <geshi lang="python"> if user == None:
gologin()
</geshi>
But this is probably best: <geshi lang="python"> if user is None:
gologin()
</geshi>
This article "what exactly is "None"" helped explain this a little better.