<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>python wiki</title>
<link>http://pythonwiki.tiddlyspot.com</link>
<description>notes on learning the language</description>
<language>en-us</language>
<copyright>Copyright 2010 WarpCat</copyright>
<pubDate>Mon, 15 Mar 2010 21:52:09 GMT</pubDate>
<lastBuildDate>Mon, 15 Mar 2010 21:52:09 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>TiddlyWiki 2.4.0</generator>
<item>
<title>Working with xml and ElementTree</title>
<description>As an alternative to &lt;code&gt;xml.dom.minidom&lt;/code&gt; (since it seems so clunky), notes on &lt;code&gt;ElementTree&lt;/code&gt;.&lt;br&gt;Docs:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/xml.etree.elementtree.html&quot; href=&quot;http://docs.python.org/library/xml.etree.elementtree.html&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/xml.etree.elementtree.html&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://effbot.org/zone/element.htm&quot; href=&quot;http://effbot.org/zone/element.htm&quot; class=&quot;externalLink&quot;&gt;http://effbot.org/zone/element.htm&lt;/a&gt; (nice tutorials, go read this now)&lt;/li&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html&quot; href=&quot;http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html&quot; class=&quot;externalLink&quot;&gt;http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;import xml.etree.ElementTree as ET
&lt;/pre&gt;&lt;strong&gt;Examples:&lt;/strong&gt;&lt;br&gt;&lt;hr&gt;Search some xml on disk called foo.xml&lt;br&gt;In it, find the first child element of the root called &quot;firstTagElement&quot;&lt;br&gt;Under that, find all elements (could be more than one) that are called &quot;subTagElement&quot;&lt;br&gt;If none of those sub-elements have the text defined by &lt;code&gt;searchText&lt;/code&gt;, then add a new sub-element, with that text.&lt;br&gt;And save to disk (and print)&lt;br&gt;&lt;pre&gt;import xml.etree.ElementTree as ET

xmlPath = &quot;c:/temp/foo.xml&quot;
searchText = &quot;addMe&quot;

noMatch = 1
tree = ET.parse(xmlPath)
root = tree.getroot()
firstTagElement = root.find(&quot;firstTagElement&quot;)

for subElement in firstTagElement.findall(&quot;subTagElement&quot;):
    if subElement.text == searchText:
        noMatch = 0
        break
if noMatch:
    element = ET.SubElement(firstTagElement ,&quot;subTagElement&quot;)
    element.text = searchText
    tree.write(xmlPath, &quot;UTF-8&quot;)
    # or:
    print ET.tostring(root)   
&lt;/pre&gt;The xml being searched (before modification) could look like this:&lt;br&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding='UTF-8'?&amp;gt;
&amp;lt;root&amp;gt;
  &amp;lt;firstTagElement&amp;gt;
    &amp;lt;subTagElement&amp;gt;some text&amp;lt;/subTagElement&amp;gt;
    &amp;lt;subTagElement&amp;gt;some more text&amp;lt;/subTagElement&amp;gt;
  &amp;lt;/firstTagElement&amp;gt;
&amp;lt;/root&amp;gt;
&lt;/pre&gt;After modification and save:&lt;br&gt;(Actually, this is a lie, it's formatting will be screwed up.  Please see my notes here: &lt;a tiddlylink=&quot;Poorly formatted xml problems&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#Poorly formatted xml problems&quot; href=&quot;http://pythonwiki.tiddlyspot.com#Poorly%20formatted%20xml%20problems&quot; class=&quot;externalLink&quot;&gt;Poorly formatted xml problems&lt;/a&gt;)&lt;br&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding='UTF-8'?&amp;gt;
&amp;lt;root&amp;gt;
  &amp;lt;firstTagElement&amp;gt;
    &amp;lt;subTagElement&amp;gt;some text&amp;lt;/subTagElement&amp;gt;
    &amp;lt;subTagElement&amp;gt;some more text&amp;lt;/subTagElement&amp;gt;
    &amp;lt;subTagElement&amp;gt;add me&amp;lt;/subTagElement&amp;gt;
  &amp;lt;/firstTagElement&amp;gt;
&amp;lt;/root&amp;gt;
&lt;/pre&gt;&lt;hr&gt;</description>
<category>XML</category>
<category>xml</category>
<category>xml.etree</category>
<category>xml.etree.ElementTree</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BWorking%20with%20xml%20and%20ElementTree%5D%5D</link>
<pubDate>Mon, 15 Mar 2010 21:52:06 GMT</pubDate>
</item>
<item>
<title>string type comparisons</title>
<description>I often do type checks against strings, and do things if strings are found:&lt;br&gt;&lt;pre&gt;if type(myVar) == type(&quot;&quot;):
    # do something
&lt;/pre&gt;However, there are different types of strings, an the above example can easily fail:&lt;br&gt;&lt;pre&gt;print type(u&quot;&quot;)
print type(r&quot;&quot;)
print type(&quot;&quot;)

&amp;lt;type 'unicode'&amp;gt;
&amp;lt;type 'str'&amp;gt;
&amp;lt;type 'str'&amp;gt;
&lt;/pre&gt;To solve for this, we can compare against the superclass of all strings, &lt;code&gt;basestring&lt;/code&gt;:&lt;br&gt;&lt;pre&gt;if issubclass(type(myVar), basestring):
    # do stuff
&lt;/pre&gt;</description>
<category>STRING</category>
<category>unicode</category>
<category>raw</category>
<category>basestring</category>
<category>issublcass</category>
<category>type</category>
<category>comparison</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5Bstring%20type%20comparisons%5D%5D</link>
<pubDate>Thu, 04 Mar 2010 20:05:00 GMT</pubDate>
</item>
<item>
<title>How can I split a string by multiple split characters?</title>
<description>Pythons built in string method &lt;code&gt;split()&lt;/code&gt; (&lt;code&gt;&quot;string&quot;.split(&quot;chars&quot;)&lt;/code&gt;) works good for splitting a string based on a given sequence of characters.  For example:&lt;br&gt;&lt;pre&gt;print &quot;split|me:please&quot;.split(&quot;|&quot;)
# ['split', 'me:please']
&lt;/pre&gt;But what if you wanted to split that line by both the pipe (&lt;code&gt;|&lt;/code&gt;) and the colon (&lt;code&gt;:&lt;/code&gt;)?  This would fail:&lt;br&gt;&lt;pre&gt;print &quot;split|me:please&quot;.split(&quot;|:&quot;)
# ['split|me:please']
&lt;/pre&gt;It fails because Python can't find an exact match of the string '&lt;code&gt;|:&lt;/code&gt;'.  &lt;br&gt;&lt;br&gt;Here are several solutions using &lt;code&gt;re&lt;/code&gt; that works nicely:  (I found this info &lt;a target=&quot;_blank&quot; title=&quot;External link to http://bytes.com/topic/python/answers/716054-s-split-multiple-separators&quot; href=&quot;http://bytes.com/topic/python/answers/716054-s-split-multiple-separators&quot; class=&quot;externalLink&quot;&gt;here&lt;/a&gt;) &lt;br&gt;&lt;pre&gt;import re
word = &quot;split|me:please&quot;
reComp = re.compile('[|:]')
print reComp.split(word) 
print re.split('[|:]', word)
print re.split('[|:]+', word)
print re.findall('[^|:]+', word)
&lt;/pre&gt;All instances print:&lt;br&gt;&lt;pre&gt;['split', 'me', 'please']
&lt;/pre&gt;&lt;hr&gt;Older function I wrote that works around the issue, kept for prosperity:&lt;br&gt;&lt;pre&gt;def charSplit(string, chars):
    &quot;&quot;&quot;
    string : The string in question to split
    chars : list : list of strings to chop string up by
    &quot;&quot;&quot;
    splitted = [string]

    for c in chars:
        newSplit = []
        for s in splitted:
            chopped = s.split(c)
            try:
                chopped.remove(&quot;&quot;)
            except ValueError:
                pass
            newSplit = newSplit + chopped
        splitted = newSplit
        
    return splitted
&lt;/pre&gt;&lt;pre&gt;name = &quot;namespace:one|namespace:two|namespace:three&quot;
splitted = charSplit(name, list(&quot;:|&quot;))
print splitted
&lt;/pre&gt;&lt;pre&gt;['namespace', 'one', 'namespace', 'two', 'namespace', 'three']
&lt;/pre&gt;By letting you pass in a list, you can still try to separate by multiple-character strings:&lt;br&gt;&lt;pre&gt;charSplit(name, [&quot;|&quot;, &quot;&amp;lt;&amp;gt;&quot;, &quot;@@&quot;])
&lt;/pre&gt;</description>
<category>STRING</category>
<category>string.split</category>
<category>re</category>
<category>re.compile</category>
<category>re.split</category>
<category>re.findall</category>
<category>split</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20split%20a%20string%20by%20multiple%20split%20characters%3F%5D%5D</link>
<pubDate>Fri, 19 Feb 2010 21:53:00 GMT</pubDate>
</item>
<item>
<title>How can I execute a system command, and capture the return?</title>
<description>Python 2.6 and &lt;em&gt;newer&lt;/em&gt; (&lt;code&gt;os.popen&lt;/code&gt; is deprecated). Use &lt;code&gt;subprocess&lt;/code&gt;:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/subprocess.html&quot; href=&quot;http://docs.python.org/library/subprocess.html&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/subprocess.html&lt;/a&gt;&lt;br&gt;&lt;pre&gt;import subprocess
&lt;/pre&gt;&lt;hr&gt;New example, taken from &lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.logilab.org/blogentry/20469&quot; href=&quot;http://www.logilab.org/blogentry/20469&quot; class=&quot;externalLink&quot;&gt;this post&lt;/a&gt;:&lt;br&gt;&lt;blockquote&gt;&quot;&lt;code&gt;subprocess.Popen&lt;/code&gt; takes a list of arguments as first parameter, which are passed as-is to the new process creation system call of your platform, and not interpreted by the shell:&quot;&lt;br&gt;&lt;pre&gt;pipe = subprocess.Popen(['diff', '-u', appl_file, ref_file], stdout=subprocess.PIPE)
output = pipe.stdout
&lt;/pre&gt;&quot;By now, you should have guessed that the shell=True parameter of subprocess.Popen should not be used unless you really really need it (and even them, I encourage you to question that need).&quot;&lt;br&gt;&lt;/blockquote&gt;&lt;hr&gt;A couple different ways:&lt;br&gt;Version A, using communicate()&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate&quot; href=&quot;http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate&lt;/a&gt;&lt;br&gt;&lt;pre&gt;result = subprocess.Popen(&quot;system comands here&quot;, stdout=subprocess.PIPE).communicate()
foo = [f.strip() for f in result[0].split(&quot;\r\n&quot;) if f != &quot;&quot;]
for f in foo:
    print f
&lt;/pre&gt;&lt;code&gt;communicate()&lt;/code&gt; returns a 2-index tuple: Index[0] is the stdout stream, and index[1] is the stdin stream.  We use a &lt;code&gt;list comprehension&lt;/code&gt; to extract each of the lines as a list:&lt;br&gt;Accessing index[0] of the tuple (which is a string) we split that by return &amp;amp; newline chars into a list.  For each item in that list, if the item &lt;em&gt;isn't&lt;/em&gt; an empty string, we strip off any other tabs and whatnot, and pass the result into our final list &lt;code&gt;foo&lt;/code&gt;.&lt;br&gt;&lt;hr&gt;Version B, using .stdout&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/subprocess.html#subprocess.Popen.stdout&quot; href=&quot;http://docs.python.org/library/subprocess.html#subprocess.Popen.stdout&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/subprocess.html#subprocess.Popen.stdout&lt;/a&gt;&lt;br&gt;&lt;pre&gt;result = subprocess.Popen(&quot;system comands here&quot;, stdout=subprocess.PIPE).stdout
foo = [f.strip() for f in result.readlines() if f.strip() != &quot;&quot;]
for f in foo:
    print f
&lt;/pre&gt;This is similar to version A, but we're not using &lt;code&gt;communicate()&lt;/code&gt;, and access the &lt;code&gt;stdout&lt;/code&gt; stream directly.  This returns a &lt;code&gt;file&lt;/code&gt; object with the &lt;code&gt;stdout&lt;/code&gt; data we care about, which we call &lt;code&gt;readlines()&lt;/code&gt; method on, giving us a list, which we then clean up and return into the &lt;code&gt;foo&lt;/code&gt; list via the &lt;code&gt;list comprehension&lt;/code&gt;.&lt;br&gt;&lt;hr&gt;Sometimes, depending on what the system command is, you need to pass in the args &lt;code&gt;shell=True&lt;/code&gt;:&lt;br&gt;&lt;pre&gt;# result as a tuple, (stdout, stdin):
result = subprocess.Popen(&quot;dir c:\\&quot;, 
                          stdout=subprocess.PIPE, 
                          shell=True).communicate()

# result as a file object:
result = subprocess.Popen(&quot;dir c:\\&quot;, 
                          stdout=subprocess.PIPE, 
                          shell=True).stdout
&lt;/pre&gt;You also need to do this to launch certain external apps.  If you don't care about capturing any return, it's a bit less code:&lt;br&gt;&lt;pre&gt;# launch notepad:
subprocess.Popen(&quot;start notepad&quot;, shell=True)
&lt;/pre&gt;&lt;hr&gt;I've also ran into instance where I'd get this kind of error reported back:&lt;br&gt;&lt;pre&gt;WindowsError: (6, 'The handle is invalid')
&lt;/pre&gt;If this is the case, I found the fix was to also pass in a pipe to the &lt;code&gt;stdin&lt;/code&gt;&lt;br&gt;&lt;pre&gt;result = subprocess.Popen(&quot;dir c:\\&quot;, 
                          stdout=subprocess.PIPE, 
                          stdin=subprocess.PIPE, 
                          shell=True).communicate()
&lt;/pre&gt;&lt;hr&gt;&lt;em&gt;previous&lt;/em&gt; to Python2.6:&lt;br&gt;&lt;pre&gt;import os

results =  os.popen(&quot;system commands here&quot;).read().strip()  
# returns list
# strip() is optional, but useful
&lt;/pre&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/os.html#os.popen&quot; href=&quot;http://docs.python.org/library/os.html#os.popen&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/os.html#os.popen&lt;/a&gt;</description>
<category>FILESYSTEMS</category>
<category>EXECUTION</category>
<category>EXTERNAL APPS</category>
<category>os</category>
<category>os.popen</category>
<category>commands</category>
<category>string.strip</category>
<category>file.read</category>
<category>subprocess</category>
<category>subprocess.Popen</category>
<category>subprocess.Popen.communicate</category>
<category>notepad</category>
<category>launch</category>
<category>start</category>
<category>WindowsError</category>
<category>error 6</category>
<category>The handle is invalid</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20execute%20a%20system%20command%2C%20and%20capture%20the%20return%3F%5D%5D</link>
<pubDate>Fri, 12 Feb 2010 16:16:00 GMT</pubDate>
</item>
<item>
<title>List info</title>
<description>Rather than just retype it all, might as well use the actual docs ;)&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/lib/typesseq-mutable.html&quot; href=&quot;http://docs.python.org/lib/typesseq-mutable.html&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/lib/typesseq-mutable.html&lt;/a&gt; 'Mutable Sequence Types'&lt;br&gt;and&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/tut/node7.html&quot; href=&quot;http://docs.python.org/tut/node7.html&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/tut/node7.html&lt;/a&gt; 'Data Structures'&lt;br&gt;&lt;strong&gt;Hilights&lt;/strong&gt;:&lt;br&gt;&lt;ul&gt;&lt;li&gt;List methods (also see &lt;a tiddlylink=&quot;List methods&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#List methods&quot; href=&quot;http://pythonwiki.tiddlyspot.com#List%20methods&quot; class=&quot;externalLink&quot;&gt;List methods&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;Lists as Stacks&lt;/li&gt;&lt;li&gt;Using Lists as Queues&lt;/li&gt;&lt;li&gt;Functional Programming Tools:&lt;ul&gt;&lt;li&gt;Filter&lt;/li&gt;&lt;li&gt;Map&lt;/li&gt;&lt;li&gt;Reduce&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;List Comprehensions&lt;/li&gt;&lt;li&gt;The del statement&lt;/li&gt;&lt;li&gt;Tuples and Sequences&lt;/li&gt;&lt;li&gt;Sets&lt;/li&gt;&lt;li&gt;Dictionaries&lt;/li&gt;&lt;li&gt;Looping Techniques&lt;/li&gt;&lt;li&gt;Comparing Sequences and Other Types&lt;/li&gt;&lt;/ul&gt;&lt;strong&gt;Tips &amp;amp; Tricks:&lt;/strong&gt;&lt;br&gt;&lt;hr&gt;How can I remove duplicates from a list?&lt;br&gt;&lt;span tiddler=&quot;How can I remove duplicates from a list?&quot; refresh=&quot;content&quot;&gt;&lt;pre&gt;seq = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]
seq = list(set(seq))
print seq
# ['a', 'c', 'b']
&lt;/pre&gt;By using &lt;code&gt;set&lt;/code&gt;s (which can't hold duplicate values), you can prune out the dupes.&lt;/span&gt;&lt;br&gt;&lt;hr&gt;If you want to make a &lt;strong&gt;copy of a list&lt;/strong&gt;, this is how you &lt;em&gt;shouldn't&lt;/em&gt; do it:&lt;br&gt;&lt;pre&gt;listA = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]
listB = listA
if listA is listB:
    print &quot;true!&quot;
# true!
&lt;/pre&gt;In this case, the variable name &lt;code&gt;listA&lt;/code&gt; and &lt;code&gt;listB&lt;/code&gt; are both poting to the exact same place in memory, which holds the data &lt;code&gt;[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]&lt;/code&gt;.  This is proven by using the &lt;code&gt;is&lt;/code&gt; test in the above example.&lt;br&gt;To properly make a copy, you can grab a slice, which happens to be the whole list:&lt;br&gt;&lt;pre&gt;listA = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]
listB = listA[:]
&lt;/pre&gt;If you want to make a &lt;em&gt;reversed&lt;/em&gt; copy:&lt;br&gt;&lt;pre&gt;listB = listA[::-1]
&lt;/pre&gt;&lt;hr&gt;&lt;pre&gt;a = [1, 2, 3]
b = [4, 5, 6]
&lt;/pre&gt;Add lists:&lt;br&gt;&lt;pre&gt;c = a + b
print c
# [1, 2, 3, 4, 5, 6]
&lt;/pre&gt;Zip lists (returns list of tuples):&lt;br&gt;&lt;pre&gt;d = zip(a,b)
print d
[(1, 4), (2, 5), (3, 6)]
&lt;/pre&gt;Map lists (using lambda).  &lt;br&gt;Will multiply each element of list a against list b:&lt;br&gt;&lt;pre&gt;e = map(lambda x, y: x*y, a,b)
print e
# [4, 10, 18]
&lt;/pre&gt;&lt;hr&gt;Full copy:&lt;br&gt;&lt;pre&gt;copy.deepcopy(X)
list(L)
&lt;/pre&gt;Insert items at front of list L:&lt;br&gt;&lt;pre&gt;L[:0] = [X,Y,Z,]
&lt;/pre&gt;Insert multiple items at the end of list L, in-place:&lt;br&gt;&lt;pre&gt;L[len(L):] = [X,Y,Z]
L.extend([X,Y,Z]
L += [ X,Y,Z]
&lt;/pre&gt;Implement in-place stack operations, where the end of the list is the top of the stack:&lt;br&gt;&lt;pre&gt;L.append(X)
X=L.pop()
&lt;/pre&gt;Indexing:&lt;br&gt;&lt;pre&gt;# first item in the list:
S[0]
# last item in the list:
S[-1]
# slice list from offsets 1-3:
S[1:4]
# slice everything after the first
S[1:]
# slice everything but the last:
S[:-1]
# Make a copy of list
S[:]
&lt;/pre&gt;</description>
<category>VARIABLES</category>
<category>LIST</category>
<category>listMethod</category>
<category>del</category>
<category>list comprehension</category>
<category>set</category>
<category>dictionary</category>
<category>filter</category>
<category>map</category>
<category>reduce</category>
<category>stack</category>
<category>queue</category>
<category>loop</category>
<category>tupple</category>
<category>sequence</category>
<category>zip</category>
<category>lambda</category>
<category>reverse</category>
<category>reversed</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BList%20info%5D%5D</link>
<pubDate>Tue, 09 Feb 2010 18:32:00 GMT</pubDate>
</item>
<item>
<title>Accessing Windows data</title>
<description>You need:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://sourceforge.net/projects/pywin32/&quot; href=&quot;http://sourceforge.net/projects/pywin32/&quot; class=&quot;externalLink&quot;&gt;pywin32&lt;/a&gt;, also known as 'Python for windows extensions'&lt;/li&gt;&lt;/ul&gt;You probably want:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://timgolden.me.uk/python/winshell.html&quot; href=&quot;http://timgolden.me.uk/python/winshell.html&quot; class=&quot;externalLink&quot;&gt;winshell&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;hr&gt;Nice blog on how to make Windows shortcuts:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/&quot; href=&quot;http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/&quot; class=&quot;externalLink&quot;&gt;http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/&lt;/a&gt;&lt;br&gt;&lt;hr&gt;Recipe and blog post on gathering Windows information (that actually &lt;em&gt;doesn't&lt;/em&gt; require the above modules)&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://code.activestate.com/recipes/511491/&quot; href=&quot;http://code.activestate.com/recipes/511491/&quot; class=&quot;externalLink&quot;&gt;http://code.activestate.com/recipes/511491/&lt;/a&gt;&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.blog.pythonlibrary.org/2010/01/27/getting-windows-system-information-with-python/&quot; href=&quot;http://www.blog.pythonlibrary.org/2010/01/27/getting-windows-system-information-with-python/&quot; class=&quot;externalLink&quot;&gt;http://www.blog.pythonlibrary.org/2010/01/27/getting-windows-system-information-with-python/&lt;/a&gt;&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.blog.pythonlibrary.org/2010/02/06/more-windows-system-information-with-python/&quot; href=&quot;http://www.blog.pythonlibrary.org/2010/02/06/more-windows-system-information-with-python/&quot; class=&quot;externalLink&quot;&gt;http://www.blog.pythonlibrary.org/2010/02/06/more-windows-system-information-with-python/&lt;/a&gt;&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/&quot; href=&quot;http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/&quot; class=&quot;externalLink&quot;&gt;http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/&lt;/a&gt; (more registry stuff)&lt;br&gt;&lt;hr&gt;&lt;br&gt;</description>
<category>FILESYSTEMS</category>
<category>INFO</category>
<category>windows</category>
<category>pywin32</category>
<category>winshell</category>
<category>shortcut</category>
<category>registry</category>
<category>cpu</category>
<category>ram</category>
<category>memory</category>
<category>drive space</category>
<category>drive size</category>
<category>version</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BAccessing%20Windows%20data%5D%5D</link>
<pubDate>Mon, 08 Feb 2010 16:47:00 GMT</pubDate>
</item>
<item>
<title>list comprehension</title>
<description>In addition to sequence operations and list methods, you can &lt;em&gt;really&lt;/em&gt; try and confuse yourself with list comprehensions ;)  It is said they can run up to twice as fast as a 'for loop'... so if speed is your thing, check them out...&lt;br&gt;List comprehensions take lists, and create new lists from them.  They are enclosed in square brackets: &lt;code&gt;[]&lt;/code&gt;&lt;br&gt;Python docs:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/tutorial/datastructures.html#list-comprehensions&quot; href=&quot;http://docs.python.org/tutorial/datastructures.html#list-comprehensions&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/tutorial/datastructures.html#list-comprehensions&lt;/a&gt;&lt;br&gt;(See notes on &lt;strong&gt;generator expressions&lt;/strong&gt; at the bottom)&lt;br&gt;&lt;hr&gt;Given two list of numbers (of the same length, in this case they're vectors), add each index from each list toether:&lt;br&gt;&lt;pre&gt;vec1 = [1,0,0]
vec2 = [0,1,0]
sum = [vec1[i] + vec2[i] for i in range(len(vec1))]
# [1, 1, 0]
&lt;/pre&gt;&lt;hr&gt;Some examples:  (I pulled a bunch of notes from 'Learning Python Third Edition by Mark Lutz')&lt;br&gt;&lt;pre&gt;# make a 3x3 matrix :
mx = [[1,2,3], [4,5,6], [7,8,9]]
# get just the middle column from the matrix:
col2 = [row[1] for row in mx]
print col2
&amp;gt;&amp;gt;&amp;gt; [2, 5, 8]
&lt;/pre&gt;&lt;pre&gt;# add a value to the middle column (but don't actually modify the matrix)
print [row[1] + 1 for row in mx]
&amp;gt;&amp;gt;&amp;gt; [3, 6, 9]
&lt;/pre&gt;&lt;pre&gt;# get only even numbers from the middle column:
print [row[1] for row in mx if row[1] % 2 == 0]
&amp;gt;&amp;gt;&amp;gt; [2, 8]
&lt;/pre&gt;&lt;pre&gt;# pull diagonal values:
diagonal = [mx[i][i] for i in [0,1,2]]
print diagonal
&amp;gt;&amp;gt;&amp;gt; [1, 5, 9]
&lt;/pre&gt;&lt;pre&gt;# Repeat chars in a string
word = &quot;foo&quot;
double = [i * 2 for i in word]
print double
&amp;gt;&amp;gt;&amp;gt; ['ff', 'oo', 'oo']
&lt;/pre&gt;&lt;pre&gt;# Comparing list comprehensions, and for loops:

# List Comprehension:
squares = [i ** 2 for i in range(1,6)]
print squares
&amp;gt;&amp;gt;&amp;gt; [1, 4, 9, 16, 25]

# For Loop equivalent:
squares = []
for i in range(1,6):
    squares.append(i**2)
print squares
&amp;gt;&amp;gt;&amp;gt; [1, 4, 9, 16, 25]
&lt;/pre&gt;&lt;hr&gt;Turn a list into a grouping of sublists.&lt;br&gt;Here, we take a list of 16 items (from a matrix), and turn it into a list with four sub-lists, each with four items.&lt;br&gt;(Pulled from &lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.garyrobinson.net/2008/04/splitting-a-pyt.html&quot; href=&quot;http://www.garyrobinson.net/2008/04/splitting-a-pyt.html&quot; class=&quot;externalLink&quot;&gt;this post&lt;/a&gt;)&lt;br&gt;&lt;pre&gt;mtxList = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
size=4
mtxQuad = [mtxLst[i:i+size] for i  in range(0, len(mtxLst), size)]
print mtxQuad
# [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
&lt;/pre&gt;&lt;hr&gt;&lt;pre&gt;# return number, and cube root of number 1-5:
cubeRoots = [[x, x ** 3] for x in range(1,6)]
print cubeRoots
&amp;gt;&amp;gt;&amp;gt; [[1, 1], [2, 8], [3, 27], [4, 64], [5, 125]]
&lt;/pre&gt;&lt;hr&gt;More examples I pulled directly from the &lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/tut/node7.html#SECTION007140000000000000000&quot; href=&quot;http://docs.python.org/tut/node7.html#SECTION007140000000000000000&quot; class=&quot;externalLink&quot;&gt;Python reference&lt;/a&gt;:&lt;br&gt;As you can see, you can also inlude &lt;code&gt;if&lt;/code&gt; clauses in the list comprehension for testing:&lt;br&gt;&lt;pre&gt;# strip whitespace:
freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
print [weapon.strip() for weapon in freshfruit]
&amp;gt;&amp;gt;&amp;gt; ['banana', 'loganberry', 'passion fruit']
&lt;/pre&gt;&lt;pre&gt;vec = [2, 4, 6]

print [3*x for x in vec]
&amp;gt;&amp;gt;&amp;gt; [6, 12, 18]
print [3*x for x in vec if x &amp;gt; 3]
&amp;gt;&amp;gt;&amp;gt; [12, 18]
print [3*x for x in vec if x &amp;lt; 2]
&amp;gt;&amp;gt;&amp;gt; []
print [[x,x**2] for x in vec]
&amp;gt;&amp;gt;&amp;gt; [[2, 4], [4, 16], [6, 36]]
print [(x, x**2) for x in vec]
&amp;gt;&amp;gt;&amp;gt; [(2, 4), (4, 16), (6, 36)]
vec1 = [2, 4, 6]
vec2 = [4, 3, -9]
print [x*y for x in vec1 for y in vec2]
&amp;gt;&amp;gt;&amp;gt; [8, 6, -18, 16, 12, -36, 24, 18, -54]
print [x+y for x in vec1 for y in vec2]
&amp;gt;&amp;gt;&amp;gt; [6, 5, -7, 8, 7, -5, 10, 9, -3]
print [vec1[i]*vec2[i] for i in range(len(vec1))]
&amp;gt;&amp;gt;&amp;gt; [8, 12, -54]
&lt;/pre&gt;&lt;hr&gt;Working on multiple lists at once:&lt;br&gt;&lt;pre&gt;A = ['a0', 'a1']
B = ['b0', 'b1']
C = ['c0', 'c1']

D = [(a,b,c) for a in A for b in B for c in C ]

for d in D:
    print d
&lt;/pre&gt;prints:&lt;br&gt;&lt;pre&gt;('a0', 'b0', 'c0')
('a0', 'b0', 'c1')
('a0', 'b1', 'c0')
('a0', 'b1', 'c1')
('a1', 'b0', 'c0')
('a1', 'b0', 'c1')
('a1', 'b1', 'c0')
('a1', 'b1', 'c1')
&lt;/pre&gt;&lt;br&gt;&lt;hr&gt;While there doesn't seem to be such thing as a '&lt;code&gt;tuple comprehension&lt;/code&gt;', you can turn any list comprehension into a &lt;code&gt;tuple&lt;/code&gt;:&lt;br&gt;&lt;pre&gt;foo = tuple([3*x for x in vec])
# (6, 12, 18)
&lt;/pre&gt;I've seen examples of this:&lt;br&gt;&lt;pre&gt;foo = tuple(3*x for x in vec)
# (6, 12, 18)
&lt;/pre&gt;Which creates a &lt;em&gt;generator expressions&lt;/em&gt; (see below), and turns that into a &lt;code&gt;tuple&lt;/code&gt;, but I've read it's actually slower that way.&lt;br&gt;&lt;hr&gt;You can also do a mashup of list comprehensions and &lt;a tiddlylink=&quot;Function&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#Function&quot; href=&quot;http://pythonwiki.tiddlyspot.com#Function&quot; class=&quot;externalLink&quot;&gt;generator functions&lt;/a&gt; to make &lt;strong&gt;'generator expressions&lt;/strong&gt;'.  They act like list comprehensions, but are surrounded in parenthesis instead of square brackets.&lt;br&gt;&lt;pre&gt;genEx = (x * 2 for x in [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])
print genEx.next()
# aa
print genEx.next()
# bb
# etc...
&lt;/pre&gt;They raise a &lt;code&gt;StopIteration&lt;/code&gt; exception when complete.&lt;br&gt;I read that generator expressions are known to save on memory, since they don't require the entire list to be constructed at once, but can actually run slower in practice, so should really only be used on large data sets.&lt;br&gt;See the &lt;a tiddlylink=&quot;Function&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#Function&quot; href=&quot;http://pythonwiki.tiddlyspot.com#Function&quot; class=&quot;externalLink&quot;&gt;generator function&lt;/a&gt; notes for more info on their methods.</description>
<category>LIST</category>
<category>list comprehension</category>
<category>generator expression</category>
<category>generator</category>
<category>tuple</category>
<category>tuple comprehension</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5Blist%20comprehension%5D%5D</link>
<pubDate>Thu, 28 Jan 2010 17:12:00 GMT</pubDate>
</item>
<item>
<title>Object concepts</title>
<description>Nice overview on &lt;a target=&quot;_blank&quot; title=&quot;External link to http://effbot.org/zone/python-objects.htm&quot; href=&quot;http://effbot.org/zone/python-objects.htm&quot; class=&quot;externalLink&quot;&gt;effbot.org&lt;/a&gt;&lt;br&gt;&lt;hr&gt;All objects have three main data-points that describe them:&lt;br&gt;&lt;ul&gt;&lt;li&gt;identity:  Its location in memory, accessed via &lt;code&gt;id()&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;type: The representation of the object to Python, accessed via &lt;code&gt;type()&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;value:  The data (attributes) stored inside, accessed via &lt;code&gt;dir()&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;# Make a 'string' object assigned to the 'spam' variable:
spam = &quot;eggs&quot;
print &quot;spam id:&quot;, id(spam)
print &quot;spam type:&quot;, type(spam)
print &quot;spam attributes:&quot;
for d in dir(spam):
    print &quot;\t&quot;,d
&lt;/pre&gt;Prints:&lt;br&gt;&lt;pre&gt;spam id: 50854480
spam type: &amp;lt;type 'str'&amp;gt;
spam attributes:
	__add__
	__class__
	__contains__
	__delattr__
        #... many, many more....
&lt;/pre&gt;</description>
<category>OOP</category>
<category>INFO</category>
<category>object</category>
<category>id</category>
<category>identity</category>
<category>type</category>
<category>dir</category>
<category>attribute</category>
<category>memory</category>
<category>memory location</category>
<category>representation</category>
<category>data</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BObject%20concepts%5D%5D</link>
<pubDate>Tue, 26 Jan 2010 19:46:00 GMT</pubDate>
</item>
<item>
<title>Accessing the Windows clipboard</title>
<description>If &lt;code&gt;win32clipboard&lt;/code&gt; isn't part of your standard install, you can download it here as part of the 'pywin32' extension for Windows here:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://sourceforge.net/projects/pywin32/&quot; href=&quot;http://sourceforge.net/projects/pywin32/&quot; class=&quot;externalLink&quot;&gt;http://sourceforge.net/projects/pywin32/&lt;/a&gt;&lt;br&gt;It seems to come standard with the ActiveState 'ActivePython' distribution here, if you use that distib:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.activestate.com/activepython/&quot; href=&quot;http://www.activestate.com/activepython/&quot; class=&quot;externalLink&quot;&gt;http://www.activestate.com/activepython/&lt;/a&gt;&lt;br&gt;&lt;hr&gt;&lt;br&gt;From:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://snippets.dzone.com/posts/show/724&quot; href=&quot;http://snippets.dzone.com/posts/show/724&quot; class=&quot;externalLink&quot;&gt;http://snippets.dzone.com/posts/show/724&lt;/a&gt;&lt;br&gt;&lt;pre&gt;import win32clipboard as w 
import win32con

def getText(): 
    w.OpenClipboard() 
    d=w.GetClipboardData(win32con.CF_TEXT) 
    w.CloseClipboard() 
    return d 
 
def setText(aType,aString): 
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(aType,aString) 
    w.CloseClipboard()
&lt;/pre&gt;From:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://mail.python.org/pipermail/python-list/2007-June/617808.html&quot; href=&quot;http://mail.python.org/pipermail/python-list/2007-June/617808.html&quot; class=&quot;externalLink&quot;&gt;http://mail.python.org/pipermail/python-list/2007-June/617808.html&lt;/a&gt;&lt;br&gt;&lt;pre&gt;from win32clipboard import *
OpenClipboard()
EmptyClipboard()
SetClipboardText(&quot;Hello from Python!&quot;)
CloseClipboard()
&lt;/pre&gt;&lt;hr&gt;And here's a function I came up with for pasting to the clipboard:&lt;br&gt;&lt;pre&gt;import win32clipboard as w32c
def pasteToClipboard(stuff):
    &quot;&quot;&quot;
    Paste the passed in data to the windows clipboard
    
    stuff : string or list : data to paste to clipboard
    &quot;&quot;&quot;
    if type(stuff) != type([]):
        stuff = [stuff]
    paste = '\n'.join(stuff)
    
    w32c.OpenClipboard()
    w32c.EmptyClipboard()
    w32c.SetClipboardText(paste)
    w32c.CloseClipboard()
&lt;/pre&gt;</description>
<category>FILESYSTEMS</category>
<category>clipboard</category>
<category>copy</category>
<category>paste</category>
<category>win32clipboard</category>
<category>win32con</category>
<category>pywin32</category>
<category>windows</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BAccessing%20the%20Windows%20clipboard%5D%5D</link>
<pubDate>Tue, 26 Jan 2010 16:31:00 GMT</pubDate>
</item>
<item>
<title>How can I sort by my own rules?</title>
<description>I recently had a list of axis vector names (see example below) that had both positive and negative names.  When I'd sort the list, it would place the negative names first, since the minus character sorts before the alpha-numeric ones.  The &lt;code&gt;sorted&lt;/code&gt; function, or the list method &lt;code&gt;sorted&lt;/code&gt; both accept a &lt;code&gt;cmp&lt;/code&gt; argument that you can use to do your own sort rules.  In my case, I wanted to make sure that the minus '-' characters got sorted after alpha-numeric ones.&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/functions.html#sorted&quot; href=&quot;http://docs.python.org/library/functions.html#sorted&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/functions.html#sorted&lt;/a&gt;&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/tutorial/datastructures.html#more-on-lists&quot; href=&quot;http://docs.python.org/tutorial/datastructures.html#more-on-lists&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/tutorial/datastructures.html#more-on-lists&lt;/a&gt;&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/library/stdtypes.html#mutable-sequence-types&quot; href=&quot;http://docs.python.org/library/stdtypes.html#mutable-sequence-types&quot; class=&quot;externalLink&quot;&gt;http://docs.python.org/library/stdtypes.html#mutable-sequence-types&lt;/a&gt;&lt;br&gt;&lt;hr&gt;As you can see, just sorting it doesn't do what I want:&lt;br&gt;&lt;pre&gt;print sorted(['x', 'y', 'z', '-x', '-y', '-z'])
# ['-x', '-y', '-z', 'x', 'y', 'z']
&lt;/pre&gt;So, we make a sort rule:&lt;br&gt;&lt;pre&gt;# My cmp tester:
def negTest(x,y):
    if '-' in x and '-' not in y:
        return 0
    else:
        return 1
&lt;/pre&gt;Using the list &lt;code&gt;sort&lt;/code&gt; method, which changes the list in-place:&lt;br&gt;&lt;pre&gt;s.sort(cmp=negTest)
print s
# ['x', 'y', 'z', '-x', '-y', '-z']
&lt;/pre&gt;Or using the &lt;code&gt;sorted&lt;/code&gt; function, which creates a new list:&lt;br&gt;&lt;pre&gt;newS = sorted(s, cmp=negTest)
print newS 
# ['x', 'y', 'z', '-x', '-y', '-z']
&lt;/pre&gt;You can also use a &lt;code&gt;lambda&lt;/code&gt; for the &lt;code&gt;cmp&lt;/code&gt; arg, but my brain wasn't working well enough to make this happen.</description>
<category>LIST</category>
<category>sort</category>
<category>sorted</category>
<category>list.sort</category>
<category>cmp</category>
<category>lambda</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20sort%20by%20my%20own%20rules%3F%5D%5D</link>
<pubDate>Mon, 25 Jan 2010 17:15:00 GMT</pubDate>
</item>
<item>
<title>How can I launch an external application?</title>
<description>&lt;pre&gt;# Windows only:
import os
os.startfile(&quot;notepad&quot;)
&lt;/pre&gt;&lt;hr&gt;Also see: &lt;a tiddlylink=&quot;How can I execute a system command, and capture the return?&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#How can I execute a system command, and capture the return?&quot; href=&quot;http://pythonwiki.tiddlyspot.com#How%20can%20I%20execute%20a%20system%20command,%20and%20capture%20the%20return?&quot; class=&quot;externalLink&quot;&gt;How can I execute a system command, and capture the return?&lt;/a&gt;</description>
<category>EXTERNAL APPS</category>
<category>FILESYSTEMS</category>
<category>EXECUTION</category>
<category>launch</category>
<category>execute</category>
<category>os</category>
<category>os.startfile</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20launch%20an%20external%20application%3F%5D%5D</link>
<pubDate>Fri, 22 Jan 2010 00:38:00 GMT</pubDate>
</item>
<item>
<title>Python IDE's</title>
<description>&lt;h1&gt;Wing:&lt;/h1&gt;Personal favorite, see my subject &lt;a tiddlylink=&quot;Wing IDE&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#Wing IDE&quot; href=&quot;http://pythonwiki.tiddlyspot.com#Wing%20IDE&quot; class=&quot;externalLink&quot;&gt;Wing IDE&lt;/a&gt;&lt;br&gt;&lt;h1&gt;Eclipse:&lt;/h1&gt;Very popular in the Java world, but does Python too.&lt;br&gt;&lt;ul&gt;&lt;li&gt;Main page:  &lt;a target=&quot;_blank&quot; title=&quot;External link to http://eclipse.org/&quot; href=&quot;http://eclipse.org/&quot; class=&quot;externalLink&quot;&gt;http://eclipse.org/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;And for Python support, requires Pydev plugin:  &lt;a target=&quot;_blank&quot; title=&quot;External link to http://pydev.org/&quot; href=&quot;http://pydev.org/&quot; class=&quot;externalLink&quot;&gt;http://pydev.org/&lt;/a&gt;&lt;ul&gt;&lt;li&gt;Pydev blog:  &lt;a target=&quot;_blank&quot; title=&quot;External link to http://pydev.blogspot.com/&quot; href=&quot;http://pydev.blogspot.com/&quot; class=&quot;externalLink&quot;&gt;http://pydev.blogspot.com/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Pydev supports development for Python, &lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.jython.org/&quot; href=&quot;http://www.jython.org/&quot; class=&quot;externalLink&quot;&gt;Jython&lt;/a&gt;, and &lt;a target=&quot;_blank&quot; title=&quot;External link to http://ironpython.net/&quot; href=&quot;http://ironpython.net/&quot; class=&quot;externalLink&quot;&gt;IronPython&lt;/a&gt; (.NET)&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;PyScripter&lt;/h1&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://code.google.com/p/pyscripter/&quot; href=&quot;http://code.google.com/p/pyscripter/&quot; class=&quot;externalLink&quot;&gt;http://code.google.com/p/pyscripter/&lt;/a&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;&quot;PyScripter is a free and open-source Python Integrated Development Environment (IDE) created with the ambition to become competitive in functionality with commercial Windows-based IDEs available for other languages. Being built in a compiled language is rather snappier than some of the other Python IDEs and provides an extensive blend of features that make it a productive Python development environment.&quot;&lt;/li&gt;&lt;/ul&gt;</description>
<category>UI</category>
<category>ide</category>
<category>eclipse</category>
<category>pydev</category>
<category>wing</category>
<category>jython</category>
<category>ironpython</category>
<category>.net</category>
<category>pyscripter</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BPython%20IDE's%5D%5D</link>
<pubDate>Wed, 20 Jan 2010 17:00:00 GMT</pubDate>
</item>
<item>
<title>Perforce access via Python</title>
<description>&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.perforce.com/&quot; href=&quot;http://www.perforce.com/&quot; class=&quot;externalLink&quot;&gt;http://www.perforce.com/&lt;/a&gt;&lt;br&gt;Perforce is version control software that I use in games development.  Having my animation package Maya (which supports Python scripting) interface with it is really handy.  What resources exist to let Python talk to it?	&lt;br&gt;&lt;hr&gt;They release their installers by version, by os:&lt;br&gt;Windows Installer (Python 2.5):&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to ftp://ftp.perforce.com/perforce/r07.3/bin.ntx86/p4python25.exe&quot; href=&quot;ftp://ftp.perforce.com/perforce/r07.3/bin.ntx86/p4python25.exe&quot; class=&quot;externalLink&quot;&gt;ftp://ftp.perforce.com/perforce/r07.3/bin.ntx86/p4python25.exe&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;Source:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to ftp://ftp.perforce.com/perforce/r07.3/tools/p4python.tgz&quot; href=&quot;ftp://ftp.perforce.com/perforce/r07.3/tools/p4python.tgz&quot; class=&quot;externalLink&quot;&gt;ftp://ftp.perforce.com/perforce/r07.3/tools/p4python.tgz&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;Here's the root installer download page, to grab the latest version:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to ftp://ftp.perforce.com/perforce/&quot; href=&quot;ftp://ftp.perforce.com/perforce/&quot; class=&quot;externalLink&quot;&gt;ftp://ftp.perforce.com/perforce/&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;Documentation:&lt;br&gt;&lt;ul&gt;&lt;li&gt;Main Page:  &lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.perforce.com/perforce/technical.html&quot; href=&quot;http://www.perforce.com/perforce/technical.html&quot; class=&quot;externalLink&quot;&gt;http://www.perforce.com/perforce/technical.html&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Python specific HTML: &lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373&quot; href=&quot;http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373&quot; class=&quot;externalLink&quot;&gt;http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Multi-language .pdf:  &lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf&quot; href=&quot;http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf&quot; class=&quot;externalLink&quot;&gt;http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;The install appears to stick the modules here:&lt;br&gt;&lt;pre&gt;C:\Python25\Lib\site-packages\

P4.py
p4.pyc
p4.pyo
P4API.pyd
P4Python-2007.3-py2.5.egg-info
&lt;/pre&gt;Some example code modified from their manual (they had... mistakes...)&lt;br&gt;&lt;pre&gt;from P4 import P4, P4Exception

p4 = P4()
p4.port = &quot;myPort:1666&quot;
p4.user = &quot;myName&quot;
p4.client = &quot;myClient&quot;
try:
	p4.connect()
	info = p4.run(&quot;info&quot;) # returns a list
	d = info[0]  # extract the dictionary
	for key in d.keys():   # print our 'info'
		print key + &quot;  :  &quot; + str(d[key])
	p4.disconnect()
except P4Exception:
	for e in p4.errors:
		print e
&lt;/pre&gt;&lt;hr&gt;&lt;strong&gt;Notes:&lt;/strong&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;It appears that most of P4's commands are wrappered in methods that start with &lt;code&gt;run_&lt;/code&gt;, or can be called to via the &lt;code&gt;run()&lt;/code&gt; method (as in the example above):&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;info = p4.run(&quot;info&quot;)
info = p4.run_info()
# Both appear to do the same thing. 
&lt;/pre&gt;&lt;pre&gt;# Both do the same thing, but sometimes run takes extra args:
files = p4.run_edit([r&quot;c:\myFile.txt&quot;, r&quot;c:\myFileB.txt])
files = p4.run(&quot;edit&quot;, [r&quot;c:\myFile.txt&quot;, r&quot;c:\myFileB.txt])
&lt;/pre&gt;&lt;br&gt;</description>
<category>EXTERNAL APPS</category>
<category>Perforce</category>
<category>P4</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BPerforce%20access%20via%20Python%5D%5D</link>
<pubDate>Tue, 19 Jan 2010 16:45:00 GMT</pubDate>
</item>
<item>
<title>How can I get a list of files from a directory, based on a wildcard?</title>
<description>&lt;pre&gt;import glob
glob.glob(&quot;c:\\temp\\*.txt&quot;)
&lt;/pre&gt;</description>
<category>FILESYSTEMS</category>
<category>*</category>
<category>glob</category>
<category>glob.glob</category>
<category>directory</category>
<category>file</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20get%20a%20list%20of%20files%20from%20a%20directory%2C%20based%20on%20a%20wildcard%3F%5D%5D</link>
<pubDate>Mon, 18 Jan 2010 17:28:00 GMT</pubDate>
</item>
<item>
<title>How can I find empty directories?</title>
<description>You could look at this fine blog pose here:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://codeboje.de/findingemptydirectories/&quot; href=&quot;http://codeboje.de/findingemptydirectories/&quot; class=&quot;externalLink&quot;&gt;http://codeboje.de/findingemptydirectories/&lt;/a&gt;&lt;br&gt;Reposted here for prosperity.  &lt;span class=&quot;marked&quot;&gt;Thanks Kerim!&lt;/span&gt;&lt;br&gt;Verison A:&lt;br&gt;&lt;pre&gt;import os
from os.path import join, getsize
for root, dirs, files in os.walk('c:\\'):
    if len(dirs)==0 and len(files)==0:
        print root
&lt;/pre&gt;Better version:&lt;br&gt;&lt;pre&gt;import os
from os.path import join, isfile

def walksub(dir):
    isEmpty=True
    subDirs=[]
    for entry in os.listdir(dir):
        if isfile(join(dir,entry))==True:
            isEmpty = False
        else:
            subEmpty =  walksub(join(dir, entry))
            if subEmpty==True:
                subDirs.append(join(dir, entry))
            else:
                isEmpty=False

    if isEmpty == False:
        for subDir in subDirs:
            print subDir
    return isEmpty
&lt;/pre&gt;&lt;pre&gt;walksub('c:\\')
&lt;/pre&gt;</description>
<category>FILESYSTEMS</category>
<category>directory</category>
<category>empty</category>
<category>os</category>
<category>os.path</category>
<category>os.path.join</category>
<category>os.path.getsize</category>
<category>os.listdir</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20find%20empty%20directories%3F%5D%5D</link>
<pubDate>Mon, 18 Jan 2010 16:50:00 GMT</pubDate>
</item>
<item>
<title>Finding a packages modules</title>
<description>Given a &lt;a tiddlylink=&quot;Packages&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#Packages&quot; href=&quot;http://pythonwiki.tiddlyspot.com#Packages&quot; class=&quot;externalLink&quot;&gt;package&lt;/a&gt; named &lt;code&gt;myPackage&lt;/code&gt; setup like so:&lt;br&gt;&lt;ul&gt;&lt;li&gt;/pythonStuff (in Python path)&lt;ul&gt;&lt;li&gt;/myPackage&lt;ul&gt;&lt;li&gt;/&lt;code&gt;__init__.py&lt;/code&gt;&lt;/li&gt;&lt;li&gt;/&lt;code&gt;moduleA.py&lt;/code&gt;&lt;/li&gt;&lt;li&gt;/&lt;code&gt;moduleB.py&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;How can you query the &lt;a tiddlylink=&quot;module&quot; refresh=&quot;link&quot; target=&quot;_blank&quot; title=&quot;External link to http://pythonwiki.tiddlyspot.com#module&quot; href=&quot;http://pythonwiki.tiddlyspot.com#module&quot; class=&quot;externalLink&quot;&gt;module&lt;/a&gt;s that live in &lt;code&gt;myPackage&lt;/code&gt;?&lt;br&gt;&lt;h3&gt;Solution A:&lt;/h3&gt;Use the tools Python provides you:&lt;br&gt;&lt;pre&gt;# testA.py
import pkgutil
import myPackage

modules = []
for p in pkgutil.iter_modules(myPackage.__path__):
    modules.append(p[1])
print modules
&lt;/pre&gt;prints:&lt;br&gt;&lt;pre&gt;['moduleA', 'moduleB']
&lt;/pre&gt;After I wrote this, I found a blog post here doing something similar:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://jeetworks.org/node/49&quot; href=&quot;http://jeetworks.org/node/49&quot; class=&quot;externalLink&quot;&gt;http://jeetworks.org/node/49&lt;/a&gt;&lt;br&gt;&lt;h3&gt;Solution B:&lt;/h3&gt;Roll your own.  I actually came up with this solution first (with help from the Python Tutor email list), before I found out about &lt;code&gt;pkgutil.iter_modules()&lt;/code&gt;.&lt;br&gt;What we do is auto-populate the &lt;code&gt;__all__&lt;/code&gt; attr in the package's &lt;code&gt;__init__.py&lt;/code&gt; module.  This is a special attribute that is designed to store what package modules are visible to the outside world.  Normally these are authored by hand.&lt;br&gt;See docs on the package &lt;code&gt;__all__&lt;/code&gt; attr &lt;a target=&quot;_blank&quot; title=&quot;External link to http://docs.python.org/tutorial/modules.html#importing-from-a-package&quot; href=&quot;http://docs.python.org/tutorial/modules.html#importing-from-a-package&quot; class=&quot;externalLink&quot;&gt;here&lt;/a&gt;&lt;br&gt;&lt;pre&gt;# /myPackage/__init__.py
import os
import glob

# Fill our __all__ attr with the modules currently living in this package:
__all__ = []

packageDir = os.path.split(__file__)[0]
modules = glob.glob( os.path.join(packageDir, &quot;*.py&quot;) )

for m in modules:
    justModule = os.path.splitext(os.path.split(m)[-1])[0]
    # Don't add itself to the list:
    if justModule != '__init__':
        __all__.append(justModule)

&lt;/pre&gt;Then to test:&lt;br&gt;&lt;pre&gt;# testB.py
import myPackage

print myPackage.__all__
&lt;/pre&gt;prints:&lt;br&gt;&lt;pre&gt;['moduleA', 'moduleB']
&lt;/pre&gt;</description>
<category>INFO</category>
<category>__all__</category>
<category>packages</category>
<category>pkgutil</category>
<category>pkgutil.iter_modules</category>
<category>module</category>
<category>os</category>
<category>os.path</category>
<category>os.path.split</category>
<category>os.path.splitext</category>
<category>os.path.join</category>
<category>glob</category>
<category>glob.glob</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BFinding%20a%20packages%20modules%5D%5D</link>
<pubDate>Mon, 18 Jan 2010 16:46:00 GMT</pubDate>
</item>
<item>
<title>How can I find the name of a variable?</title>
<description>This is something I'd been trying to figure out for a long time:  Given a variable that points to an object, how can you find the string that is the variable name?  Not that I have any application for this, but I like to see if things are possible.  Based on all my question asking on the forums and whatnot, the answer was always &quot;no, you can't do this&quot;.  Well, based on the below blog post, it looks like you can ;)&lt;br&gt;&lt;br&gt;The one danger is that it uses &lt;code&gt;sys._getframe()&lt;/code&gt;, which as you can tell by the leading underscore is considered private, and could change with future versions of Maya, or different implementations.&lt;br&gt;&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://pythonic.pocoo.org/2009/5/30/finding-objects-names&quot; href=&quot;http://pythonic.pocoo.org/2009/5/30/finding-objects-names&quot; class=&quot;externalLink&quot;&gt;http://pythonic.pocoo.org/2009/5/30/finding-objects-names&lt;/a&gt;&lt;br&gt;&lt;pre&gt;import gc, sys

def find_names(obj):
    frame = sys._getframe()
    for frame in iter(lambda: frame.f_back, None):
        frame.f_locals
    result = []
    for referrer in gc.get_referrers(obj):
        if isinstance(referrer, dict):
            for k, v in referrer.iteritems():
                if v is obj:
                    result.append(k)
    return result

foo = []

def demo():
    bar = foo
    print find_names(bar)

demo()

# ['bar', 'foo']
&lt;/pre&gt;I also found this blog post that does something very similar:&lt;br&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://posted-stuff.blogspot.com/2010/01/locating-instances-of-python-classes.html&quot; href=&quot;http://posted-stuff.blogspot.com/2010/01/locating-instances-of-python-classes.html&quot; class=&quot;externalLink&quot;&gt;http://posted-stuff.blogspot.com/2010/01/locating-instances-of-python-classes.html&lt;/a&gt;</description>
<category>VARIABLES</category>
<category>gc</category>
<category>gc.get_referrers</category>
<category>sys</category>
<category>sys._getframe</category>
<category>name</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20find%20the%20name%20of%20a%20variable%3F%5D%5D</link>
<pubDate>Mon, 18 Jan 2010 16:45:00 GMT</pubDate>
</item>
<item>
<title>Python Implementations</title>
<description>&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.python.org/dev/implementations/&quot; href=&quot;http://www.python.org/dev/implementations/&quot; class=&quot;externalLink&quot;&gt;http://www.python.org/dev/implementations/&lt;/a&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://python.org/&quot; href=&quot;http://python.org/&quot; class=&quot;externalLink&quot;&gt;CPython&lt;/a&gt;  :  The standard distribution of Python, the one you get off of python.org.  It &lt;em&gt;is Python&lt;/em&gt; implemented via the &lt;strong&gt;C&lt;/strong&gt; programming language.&lt;/li&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.jython.org/&quot; href=&quot;http://www.jython.org/&quot; class=&quot;externalLink&quot;&gt;Jython&lt;/a&gt;  :   (originally called JPython)  Targeted for interaction with the Java programming language.  Consists of Java classes that compile Python source code into Java byte code and route that to the Java Virtual Machine (JVM).  It &lt;em&gt;is Python&lt;/em&gt;, but implemented via &lt;strong&gt;Java&lt;/strong&gt; (rather than C).&lt;/li&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.codeplex.com/IronPython&quot; href=&quot;http://www.codeplex.com/IronPython&quot; class=&quot;externalLink&quot;&gt;IronPython&lt;/a&gt;  :  Designed to allow Python to integrate with apps coded to work with Microsoft's &lt;code&gt;.NET&lt;/code&gt; Framework for Windows (authored in &lt;code&gt;C#&lt;/code&gt;), as well as the &lt;code&gt;Mono&lt;/code&gt; open source equivalent for Linux.  It &lt;em&gt;is Python&lt;/em&gt;, implemented via &lt;strong&gt;.NET&lt;/strong&gt; and &lt;strong&gt;Silverlight&lt;/strong&gt; (rather than C).&lt;/li&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.stackless.com/&quot; href=&quot;http://www.stackless.com/&quot; class=&quot;externalLink&quot;&gt;Stackless Python&lt;/a&gt;  :  &quot;Stackless Python is an enhanced version of the Python programming language. It allows programmers to reap the benefits of thread-based programming without the performance and complexity problems associated with conventional threads.&quot;&lt;/li&gt;&lt;li&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://cython.org/&quot; href=&quot;http://cython.org/&quot; class=&quot;externalLink&quot;&gt;Cython&lt;/a&gt;  :  &quot;Cython is a language that makes writing C extensions for the Python language as easy as Python itself. Cython is based on the well-known Pyrex, but supports more cutting edge functionality and optimizations.  The Cython language is very close to the Python language, but Cython additionally supports calling C functions and declaring C types on variables and class attributes. This allows the compiler to generate very efficient C code from Cython code.  This makes Cython the ideal language for wrapping external C libraries, and for fast C modules that speed up the execution of Python code. &quot;&lt;/li&gt;&lt;/ul&gt;</description>
<category>FUNDAMENTALS</category>
<category>ENVIRONMENT</category>
<category>jython</category>
<category>ironpython</category>
<category>stackless</category>
<category>.net</category>
<category>java</category>
<category>c#</category>
<category>mono</category>
<category>silverlight</category>
<category>implementation</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BPython%20Implementations%5D%5D</link>
<pubDate>Mon, 18 Jan 2010 16:41:00 GMT</pubDate>
</item>
<item>
<title>How can I query if Python is 32-bit or 64-bit?</title>
<description>This seems hacky, but it works... at least on a Windows system:&lt;br&gt;&lt;pre&gt;import sys
print sys.version
split = sys.version.split()
bit = split[split.index('bit')-1]
print bit
&lt;/pre&gt;&lt;pre&gt;2.6.1 (r26:67517, Dec  4 2008, 16:59:09) [MSC v.1500 64 bit (AMD64)]
64
&lt;/pre&gt;</description>
<category>INFO</category>
<category>sys</category>
<category>sys.version</category>
<category>bit</category>
<category>32 bit</category>
<category>64 bit</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20query%20if%20Python%20is%2032-bit%20or%2064-bit%3F%5D%5D</link>
<pubDate>Fri, 15 Jan 2010 21:08:00 GMT</pubDate>
</item>
<item>
<title>Generating Python documentation</title>
<description>&lt;h1&gt;pydoc:&lt;/h1&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://www.python.org/doc/lib/module-pydoc.html&quot; href=&quot;http://www.python.org/doc/lib/module-pydoc.html&quot; class=&quot;externalLink&quot;&gt;http://www.python.org/doc/lib/module-pydoc.html&lt;/a&gt;&lt;br&gt;&lt;code&gt;pydoc&lt;/code&gt; is a built-in Python module.&lt;br&gt;Here's a simple example from the commandline:&lt;br&gt;&lt;pre&gt;C:\Python26\Lib&amp;gt;python pydoc.py -w C:\pystuff\myModule.py
&lt;/pre&gt;This will save this file:&lt;br&gt;&lt;pre&gt;C:\Python26\Lib\myModule.html
&lt;/pre&gt;To make docs for all modules in a dir, you can do the below options.  Also, by browsing to that dir directly, and calling to the full path of where &lt;code&gt;pydoc.py&lt;/code&gt; is located, it will save the resultant .html files in that dir:&lt;br&gt;&lt;pre&gt;C:\pystuff&amp;gt;python C:\Python26\Lib\pydoc.py -w C:\pystuff
&lt;/pre&gt;&lt;h1&gt;Epydoc:&lt;/h1&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://epydoc.sourceforge.net/&quot; href=&quot;http://epydoc.sourceforge.net/&quot; class=&quot;externalLink&quot;&gt;http://epydoc.sourceforge.net/&lt;/a&gt;&lt;br&gt;&lt;h1&gt;docutils&lt;/h1&gt;&lt;a target=&quot;_blank&quot; title=&quot;External link to http://docutils.sourceforge.net/&quot; href=&quot;http://docutils.sourceforge.net/&quot; class=&quot;externalLink&quot;&gt;http://docutils.sourceforge.net/&lt;/a&gt;</description>
<category>FILESYSTEMS</category>
<category>FUNDAMENTALS</category>
<category>documentation</category>
<category>pydoc</category>
<category>Epydoc</category>
<category>docutils</category>
<category>html</category>
<link>http://pythonwiki.tiddlyspot.com#%5B%5BGenerating%20Python%20documentation%5D%5D</link>
<pubDate>Wed, 13 Jan 2010 00:17:00 GMT</pubDate>
</item>
</channel>
</rss>