Python Tips: Difference between revisions

From EggeWiki
m (New page: 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. == How to tell if an object is None == This won't...)
 
mNo edit summary
 
(One intermediate revision by one other user not shown)
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 ==
Line 10: Line 51:
</geshi>
</geshi>


This parses but doesn't work:
This works:
<geshi lang="python">
if not bool(user):
    gologin()
</geshi>
 
As does:
<geshi lang="python">
<geshi lang="python">
user = users.get_current_user()
if user == None:
if not user:
     gologin()
     gologin()
</geshi>
</geshi>


This works!
But this is probably best:
<geshi lang="python">
<geshi lang="python">
user = users.get_current_user()
if user is None:
if user is None:
     gologin()
     gologin()
</geshi>
</geshi>
This article "[http://mail.python.org/pipermail/python-list/2003-March/192769.html what exactly is "None"]" helped explain this a little better.
[[Category:Python]]

Latest revision as of 22:33, 3 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.