<!--{{{-->
<!--}}}-->
Background: #fff
Foreground: #000
PrimaryPale: #8cf
PrimaryLight: #18f
PrimaryMid: #04b
PrimaryDark: #014
SecondaryPale: #ffc
SecondaryLight: #fe8
SecondaryMid: #db4
SecondaryDark: #841
TertiaryPale: #eee
TertiaryLight: #ccc
TertiaryMid: #999
TertiaryDark: #666
Error: #f88
/*{{{*/
body {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];}

a {color:[[ColorPalette::PrimaryMid]];}
a:hover {background-color:[[ColorPalette::PrimaryMid]]; color:[[ColorPalette::Background]];}
a img {border:0;}

h1,h2,h3,h4,h5,h6 {color:[[ColorPalette::SecondaryDark]]; background:transparent;}
h1 {border-bottom:2px solid [[ColorPalette::TertiaryLight]];}
h2,h3 {border-bottom:1px solid [[ColorPalette::TertiaryLight]];}

.button {color:[[ColorPalette::PrimaryDark]]; border:1px solid [[ColorPalette::Background]];}
.button:hover {color:[[ColorPalette::PrimaryDark]]; background:[[ColorPalette::SecondaryLight]]; border-color:[[ColorPalette::SecondaryMid]];}
.button:active {color:[[ColorPalette::Background]]; background:[[ColorPalette::SecondaryMid]]; border:1px solid [[ColorPalette::SecondaryDark]];}

.tabSelected{color:[[ColorPalette::PrimaryDark]];
background:[[ColorPalette::TertiaryPale]];
border-left:1px solid [[ColorPalette::TertiaryLight]];
border-top:1px solid [[ColorPalette::TertiaryLight]];
border-right:1px solid [[ColorPalette::TertiaryLight]];
}
.tabUnselected {color:[[ColorPalette::Background]]; background:[[ColorPalette::TertiaryMid]];}
.tabContents {color:[[ColorPalette::PrimaryDark]]; background:[[ColorPalette::TertiaryPale]]; border:1px solid [[ColorPalette::TertiaryLight]];}
.tabContents .button {border:0;}

#sidebar {}
#sidebarOptions input {border:1px solid [[ColorPalette::PrimaryMid]];}
#sidebarOptions .sliderPanel {background:[[ColorPalette::PrimaryPale]];}
#sidebarOptions .sliderPanel a {border:none;color:[[ColorPalette::PrimaryMid]];}
#sidebarOptions .sliderPanel a:hover {color:[[ColorPalette::Background]]; background:[[ColorPalette::PrimaryMid]];}
#sidebarOptions .sliderPanel a:active {color:[[ColorPalette::PrimaryMid]]; background:[[ColorPalette::Background]];}

.wizard {background:[[ColorPalette::PrimaryPale]]; border:1px solid [[ColorPalette::PrimaryMid]];}
.wizard h1 {color:[[ColorPalette::PrimaryDark]]; border:none;}
.wizard h2 {color:[[ColorPalette::Foreground]]; border:none;}
.wizardStep {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];
border:1px solid [[ColorPalette::PrimaryMid]];}
.wizardStep.wizardStepDone {background:[[ColorPalette::TertiaryLight]];}
.wizardFooter {background:[[ColorPalette::PrimaryPale]];}
.wizardFooter .status {background:[[ColorPalette::PrimaryDark]]; color:[[ColorPalette::Background]];}
.wizard .button {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::SecondaryLight]]; border: 1px solid;
border-color:[[ColorPalette::SecondaryPale]] [[ColorPalette::SecondaryDark]] [[ColorPalette::SecondaryDark]] [[ColorPalette::SecondaryPale]];}
.wizard .button:hover {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::Background]];}
.wizard .button:active {color:[[ColorPalette::Background]]; background:[[ColorPalette::Foreground]]; border: 1px solid;
border-color:[[ColorPalette::PrimaryDark]] [[ColorPalette::PrimaryPale]] [[ColorPalette::PrimaryPale]] [[ColorPalette::PrimaryDark]];}

#messageArea {border:1px solid [[ColorPalette::SecondaryMid]]; background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]];}
#messageArea .button {color:[[ColorPalette::PrimaryMid]]; background:[[ColorPalette::SecondaryPale]]; border:none;}

.popupTiddler {background:[[ColorPalette::TertiaryPale]]; border:2px solid [[ColorPalette::TertiaryMid]];}

.popup {background:[[ColorPalette::TertiaryPale]]; color:[[ColorPalette::TertiaryDark]]; border-left:1px solid [[ColorPalette::TertiaryMid]]; border-top:1px solid [[ColorPalette::TertiaryMid]]; border-right:2px solid [[ColorPalette::TertiaryDark]]; border-bottom:2px solid [[ColorPalette::TertiaryDark]];}
.popup hr {color:[[ColorPalette::PrimaryDark]]; background:[[ColorPalette::PrimaryDark]]; border-bottom:1px;}
.popup li.disabled {color:[[ColorPalette::TertiaryMid]];}
.popup li a, .popup li a:visited {color:[[ColorPalette::Foreground]]; border: none;}
.popup li a:hover {background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]]; border: none;}
.popup li a:active {background:[[ColorPalette::SecondaryPale]]; color:[[ColorPalette::Foreground]]; border: none;}
.popupHighlight {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];}
.listBreak div {border-bottom:1px solid [[ColorPalette::TertiaryDark]];}

.tiddler .defaultCommand {font-weight:bold;}

.title {color:[[ColorPalette::SecondaryDark]];}
.subtitle {color:[[ColorPalette::TertiaryDark]];}

.toolbar {color:[[ColorPalette::PrimaryMid]];}
.toolbar a {color:[[ColorPalette::TertiaryLight]];}
.selected .toolbar a {color:[[ColorPalette::TertiaryMid]];}
.selected .toolbar a:hover {color:[[ColorPalette::Foreground]];}

.tagging, .tagged {border:1px solid [[ColorPalette::TertiaryPale]]; background-color:[[ColorPalette::TertiaryPale]];}
.selected .tagging, .selected .tagged {background-color:[[ColorPalette::TertiaryLight]]; border:1px solid [[ColorPalette::TertiaryMid]];}
.tagging .listTitle, .tagged .listTitle {color:[[ColorPalette::PrimaryDark]];}
.tagging .button, .tagged .button {border:none;}

.footer {color:[[ColorPalette::TertiaryLight]];}
.selected .footer {color:[[ColorPalette::TertiaryMid]];}

.sparkline {background:[[ColorPalette::PrimaryPale]]; border:0;}
.sparktick {background:[[ColorPalette::PrimaryDark]];}

.error, .errorButton {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::Error]];}
.warning {color:[[ColorPalette::Foreground]]; background:[[ColorPalette::SecondaryPale]];}
.lowlight {background:[[ColorPalette::TertiaryLight]];}

.zoomer {background:none; color:[[ColorPalette::TertiaryMid]]; border:3px solid [[ColorPalette::TertiaryMid]];}

.annotation {background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]]; border:2px solid [[ColorPalette::SecondaryMid]];}

.viewer .listTitle {list-style-type:none; margin-left:-2em;}
.viewer .button {border:1px solid [[ColorPalette::SecondaryMid]];}
.viewer blockquote {border-left:3px solid [[ColorPalette::TertiaryDark]];}

.viewer table, table.twtable {border:2px solid [[ColorPalette::TertiaryDark]];}
.viewer th, .viewer thead td, .twtable th, .twtable thead td {background:[[ColorPalette::SecondaryMid]]; border:1px solid [[ColorPalette::TertiaryDark]]; color:[[ColorPalette::Background]];}
.viewer td, .viewer tr, .twtable td, .twtable tr {border:1px solid [[ColorPalette::TertiaryDark]];}

.viewer pre {border:1px solid [[ColorPalette::SecondaryLight]]; background:[[ColorPalette::SecondaryPale]];}
.viewer code {color:[[ColorPalette::SecondaryDark]];}
.viewer hr {border:0; border-top:dashed 1px [[ColorPalette::TertiaryDark]]; color:[[ColorPalette::TertiaryDark]];}

.highlight, .marked {background:[[ColorPalette::SecondaryLight]];}

.editor input {border:1px solid [[ColorPalette::PrimaryMid]];}
.editor textarea {border:1px solid [[ColorPalette::PrimaryMid]]; width:100%;}
.editorFooter {color:[[ColorPalette::TertiaryMid]];}

#backstageArea {background:[[ColorPalette::Foreground]]; color:[[ColorPalette::TertiaryMid]];}
#backstageArea a {background:[[ColorPalette::Foreground]]; color:[[ColorPalette::Background]]; border:none;}
#backstageArea a:hover {background:[[ColorPalette::SecondaryLight]]; color:[[ColorPalette::Foreground]]; }
#backstageArea a.backstageSelTab {background:[[ColorPalette::Background]]; color:[[ColorPalette::Foreground]];}
#backstageButton a {background:none; color:[[ColorPalette::Background]]; border:none;}
#backstageButton a:hover {background:[[ColorPalette::Foreground]]; color:[[ColorPalette::Background]]; border:none;}
#backstagePanel {background:[[ColorPalette::Background]]; border-color: [[ColorPalette::Background]] [[ColorPalette::TertiaryDark]] [[ColorPalette::TertiaryDark]] [[ColorPalette::TertiaryDark]];}
.backstagePanelFooter .button {border:none; color:[[ColorPalette::Background]];}
.backstagePanelFooter .button:hover {color:[[ColorPalette::Foreground]];}
#backstageCloak {background:[[ColorPalette::Foreground]]; opacity:0.6; filter:'alpha(opacity:60)';}
/*}}}*/
/*{{{*/
* html .tiddler {height:1%;}

h1,h2,h3,h4,h5,h6 {font-weight:bold; text-decoration:none;}
h4,h5,h6 {margin-top:1em;}
h1 {font-size:1.35em;}
h2 {font-size:1.25em;}
h3 {font-size:1.1em;}
h4 {font-size:1em;}
h5 {font-size:.9em;}

hr {height:1px;}

a {text-decoration:none;}

dt {font-weight:bold;}

ol {list-style-type:decimal;}
ol ol {list-style-type:lower-alpha;}
ol ol ol {list-style-type:lower-roman;}
ol ol ol ol {list-style-type:decimal;}
ol ol ol ol ol {list-style-type:lower-alpha;}
ol ol ol ol ol ol {list-style-type:lower-roman;}
ol ol ol ol ol ol ol {list-style-type:decimal;}

.txtOptionInput {width:11em;}

#contentWrapper .chkOptionInput {border:0;}

.indent {margin-left:3em;}
.outdent {margin-left:3em; text-indent:-3em;}
code.escaped {white-space:nowrap;}

/* the 'a' is required for IE, otherwise it renders the whole tiddler in bold */

.siteTitle {font-size:3em;}
.siteSubtitle {font-size:1.2em;}

#sidebar {position:absolute; right:3px; width:16em; font-size:.9em;}
#sidebarOptions a {margin:0em 0.2em; padding:0.2em 0.3em; display:block;}
#sidebarOptions input {margin:0.4em 0.5em;}
#sidebarOptions .sliderPanel a {font-weight:bold; display:inline; padding:0;}
#sidebarOptions .sliderPanel input {margin:0 0 .3em 0;}
#sidebarTabs .tabContents {width:15em; overflow:hidden;}

.wizard h1 {font-size:2em; font-weight:bold; background:none; padding:0em 0em 0em 0em; margin:0.4em 0em 0.2em 0em;}
.wizard h2 {font-size:1.2em; font-weight:bold; background:none; padding:0em 0em 0em 0em; margin:0.4em 0em 0.2em 0em;}
.wizard .button {margin:0.5em 0em 0em 0em; font-size:1.2em;}
.wizardFooter .status {padding:0em 0.4em 0em 0.4em; margin-left:1em;}
.wizard .button {padding:0.1em 0.2em 0.1em 0.2em;}

#messageArea {position:fixed; top:2em; right:0em; margin:0.5em; padding:0.5em; z-index:2000; _position:absolute;}
.messageToolbar {display:block; text-align:right; padding:0.2em 0.2em 0.2em 0.2em;}
#messageArea a {text-decoration:underline;}

.popupTiddler {position: absolute; z-index:300; padding:1em 1em 1em 1em; margin:0;}

.popup {position:absolute; z-index:300; font-size:.9em; padding:0; list-style:none; margin:0;}
.popup hr {display:block; height:1px; width:auto; padding:0; margin:0.2em 0em;}
.popup li a {display:block; padding:0.4em; font-weight:normal; cursor:pointer;}
.listBreak {font-size:1px; line-height:1px;}
.listBreak div {margin:2px 0;}

.tab {margin:0em 0em 0em 0.25em; padding:2px;}
.tabContents ul, .tabContents ol {margin:0; padding:0;}
.txtMainTab .tabContents li {list-style:none;}

#contentWrapper {display:block;}
#splashScreen {display:none;}

#displayArea {margin:1em 17em 0em 14em;}

.toolbar {text-align:right; font-size:.9em;}

.missing .viewer,.missing .title {font-style:italic;}

.title {font-size:1.6em; font-weight:bold;}

.missing .subtitle {display:none;}
.subtitle {font-size:1.1em;}

.tagging {margin:0.5em 0.5em 0.5em 0; float:left; display:none;}
.isTag .tagging {display:block;}
.tagged {margin:0.5em; float:right;}
.tagging ul, .tagged ul {list-style:none; margin:0.25em; padding:0;}
.tagClear {clear:both;}

.footer {font-size:.9em;}
.footer li {display:inline;}

* html .viewer pre {width:99%; padding:0 0 1em 0;}
.viewer .button {margin:0em 0.25em; padding:0em 0.25em;}
.viewer ul, .viewer ol {margin-left:0.5em; padding-left:1.5em;}

.viewer table, table.twtable {border-collapse:collapse; margin:0.8em 1.0em;}
.viewer th, .viewer td, .viewer tr,.viewer caption,.twtable th, .twtable td, .twtable tr,.twtable caption {padding:3px;}
table.listView {font-size:0.85em; margin:0.8em 1.0em;}
table.listView th, table.listView td, table.listView tr {padding:0px 3px 0px 3px;}

.viewer pre {padding:0.5em; margin-left:0.5em; font-size:1.2em; line-height:1.4em; overflow:auto;}
.viewer code {font-size:1.2em; line-height:1.4em;}

.editor {font-size:1.1em;}
.editor input, .editor textarea {display:block; width:100%; font:inherit;}

.fieldsetFix {border:0; padding:0; margin:1px 0px 1px 0px;}

.sparkline {line-height:1em;}
.sparktick {outline:0;}

.zoomer {font-size:1.1em; position:absolute; overflow:hidden;}

* html #backstage {width:99%;}
* html #backstageArea {width:99%;}
#backstageArea {display:none; position:relative; overflow: hidden; z-index:150; padding:0.3em 0.5em 0.3em 0.5em;}
#backstageToolbar {position:relative;}
#backstageArea a {font-weight:bold; margin-left:0.5em; padding:0.3em 0.5em 0.3em 0.5em;}
#backstageButton {display:none; position:absolute; z-index:175; top:0em; right:0em;}
#backstageButton a {padding:0.1em 0.4em 0.1em 0.4em; margin:0.1em 0.1em 0.1em 0.1em;}
#backstage {position:relative; width:100%; z-index:50;}
#backstagePanel {display:none; z-index:100; position:absolute; margin:0em 3em 0em 3em; padding:1em 1em 1em 1em;}
.backstagePanelFooter a {padding:0.2em 0.4em 0.2em 0.4em;}
#backstageCloak {display:none; z-index:20; position:absolute; width:100%; height:100px;}

.whenBackstage {display:none;}
.backstageVisible .whenBackstage {display:block;}
/*}}}*/
/***
StyleSheet for use when a translation requires any css style changes.
This StyleSheet can be used directly by languages such as Chinese, Japanese and Korean which need larger font sizes.
***/
/*{{{*/
body {font-size:0.8em;}
#sidebarOptions {font-size:1.05em;}
#sidebarOptions a {font-style:normal;}
#sidebarOptions .sliderPanel {font-size:0.95em;}
.subtitle {font-size:0.8em;}
.viewer table.listView {font-size:0.95em;}
/*}}}*/
/*{{{*/
@media print {
#mainMenu, #sidebar, #messageArea, .toolbar, #backstageButton, #backstageArea {display: none ! important;}
#displayArea {margin: 1em 1em 0em 1em;}
/* Fixes a feature in Firefox 1.5.0.2 where print preview displays the noscript content */
noscript {display:none;}
}
/*}}}*/
<!--{{{-->
<span class='siteTitle' refresh='content' tiddler='SiteTitle'></span>&nbsp;
<span class='siteSubtitle' refresh='content' tiddler='SiteSubtitle'></span>
</div>
<span class='siteTitle' refresh='content' tiddler='SiteTitle'></span>&nbsp;
<span class='siteSubtitle' refresh='content' tiddler='SiteSubtitle'></span>
</div>
</div>
<div id='sidebar'>
<div id='sidebarOptions' refresh='content' tiddler='SideBarOptions'></div>
<div id='sidebarTabs' refresh='content' force='true' tiddler='SideBarTabs'></div>
</div>
<div id='displayArea'>
<div id='messageArea'></div>
<div id='tiddlerDisplay'></div>
</div>
<!--}}}-->
<!--{{{-->
<div class='toolbar' macro='toolbar [[ToolbarCommands::ViewToolbar]]'></div>
<div class='title' macro='view title'></div>
<div class='subtitle'><span macro='view modifier link'></span>, <span macro='view modified date'></span> (<span macro='message views.wikified.createdPrompt'></span> <span macro='view created date'></span>)</div>
<div class='tagging' macro='tagging'></div>
<div class='tagged' macro='tags'></div>
<div class='viewer' macro='view text wikified'></div>
<div class='tagClear'></div>
<!--}}}-->
<!--{{{-->
<div class='toolbar' macro='toolbar [[ToolbarCommands::EditToolbar]]'></div>
<div class='title' macro='view title'></div>
<div class='editor' macro='edit title'></div>
<div macro='annotations'></div>
<div class='editor' macro='edit text'></div>
<div class='editor' macro='edit tags'></div><div class='editorFooter'><span macro='message views.editor.tagPrompt'></span><span macro='tagChooser'></span></div>
<!--}}}-->
To get started with this blank TiddlyWiki, you'll need to modify the following tiddlers:
* SiteTitle & SiteSubtitle: The title and subtitle of the site, as shown above (after saving, they will also appear in the browser title bar)
* DefaultTiddlers: Contains the names of the tiddlers that you want to appear when the TiddlyWiki is opened
You'll also need to enter your username for signing your edits: <<option txtUserName>>
These InterfaceOptions for customising TiddlyWiki are saved in your browser

<<option chkSaveBackups>> SaveBackups
<<option chkAutoSave>> AutoSave
<<option chkRegExpSearch>> RegExpSearch
<<option chkCaseSensitiveSearch>> CaseSensitiveSearch
<<option chkAnimate>> EnableAnimations

----
Also see AdvancedOptions
<<importTiddlers>>
python tiddlywiki is born.  I've been meaning to do this for a long time, finally got around to getting my old notes put up in here.
I've finally made a blog to tie together my various wiki's and projects.  It can be found here:
http://warpcat.blogspot.com/
Was directed to a couple good links on Python:
*http://www.siafoo.net/article/57
*http://www.siafoo.net/article/52
**Lots of tips, tricks, and hacks.
Added a new Category:  [[VARIABLES]]
I already had categories for different variable types, but there were subjects that spanned types, so I thought it'd be good to have a place to collect that data.  Went through all the tiddlers, and re-tagged them appropriately.
Added the new [[EXECUTION]] category, and re-tagged all appropriate tiddlers.  This category is designed to capture anything to do with executing Python code.
Added the [[WEB]] category.  What more is there to say? ;)
Added the [[PYGAME]] Category, since I have a feeling I'll be adding a lot more PyGame notes very soon...
Started authoring a "[[Visual Guide to Tkinter widgets]]".  Good reference.  Check out my blog on it here:
http://www.akeric.com/blog/?p=350
I've got my PyGame wiki live:
http://pygamewiki.tiddlyspot.com/

Originally I had started dumping PyGame into into this wiki, but I quickly saw that it could easily be its own 'subject', so it has been branched off to its own area.
Found a big'ol list here:
http://www.vrplumber.com/py3d.py
I started this as my online notes while I learn [[Python|http://www.python.org/]].  At the creation date of this wiki I'd define myself as a 'python noob', so the things listed are the foundations of learning the language.  My background in scripting\programming is entirely self-taught:  I first learned the scripting language MEL (Maya Embedded Language, for which I have it's own dedicated wiki: http://mayamel.tiddlyspot.com/) and have dabbled in Processing (http://www.processing.org/).  Because of that, my approach to learning a language may be a bit different from the 'standard programmer'.  I often find myself missing out on key pieces of programming lore, that take me longer than I presume normal to figure out.

*http://warpcat.pbwiki.com/HowToFindStuffInPython
*http://www.openwiki.com/ow.asp?ToFindStuffInPython
But I found the tiddlywiki style to be much easier to deal with.  That page has been transfered here, and expanded upon.

''DISCLAIMER''
*I can't be blamed for anything misrepresented on this page! If it doesn't work, no finger-waving my direction.
*If you find something wrong, please tell me.  I //am// trying to correctly learn this stuff.
*Since I work on a Windows system, the majority of the command line stuff I call out to is Win-centric. If you're using Lunix\Unix\OSX etc, I can't comment on how well things will work.
When you have packages setup, and import modules from one subpackage into another subpackage, I've ran into edge-cases where the module doing the importing, and the root of the package of the imported module have the same name.

For example, given  setup of two packages, {{{packageA}}} and {{{someName}}}:
*\root
**\packageA
***{{{__init__.py}}}
***\subpackageA
****{{{__init__.py}}}
****@@{{{someName.py}}}@@
**@@\someName@@
***{{{__init__.py}}}
***\someOtherName
****{{{__init__.py}}}
****{{{greatName.py}}}
Let's say you're authoring {{{packageA.subpackageA.someName.py}}}, and you want to import {{{someName.someOtherName.greatName.py}}}:  When you do your import like so:
{{{
# packageA.subpackageA.someName.py
from someName.someOtherName import greatName as great
}}}
The module tries to import //itself// ({{{import someName.}}}), and then errors when it can't find the attribute {{{.someOtherName}}} in itself.

To work around this, I've used the {{{__import__}}} function with success:
http://docs.python.org/library/functions.html#__import__
{{{
# packageA.subpackageA.someName.py
_temp = __import__('someName.someOtherName', locals(), globals, ['greatName'], 0)
great = _temp.greatName
}}}
The last zero in the argument list tells the function to make this an //absolute//, rather than relative, import.
Update:
<<<
Recently found this blog post:
http://pysnippet.blogspot.com/2010/05/abcs-abstract-base-classes.html
That talks about the 'Abstract Base Class' module, which I was unaware of when authoring my system below.  But they have similarities.  [[docs|http://docs.python.org/library/abc.html]]
<<<
----
At the time of this authoring, abstract methods are on the periphery of my knowledge-base ;)
As I understand it, 'abstract methods' are methods implemented by a superclass '[[interface|Implementing interfaces]]' (borrowing terminology from some other languages, like Java).  The abstract method doesn't do anything, but it's existence means that a subclass needs to implement it.  It's a way, in my mind, of enforcing polymorphism.
To aid in making sure this is properly communicated to the user, you can use the {{{NotImplementedError}}} exception, for which the [[docs|http://docs.python.org/library/exceptions.html#exceptions.NotImplementedError]] on it say:
>In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method.
In the below example, I illustrate this via a self-made [[decorator]]:  In your superclass, you wrapper your abstract methods with the {{{@abstract}}} decorator.  If the user calls to one of these abstract methods, it will raise a {{{NotImplementedError}}} exception.  But if a subclass has overwritten the abstract method with it's own, it will obviously work:
{{{
def abstract(func):
"""
This will be used as a decorator (@abstract) to create abstract methods.
"""
def notImplemented(*args):
raise NotImplementedError(func)
return notImplemented

class Spam(object):
# Our superclass, with abstract method.
@abstract
def myMethod(self):
pass

class SubSpam(Spam):
# Our subclass, that replaces the abstract method with a valid one.
def myMethod(self):
print "Replaced abstract method!"

eggs = Spam()
subEggs = SubSpam()
}}}
{{{
eggs.myMethod()
# NotImplementedError: <function myMethod at 0x000000000371F2E8>
}}}
{{{
subEggs.myMethod()
# Replaced abstract method!
}}}
----
Also see:
*[[Implementing interfaces]]
Found this post:
http://coreygoldberg.blogspot.com/2011/07/python-processing-gmail-imap-email.html
By Cory Goldberg, that makes it really darn easy to access your gmail.  Code reproduced below:
{{{
import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
mail.select('Inbox')

status, data = mail.search(None, 'ALL')
for num in data[0].split():
status, data = mail.fetch(num, '(RFC822)')
print 'Message %s\n%s\n' % (num, data[0][1])

mail.close()
mail.logout()
}}}
And here's the docs for {{{imaplib}}}
http://docs.python.org/library/imaplib.html
The Python module [[ctypes|http://docs.python.org/library/ctypes.html]] is a good interface into grabbing data from Windows (the OS) itself.

And example is given here, for querying the location of a window on screen (search down for the {{{GetWindowRect}}} section)
http://docs.python.org/library/ctypes.html#function-prototypes
Let's break it down (based on my powers of deduction...):
Here's what the Windows ref say about the {{{GetWindowRef()}}} function:
{{{
BOOL GetWindowRect(
HWND hWnd,
LPRECT lpRect
);
}}}
So, it returns a {{{boolean}}}, and takes two args, {{{hWnd}}}, and {{{lpRect}}}
This is the Python documentation implementation:
{{{
from ctypes import POINTER, WINFUNCTYPE, windll
from ctypes.wintypes import BOOL, HWND, RECT

prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))  #1
paramflags = (1, "hwnd"), (2, "lprect")  #2
GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)  #3
}}}
*1 : {{{prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT)) }}}
**Create a {{{WINFUNCTYPE}}} (see [[docs|http://docs.python.org/library/ctypes.html#function-prototypes]]) function object (called {{{prototype}}}), for which apparently the first arg is the return type, and consecutive args are the args to that function.
**{{{ctypes.WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)}}}
*2 : {{{paramflags = (1, "hwnd"), (2, "lprect")}}}
**Create the {{{paramflags}}} arguments we will pass into this {{{ctype}}} function.  From the docs:
<<<
paramflags must be a tuple of the same length as argtypes.
Each item in this tuple contains further information about a parameter, it must be a tuple containing one, two, or three items.
The first item is an integer containing a combination of direction flags for the parameter:
*1 : Specifies an input parameter to the function.
*2 : Output parameter. The foreign function fills in a value.
*4 : Input parameter which defaults to the integer zero.
The optional second item is the parameter name as string. If this is specified, the foreign function can be called with named parameters.
The optional third item is the default value for this parameter.
<<<
*3 : {{{GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)}}}
**Create our {{{GetWindowRect}}} object.  There are four different ways to call to a {{{prototype}}} object:
****Returns a foreign function at the specified address which must be an integer.
***{{{prototype(callable)}}}
****Create a C callable function (a callback function) from a Python callable.
***{{{prototype(func_spec[, paramflags])}}}  {{{<--}}} ''The one we're using.''
****Returns a foreign function exported by a shared library. func_spec must be a 2-tuple (name_or_ordinal, library). The first item is the name of the exported function as string, or the ordinal of the exported function as small integer. The second item is the shared library instance.
***{{{prototype(vtbl_index, name[, paramflags[, iid]])}}}
****    Returns a foreign function that will call a COM method. vtbl_index is the index into the virtual function table, a small non-negative integer. name is name of the COM method. iid is an optional pointer to the interface identifier which is used in extended error reporting.  COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that are specified in the argtypes tuple.
Now that we finally have a {{{getWindowRect}}} object, we can instance it:
{{{
# and example of getting 'hwnd' from Pygame:
hwnd = pygame.display.get_wm_info()["window"]

rect = GetWindowRect(hwnd)
}}}
And once instanced, we can access it's attrs:
{{{
print "top, left, bottom, right: ", rect.top, rect.left, rect.bottom, rect.right
}}}
----
Using that example, here are more Windows functions that can be accessed by {{{ctypes}}}
http://msdn.microsoft.com/en-us/library/dd469351%28VS.85%29.aspx

I wonder how much data can be pulled from the [[WINDOWINFO|http://msdn.microsoft.com/en-us/library/ms632610%28VS.85%29.aspx]] structure?
You need:
*[[pywin32|http://sourceforge.net/projects/pywin32/]], also known as 'Python for windows extensions'
*[[comtypes|http://sourceforge.net/projects/comtypes/]], or you need this.
You probably want:
*[[winshell|http://timgolden.me.uk/python/winshell.html]]
----
Nice blog on how to make Windows shortcuts:
http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/
----
Recipe and blog post on gathering Windows information (that actually //doesn't// require the above modules)
http://code.activestate.com/recipes/511491/
http://www.blog.pythonlibrary.org/2010/01/27/getting-windows-system-information-with-python/
http://www.blog.pythonlibrary.org/2010/02/06/more-windows-system-information-with-python/
http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/ (more registry stuff)
----


!pyperclip:
http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/
Uses code similar to the {{{ctypes}}} example below.
!ctypes:
Example from:
http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python
This is pretty cool, since it uses no external libs, only [[ctypes|http://docs.python.org/library/ctypes]]
Had to modify {{{paste}}}, since it appears to be original written for Python 3.0 (I'm on 2.6)
{{{
import ctypes

#Get required functions, strcpy..
strcpy = ctypes.cdll.msvcrt.strcpy
ocb = ctypes.windll.user32.OpenClipboard    #Basic Clipboard functions
ecb = ctypes.windll.user32.EmptyClipboard
gcd = ctypes.windll.user32.GetClipboardData
scd = ctypes.windll.user32.SetClipboardData
ccb = ctypes.windll.user32.CloseClipboard
ga = ctypes.windll.kernel32.GlobalAlloc    # Global Memory allocation
gl = ctypes.windll.kernel32.GlobalLock     # Global Memory Locking
gul = ctypes.windll.kernel32.GlobalUnlock
GMEM_DDESHARE = 0x2000

def get():
ocb(None) # Open Clip, Default task
pcontents = gcd(1) # 1 means CF_TEXT.. too lazy to get the token thingy ...
data = ctypes.c_char_p(pcontents).value
#gul(pcontents) ?
ccb()
return data

def paste(data):
ocb(None) # Open Clip, Default task
ecb()
try:
hCd = ga(GMEM_DDESHARE, len(bytes(data))+1) # Python 2
except TypeError:
hCd = ga(GMEM_DDESHARE, len(bytes(data,"ascii"))+1) # Python 3
pchData = gl(hCd)
try:
strcpy(ctypes.c_char_p(pchData), bytes(data))
except TypeError:
strcpy(ctypes.c_char_p(pchData), bytes(data,"ascii"))
gul(hCd)
scd(1,hCd)
ccb()
}}}
!Tkinter
Example from:
http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python
This is nice, since it uses [[Tkinter|http://docs.python.org/library/tkinter.html]] which ships with Python, and is less code than ctypes:
{{{
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()
}}}
!{{{win32clipboard}}}
If {{{win32clipboard}}} isn't part of your standard install, you can download it here as part of the 'pywin32' extension for Windows here:
http://sourceforge.net/projects/pywin32/
It seems to come standard with the ~ActiveState '~ActivePython' distribution here, if you use that distib:
http://www.activestate.com/activepython/
----

From:
http://snippets.dzone.com/posts/show/724
{{{
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()
}}}
From:
http://mail.python.org/pipermail/python-list/2007-June/617808.html
{{{
from win32clipboard import *
OpenClipboard()
EmptyClipboard()
SetClipboardText("Hello from Python!")
CloseClipboard()
}}}
----
And here's a function I came up with for pasting to the clipboard:
{{{
import win32clipboard as w32c
def pasteToClipboard(stuff):
"""
Paste the passed in data to the windows clipboard

stuff : string or list : data to paste to clipboard
"""
if type(stuff) != type([]):
stuff = [stuff]
paste = '\n'.join(stuff)

w32c.OpenClipboard()
w32c.EmptyClipboard()
w32c.SetClipboardText(paste)
w32c.CloseClipboard()
}}}
(On the Windows system at least...) this directory is stored in the system variable {{{TMP}}} or {{{TEMP}}}.
----
Create a name for a file in the users temp directory:
{{{
import os

# TMPDIR on mac
tempfile = os.path.join(os.getenv("TMP"), "tempfile.txt")
print tempfile
}}}
On Vista:
{{{
}}}
On Mac:
{{{
/var/folders/kv/dk8t43d535s3pvw9z_8zhh2c0000gn/T/tempfile.ma
}}}
----
http://docs.python.org/library/tempfile.html
http://blog.doughellmann.com/2008/02/pymotw-tempfile.html


Below are a list of //all// the subjects (tiddlers) in the wiki.  Note that there will be ones for site maintenance etc, that should be disregarded.
----
<<list all>>
Found here: http://nodebox.net/code/index.php/Math
{{{
from math import degrees, atan2
# 2d points:
def angle(x0, y0, x1, y1):
a = degrees( atan2(y1-y0, x1-x0) )
return a
}}}
The python {{{math}}} module is missing calls to any kind of vector math.  :-(  I've found a few solutions listed below.  I also have some notes on doing it by hand [[here|Vector math]].
!~ActiveState recipes:
*[[Vector|http://code.activestate.com/recipes/578006-vector/]] : A fairly robust looking 2d vector solution.
!~PyGame
*~PyGame have contributed [[Vec2d|http://pygame.org/wiki/2DVectorClass]] and [[Vec3d|http://pygame.org/wiki/3DVectorClass]] classes.
!gameobjects
*"Game Objects is a collection of 2D and 3D maths classes, and algorithms for helping in the creation of games with Python. Suitable for PyGame, but independent of it."
*Specifically its {{{Vector2}}} and {{{Vector3}}} classes.
!~NumPy
*Main Page:  http://numpy.scipy.org/
*Documentation:  http://www.tramy.us/numpybook.pdf
*Source:  http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103
!~PyEuclid
*Main Page:  http://partiallydisassembled.net/euclid.html
*Documentation: http://partiallydisassembled.net/euclid/
It includes these data structures:
*2D and 3D Vector
*3x3 and 4x4 Matrix
*Quaternion
And these physical representations:
*Point
*Line, ray, line segment
*Circle, sphere
!~VPython
*Main Page:  http://vpython.org
*Documentation:  http://vpython.org/webdoc/visual/index.html
*Docs on vectors specifically:  http://vpython.org/webdoc/visual/vector.html
**Includes mag, mag2, norm, cross, dot, rotate, diff_angle
Examples:
{{{
# Includes a vector object type:
from visual import *

v1 = vector(1,2,3)
v2 = vector(10,20,30)
print v1+v2
# <11, 22, 33>
print 2*v1
# <2, 4, 6>
m = v1.mag
print m
# 3.74165738677
print v1.norm()
# <0.267261, 0.534522, 0.801784>
}}}
Vector objects have these attributes:
*{{{mag}}}
*{{{mag2}}}
*{{{x}}}
*{{{y}}}
*{{{z}}}
And these methods:
*{{{astuple(...)}}}
**Convert this vector to a tuple.  Same as tuple(vector), but much faster.
*{{{clear(...)}}}
**Zero the state of this vector.  Potentially useful for reusing a temporary variable.
*{{{comp(...)}}}
**The scalar projection of this vector onto another.
*{{{cross(...)}}}
**The cross product of this vector and another.
*{{{diff_angle(...)}}}
**The angle between this vector and another, in radians.
*{{{dot(...)}}}
**The dot product of this vector and another.
*{{{norm(...)}}}
**The unit vector of this vector.
*{{{proj(...)}}}
**The vector projection of this vector onto another.
*{{{rotate(...)}}}
**Rotate this vector about the specified axis through the specified angle, in radians
Just a collection of reference links:
----
*http://www.pymedia.org/
**"~PyMedia is a Python module for wav, mp3, ogg, avi, divx, dvd, cdda etc files manipulations. It allows you to parse, demutiplex, multiplex, decode and encode all supported formats. It can be compiled for Windows, Linux and cygwin."
----
http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/
Blog showing how to use [[wxPython|http://www.wxpython.org/]], [[MPlayer|http://www.mplayerhq.hu/design7/news.html]], and [[MplayerCtrl|http://pypi.python.org/pypi/MplayerCtrl/]] to make a video-player.
----
http://lateral.netmanagers.com.ar/weblog/posts/BB905.html
Making an audio and video player with [[PyQt|http://wiki.python.org/moin/PyQt]]
I've been looking for ways to author SVG images in Python to support work on my Ostrich Egg-bot.  Found this recipe on ~ActiveState to help me along:
*http://code.activestate.com/recipes/578123-draw-svg-images-in-python-python-recipe-enhanced-v/
**It uses the software [[ImageMagick|http://www.imagemagick.org/script/index.php]] to view them.
I need to learn this.  Thus, I will put notes here!
See: [[Character and numric conversion]]
*http://docs.python.org/library/stdtypes.html#bit-string-operations-on-integer-types
*http://wiki.python.org/moin/BitManipulation
*http://compsci.ca/v3/viewtopic.php?t=9893
*http://en.wikipedia.org/wiki/Bitwise_operation
*Help from Processing docs:
**http://www.processing.org/reference/bitwiseOR.html
**http://www.processing.org/reference/bitwiseAND.html
**http://www.processing.org/reference/leftshift.html
**http://www.processing.org/reference/rightshift.html
----
----
*When doing bitwise operations in Python, you can apply the operations directly to integer values:  It applies the operation on their binary equivalent, and returns the integer value.
*When thinking about bitwise operations, it helps (me at least) to relate them to sets, since they behave similarly.
----
| Operation | Result | Notes |
| | | |
| x {{{|}}} y |bitwise ''or'' of x and y |True if either bit is positive, else false.  Combines bits, where 1 bits have priority. |
| x {{{^}}} y |bitwise ''exclusive or'' ('xor') of x and y |True if bits are opposite, false if the same. |
| x {{{&}}} y |bitwise ''and'' of x and y |True if both bits are positive, else false.  Acts like a mask where 0 bits have priority. |
| x {{{<<}}} n |x ''shifted left'' by n bits |Each shift left multiplies by two. |
| x {{{>>}}} n |x ''shifted right'' by n bits |Each shift right divides by two.  Will truncate decimals. |
| {{{~}}}x |the bits of x ''inverted''.  'Not x' |yields -(num + 1) |

|  X  Y  | X {{{&}}} (and) Y   |  X {{{|}}} (or) Y  | {{{~}}} (not) X  | {{{~}}} (not) Y  |  X {{{^}}} (xor) Y |
|        |           |          |        |         |  |
|  1  1  |    1      |    1     |   0    |    0    |     0 |
|  1  0  |    0      |    1     |   0    |    1    |     1 |
|  0  1  |    0      |    1     |   1    |    0    |     1 |
|  0  0  |    0      |    0     |   1    |    1    |     0 |
----
!!!Combined example:
(Borrowed and modified from the Processing doc examples listed above...)
Bit shifting and manipulation is commonly used for dealing with color values.  So let's store an ARGB (alpha, red, green blue) color value in 32-bits.
Each channel starts out as an int from 0-255, but then is bit-shifted into its binary format.
{{{
# Make our color values in binary format.
a = 255 << 24 # bin(a) =  11111111000000000000000000000000
r = 204 << 16 # bin(r) =  00000000110011000000000000000000
g = 204 << 8  # bin(g) =  00000000000000001100110000000000
b = 51        # bin(b) =  00000000000000000000000000110011
}}}
Now, {{{OR}}} the values together into a single 32-bit number:
{{{
# argb is now a single value storing all four a, r, g, b values:
argb = a | r | g | b  # bin(argb) = 11111111110011001100110000110011
}}}
{{{
# Prove it:
print bin(a).replace('0b', '').zfill(32)
print bin(r).replace('0b', '').zfill(32)
print bin(g).replace('0b', '').zfill(32)
print bin(b).replace('0b', '').zfill(32)
print bin(argb).replace('0b', '')
}}}
{{{
11111111000000000000000000000000 # a
00000000110011000000000000000000 # r
00000000000000001100110000000000 # g
00000000000000000000000000110011 # b
11111111110011001100110000110011 # argb, values OR'd together
}}}
Convert from Binary back to 0-255 integer:
The sytax "{{{& 0xFF}}}" compares the binary representation of the two values and makes all but the last 8 bits into a 0.
"{{{0xFF}}}" is {{{00000000000000000000000011111111}}}
Meaning, it makes a mask only showing the last 8 digits.  So first, the value is bit-shifted to the right by the appropriate amount, then the appropriate values are masked for the result:
{{{
a = argb >> 24 & 0xFF
r = argb >> 16 & 0xFF
g = argb >> 8 & 0xFF
b = argb & 0xFF

print a, r, g ,b
# 255 204 204 51
}}}
----
!!!Shifting examples, {{{<<}}}, {{{>>}}}
As explained above, each shift to the left multiplies the value by 2, and each shift to the right divides the value by two.  Right shift (division) can be lossy, since decimal values aren't preserved.

Shifting left by 1 = val * 2
Shifting left by 2 = val * 4
Shifting left by 3 = val * 8
etc...

Shifting right by 1 = val / 2
Shifting right by 2 = val / 4
Shifting right by 3 = val / 8
etc...
{{{
intVal0 = 130
binVal0 = bin(intVal0)

intLeft1 = intVal0 << 1
binValLeft1 = bin(intLeft1)

intRight1 = intVal0 >> 1
binValRight1 = bin(intRight1)

intLeft2 = intVal0 << 2
binValLeft2 = bin(intLeft2)

intRight2 = intVal0 >> 2
binValRight2 = bin(intRight2)

print "%11s %4s, %12s"%("Orig Value:", intVal0, binVal0)
print "%11s %4s, %12s"%("val << 1 :", intLeft1, binValLeft1)
print "%11s %4s, %12s"%("val << 2 :", intLeft2, binValLeft2)
print "%11s %4s, %12s"%("val >> 1 :", intRight1, binValRight1)
print "%11s %4s, %12s"%("val >> 2 :", intRight2, binValRight2)
}}}
{{{
Orig Value:  130,   0b10000010
val << 1 :  260,  0b100000100
val << 2 :  520, 0b1000001000
val >> 1 :   65,    0b1000001
val >> 2 :   32,     0b100000
}}}
!!!Inversion \ 'not' examples, {{{~}}}
As mentioned above, this operations yields -(num + 1).
{{{
intVal0 = 130
binVal0 = bin(intVal0)

intNot1 = ~intVal0
binNot1 = bin(intNot1 )

print "%11s %4s, %12s"%("Orig Value:", intVal0, binVal0)
print "%11s %4s, %12s"%("~Value:", intNot1, binNot1)
}}}
{{{
Orig Value:  130,   0b10000010
~Value: -131,  -0b10000011
}}}
<<gradient horiz #ffffff  #ddddff #8888ff >>
The python tiddlywiki blog
*[[2009 08 27]] - PyGame wiki is live!
*[[2009 04 11]] - Visual Guide to Tkinter widgets
*[[2009 04 07]] - Added [[PYGAME]] Category
*[[2009 04 03]] - Added [[WEB]] Category
*[[2008 08 17]] - Added [[EXECUTION]] Category
*[[2008 08 08]] - Added [[VARIABLES]] Category
*[[2008 07 02]] - Some good Python links on 'special methods', and hacks
*[[2008 06 08]] - Blog about my new blog (blog blog blog)
*[[2008 05 19]] - Python tiddlywiki is born!
If you have a subclass that overrides a superclass's method, how can you still call to the superclass method through the subclass?
You can call to the superclass method directly, passing in the child class object as the first argument ({{{self}}}).
{{{
class Parent(object):
def __init__(self):
self.value = 0
def doIt(self):
self.value = 23

class Child(Parent):
def __init__(self):
super(Child, self).__init__()
def doIt(self):
self.value = 100

spam = Child()
print spam.value

spam.doIt()
print spam.value

Parent.doIt(spam)
print spam.value
}}}
prints:
{{{
0
100
23
}}}
*When {{{spam}}} is first created through the {{{Child}}} class, {{{value}}} is set to 0, as defined by the superclass {{{Parent.__init__()}}}.
*When the {{{spam.doIt()}}} is called (through the {{{Child}}} class), it sets {{{value}}} to 100.
*But then the superclass {{{Parent.doIt()}}} method is called on {{{spam}}}, setting {{{value}}} to 23.


Sho'nuff:
{{{
def foo(arg):
print arg

def goo(something):
something("yes!")

goo(foo)
# yes!
}}}
In the above example, we define a simple function {{{foo()}}} that takes a single argument, and prints it.  The function {{{goo()}}} has an argument, presumed to be some anonymous function, and then passes a single arg into it for execution.

This is a really simple example, but in practice, it allows you to have a single master wrapper function plug arguments into arbitrary anonymous functions passed into it.
Recently ran into the need to launch Python from a shell and pass in certain arguments to it.  Simple example:
{{{
>python - arg1 arg2 arg3
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
>>> import sys
>>> print sys.argv
['-', 'argA', 'argB', 'argC']
}}}
Python can obviously take a variety of flags when executed via command line ({{{>python -h}}}).  Passing it the 'minus' character '{{{-}}}' tells {{{sys.argv}}} to collect all args passed in after it.  So the args are {{{sys.argv[1:]}}}.
Credit goes to [[this blog post|http://www.chrisevans3d.com/pub_blog/?p=494]] by Christopher Evans.

Via the UI, it's pretty easy to 'drag and drop' one or more file's onto a Python module's, and capture the name(s) of the dropped file(s):
{{{
# droptest.py
import sys

def main(data):
f = open('c:\\droptest.txt', 'w')
for d in data:
f.write('%s\n'%d)
f.close()

if __name__ == "__main__":
# Capture the dropped file info:
main(sys.argv[1:])
}}}
Now, you can drag a file onto the icon for {{{droptest.py}}}, and it will update the {{{droptest.txt}}} file with the full path-name of the dropped file.  Pretty slick!

{{{sys.argv}}} is a list of items:
*index0: name of the module being executed
*index1->n : All arguments passed into the module.  In this case, it will be all the filenames dropped on the module's icon.


{{{
print '%s%s%s'%('A'.ljust(8,'.'), '<-->'.center(8,'.'), 'B'.rjust(8,'.'))
print '%s%s%s'%('aaaa'.ljust(8,'.'), '<-->'.center(8,'.'), 'bbbb'.rjust(8,'.'))
print '%s%s%s'%('aaaaaaaaa'.ljust(8,'.'), '<-->'.center(8,'.'), 'bbbbbbbbb'.rjust(8,'.'))
}}}
{{{
A.........<-->.........B
aaaa......<-->......bbbb
aaaaaaaaa..<-->..bbbbbbbbb
}}}
Notes from Python's [[Built In Functions|http://docs.python.org/library/functions.html#built-in-functions]], and other places around the web...
!Numbers:
[[Convert an integer number to a binary string|http://docs.python.org/library/functions.html#bin]]:
{{{
bin(x)
}}}
----
[[Create a complex number with the value real + imag*j or convert a string or number to a complex number|http://docs.python.org/library/functions.html#complex]]:
{{{
complex([real[, imag]])
}}}
----
[[Convert a string or a number to floating point|http://docs.python.org/library/functions.html#float]]:
{{{
float([x])
}}}
----
[[Convert an integer number (of any size) to a hexadecimal string|http://docs.python.org/library/functions.html#hex]]:
{{{
hex(x)
}}}
----
[[Convert a string or number to a plain integer|http://docs.python.org/library/functions.html#int]]:
The base parameter gives the base for the conversion (which is 10 by default) and may be any integer in the range [2, 36], or zero.
{{{
int([x[, base]])
}}}
----
[[Convert an integer number (of any size) to an octal string|http://docs.python.org/library/functions.html#oct]]:
{{{
oct(x)
}}}
----
[[Given a string of length one, return an integer representing the Unicode code point of the character|http://docs.python.org/library/functions.html#ord]]
This is the inverse of chr() for 8-bit strings and of unichr()  for unicode objects.
{{{
ord(c)
}}}
----
Based on code given in a talk [[HERE|http://python.mirocommunity.org/video/1591/pycon-2010-the-mighty-dictiona]] by Brandon Craig Rhodes:
Given an integer, return the value in 32-bit binary notation:
{{{
def bits(n):
n += 2**32
return bin(n)[-32:] # remove '0b'
}}}
{{{
print bits(56)
00000000000000000000000000111000
}}}
!Numric conversion methods:
Return the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros:
{{{
int.bit_length()
long.bit_length()
}}}
----
Return a pair of integers whose ratio is exactly equal to the original float and with a positive denominator:
{{{
float.as_integer_ratio()
}}}
Return a representation of a floating-point number as a hexadecimal string:
{{{
float.hex()
}}}
Class method to return the float represented by a hexadecimal string s:
{{{
float.fromhex(s)
}}}
!Characters:
[[Return a string of one character whose ASCII code is the integer i|http://docs.python.org/library/functions.html#chr]]:
{{{
chr(i)
}}}
----
[[Return a string containing a nicely printable representation of an object|Return a string containing a nicely printable representation of an object.]]:
{{{
str([object])
}}}
----
[[Return the Unicode string of one character whose Unicode code is the integer i|http://docs.python.org/library/functions.html#unichr]]:
{{{
unichr(i)
}}}
----
[[Return the Unicode string version of object|http://docs.python.org/library/functions.html#unicode]]:
{{{
unicode([object[, encoding[, errors]]])
}}}
Object Oriented Programming (OOP) In Python.

Also see [[Class - advanced]] for deeper info on classes.

Here is a pretty good overview:
http://www.pasteur.fr/formation/infobio/python/ch18.html
This is pulled from the 'Programming Course for Biologists at the Pasteur Institute', so the examples are a bit... 'genetics related'. But they have nice pictures, and get the point across.

''Simple Overview:''
*''Class'':  AKA, "The Blueprint".  A class is an //object// maker: it contains all the statements needed to create an //object//, it's //attributes//, as well as the statements to describe the operations that the //object// will be able to perform, via its //methods//.  Think of it as an '//object// factory'.  To say it another way:  A //class// isn't an //object//, it's a design for one.  Consider a //class// as the blueprint for an //object//.
*''Instance'':  AKA, "The Process".  The process of creating //objects// from a //class// is called //instantiation//.  //Classes// make //instances// of themselves, which are //objects//.  You create an //instance// by calling to a //function// with the same name as the //class//.  This function is called the //constructor method// (see below).  You can actually have multiple constructors, allowing for different default arguments to be passed in during //instancing//.  This is called "overloading the constructor".
*''Object'':  AKA, "The Result".  An //object// is a framework to group values and operations on these values.  It is an //instance// of a //class//.  When a //class// goes to work, an //object// is the result (via instantiation).
*''Method'':  AKA, "An Ability".  A //method// is a [[function|Function]] defined for a //class//, specifying a behavior of the //class instances//.  Method definitions follow exactly the same pattern as standard [[function|Function]] definitions, except that they must have declared a first argument (called {{{self}}}, see below) for referring to the //instance// they belong too.  A method gives an object the ability to do something.
*''Attribute'':  AKA, "A Memory".  //Attributes// are variables that live inside of an //object//, as defined by the //class//.  They can be filled with values when the //instance// is made, as defined by the //constructor function//.  Basically, //attributes// let objects remember things, and act on them via their //methods//.
Simple data breakdown:
*''Class''  -  Makes (via instancing):
**''Objects''  -  Instance of a class, which contain (based on the class definitions):
***''Methods''  -  (functions defined inside of the class)
***''Attributes''  -  (variables defined inside of the class)
A simple class:
{{{
class Foo(object):        # 1

def __init__(self, namer, num):        # 2
self.name = namer        # 3
self.number = num        # 4

def bar(self):        #5
print self.name + "_" + str(self.number)        #6

def incNum(self):        #7
self.number = self.number + 1        #8
print self.number        #9
}}}
Now build an object, and see what it does:
{{{
>>> myObject = Foo("doof", 3)        #10
>>> myObject.incNum()        # 11
4        #12
>>> myClass.bar()        #13
doof_4        #14
}}}
What exactly is going on here?
#Define the class name.  __Class names are ''C''apitalized__.  By specifying {{{(object)}}}, this is defined as a //new style class// (a good thing, do it).
#{{{__init__}}} is the //constructor method// (also called the 'initialization method') that is called when a new object is created, via //class instantiation//.  This method is called to in #10.  {{{namer}}} & {{{num}}} are //arguments// used to pass in creation data, usually used in the following listed //attributes// (lines #3 & #4).  Default argument values are also supported: {{{namer = "geewiz"}}} would also be acceptable.
#Create an __attribute__ called {{{.name}}}.  Fill it with the value the user passes in through the //argument// {{{namer}}}:  the string {{{"doof"}}} from #10.  FYI, the //argument// and the //attribute// can have  the same name, but I made them different here to make how the data flows more visual.
#Create an __attribute__ called {{{.number}}}.  Fill it with the value the user passes in through the //argument// {{{num}}}:  The int {{{3}}} from #10.
#Define a __method__  for this class called {{{bar}}}.  After an object is created, methods can be called to via '//dot notation//', as seen in #11 & #13.
#This method simply prints the value of the //attribute// {{{.name}}}, adds an string underscore {{{_}}}, and then adds the current value of the attribute {{{.number}}} to the end. (#14)
#Define another __method__ for this class called {{{incNum}}}.
#Take the current value of the //attribute// {{{.number}}}, and increment it by 1.
#Print the current value of the //attribute// {{{.number}}} (#12).
#Create an //instance// of our {{{Foo}}} //class//, as //object//  {{{myObject}}}.  Pass in the string {{{"doof"}}} as the //argument// {{{namer}}} to the //constructor method// (#2).  Also pass in the int {{{3}}} to the //argument// {{{num}}}.
#Call to the {{{incNum}}} method on our {{{myObject}}} //object//.  This will add 1 to the current //attribute value//.
#The {{{incNum}}} method's {{{print}}} command (#9) prints out the current //attribute value//
#Call to the {{{bar}}} method on our {{{myObject}}} //object//.  This concatenates the strings and ints together, and prints them out (#6).
----
''Want to know more about '[[self|What is up with 'self'?]]'?''
----
From the link at the top, here is another simple class example.  The user can define the starting position on a xy plane, and via it's methods, move it around, and 'show' where it is at:
{{{
class Point(object):
"""This is a docstring, use them"""

def __init__(self, x, y):
self.xy = (x, y)

def show(self):
print self.xy

def move(self, mx, my):
self.xy = (self.xy[0]+mx, self.xy[1]+my)

p = Point(1, 2)
}}}
For a more basic overview of classes, please see [[Class]].

Good Class Authoring (from "Python Programming For The Absolute Beginner"):
*When you write a class:
**Create methods so that clients (users) won't need to directly access an object's attributes.
**Use privacy sparingly and only for those few attributes and methods that are completely internal to the operation of objects.
*When you use an object:
**Minimize the direct reading of an object's attributes.
**Avoid directly altering an object's attributes.
**Never directly access an object's private attributes or methods.
----
''List of topics'':
*[[Class Attributes]]
*[[Static Methods]]
*[[Private Attributes]]
*[[Private Methods]]
*[[Get and set methods]]
*[[Properties|property]]
*[[Class Inheritance]]
*[[Method Overriding]]
*[[Emulating Numeric Types \ Operator Overloading|How can I have objects add themselves together?  Subtract one another, etc?]]
''Class Attributes'':
Any variable assigned //outside// of a method creates a 'class attribute'.
{{{
# let's make a class attribute:
class SomeClass(object):
classAttrA = 0
def __init__(self):
SomeClass.classAttrA += 1
# etc, furthermore, and so-on...
}}}
{{{classAttrA}}} is now available to all instances of the class.  Meaning, any object created via the class will see the same variable data.  Getting the class attribute value, whether in a method, or outside the class completely, is done by accessing the //class// directly:
{{{
classAttrVal = SomeClass.classAttrA
}}}
The assignment statement is only executed once, when Python first sees the class definition.  This means that the class attribute exists even before a single object is created:  You can use a class attribute without any objects of the class in existence.  As see in the above example, an object's method can modify a class attribute.  They can also be modified through the class directly, outside of any object.method:
{{{
SomeClass.classAttrA = 10
}}}
This would fail:
{{{
sc = SomeClass()
sc.classAttrA = 10
}}}
It won't give an exception, but neither will it actually update the class attribute.
When one class inherits another, it automatically gets (or inherits) all of the methods and attributes of the current class.  Example:
{{{
# make our base class:
class Foo(object):
def __init__(self, val):
self.val = val

def someMethod():
self.attr = self.attr * 10

# make our derived class that inherits Foo:
class NewFoo(Foo):
def someNewFooMethod():
self.newFooAttr = self.attr * .1
}}}
{{{class NewFoo}}} inherits all of the methods from {{{Foo}}} ({{{__init__}}} & {{{someMethod}}}), plus, can create new ones ({{{someNewFooMethod}}}).  {{{NewFoo}}} is considerfed a //derived class//, since it derives part of its definition from {{{Foo}}}.  {{{Foo}}} is considered a //base class// (or //superclass//), because {{{NewFoo}}} is based on it.

Here is another example.  In it, we have a superclass {{{Foo}}} that takes arguments.  We build a subclass {{{Goo}}} that calls too it, but passes arguments through in a special way:
{{{
class Foo(object):
def __init__(self, goo = "gOo", num = 45):
self.goo = goo
self.num = num

class Noo(Foo):
def __init__(self, snarg = "snarg!", **kwargs):
Foo.__init__(self, **kwargs)
self.snarg = snarg

n = Noo(goo = "goo!", num = 23)

print n.snarg
print n.goo
print n.num

# snarg!
# goo!
# 23
}}}
Rather than having to explicitly type each of the {{{Foo.__init__}}} args into the {{{Noo.__init__}}} definition, we use the 'keyword argument' method ({{{**kwargs}}}) of passing them through.  By doing this, it saves us from having to update {{{Noo.__init__}}} with new args every time we decide to add a new arg to {{{Foo.__init__}}}.
http://docs.python.org/library/functions.html#classmethod
Class methods differ from [[Static Methods]] in that they return (or can return) a new object of the type of class (If not returning a class, might as well use a static method).  They can be authored using a [[decorator]] {{{@}}}.  Rather than having their first argument be {{{self}}}, or a reference to the instance object in question, their first arg is {{{cls}}} (by convention, the name {{{cls}}} is arbitrary, just like {{{self}}}), which is a reference to the //class//.

Example, with a {{{Vect2}}} class.  We author a 'makeFromPoints' function as a {{{classmethod}}}.  When called to via the class itself, it will return a new {{{Vect2}}} object:
{{{
class Vect2(object):

@classmethod
def makeFromPoints(cls, P1, P2):
# Pass in two sets of points,
# returns the Vector2 object between them.
return cls(P2[0] - P1[0], P2[1] - P1[1])

def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y

def __str__(self):
return "(%s, %s)" % (self.x, self.y)
}}}
{{{
p1 = [2.0, 1.0]
p2 = [3.0, 1.0]
p3 = Vect2.makeFromPoints(p1, p2)
print p3
# (1.0, 0.0)
}}}
{{{Tkinter}}} is a Python binding to the [[Tcl|http://www.tcl.tk/]] (tool command language) GUI ''t''ool''k''it (Tk).
----
Every widget can use their {{{place}}}, {{{pack}}}, or {{{grid}}} methods depending on which //geometry manager// is being used.
----
''Overview'':
*{{{BitmapImage}}}  :  Image object for placing bitmap images on other widgets
*{{{Button}}}  :  a button.
*{{{Canvas}}}  :  Graphics drawing area: lines, circles, photos, text, etc.
*{{{Checkbutton}}} :  a checkbox.  Before you can make one, you first need to first create an object based on the {{{BooleanVar}}} class, to store the checkbox value:  {{{myCheckVal = BooleanVar()}}}.  FYI, the {{{BooleanVar}}} object has a {{{.get()}}} method, for 'getting' it's value.  Parameters:
**{{{variable}}}  :  pointer to the {{{BooleanVar}}} object created beforehand.
*{{{Entry}}}  :  Single-line text entry field. Methods:
**{{{.get}}}  :
**{{{.delete}}} :  See the Text.delete method below
*{{{Frame}}}  :
*{{{Label}}}  :  a string of text in a UI.
*{{{LabelFrame}}}  :  Labled Frame widget
*{{{Listbox}}}  :  List of selection names
*{{{Message}}}  :  Multi-line text display field (label)
*{{{OptionMenu}}}  :  Composite:  pull-down selection list.
*{{{PanedWindow}}}  :  Multipane window interface
*{{{PhotoImage}}}  :  Image object for placing full-color images on other widgets
*{{{Radiobutton}}}  :  Like {{{Checkbutton}}}, requires you first make an object to store the value.  In this case, it's a {{{StringVar}}} object.  {{{StringVar}}} has a {{{.get()}}} method, for getting its value.  {{{Radiobutton}}}s are 'grouped' together based on the {{{StringVar}}} they share.
*{{{Scale}}}  :  A slider widget with scalable position
*{{{Scrollbar}}}  :  Bar for scrolling other widgets
*{{{ScrolledText}}}  :  Composite:  text with attached scrollbar
*{{{Spinbox}}}  :  Multiple selection widget
*{{{Text}}}  :  Multi-line text browse/edit widget.  Supports fonts.  Methods:
**{{{.get}}}  :
**{{{.delete}}}  : for example:  {{{.delete(0.0, END)}}}  :  This deletes all text from column 0, row 0, to the end of the text box,
**{{{.insert}}} :  Similar style to the {{{.delete}}} method, but the second arg is the name of the string to insert.
*{{{Tk}}} :  For making the main window.  Also {{{Toplevel}}}.  Methods:
**{{{.title}}}  :  takes a string
**{{{.geometry}}}  :  Takes a string that is the width and height of the window:  {{{.geometry("250x150")}}}
Also see:
*[[UI basics with Tkinter]]
So, Python comes with a builtin function for this, which I failed to realize:
{{{
print divmod(15, 6)
(2,3)
# 15/6 = 2, remainder 3
}}}
But, my older notes below:
----
{{{
def divMod(val, divisor):
return val//divisor, val%divisor
}}}
{{{
print divMod(15, 6)
(2, 3)
# 15/6 = 2, remainder 3
}}}
{{{//}}} Is 'floor division':  Returns the value without remainder.
{{{%}}} is 'modulus'  : Returns just the remainder.
----
Both of these are present in Python's [[operator|http://docs.python.org/library/operator.html]] module as well:
{{{
import operator
def divWithRemainder(val, divisor):
return operator.floordiv(val, divisor), operator.mod(val, divisor)
}}}
Constants in Python have variable names in ALL CAPS, and should be authored at the top of the module, after imports, in global scope ([[PEP8|http://www.python.org/dev/peps/pep-0008/]]).
{{{
BOB = "human"
WIDTH = 256
ZERO = 0
}}}
By defining a constant, you're basically telling yourself to //not// change its value.  You still can, Python won't stop you, but that would be //bad// based on convention....
When you assign a variable to an object, you're simply pointing that variable to a location in memory (the object in question).  If you then pass that variable name as argument to other functions\methods, they all end up referencing the same location in memory.  Sometimes this is desired, and sometimes not.  If //not//, how can you get //unique// objects in memory, rather than //shared// references?

Build our example classes:
{{{
import copy

class Foo(object):
# simply holds some data, modified by external class
def __init__(self):
self.data = []

class Worker(object):
def __init__(self, foo, work):
self.foo = foo
self.work = work

def update(self):
# update each Foo class with work data
for f in self.foo:
f.data.append(self.work)

def printData(self):
for f in self.foo:
print f.data
}}}
Example #1: Build a list of two {{{Foo}}} objects, pass that list into our two {{{Worker}}} objects, and have each worker objects do some work:
{{{
# -----------------
myList = [Foo(), Foo()]

workerA = Worker(myList, "workerA")
workerB = Worker(myList, "workerB")

workerA.update()
workerA.printData()
#['workerA']
#['workerA']

workerB.update()
workerB.printData()
#['workerA', 'workerB']
#['workerA', 'workerB']
}}}
As you can see, the first object execution of {{{workerA}}} does what is expected.  But when you run {{{workerB}}}, it too sees the results from {{{workerA}}}.  Why is this?  Because both {{{Worker}}} objects are pointing to the same {{{list}}} object ({{{myList}}}), which in turn points to the same {{{Foo}}} objects.

Example #2.  We need each list to contain unique {{{Foo}}} objects.  That's what {{{copy.deepcopy()}}} does for us:
{{{
# -------
myList = [Foo(), Foo()]

workerA = Worker(copy.deepcopy(myList), "workerA")
workerB = Worker(copy.deepcopy(myList), "workerB")

workerA.update()
workerA.printData()
#['workerA']
#['workerA']

workerB.update()
workerB.printData()
#['workerB']
#['workerB']
}}}
Now, when the {{{Worker}}} objects do their work, the results are saved on unique {{{Foo}}} objects.
Much of this I have learned from others, we all ride on shoulders of giants.  But for what I present as my own work, it's under the below licence:
----
All information on this wiki is under the Apache Licence, v 2.0:
{{{

you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
}}}
To understand how this licence works see [[this overview|http://developer.kde.org/documentation/licensing/licenses_summary.html]].
But it pretty much means you can use the information on this wiki however you want, but I always appreciate credit where applicable.
This blog post:
http://ilab.cs.byu.edu/python/socketmodule.html
*[[Simple Echo Server|http://ilab.cs.byu.edu/python/socket/echoserver.html]]
*[[Simple Echo Client|http://ilab.cs.byu.edu/python/socket/echoclient.html]]
Provide great, simple examples on how to setup a network in Python.  If you evaluate the code in two separate shells, you can have the one talk to the other.

Link to Python's {{{socket}}} docs [[here|http://docs.python.org/library/socket.html]]
----
Some more examples on ~GitHub:
https://github.com/rspivak/csdesign
When authoring a [[function|Function]] or [[method|Method]] definition, you have the option of creating //parameters//, which accept [[arguments|argument]].  A nice thing about Python is its ability to enable you to author //default arguments//:  These are arguments that come with built-in values.  Example:
{{{
def foo(param1 = "arg1", param2 = "arg2"):
print param1
print param2

>> foo()
param1
param2
}}}
In the above example, the parameters had default default arguments, so the user did not need to input any.  They could if they wanted to of course:
{{{
>> foo(1,2)
1
2
}}}
You can also mix up which parameters have default arguments.  FYI, if any parameters //won't// have default arguments, they need to be listed //first//.
{{{
def foo(param1, param2 = "arg2"):
print arg1
print arg2

>> foo(1)
1
arg2
}}}
Since no default argument was provided for the first parameter in the function signature, I had to provide one.  If I had just executed {{{foo()}}}, I would have got a {{{TypeError}}}.
----
Python provides ways to find these default values.  Based on [[this link|http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy]] under 'Callable Types -> User-defined functions -> Special attributes' they have a table showing built in function object attrs that can be queried.  Specifically an attribute called {{{func_defaults}}}:
{{{
def func(a,b=2):
c=3
cnuf= func

print cnuf.func_defaults
(2,)
}}}

[[Welcome]]
I'm always surprised that the Perfoce ui doesn't give the user the option to delete all empty changelists.  Here's a simple function that will do it via the P4 API.  See [[Perforce access via Python]].
{{{
import P4

def deleteEmptyChangelists():
p4 = P4.P4()
# User needs to handle p4.errors & p4.warnings (not done in this code):
p4.exception_level = 0
p4.connect()
# Get all current changes for this client:
data = p4.run("changes", "-s", "pending", "-c", p4.client)

emptyChange = []
for d in data:
if 'shelved' in d:
# Archived\shelved, skip
continue

# Find files in this change:
files = p4.run("describe", d['change'])
if 'depotFile' not in files[0]:
emptyChange.append(d['change'])

if emptyChange:
for empty in emptyChange:
# Need to loop, can't delete multiple changes at once:
p4.run("change", "-d", empty)
print "Deleted empty changelists:", emptyChange
else:
print "No empty changelists to delete."

p4.disconnect()
return emptyChange
}}}
[[Perforce Python API docs|http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373]]
Maybe there's more than this?  A growing list of dev environments as I find them...
*[[Pythonista|http://omz-software.com/pythonista/]]
**[[Automating iOS: How Pythonista Changed My Workflow|http://www.macstories.net/stories/automating-ios-how-pythonista-changed-my-workflow]]
Also see [[Dictionary methods]]
Python Docs on [[Mapping Types|http://docs.python.org/lib/typesmapping.html]]
Also known as 'hash tables' or 'associative arrays'.
They are authored as a series of {{{key:value}}} pairs inside curly braces.
----
Good video on the guts of the dictionary:
http://python.mirocommunity.org/video/1591/pycon-2010-the-mighty-dictiona
----
''Tips and Tricks:''
{{{
# Simple creation:
d = {"duck": "eend", "water": "water"}
}}}
Get 'default' value (not sure why it's called 'set default').  If it can find the provided key, it returns that value.  If it can't find the key, it will return {{{None}}}, unless you supply an optional value to return:
{{{
dic = {"A": 1, "B": 2, "C": 3}
print dic.setdefault("A")
1
print dic.setdefault("Q", "missing")
"missing"
}}}
Create by zipping two lists:
{{{
keys = ["key1", "key2"]
values = [23, 53]
D = dict(zip(keys, values))
# {'key2': 53, 'key1': 23}
}}}
Concatenate two dicts:
{{{
d1 = {"a":1, "b":2, "c":3}
d2 = {"e":4, "f":5, "g":6}
d1.update(d2)
print d1
print d2
# {'a': 1, 'c': 3, 'b': 2, 'e': 4, 'g': 6, 'f': 5}
# {'e': 4, 'g': 6, 'f': 5}
}}}
Assignment:
{{{
foo = {}
foo["key"] = "value"
print foo
# {'key': 'value'}
}}}
Lookup:
{{{
d["duck"]
# "eend"
d["back"]
# raises KeyError exception
}}}
Delete, insert, overwrite:
{{{
del d["water"]
# {"duck": "eend", "back": "rug"}
d["back"] = "rug"
# {"duck": "eend", "back": "rug"}
d["duck"] = "duik"
# {"duck": "duik", "back": "rug"
}}}
Dictionaries can have lists as well:
{{{
dict["list"] = ["a", "b"]
# {'lista': ['a', 'b']}
dict["lista"] = dict["lista" + ["c"]
# {'lista': ['a', 'b', 'c']
}}}
Copy a dictionary:
{{{
D.copy()
}}}
Iterate through:
{{{
for key in D:
# stuff
}}}
Iterate over dictionary items in sorted fashion:
{{{
for key in sorted(D):
print key
}}}
Print all {{{key:value}}} pairs.  (Need to convert the values to strings to catenate properly)
{{{
# old way, slower, not using iterators:
for key in d.keys():
print key + "    :    " + str(d[key])

# new way, faster, using dictionary iterator:
for key in D:
print key, str(D[key])
}}}
----
Dictionaries can hold other dictionaries:
{{{
# method1:
d = {"key":"value"}
d["subD"] = {"subKey":"subValue"}
print d
# {'subD': {'subKey': 'subValue'}, 'key': 'value'}
}}}
{{{
# method 2:
d = {"key":"value", "subD":{"subKey":"subValue"}}
print d
# {'subD': {'subKey': 'subValue'}, 'key': 'value'}
}}}
update sub-dictionary:
{{{
# method 1
subD = d["subD"]
subD["newSubKey"] = "newSubValue"
print d
# {'subD': {'newSubKey': 'newSubValue', 'subKey': 'subValue'}, 'key': 'value'}
}}}
{{{
# method 2
dicA = {}
dicA["dicB"] = {}
dicA["dicB"]["dicC"] = {}
dicA["dicB"]["dicC"]["dicD"] = {}
print dicA
#  {'dicB': {'dicC': {'dicD': {}}}}

dicA["dicB"]["someList"] = ["a", "b", "c"]
dicA["dicB"]["dicC"]["dicCList"] = ["f", "g", "h"]
dicA["dicB"]["dicC"]["dicD"]["thingy"] = "dicD thing"
dicA["dicB"]["dicC"]["dicD"]["wingy"] = "dicD wing"
print dicA
#  {'dicB': {'someList': ['a', 'b', 'c'], 'dicC': {'dicCList': ['f', 'g', 'h'], 'dicD': {'thingy': 'dicD thing', 'wingy': 'dicD wing'}}}}
}}}
----
Search and update sub dictionaries:
{{{
d = {}
subDic = "info"
d[subDic] = {}
print d
#  {'info': {}}

searchKey = "bob"
newValue = "human"
if d.has_key(subDic):
subD = d[subDic]
# does the sub-dictionary have a certain key?
if not subD.has_key(searchKey):
subD[searchKey] = newValue
print d
# {'info': {'bob': 'human'}}
}}}
A dictionary of lists:
{{{
d = {"one":[], "two":[1,3]}

# update list:
d["one"].append("spam")
print d["one"]
# ['spam']

# get val from list:
num = d["two"][1]
# 3
}}}
Python Docs on [[Mapping Types|http://docs.python.org/lib/typesmapping.html]]
{{{
>>> d = {"duck": "end", "water": "h20"}
>>> for thing in dir(d):
>>>     print thing
clear
copy
fromkeys
get
has_key
items
iteritems
iterkeys
itervalues
keys
pop
popitem
setdefault
update
values
}}}
Also:
{{{
help ({})
}}}
[[This post|http://ibiblio.org/g2swap/byteofpython/read/exec-statement.html]] (from 'A Byte of Python') does a nice summary, pasted (and slightly modified) below:
----
The {{{exec}}} //statement// is used to execute Python ''statements'' which are stored in a string or file. For example, we can generate a string containing Python code at runtime and then execute these statements using the exec statement. A simple example is shown below.
http://docs.python.org/reference/simple_stmts.html#exec
{{{
>>> exec 'print "Hello World"'
Hello World
}}}
The {{{eval}}} //function// is used to evaluate valid Python ''expressions'' which are stored in a string. A simple example is shown below.
http://docs.python.org/library/functions.html#eval
{{{
>>> eval('2*3')
6
}}}


Found here: http://nodebox.net/code/index.php/Math
{{{
from math import sqrt
# 2d points:
def distance(x0, y0, x1, y1):
return sqrt(pow(x1-x0, 2) + pow(y1-y0, 2))
}}}
----
Another solution I came up with, works with 2d or 3d points:
{{{
from math import sqrt
def distBetween(x, y):
return sqrt( sum( map( lambda z:pow(z[0]-z[1], 2), zip(x,y) ) ) )
}}}
http://easygui.sourceforge.net/
<<<
~EasyGUI is a module for very simple, very easy GUI programming in Python.

Experienced Pythonistas need support for quick and dirty GUI features. New Python programmers need GUI capabilities that don't require any knowledge of Tkinter, frames, widgets, callbacks or lambda. This is what ~EasyGUI provides. Using ~EasyGUI, all GUI interactions are invoked by simple function calls.

~EasyGUI is different from other ~GUIs in that ~EasyGUI is NOT event-driven. It allows you to program in a traditional linear fashion, and to put up dialogs for simple input and output when you need to. If you have not yet learned the event-driven paradigm for GUI programming, ~EasyGUI will allow you to be productive with very basic tasks immediately. Later, if you wish to make the transition to an event-driven GUI paradigm, you can do so with a more powerful GUI package such as anygui, ~PythonCard, Tkinter, wxPython, etc.
<<<
I use [[Wing IDE]] for all my Python coding.  Here is a hotkey you can use to open Windows Explorer to where the active document is saved.  I map this to {{{Ctrl+E}}}, and it is saved in a module here:
{{{
C:\Users\epavey\AppData\Roaming\Wing IDE 4\scripts\wingHotkeys.py
}}}
{{{
# wingHotkeys.py

import os
import wingapi

def exploreActiveDocument():
"""
For the open document, open a Windows Explorer to that directory.
"""
editor = wingapi.gApplication.GetActiveEditor()
if editor is None:
return
doc = editor.GetDocument()
filename = doc.GetFilename()
if os.path.isfile(filename):
path = os.path.split(filename)[0]
os.system('explorer %s'%path)
print "Explored to: %s"%path
else:
print "Current document doesn't have a valid path: %s"%filename
}}}
A link to all file object methods
http://docs.python.org/lib/bltin-file-objects.html

http://code.activestate.com/recipes/66527/
Given a [[package|Packages]] named {{{myPackage}}} setup like so:
*/pythonStuff (in Python path)
**/myPackage
***/{{{__init__.py}}}
***/{{{moduleA.py}}}
***/{{{moduleB.py}}}
How can you query the [[module]]s that live in {{{myPackage}}}?
!!!Solution A:
Use the tools Python provides you:
{{{
# testA.py
import pkgutil
import myPackage

modules = []
for p in pkgutil.iter_modules(myPackage.__path__):
modules.append(p[1])
print modules
}}}
prints:
{{{
['moduleA', 'moduleB']
}}}
After I wrote this, I found a blog post here doing something similar:
http://jeetworks.org/node/49
!!!Solution B:
Roll your own.  I actually came up with this solution first (with help from the Python Tutor email list), before I found out about {{{pkgutil.iter_modules()}}}.
What we do is auto-populate the {{{__all__}}} attr in the package's {{{__init__.py}}} 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.
See docs on the package {{{__all__}}} attr [[here|http://docs.python.org/tutorial/modules.html#importing-from-a-package]]
{{{
# /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, "*.py") )

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)

}}}
Then to test:
{{{
# testB.py
import myPackage

print myPackage.__all__
}}}
prints:
{{{
['moduleA', 'moduleB']
}}}
General notes on floats and ints:
http://docs.python.org/lib/typesnumeric.html
{{{
7/3
2 # obviously, we're missing something here...
7.0/3.0
2.3333333333333335 # more what we were after.
}}}
{{{
>>> floatFoo = 1.0
>>> for thing in dir(floatFoo):
>>>     print thing
# there are no methods for floats?
}}}
I don't think so, they're //immutable//
An easy way to control the printed result of a small floating point number is with string formatting:
{{{
num = 12345.12567
print "%.2f" % num
# 12345.13
}}}
the {{{%.2f}}} will only display {{{2}}} places to the right of the decimal point, with rounding.
----
The {{{round()}}} function works pretty well too:
{{{
print round(1.23423423434, 3)
1.234
}}}
----
Also see:
*[[How can I reduce the precision of a floating point number?]]
*http://docs.python.org/library/stdtypes.html#string-formatting
*http://docs.python.org/library/functions.html#round
I found this code snippet a way down this page:
http://nodebox.net/code/index.php/Core_Image
(credit to Duane Bailey)
{{{
def fractal(x, y, depth=64):
z = complex(x, y)
o = complex(0, 0)
for i in range(depth):
if abs(o) <= 2: o = o*o + z
else:
return i
return 0 #default, black

pixels = []
w = h = 150
for i in range(w):
for j in range(h):
v = fractal(float(i)/w, float(j)/h)
pixels.append(color(v/10.0, v/20.0, v/10.0))

l = canvas.append(pixels, w, h)
}}}
[img[http://nodebox.net/code/data/media/coreimage-layers7c.jpg]]
I've not tried it myself yet, but it looked very elegant.
Also known as procedures (in other languages, if they //don't// [[return]] values), gives you a way to name a piece of code, and pass arguments to it.  The '{{{def}}}' statement creates a 'function //object//'.  Function objects can do work, [[return]] data, or do both.  See [[Python docs|http://docs.python.org/ref/function.html]].
{{{
def square(x):
"""This is a 'docstring'.  They're good to have,
so users know what the function is doing."""
return x*x
}}}
{{{
>>> print square(2)
4
}}}
<<<
''Note'':  I wrote the below section before I really grasped the difference in concept between 'parameters' and 'arguments'.  So //everything// I discuss below this note is refers to 'arguments only'.  However, parameters and arguments are two different things, which I'll describe here first, and maybe get around to re-authoring the below text later ;)

A function can take a series of identifiers, also known as //parameters//:
{{{
def foo(parameter1, parameter2="defaultArgument"):
# do work
}}}
And when you call to the function you pass values, known as //arguments//, to the parameters:
{{{
foo('argument1')
}}}
Based on this example, the //parameter// {{{parameter2}}} has a //[[default argument|Default Arguments]]// with the string //value// of {{{"defaultArgument"}}}.  If you don't pass an argument to {{{parameter2}}} when the function is called, then the //default argument// is used instead.  The //parameter// {{{paramter1}}} has the argument {{{'argument1'}}} passed in.

So when 'parameters' are discussed, they refer to the 'identifier name' the function expects, and when 'arguments' are discussed, it refers to the 'values' passed into the function.
<<<
Functions can have [[argument]]s defined, and passed in, in four different ways.  Check the tiddler on [[argument]]s for details.

Crazy as it sounds (to me, when I first learned it), functions can be nested in functions.  However, I hear as a general rule this should be avoided, just to make the code simpler to understand.  I have yet to find a reason to do this... ;)
{{{
def func(A):
def nestFunc(B):
return A ** B
return nestFunc

foo = func(2)
print foo
# <function nestFunc at 0x00C01A70>
print foo(4)
# 16
}}}
In the above example, we define a function {{{func()}}}, that has a nested function {{{nestFunc()}}}.  When you execute {{{func()}}}, it passes its [[argument]] to {{{nestFunc}}}, and then //returns// the function object {{{nestFunc()}}}, named {{{foo}}}.  When you then execute that function object passing in some argument, the new function object returns back the square of that value.  What's interesting, is even after {{{func()}}} exits, the newly created function object {{{foo}}} still remembers the local variable that was passed into it ({{{A}}}).  I read that earlier version of Python (<2.5) would fail, because they'd not search the enclosing scope of the defined function.  But now a'days that appears to be handled nicely...
----
Functions //are// objects, and can be passed around just like any other object type (string, float, etc).  They're just variables assigned in the current scope.  They can be stored in other data structures, and called to later:
{{{
def printMe(x):
print x
name1, name2 = printMe, printMe
names =[(name1, "george"), (name2, "larry")]
for(func, arg) in names:
func(arg)
# george
# lary
}}}
----
[[lambda|Understanding lambda]]'s are also called 'anonymous functions'.  They are 'expression forms' that generate function objects.
----
You can also make '''generator functions''' (see Python docs [[here|http://docs.python.org/tut/node11.html#SECTION00111000000000000000000]], and [[here|http://docs.python.org/ref/yieldexpr.html]]).  Generators allow values to be sent back on at at time, so they can be resumed at a later point.  They 'generate' a sequence of values over time.  This is done by introducing the {{{yield}}} statement ([[python docs|http://docs.python.org/ref/yield.html]]) to the function instead of a [[return]] statement.  You still can have a {{{return}}} statement in the function:  If it is reached, the function is exited.
{{{
def genMult(num, mult):
for i in range(num):
yield mult * i

x = genMult(10, 5)
print x.next()
# 0
print x.next()
# 5
print x.next()
# 10
# etc...
}}}
Generator objects have a {{{.next()}}} method that can be called to to provide the next iteration of data.
It is explained that an advantage of generators is that they only process data 'one step at a time', rather than 'all at once' based on a normal function def.  This means they can be more efficient when dealing with large lists.
When done they raise a {{{StopIteration}}} exception.  They also have methods {{{.send(value)}}} and {{{.throw(type)}}}.
----
You can also do a mashup of list comprehensions and generators to make '''generator expressions'''.  They act like list comprehensions, but are surrounded in parenthesis instead of square brackets.  See notes on [[list comprehension]].
----
Function objects (yes, functions are objects) have a number of special attributes that can be accessed (docs found [[here|http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy]]):
| ''Attribute'' | ''Meaning'' | |
| {{{func_doc}}} |The function’s documentation string, or None if unavailable |	Writable |
| {{{__doc__}}} |	Another way of spelling func_doc |	Writable |
| {{{func_name}}} |The function’s name |	Writable |
| {{{__name__}}} |Another way of spelling func_name |	Writable |
| {{{__module__}}} |The name of the module the function was defined in, or None if unavailable. |	Writable |
| {{{func_defaults}}} |A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value |	Writable |
| {{{func_code}}} |The code object representing the compiled function body. |	Writable |
| {{{func_globals}}} |A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined. |	Read-only |
| {{{func_dict}}} |The namespace supporting arbitrary function attributes. 	| Writable |
| {{{func_closure}}} |None or a tuple of cells that contain bindings for the function’s free variables. |	Read-only |

Official Python list:
http://wiki.python.org/moin/PythonGames
I could duplicate it all here, but what's the point?
----
*[[Kivy|http://kivy.org/#home]]
!pydoc:
http://www.python.org/doc/lib/module-pydoc.html
{{{pydoc}}} is a built-in Python module.
Here's a simple example from the commandline:
{{{
>python -m pydoc -w C:\pystuff\myModule.py
}}}
This will save this file:
{{{
C:\Python26\Lib\myModule.html
}}}
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 {{{pydoc.py}}} is located, it will save the resultant .html files in that dir:
{{{
C:\pystuff>python C:\Python26\Lib\pydoc.py -w C:\pystuff
}}}
It should be noted that {{{pydoc}}} needs to import the module to build documentation on it.  If the imported module imports other modules that for some reason aren't part of the current Python path, it will raise a {{{exceptions.ImportError}}}.
!Epydoc:
http://epydoc.sourceforge.net/
I'm currently using this to generate documentation.  I like it over {{{pydoc}}} because it won't get hung up on missing imported modules:   Working in Autodesk's Maya, It wrappers its scripting language mel in Python.  but the bulk of it's {{{maya.cmds}}} package is actually generated on the fly when the program starts, there are no actual modules on disk.  This cause imports of them to fail for things like {{{pydoc}}} (since Maya isn't running when the documentation is being generated), but {{{Epydoc}}} can work around it.
!docutils
http://docutils.sourceforge.net/
!Sphinx
http://sphinx.pocoo.org/
Conceptually, it's better to have the user access a method for data, rather than an attribute directly.  You can write methods to interface this data for the user:
{{{
class Foo(object):
def __init__(self):
self.__val = 0
def get_val(self):
return self.__val
def set_val(self, new_val):
self.__val = new_val
}}}
I seem to have a growing number of subjects on this topic :)  Let's collect some more info here!

In a module, you can have data defined in a variety of different ways:  The module itself, functions, classes, etc.  How can that module query both where it lives on disk, and it's module path, relative to any possible [[package|Packages]] it lives in?

*{{{__file__}}} is a special module attribute that stores the full path to where is is saved.
*{{{__module__}}} is a special class attribute storing the module where the class was created.
*{{{__name__}}} is a special module attribute (for classes too, but for class names) that returns back the name of the module it's called in.  The only exception to this is that if the module in question is being executed directly (from a command shell, icon-double-click, etc) this value will = '{{{__main__}}}'
Docs on [[special attributes|http://docs.python.org/library/stdtypes.html#special-attributes]]
{{{
# nametest.py in the animation package

print "Module full path:", __file__

class Spam(object):
def __init__(self):
print "Class defined in module:", self.__module__

def main():
print "Function defined in module:", __name__
}}}
Execution:
{{{
import animation.nametest
animation.nametest.main()
eggs = animation.nametest.Spam()
}}}
Results:
{{{
Module full path: D:\pystuff\animation\nametest.py
Function defined in module: animation.nametest
Class defined in module: animation.nametest
}}}
----
Also see:
*[[How can I query where a module is saved?]]
*[[How can a class query where it was created?]]
*[[How can a module reference where itself has been saved?]]
*[[What's up with: if __name__ == "__main__":]]
*[[How can a module query relative directories to where it has been saved?]]
Given a list of names, that may or may not have numbers on the end of them, add a certain value to each of the numbers, and give back a new list of names.  Be conscious of padding:
{{{
import re
increment = 1
files = ["file0001", "file0022", "file0033", "filex"]
newFiles = []
for f in files:
try:
num = re.findall('[0-9]+$', f)[0] name = f[:-(len(num))] + str(int(num)+increment).zfill(len(num)) newFiles.append(name) except IndexError: newFiles.append(f) print newFiles }}} {{{ ['file0002', 'file0023', 'file0034', 'filex'] }}} {{{ mylist = [6, 5, 4, 3, 2, 1, 0] print mylist.index(4) # 2 }}} The number 4 is in the 2nd index position in the list. If it isn't in the list, it will raise a {{{ValueError}}} exception. I recently ran into this problem: I had multiple lists of multiple items. I needed to find items in the supplied lists that only occurred once in all the lists. I thought I could implement this via {{{sets}}}, but I just couldn't make it happen. Maybe it is doable? At any rate, here is the quick solution I came up with: For each item in each list, put it in a dict, and count the number of times its been added. Return only those things added once. {{{ def getUniqueItems(*lists): """ *lists : each item passed in is expected to be a list. return : list : Of unique items only found once in all lists. """ data = {} for nodes in lists: for item in nodes: if item in data: data[item] += 1 else: data[item] = 1 result = [item for item in data if data[item] == 1] return result }}} {{{ a = ['a','b','c','d'] b = ['b','c','d','e'] c = ['c', 'd', 'e', 'f'] unique = getUniqueItems(a,b,c) # ['a', 'f'] }}} Found here: http://nodebox.net/code/index.php/Math {{{ # Fibonacci sequence def fib(n): if n == 0: return 0 if n == 1: return 1 if n >= 2: return fib(n-1) + fib(n-2) def goldenratio(n, f=4): # Returns two proportional numbers whose sum is n. f = max(1, min(f, 10)) n /= float(fib(f+2)) return n*fib(f+1), n*fib(f) }}} Thought I would log pages that show 'good code examples' in Python. ---- !Conventions *[[PEP8|http://python.org/dev/peps/pep-0008/]], of course. *[[Code Like a Pythonista: Idiomatic Python|http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html]] !Videos: *[[PyCon 2011: Python for High Performance Computing|http://blip.tv/file/4881240]] !Examples \ Recipes: *Good examples that start at one line and work up to 33 lines: **http://wiki.python.org/moin/SimplePrograms *Recipes: **http://code.activestate.com/recipes/langs/python/ *Working with the {{{itertools}}} module: **http://docs.python.org/dev/library/itertools.html#recipes *Sorting mini-how to: **http://wiki.python.org/moin/HowTo/Sorting !Visualization: Tools to help visualize efficiency *[[RunSnakeRun|http://www.vrplumber.com/programming/runsnakerun/]] : ...is a small GUI utility that allows you to view (Python) cProfile or Profile profiler dumps in a sortable GUI view. It allows you to explore the profiler information using a "square map" visualization or sortable tables of data. *[[Gprof2Dot|http://code.google.com/p/jrfonseca/wiki/Gprof2Dot]] : ...is a Python script to convert the output from many profilers into a dot graph. *[[pycallgraph|http://pycallgraph.slowchop.com/]] : ...is a Python module that creates call graphs for Python programs. !Efficiency: *[[Python Idioms and Efficiency|http://bayes.colorado.edu/PythonIdioms.html]] *[[Python Speed/Performance Tips|http://wiki.python.org/moin/PythonSpeed/PerformanceTips]] *[[Python Patterns - An Optimization Anecdote|http://www.python.org/doc/essays/list2str.html]] - Guido van Rossum *[[Efficient String Concatenation in Python|http://www.skymind.com/~ocrow/python_string/]] *[[Python 3: Informal String Formatting Performance Comparison|http://www.protocolostomy.com/2011/01/02/python-3-informal-string-formatting-performance-comparison/]] ---- {{{array}}} module: "Efficient arrays of numeric values" *http://docs.python.org/library/array.html ---- !!!Notes from the above docs: From: http://docs.python.org/dev/library/itertools.html#module-itertools >...the multiplication operator can be mapped across two vectors to form an efficient dot-product: {{{ import operator from itertools import imap def dotproduct(vec1, vec2): return sum(imap(operator.mul, vec1, vec2)) print dotproduct((2,2,2), (3,3,3)) # 18 }}} >Note, many of the above recipes ''can be optimized by replacing global lookups with local variables defined as default values''. For example, the dotproduct recipe can be written as: {{{ def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul): return sum(imap(mul, vec1, vec2)) }}} ---- Stuff I've read: *"Python accesses local variables much more efficiently than global variables." *"...function call overhead in Python is bigger than for loop overhead." (guido) *"...local variable lookups are much faster than global or built-in variable lookups: the Python "compiler" optimizes most function bodies so that for local variables, no dictionary lookup is necessary, but a simple array indexing operation is sufficient.""" (guido) ---- Looping: *Use {{{map}}} rather than a for-loop, since {{{map}}} takes place entirely in the C API and never has to bind loop variables to Python objects. **Calls to {{{map}}} happen in C, while calls external to it happen in the bytecode interpreter, which is slower. *List comprehensions are often as fast or faster than equivalent use of {{{map}}}. {{{ # Standard: newlist = [] for word in oldlist: newlist.append(word.upper()) # Faster: newlist = map(str.upper, oldlist) # Fastest: newlist = [s.upper() for s in oldlist] }}} ---- Making strings: {{{ # No: for s in strings: result += s # Yes: result = ''.join(strings) }}} Although it looks like current version of Python (2.6ish) don't suffer this problem, and string concatenation is just as fast as the join method: http://wiki.python.org/moin/ConcatenationTestCode ---- Use coercion if an object must be a particular type: {{{ # No: isinstance(str, x) # Yes: str(x) }}} ---- To quickly reverse-sort a list: {{{ list.sort() list.reverse() }}} ---- Finding largest (or smallest) value in a list. {{{ from random import randint bigListA = [randint(0, 1000) for item in range(1000)] bigListB = bigListA[:] bigListC = bigListA[:] def sortA(): biggestA = sorted(bigListA)[-1] def sortB(): biggestB = max(bigListB) def sortC(): bigListC.sort() biggestC = bigListC[-1] }}} Running the above code through a timer gave these results: {{{ Completed 1000 iterations of sortA() in 0.281000 seconds, 0.000281 per loop Completed 1000 iterations of sortB() in 0.028000 seconds, 0.000028 per loop Completed 1000 iterations of sortC() in 0.023000 seconds, 0.000023 per loop }}} {{{max()}}} has nearly a 10x speed increase over {{{sorted()}}}, and the in-place {{{list.sort()}}} is roughly 21% faster than {{{max()}}}. ---- To swap variables without making temporary ones: {{{ a, b = b, a }}} ---- Use tests for object identity when appropriate: {{{ # No: if x != None # Yes: if x is not None: }}} It is much more efficient to test objects for identity than equality, because identity only checks their address in memory (two objects are identical if they are the same object in the same physical location) and not their actual data ---- Use dictionaries (or sets) for searching, not lists. Searching a list for an item is linear-time, while searching a dict or set for an item is constant time. This can often let you reduce search time from quadratic to linear. ---- Reversing lists: Which is faster: Adding a bunch of items to a list and reversing it, or inserting each item to the beginning of the list? (from the book "Python Algorithms") Append\reverse: {{{ count = 10**5 nums = [] for i in range(count): nums.append(i) nums.reverse() }}} Insert in front: {{{ count = 10**5 nums = [] for i in range(count): nums.insert(0, i) }}} Running each of these through a timer tool, "append\reverse" took 0.297 seconds, while "insert in front" took 5.516 seconds (18.5x slower, on my old laptop). So it's safe to say that {{{list.append()}}} is //way more efficient// than {{{list.insert()}}} :-) ----  Found this snippet online by Stephen Akiki: http://code.activestate.com/recipes/577282-finding-the-gcd-of-a-list-of-numbers-aka-reducing-/ {{{ def GCD(a,b): a = abs(a) b = abs(b) while a: a, b = b%a, a return b def GCD_List(list): return reduce(GCD, list) }}} {{{ print GCD_List([8, 24, 12]) # 4 }}}  <<timeline>> (Referenced from Learning Python by Mark Lutz) Of the built-in //object types//, how are they generally organized? http://www.python.org/doc/current/lib/module-types.html#l2h-858 http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy http://docs.python.org/library/stdtypes.html *Numbers (immutable) **Integers ***Integer ***Long ***Boolean **Float **Complex **Set **Decimal *Collections **Sequences ***Immutable ****String ****Unicode ****Tuple ***Mutable ****List **Mappings ***Dictionary *Callables **Functions **Class **Method ***Bound ***Unbound *Other **Module **Instance **File **None *Internals **Type **Code **Frame **Traceback ---- Also see: *[[What are Pythons built in types?]] Python ships with many 'built-in' functions and modules. I'm used to the scripting language MEL, which has a core library of 'mel commands' (equivalent to functions) and associated 'mel scripts' (equivalent to modules). But coming to Python, the way it delivers and organizes its data seemed a bit more confusing. But I think I've figured how how it breaks down: *Python ships with many (built-in) [[functions|Function]], which live in the {{{__builtin__}}} //module//. These functions include such as {{{abs}}}, {{{dir}}}, {{{help}}}, {{{map}}}, {{{open}}}, {{{raw_input}}}, {{{range}}}, etc, which flush out the basic requirements of the language. **Link to the full list of built-in functions [[HERE|http://docs.python.org/lib/built-in-funcs.html]] {{{ # if you want to pull help on __builtin__, you need to import it first: import __builtin__ help(__builtin__) }}} *Python ships with many (built in) [[module]]s (of which {{{__builtin__}}} from above is a member). These obviously extend the built-in functions, and include such as {{{sys}}}, {{{os}}}, {{{re}}}, {{{pickle}}}, etc. Each of these usually has many associated functions that you end up accessing, and not the module name itself. **Link to the 'Global Module Index' [[HERE|http://docs.python.org/modindex.html]] **Install location: {{{..\PythonXX\Lib\}}}  http://docs.python.org/library/time.html#time.sleep {{{ import time print "waiting half a second:" time.sleep(.5) print "done!" }}} Via the 'Python Image Library' (PIL) http://www.pythonware.com/products/pil/index.htm "The Python Imaging Library (PIL) adds image processing capabilities to your Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities."  I ran into a situation where I wanted to pass in an arbitrary number of keyword [[arguments|argument]] to a new object, and have them all added as [[attributes|attribute]]. This is what I came up with, using {{{setattr}}}: {{{ class Spam(object): def __init__(self, **kwargs): for arg in kwargs: setattr(self, arg, kwargs[arg]) spam = Spam(argA=23) for attr in dir(spam): if not attr.startswith('__'): print attr, " : ", eval('spam.%s'%attr) }}} {{{ argA : 23 }}}  Certain packages expect to be placed in Python's {{{\site-packages}}} dir, which by default lives here: {{{ C:\Python26\Lib\site-packages }}} But maybe you want to set up a {{{site-packages}}} dir somewhere else. How to do? Easy enough, via the {{{site}}} module: {{{ newPackageDir = "c:/temp/hackPackages" site.addsitedir(newPackageDir ) }}} http://docs.python.org/library/site.html#site.addsitedir ---- Also see: *[[Using the sitecustomize & usercustomize modules]] *[[How can I set Python's path?]] Sometimes I want to catch an exception, print some useful info to the user, and then also print the traceback info from the exception as well. One way to do it is listed below. Docs: *http://docs.python.org/library/traceback.html *http://docs.python.org/library/sys.html#sys.exc_info *http://docs.python.org/library/exceptions.html To do this, we have to import the {{{traceback}}} and {{{sys}}} modules: The third item {{{sys.exc_info()}}} returns is a {{{traceback}}} object, and we can use the {{{traceback}}} module's {{{print_tb()}}} function to print out interesting info about it. Make a module that will raise an exception: {{{ # exceptionMaker.py def main(): raise Exception("This is an exception in exceptionMaker.py") }}} This module will catch the exception, and print out useful info in the process. {{{ # tracebackCatcher.py import sys import traceback import exceptionMaker try: exceptionMaker.main() except Exception, e: tb = sys.exc_info()[2] print "Traceback:" traceback.print_tb(tb) print "traceback type:", type(tb) print "Exception type:", type(e) print "Exception.args:", e.args # Deprecated in Python 2.6 and newer, just use # e.args[0] instead: #print "Exception.message:", e.message }}} Run {{{tracebackCatcher.py}}}: {{{ import tracebackCatcher }}} prints: {{{ Traceback: File "D:\python\tracebackCatcher.py", line 8, in <module> exceptionMaker.main() File "D:\python\exceptionMaker.py", line 4, in main raise Exception("This is an exception in exceptionMaker.py") traceback type: <type 'traceback'> Exception type: <type 'exceptions.Exception'> Exception.args: ('This is an exception in exceptionMaker.py',) }}}  [[os.path.getsize(pathToMyFile)|http://docs.python.org/2/library/os.path.html#os.path.getsize]]  Humans make mistakes. So it's important to check that your xml file is well-formed. Here's two xml files to test. The second one has a bad tag: {{{note.xml}}}: {{{ <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> }}} {{{note_bad.xml}}}: (see the last tag, it has an extra 's') {{{ <?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </notes> }}} First, a parser object is created. Then a (generic) content handler is added to the parser. Then with a {{{try}}} statement runs, catching exceptions resulting from poorly formed xml documents: {{{ from xml.sax.handler import ContentHandler import xml.sax xmlFiles = ["note.xml", "note_bad.xml"] # create the parser xmlparser = xml.sax.make_parser() # attach a generic content handler to parser: xmlparser.setContentHandler(ContentHandler()) # Parse the files and handle exceptions: for xmlF in xmlFiles: try: xmlparser.parse(xmlF) print "%s is well formed!" % xmlF except Exception, err: print "Problem: %s:\n\t %s is not a well-formed file :(" % (err, xmlF) }}} {{{ c:\temp\xml\note.xml is well formed! Problem: c:\temp\xml\note_bad.xml:8:2: mismatched tag: c:\temp\xml\note_bad.xml is not a well-formed file :( }}} To keep the variable around, but give it 'no value' irregardless of what type of object the pointed too, you can set it to {{{None}}}. This however physically changes the type of object it points to as {{{NoneType}}}: It's still pointing to an object is simply a "None" object: {{{ spam = "a string" print spam >>> a string print type(spam) >>> <type 'str'> spam = None print spam >>> None print type(spam) >>> <type 'NoneType'> }}} If you want to completely delete the variable (unbind the name \ object reference), you can use the {{{del()}}} function: {{{ del(spam) print spam >>> Traceback (most recent call last): >>> File "<string>", line 1, in <string> >>> NameError: name 'spam' is not defined }}} If you want to 'clear' the object the variable points at, but keep its object type the //same as when it was last defined//, you can use this method: {{{ # make a dictionary: spam = {"key1":"value1", "key2":"value2"} print spam >>> {'key2': 'value2', 'key1': 'value1'} spam = spam.__class__() print spam >>> {} print type(spam) >>> <type 'dict'> }}} Mac's come pre-installed with Python (awesome). However, 64-bit Python isn't as widespread as 32-bit, so sometimes you need to help you Mac to tell it what version to use. In a Mac shell: {{{ defaults write com.apple.versioner.python Prefer-32-Bit -bool yes }}} And here's some Mac docs: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/python.1.html These convert you code into what is known as 'frozen binaries'. *py2exe (Windows) : http://www.py2exe.org/ *~PyInstaller (Windows, Linux, Unix\Irix) : http://pyinstaller.python-hosting.com/ *freeze : (Unix) : http://wiki.python.org/moin/Freeze ---- *[[Dependency Walker|http://www.dependencywalker.com/]] : Windows, for finding DLL dependency issues. ---- ''gui2exe'' is a ui front end to many of the above applications: *Main page: http://code.google.com/p/gui2exe/ *Tutorial: http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/ http://docs.python.org/library/shutil.html {{{ import shutil # if the destination file already exists: shutil.copyfile('c:/temp/from.txt', 'c:/temp/to.txt') # if the destination file doesn't exist: shutil.copy('c:/temp/from.txt', 'c:/temp/to.txt') shutil.move('c:/temp/from.txt', 'c:/goo/from.txt') }}} [[itertools.count()|http://docs.python.org/library/itertools.html#itertools.count]] {{{ from itertools import count for i in count(start=0, step=1): # do something with i }}} The {{{count()}}} function could be authored like so (from the docs): {{{ def count(start=0, step=1): n = start while True: yield n n += step }}} ---- The below example is doing the same as above, but takes more lines (but no extra import). {{{ i = 0 while True: # do something with i i += 1 }}} As of Python 2.6: Use the {{{fraction}}} module: http://docs.python.org/library/fractions.html From the docs: {{{ from fractions import Fraction Fraction(16, -10) # Fraction(-8, 5) Fraction(123) # Fraction(123, 1) Fraction() # Fraction(0, 1) Fraction('3/7') # Fraction(3, 7) # [40794 refs] Fraction(' -3/7 ') # Fraction(-3, 7) Fraction('1.414213 \t\n') # Fraction(1414213, 1000000) Fraction('-.125') # Fraction(-1, 8) }}}  The [[zipfile|http://docs.python.org/library/zipfile.html]] module (among others). Good notes here: http://effbot.org/librarybook/zipfile.htm Really simple example for making zips: {{{ # zipper.py import os import locale import zipfile def main(files, zipname, storePaths=False): """ files : list : List of full file-paths to zip. zipname : string : Full path of .zip file to save. storePaths : bool : Default False. If storePaths == True, then full directory paths are saved to each file in the zip. If False, then all files are at root level of zip. """ loc = locale.getdefaultlocale()[1] zipper = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) for f in files: name = f.encode(loc) if storePaths: zipper.write(name) else: zipper.write(name, os.path.basename(name)) zipper.close() for f in files: print "\t",f print "Added above files to zip:", zipFileName }}}  {{{ import os os.remove("c:/temp/foo.mel") # or you can use: os.unlink("c:/temp/foo.mel") }}} {{{mimetypes}}} bases its search of the extension it seems. http://docs.python.org/library/mimetypes.html There is also [[Python Magic|http://pypi.python.org/pypi/python-magic/]] which seems more robust, but its for Linux only. {{{ import mimetypes mimetypes.init() j = "C:/temp/test.jpg" t = "C:/temp/test.txt" guessJ = mimetypes.guess_type(j)[0] guessT = mimetypes.guess_type(t)[0] print guessJ, guessT # image/jpeg text/plain }}}  Say you have two lists with the same number of elements, and you want to subtract each member from the corresponding other. How to easily do: {{{ foo = [4, 4, 5] goo = [1, 1, 5] e = map(lambda x, y: x-y, foo, goo) print e # [3, 3, 0] }}} You can also do this on an arbitrary number of lists with a bit more work (and I have a feeling there's an even slicker way to do this...): {{{ dataA = [1,2,3] dataB = [-1,-2,-3] dataC = [10,10,10] listOfLists = [dataA, dataB, dataC] def adder(*args): val = 0 for v in args: val += v return v result = map(adder, *listOfLists) print result # [10, 10, 10] }}} By putting the asterix ({{{*}}}) in front of our variable name ({{{*listOfLists}}}), the map command will unpack the sub-lists from {{{listOfLists}}}, incrementally grab the same indicies from each, package them as a new list (first loop builds a list {{{[1, -1, 10]}}}, second loop is {{{[2, -2, 10]}}}, etc), pass them to {{{adder()}}}, which again, via the {{{*args}}} notation, unpacks that new list as individual elements, and processes them.  You override the {{{__nonzero__}}} method http://docs.python.org/reference/datamodel.html#object.__nonzero__ {{{ class Foo(object): def __init__(self, arg=None): self.arg = arg def __nonzero__(self): if self.arg: return True else: return False f = Foo() f.arg = "stuff" if f: print "good" else: print "bad" # good }}} ---- Also see: *[[Operator Overloading]] Good blog post on it here: http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/ Code snippets from that link: {{{ # Python 2 code import urllib import urllib2 import requests url = 'http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip' print "downloading with urllib" urllib.urlretrieve(url, "code.zip") print "downloading with urllib2" f = urllib2.urlopen(url) data = f.read() with open("code2.zip", "wb") as code: code.write(data) print "downloading with requests" r = requests.get(url) with open("code3.zip", "wb") as code: code.write(r.content) }}} {{{ # Python 3 code import urllib.request, urllib.parse, urllib.error url = 'http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip' print("downloading with urllib") urllib.request.urlretrieve(url, "code.zip") print("downloading with urllib2") f = urllib.request.urlopen(url) data = f.read() with open("code2.zip", "wb") as code: code.write(data) }}} [[pprint|http://docs.python.org/library/pprint.html]] {{{ import pprint myD = {"a":"A", 2:[1,2], "foo":{"subDict":[1,2,["a", 23]], "stuff":("my", "tupple", 52)}} pprint.pprint(myD, width=32) }}} prints: {{{ {2: [1, 2], 'a': 'A', 'foo': {'stuff': ('my', 'tupple', 52), 'subDict': [1, 2, ['a', 23]]}} }}} The built-in {{{operator}}} module has all the goodness that you need: {{{ import operator num = operator.add(10,5) print num # 15 }}} This is closely tied to [[emulating numeric types|How can I have objects add themselves together? Subtract one another, etc?]] and 'operator overloading' in OOP. Presuming you're doing this in another module, you'll need to add its location to the path first (unless there is a fancier way). In the below example, I define the full path to the module in a variable. I then extract just the path and module name. Add the path to {{{sys.path}}} then import the module name via {{{exec}}}. {{{ import os import sys modulePath = r'c:\python\someModule.py' moduleDir, moduleName = os.path.split(modulePath) module, ext = os.path.splitext(moduleName) sys.path.append(os.path.normpath(moduleDir)) exec('import %s as myModule'%module) # presuming the module has a main function() myModule.main() }}} ---- Available in Python 2.7 and //newer//, the {{{runpy.run_path()}}} function is available: {{{ import runpy modulePath = r'C:\path\to\my\module.py' runpy.run_path(modulePath) }}} http://docs.python.org/library/runpy.html ---- Found in [[this post|http://sayspy.blogspot.com/2011/07/how-to-import-module-from-just-file.html]], this code works in Python 3 (but not Python 2.x): {{{ mod = exec(open('path', 'r').read(), __import__('sys').modules.setdefault('name', __import__('imp').new_module(name)).__dict__) }}} The above was considered a hack to simply do the whole thing on one line. But that blog post exposes other examples for doing this as well. ---- Another option of course is simply add that modules path to the path, then import it: {{{ import sys sys.path.insert(0,"/path/to/module") import module }}} I had an instance where I needed to execute a module that was outside my Python path (which launches a UI), and I didn't want to first add its directory as part of my Python path. Furthermore, the module in question was named illegally: {{{ someModule.2.py }}} It had an extra {{{.2}}} in its name, which really upsets python on import statements. To work around this, from a Python module, I called //to// Python at the system level (dos, Win7), to execute the module and launch the UI. The key was to use the system {{{chdir}}} to first put the system at that dir, //then// execute the module: {{{ import os import subprocess module = r'C:\full\path\to\some\module\ofMine.py' mpath, mfile = os.path.split(module) # Two different open solutions: subprocess.Popen("chdir %s & %s"%(mpath,mfile), shell=True) # But this one locks out the executing module from completing until the ui is closed: os.system("chdir %s & %s"%(mpath,mfile)) }}} I had first tried executing this from a shell: {{{ > python -m C:\full\path\to\some\module\ofMine.py }}} But I got this error: {{{ C:\Python26\python.exe: Import by filename is not supported. }}} Which led me to the above solution. Python 2.6 and //newer// ({{{os.popen}}} is deprecated). Use {{{subprocess}}}: http://docs.python.org/library/subprocess.html http://www.doughellmann.com/PyMOTW/subprocess/ {{{ import subprocess }}} ---- Note: If you want to execute a system command and don't care about the result, you can use {{{subprocess.call}}}. ---- New example, taken from [[this post|http://www.logilab.org/blogentry/20469]]: <<< "{{{subprocess.Popen}}} 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:" {{{ pipe = subprocess.Popen(['diff', '-u', appl_file, ref_file], stdout=subprocess.PIPE) output = pipe.stdout }}} "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)." <<< ---- A couple different ways: Version A, using communicate() http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate {{{ result = subprocess.Popen("system comands here", stdout=subprocess.PIPE).communicate() foo = [f.strip() for f in result[0].split("\r\n") if f != ""] for f in foo: print f }}} {{{communicate()}}} returns a 2-index tuple: Index[0] is the stdout stream, and index[1] is the stdin stream. We use a {{{list comprehension}}} to extract each of the lines as a list: Accessing index[0] of the tuple (which is a string) we split that by return & newline chars into a list. For each item in that list, if the item //isn't// an empty string, we strip off any other tabs and whatnot, and pass the result into our final list {{{foo}}}. ---- Version B, using .stdout http://docs.python.org/library/subprocess.html#subprocess.Popen.stdout {{{ result = subprocess.Popen("system comands here", stdout=subprocess.PIPE).stdout foo = [f.strip() for f in result.readlines() if f.strip() != ""] for f in foo: print f }}} This is similar to version A, but we're not using {{{communicate()}}}, and access the {{{stdout}}} stream directly. This returns a {{{file}}} object with the {{{stdout}}} data we care about, which we call {{{readlines()}}} method on, giving us a list, which we then clean up and return into the {{{foo}}} list via the {{{list comprehension}}}. ---- Sometimes, depending on what the system command is, you need to pass in the args {{{shell=True}}}: {{{ # result as a tuple, (stdout, stdin): result = subprocess.Popen("dir c:\\", stdout=subprocess.PIPE, shell=True).communicate() # result as a file object: result = subprocess.Popen("dir c:\\", stdout=subprocess.PIPE, shell=True).stdout }}} 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: {{{ # launch notepad: subprocess.Popen("start notepad", shell=True) }}} ---- I've also ran into instance where I'd get this kind of error reported back: {{{ WindowsError: (6, 'The handle is invalid') }}} If this is the case, I found the fix was to also pass in a pipe to the {{{stdin}}} {{{ result = subprocess.Popen("dir c:\\", stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True).communicate() }}} ---- //previous// to Python2.6: {{{ import os results = os.popen("system commands here").read().strip() # returns list # strip() is optional, but useful }}} http://docs.python.org/library/os.html#os.popen ---- Finally, you can use {{{os.system(command)}}}: {{{ import os ret = os.system('notepad') print ret # 0 }}} {{{subprocess}}} is far superior however: {{{os.system}}} will always return {{{0}}} on windows machines, and will hang your program during execution (which may or may not be wanted. [[exec|http://docs.python.org/ref/exec.html]] - statement [[execfile|http://docs.python.org/lib/built-in-funcs.html]] - built-in function (search down in docs) [[eval|http://docs.python.org/lib/built-in-funcs.html]] - built-in function (search down in docs) ---- Also see *[[Statement]] *[[Difference between exec and eval()]] http://docs.python.org/library/sys.html#sys.builtin_module_names {{{ import sys for m in sorted(sys.builtin_module_names): print m }}} prints: {{{ __builtin__ __main__ _ast _bisect _bytesio _codecs # etc... }}} Check out the {{{modulefinder}}} module: http://docs.python.org/library/modulefinder.html {{{ from modulefinder import ModuleFinder finder = ModuleFinder() finder.run_script('c:/path/to/my/module.py') modules = sorted([name for name,mod in finder.modules.iteritems()]) print modules }}} {{{ ['__main__', 'someOtherModuleA', 'anotherModuleB', 'etc'] }}} As of Python 2.6: http://docs.python.org/library/itertools.html#itertools.combinations {{{ import itertools items = 'ABCD' combinations = list(itertools.combinations(items, 2)) print combinations # [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')] }}} Presume you have some module ({{{stuff.py}}}) that has classes defined at the top level of the module like so: {{{ # stuff.py class Spam(object): pass class Eggs(object): pass }}} How can you query what those classes are in some other module? {{{ # findStuff.py import inspect import stuff for d in stuff.__dict__: if inspect.isclass(stuff.__dict__[d]): print stuff.__dict__[d].__name__, stuff.__dict__[d] }}} prints: {{{ Spam <class 'stuff.Spam'> Eggs <class 'stuff.Eggs'> }}} Everything in Python is an object: modules, functions, classes, instances of user defined classes, strings, etc. These objects can have [[attribute]]s which in turn can point to other objects. But some objects can be called to (if they support the {{{__call__}}} method) like functions, while others are treated like simple data members, and others could be modules themselves. The [[dir|http://docs.python.org/library/functions.html#dir]] function will give us a list of all attributes on an object. Then we just need to filter them via the [[callable|http://docs.python.org/library/functions.html#callable]] function. The below example exposes a quick way to break them into two camps: Callable, or not. From the below example, it lets you know you could do this: {{{ print sys.argv # this isn't callable, but we can print its data result = sys.callstats() # call to the function and capture the return }}} ---- {{{ # let's inspect this module! import types import sys call = [] data = [] module = [] # filter our results: for d in sorted(dir(sys)): if callable(eval('sys.%s'%d)): call.append(d) else: if type(eval('sys.%s'%d)) is types.ModuleType: module.append(d) else: data.append(d) if len(data): print "DATA:" for d in data: print "\t", d if len(call): print "CALLABLE:" for c in call: print "\t", c if len(module): print "MODULES:" for d in module: print "\t", d }}} prints (truncated): {{{ DATA: __doc__ __name__ __package__ __stderr__ __stdin__ __stdout__ api_version argv builtin_module_names byteorder ... CALLABLE: __displayhook__ __excepthook__ _clear_type_cache _current_frames _getframe call_tracing callstats displayhook ... }}} You could look at this fine blog pose here: http://codeboje.de/findingemptydirectories/ Reposted here for prosperity. @@Thanks Kerim!@@ Verison A: {{{ 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 }}} Better version: {{{ 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 }}} {{{ walksub('c:\\') }}} Search through every package in every path for .py modules. Search in each module for functions and classes. {{{ """ Create a dictionary. Each key is the path to a given module. The values are in fact sub-dics, who's keys are 'classes' and 'functions', who's values are lists of the found classes \ functions. """ import sys import os def getMFC(): """ Get our Module, Function, and Classes """ data = {} for p in sys.path: for dirpath, dirnames, filenames in os.walk(p): if '__init__.py' in filenames: for f in filenames: if f.endswith('.py'): key = os.path.join(dirpath, f) data[key] = {'functions':[], 'classes':[]} # Crack open the file to inspect... fh = open(key) for line in fh: if line.startswith('def'): store = line.strip()[4:-1] data[key]['functions'].append(store) if line.startswith('class'): store = line.strip()[6:-1] data[key]['classes'].append(store) return data }}} Example of some of the printed data: {{{ import pprint # just for printing our results pretty :) pprint.pprint(getMFC()) '(path truncated)/misc.py': {'classes': ['Set', 'Stack'], 'functions': ['flatten(tup)', 'mangle(name, klass)', 'set_filename(filename, tree)']}, }}} Armed with that data, it's not too hard now to ''search inside of it'' for a certain keyword \ string: {{{ def find(searchStr): """ Return a list of matching modules \ modules + functions & defs where searchStr is found it. Case insensitive. """ data = getMFC() results = [] searchStr = searchStr.lower() for d in data: if searchStr in d.lower(): results.append(d) for func in data[d]['functions']: if searchStr in func.lower(): results.append('%s - def %s'%(d, func)) for klass in data[d]['classes']: if searchStr in klass.lower(): results.append('%s - class %s'%(d, klass)) pprint.pprint(results) }}} {{{ searchStr = 'color' find(searchStr) }}} Example result (on a Mac): {{{ ['(path truncated...)/python2.6/site-packages/pygame/surfarray.py - def array_colorkey (surface)', '(path truncated...)/python2.6/site-packages/pygame/examples/stars.py - def draw_stars(surface, stars, color)', etc... }}} As of Python 2.6: http://docs.python.org/library/os.path.html#os.path.relpath {{{ import os rel = os.path.relpath(r"c:\temp\foo\stuff", r"c:\temp") print rel # foo\stuff }}} Presuming you want to find all the text before (and including) the first underscore '_': {{{ s = "prefix_stuff" print s[0:s.find("_")+1] # prefix_ }}} If you don't want to include the underscore, remove the {{{+1}}}. If there is no prefix, an empty string is returned. Here is another technique: {{{ s = "prefix_stuff" print s.split('_')[0] # prefix }}} This leaves off the 'split' character. Also, if there is no prefix to split, the whole string is returned. I finally found a use for the {{{reduce}}} function! (plus some {{{lambda}}} action) {{{ def average(numbers): return reduce(lambda x, y: x + y, numbers) / len(numbers) }}} {{{ nums = [1, 2, 3, 4, 5.5] print average(nums) # 3.1 }}} ---- Another way with {{{reduce}}} and the {{{operator.add}}} function: {{{ from operator import add def average(numbers): return reduce(add, numbers) / len(numbers) }}} {{{ numbers = [float(n) for n in range(1,10)] print average(numbers) # 5.0 }}} ---- Another way just using {{{sum}}}, and converting the passed in argument to a list: {{{ def average(*numbers): return sum(numbers)/len(numbers) }}} {{{ print average(1,2,3,4,5.5) # 3.1 }}} Copied directly from [[this recipe|http://code.activestate.com/recipes/578422/]] over at Activestate. By Oren Tirosh: <<< This is a quick one-liner to produce the filename and line number of the caller of the current function. {{{ '{0.f_code.co_filename}:{0.f_lineno}'.format(sys._getframe(1)) }}} This is useful for debugging and logging. It also nicely demonstrates the attribute access feature of the {{{.format()}}} method format string. If you wrap it in a function change the argument to {{{_getframe()}}} to {{{2}}}. Add {{{'{0.f_code.co_name}'}}} to the format string to get the name of function containing the call site. <<< My own warning: @@NOTE:@@ {{{sys._geframe()}}} is a {{{CPython}}} implementation detail: the leading underscore is telling you this is behind the scenes magic, and you shouldn't play with it, because it could change in future versions, or not be available in other Python implementations. Use at your own risk... Presuming you have a module with globals, and a function in the module... given the module itself, how can you find its global scope? All function objects have a variety of attributes that can be accessed. One of which is {{{func_globals}}}, which is from the docs: "A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined." {{{ # someModule.py G = "I'm a global!" def spam(): pass def main(): funcGlobals = spam.func_globals for k in funcGlobals: print k, funcGlobals[k] }}} Docs [[here|http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy]] under the 'Callable types' section. 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 "no, you can't do this". Well, based on the below blog post, it looks like you can ;) The one danger is that it uses {{{sys._getframe()}}}, which as you can tell by the leading underscore is considered private, and could change with future versions of Python, or different implementations. http://docs.python.org/library/gc.html http://pythonic.pocoo.org/2009/5/30/finding-objects-names {{{ 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'] }}} I also found this blog post that does something very similar: http://posted-stuff.blogspot.com/2010/01/locating-instances-of-python-classes.html ---- Good overview of the {{{gc}}} module: http://www.doughellmann.com/PyMOTW/gc/ Variable names point to objects. A single object can be pointed to by multiple variables. How can you query how many pointers a given object has? {{{ import sys # define an list object [1,2,3], and assign it to variable foo foo = [1,2,3] assign list object [1,2,3] to variable goo goo = foo # how many pointers does list object [1,2,3] have to it? print sys.getrefcount(foo) # 3 }}} I'm guessing the '3' means that the {{{sys.getrefcount}}} also has a pointer to it too. How many pointers does the object '1' have to it? {{{ print sys.getrefcount(1) # 577 }}} 577 pointers on my machine at least ;) {{{ def precentageBetween(low, val, high): if low > high: # swap low for high if reversed: low, high = high, low return float(val - low) / float(high - low) low = -10 high = 20 val = 5 print precentageBetween(low, val, high) # 0.5 }}} ---- Also see: *[[How do I lerp?]] Found a lot of solutions online, this was the shortest one: {{{ import locale locale.setlocale(locale.LC_ALL, "") print locale.format('%d', 12345, True) # 12,345 }}} http://docs.python.org/library/locale.html#locale.format The [[range|http://docs.python.org/library/functions.html#range]] function returns a list of integers. But what if you want to generate a range of floats evenly spaced apart by some value? [[itertools.count|http://docs.python.org/library/itertools.html#itertools.count]] can help with this, but only if you're in Python 2.7 or newer. What about older? Using part of their example, this is what I came up with: {{{ def floatrange(end, start=0, step=1.0): def count(cstart, cstep): n = cstart while True: yield n n += cstep result = [] counter = count(start, step) val = start while (val+step) <= end: val = float(counter.next()) result.append(val) return result }}} {{{ foo = floatrange(5, start=1.5, step=.25) print foo [1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75, 5.0] }}} It will return a list including every float value in-between and including start and end, based on the step size. The [[uuid|http://docs.python.org/2/library/uuid.html]] module ([[Universally Unique Identifier|http://en.wikipedia.org/wiki/UUID]]) seems to work pretty well. From their [[examples|http://docs.python.org/2/library/uuid.html#example]] section, generate a random uid: {{{ import uuid uid = uuid.uuid4() # 146ad84d-d17b-4502-8958-7bd367662cca }}} {{{os.environ}}} returns a [[Dictionary|DICTIONARY]] {{{ import os for key in os.environ: print key + " : " + os.environ[key] }}} Also see: *[[How can I query the value of an Environment Variable?]] *[[How can I set an environment variable?]] {{{dir}}} queries all the [[attributes|attribute]] (both variables, and functions) in the namespace being requested. {{{ for thingy in dir(open): print thingy __call__ __class__ __cmp__ __delattr__ __doc__ __getattribute__ __hash__ __init__ __module__ __name__ __new__ __reduce__ __reduce_ex__ __repr__ __self__ __setattr__ __str__ }}} Also see, for a comparison: *[[How can I get a list of an object's attributes and methods?]] Thanks to my bud Demetrius Leal on the tip with {{{netstat}}}: On a Windows system, you can use {{{subprocess.Popen}}} to run the system {{{netstat}}} command. It returns a file-object which you can then parse: {{{ import subprocess pipe = subprocess.Popen(['netstat', '-ab'], stdout=subprocess.PIPE) for line in pipe.stdout: print line.strip() }}} {{{-a}}} : Displays all connections and listening ports. {{{-b}}} : Displays the executable involved in creating each connection or listening port. prints stuff like: {{{ Active Connections Proto Local Address Foreign Address State TCP 0.0.0.0:235 username:0 LISTENING RpcSs [svchost.exe] TCP 0.0.0.0:545 username:0 LISTENING Can not obtain ownership information TCP 0.0.0.0:4071 username:0 LISTENING [javaw.exe] ... }}} Maybe there's a more built-in \ non-os specific way? On ''Windows'', you can easily acces this via the {{{tasklist}}} system command: {{{ import subprocess output = subprocess.Popen(['tasklist'], stdout=subprocess.PIPE).stdout for line in output: print line.strip() }}} {{{ Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ System Idle Process 0 Services 0 24 K System 4 Services 0 38,204 K smss.exe 312 Services 0 1,356 K csrss.exe 412 Services 0 6,076 K etc... }}} You can also filter the result by passing args to the {{{tasklist}}} command: {{{ import subprocess output = subprocess.Popen(['tasklist', '/FI', 'IMAGENAME eq svcHost.exe'], stdout=subprocess.PIPE).stdout for line in output: print line.strip() }}} {{{ Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ svchost.exe 692 Services 0 12,284 K svchost.exe 840 Services 0 13,968 K svchost.exe 940 Services 0 20,796 K etc... }}} Here's a more robust solution that turns all this data into a list of Python dicts, with each dict representing a specific process: {{{ import subprocess def getTaskData(searchStr=None): """ Windows only: Uses the tasklist dos cmd. Based on all running processes, returns a list of dicts: Each dict has these keys: "Image Name", "PID", "Session Name", "Session#", "Mem Usage", "Status", "User Name", "CPU Time", "Window Title". Optionally the user can pass in a search string, which will be used as a filter on the "Image Name" (executable name). """ output = None if searchStr: output = subprocess.Popen(['tasklist', '/FO', 'LIST', '/V', '/FI', 'IMAGENAME eq python*'], stdout=subprocess.PIPE).stdout else: output = subprocess.Popen(['tasklist', '/FO', 'LIST'], stdout=subprocess.PIPE).stdout data = [] appender = {} for line in output: stripped = line.strip() if len(stripped) == 0 and len(appender): data.append(appender) appender = {} continue kv = [item.strip() for item in line.split(": ")] if kv > 2: # Some of our 'values' have the ": " string in them, so we need to # rebuild that data after being split: k = kv.pop(0) v = ': '.join(kv) kv = [k,v] if kv[1] != '': appender[kv[0]] = kv[1] if not len(data): data.append(appender) return data }}} ---- If you're looking for a way more directly authored in Python, check out this blog post: http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/  {{{dir}}} queries all the attributes (both variables, and functions) in the namespace being requested. make a file object f {{{ f = open("c:/temp/foo.txt") }}} Now, query the *objects* [[methods|Method]] and [[attributes|attribute]]: {{{ for thingy in dir(f): print thingy __class__ __delattr__ __doc__ __enter__ __exit__ __getattribute__ __hash__ __init__ __iter__ __new__ __reduce__ __reduce_ex__ __repr__ __setattr__ __str__ close closed encoding fileno flush isatty mode name newlines next read readinto readline readlines seek softspace tell truncate write writelines xreadlines }}} Also see, for a comparison: *[[How can I get a list of a function's attributes and methods?]] {{{ import glob glob.glob("c:\\temp\\*.txt") }}} Also see: *[[How can I get a list of files from a directory?]] {{{ import os files = os.listdir(myDir) }}} However, this will list subdirs as well ---- {{{ import os files = os.walk(pywin32packageDir).next()[2] }}} This will list only files. ---- Also see: *[[How can I get a list of files from a directory, based on a wildcard?]] Python comes equipped with two functions: {{{locals()}}} and {{{globals()}}}. They are //dictionaries//. To get a printable list: {{{ for key in globals(): print key + " : " + str(globals()[key]) }}} Remember, locals are the current function scope that you're in, and globals is the scope of the module. {{{LEGB}}} -> ''L''ocal (function) -> ''E''nclosing (function), ''G''lobal (module), '''B''uiltin'. {{{ import os walker = os.walk(r"c:\temp") subdirList = walker.next()[1] print subdirList # ['subDirA', 'subDirB'] }}} {{{os.walk}}} returns a generator object. Each time you run it, it returns another level of subdirs etc. So the first time you run it using it's {{{.next}}} method, you capture the 2nd index (1) in the list, which his the list of subdirs. The first index (0) is the current dir, and the 3rd index (2) is the list of files in the current dir. In the below example, we get a string representing the previous version of a given depot file from Perforce. An interesting thing to note: {{{p4.run('print')}}} executes //much faster// than {{{p4.run_print()}}}. {{{ from P4 import P4 p4 = P4() p4.connect() depotFile = "//myDepot/path/to/my/file.txt" depotInfo = p4.run("fstat", depotFile) headRev = int(depotInfo[0]["headRev"]) prevRev = headRev-1 oldRevData = p4.run('print', "-q", ['%s#%s'%(depotFile,prevRev)]) oldRevStr = oldRevData[1] }}} {{{ import os startPaths = ['c:/temp', 'c/foo'] for sp in startPaths: print os.path.normpath(sp) for dirpath, dirnames, filenames in os.walk(sp): for dir in dirnames: name = os.path.join(dirpath, dir) print os.path.normpath(name) }}} Also see: *[[How can I recursively search directories and files, for something in a file?]] *[[How can I search for files in directories faster than os.walk?]] {{{ import socket help(socket) }}} Given a number like {{{2.32525}}}, how can I find just the {{{.32525}}} part? {{{ import re num = 2.32525 print num - int(num) print float('0.%s'%str(num).split('.')[-1]) print float('0.%s'%re.findall('[0-9]+$', str(num))[0])
}}}
{{{
0.32525
0.32525
0.32525
}}}
But none of these seem very elegant... there //must be a better way!//
Modify the Python [[environment variable|What are Python-specific Environment Variables?]] {{{PYTHONSTARTUP}}} to the name of a readable file: the Python commands in that file are executed before the first prompt is displayed in interactive mode.

Also see : [[Using the sitecustomize & usercustomize modules]]
In Windows:
{{{
from win32com.client import Dispatch

# Open Photoshop
photoshop = Dispatch("Photoshop.Application")
#  Open a file
document = photoshop.Open("c:/temp/tempfile.bmp")
#  Rotate it:
document.Rotate(45)
}}}
You'll need to have the Win32 Extensions package installed to access {{{win32com}}}:
*http://sourceforge.net/projects/pywin32/
A good blog post:
*http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html
A list of scripts to give you an idea of how to author your own:
*http://morris-photographics.com/photoshop/scripts/
You can find official documentation here:
I recommend the "~VisualBasic" reference for the applicable version of Photoshop you're running:  The syntax of the API more closely matches the Python version.
----
There are many ''classes'' in the API that don't inherit from the {{{Application}}} object.  For example, when doing a {{{Document.SaveAs()}}}, you need to pass in an object that represents the file-type you want to save.  If you wanted to save a a targa (tga) file, you'd need an instance of a {{{TargaSaveOptions}}} object.  How can you get this type of object ?  Just like so:
{{{
# Say you want to save as a targa:
tgaSaveOptions = win32com.client.Dispatch("Photoshop.TargaSaveOptions")
document.SaveAs(pathToFile, tgaSaveOptions)
}}}
''Constants'' are another story:  Authoring code via the native {{{JavaScript}}} \ {{{VisualBasic}}} \ {{{AppleScript}}} interfaces exposes theses constants automatically.  I have yet to get access to them via Python.  Via [[this page|http://docs.activestate.com/activepython/2.4/pywin32/html/com/win32com/HTML/QuickStartClientCom.html#UsingComConstants]] (and others), they describe this method of gaining access to constants:
<<<
Makepy automatically installs all generated constants from a type library in an object called {{{win32com.clients.constants}}}. You do not need to do anything special to make these constants work, other than create the object itself (ie, in the example above, the constants relating to Word would automatically be available after the {{{w=win32com.client.Dispatch("Word.Application")}}} statement.
For example, immediately after executing the code above, you could execute the following:
{{{
>>> import win32com.client
>>> w=win32com.client.Dispatch("Word.Application")
>>> w.WindowState = win32com.client.constants.wdWindowStateMinimize
}}}
and Word will Minimize.
<<<
However, this doesn't work for me using their example, or applying it to Photoshop:  All I get are {{{AttributeErrors}}} since the {{{constants}}} type library appears to be empty.  By running this code:
{{{
for d in dir(win32com.client.constants):
print d
}}}
All it prints is a few built-in special methods:  No application-related constants at all.... :-S
The fix I've found is simply to find the enumerated value in the {{{VisualBasic}}} constant docs, and plug that in directly.  It seems a big clunky, but does work.
----

''Some general notes as I use the API more:''

Object hierarchy:
*{{{Application}}} : The open Photoshop application.
**{{{Document}}} : An open File in Photoshop, part of the {{{Documents}}} collection object for the given parent.
***{{{ArtLayer}}} : one or more 'layers', part of the {{{ArtLayers}}} & {{{Layers}}} collections for its parent {{{Document}}}.
***{{{LayerSet}}} : one or more 'groups', part of the {{{LayerSets}}} & {{{Layers}}} collection object for the given parent.  Can contain:
****{{{ArtLayer}}} : one or more 'layers', part of the {{{ArtLayers}}} & {{{Layers}}} collection for its parent {{{LayerSet}}}.
****{{{LayerSet}}} : This would be considered a sub-group, part of the {{{LayerSets}}} & {{{Layers}}} collection for it's parent {{{LayerSet}}}.  This recursion can go forever with more child {{{LayerSet}}} and {{{ArtLayer}}} objects.
*****{{{...}}}
Notes on {{{Layers}}}, {{{LayerSets}}}, {{{ArtLayers}}}, & {{{LayerSet}}} and {{{ArtLayer}}} objects:
*{{{Layers}}}, {{{LayerSets}}}, and {{{ArtLayers}}} are container objects:  They tell you, for the given parent object, what children they have of the given type.  However, these collections aren't part of the hierarchy:  They just help organize, and iterate over data.
*{{{Layers}}} is a collection of both {{{ArtLayer}}} and {{{LayerSet}}} objects, for the given parent object, which could be a parental {{{Document}}} (psd), or {{{LayerSet}}} (group).  This is just a collection of data, it's not part of the tree.
*{{{LayerSets}}} : A collection of {{{LayerSet}}} objects (groups in Photoshop) for the given parent object, which could be a {{{Document}}} (psd), or it could be another {{{LayerSet}}} (since groups can live in groups, etc).  This is just a collection of data, it's not part of the tree.
*{{{ArtLayers}}} : A collection of {{{ArtLayer}}} objects (layers in Photoshop) for the given parent object, which could be a {{{Document}}} (psd), or {{{LayerSet}}} (group in Photoshop).  This is just a collection of data, it's not part of the tree.
*{{{LayerSet}}} : A 'group' in Photoshop.  It can contain children {{{Layers}}}, {{{ArtLayers}}}, and {{{LayerSets}}} objects.  It's parent is either a {{{Document}}} (if it is a root group) or another {{{LayerSet}}} object (if it is a subgroup).
*{{{ArtLayer}}} : A 'layer' in Photoshop.  It has no children.  It's parent in the docs is listed as only a {{{Document}}}, but since {{{ArtLayer}}} objects can live in {{{LayerSet}}} (group) objects, it seems like a {{{LayerSet}}} could be a parent as well.
So based on that info, this shows the child data for each object type:
*{{{Application}}}
**{{{Document}}} : One or more {{{Document}}} objects, referenced via the {{{Application.Documents}}} collection.
*{{{Document}}}:
**{{{LayerSet}}} : One or more {{{LayerSet}}} objects (groups in Photoshop), referenced via the {{{Document.LayerSets}}} collection, and part of the {{{Document.Layers}}} collection.
**{{{ArtLayer}}} : One or more {{{ArtLayer}}} objects (layer in Photoshop), referenced via the {{{Document.ArtLayers}}} collection, and part of the {{{Document.Layers}}} collection.
*{{{LayerSet}}}:
**{{{LayerSet}}} : One or more child {{{LayerSet}}} objects (sub-groups in Photoshop), referenced via the {{{LayerSet.LayerSets}}} collection, and part of the {{{LayerSet.Layers}}} collection.
**{{{ArtLayer}}} : One or more {{{ArtLayer}}} objects (layer in Photoshop), referenced via the {{{LayerSet.ArtLayers}}} collection, and part of the {{{LayerSet.Layers}}} collection.
*{{{ArtLayer}}} :
**Has no children, is a 'layer' in Photoshop.  Referenced via the parent objects {{{ArtLayers}}} collection, and part of the parent objects {{{Layers}}} collection.
----
Useful methods and properties (attributes) for the API objects, based on the ~CS3 ~VisualBasic docs:
*{{{Application}}}
**Created via: {{{application = Dispatch("Photoshop.Application") }}}
**Properties:
***{{{ActiveDocument}}} : The active (top most in Photoshop) {{{Document}}} object.  Read & write.
***{{{Documents}}} : Reference to its {{{Documents}}} object, which is an iterable collection of {{{Document}}} objects.  Read-only.
***{{{Path}}} : String to the full path of the Photoshop executable.  Read only.
***{{{Version}}} : String of the Photoshop version.  Read only.
**Methods:
***{{{ExecuteAction}}} : Play an {{{ActionManager}}} event.
***{{{Open}}} : For the passed in string, open that image as a {{{Document}}}, and return the {{{Document}}}.
***{{{Quit}}}
***{{{Refresh}}} : Pause the script and refresh the application.
*{{{Documents}}} - iterable collection object
**Properties:
***{{{Application}}}, {{{Parent}}} : Reference to the {{{Application}}} object it belongs to.
***{{{Count}}} : Long : Number of {{{Document}}} objects in this {{{Documents}}} collection.
**Methods:
***{{{Add}}} : Create (and return) a new\empty {{{Document}}} object.
*{{{Document}}} - Single object
**New created via {{{Documents.Add()}}}.  Opened via {{{Application.Open()}}}.
**Properties:
***{{{Application}}}, {{{Parent}}} : The {{{Application}}} object it belongs to.  Read-only.
***{{{BackgroundLayer}}} : Reference to the background {{{ArtLayer}}}.  Read-only.
***{{{FullName}}} : String, full path on disk to this document.  Read-only.
***{{{Name}}} : String, just the leaf document name (name.ext), read-only.
***{{{Path}}} : String, just the path (no leaf file name) to the document, read-only.
***{{{Height}}} : Double, height of the document, read-only.
***{{{Width}}} : Double, width of the document, read-only.
***{{{Layers}}} Reference to this objects {{{Layers}}} object, a collection of both {{{ArtLayer}}} and {{{LayerSet}}} objects.   Could be imagined as a combination of the {{{ArtLayers}}} and {{{LayerSets}}} objects.
***{{{ArtLayers}}} : Reference to its {{{ArtLayers}}} object, which is an iterable collection of {{{ArtLayer}}} objects ('layers' in Photoshop).
***{{{LayerSets}}} : Reference to its {{{LayerSets}}} object, which is an iterable collection of {{{LayerSet}}} objects ('groups' in Photoshop).
***{{{Resolution}}} : Double, the documents resolution in pixels per inch.
***{{{Saved}}} : Bool, is it saved since the last change?  Read-only.
**Methods:
***{{{Close}}} Close the document.  It should be noted that unless this is the 'active document', Phothoshop will launch a confirm dialog, even if the {{{PsSaveOptions}}} arg {{{2}}} (close without dialog) is passed in.
***{{{Save}}} : Save the document (presuming it has already been saved before).
***{{{SaveAs}}} : Save a new document, or save an old one with a new path.
*{{{Layers}}} - iterable collection object (of Photoshop 'layers' and 'groups')
**Properties:
***{{{Application}}} : Reference to the {{{Application}}} object this belongs to.  Read-only.
***{{{Count}}} : Long, number of {{{LayerSet}}} and {{{ArtLayer}}} objects in this collection. Read-only.
***{{{Parent}}} : Reference to the {{{Document}}} (or {{{LayerSet}}}) object this belongs to.
**Methods:
***{{{RemoveAll}}} : Removes all layers from this collection.
*{{{LayerSets}}} - iterable collection object (of Photoshop 'groups')
**Properties:
***{{{Application}}} : Reference to the {{{Application}}} object this belongs to.  Read-only.
***{{{Count}}} : Long, number of {{{LayerSet}}} objects in this collection. Read-only.
***{{{Parent}}} : Reference to the {{{Document}}} (or {{{LayerSet}}}) object this belongs to.
**Methods:
***{{{Add}}} : Create (and return) a new {{{LayerSet}}} object
***{{{RemoveAll}}} : Removes all layers from this collection.
*{{{ArtLayers}}} - iterable collection object (of Photoshop 'layers')
**Properties:
***{{{Application}}} : Reference to the {{{Application}}} object this belongs to.  Read-only.
***{{{Count}}} : Long, number of {{{ArtLayer}}} objects in this collection. Read-only.
***{{{Parent}}} : Reference to the {{{Document}}} object this belongs to.
**Methods:
***{{{Add}}} : Create (and return) a new {{{ArtLayer}}} object
***{{{RemoveAll}}} : Removes all layers from this collection.
*{{{LayerSet}}} - single object representation of a 'group' in Photoshop.
**Properties:
***{{{Application}}} : Reference to the {{{Application}}} object this belongs to.  Read-only.
***{{{ArtLayers}}} : Reference to the {{{ArtLayers}}} container in this {{{LayerSet}}}.  Read-only.
***{{{Bounds}}} : [minX, minY, maxX, maxY] : Bounding rectangle of the {{{LayerSet}}}.  Read-only.
***{{{Layers}}} : Reference to the {{{Layers}}} container in this {{{LayerSet}}}.   Read-only.
***{{{LayerSets}}} : Reference to the top layer {{{LayerSets}}} object in this document.
***{{{Name}}} : String name of this {{{LayerSet}}}, read-write.
***{{{Parent}}} : Either the parent {{{Document}}}, or {{{LayerSet}}} for this {{{LayerSet}}}.  Read-only.
**Methods:
***{{{Delete}}} : Delete this {{{LayerSet}}}.
***{{{Duplicate}}} : Create a duplicate of this {{{LayerSet}}} (which it returns).  Pass it a {{{ArtLayer}}} or {{{LayerSet}}} as the new parent.
***{{{Merge}}} : Merge the layerset, return a reference to the {{{ArtLayer}}} created by this method.
***{{{Move}}} : Move the {{{LayerSet}}} object to a new {{{Application}}}.  Not sure how this works yet...
***{{{Resize}}} : Resize all layers in this {{{LayerSet}}}.  Values passed in are percentages, range is 0->100.
***{{{Rotate}}} : Rotate clockwise by the given angle.
***{{{Translate}}} :  Move this number of pixels.  {{{Document}}} resolution must be set to 72 ppi first.
*{{{ArtLayer}}} - single object representation of a 'layer' in Photoshop.
**Properties:
***{{{Application}}} : Reference to the {{{Application}}} object this belongs to.  Read-only.
***{{{Bounds}}} : [minX, minY, maxX, maxY] : Bounding rectangle of the {{{LayerSet}}}.  Read-only.
***{{{Name}}} : String name of this {{{LayerSet}}}, read-write.
***{{{Parent}}} : The parent {{{Document}}}, read-only.
**Methods:
***{{{Clear}}} : Cut the layer without moving it to the clipboard.
***{{{Copy}}} : Copy the layer to the clipboard.
***{{{Cut}}} : Cut the layer to the clipboard.
***{{{Delete}}} : Delete this {{{LayerSet}}}.
***{{{Duplicate}}} : Create a duplicate of this {{{LayerSet}}}.  Pass it a {{{ArtLayer}}} or {{{LayerSet}}} as the new parent.  Returns {{{ArtLayer}}}.
***{{{Merge}}} : Merge the layer down, return a reference to the {{{ArtLayer}}} this is merged into.
***{{{Move}}} : Move the {{{ArtLayer}}} object to a new {{{ArtLayer}}} or {{{LayerSet}}}.
***{{{Resize}}} : Resize all layers in this {{{LayerSet}}}.  Values passed in are percentages, range is 0->100.
***{{{Rotate}}} : Rotate clockwise by the given angle.
***{{{Translate}}} :  Move this number of pixels.  {{{Document}}} resolution must be set to 72 ppi first.


The docs call this 'emulating numeric types':
http://docs.python.org/ref/numeric-types.html
See the docs for ALL of the various special methods you can define.  This is Python's approach to 'operator overloading'.

These special methods' can be added to your class, that let you specify what happens when you add (for example) one object to another:
{{{
class MyObj(object):
num =0

def __init__(self, num):
self.num = num

num = self.num + obj.num
return num

foo = MyObj(2)
goo = MyObj(3)
print foo + goo
# 5
}}}
''Other useful methods include:''
{{{__getitem__}}} is called when getting value from an object:
{{{
L = [1,2,3]
num = L[0]
# this is equal to:
num = L.__getitem__(0)
}}}
{{{__setitem__}}} is called when setting some value of an object:
{{{
L = [1,2,3]
L[0] = 10
# this is equal to:
L.__setitem__(0, 10)
}}}
So as you can see, in your own objects, you can modify the behavior of {{{__getitem__}}} and {{{__setitem__}}} to be whatever you need.
Found this code (only works on Windows):
http://code.activestate.com/recipes/578300-python-subprocess-hide-console-on-windows/
{{{
import sys
import subprocess

IS_WIN32 = 'win32' in str(sys.platform).lower()

def subprocess_call(*args, **kwargs):
#also works for Popen. It creates a new *hidden* window, so it will work in frozen apps (.exe).
if IS_WIN32:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
kwargs['startupinfo'] = startupinfo
retcode = subprocess.call(*args, **kwargs)
return retcode
}}}
http://docs.python.org/library/subprocess.html
This is a function I had made for my [[BubblePaint|http://www.akeric.com/blog/?p=601]] program in [[PyGame|http://www.pygame.org]].  In a nutshell, it will save the screen to a {{{png}}} image, and increment the filename with each image in the dir.  This is a modification of a post over on my [[PyGame Wiki|http://pygamewiki.tiddlyspot.com/]].
{{{
import glob, os, re

def saveImage():
"""
Save the current image to the working directory of the program.
"""
currentImages = glob.glob(".\\*.png")
numList = [0]
for img in currentImages:
i = os.path.splitext(img)[0]
try:
num = re.findall('[0-9]+$', i)[0] numList.append(int(num)) except IndexError: pass numList = sorted(numList) newNum = numList[-1]+1 saveName = 'bubblePaint.%04d.png' % newNum print "Saving %s" % saveName pygame.image.save(screen, saveName) }}} Will save out files like this: {{{ bubblePaint.0001.png bubblePaint.0002.png etc... }}} I thought it'd be cool to have some sort of "pickle inspector" window, where you could see the contents of saved pickle data. The below module is the result: You can drag&drop the pickle file onto this module's icon, and it'll print out the pickled data. That being said, it's pretty easy to break: If the pickled data requires any imports from modules that aren't part of your default Python path, it will fail. But I thought it was a good place to start. {{{ # pickleInspector.py import sys import pickle from pprint import pprint def main(args): """ Print the values from the pickled data. """ if len(args) != 2: return with open(args[1]) as gerken: loadedData = pickle.load(gerken) pprint(loadedData) if __name__ == "__main__": try: main(sys.argv) except Exception, e: print "EXCEPTION", e finally: raw_input("Press Enter to exit...") }}} {{{ # code... raw_input("Press the enter key to exit.") }}} It's easy to make a {{{Tkinter}}} window, but I was often stumped on how to close them, if one is already open. Meaning: A window is open, the user re-clicks on the icon to create it: now there are two windows. Instead, I want the first one deleted, before the second one is created. The below code will delete any application if it can match the given 'title' string in the data that the dos {{{tasklist}}} returns. Windows only, obviously. The idea is, before the window is created, this code is first ran to see if there is a pre-existing one, and if so, it kills it. {{{ import subprocess def killWindow(title): """ Windows only: Uses the tasklist & taskkill dos cmds. Kill the window with the given title. """ output = subprocess.Popen(['tasklist', '/FO', 'TABLE', '/V', '/FI', 'IMAGENAME eq python*'], stdout=subprocess.PIPE).stdout for line in output: strip = line.strip() if title in strip: # PID is the 2nd item in the list: pid = strip.split()[1] subprocess.Popen(['taskkill', '/PID', pid]) }}} {{{ title = "My Tkinter Window" killWindow(title) }}} {{{ # Windows only: import os os.startfile("notepad") }}} On Unix of Mac you'll need to use {{{subprocess}}} or {{{os.system}}} ---- Also see: [[How can I execute a system command, and capture the return?]] {{{ # setup some lists for the below example: words = ["one","two","three","four"] numbers = [1,2,3,4] foo = ["snarg", "nerf", "toos", "flurb"] }}} ''If you have //more// than two lists'', you can use this '{{{for i in range}}}' example, or {{{enumerate}}} (they both print the same results): {{{ for i in range(len(words)): print words[i], numbers[i], foo[i] for (offset,item) in enumerate(words): print item, numbers[offset], foo[offset] # one 1 snarg # two 2 nerf # three 3 toos # four 4 flurb }}} These both require that all the lists //are the same length//. If they are not, you'll get an {{{IndexError}}}. ---- --''If you have exactly two lists''--: Update: I used to think that {{{map}}} and {{{zip}}} only worked on //two// lists, but in fact, they //do// work on multiple lists: You can use the {{{map()}}} function, passing in {{{None}}} as its first argument (the first arg is a function to apply to each item in the list, so in this case we're not using that functionality). Or you can use the {{{zip()}}} function. These are much prettier than the previous example, --but they also are limited by only working on two lists.-- {{{ # setup some lists for all the below examples: words = ["one","two","three"] # note the missing 'four' numbers = [1,2,3,4] foo = ["snarg", "nerf", "toos", "flurb"] }}} {{{map()}}}: {{{ # Notice that all the map functions insert 'None' into the lists where # there would be a missing list element: # map version 1: for w, n, f in map(None, words, numbers, foo): print w, n, f # one 1 snarg # two 2 nerf # three 3 toos # None 4 flurb # map version 2: for thing in map(None, words, numbers, foo): print thing # ('one', 1, 'snarg') # ('two', 2, 'nerf') # ('three', 3, 'toos') # (None, 4, 'flurb') # map vresion 3: (similar to version 1 in result) for thing in map(None, words, numbers, foo): print thing[0], thing[1], thing[2] # one 1 snarg # two 2 nerf # three 3 toos # None 4 flurb }}} {{{zip()}}}: {{{ # zip version 1: for w, n, f in zip(words, numbers, foo): print w, n, f # one 1 snarg # two 2 nerf # three 3 toos # Other zip examples behave the same as map, but simply # truncate the results to the shortest list. No point in doing # all the retyping here... }}} The main differences: *{{{map()}}} will work on all indicies, even if one list is shorter than another (but, that could cause problems obviously). As you can see, it introduces {{{None}}} into the missing index. *{{{zip()}}} works up until the length of the shortest list. Both of them can handle the differences in list lengths //without// raising an exception, compared to the '{{{for i in range}}}' example which will. ---- Also see: *The Python docs on looping techniques: http://docs.python.org/tut/node7.html#SECTION007600000000000000000 Python comes included with it's own {{{tkMessageBox}}} modal dialog, which you can embed in your apps: {{{ import tkMessageBox # somewhere inside your function... result = tkMessageBox.askquestion("Message Title!", "Some Question!?!?", icon='warning') if result == 'yes': doYesFunc() else: doNoFunc() }}} http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm It has many different options\dialog types, the above link gives a nice overview.  If you're wondering: http://www.perforce.com/ !!!Via the Perforce [[API|Perforce access via Python]]: [[P4 API Docs|http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373]] This will both create a changelist, and optionally add files to it. The file add system is terribly limited: It will only work on files already existing in P4, that aren't already open for edit. You'd need to write a more robust set of conditions to work with files that are to be added, files that live in another changelist, files that should be deleted, or files that someone else has checked out. But it gives you an idea for where to start... Thanks to an email from Ross Kameny got me this code snippet: {{{ import P4 def makeChangelist(description, files=[]): """ description : string : The changelist description files : list : The files to edit. Default is an empty list. """ p4 = P4.P4() p4.connect() if files: p4.run_edit(files) changeList = p4.fetch_change() changeList["Description"] = description if files: changeList["Files"] = files results = p4.save_change(changeList) changelistNum = int(results[0].split(' ')[1]) p4.disconnect() return changelistNum }}} !!!Via calls to the system: This just makes an empty changelist, doesn't add any files to it. First, you need to generate a text file with the change info in it, and save it. Like {{{c:/temp/myChangelist.txt}}}: {{{ Change: new Description: Enter description here, one line }}} Then in Python, call to P4 to create the changelist, then find the changelist number (to be used to add things to the list): {{{ import os p4changeReturn = os.popen("P4 change -i < c:/temp/myChangelist.txt").read().strip() buffer = p4changeReturn.split(" ") changelistNumber = buffer[1] }}} FYI: In Python 2.6 and newer, {{{popen}}} is depricated. Check notes on {{{subprocess}}} [[here|How can I execute a system command, and capture the return?]] On Windows, it's pretty easy to make some nice beeping sounds with {{{winsound.Beep}}}: http://docs.python.org/library/winsound.html {{{ import winsound for i in range(50, 5000, 10): winsound.Beep(i, 25) }}} ---- Blog post dealing with [[PyAudio|http://people.csail.mit.edu/hubert/pyaudio/]]: http://davywybiral.blogspot.com/2010/09/procedural-music-with-pyaudio-and-numpy.html ---- Blog post about other Python implementations: http://diagrammes-modernes.blogspot.com/2007/08/music-control-tools-python-based.html ---- [[Pyknon|https://github.com/kroger/pyknon]] "Pyknon is a simple music library for Python hackers. With Pyknon you can generate Midi files quickly and reason about musical proprieties." ---- There's also the builtin [[wave|http://docs.python.org/2/library/wave.html]] library that can read and write wav data, but I'm not sure if it can actually play it. ''pyexiv2'' http://tilloy.net/dev/pyexiv2/index.htm ---- Also see: *http://www.exif.org/ Languages like Processing have [[functions|http://www.processing.org/reference/map_.html]] that will take a given value from one range and map it into another. I can't seem to find this in Python, but here's the code: {{{ def valueRangeMap(val, origMin, origMax, newMin, newMax): """ Will remap val from the original range into the new range. val : The value you are mapping. origMin : The original minimum value that val should be greater than. origMax : The original maximum value that val should be less than. newMin : The new minimum value that val will be mapped into. newMax : the new maximum value that val will be mapped into. return : float : The newly mapped value. """ # Find the percentage val is between origMin and origMax: percetVal = float(val - origMin) / float(origMax - origMin) # Map into the new range: mappedVal = (newMin+newMax)*percetVal return mappedVal print valueRangeMap(5, 0, 10, 10, 20) # 15.0 }}}  {{{ import re search = "/my/dir/is/mixed" line = "lineData -- C:/my/Dir/is/MIXED/case --moreData" print re.findall(search, line, re.IGNORECASE) # ['/my/Dir/is/MIXED'] }}} I was designing a UI for a [[PyGame|http://www.pygame.org]] program I am working on, and needed a way to pass a value defining a "row height" into the functions that rendered my text to the screen. Since the UI could change, I didn't want to have to hard-code positions into each element, later to modify it and have to redo all the positions. What I came up with was a simple class with a single usable attribute, called {{{val}}}. Through using [[properties|property]], I'm able to control how the attr behavies at time time its value is queried: {{{ class Row(object): # class to store the current row location in the UI # Each time it is called, it will increment its value def __init__(self, val): self._val = 0 self.orig = val @property def val(self): self._val = self._val + self.orig return self._val row = Row(16) print row.val, row.val, row.val, row.val # 16 32 48 64 }}} Properties have getter, setter, and deleter methods, but the default is getter, which I used above. So as you can see, each time I call to print, it accesses the val property (via the getter), and updates the internal counter. {{{ overlay.blit(bubble, (8, row.val)) overlay.blit(toggleText, (8, row.val)) overlay.blit(lmb, (8, row.val)) }}} Rather than having to specify a {{{Y}}} value for the last arg of the tuple, I can simply pass in my object, and it passes out the current new position, based on how many times it was called before. I have no doubt there is probably some slicker way in Python, but it's what I came up with on the spot :) {{{_winreg}}} - Windows only (obviously) *http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/ See Python [[docs|http://docs.python.org/library/_winreg.html]] Couple different solutions: {{{ import os import subprocess # Paths need to be backslashes. TEMP is # all backslashes, but it's worth calling out. path = os.getenv('TEMP').replace('/','\\') os.system('explorer %s'%path) subprocess.Popen(['explorer', path]) }}} {{{ import webbrowser webbrowser.open_new("http://www.google.com") }}} Presume you want to have 5-digit padding on a number: {{{ someInt = 123 print str(someInt).zfill(5) # 00123 }}} After converting your {{{int}}} object to a {{{string}}} object, you can use the {{{string.zfill}}} method to generate the padding. It's important that you work on {{{int}}}'s, and not {{{float}}}'s, since when converted to a {{{string}}}, the {{{float}}} decimal will just confuse {{{zfill()}}} ---- Another method, using [[string formatting|http://docs.python.org/library/stdtypes.html#string-formatting]] {{{ for i in range(10): print "image%05d.bmp" %(i) }}} {{{ image00000.bmp image00001.bmp image00002.bmp image00003.bmp image00004.bmp # etc... }}} I was browsing the web for anything Python related, and ran across this page: http://www.oreillynet.com/onlamp/blog/2007/02/simple_cpython_code_comparison.html I modified the code to my needs, and here is an example: {{{ import xml.etree.ElementTree as ET import urllib rssUrl = "http://pythonwiki.tiddlyspot.com/index.xml" et = ET.parse(urllib.urlopen(rssUrl)) for elem in et.findall("//item"): print elem.find("title").text print "\t"+elem.find("link").text }}} In the example, we grab the .rss feed of this wiki, and print a list of subjects, and their permalinks. Example of printing: {{{ Accessing the Windows clipboard http://pythonwiki.tiddlyspot.com#%5B%5BAccessing%20the%20Windows%20clipboard%5D%5D list comprehension http://pythonwiki.tiddlyspot.com#%5B%5Blist%20comprehension%5D%5D How can I see if a directory exists, and if not, make one? http://pythonwiki.tiddlyspot.com#%5B%5BHow%20can%20I%20see%20if%20a%20directory%20exists%2C%20and%20if%20not%2C%20make%20one%3F%5D%5D etc... }}} Docs to: *[[urllib|http://docs.python.org/library/urllib.html]] *[[ElementTree|http://docs.python.org/library/xml.etree.elementtree.html]] Here's an example using 'Windows Media Player' on my Win7 box: {{{ import os import subprocess player = os.path.join(os.getenv("ProgramFiles(x86)"), "Windows Media Player\wmplayer.exe") vid = "C:/videos/myVideo.avi" subprocess.Popen([player, vid]) }}} On Windows: http://docs.python.org/library/winsound.html {{{ import winsound winsound.PlaySound('c:/temp/myWav.wav', winsound.SND_ALIAS) }}} ---- There's also the builtin [[wave|http://docs.python.org/2/library/wave.html]] library that can read and write wav data, but I'm not sure if it can actually play it. It's nice to know all the data associated with an object at a glance. Thanks to our friends the {{{dir}}} and {{{eval}}} functions, we can query all the attrs on an object, then 'evaluate' their values: {{{ class Spam(object): """Super dooper Spam object""" def __init__(self): self.d = {"some":"dictionary value"} self.word = "eggs!" def attrPrinter(obj): for attr in sorted(dir(obj)): print attr, " : ", eval('obj.%s'%attr) meat = Spam() attrPrinter(meat) }}} {{{ __class__ : <class '__main__.Spam'> __delattr__ : <method-wrapper '__delattr__' of Spam object at 0x00D00810> __dict__ : {'word': 'eggs!', 'd': {'some': 'dictionary value'}} __doc__ : Super dooper Spam object __format__ : <built-in method __format__ of Spam object at 0x00D00810> __getattribute__ : <method-wrapper '__getattribute__' of Spam object at 0x00D00810> __hash__ : <method-wrapper '__hash__' of Spam object at 0x00D00810> __init__ : <bound method Spam.__init__ of <__main__.Spam object at 0x00D00810>> __module__ : __main__ __new__ : <built-in method __new__ of type object at 0x1E1CB570> __reduce__ : <built-in method __reduce__ of Spam object at 0x00D00810> __reduce_ex__ : <built-in method __reduce_ex__ of Spam object at 0x00D00810> __repr__ : <method-wrapper '__repr__' of Spam object at 0x00D00810> __setattr__ : <method-wrapper '__setattr__' of Spam object at 0x00D00810> __sizeof__ : <built-in method __sizeof__ of Spam object at 0x00D00810> __str__ : <method-wrapper '__str__' of Spam object at 0x00D00810> __subclasshook__ : <built-in method __subclasshook__ of type object at 0x011860C8> __weakref__ : None d : {'some': 'dictionary value'} word : eggs! }}} {{{ import sys path = sys.path }}} {{{sys.path}}} is made up of this data (in order I believe): #Home dir of the program (the location of the top-level module that was executed). #{{{PYTHONPATH}}} dirs, if any are set. #The Standard Library dirs: {{{<drive>:\Python<ver>\Lib\*}}} #Dirs defined by any {{{.pth}}} files (if they exist). The result of these four become what {{{sys.path}}} returns. {{{ import sys print sys.version # 2.5.1 (r251:54863, Jun 5 2007, 22:56:07) [MSC v.1400 32 bit (Intel)] print sys.version_info # (2, 5, 1, 'final', 0) }}} {{{sys.version}}} prints out a nice happy string, while {{{sys.version_info}}} returns a tupple with the corresponding ingredients. Via {{{inspect.getargspec}}} http://docs.python.org/library/inspect.html#inspect.getargspec {{{ import inspect def spam(paramA, paramB, paramC='52', pamarD=[1,2,3]): pass argspec = inspect.getargspec(spam) print argspec print argspec.args }}} {{{ ArgSpec(args=['paramA', 'paramB', 'paramC', 'pamarD'], varargs=None, keywords=None, defaults=('52', [1, 2, 3])) ['paramA', 'paramB', 'paramC', 'pamarD'] }}} It returns a {{{named tuple}}} http://docs.python.org/glossary.html#term-named-tuple The [[socket|http://docs.python.org/library/socket.html]] module can be used to easily query the 'hostname', the 'fully qualified domain name', and the IP address. {{{ import socket import maya.cmds as cmds print socket.gethostname() print socket.getfqdn(socket.gethostname()) print socket.gethostbyname(socket.gethostname()) }}} {{{ wCat wCat.domainName.com 10.111.3.120 }}} ---- Also see: *[[How can I query a users name? Their 'profile' directory?]] In Windows: {{{ name = os.getenv('USERNAME') profile = os.getenv('USERPROFILE') }}} On Vista: {{{ wCat C:\Users\wCat }}} ---- Also see: *[[How can I query a users host name, domain name, IP address?]] Not that I run into this very often, BUT, it's nice to know :) @@NOTE:@@ {{{sys._geframe()}}} is a {{{CPython}}} implementation detail: the leading underscore is telling you this is behind the scenes magic, and you shouldn't play with it, because it could change in future versions, or not be available in other Python implementations. Use at your own risk... ---- Presume you have some {{{moduleA.py}}}, and it imports {{{moduleB.py}}}. But in {{{moduleB.py}}}, you want to see some of the data in {{{moduleA.py}}}. How to do? By using the {{{sys._getframe(1).f_locals}}} command (mouthfull!), you can peek into that enclosing scope: Note that import happens *after* variable declaration... {{{ # moduleA.py foo = 23 sho = "asdf" import moduleB }}} {{{ # moduleB.py import sys moduleA_stuff = sys._getframe(1).f_locals print moduleA_stuff }}} When you then run {{{moduleA.py}}} at the prompt, you get this as the printed result (reformatted slightly to make easier to read), {{{f_locals}}} returns a dict: {{{ {'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:\\Documents and Settings\\<userName>\\My Documents\\python\\moduleA.py', '__name__': '__main__', 'foo': 23, '__doc__': None, 'sho': 'asdf'} }}} From the docs on {{{sys._getframe}}} http://docs.python.org/library/sys.html#sys._getframe <<< Return a frame object from the call stack. If optional integer depth is given, return the frame object that many calls below the top of the stack. <<< Frame Objects: ({{{f_locals}}}) http://docs.python.org/reference/datamodel.html#index-834 ---- So my //guess// is, {{{sys._getframe(1).f_locals}}} is getting a frame object one {{{(1)}}} level up from the current location, which happens to be top most (global) scope of the enclosing module {{{moduleA.py}}}, and it's then getting the results of {{{f_locals}}}, which from the docs is: "...the dictionary used to look up local variables". We could have also used {{{f_globals}}} here and got the same results, since in this case locals and globals are the same thing.  This works on Window.. but can provide incorrect results on Mac: {{{ import platform platform.architecture() ('64bit', '') }}} This seems hacky, but it works... at least on a Windows system: {{{ import sys print sys.version split = sys.version.split() bit = split[split.index('bit')-1] print bit }}} {{{ 2.6.1 (r26:67517, Dec 4 2008, 16:59:09) [MSC v.1500 64 bit (AMD64)] 64 }}}  If a file lives in Perforce, the {{{fstat}}} command will return a bunch of info on it. If it doesn't live in P4, it returns an empty list. Via the Perforce Python API: {{{ import P4 p4 = P4.P4() # only raise excptions on errors, not warnings: p4.exception_level = 1 p4.connect() result = p4.run_fstat(pathToMyFile) p4.disconnect() if result: print "Lives in P4!", pathToMyFile else: print "Not in P4!", pathToMyFile }}} Few different systems listed below: ---- Pulled info from [[this post|http://techarttiki.blogspot.com/2008/08/read-only-windows-files-with-python.html]], changed it a bit below: http://docs.python.org/library/stat.html http://docs.python.org/library/os.html#os.stat {{{ import os import stat # define some magic values: writable = 33206 readonly = 33060 myFile = r'c:/temp/mayFile.txt' statInfo = os.stat(myFile) fileState = statInfo.st_mode if fileState == readonly: print "File is read-only, made writeable." os.chmod(myFile, stat.S_IWRITE) elif fileState == writable: print "File is writeable, make read-only." os.chmod(myFile, stat.S_IREAD) }}} {{{os.stat}}} returns an object which has a bunch of attrs. One of them, {{{.st_mode}}}, contains some magic values that mean if the file is readable or writable. Checking against those values tells you what state its in, at which point you can use {{{os.chmod}}} passing in attrs from the {{{stat}}} //module// to update the state. Kind of wacky if you ask me ;) ---- Another query system: http://docs.python.org/library/os.html#os.access {{{ import os os.access(file, os.F_OK) # does it exist? os.access(file, os.R_OK) # is it readable? os.access(file, os.W_OK) # is it writable? os.access(file, os.X_OK) # is it executable? }}} ---- Another way for making writable: http://docs.python.org/library/os.html#os.chmod {{{ import os os.chmod("c:/temp/foo.txt", 0777) }}}  {{{ import inspect def spam(): pass print inspect.isfunction(spam) }}} {{{ True }}} Python as the concept of the 'current working directory'. You can query this via the {{{os}}} module: {{{ import os homeDir = os.getcwd() # c:\Documents and Settings\<userName>\My Documents\python }}} You can also change this dir to be a user-defined value: {{{ os.chdir("c:/temp") print os.getcwd() # 'c:\\temp' }}} [[chdir docs|http://docs.python.org/library/os.html#os.chdir]] [[getcwd docs|http://docs.python.org/library/os.html#os.getcwd]] By getting and querying a 'frame object': http://docs.python.org/library/inspect.html#inspect.currentframe http://docs.python.org/library/inspect.html#types-and-members {{{ import inspect def getLine(): return inspect.currentframe().f_back.f_lineno }}} You can now do something like this anywhere in your code: {{{ print "The current line is: %d"%getLine() }}} ---- Note from the docs on {{{inspect.currentframe}}} <<< ~CPython implementation detail: This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns None. <<<  {{{ import datetime print datetime.date.today() # 2008-05-19 print datetime.datetime.now() # 2009-06-24 15:35:37.787000 }}} You can access the special {{{__module__}}} attribute on the given function object: {{{ # myModule.py def spam(): pass def main(): print spam.__module__ }}} This will print {{{myModule}}} when ran. Docs on special module attrs [[here|http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy]], under the 'Callable types' section.  Access the {{{__name__}}} attribute of the given function: {{{ def myfunc(): pass print myfunc print myfunc.__name__ }}} {{{ <function myfunc at 0x0000000002D28F98> myfunc }}} Kind-of practical example: You want to print both the function name called too, plus capture its return value for the user to see: {{{ def myfuncA(): return "spam" def myfuncB(): return "eggs" for func in (myfuncA, myfuncB): print "Function %s() returned"%func.__name__, func() }}} {{{ Function myfuncA() returned spam Function myfuncB() returned eggs }}} http://docs.python.org/library/sys.html#sys.platform {{{ import sys print sys.platform # win32 }}} What's interesting, is that when I run this on my Windows Vista 64-bit box, with 64-bit Python installed, it still prints{{{win32}}}. hmmm..... In a module, this is trivial: {{{ import sys print sys.executable }}} On the PC: {{{ C:\Python27\python.exe }}} On the Mac: {{{ /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python }}} ---- Also, on the Mac, from a shell, you can execute: {{{$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
}}}
It's interesting to note that the browse to that dir, and execute Python, and then print {{{sys.executable}}}, it will actually return the longer path shown above.  I'm guessing the path returned by {{{which}}} is actually some sort of simlink.
The [[sys.getsizeof()|http://docs.python.org/dev/library/sys.html#sys.getsizeof]] function will return the size of the given object, in bytes.
{{{
import sys
print sys.getsizeof('a')
# 41
}}}
So apparently, the {{{string}}} object takes up 41 bytes.
{{{os.environ}}} returns a [[Dictionary|DICTIONARY]]
{{{
import os
var = os.getenv("PATH")
}}}
----
Also see:
*[[How can I get a list of Environment Variables?]]
*[[How can I set an environment variable?]]

{{{vars()}}} - [[Python Docs|http://docs.python.org/dev/library/functions.html#vars]]

{{{vars()}}} can be used in two ways: You can pass an object into it, or not.
*If you //don't// pass in an object reference, it will return back a dictionary of the //current local [[scope's|Python variable scope]]// variable names.
*If you pass in an object reference, it will return a dictionary corresponding to that object's [[attribute]] [[namespace]] (also known as it's [[__dict__|Understanding __dict__]], or 'symbol table')

From the docs:
<<<
Without arguments, return a dictionary corresponding to the current local symbol table. With a module, class or class instance object as argument (or anything else that has a {{{__dict__}}} attribute), returns a dictionary corresponding to the object’s symbol table. The returned dictionary should not be modified: the effects on the corresponding symbol table are undefined.
<<<
The below example will return back the {{{__dict__}}} of the current scope:
{{{
D = vars()
for key in D:
print key, str(D[key])
}}}
{{{
import sys
for key in sorted(sys.modules):
print key
}}}
{{{
type(["asdf", "fdsa"])
# Result: <type 'list'> #

type(3)
# Result: <type 'int'> #

import os
type(os)
# Result: <type 'module'> #

type(open)
# Result: <type 'builtin_function_or_method'> #

type(type)
# Result: <type 'type'> #
}}}
Some options...
----
1.  You can use {{{inspect.getfile}}} or {{{inspect.getabsfile}}} to return the path to the module:
{{{
import foo
import inspect
print inspect.getfile(foo)
print inspect.getabsfile(foo)
#  c:\pylib\foo.py
#  c:\pylib\foo.py
}}}
----
2.  Calling {{{help(moduleName)}}} will print out lots of info on the module, including where it's saved.
----
3.  Calling {{{reload(moduleName)}}} will also tell you where it was reloaded from
----
4.  Some modules have the full path in their special {{{__file__}}} attr, while others only have the filename.  Still others have no {{{__file__}}} attr at all.  You can use the {{{inspect}}} function above irregardless.  But //sometimes// you can use this method too:
{{{
# inspect happens to have a valid .__file__ attr... not all modules do
import inspect
print inspect.__file__
# c:\Python25\lib\inspect.pyc
}}}
OR, inside your module, you can just do this:
{{{
# myModule.py

print __file__
}}}
Would print when executed:
{{{
c:\pyStuff\myModule.py
}}}
----
Also see:
*[[How can a module reference where itself has been saved?]]
*[[Getting module save locations]]
*[[How can a module query relative directories to where it has been saved?]]
If you are iterating over a long {{{for loop}}}, or in a {{{while loop}}} running forever, how can you cancel out?
An easy solution is to wrap the loop in a try\except checking for a {{{KeyboardInterrupt}}}, which is spawned via the user pressing {{{ctrl+c}}} in the active Python shell.  If it catches that exception, you can exit out of the func:
{{{
# myAwesomeModule.py

def main():
try:
while True:
print "running!"
except KeyboardInterrupt:
print "Cancled via ctrl+c"
return

if __name__ == "__main__":
main()
}}}
I heard about a scientist that figured out that if you randomize the letters of a word, if the first and last letters remain the same you can still read the word.  Took a shot at doing it in Python:
{{{
import random

def shuffler(sentence):
words = sentence.split(' ')
updated = []
for word in words:
if len(word) >= 4:
mid = list(word[1:-1])
random.shuffle(mid)
newWord = '%s%s%s'%(word[0], ''.join(mid), word[-1])
updated.append(newWord)
else:
updated.append(word)
result = ' '.join(updated)
return result

print shuffler("You can randomize all the letters in the middle of a word and still be able to read them.")
}}}
{{{
You can rmzaodine all the lteters in the mddlie of a wrod and sitll be albe to read tehm.
}}}

And strip off the newline characters in the process?
{{{
filelist = []
for item in f:
filelist += [item.rstrip()]
f.close()
}}}
The above works, but it loads the whole file into memory, then duplicates it in memory.
----
If you just want to iterate over each line in the file, this is much cleaner, and uses the file objects iterator method:
{{{
for line in open('myfile.txt'):
print line.rstrip()
# foo a
# foo b
# etc...
}}}
----
Here's yet another approach using iterators:
{{{
f = open('myfile.txt')
print f.next().rstrip()
# foo a
print f.next().rstrip()
# foo b
f.close()
}}}
If you reach the end of a file using this example, the {{{.next()}}} method will raise {{{StopIteration}}}
----
Another way, using a list comprehension:
{{{
lines = [line.rstrip() for line in open('myfile.txt', 'r')]
print lines
# ['foo a', 'foo b', etc...]
}}}
----
Yes, more using a [[context manager|with statement]] (and list comprehension), Python 2.6+:
{{{
with open("myFile.txt", 'r') as f:
lines = [item for item in f]
print lines
}}}
{{{
import os
import re

# path we should start searching:
startDir = r"c:\temp" # print startDir
# search parameter for filename we're looking for:
searchFile = ".py"
# when searching in the file on each line, string to look for:
searchString = "foo"

for dirpath, dirnames, filenames in os.walk(startDir):
print "Starting Search:\n"
for fil in [f for f in filenames if f.endswith(searchFile)]:
result = []
fh = open(dirpath + "\\" + fil)
for i,line in enumerate(fh):
if re.search(searchString, line):
result.append("\t"+ str(i) + ": " +line.strip())
if len(result):
print (dirpath + "\\" + fil )
for r in result:
print r
fh.close()
print "\nEnding Search\n"

# Starting Search:
#
# c:\temp\spam.py
# 	84: foo = 24
#	211: goo = foo
#
# Ending Search
}}}
----
Also see:
*[[How can I get a recursive list of directories?]]
*[[How can I search for files in directories faster than os.walk?]]
You can use the {{{round}}} built-in function:
{{{
num = 1.2345
print round(num, 2)
# 1.23
}}}
----
If you simply want to print a shorter number, but not actually modify the value itself, please see:
[[Format floating point precision for printing]]
The magical {{{reload}}} module:
{{{
}}}
{{{
seq = ["a", "b", "c", "a", "b", "c"]
seq = list(set(seq))
print seq
# ['a', 'c', 'b']
}}}
By using {{{set}}}s (which can't hold duplicate values), you can prune out the dupes.
You can comment on this over on my [[main blog|http://www.akeric.com/blog/?p=312]].

I process a lot of paths.  Some of these paths are entered by hand, by a human.  Other paths are machine generated by some tool.  Sometimes the tool will respect the case entered originally, other times it makes everything lowercase.  Based on these combinations, you could be dealing with two paths that are the exact same (location on disk), but some have upper-case characters defining part of their name, and others, lower-case text.  If you need to process these paths, and change them from one relative path to another, these case inconsistencies can become a real pain.  Below is //one// solution around the issue.  I'd be interested to see others :)

Using {{{re.findall}}}, we can search in a string.  But what really helps is {{{re.IGNORECASE}}}.  This gives us a matching string based on the //case of the source string//, which we can then use to replace with later using the {{{.replace()}}} string method:
{{{
import re

srcPath ="c:/my/path/wIth/mIxeD/case"
match = "/with/mixed/"
replace = "/normal/"
resultPath = ""

try:
sourceCaseMatch = re.findall(match, srcPath, re.IGNORECASE)[0]
resultPath = srcPath.replace(sourceCaseMatch, replace)
except:
pass

print "Result: '" + resultPath + "'"
# Result: 'c:/my/path/normal/case'
}}}
<<<
''Update'':  I lied below.  It's just as slow :(  After the first time you search it appears that the data is cached, which causes future searches to be faster.  After restarting my machine, the below method was just as slow.  But I'll leave the subject around //as a warning to others.// ;)
<<<

As explained in other subjects, you can use Python's {{{os.walk}}} to recursively walk the directory tree.  However, I've found this to be really slow at times depending on what you're looking for, and how bit the tree is that's being searched.

A quicker way (for myself at least) is rather than calling on Python to search the tree, call to the OS directly.  In my case I'm using Windows7, and a combination of the dos {{{DIR}}} and {{{FINDSTR}}} commands do the job.

In the below example, I set a starting search path to be {{{C:\my\Path}}}.  Then I query for all subdirs named {{{\someSubDir}}} and loook for all files that end with the string {{{_postfix.py}}}.  I execute the command, and capture the return via {{{subprocess.Popen}}}, and clean up the results via a [[list comprehension]]:
{{{
import subprocess
dosCmd = r'DIR C:\my\Path e /s /b |FINDSTR /R \\someSubDir\\.*_postfix\.py$' result = subprocess.Popen(dosCmd, shell=True, stdout=subprocess.PIPE).communicate() data = [f.strip() for f in result[0].split("\r\n") if f != ""] for d in data : print d }}} I've found that in my case a search via {{{os.walk}}} which easily could have taken 30-45 seconds is reduced to less than 1 second. The below function simply takes in a string (presumably some Python command), and will open a web browser, using Google to search for the command in {{{docs.python.org}}} {{{ import urllib import webbrowser def searchPyhonViaGoogle(txt): """ Search docs.python.org via google for the provided command, and launch a browser txt : (str) The Python command to search for """ url = urllib.urlencode((('q', txt + ' site:docs.python.org'), ('ie', 'utf-8'))) url = "http://www.google.com/search?" + url webbrowser.open_new(url) }}} http://docs.python.org/library/os.path.html#os.path.isdir http://docs.python.org/library/os.html#os.mkdir http://docs.python.org/library/os.html#os.makedirs http://docs.python.org/library/os.path.html#os.path.lexists The below code works if the parent dir (in this case, {{{/temp}}}) already exists: {{{ import os if not os.path.isdir("c:/temp/myPath"): os.mkdir("c:/temp/myPath") }}} You can also use for the test: {{{ os.path.lexists("c:/myPath") }}} {{{os.mkdir}}} only makes leaf directories. If you need to build a series of intermediate directories at the same time (in the below example, {{{/temp}}} already exists, but {{{/spam}}} doesn't), you need to use: {{{ import os path = 'c:/temp/spam/eggs' if not os.path.isdir(path): os.makedirs(path) }}} Which will error if the leaf dir already exists, that's why we need to check first. A variety of ways, see below. ''Important things'': *Adding a slash at the END of a path seems to make it not be recognized, so avoid that. *Python likes it's paths to have //backslashes// : {{{\}}} . Forwardslahes don't work so well. Flee from them. ---- ''Updating Solutions'': You can do it on the fly in your modules this way (but see notes on ''trickery'' at the bottom): {{{ import sys sys.path += ["c:\\someNewPath"] # or sys.path.append("c:\\someNewPath") }}} I've also seen problems when using double-backslashes and some letter combo's: Python turns them into special characters, and mangles the path. This next example is what I actually use, utilizing a 'raw' string, and //forwardslashes//: {{{ import sys import os sys.path.append(os.path.normpath(r"c:/someNewPath")) }}} ---- You can set\modify the 'PYTHONPATH' environment variable. These are added to the head of {{{sys.path}}}: {{{ PYTHONPATH = c:\someNewPath; }}} ---- Create {{{.pth}}} files, each which contains a //single path//. Store them in your python install dir, like: {{{ c:\pythonXX\myCustompath.pth }}} Also see the [[site|http://docs.python.org/library/site.html]] module ---- Modify the site.py file, updating {{{sys.path}}} in that file. The drawback is if you ever reinstall Python, this will be overwritten. {{{ c:\PythonXX\Lib\site.py }}} ---- Some ''trickery'': If you are authoring code in a IDE that allows for 'Evaluate Selection' (you can highlight blocks of text, and execute them withought having to make them their own module), when executing a selection of lines that includes pathing updates, it appears that the path updates don't 'stick' past the execution evaluation. Immediately after execution, if you query {{{sys.path}}}, the update //won't// be there. If you execute a code-block that includes pathing updates, and importing of modules from said update all at once, it //will// work, even though after the fact the path //won't// have your update. Executing the code line-by-line won't work either (since each line-block will forget any pathing info defined). //However//, if you embed this update into a module\function, and execute said function (the //normal// way of doing things, the updates will stick. Odd. ---- Also see *[[Packages]] *[[How can I append to Python's site-packages dir?]]  If you can query an environment variable via [[os.getenv|http://docs.python.org/2/library/os.html#os.getenv]] like so: {{{ import os myvar = os.getenv("MYVAR") }}} You'd //think// you'd be able to able to set one like the same way, via [[os.putenv|http://docs.python.org/2/library/os.html#os.putenv]]: {{{ os.putenv("MYVAR", "my var value") }}} On Windows however, {{{putenv}}} doesn't seem to do anything.... :-S To actually update the var, you need to update via [[os.environ|http://docs.python.org/2/library/os.html#os.environ]] {{{ os.environ["MYVAR"] = "my var value") }}} ---- Also see: *[[How can I get a list of Environment Variables?]] From this link: http://stackoverflow.com/questions/7387276/set-window-name-in-python On Windows: {{{ from os import system system("title %s"%theTitle) }}} Via [[pywin32]]: {{{ win32console.SetConsoleTitle("The Title") }}} Via the commandline: {{{ > python myModule.py -t The Title }}} Easy enough to sort a dictionary by key, but how do you sort it by value? {{{ dic = {"a":1.0, "c":3.0, "b":2.0, "d":1.0} }}} {{{ # Example #1 from operator import itemgetter dList = sorted(dic.items(), key=itemgetter(1)) print dList }}} {{{ # Example #2 dList = dic.items() dList.sort(key=lambda x: x[1]) print dList }}} They both print: {{{ [('a', 1.0), ('d', 1.0), ('b', 2.0), ('c', 3.0)] }}} As you can tell from example #1, it's {{{sorted}}} function using the {{{operator.itemgetter}}} function that does the magic. Docs: http://docs.python.org/library/functions.html#sorted http://docs.python.org/library/operator.html#operator.itemgetter For example #2, it's the method {{{list.sort}}} and {{{lambda}}}. Docs: http://docs.python.org/tutorial/datastructures.html#more-on-lists http://docs.python.org/reference/expressions.html#lambda ---- Example #3: The trick described below is to use a custom compare function which does a value comparison based on the passed in tuples, that is passed to the {{{sorted}}} function: {{{ def srtCmp(x, y): """ x & y : (key, value) : Both x & y are expected to be tuples of key:value pairs. """ keyX, valX = x keyY, valY = y if valX < valY: return -1 elif valX == valY: return 0 elif valX > valY: return 1 # Make a dict with values we want to sort by: d = {"A":1.0, "C":5.2, "Q":0.5} # Return a list of tuples of the dict items, # sorted by their value, rather than key: # The items() method returns a list of # tuple (key,value) pairs. goodSort = sorted(d.items(), cmp=srtCmp) # Returns the dict items in order or their sorted # *keys* (not what we want). badSort = [(key, d[key]) for key in sorted(d)] print badSort print goodSort [('A', 1.0), ('C', 5.2), ('Q', 0.5)] #bad, by key [('Q', 0.5), ('A', 1.0), ('C', 5.2)] #good, by value }}} You can create a custom sorter function that compares the modification date of the passed in list of files: {{{ import os import glob def sortByDate(a,b): """ Custom sort function designed to sort a list of files based on their modification date. """ aTime = os.path.getmtime(a) bTime = os.path.getmtime(b) if aTime < bTime: return -1 elif aTime == bTime: return 0 elif aTime > bTime: return 1 files = glob.glob("c:/temp/*.txt") dateSortedFiles = sorted(files, cmp=sortByDate) }}} 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 {{{sorted}}} function, or the list method {{{sorted}}} both accept a {{{cmp}}} 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. http://docs.python.org/library/functions.html#sorted http://docs.python.org/tutorial/datastructures.html#more-on-lists http://docs.python.org/library/stdtypes.html#mutable-sequence-types <<< "{{{cmp}}} specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument" <<< ---- As you can see, just sorting it doesn't do what I want: {{{ print sorted(['x', 'y', 'z', '-x', '-y', '-z']) # ['-x', '-y', '-z', 'x', 'y', 'z'] }}} So, we make a sort rule: {{{ # My cmp tester: def negTest(x,y): if '-' in x and '-' not in y: return 0 else: return 1 }}} Using the list {{{sort}}} method, which changes the list in-place: {{{ s.sort(cmp=negTest) print s # ['x', 'y', 'z', '-x', '-y', '-z'] }}} Or using the {{{sorted}}} function, which creates a new list: {{{ newS = sorted(s, cmp=negTest) print newS # ['x', 'y', 'z', '-x', '-y', '-z'] }}} You can also use a {{{lambda}}} for the {{{cmp}}} arg, but my brain wasn't working well enough to make this happen. I often encountered lists of names with numeric values on the end that have no zero padding. Meaning for example, a list of node names like so: {{{['a1', 'a9', 'a10', 'a99', 'a100']}}} Sorting this list with the default Python sort function will return it in an incorrect order though, since there is no zero padding in front of the numbers: {{{ print sorted(['a1', 'a9', 'a10', 'a99', 'a100']) ['a1', 'a10', 'a100', 'a9', 'a99'] }}} Python's {{{sorted}}} function takes a special '[[comparison function|http://docs.python.org/library/functions.html#sorted]]' argument that you can use to sort by your own rules. ---- New solution: {{{ def endExtSrt(x,y): """ Used to sort non-padded names in the correct order. """ xNums = re.findall('[0-9]+$', x)
yNums = re.findall('[0-9]+$', y) # If we find a number on both items: if xNums and yNums: numX = int(xNums[0]) numY = int(yNums[0]) if numX < numY: return -1 if numX == numY: return 0 if numX > numY: return 1 # Otherwise, do normal sort: else: return cmp(x,y) }}} {{{ data = ['bob1', 'bob10', 'bob03', 'bob2', 'bob11', 'bob', 'joe', 'frank', 'apple'] print sorted(data) print sorted(data, cmp=endExtSrt) }}} {{{ ['apple', 'bob', 'bob03', 'bob1', 'bob10', 'bob11', 'bob2', 'frank', 'joe'] # incorrect order ['apple', 'bob', 'bob1', 'bob2', 'bob03', 'bob10', 'bob11', 'frank', 'joe'] # correct order }}} ---- Older solution: Below I create one of these functions, extract the numeric values from the end of each item, pad them with zero's (I figured a 32 values should be big enough for most of my needs), then compare the results: {{{ import re def sortTest(x, y): try: xNum = re.findall('[0-9]+$', x)[0]
xStr = x.rstrip(xNum)
except IndexError:
xNum = '0'
xStr = x

try:
yNum = re.findall('[0-9]+$', y)[0] yStr = y.rstrip(yNum) except IndexError: yNum = '0' yStr = y # Rebuild our items with padded numeric values xA = '%s%s'%(xStr, xNum.zfill(32)) yA = '%s%s'%(yStr, yNum.zfill(32)) if xA < yA: return -1 elif xA == yA: return 0 elif xA > yA: return 1 data = ['a1', 'a2', 'a11', 'a113', 'a13', 'a21', 'a03', 'a30', 'a07'] print sorted(data) print sorted(data, cmp=sortTest) }}} Prints: {{{ ['a03', 'a07', 'a1', 'a11', 'a113', 'a13', 'a2', 'a21', 'a30'] # incorrect order ['a1', 'a2', 'a03', 'a07', 'a11', 'a13', 'a21', 'a30', 'a113'] # correct order with custom sorter }}} {{{ pather = r"c:\temp\foo.txt" print os.path.splitext(pather) # ('c:\\temp\\foo', '.txt') print os.path.splitext(pather)[0] # c:\temp\foo }}}  Given a list of items, I want to generate a list of sublists each with the same number of items: {{{ stuff = [1,2,3,4,5,6,7,8,9] step = 3 split = [stuff[i:i+step] for i in range(0, len(stuff), step)] print split # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] }}} Pythons built in string method {{{split()}}} ({{{"string".split("chars")}}}) works good for splitting a string based on a given sequence of characters. For example: {{{ print "split|me:please".split("|") # ['split', 'me:please'] }}} But what if you wanted to split that line by both the pipe ({{{|}}}) and the colon ({{{:}}})? This would fail: {{{ print "split|me:please".split("|:") # ['split|me:please'] }}} It fails because Python can't find an exact match of the string '{{{|:}}}'. Here are several solutions using {{{re}}} that works nicely: (I found this info [[here|http://bytes.com/topic/python/answers/716054-s-split-multiple-separators]]) {{{ import re word = "split|me:please" reComp = re.compile('[|:]') print reComp.split(word) print re.split('[|:]', word) print re.split('[|:]+', word) print re.findall('[^|:]+', word) }}} All instances print: {{{ ['split', 'me', 'please'] }}} ---- Older function I wrote that works around the issue, kept for prosperity: {{{ def charSplit(string, chars): """ string : The string in question to split chars : list : list of strings to chop string up by """ splitted = [string] for c in chars: newSplit = [] for s in splitted: chopped = s.split(c) try: chopped.remove("") except ValueError: pass newSplit = newSplit + chopped splitted = newSplit return splitted }}} {{{ name = "namespace:one|namespace:two|namespace:three" splitted = charSplit(name, list(":|")) print splitted }}} {{{ ['namespace', 'one', 'namespace', 'two', 'namespace', 'three'] }}} By letting you pass in a list, you can still try to separate by multiple-character strings: {{{ charSplit(name, ["|", "<>", "@@"]) }}}  Presuming you want to format names for proper windows directory or file names, what's a way to do it? I'm SURE there is a more elegant, Pythonic way to do this. Hey, I'm still learning! :) But this definitely works! {{{ import string def convertLegalName(txt): result = "" good = list(string.letters + string.digits + "_") letters = list(txt) for letter in letters: if letter not in good: result = result + "_" else: result = result + letter return result txt = "This line, has many (bad) characters that shouldn't be used at all!" print convertLegalName(txt) # This_line__has_many__bad__characters_that_shouldn_t_be_used_at_all_ }}} I thought I'd try doing something similar with a [[list comprehension]]. This does the same as above, removes bad chars, but //doesn't// replace then with an underscore '_'. And it's much more 'Pythonic' I think ;) {{{ def convertLegalName(txt, good): done = ''.join([char for char in list(txt) if char in list(good)]) return done # Usage: import string # this has some questionable characters in it... txt = '∩╗┐<COLLADA version="0.4">' good = string.printable convert = convertLegalName(txt, good) print convert # <COLLADA version="0.4"> }}} Also check out {{{string.printable}}} for every legal printable char (a combination of .letters, .digits, .punctuation, including 'space'), and {{{string.punctuation}}} for a list of, well, punctuation symbols. I don't know if there is a better way (maybe with sets?), but I came up with this method using List Comprehensions: {{{ listA = ["a","b"] listB = ["b", "c"] listC = [item for item in listB if item not in listA] print listC # ['c'] }}}  {{{ # convert to forwardslash paths from backslash: path = r"s:\tab\tab" path = os.path.normpath(path).replace("\\", "/") print path # "s:/tab/tab" }}} {{{ # convert to backslashes from forwardslashes: path = "s:/tab/tab" path = os.path.normpath(path) print path # "s:\tab\tab" }}} Came up with two different ways, one via the API, and one via the system. The API is nice, but I can't find any way to extract the results. Could just be my ignorance. Calling to P4 at the system level makes it easy to get the results: {{{ # Via the P4 API: import P4 changelist = 1234 p4 = P4.P4() p4.connect() # Only raise exceptions, not warnings p4.exception_level = 1 print "Starting P4 sync for changelist %s"%changelist p4.run_sync('@=%s'%changelist) p4.disconnect() }}} {{{ # Via calls to the system: import subprocess changelist = 1234 print "Starting P4 sync for changelist %s"%changelist result = subprocess.Popen(['p4', 'sync', '@=%s'%changelist], stdout=subprocess.PIPE).communicate() foo = [f.strip() for f in result[0].split("\r\n") if f != ""] if len(foo): for f in foo: print "\t", f else: print "\tEverything is up to date." print "Finished P4 changelist sync, see results above:" }}} http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373 Info from [[this doc|http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/python.1.html]] To set the 'default python': {{{$ defaults write com.apple.versioner.python Version 2.7
}}}
To set a system-wide default, replace {{{com.apple.versioner.python}}}' with {{{/Library/Preferences/com.apple.versioner.python}}}' ({{{sudo}}} admin privileges will be required).

These can also be set by env vars:
{{{
$export VERSIONER_PYTHON_VERSION=2.7 # Bourne-like shells }}} or {{{$ setenv VERSIONER_PYTHON_VERSION 2.7 # C-like shells
}}}
This environment variable takes precedence over the preference file settings.
Couple different ways:
{{{
import operator
def same(items):
"""
items : list of objects.
return : bool : True \ False
"""
return all( [ operator.is_(items[0], x) for x in items[1:] ] )
}}}
{{{
def same(items):
"""
items : list of objects.
return : bool : True \ False
"""
ret = True
for i in items[1:]:
if i != items[0]:
ret = False
break
return ret
}}}
You can use the string method {{{.join()}}} to do this:
{{{
foo = ["this", "is", "my", "list"]
sting = ' '.join(foo)
print sting
# this is my list
}}}
The characters in the string being created become the separators of the items of the list.
I did this as an exercise to wrap my head around decorators as well.  It uses the [[time|http://docs.python.org/library/time.html]] module.  There's also the [[timeit|http://docs.python.org/library/timeit.html]] module that has a lot of functionality.

The decorator object will execute the wrappered function 1000 times, print how long the whole operation took, and how long each loop took.  The decorator also returns a list of [evaluationTime, functionName].  This allows you to profile many different functions, capture the return, and sort the results for which was fastest.

It should be noted that this code expects the functions being decorated to not return anything, since the decorator obviously returns it's own data and would hide anything the called function would.
{{{
import time

class Timer(object):
# Our Decorator

def __init__(self, f):
self.f = f

def __call__(self):
start = time.time()
for i in range(1000):
self.f()
end = time.time()
total = end - start
print ("Completed 1000 iterations of %s() in %.6f seconds, %.6f per loop"+
"")%(self.f.__name__, total, total/1000.0)
return [total, self.f.__name__]

@Timer
# Example function to time, wrapped in the decorator.
nums = [str(num) for num in range(1000)]
result = " ".join(nums)

@Timer
# Example function to time, wrapped in the decorator.
nums = range(1000)
result = ""
for i in range(len(nums)):
result += str(i)
}}}
{{{
speedTest = []

speedTest.sort()
for speed in speedTest:
print speed
}}}
{{{
Completed 1000 iterations of stringAddA() in 0.203000 seconds, 0.000203 per loop
Completed 1000 iterations of stringAddB() in 0.390000 seconds, 0.000390 per loop
}}}
Python 2.6 and newer can use the {{{with}}} context manager, which is pretty slick, since it handles the file closing for you:
{{{
with open("c:/temp/foo.txt", "w") as f:
f.write("data\n")
}}}
Other ways:
{{{
f = open("c:/temp/foo.txt", "w")
f.write("data\n")
f.close()
}}}
{{{
p = "c:/temp/temp.txt"
t = ["line1\n", "line2\n", "line3\n"]
f = open(p, 'w')
f.writelines(t)
f.close()
}}}
http://docs.python.org/library/stdtypes.html#bltin-file-objects

The [[atexit|http://docs.python.org/library/atexit.html#module-atexit]] module allow for this:  You can, via its [[register|http://docs.python.org/library/atexit.html#atexit.register]] function, have multiple functions be execute during exit.

[[sys.exitfunc|http://docs.python.org/library/sys.html#sys.exitfunc]] also provides for this, but only with a single function.
----
Example function that can be ran, that will write out a log file of the time Python was closed:
{{{
# makeExitLog.py

import os
import atexit
import datetime

logfile = os.path.join(os.getenv('TEMP'), 'pyCloseLog.txt')

def logWhenClose(*args):
"""
Will save a log file recording when Python was closed to the users TEMP dir,
which on Windows 7 is here: C:\Users\<userName>\AppData\Local\Temp
"""
date = datetime.datetime.now()
with open(logfile, "a") as f:
f.write("Python exited: %s\n"%date)
print "Wrote to exit log!"

def main():
atexit.register(logWhenClose)
}}}
{{{
$python >>> import makeExitLog >>> makeExitLog.main() >>> quit() Wrote to exit log! }}} Then you can go open {{{C:\Users\<userName>\AppData\Local\Temp\pyCloseLog.txt}}} to confirm this.  objects come with a special attribute called {{{__module__}}} that holds the module name where the class was created. Furthermore, it returns back the full [[package|Packages]] path to the module, if the module is in a package. {{{ # whereAmI.py, in the data package class Spam(object): def __init__(self): print self.__module__ }}} {{{ import data.whereAmI eggs = whereAmI.Spam() }}} prints: {{{ data.whereAmI }}} I found this out by poking around the [[inspect|http://docs.python.org/library/inspect.html#inspect.getmembers]] module. ---- Also see: *[[Getting module save locations]] You can combine the 'up a directory' syntax '{{{..}}}' combined with the module's {{{__file___}}} attribute wrappered in {{{os.path.join}}} and {{{os.path.realpath}}}: {{{ # saved in c:/temp/foo/dirtest.py import os print "Module save dir:", os.path.realpath(os.path.join(__file__, '..')) print "Up a dir:", os.path.realpath(os.path.join(__file__, '..', '..')) }}} {{{ C:\temp\foo>dirtest.py Module save dir: C:\temp\foo Up a dir: C:\temp }}} It's one thing for a module to go look and see where //another// module has been saved. But how can a module reference where itself has been saved? This is presuming these are modules authored by hand, and saved as {{{.py}}}. ''A few ideas:'' ---- When a module is executed, you can use {{{sys.argv[0]}}} to find the full path of the executed module. Pass that into some {{{os}}} functions to find the absolute path. Presuming both of these modules have been saved under {{{c:\stuff}}}: {{{ # fileA.py import sys print "fileA.py:", os.path.dirname(os.path.abspath(sys.argv[0])) }}} {{{ # fileB.py import sys import fileA print "fileB.py:", os.path.dirname(os.path.abspath(sys.argv[0])) }}} When {{{fileB.py}}} is executed: {{{ fileA.py: c:\stuff fileB.py: c:\stuff }}} I should note this isn't always true: I've queried this, and it actually returned the name of the executable that was launched //first// (Maya), then ran its own version of Python which called to my module... sigh.... ---- I've used the special {{{__file__}}} attr with good success: {{{ # c:\stuff\myModule.py print __file__ }}} would print: {{{ c:\stuff\myModule.py }}} If you want to get just the directory, here is some shorthand: {{{ # c:/temp/test.py import os print os.path.realpath(os.path.join(__file__, '..')) }}} would print: {{{ c:/temp }}} ---- Found [[this post|http://mail.python.org/pipermail/python-list/2003-December/242365.html]] describing a cleaver way to use [[inspect.getsourcefile|http://docs.python.org/library/inspect.html#inspect.getsourcefile]] and {{{lambda}}}. {{{ # c:\stuff\myModule.py import inspect print inspect.getsourcefile(lambda:None) }}} would print: {{{ c:\stuff\myModule.py }}} {{{getsourcfile}}}, as the name says, returns back the name of the module a piece of code was authored in, and {{{lambda}}} returns back an anonymous function inside of a given module. ---- Also see: *[[How can I query where a module is saved?]] *[[Getting module save locations]]  You can call to the {{{object.__class__.__name__}}} attr: {{{ class Spam(object): def __init__(self): print self.__class__.__name__ eggs = Spam() }}} {{{ Spam }}} {{{ x = input("Please enter a value") }}} You can use the {{{try}}} statement \ {{{except}}} clause: {{{ mylist = ["foo", "goo", "shoe"] val = mylist.index("new") # Traceback (most recent call last): # File "<string>", line 1, in <string> # ValueError: list.index(x): x not in list }}} As we can see, we've tried to access a value in {{{mylist}}} that doesn't exist, so Python raises a {{{ValueError}}}, which would stop the module\function from running. How to handle the exception: {{{ try: mylist = ["foo", "goo", "shoe"] val = mylist.index("new") except(ValueError): print "Caught a exception, which was 'ValueError'." # Caught a exception, which was 'ValueError'. }}} The {{{try}}} \ {{{except}}} catches the exception, and will allow the code to keep on running. There are many different types of exceptions that can be caught. The listed {{{ValueError}}} is singling out a specific type of exception for the user. If you don't want to be so specific, you can use just {{{except:}}}, which will catch any and all kinds of exceptions. You can also catch the 'exception argument', which is the message Python tells you what is wrong when the exception occurs. Based on the above example, we'll change our exception handling to grab any type of exception, and report what it found. This is done via the {{{e}}} seen below. {{{ try: mylist = ["foo", "goo", "shoe"] val = mylist.index("new") except(Exception), e: print "Caught an exception. This is what Python said:\n", e # Caught an exception. This is what Python said: # list.index(x): x not in list }}} ---- ''Here's a link to the full list of [[Built-In Exceptions|http://docs.python.org/library/exceptions.html]]'' ---- Why, you may ask, are there so many different kinds of exceptions? For example, my scripting background is {{{MEL}}} (Maya Embedded Language), which has nothing so robust as Python for catching exceptions. In {{{MEL}}}, all you have is the generic {{{catch}}} command, which has the same general effect as calling to {{{except:}}} by itself. As I've leaned, by providing differnt types of exceptions to catch, it allows the user to tailor their code to behave differently when different problems are detected, rather than only being able to detect a simple success\failure state. ---- Also see: *[[Understanding exceptions]] {{{ import os os.makedirs("c:/temp/foo/fooAgain") # This will delete /fooAgain if it is empty, and it will also delete the parent dirs, if they're empty }}} {{{ import shutil shutil.rmtree("c:/temp/foo") }}} {{{lerp}}}: 'linear interpolation': http://en.wikipedia.org/wiki/Lerp_(computing) I can't seem to find this ability as a builtin in Python. How to do? I have this on my [[mel wiki|http://mayamel.tiddlyspot.com/]], but since t his is for Python, I should reproduce here: Here's how it works: {{{val1}}} and {{{val2}}} are the two values. {{{amt}}} is the percentage (from 0-1) between them you want to find the new value. If {{{amt}}} is {{{.5}}}, {{{lerp()}}} will return the value 50% between {{{val1}}} and {{{val2}}}: {{{ def lerp(val1, val2, amt): return val1 + (val2 - val1) * amt print lerp(2, 16, .5) # 9 }}} ---- And if you can lerp in 1d, why not 3d? Next section expands on the concept, but using 3d points rather than single values. {{{p1}}} and {{{p2}}} are the two 3d points in space. {{{amt}}} is the percentage (from 0-1) between them you want to find the new point. If {{{amt}}} is {{{.5}}}, {{{lerp3D()}}} will return a point 50% between {{{p1}}} and {{{p2}}}: {{{ def lerp3D(p1, p2, amt): x = p1[0] + (p2[0] - p1[0]) * amt y = p1[1] + (p2[1] - p1[1]) * amt z = p1[2] + (p2[2] - p1[2]) * amt return [x, y, z] print lerp3d([2,2,2], [10,20,10], .25) # [4.0, 6.5, 4.0] }}} ---- Since color values are also expressed (usually) as three ints, you could use this code to {{{lerp}}} colors too: {{{ print lerp3d([0,0,0], [255, 255, 255], .75) # [191.25, 191.25, 191.25] }}} But, you'd need to convert those to {{{int}}}'s to be usable as color vals. ---- Also see: *[[How can I find the percentage a value is between two other values?]] This only works if the dir is empty: {{{ import os os.rmdir("c:/temp") }}} if you want to remove a directory, and all the stuff in it: {{{ import shutil shutil.rmtree("c:/temp") }}} {{{ import os os.removedirs("c:/temp/foo") }}} {{{ import os os.rename("c:/temp/renameFrom.txt", "c:/temp/renameTo.txt") }}} {{{ import sys for arg in sys.argv: print arg }}} {{{sys.argv}}} is a list of all the arguments passed into the command. The first index 0 is the name of the script itself. For example, you have a script called "c:/tempreturnargs.py" with the above code, and you entered it into the command line with some random arguments, this is the result you'd get: {{{ returnargs.py one two happy bob # --> the code we just entered at the prompt c:/tempreturnargs.py #--> this is sys.argv[0] one #--> this is sys.argv[1] two #--> this is sys.argv[2], etc... happy bob }}}  PIL: *[[Download|http://www.pythonware.com/products/pil/]] *[[Docs|http://www.pythonware.com/library/pil/handbook/index.htm]] The below example illustrates simple image manipulation: Save the given module, and drag images onto its icon: It will save out (in the same dir(s) as the source images) inverted version of them via the {{{Image.point}}} method: {{{ # invertImage.py import os, sys from PIL import Image def invertFunc(pixel): pixel = 255 - pixel return pixel def main(image): f, e = os.path.splitext(image) outfile = "%s_invert.jpg"%f inImg = Image.open(image) outImg = inImg.point(invertFunc) outImg.save(outfile) print "Saved %s"%outfile if __name__ == "__main__": for image in sys.argv[1:]: try: main(image) except Exception, e: print "Failed:", e raw_input("press enter to exit") }}} Update: <<< Recently found this blog post: http://pysnippet.blogspot.com/2010/05/abcs-abstract-base-classes.html That talks about the 'Abstract Base Class' module, which I was unaware of when authoring my system below. But they have similarities. [[docs|http://docs.python.org/library/abc.html]] <<< ---- Interfaces are common in other languages like Java, but (to my knowledge) have no direct comparison in Python (there is a PEP for it [[here|http://www.python.org/dev/peps/pep-0245/]]). Interfaces are like classes that imply define the names of the attributes of what should exist, but don't define what they do. If a class implements an interface, they must implement all of the interfaces methods, etc, or you get failure. It's a way to maintain consistency and polymorphism in OOP. While reading a [[tutorial|http://www.javalobby.org/articles/jython/#Simulating%20interfaces%20with%20multiple%20inheritance]] for [[Jython|http://www.jython.org/]], I found a way to simulate this in Python (modified slightly from source): {{{ class MyInterface(object): def __init__(self): if not hasattr(self, "myMethod"): raise AttributeError("%s must implement myMethod"%self.__class__.__name__) class MyClass(MyInterface): def __init__(self): MyInterface.__init__(self) def myOtherMethod(self): print "This will not fulfill the interface" foo = MyClass() }}} {{{ AttributeError: MyClass must implement myMethod }}} While in Java interfaces are their own data types (like classes), in Python we simulate it by creating a class that when instanced, checks for given methods. If they don't exist, an exception is raised, since the interface wasn't fulfilled. ---- Also see: *[[Abstract Methods]] I've found two main ways to initialize a superclass from a subclass: {{{ class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() }}} and {{{ class Child(SomeBaseClass): def __init__(self): SomeBaseClass.__init__(self) }}} What are the benefits of using either? If using single inheritance, calling directly to the {{{SomeBaseClass.__init__()}}} in the lower example is fine. If you're working with multiple inheritance, the calling to {{{super()}}} is probably a better idea. Why? Check out these notes: *http://stackoverflow.com/questions/222877/how-to-use-super *http://fuhm.net/super-harmful/  !On the mac Has been a nightmare. Lion 10.7.5, and Xcode 4.5.2 !!Numpy: Built source and installed via (all) the instructions here: http://www.scipy.org/Installing_SciPy/Mac_OS_X Amazingly, worked. !!Scipy !!!!FAIL SECTION: {{{$ pip install scipy
}}}
{{{
error: library dfftpack has Fortran sources but no Fortran compiler found
}}}
(and yes, I installed that compiler via the notes on [[this page|http://www.scipy.org/Installing_SciPy/Mac_OS_X]], restarted)
----
Build from source via this link:
http://www.scipy.org/Installing_SciPy/Mac_OS_X
{{{
error: library dfftpack has Fortran sources but no Fortran compiler found
}}}
----
Via the "Scipy Superpack":
http://fonnesbeck.github.com/ScipySuperpack/
This installed Numpy, but scipy failed
!!!!WIN SECTION:
Via the installer {{{scipy-0.11.0-py2.7-python.org-macosx10.6.dmg}}} located in Sourceforge here:
http://sourceforge.net/projects/scipy/files/scipy/0.11.0/
That one worked after I tried all the above stuff, so maybe some of all those other installs helped contribute to it's success.
!!Matplotlib
{{{
pip install matplotlib
}}}
Worked on the first try.  WHAT?!?!

What... a nightmare....
I have Python 2.6 installed on Lion 10.7.5.  On the PC it's a snap to get PIL installed.  But on the mac... I kept getting this thing about missing the {{{_imaging}}} library.  And when I'd download the PIL source and try to build, I'd constantly get something like "gcc4.0 not in path".  Long story short:  All sorts of building magic had to happen, and I could only get it to work via building PIL via [[Macports|http://www.macports.org/]].  Of course, I don't know anything about Macports, so this web post hooked me up:
http://parezcoydigo.wordpress.com/2011/05/31/installing-pil-on-snow-leopard/
Important stuff from it copied here for safety:
----
#1 Re-install libjpeg with Macports and adding the +universal argument to the command:
{{{
$sudo port install jpeg +universal }}} #2 Install PIL with Macports in the same manner: {{{$ sudo port install py26-pil +universal.
}}}
----
Probably took half an hour building all the dependencies on my Macbook Air, but when it was done I coped the data from here:
{{{
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL
}}}
to:
{{{
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL
}}}
And amazingly, it worked.  I'm seriously amazed.  The Mac really sucks for some things....
Taken me a long time to figure out how to install Python packages.  I hear complaints around the community about this as well.
In a nutshell, use {{{pip}}}.  And if that fails, try to "Install from source".  Optionally, on a Mac you can use [[MacPorts|http://www.macports.org/]] for many different packages, but that's not covered here.
!pip
[[pip|http://www.pip-installer.org]] makes it really easy to instal Python packages directly from [[PyPi|http://pypi.python.org/pypi]]

{{{pip}}} is actually a wrapper for {{{setuptools}}} (see below).  You'll need to have that installed first before pip can be installed.  Mac & Linux systems should already have it installed, but Windows people will need to download the latest executable installer from [[here|http://pypi.python.org/pypi/setuptools#files]]

{{{pip}}} installation instructions [[here|http://www.pip-installer.org/en/latest/installing.html]]
<<<
From that doc, on the Mac, in a terminal, just do this:
{{{
curl http://python-distribute.org/distribute_setup.py | python
}}}
I've had instances of that not working, and needed to do this as well:
{{{
$curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py$ sudo python get-pip.py
}}}
Or optionally, I heard you can just do:
{{{
$easy_install pip }}} ---- To install for Linux (Debian): {{{$ sudo apt-get install python-pip
}}}
----
To install for PC:
*From the archive, extract the {{{\pip-1.2.1}}} folder (or whatever version they have in there, it seems several levels deep in the archive) to a temp location.
*Browse to that folder in a shell and install from source:
{{{
\pip-1.2.1> python setup.py install
}}}
<<<
For the 'active' version of Python (what the system thinks the current version is, since you can have multiple versions installed), you can now easily install packages.  For example, if you have Python 2.6 installed, running this code will install the package into that versions {{{/site-packages}}} directory (well, usually that dir).
{{{
$pip install numpy }}} Or if you already have it installed, and want to upgrade it (or if you've installed a new version of Python and want to reinstall previously installed packages in the new Python version): {{{$ pip install numpy --upgrade
}}}
To see all the packages {{{pip}}} has installed, you can do a:
{{{
$pip freeze }}} You can also limit {{{pip}}} to query only a specific version of Python: {{{$ pip-2.7 freeze
}}}
You can use this same syntax when updating\installing packages, for a specific version of Python, rather than the 'current' version.
!Install from source using setup.py
Often times when getting the //source// of a package (from say [[PyPi|http://pypi.python.org/pypi]]), you'll find a {{{setup.py}}} file in the root dir.  What to do?  From the install dir that contains {{{setup.py}}}:
{{{
$python setup.py install }}} And this will work all the magic to install and configure the package. This is just like the PC install example above for {{{pip}}}. {{{setup.py}}} files themselves are covered in the [[distutils docs|http://docs.python.org/distutils/setupscript.html]] !setuptools & easy_install: Really, you should just use {{{pip}}}, which is a wrapper around setuptools (see above). setuptools is the name of the program, and {{{easy_install}}} is the name of the command you execute to do stuff. For the longest time I heard about 'Python Eggs'... and this is where it lead me: *Download and install {{{setuptools}}}: **http://pypi.python.org/pypi/setuptools#downloads **This also installs {{{easy_install}}} in {{{\site-packages}}} *Read the docs! :) **http://peak.telecommunity.com/DevCenter/EasyInstall *Other notes: **On Windows, an {{{easy_install.exe}}} application will be installed in your {{{c:\PythonXX\Scripts}}} dir. Make sure that dir is part of Window's path, so you can execute it. Using {{{easy_install}}} is pretty straightforward (when it works). From a shell: {{{$ easy_install <package name>
}}}
And it will go off and search [[PyPi|http://pypi.python.org/pypi]] for the {{{<package name>}}} and install it.

FYI, {{{.egg}}} files are really just zips, so if you want to look inside, use the unzipper you're most comfortable with.

<<gradient horiz #ddddff  #ffffff  >>This is a [[TiddlyWiki|http://tiddlywiki.com/]].  To really get a grasp on how to navigate it, check out their [[homepage|http://tiddlywiki.com/]].  Quick directions below:
----
''SEARCHING FOR DATA:''
#Browse the "Tags" tab in the //right column// for Python-ish key-words.
**Inside the Tags tab, major ''Categories'' are all in caps, like "[[ENVIRONMENT]]".
**When picking a 'Tag' with more than one link, you can either:
###'{{{Open all}}}' the topics in that Tag (meaning, fully expand all the topics listed in the middle of that pop-up menu).
###Open a single topic by picking its heading.
###Show all the headings in the Tag by picking: {{{Open tag 'tagname}}}'.
#Use your web browsers screen-search ability ('Ctrl+F' in both Firefox & Internet Explorer) to find key-words you're after (good if 'Tags' tab is open).
#Or start browsing from the ''Categories'' section in the //left column//.  This will open each of their major headings in a new tiddler.
If things get too crowded, use the "close all" from the //right column// to clean up the page.  Or, you can use "close others" from an individual tiddler (This block called "[[Instructions For Use]]" is a tiddler, for example).
----
''COPYING DATA FROM WIKI, TO PYTHON:''
*The way the text has been entered into this wiki, copying code from the source-boxes should work:
{{{
source-code in a box;
}}}
*Other times it's not in a box, but is still safe for copy:
{{{source-code not in a box, but still safe to copy;}}}
*If you copy any code outside of a box that's 'not safe', Python //may// have a hard time with it's formatting and be angered. Weird>>
Python has nice indexed\searchable help app (presuming you've [[downloaded\installed|http://www.python.org/doc/]] Python's current documentation).  Here is an example of it's save location:
{{{
C:\Python25\Doc\Python25.chm
}}}
I have a shortcut to that on my desktop.
----
You can also launch {{{pydocgui.pyw}}} from the system:
{{{
C:\Python<version>\Tools\Scripts\pydocgui.pyw
}}}
This will open a nice graphical user interface that lets the user search for functions, classes, etc.  And, it will open a web-browser with the results.  If you want to launch this from your IDE, you can just {{{import pydoc}}}:
{{{
import pydoc
pydoc.gui()
}}}
Print all the Python keywords:
{{{
import keyword
for kw in keyword.kwlist:
print kw
}}}
Also includes a handy {{{iskeyword()}}} function.
http://docs.python.org/library/keyword.html
To download \ install the Kivy app, you can do that directly from their web page here:  http://kivy.org/#download
This will allow you to run a kivy Python file from the command line like so:
{{{
$kivy myKivyApp.py }}} But to actually get the source installed, so you can use the libraries in your own IDE, it requires more hoop jumping: It requires [[Cython|http://cython.org/]] however, so you'll need to make sure that's installed first. To do this, I used [[pip|Installing Python Packages]]: {{{$ pip install cython
}}}

After unzipping it, browse to the folder and install from source:
{{{
$python setup.py install }}} That was about it. I can now: {{{$ python
>>> import kivy
[INFO   ] Kivy v1.5.2-dev
}}}

On the Mac, it installed the library here:
{{{
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/kivy
}}}


Rather than just retype it all, might as well use the actual docs ;)
http://docs.python.org/lib/typesseq-mutable.html 'Mutable Sequence Types'
and
http://docs.python.org/tut/node7.html 'Data Structures'
''Hilights'':
*List methods (also see [[List methods]])
*Lists as Stacks
*Using Lists as Queues
*Functional Programming Tools:
**Filter
**Map
**Reduce
*List Comprehensions
*The del statement
*Tuples and Sequences
*Sets
*Dictionaries
*Looping Techniques
*Comparing Sequences and Other Types
''Tips & Tricks:''
----
How can I remove duplicates from a list?
<<tiddler [[How can I remove duplicates from a list?]]>>
----
If you want to make a ''copy of a list'', this is how you //shouldn't// do it:
{{{
listA = ["a","b","c"]
listB = listA
if listA is listB:
print "true!"
# true!
}}}
In this case, the variable name {{{listA}}} and {{{listB}}} are both poting to the exact same place in memory, which holds the data {{{["a","b","c"]}}}.  This is proven by using the {{{is}}} test in the above example.
To properly make a copy, you can grab a slice, which happens to be the whole list:
{{{
listA = ["a","b","c"]
listB = listA[:]
}}}
If you want to make a //reversed// copy:
{{{
listB = listA[::-1]
}}}
----
{{{
a = [1, 2, 3]
b = [4, 5, 6]
}}}
{{{
c = a + b
print c
# [1, 2, 3, 4, 5, 6]
}}}
Zip lists (returns list of tuples):
{{{
d = zip(a,b)
print d
[(1, 4), (2, 5), (3, 6)]
}}}
Map lists (using lambda).
Will multiply each element of list a against list b:
{{{
e = map(lambda x, y: x*y, a,b)
print e
# [4, 10, 18]
}}}
----
Full copy:
{{{
copy.deepcopy(X)
list(L)
}}}
Insert items at front of list L:
{{{
L[:0] = [X,Y,Z,]
}}}
Insert multiple items at the end of list L, in-place:
{{{
L[len(L):] = [X,Y,Z]
L.extend([X,Y,Z]
L += [ X,Y,Z]
}}}
Implement in-place stack operations, where the end of the list is the top of the stack:
{{{
L.append(X)
X=L.pop()
}}}
Indexing:
{{{
# 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[:]
}}}

http://docs.python.org/tut/node7.html
{{{
>>> listFoo = ["this", "is", "a", "list"]
>>> for thing in dir(listFoo):
>>>     print thing
append
count
extend
index
insert
pop
remove
reverse
sort
}}}
{{{
\a # Sounds system chime?
\t # Horizontal Tab
\v # Vertical Tab
\f # Formfeed
\n # Linefeed
\r # Carriage Return
\b # Backspace
\\ # Backslash.  Prints a single backslash
\' \" # prints quotes
}}}
There are more than below, run '{{{help(os.path)}}}' to see the whole list:
{{{
import os

os.path.abspath # converts '/' paths to '\' paths
os.path.basename # returns just the filename of a filepath
os.path.dirname # list just the directory component of a filepath
os.path.exists # returns whether a file or path exists
os.path.getsize # returs the KB size of a file
os.path.isdir # returns if the argument is a path
os.path.isfile #  returns if the argument is a file
os.path.normcase # makes all text lowercase, and all paths backslashes
os.path.normpath # makes all paths backslashes.
os.path.split  #  split a pathname into path, file
os.path.splitdrive # split pathname into drive, path
os.path.splitext # split the pathname into path, .extension
os.path.walk # walk the directory tree and call functions
# also check out os.walk
}}}
/***
|''Description:''|When the TiddlyWiki file is located on the web (view over http) the content of [[SiteProxy]] tiddler is added in front of the file url. If [[SiteProxy]] does not exist "/proxy/" is added. |
|''Version:''|1.1.0|
|''Date:''|mar 17, 2007|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''~CoreVersion:''|2.2.0|
***/
//{{{
major: 1, minor: 1, revision: 0,
date: new Date("mar 17, 2007"),

if (!window.bidix) window.bidix = {}; // bidix namespace
if (!bidix.core) bidix.core = {};

{
if ((document.location.toString().substr(0,4) == "http") && (url.substr(0,4) == "http")){
url = store.getTiddlerText("SiteProxy", "/proxy/") + url;
}
}
//}}}

Every so often, I'll get an [[exception|Understanding exceptions]] like this:
{{{
UnboundLocalError: local variable 'myVar' referenced before assignment
}}}
Here's a test case explaining the issue:
----
This this sample module {{{test.py}}}, there is a //global variable// named {{{shoe}}}.  Inside the {{{main()}}} function, the function {{{stuff()}}} takes in the global variable and returns a new value.  ''All is well:''
{{{
# test.py

# global var:
shoe = 23

def stuff(n):
return n * 2

def main():
# global var is passed into function:
var = stuff(shoe)
print var

if __name__ == "__main__":
main()
}}}
It prints
{{{
46
}}}
The below modification ''will fail:''
{{{
# test.py

shoe = 23

def stuff(n):
return n * 2

def main():
var = stuff(shoe)
print var
# note the assignment to shoe, making it *local*
shoe = 32

if __name__ == "__main__":
main()
}}}
When executed:
{{{
UnboundLocalError: local variable 'shoe' referenced before assignment
}}}
''What's going on?''
Python realizes that {{{shoe}}} has had an assignment made to it inside the {{{main()}}} function, thus making the declaration there //local to main()// ({{{main()}}} no longer pays any attention to the //global// {{{shoe}}}).  And since this happened //after// {{{stuff()}}} consumed it, it appears to be an order of operation failure.  A few solutions (trimming off the unmodified code):
!!!Pass in shoe:
{{{
# modify main as such:
def main(shoe=shoe):
}}}
!!!Put assignment //first//:
{{{
def main():
shoe = 32
}}}
!!!Make a function to explicitly modify the //global var//, and call to it:
{{{
def modShoe():
return shoe * 2

def main():
var = modShoe()
}}}


Found here: Found here: http://nodebox.net/code/index.php/Math
{{{
from math import radians, sin, cos
def coordinates(x0, y0, distance, angle):
x1 = x0 + cos(radians(angle)) * distance
y1 = y0 + sin(radians(angle)) * distance
return x1, y1
}}}
See the Python reference here:
http://docs.python.org/tut/node7.html#SECTION007600000000000000000
Topics:
*Looping through dictionaries ({{{dict.iteritems}}}).
*Looping through a sequence, and having access to the current index and value ({{{enumerate}}}).
*Loop over two or more sequences at the same time ({{{zip}}}).
*Loop over a sequence in reverse ({{{reversed}}}).
*Loop over a sequence in sorted order ({{{sorted}}}).
Easier than just retyping it all ;)
----
Also see:
*[[How can I loop through multiple lists?]]
<<gradient horiz  #ddddff  #ffffff  >>
[[Welcome]]
[[Instructions for use]]
----
''Subscribe'':
----
[[All Subjects]]
----
''Categories:''
[[DICTIONARY]]
[[ENVIRONMENT]]
[[EXECUTION]]
[[EXTERNAL APPS]]
[[FILESYSTEMS]]
[[FUNDAMENTALS]]
[[INFO]]
[[I/O]]
[[KIVY]]
[[LIST]]
[[MATH]]
[[MATPLOTLIB]]
[[MEDIA]]
[[NETWORK]]
[[NUMBERS]]
[[NUMPY]]
[[OOP]]
[[PIL]]
[[P4]]
[[PYGAME]]
[[RASPBERRY PI]]
[[SCIPY]]
[[STRING]]
[[TROUBLESHOOTING]]
[[UI]]
[[VARIABLES]]
[[WEB]]
[[XML]]

http://effbot.org/tkinterbook/bitmapimage.htm
From that page:
<<<
An X11 bitmap image consists of a C fragment that defines a width, a height, and a data array containing the bitmap. To embed a bitmap in a Python program, you can put it inside a triple-quoted string:
{{{
BITMAP = """
#define im_width 32
#define im_height 32
static char im_bits[] = {
0xaf,0x6d,0xeb,0xd6,0x55,0xdb,0xb6,0x2f,
0xaf,0xaa,0x6a,0x6d,0x55,0x7b,0xd7,0x1b,
0x69,0x4b,0x3f,0xb4,0x9e,0x92,0xb5,0xed,
0xd5,0xca,0x9c,0xb4,0x5a,0xa1,0x2a,0x6d,
0x2c,0xa5,0x2a,0xd3,0x9a,0x8a,0x4f,0xfd,
0x2c,0x25,0x4a,0x6b,0x4d,0x45,0x9f,0xba,
0x1a,0xaa,0x7a,0xb5,0xaa,0x44,0x6b,0x5b,
0x1a,0x55,0xfd,0x5e,0x4e,0xa2,0x6b,0x59,
0x9a,0xa4,0xde,0x4a,0x4a,0xd2,0xf5,0xaa
};
"""
}}}
To create X11 bitmaps, you can use the X11 bitmap editor provided with most Unix systems, or draw your image in some other drawing program and convert it to a bitmap using e.g. the [[Python Imaging Library|http://www.pythonware.com/products/pil/]].
<<<
^^Note:  They list width and height to both be 32 in that example,but based on the values it would be 8x16.  Not sure if that's a typo...^^
Those bits are stored as a hexadecimal string.  If you turn the C im_bits array into a Python list and print, the string representation will be the integer value.
----
To do the conversion via PIL:
*http://www.pythonware.com/library/pil/handbook/image.htm
*Check out the {{{tobitmap()}}} function.
{{{
from PIL import Image
im = Image.open("myImage.jpg")
bitmap = im.tobitmap()
}}}
----
Here is a Python function that will create a X11 bitmap string from the given args.
{{{
def makeX11(width, height, bits):
"""
width : int : bitmap width.
height : int : bitmap height.
bits : list : List of int values.  len(bits) must equal width * height.
"""
assert (width * height) == len(bits), "Width (%s) * height (%s) != len of bits (%s)"%(width, height, len(bits))
bitmapData = []
bitmapData.append("#define im_width %s"%width)
bitmapData.append("#define im_height %s"%height)
bitmapData.append("static char im_bits[] = {")
for h in range(height):
hexRow = [hex(bit) for bit in bits[h*width:h*width+width]]
bitmapData.append(', '.join(hexRow))
bitmapData.append("};")
return bitmapData
}}}
As an example, I converted the above hexadecimal values into ints:
{{{
width = 8
height = 16
bits = [175, 109, 235, 214, 85, 219, 182, 47,
175, 170, 106, 109, 85, 123, 215, 27,
173, 214, 181, 174, 173, 85, 111, 5,
173, 186, 171, 214, 170, 213, 95, 147,
173, 118, 125, 103, 90, 213, 215, 163,
173, 189, 254, 234, 90, 171, 105, 179,
173, 85, 222, 216, 46, 43, 181, 106,
105, 75, 63, 180, 158, 146, 181, 237,
213, 202, 156, 180, 90, 161, 42, 109,
173, 108, 95, 218, 44, 145, 187, 246,
173, 170, 150, 170, 90, 202, 157, 254,
44, 165, 42, 211, 154, 138, 79, 253,
44, 37, 74, 107, 77, 69, 159, 186,
26, 170, 122, 181, 170, 68, 107, 91,
26, 85, 253, 94, 78, 162, 107, 89,
154, 164, 222, 74, 74, 210, 245, 170]

bitmapData = makeX11(width, height, bits)
for bd in bitmapData:
print bd
}}}
prints the //exact// {{{BITMAP}}} string from above.
*http://www.doughellmann.com/PyMOTW/math/
**Great overview of the {{{math}}} module in the standard library
*http://inventwithpython.com/blog/2012/07/18/using-trigonometry-to-animate-bounces-draw-clocks-and-point-cannons-at-a-target/
**Great visual overview for using trigonometry to animate stuff using Pygame.
*[[Autodesk University: ICE Design Tools|http://area.autodesk.com/userdata/blogs/marks/ToddAkita_ICE_Design_Tools.pdf]] (pdf) - This is for Softimage's ICE, but the reference it has for trig and vectors is great.
I recently found [[these sites:
that gives me the ascii codes so I can embed them in the wiki... so let's get started...
----
!!!Delta (upper case)
&#8710;  [[Change|http://en.wikipedia.org/wiki/Delta_(letter)#Upper_case]]
!!!Delta (lower case)
&#948;
!!!Sigma (upper-case)
&#8721; [[Summation|http://en.wikipedia.org/wiki/Summation]]

[img[http://upload.wikimedia.org/math/1/a/6/1a693bfe9a392b08ed135a9e4117d3b9.png]] {{{sum([i**2 for i in range(3,7)])}}}
!!!Sigma (lower-case)
&#963;
http://matplotlib.org/

The bulk of these notes are pulled from [[Programming Computer Vision With Python|http://shop.oreilly.com/product/0636920022923.do]]
The ~PyLab interface is the set of functions that allow the user to create plots.  {{{pylab}}} is actually a module in the {{{matplotlib}}} package ({{{matplotlib.pylab}}}), however, it also gets its own module external to that packaged that lives at the root of the site-packages dir you can import directly.  It only has three lines:
{{{
from matplotlib.pylab import *
import matplotlib.pylab
__doc__ = matplotlib.pylab.__doc__
}}}
Matplotlib can use PIL Image objects directly, the below examples make use of them.
It should also be noted that the ~PyLab {{{array}}} type is actually borrowed from Numpy.
----
For the below examples:
{{{
from PIL import Image
from pylab import *
}}}
----
Plot an image:
The {{{show()}}} command starts the figure GUI and raises the figure windows.  Call {{{show()}}} only once per script, usually at the end.
{{{
im = array(Image.open('spam.jpg'))
imshow(im)
show()
}}}
----
Don't use colors:
{{{
gray()
}}}
----
Create a new window (figure):
{{{
figure()
}}}
Calling this to multiple times in a module will set this as the active window, and display it when {{{show()}}} is called.
----
Turn off axes:
{{{
axis('off')
}}}
----
Plot lines and points:
{{{
plot(x,y,'color & style')
}}}
'Color & style is a special string combos that are combined:
*Colors : 'r' = red, 'g' = green, 'b' = blue, 'c' = cyan, 'm' = magenta, 'y' = yellow, 'k' = black, 'w' = white.
*Styles : ' - ' : solid, ' - - ' : dashed, ' : ' : dotted.
*Marker : ' . ' : point, ' o ' : circle, ' s ' : square, ' * ' : star, ' + ' : plus, ' x ' : x.
Examples:
*'r*' : red star markers.
*'go-' : green line with circle markers.
*'ks:' : black dotten line with square markers.
----
Display a histogram for an image:
{{{
# Open an image, convert to gray-scale:
im = array(Image.open('spam.jpg').convert('L'))
figure()
gray()
hist(im.flatten(), 128)
show()
}}}
----
Display image contours (edge-detection):
{{{
# Open an image, convert to gray-scale:
im = array(Image.open('spam.jpg').convert('L'))
figure()
gray()
contour(im, origin='image')
show()
}}}
----
Get three interactively-picked points
{{{
points = ginput(3)
}}}
The user can then pick three points in the current image, points will be a list of tuples of two (x,y) values.
----

Thought I'd try my hand at a maze solver.  It works, but doesn't tell you the path it took, just that it solved it.  It's.. a start ;)
{{{
maze =\
"""
.X..X.
.XX...
....XX
XXX...
X...X.
O.XX..
"""
# This creates entries in y,x order:
mazer = [list(row) for row in maze.split()]

def check(position):
if position[0] < 0 or position[1] < 0:
return False
try:
val = mazer[position[0]][position[1]]
except IndexError:
return False
if val == 'O':
return "cheese"
elif val == 'X':
return False
else:
return True

def solve(maze, start = [0,0]):
"""
maze : string : Any multiline string.  Empty spaces are marked with period '.'.
Walls are marked with capital X.  The exit is marked with capital O.
start : The x,y] location to start the maze at.
return : The x,y coordinate of the exit, or False if the puzzle can't be solved.
"""
# flip to y,x for internal processing.
start = [start[1], start[0]]
north,south,east,west = [-1,0], [1,0], [0,1], [0,-1]

cheese = False
visited = []
positions = [start]

for p in positions:
if cheese:
break
visited.append(p)
for direction in [north,south,east,west]:
location = [p[0]+direction[0], p[1]+direction[1]]
if location in visited:
continue
result = check(location)
if result == 'cheese':
cheese = location
elif result:
positions.append(location)

# Flip y,x to x,y:
return cheese[1], cheese[0]

print solve(maze)
}}}
{{{
(0, 5)
}}}
Methods are [[Function]]s that are called using the [[attribute]] notation. There are two flavors: built-in methods (such as .append() on lists) and class instance methods. Built-in methods are described with the types that support them.  You can create methods in your [[Class]] objects.
When you inherit a class and it's method, it's possible to additionally either override an inherited method, or modify an inherited method.  To //override// a method, you simply redefine the method in your new derived class.  But to modify an inherited method, you need to use the {{{super}}} function.  Based on the [[Class Inheritance]] example:
{{{
# make a new derived class, that modifies Foo's __init__ method (now accepts 'newVal') without overriding it:
class Fooie(Foo):
def __init__(self, val, newVal):
super(Fooie, self).__init__(val)
self.newVal = newVal
}}}
{{{super}}} lets you invoke the method of a base class (also called a //superclass//), and pass its values to the derived class.  So in th above example, {{{super}}} is getting the {{{val}}} data from the 'base class'\superclass {{{Foo}}}, and assigning it to the derived class {{{Fooie}}}'s inherited {{{val}}} attribute.  {{{super}}} was introduced in Python 2.2, and only works with 'new style classes':  Meaning, classes defined as {{{class Foo(object):}}} rather than {{{class Foo:}}}.
Blog post showing how to use {{{PyWin32}}} to monkey with Microsoft Office:
http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/
So it is known you can make multiline strings like so:
{{{
s = """
lineA
lineB
lineC
"""
}}}
However, this will cause an extra line to be printed before, and after the multi-line block:
{{{
print "above"
print s
print "below"
}}}
{{{
above

lineA
lineB
lineC

below
}}}
To get around this, you can use a line escape characters to the mix:
{{{
s = """\
lineA
lineB
lineC\
"""
print "above"
print s
print "below"
}}}
{{{
above
lineA
lineB
lineC
below
}}}
''Mutable'' objects can be changed in-place (usually via their [[Method]]s). ''Immutable'' objects can't.  Here's //the list// (hope I got it right...):
*''Mutable''
**list
**dictionary
*''Immutable''
**tupple
**string
**numbers (int, long, bool, float, complex, set, decima, etc...)
See Python docs on [[Mutable Sequence Types|http://docs.python.org/lib/typesseq-mutable.html]]

The [[PEP 8 Style Guide|http://python.org/dev/peps/pep-0008/]], under ' {{{Prescriptive: Naming Conventions}}} ' (search for it on that page) has many good naming conventions.  No point to retype it all here, go there to see what's going on.  But, here's a handy chart:
| Name Type | only lowercase | mixedCase | only uppercase | underscores (inside the name) | notes | example |
| | | | | | | |
| [[package|Packages]] | X | | | discouraged | short names | {{{spam}}} |
| [[module]] | X | | | if it improves readability | short names | {{{spam}}} \ {{{spam_eggs}}} |
| [[class|Class]] | | X | | | Classes always //start capitalized// (using the ~CapWords\~CamelCase convention) | {{{Spam}}} \ {{{SpamEggs}}} |
| [[function|Function]] or [[method|Class Methods]] | should be | only if it's the prevailing style | | as necessary to improve readability | | {{{spam}}} \  {{{spam_eggs}}} \ {{{spamEggs}}}  |
| global variable | should be | only if it's the prevailing style | | as necessary to improve readability | | {{{spam}}} \ {{{spam_eggs}}} \ {{{spamEggs}}} |
| constants | | | X | X | | {{{SPAM}}} \ {{{SPAM_EGGS}}} |
Many of these can be modified with 'special underscores', please see the section below:
!Underscores
Per the //Naming Convention// section of the [[PEP8 Style Guide|http://www.python.org/dev/peps/pep-0008/]] under ' {{{Descriptive: Naming Styles
}}} '
**weak "internal use" indicator.  E.g. "{{{from M import *}}}" does not import objects whose name starts with an underscore.
*{{{single_trailing_underscore_}}}
**used by convention to avoid conflicts with Python keyword, e.g. {{{Tkinter.Toplevel(master, class_='ClassName')}}}
**when naming a [[class|Class]] attribute, invokes name mangling (inside {{{class FooBar}}}, {{{__boo}}} becomes {{{_FooBar__boo}}}).  See [[Private Attributes]] & [[Private Methods]]
**"magic" objects or [[attribute]]s that live in user-controlled namespaces.  E.g. {{{__init__}}}, {{{__import__}}} or {{{__file__}}}.  Never invent such names; only use them as documented.   See [[Special Methods]], [[Special Attributes]]
!'Dots' in names:
This was sort of a 'oh, durrr...' moment on my part:
Don't name anything in Python with a 'dot\period' in their names:  Modules, functions, classes, attributes.... anything.  Python will raise a {{{SyntaxError}}}.  This is because the 'dot' refers to an [[attribute]], not some sub-part of the name.
This may seem fairly obvious, but I had named a module, and the dir it was in like such, to track versioning:
{{{
c:\pyStuff\proj.0.2.1\proj.0.2.1.py
}}}
//Executing// the code from the command-line\icon worked just fine.  But if you try to import that into another module, it just won't work.  To make it work if you really want to use the versioning, use underscores:
{{{
c:\pyStuff\proj_0_2_1\proj_0_2_1.py
}}}
Live and learn ;)
The bulk of these notes are pulled from [[Programming Computer Vision With Python|http://shop.oreilly.com/product/0636920022923.do]]
----
Docs, etc:
http://numpy.scipy.org/
http://docs.scipy.org/doc/numpy/reference/
----
!!!Numpy Arrays:
http://docs.scipy.org/doc/numpy/reference/arrays.html
{{{
# Example opening a PIL Image into an array:
from PIL import Image
from numpy import array
im = array(Image.open(myImage))
}}}
*Multi-dimensional, can represent vectors, matrices, and images.
*Can perform matrix multiplication, transposition, vector math.
*Array's are like lists of lists, but is restricted to having all elements of the same type.
It should be noted Matlotlib's {{{pylab}}} interface has calls to the same {{{array}}} types.
!!!!Attribues:
*shape : (800, 600, 3) : An image's width, height, and number of channels.  (800, 600) : Images width & height: No channel info, must be grayscale.
*dtype : The data type of the array elements.  Images are usually encoded with unsigned 8-bit integers ({{{uint8}}}).  Grayscale images are {{{float32}}}.
!!!!Element Access:
Via indices.
{{{
Get the value at coord x,y (row, column), color c:
value = im[x,y,c]
}}}
Arrays support full slicing support, with querying and assignment.
!!!!Image operations:
{{{
# Open image, convert to grayscale, stick in array:
im = array(Image.open(myImage).convert('L'))
}}}
Invert an image:
{{{
inverted = 255 - im
}}}
Clamp intensities to between values 100->200
{{{
clamp = (100.0/255) * im + 100
}}}
Apply a quadratic function lowering the values of the darker pixels:
{{{
}}}
Print the min and max values:
{{{
print int(im.min()), int(im.max())
}}}
Convert an array back to a PIL Image type:
{{{
pil_im = Image.fromarray(im)
#If the image was changed to a different format:
from numpy import uint8
pil_im = Image.fromarray(uint8(im))
}}}
Nice overview on [[effbot.org|http://effbot.org/zone/python-objects.htm]]
----
All objects have three main data-points that describe them:
*identity:  Its location in memory, accessed via {{{id()}}}.
*type: The representation of the object to Python, accessed via {{{type()}}}.
*value:  The data (attributes) stored inside, accessed via {{{dir()}}}.
{{{
# Make a 'string' object assigned to the 'spam' variable:
spam = "eggs"
print "spam id:", id(spam)
print "spam type:", type(spam)
print "spam attributes:"
for d in dir(spam):
print "\t",d
}}}
Prints:
{{{
spam id: 50854480
spam type: <type 'str'>
spam attributes:
__class__
__contains__
__delattr__
#... many, many more....
}}}
Main page:
http://opencv.willowgarage.com/wiki/

Install blog:
http://luugiathuy.com/2011/02/setup-opencv-for-python/

*http://sourceforge.net/projects/opencvlibrary/
*http://new.scipy.org/download.html - need to download both numpy and scipy
From [[this blog post|http://tartley.com/?p=1312]]

*[[Learning Modern 3D Graphics Programming Through OpenGL|http://www.arcsynthesis.org/gltut/index.html]] (C)
*[[An intro to modern OpenGL|http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html]] (C)
*[[Learning WebGL|http://learningwebgl.com/blog/?category_name=lessons]] (Javascript)
And, there is the first link slowly being ported to Python:
*[[https://bitbucket.org/tartley/gltutpy]]
Common class methods.  See Python docs here:
http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes
----
{{{__init__}}}
http://docs.python.org/reference/datamodel.html#object.__init__
Called when the instance is created.  Probably the most popular type of operator overloader, but not required.  Makes it easy to setup default states for your object, and can receive passed in arguments.  See examples below.
----
{{{__call__}}}
http://docs.python.org/reference/datamodel.html#object.__call__
Allows an object to be called like a function.
----
{{{__str__}}}
http://docs.python.org/reference/datamodel.html#object.__str__
From docs:
<<<
Called by the {{{str()}}} built-in function and by the {{{print}}} statement to compute the “informal” string representation of an object. This differs from {{{__repr__()}}} in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object.
<<<
----
{{{__repr__}}}
I've heard these called 'reproduction strings'.
http://docs.python.org/reference/datamodel.html#object.__repr__
From docs:
<<<
Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object.
<<<
----
{{{__del__}}}
http://docs.python.org/reference/datamodel.html#object.__del__
From docs:
<<<
Called when the instance is about to be destroyed. This is also called a destructor.
<<<
----
{{{__nonzero__}}}
http://docs.python.org/reference/datamodel.html#object.__nonzero__
<<<
Called to implement truth value testing and the built-in operation bool(); should return False or True, or their integer equivalents 0 or 1.
<<<
See: [[How can I do truth value testing on my object?]]
----
Example:
{{{
class MyObj(object):

# Executed to when an object is first instanced

def __call__(self, word):
# Exected to when an object is called to.
return self.value + " " + word

def __str__(self):
# Executed when an object is 'printed'
return "CallMe"

def __repr__(self):
# Executed when called to by repr(), or with
# back-quotes:
return "<MyObj('"+self.value+"')>"

def __del__(self):
# Executed during Python garbage cleanup (when
# the object is being removed from memory)
print "Ack, " + self.__repr__() + " is dying!"

# Executes __init__
caller = MyObj("Hello?")

# Executes __call__
val = caller("Are you there?")
print val
# Hello? Are you there?

# Executes __str__
print caller
# CallMe

# Executes __repr__
print caller
# <MyObj('Hello?')>

# Executes __del__, since caller is being re-defined
caller = None
# Ack, CallMe('Hello?') is dying!
}}}
----

Also see:
*[[Special Methods]]
*[[How can I have objects add themselves together?  Subtract one another, etc?]]
The bulk of these notes are pulled from [[Programming Computer Vision With Python|http://shop.oreilly.com/product/0636920022923.do]]
PIL documentation:
*http://www.pythonware.com/products/pil/
*http://www.pythonware.com/library/pil/handbook/index.htm
----
The below examples wil need this as well:
{{{
from PIL import Image
}}}
Open an image
{{{
pim = Image.open('myImage.jpg')
}}}
Show an image in the default viewer:
{{{
pim.show()
}}}
Open an image and convert it to grayscale:
I should note, when I do this, it seems to convert the images to "rainbow"... hmm...
{{{
gray = pim.convert('L')
}}}
Save an image in a different format:
{{{
pim.save("myImage.tga")
}}}
Create a thumbnail:
{{{
thumb = pim.thumbnail((128,128))
}}}
Crop an image:
{{{
# (left, upper, right, lower)
box = (25, 25, 300, 300)
cropped = pim.crop(box)
}}}
Rotate a cropped region and paste:
{{{
cropped.transpose(Image.ROTATE_180)
pim.paste(cropped, box)
}}}
Resize an image:
{{{
small = pim.resize(64,64)
}}}
Rotate an image:
{{{
# Positive values are counter-clockwise:
rotated = pim.rotate(74.254)
}}}
When authoring [[Python's path|How can I set Python's path?]], another (and recomended) way of accessing your code is through //packages//.
Official docs:
http://docs.python.org/tutorial/modules.html#packages
Supporting module {{{pkgutil}}}:
http://docs.python.org/library/pkgutil.html

In a nutshell, packages allow you to access code in subdirectories without having to actually define all of those subdirs as part of your path.  All you have to do is two things:
#Make sure the //parental// dir is in the path.
#In each dir that is part of the package, add an {{{__init__.py}}} module.  It can be an empty module (but check the docs for useful things you //can// put in there).
Example:  Presume this is your dir structure:
*\root
**\pylib
***\files
***\rigging
***\images
{{{\root}}} is in your path (and it does //not// have an {{{__init__.py}}} module).  All its subdirs have an {{{__init__.py}}} file saved in them.  Each of the child dirs also has a suite of python modules.  In your code, you could then access the modules through 'dot notation':
{{{
import pylib.files.moduleA
from pylib.rigging import moduleB
# then execute:
pylib.files.moduleA(someArg)
moduleB(someArg)
}}}
This is just a quick overview, check the above linked docs for more.
----
''Relative Imports''
You can do relative imports with packages.
Here are [[docs|http://docs.python.org/tutorial/modules.html#intra-package-references]]
Using paths from the above example:
{{{
# Where this module is saved:
# pylib.rigging.rig.py
# or could be thought of as:
# /root/pylib/rigging/rig.py

# These two are the same, and they're looking for a module called
# someModule.py in /rigging (same dir as this module)
import someModule
from . import someModule

# This looks a dir up in /pylib for someOtherModule.py
from .. import someOtherModule
}}}


http://code.google.com/p/py-lepton/
Good overview in the June 2009 issue of [[Python Magazine|http://pymag.phparch.com/]]
From the page:
<<<
Lepton is designed to make complex and beautiful particle effects possible, and even easy from Python programs.
Lepton provides the following core features:
*Native-code core for high-performance particle dynamics and rendering
*Pluggable particle controllers for specifying particle behavior
*Two pluggable ~OpenGL renderers, and two pygame renderers
*Spacial domains, used to control particle emission and behavior
*Easy to use and powerful texture support, including animation
*Modular architecture that lets you easily configure and customize the engine
<<<
----
Reference:
*http://www.particlesystems.org/
*http://www.opengl.org/
http://www.perforce.com/
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?
----
They release their installers by version, by os:
Windows Installer (Python 2.5):
*ftp://ftp.perforce.com/perforce/r07.3/bin.ntx86/p4python25.exe
Source:
*ftp://ftp.perforce.com/perforce/r07.3/tools/p4python.tgz
*ftp://ftp.perforce.com/perforce/
Documentation:
*Main Page:  http://www.perforce.com/perforce/technical.html
*Python specific HTML: http://www.perforce.com/perforce/doc.current/manuals/p4script/03_python.html#1116373
*Multi-language .pdf:  http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf
Informative Blog Posts:
*[[Perforce Triggers in Python (part 1)|http://www.chrisevans3d.com/pub_blog/?p=55]]
*[[Perforce Triggers in Python (part 2)|http://www.chrisevans3d.com/pub_blog/?p=571]]
*[[Writing Custom Perforce Plugins in Python|http://www.chrisevans3d.com/pub_blog/?p=584]]
*[[Perforce Python API Basics|http://techarttiki.blogspot.com/2012/03/perforce-python-api-basics.html]]
The install appears to stick the modules here:
{{{
C:\Python25\Lib\site-packages\

P4.py
p4.pyc
p4.pyo
P4API.pyd
P4Python-2007.3-py2.5.egg-info
}}}
Some example code modified from their manual (they had... mistakes...)
{{{
from P4 import P4, P4Exception

p4 = P4()
p4.port = "myPort:1666"
p4.user = "myName"
p4.client = "myClient"
try:
p4.connect()
info = p4.run("info") # returns a list
d = info[0]  # extract the dictionary
for key in d.keys():   # print our 'info'
print key + "  :  " + str(d[key])
p4.disconnect()
except P4Exception:
for e in p4.errors:
print e
}}}
----
''Notes:''
*It appears that most of P4's commands are wrappered in methods that start with {{{run_}}}, or can be called to via the {{{run()}}} method (as in the example above):
{{{
info = p4.run("info")
info = p4.run_info()
# Both appear to do the same thing.
}}}
{{{
# Both do the same thing, but sometimes run takes extra args:
files = p4.run_edit([r"c:\myFile.txt", r"c:\myFileB.txt])
files = p4.run("edit", [r"c:\myFile.txt", r"c:\myFileB.txt])
}}}
*I've also ran into issues that some commands run much faster than others based on the {{{run()}}} syntax.
*For example, {{{p4.run('print')}}} executes //much faster// than {{{p4.run_print()}}}.


While not specifically Python related, this post does a great job of covering concepts and pseudo-code (Python-ish) behind it:
http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
And this Pygame app uses those concepts in Python:
http://www.pygame.org/project/2446/?release_id=4110
Great page covering many physics topics from cloth simulation to inverse kinematics:
http://freespace.virgin.net/hugo.elias/models/m_main.htm
While not directly Python related, much of the pseudo-code supplied is easily transferrable.
Not Python related persay, but nice selection of papers here:
http://algorithmicbotany.org/papers/
In Python, it's the concept of being able to treat different types of things in the same way.  Given an example:
{{{
print len("foo foo foo")
# 11
print len((4,6,32,61))
# 4
print len(["foo", "goo", "shoe"])
# 3
}}}
...you could say the {{{len}}} function is polymorphic, since it can deal with different data types like strings, tupples, or lists in the same way.
You'd think updating a pre-existing xml file with a new element and saving it would be an easy thing, right?  Wrong.  Well, unless I'm really missing something.  It appears there's a major issue with both {{{ElementTree}}} and {{{minidom}}}:  When you add a new element into a tree, it will mangle the human-readable text formatting after the file is printed\saved.  While it's still a valid xml file, tags are on the wrong lines, and indentation will be off.

After a lot of troubleshooting and hunting the web, I finally figured out what was going on, and have a solution below.  I've talked with a lot of people, and no one has had a better solution... but I'm surprised it's really this difficult to do it.

Below are presented two solutions, one for {{{ElementTree}}}, and one for {{{minidom}}} ({{{minidom}}} solution still doesn't solve //all// the issues).  In both examples, they update the xml file with a new element called {{{<test>}}}, with the text {{{testText}}}, as a child of the element {{{<firstTagElement>}}}.  I'll list code that you'd THINK would work, and the resultant file.  Then I"ll show the code that you need to make it work, and the resulatant file:

''Source File'':
{{{c:/temp/foo.xml}}}
{{{
<?xml version='1.0' encoding='UTF-8'?>
<root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
</firstTagElement>
</root>
}}}
!minidom
''Bad Code:'' (Code you'd think would work, but doesn't)
{{{
import xml.dom.minidom
doc = r"c:/temp/foo.xml"
mDom= xml.dom.minidom.parse(doc)

for i in mDom.getElementsByTagName("firstTagElement"):
test = mDom.createElement("test")
tText = mDom.createTextNode("testText")
test.appendChild(tText)
i.appendChild(test)
print mDom.toxml("UTF-8")
}}}
The new file it prints is:
{{{
<?xml version="1.0" encoding="UTF-8"?><root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
<test>testText</test></firstTagElement>
</root>
}}}
Notice:
*The first {{{<root>}}} tag is on the wrong line
*The {{{<test>}}} tag has the wrong indentation level
*The closing {{{</firstTagElement>}}} is on the wrong line
----
''Good code:'' (well, sort of, it still has one bug, see below)
{{{
import xml.dom.minidom
doc = r"c:/temp/foo.xml"
mDom= xml.dom.minidom.parse(doc)

for i in mDom.getElementsByTagName("firstTagElement"):
dumbA = mDom.createTextNode("  ")
i.appendChild(dumbA)

test = mDom.createElement("test")
tText = mDom.createTextNode("testText")
test.appendChild(tText)
i.appendChild(test)

dumbB = mDom.createTextNode("\n  ")
i.appendChild(dumbB)

print mDom.toxml("UTF-8")
}}}
The file it prints:
{{{
<?xml version="1.0" encoding="UTF-8"?><root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
<test>testText</test>
</firstTagElement>
</root>
}}}
Notice:
*This code is unfinished, the first {{{<root>}}} is on the wrong line.  I'm currently not that sure how to fix that one... :-S
*All other problems however, are solved
*You have to create a text node before your new element, that has the tab required to place it properly in the saved xml file.
*You hav to create a text node after your new element, so that the parental element will be on the correct line, with the correct tab as well.
!~ElementTree
''Bad Code:'' (Code you'd think would work, but doesn't)
{{{
import xml.etree.ElementTree as ET

doc = "c:/temp/foo.xml"
eTree = ET.parse(doc)

firstTagElement = eTree.getroot().find("firstTagElement")
test = ET.Element("test")
test.text = "testText"
firstTagElement.append(test)

eTree.write(doc, "UTF-8")
}}}
The file it writes out is:
{{{
<?xml version='1.0' encoding='UTF-8'?>
<root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
<test>testText</test></firstTagElement>
</root>
}}}
Notice:
*Tag {{{<test>}}} has the wrong indentation level
*Tag {{{</firstTagElement>}}} is on the wrong line.
----
''Good code:''
{{{
import xml.etree.ElementTree as ET

doc = "c:/temp/foo.xml"
eTree = ET.parse(doc)

firstTagElement = eTree.getroot().find("firstTagElement")
firstTagElement[-1].tail = "\n    "
test = ET.Element("test")
test.text = "testText"
test.tail = "\n  "
firstTagElement.append(test)

eTree.write(doc, "UTF-8")
}}}
The file it writes out is:
{{{
<?xml version='1.0' encoding='UTF-8'?>
<root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
<test>testText</test>
</firstTagElement>
</root>
}}}
Notice:
*ALL issues resolved
*In the code, the '{{{.tail}}}' of the last element under {{{<firstTagElement>}}} is updated to have a return char with the right tab spacing for our new element.
*The new element we created also has it's '{{{.tail}}}' value set to a newline, and the correct tab level for the parental element.
!In conclusion:
Clunky!
I can't believe this is standard procedure!
[[PortablePython|http://portablepython.com/]] is, from their web page:
<<<
...a  Python programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python application.
<<<
It's built for both the 2.7 and 3.1 versions, and includes these libs built-in (at the time of this authoring):

2.7:
*~PyScripter v2.5.3
*~NymPy 1.6.1
*~SciPy 0.10.1
*Matplotlib 1.1.0
*~PyWin32 216
*Django 1.4
*PIL 1.1.7
*~Py2Exe 0.6.9
*wxPython 2.9.3.1
*~NetworkX 1.6
*Lxml 2.3
*~PySerial 2.5
*~PyODBC 3.0.2
*PyGame 1.9.1
*~PyGTK 2.24.2
*~PyQt 4.9.1-1

3.1:
*~NetworkX v1.4
*~PySerial 2.5
*~PyScripter v2.4.1
*~PyWin32 v.216
*~RPyC-3.0.7
There are a number of "Precompiled Python Distributions" available.  There is a complete list [[here|http://wiki.python.org/moin/PythonDistributions]].  Two of the more popular ones are:
*[[Enthought Python Distribution|http://www.enthought.com/products/epd_free.php]] (free version)
*[[ActiveState ActivePython|http://www.activestate.com/activepython]]
*[[Portable Python|http://www.portablepython.com/]]
**A list of what packages it has can be found for each of its downloads: [[2.x|http://portablepython.com/wiki/PortablePython2.7.3.2]], [[3.x|http://portablepython.com/wiki/PortablePython3.2.1.1]] (those links are bound to stale, navigate from their home page for latest).
**It's Windows only :(
Like [[Private Methods]], by default any attribute created in a class is considered 'public':  It can be modified by the methods of an object, or outside of an object.  To create a attribute //private// to an object, add two underscores '{{{__}}}' before its name:
{{{
# let's make a private attribute:
class Foo(object):
def __init__(self, namer):
self.__name = namer
}}}
This is just a //convention// though, you can still access that attribute outside of the object if you want to.  But //conceptually//, it's telling the programmer that it should be private, and respected as such.
Like [[Private Attributes]], you can define methods to be private.  Again, you use the same 'double-underscore' '{{{__}}}' notation like attributes:
{{{
# let's make a private method:
class Foo(object):

def __private_method(self):
print "This is a private method."

def public_method(self):
print "This is a public method."
self.__private_method()

f = Foo()
f.public_method()
#This is a public method.
#This is a private method.
}}}
Like the notes from 'private attribute' above, you can actually access the 'private' method from out side the object if you wanted to.  But you shouldn't, since the 'private convention' is telling the programmer explicitly //not// to do this.
Programs are the concepts behind implementing a idea. They are usually made up of one or more [[Module]]s.
http://projecteuler.net/
*"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."
Why is this in my Python wiki?  Because there are other pages devoted to the Python solutions:
*http://wiki.python.org/moin/ProblemSets/Project%20Euler%20Solutions
*http://pyeuler.wikidot.com/
Good way to understand math, and Python
http://pybrain.org/pages/home
<<<
PyBrain is a modular Machine Learning Library for Python. Its goal is to offer flexible, easy-to-use yet still powerful algorithms for Machine Learning Tasks and a variety of predefined environments to test and compare your algorithms.

PyBrain is short for ~Python-Based Reinforcement Learning, Artificial Intelligence and Neural Network Library. In fact, we came up with the name first and later reverse-engineered this quite descriptive "Backronym".
<<<
''Please see my PyGame wiki here:''
http://pygamewiki.tiddlyspot.com/

It could be considered a 'sister-wiki' to this one, focusing solely on PyGame.

PyMT = Python Multitouch
http://pymt.txzone.net/
*"PyMT is a python module for developing multi-touch enabled media rich applications. Currently the aim is to allow for quick and easy interaction design and rapid prototype development. "
*http://vimeo.com/3548811
*http://vimeo.com/3245665?pg=embed&sec
Requires [[pyglet]]


Just some references I've found:
*[[Thinking In Python|http://mindview.net/Books/TIPython]] - Bruce Eckle - Free book download
*{{{.py}}}  --  Standard text-based Python file ([[module]]), authored //in Python//.  To be considered Python 'source code'.
*{{{.pyc}}}  -- 'Python Compiler Script'.  A 'bytecode compiled' version of a .py file.  Generated automatically when a .py module is //[[import]]ed//.  It sits 'in-between' the 'source' .py file, and the runtime (Python Virtual Machine).
*{{{.pyd}}} & {{{.dll}}} (Win) -- {{{.so}}} (Linux) -- 'Python Dynamic Module'.  They are in the format of a .DLL file; intended specifically as a Python extension.  Written in C\C++ (thus not authored //in// Python).  They can be imported and executed like {{{.py}}} files.
*{{{.pyo}}} -- 'Python Optimized Byte Code'.  Also contains bytecode.  Can be executed with the {{{-O}}} command line flag. They can run slightly faster than {{{.pyc}}} files.
*{{{.zip}}} -- Haven't tested this, but apparently it works too, by extracting the {{{.zip}}} automatically when imported.

!Wing:
Personal favorite, see my subject [[Wing IDE]]
!Eclipse:
Very popular in the Java world, but does Python too.
*Main page:  http://eclipse.org/
*And for Python support, requires Pydev plugin:  http://pydev.org/
**Pydev blog:  http://pydev.blogspot.com/
**Pydev supports development for Python, [[Jython|http://www.jython.org/]], and [[IronPython|http://ironpython.net/]] (.NET)
!~PyScripter
*"~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."
http://www.python.org/dev/implementations/
*[[CPython|http://python.org/]]  :  The standard distribution of Python, the one you get off of python.org.  It //is Python// implemented via the ''C'' programming language.
*[[Jython|http://www.jython.org/]]  :   (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 //is Python//, but implemented via ''Java'' (rather than C).
*[[IronPython|http://www.codeplex.com/IronPython]]  :  Designed to allow Python to integrate with apps coded to work with Microsoft's {{{.NET}}} Framework for Windows (authored in {{{C#}}}), as well as the {{{Mono}}} open source equivalent for Linux.  It //is Python//, implemented via ''.NET'' and ''Silverlight'' (rather than C).
*[[Stackless Python|http://www.stackless.com/]]  :  "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."
*[[Cython|http://cython.org/]]  :  "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. "
!!Official web:
*[[3.x Python Documentation|http://docs.python.org/3.1/]]
**[[What's new|http://docs.python.org/3.1/whatsnew/index.html]]
**Detailed Release Notes:
***[[3.1.1|http://svn.python.org/projects/python/tags/r311/Misc/NEWS]]  [[3.1|http://svn.python.org/projects/python/tags/r31/Misc/NEWS]] [[3.0.1|http://svn.python.org/projects/python/tags/r301/Misc/NEWS]] [[3.0|http://svn.python.org/projects/python/tags/r30/Misc/NEWS]]
----
*[[2.x Python Documentation|http://docs.python.org/index.html]] (main)
**[[What's new|http://docs.python.org/whatsnew/index.html]]
**Detailed Release Notes:
**[[Global Module Index|http://docs.python.org/modindex.html]]
**[[Python Reference Manual|http://docs.python.org/ref/ref.html]]
**[[Python Library Reference|http://docs.python.org/lib/lib.html]] (below listed things I access on a more regular basis)
***[[Built-in Functions|http://docs.python.org/lib/built-in-funcs.html]] (always available via the Python interpreter)
***[[Built-in Types|http://docs.python.org/lib/types.html]] (The principal built-in types are numerics, sequences, mappings, files classes, instances and exceptions)
***[[Built-in Exceptions|http://docs.python.org/lib/module-exceptions.html]]
***[[Built-in Constants|http://docs.python.org/lib/node8.html]]
***[[Python Runtime Services|http://docs.python.org/lib/python.html]] (provide a wide range of services related to the Python interpreter and its interaction with its environment)
***[[Generic Operating System Services|http://docs.python.org/lib/allos.html]]
***[[Multimedia Services|http://docs.python.org/lib/mmedia.html]]
***[[String Services|http://docs.python.org/lib/strings.html]] (provide a wide range of string manipulation operations)
***[[Structured Markup Processing Tools|http://docs.python.org/lib/markup.html]] (Have Python play with HTML, SGML, & XML)
***[[Graphical User Interfaces with Tk|http://docs.python.org/lib/tkinter.html]]
**[[Python 'How-to's'|http://docs.python.org/dev/howto/index.html]]
----
*[[Search Python Resources|http://www.python.org/search/]]
*[[Python Package Index|http://pypi.python.org/pypi/]] (see [[Packages]])
*[[Python FAQ|http://www.python.org/doc/faq/]]
*[[PEP 8 -- Style Guide for Python Code|http://www.python.org/dev/peps/pep-0008/]]
*[[mail.python.org|http://mail.python.org/mailman/listinfo]] - A listing of all the public mailing lists on mail.python.org
!!Unofficial web:
*[[pyschools|http://www.pyschools.com/]]
*[[Hidden features of Python|http://stackoverflow.com/questions/101268/hidden-features-of-python]] : From Stack Overflow.
*Python [[Quick Reference Cards|http://www.limsi.fr/Individu/pointal/python/pqrc/]] (for Python 2.4)
*[[Python Module of the Week|http://www.doughellmann.com/PyMOTW/contents.html]]  Great notes on a big chunk of the Python standard library.
*[[Python Package Index|http://pypi.python.org/pypi]] (a repository of software for the Python programming language)
*[[Python Tips, Tricks, and Hacks|http://www.siafoo.net/article/52]]
*[[ShowMeDo|http://showmedo.com/videos/python]]  - Videos showing you how to use Python, nice!  I've seen recommendations to [[this|http://showmedo.com/videos/?author=709]] author.
*[[Python 2.0 Quick Reference|http://www.brunningonline.net/simon/python/quick-ref2_0.html]] - nice overview.
*[[Python Tutorials for kids 8+|http://python4kids.wordpress.com/]]
*[[Introduction to Python|http://lionel.chimou.com/python_for_beginners/]] : Lots of good small examples
----
''On your local disk'' (based on Python install location):
*[[Interactive Python Help]]
!!Books:
*[[Python Programming for the Absolute Beginner - Michael Dawson|http://www.courseptr.com/ptr_detail.cfm?group=Programming&subcat=Other&isbn=978-1-59863-112-8]] - I started learning Python with this book (first edition), @@recommended if you start out a noob like me@@.
*[[Python Pocket Reference - Mark Lutz|http://oreilly.com/catalog/9780596009403/index.html]]  -  Feels nice in the palm of your hand.
*[[Python Phrasebook - Brad Dayley|http://www.informit.com/store/product.aspx?isbn=0672329107]] - Same size as pocket reference, but more actual code examples given for tasks.  Good stuff.
*[[Learning Python - Mark Lutz|http://oreilly.com/catalog/9780596513986/index.html]]  -  @@I've pulled a //lot// from this one@@, after graduating from 'Python Programming for the Absolute Beginner'
*[[Python Cookbook - various|http://oreilly.com/catalog/9780596007973/index.html]]  -  If you're tired of looking to the web for examples, this is a good one to read by the fire, with a glass of wine.
*[[Python Programming - Mark Lutz|http://oreilly.com/catalog/9780596009250/index.html]] - This appears to be the mothership.  I have yet to get it, but some day, I will.
*http://www.packtpub.com/ : Seems to have a good selection of Python material covering many subjects.
!!Online Books (free):
*A list of free online Python books can be found [[here|http://www.e-booksdirectory.com/programming.php#python]].
*[[Invent Your Own Computer Games With Python|http://inventwithpython.com/]] : "...is a free e-Book that teaches you how to program in the Python programming language. Each chapter gives you the complete source code for a new game, and then teaches the programming concepts from the example."
*[[Learn Python The Hard Way|http://learnpythonthehardway.org]] "The book is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up their skills before starting a more detailed book. "
*[[Building Skills In Programming|http://homepage.mac.com/s_lott/books/nonprogrammer.html]]
*[[Building Skills In Python|http://homepage.mac.com/s_lott/books/python.html]]
*[[Building Skills in Object-Oriented Design|http://homepage.mac.com/s_lott/books/oodesign.html]]
!!Magazines:
*[[PyMag|http://pymag.phparch.com/]]  -  Do you want Python delivered to your doorstep once a month?
**I've been subscribing to this since Dec 2008.  While (as of this authoring, March 2009) much of it is over my head, it does provide a lot of inspiration, and a good source of Python applications in the real wor.d
*[[The Python: Rag|http://www.pythonrag.org/]] : A monthly .pdf magazine.
*[[Py|http://www.pyzine.com]] - Online zine, but hasn't been updated in years.
This is my overview of Python.  Probably going to be different from other people's overviews since it's heavily skewed based around what I use Python for:  Authoring tools and pipelines on the technical art (characters specifically) side of video game development.  But I thought it would be good, for my own brain, to write down high-level features of the language to get a better understanding of it myself.  And based on the time of this authoring, it's all still based on Python 2.6, since that's what I use.

@@Very, very very (very) work in progress.@@  Will update it from time to time...

!Things Python Comes With:
*[[Great Documentation|http://www.python.org/doc/]] (some callouts below):
**[[Glossary|http://docs.python.org/glossary.html#glossary]]
**[[Language Reference|http://docs.python.org/reference/]]
*[[The Standard Library|http://docs.python.org/library/index.html]] :  This encompass everything that ships with the standard install of Python.  I break it down into three main areas:  Built-in //functions//, built-in //types//, built-in (global) //modules//:
**[[Built-in functions|http://docs.python.org/library/functions.html]].  These functions come with the standard install.  You always have them at your fingertips.
**[[Built-in types|http://docs.python.org/library/stdtypes.html]] : Sequences, numeric, comparison, Boolean, iterator, generator, string, etc.  If a piece of built-in data has a 'type', this is where you find it.  When you assign a value to a variable, the value will assume a given 'type'.
**[[Global Modules|http://docs.python.org/modindex.html]] : These modules, like built-in functions, area always available, but must be imported before usage.
**There are more than this though:  The Standard Library link lists the rest: Built-in Constants, Built-in Exceptions, etc.  Everything after that is a more organized list of the Global Modules.
!Implementations of Python:
I think of Python as a concept, or a syntax, or a way of doing things.  The way that concept\syntax is implemented can vary.  The default implementation of Python is via the C programming language, and is known as '~CPython'.  But there are others.  For example, Jython is the same python syntax as ~CPython, but implemented in Java.  This lets you create Java code, but author it via the Python syntax, which is pretty slick.  Find a more robust list under my subject: [[Python Implementations]].

!General Concepts:
!!! Statements and Expressions
Statements are one or more lines of Python code that give Python orders.  Expressions do the work that return values.  See my notes on [[Statement]].  For example
{{{
stuff = 2 + 3
}}}
That is an 'assignment statement'.  {{{2 + 3}}} is the expression, which returns a value (which becomes an integer object) which the variable name {{{stuff}}} is assigned to.
!!!Importing, namespaces, and attributes:
!!!What's with all those {{{__}}}underscores{{{__}}}?
Lib notes [[here|http://docs.python.org/reference/lexical_analysis.html#reserved-classes-of-identifiers]]

!Scope
!!!Local
Function Level
!!!Global
Module Level
!!!Universal
Built-in Level

!Packages & Pathing

!Language Features
Functions, modules, and concepts that I find to be darn handy.
!!Functions:
!!!lambda
!!!map
!!!filter
!!!reduce
!!!dir
!!Concepts:
!!!parameters & arguments
!!!unpacking variables
!!!decorators
!!!list comprehentions
!!!Dynamically Typed Language
*Not typed as in 'you typed something', but the 'types of data' are dynamically assigned at time of creation:
<<<
{{{
# Python
spam = 23  # Dynamically typed
# Java
int spam = 23;  # Strongly typed
}}}
In the Python example, 23 becomes an integer type, that the variable {{{spam}}} simply points to.
In the Java example, we create a variable named spam that we define as an integer type, and assign it the value of 23.
<<<

!Object Oriented Programming
!!!Properties
!!!Polymorphism

[[Blender|www.blender.org]] is free, open-source computer animation software.  I've never gotten around to learning it, despite the fact I'd really like to (currently [[Processing|www.processing.org]] takes up most my free dev time...).
Blender is based around Python however, so if you have a hankerin' to do some full-blown 3D with Python...

You can find their "Scripting" documentation page here:  http://www.blender.org/development/
And the current link (Blender 2.48) to the Python API is here:  http://www.blender.org/documentation/248PythonDoc/
----
Video describing Python integration with Blender 2.5: (major update version for Blender)
http://python.mirocommunity.org/video/1716/using-python-for-blender-anima
* The eBook [[Music For Geeks And Nerds|http://musicforgeeksandnerds.com/]]
http://zine.pocoo.org/
Really verbose comparison of PHP, Perl, Python, Ruby:
http://hyperpolyglot.org/scripting

That site actually has comparisons for many other languages as well:
http://hyperpolyglot.org/
I ran across this blog post that compares several with code examples.  Nice.

http://stackoverflow.com/questions/326300/python-best-library-for-drawing

*http://networkx.lanl.gov/ - for graphing & plotting
I have a whole other wiki dedicated to Maya, and mel (its scripting language) specifically.  For ~Maya-Python related stuff, you can find it there too:
http://mayamel.tiddlyspot.com/
Check out the 'PYTHON' catagory:
http://mayamel.tiddlyspot.com/#PYTHON
*Original blog: http://blog.jimmy.schementi.com/2010/03/pycon-2010-python-in-browser.html
This blog post goes on to explain how to make this happen, via [[IronPython|http://ironpython.net/]] and [[Microsoft Silverlight|http://www.silverlight.net/]]
----
This now has an official home as well, with examples and more instruction:
*http://ironpython.net/browser/
What happens when you get one of these?
{{{
RuntimeError: Bad magic number in .pyc file
}}}
Just delete the {{{.pyc}}} file, and rerun the module.  I'm told it means the {{{.pyc}}} was compiled against a different version of Python, and needs to be recompiled.
*[[Python for Android|https://github.com/kivy/python-for-android]] - Not yet used this.
**I have notes on my blog for setting this up.  [[Install SL4A on the Emulator|http://www.akeric.com/blog/?p=879]], [[Install SL4A on an Android phone|http://www.akeric.com/blog/?p=1250]].
Different setup than Python on a PC:  Mac comes with Python, PC doesn't.  When you install it on the PC, you're usually installing the current version (or any version you choose).  But the mac already has it installed in it's Frameworks structure, and it's usually //not// the current version.  Here is some documentation on the issue:
*http://wiki.python.org/moin/MacPython
*http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/python.1.html
Places to paste Python code for good quality sharing:
*http://codepad.org/
This blog post talks about it:
And here is some code:
https://bitbucket.org/amorano/pyps

There has been reported success building this in Visual Studio 2008 on Windows, using Photoshop CS 5.1 and Python 2.6.
You'll need the Photoshop SDK:
http://www.adobe.com/devnet/photoshop/sdk.html
This is closely tied to Python [[namespace]]s.
Python has three (well, possibly four, see below) scopes that variables can be found in:
*{{{__main__}}}  :  The top most scope in Python (where {{{__builtins__}}} live):  If you execute a [[Statement]] from the interactive prompt, it is local to this location.  It's also the scope of a module when its icon is double-clicked.  See Python [[docs|http://docs.python.org/lib/module-main.html]]
**{{{globals()}}}  :  The scope of the current module (if currently executing in a module).  See Python [[docs|http://docs.python.org/ref/exec.html#l2h-587l]]
***{{{locals()}}}  :  The scope of the current function (if currently executing in a function).  This scope can actually be in an 'enclosing local scope', if function definitions are nested (thus adding another scope to search).  See Python [[docs|http://docs.python.org/ref/exec.html#l2h-587l]]
If you execute an expression like such:
{{{
print foo
}}}
Python first searches for variable names in {{{locals()}}} scope, then the 'enclosing locals scope' (if present), then {{{globals()}}} scope, and finally {{{__main__}}} scope.  If {{{foo}}} was defined in both a parental module, //and// the current function being executed, the definition in the function would be used, since its searched first.
>{{{__main__}}}
>Search here fourth.  This is also where {{{__builtins__}}} live.  I call this the 'universal' scope.  It's also known as the 'builtin' scope.  Is {{{foo}}} here?  If not, raise an Exception.
>>{{{someModule.py}}} - {{{globals()}}}
>>Global scope.  Search here third.  Is {{{foo}}} here?  If not, look one scope higher.
>>>{{{someEnclosingFunction()}}} - {{{locals()}}}
>>>Enclosing Local scope.  Search here second (if present).  Functions can be nested in functions.
>>>>{{{someFunction()}}} - {{{locals()}}}
>>>>Local scope.  Search here first.  Is {{{foo}}} here?  If not, look one scope higher.
{{{locals()}}} and {{{globals()}}} are both built-in functions (from the {{{__builtin__}}} module) that return dictionaries of the variables defined at the specified scope.

The {{{global}}} //command// however lets you point to 'global' variables in a 'local' scope:
{{{
# someModule.py - starts global scope
gVar = 23 # define global var gVar
lVar = 100 # define global var lVar
def myDef():  # enter local scope
global gVar  # define gVar in this scope to be global
gVar = "foo!"  # update global var gVar
lVar = "boo!"  # define new *local* var lVar
print gVar, lVar
# 23 100
myDef()
print gVar, lVar
# foo! 100
}}}
----
{{{<<}}}''Tangent''{{{>>}}}
<<<
Everything defined in the {{{__builtin__}}} module is local to the {{{__main__}}} scope.  However (and this is my personal assumption), {{{__builtin__}}} is imported into {{{__main__}}} in the [[namespace]] {{{__builtins__}}} (note the 's').  I've heard the location that {{{__builtin__}}} lives in as '//the implied outer built-in scope//'.  {{{__builtin__}}} is always present, always there, freely giving you its goodness.  This is why you //don't// have to type this to access its functions:
{{{
import __builtin__
__builtin__.globals()
# I mean, you could do this... but why all the extra typing?
}}}
{{{
# Just do:
globals()
# instead, without need of the previous import.
# __builtin__ has already been imported by Python, so you don't need to do it.
}}}
I'm not sure exactly how this works in practice, but one theory I have is that when Python starts, it does something like this:
{{{
# import __builtin__ in the __builtins__ namespace:
import __builtin__ as __builtins__
from __builtin__ import *
}}}
But I'm just guessing here...
<<<
{{{<<}}}''End Tangent''{{{>>}}}
----
When you interactively enter [[Statement]]s in Python (say, at the command line, or via IDLE), they are added to {{{__main__}}}'s dictionary {{{__dict__}}} for subsequent retrieval.  Since {{{__main__}}} is the 'top level script environment', variables defined there are seen in all other modules.  They're not 'global', I call them 'universal' ;)
{{{
# Define a variable, which is assigned to __main__.__dict__:
foo = 23
# Since this was defined in __main__, we can access directly:
print foo
# 23

# To actually see where that data physically lives:
import main
print __main__.__dict__["foo"]
# 23
}}}
However, as shown above, to actually access {{{__main__.__dict__}}}, you first need to import it.
----
''Universal scope''
That's a term I made up just for Python.  So:  Globals is module level.  Locals is function level.  What's above globals?
As mentioned above, {{{__main__}}} is the top most module.  But if you truly want to store persistent data between modules, you can modify the {{{__builtin__}}} namespace that all modules share, or the module-level specifc import of {{{__builtin__}}}: '{{{___builtins__}}}' (note the 's')
{{{
# moduleA.py
import __builtin__
__builtin__.spam = 42
# __builtins__ already exists in the module as a dictionary, no need to import:
__builtins__["ham"] =24
}}}
{{{
# moduleB.py
# this will fail if moduleA isn't executed first
print spam, ham
}}}
{{{
>>> import moduleA
>>> import moduleB
42 24
}}}
As you can see, in {{{moduleB.py}}}, we don't do any importing, we just {{{print spam, ham}}}.  And since {{{moduleA.py}}} had previously updated {{{__builtin__}}} with the 'foo attribute', and the {{{__builtins__}}} dict with the 'ham' key, it was then visible to any other module.
I should say that many would frown on working this way, and I'm not saying you should.  But I do think its important to understand how things work.
----
Also see:
*[[Understanding __dict__]]


The only way I can find via the Standard Libaray is via {{{Tkinter}}}'s {{{PhotoImage}}} class:
{{{
import Tkinter

# Need to call to Tk, I guess to initialize things?
root = Tkinter.Tk()
img = Tkinter.PhotoImage(file='c:/temp/spam.gif')
print img.height(), img.width()
# 32 64
}}}
This is limiting, since {{{PhotoImage}}} only supports GIF and PGM/PPM image formats.  It also has the {{{BitmapImage}}} class, but it only supports monochrome (two-color) images in the X11 bitmap format.
----
For more robust functionality (and a wider range of file formats), you can use the (external lib) [[Python Image Library's|http://www.pythonware.com/products/pil/]] (PIL) [[Image module|http://www.pythonware.com/library/pil/handbook/image.htm]]:
{{{
from PIL import Image
im = Image.open("spam.jpg")
width, height = im.size
}}}


I have yet to find a ''built-in\platform-independent'' way of doing this (at the same time).  But below are a few options.
----
If you have the module [[win32api|http://python.net/crew/mhammond/win32/]] installed you can query it this way on a ''Windows'' box:
{{{
from win32api import GetSystemMetrics
res = ( GetSystemMetrics(0), GetSystemMetrics(1) )
}}}
----
If you have [[PyGame|http://www.pygame.org]] installed, you can use it as a ''platform-independent'' query:
{{{
import pygame

info = pygame.display.Info()
res = (info.current_w, info.current_h)
print res
# (1920, 1200)
}}}
Most of this I learned from the [[Raspberry Pi Users Guide||http://www.amazon.com/Raspberry-User-Guide-Gareth-Halfacree/dp/111846446X]].
!!! ~RPi.GPIO Installation
The Python library that book recommends to access the GPIO ports is:
*http://pypi.python.org/pypi/RPi.GPIO
To install it on your Pi, in a Pi terminal, cd to your home directory.  Then (the below files are based on the current ones at the time of authoring.  Check the site for the latest version).
Documentation:  There does't appear to be much (at least, none that I can find).  However, a [[test.py|http://code.google.com/p/raspberry-gpio-python/source/browse/test/test.py]] module is included, showing much of its functionality.
{{{
tar xvzf RPi.GPIO-0.4.1a.tar.gz
cd RPi.GPIO-0.4.1a
sudo python setup.py
}}}
!!!Pin Layout
The Pi has 26 pins, in two rows.  While they are numbered 1-26, they can be indexed two different ways:  A, Via these numbers, or B, via a different set of 'GPIO' numbers (discussed below).
http://elinux.org/RPi_Low-level_peripherals : Includes a good descriptive pic, illustrating the 'GPIO numbering' indexing.

If you hold the pi so you can read all the words properly:
| 2 : 5v | 4 : DNC | 6 : grnd | 8 : GPIO 14 (UART TXD) |10 : GPIO 15 (UART RXD) | @@12 :GPIO 18@@ | 14 : DNC | @@16 : GPIO 23@@ | @@18 :GPIO 24@@ | 20 : DNC | @@22 : GPIO 25@@ | 24 : GPIO 8 (SPI 0) | 26 : GPIO 7 (SPI 1) |
| 1 : 3v3 | 3 : GPIO 0 (I^^2^^C SDA) | 5 :GPIO 1 (I^^2^^C SCL) | 7 : GPIO 4 (~GPCLK0) | 9 : DNC | @@11 : GPIO 17@@ | @@13 : GPIO 21@@ | @@15 : GPIO 22@@ | 17 : DNC | 19 : GPIO 10 (SPI MOSI) | 21 : GPIO 9 (SPI MISO) | 23 : GPIO 11 (SPI SLCK)  | 25 : DNC |
Anything with 'DNC' means 'do not connect' (according to the book).  Anything @@highlighted yellow@@ are the pins truly 'general purpose'. and available for arbitrary usage.  The other pins are used for different communication protocols.
!!!Pin Power
*Incoming Power:
**+5v over USB.  ~PoE (Power over Ethernet) isn't yet possible.
*GPIO Pin Info:
**+3.3v out, 50mA max
**The +5v pins, It's 1A max minus the boards own usage (700ma).  This equates to: 1000 mA - 700 mA -> max current draw: 300 mA
Internal pull-up and pull-down resistors can be set on pins:
{{{
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_OFF) # The default
}}}
!!!Pin Access
The {{{RPi.GPIO}}} library gives easy access to the {{{GPIO}}} pins.  That being said, I had a bit of confusion initially based on some code changes that went in after the "Raspberry Pi Users Guide" was published.  Namely, how you know which method you're using to access the pins, whether the board-pin layout, or the 'GPIO numbers' layout?
Long story short:  If you want to use the pin numbers as ordered on the Pi itself, you need to call to this function to set it up:
{{{
import RPi.GPIO as GPIO
# Set to use physical board pin nums, rather than GPIO nums:
GPIO.setmode(GPIO.BOARD)
}}}
If you want to use the 'GPIO' pin numbers, call using this arg instead:
{{{
import RPi.GPIO as GPIO
# Set to use GPIO nums, rather than physical board nums:
GPIO.setmode(GPIO.BCM)
}}}
And what is 'BCM' anyway?  (I think it's possibly an abbreviation for the "~BroadCoM" chipset it uses).

!!!Examples
Here's a simple example (I have running on the Pi as I type, via Adafruit's [[Raspberry Pi's WebIDE|http://learn.adafruit.com/webide]] * the Pi Cobbler) showcasing these concepts.
{{{
#!/user/bin/env python
"""
gpiooutput.py
Modified from pg 195 of "Raspberry Pi Users Guide"

Make a LED flash on\off.
Hardware Setup:
* LED+ lead hooked to GPIO 17 (board pin 11)
* LED- lead connected to resistor.
* Resistor connected to ground (board pin 6)
"""
import time
import RPi.GPIO as GPIO

# This is the GPIO number.  The actual board pin is number 11.
PINOUT = 17

def main():
GPIO.setmode(GPIO.BCM) # Set to use GPIO nums, rather than physical board nums
GPIO.setup(PINOUT, GPIO.OUT)
print "Blink begins!  Press ctrl+c to exit"
try:
i = 1
while True:
GPIO.output(PINOUT, True)
time.sleep(2)
GPIO.output(PINOUT, False)
time.sleep(2)
i += 1
except KeyboardInterrupt:
GPIO.output(PINOUT, False)
return

if __name__ == "__main__":
main()
}}}
Found here: http://nodebox.net/code/index.php/Math
Source:
*[[distance|Location of a point based on angle and distance]]
*[[angle|Angle between two points]]
*[[coordinates|Location of a point based on angle and distance]]
{{{
def reflect(x0, y0, x1, y1, d=1.0, a=180):
d *= distance(x0, y0, x1, y1)
a += angle(x0, y0, x1, y1)
x, y = coordinates(x0, y0, d, a)
return x, y
}}}
[[Regular Expression Syntax|http://www.python.org/doc/2.4.2/lib/re-syntax.html]]
[[Regular Expression HOWTO|http://docs.python.org/howto/regex.html]]
http://www.doughellmann.com/PyMOTW/re/
{{{
import re
}}}
{{{
# re.match()
# Determine if the RE matches at the beginning of the string.
# This returns an object, and it's mainly good for truth testing.
foo = "new_stuff"
if re.match("new", foo):
print "yes"
# yes
}}}
{{{
# Scan through a string, looking for any location where this RE matches.
re.search()

# Find all substrings where the RE matches, and returns them as a list.
re.findall()

# Find all substrings where the RE matches, and returns them as an iterator.
re.finditer()
}}}
----
If you're trying to match a sub-string in a longer string, but you want to match even if the case doesn't:
<<tiddler [[How can I match strings irregardless of case?]]>>
----
Extract the last sequence of integer numbers from a string:
{{{
myStr = 'pPlane1.vtx[60]'
pattern = r'\d+'
vid = int(re.findall(pattern, myStr)[-1])
# 60
}}}
{{{
>>> if re.search("word", "is there a word in here?"):
>>>     print "found match!"
found match!
}}}
{{{
# note that .group() only works if there is a match
>>> m = re.match("ab", "abcd").group()
>>> print m
'ab'
}}}
{{{
>>> re.findall(r'bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
}}}
{{{
>>> re.sub(r'(b[a-z]+) 1', r'1', 'cat in the the hat')
'cat in the hat'
}}}
{{{
>>> fooRe = re.compile(r'foo.*?((.*?))')
>>> fooRe.search('foo(bar)').group(1)
'bar'
>>> fooRe.search('This is a foo bar baz blah blah (bar)').group(1)
'bar'
}}}
{{{
word = "find the number at the end 23"
print re.findall('[0-9]+$', word) # ['23'] }}} ---- This doesn't use the {{{re}}} module, but its so similar I think it should be listed. <<tiddler [[How can I find the 'prefix' of a string?]]>> *2D: **[[pymunk|http://code.google.com/p/pymunk/]] is a wrapper around [[Chipmunk|http://code.google.com/p/chipmunk-physics/]] : a "2D rigid body physics library in C" **[[pybox2d|http://code.google.com/p/pybox2d/]] is a wrapper around [[Box2D|http://www.box2d.org/]] : a "2D rigid body physics library in C++" ***[[Box2D Wiki for pybox2d|http://www.box2d.org/wiki/index.php?title=Box2D_with_Python]] *3D **[[PyODE|http://pyode.sourceforge.net/]] is a set of open-source Python bindings for the [[Open Dynamics Engine|http://ode.org/]] **[[cgkit|http://cgkit.sourceforge.net/]] has a wrapper around the [[Open Dynamics Engine|http://ode.org/]] in its ~ODEDynamics ([[docs|http://cgkit.sourceforge.net/doc2/odedynamics.html]]) module. **[[VPython|http://vpython.org]] ***Example videos: http://showmedo.com/videotutorials/series?name=pythonthompsonvpythonseries Notes and code examples: *http://panela.blog-city.com/ann_coreit__helper_to_throw_python_code_onto_cores.htm **http://github.com/mattharrison/coreit **http://github.com/mattharrison/coreit/blob/master/coreit.py !pickle \ cPickle The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”. *http://docs.python.org/library/pickle.html *http://docs.python.org/library/pickle.html#module-cPickle *[[What can be pickled, and unpickled?|http://docs.python.org/library/pickle.html#what-can-be-pickled-and-unpickled]] >The pickle module has an optimized cousin called the cPickle module. As its name implies, cPickle is written in C, so it can be up to 1000 times faster than pickle. However it does not support subclassing of the Pickler() and Unpickler() classes, because in cPickle these are functions, not classes. Most applications have no need for this functionality, and can benefit from the improved performance of cPickle. Other than that, the interfaces of the two modules are nearly identical; the common interface is described in this manual and differences are pointed out where necessary. Simple Example: {{{ import os import cPickle # Create a temp filename to store our data in: dataFile = os.path.join(os.getenv("TMP"), "pyData.txt") # in my case, it lives here (in Vista): # C:\Users\<USERNAME>\AppData\Local\Temp\pyData.txt # Make dictionary of some data to store: data = {"first":1, "second":2} # Write/pickle/serialize/dump our data to disk: with open(dataFile, 'w') as outf: # The 'protocol' has been set to 2, which controls how return # characters are stored. Use it. cPickle.dump(data, outf, 2) # Read/unpickle/unserialize/load the data: with open(dataFile) as inf: loadedData = cPickle.load(inf) print loadedData }}} {{{ {'second': 2, 'first': 1} }}} !shelve A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings. *http://docs.python.org/library/shelve.html *http://inventwithpython.com/blog/2012/05/03/implement-a-save-game-feature-in-python-with-the-shelve-module/ !marshal This module contains functions that can read and write Python values in a binary format. The format is specific to Python, but independent of machine architecture issues (e.g., you can write a Python value to a file on a PC, transport the file to a Sun, and read it back there). Details of the format are undocumented on purpose; it may change between Python versions (although it rarely does). This is not a general “persistence” module. For general persistence and transfer of Python objects through RPC calls, see the modules pickle and shelve. The marshal module exists mainly to support reading and writing the “pseudo-compiled” code for Python modules of .pyc files. Therefore, the Python maintainers reserve the right to modify the marshal format in backward incompatible ways should the need arise. If you’re serializing and de-serializing Python objects, use the pickle module instead – the performance is comparable, version independence is guaranteed, and pickle supports a substantially wider range of objects than marshal. *http://docs.python.org/library/marshal.html  The bulk of these notes are pulled from [[Programming Computer Vision With Python|http://shop.oreilly.com/product/0636920022923.do]] ---- Docs, etc: http://www.scipy.org/ http://docs.scipy.org/doc/scipy/reference/ ---- ~SciPy builds on ~NumPy and extends it. ---- Blur an image, via image convolution: {{{ from PIL import Image from numpy import array from scipy.ndimage import filters im = array(Image.open(myImage).convert('L')) imBlur = filters.gaussian_filter(im, 5) }}} ---- Open some [[Matlab|http://www.mathworks.com/products/matlab/]] {{{.mat}}} data: {{{ import scipy data = scipy.io.loadmat('test.mat') }}} {{{data}}} is a {{{dict}}} with keys corresponding to variable names saved in the {{{.mat}}} file. ---- Save [[Matlab|http://www.mathworks.com/products/matlab/]] {{{.mat}}} data: {{{ import scipy data = {} data['x'] = 23 scipy.io.savemat('test.mat', data) }}} ---- Save a Numpy array as an image: {{{ import scipy scipy.misc.imsave("test.jpg", myArray) }}} You can also save out a "Lena" grayscale test image, 512x512: {{{ lena = scipy.misc.lena() }}} I recently ran across this post that spells it out pretty nicely: http://www.blog.pythonlibrary.org/2010/05/14/how-to-send-email-with-python/ serial port: ~COM-port, or ~RS232 ---- Python seems to have no builtin 'serial' module\package. But searching the Python docs here: http://docs.python.org/3.1/faq/library.html#how-do-i-access-the-serial-rs232-port Leads one to ''pySerial'': http://pyserial.sourceforge.net/ pySerial Class docs: http://pyserial.sourceforge.net/pyserial_api.html#classes ---- Example ''sending'' data to the serial port (example pulled from the [[Arduino docs|http://www.arduino.cc/playground/Interfacing/Python]]). It should be noted that this only works properly when excuted from an interactive shell... it fails to work when in a module. I'm trying to figure out why... {{{ >>> import serial >>> PORT = 'COM4' >>> ser = serial.Serial(PORT) >>> ser.write('5') >>> ser.close() }}} ---- [[Video from PyCon 2012|http://www.youtube.com/watch?v=54XwSUC8klI&feature=player_embedded#!]] by Peter Kropf illustrating how to have Python communicate over a serial port to control an Arduino. ---- Example with ''capturing'' serial data, in this case sent from an [[Arduino|http://www.arduino.cc/]] microcontroller. {{{ import serial ARDUINO = "COM4" def main(): ser = serial.Serial(ARDUINO, timeout=1) prevVal = None while 1: # Read the serial value ser.flushInput() serialValue = ser.readline().strip() # Catch any bad serial data: try: if serialValue != prevVal: # Print the value if it differs from the prevVal: print "New Val: ", serialValue prevVal = serialValue except ValueError: pass ser.close() if __name__ == '__main__': main() }}} If you run this from a command prompt, presuming the Arduino is broadcasting data, any //new// data will be printed to the display. If you want to see more examples of this using the Arduino, including code running in [[Pygame|http://www.pygame.org]] and [[Processing|http://www.processing.org/]], you can check my blog here: http://www.akeric.com/blog/?p=1015 ---- Finding Serial Ports: http://pyserial.sourceforge.net/examples.html#finding-serial-ports ---- !!!Notes from the web: http://ayaz.wordpress.com/2007/06/11/serial-port-communication-on-slackware-using-pyserial/ <<< You can use two features of pySerial to your advantage. One is the timeout parameter that you specify when creating a serial.Serial() object. If you set timeout=None, and call method read() or readline(), pySerial will wait forever until there is data in its receive buffer. The second feature is the inWaiting() method. It tells you how many characters are waiting in the receive buffer to be read. If you set timeout=None, run a loop, but call readline() once within the loop, your program will block until there are characters in the receive buffer. You can, then, do what you want with the data received. Or, you could loop around a call to inWaiting(), only calling read() or readline() whenever there are characters waiting in the receive buffer. <<< ---- http://eli.thegreenplace.net/category/programming/serial-port/ many subjects listed ---- <<closeAll>><<permaview>><<newTiddler>><<saveChanges>><<tiddler TspotSidebar>><<slider chkSliderOptionsPanel OptionsPanel 'options »' 'Change TiddlyWiki advanced options'>> <<tabs txtMainTab Tags 'All tags' TabTags >> <<tabs txtMainTab Dummy Dummy TabDummy Timeline Timeline TabTimeline All 'All tiddlers' TabAll More 'More lists' TabMore>> Found this blog post [[here|http://ruslanspivak.com/2011/03/30/simple-http-server-from-the-command-line/]], reposted below: <<< I’ve been using this small Python gem for couple years now and it really comes in handy when you need a quick web server running without setting up nginx or apache or what have you. Just open up a terminal and type:﻿ {{{$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
}}}
Now check it in your browser: http://localhost:8000/

You can also change port the server is listening on:
{{{
$python -m SimpleHTTPServer 7070 Serving HTTP on 0.0.0.0 port 7070 ... }}} <<< Since I (currently) do zero web\server stuff with Python, this is pretty fascinating to me. When I open my browser, I see a mirror of this dir tree (from Windows 7): {{{ C:\Users\<userName> }}} Whenever I need to write a recursive function I always tie my brain in a knot trying to figure it out, when it's really not that hard. Simple example below. Its a silly example, because it's redoing what {{{os.walk}}} does, but it does show recursion. {{{ import os startDir = r'c:\temp' # This will be list that holds all the stuff we find: dirs = [startDir] # Loop over our ever expanding list: for d in dirs: walker = os.walk(d) subdirList = walker.next()[1] if len(subdirList): subdirs = [os.path.join(d, sd) for sd in subdirList] # Extend our list with the new stuff we find, which will # be iterated over in future loops: dirs.extend(subdirs) }}} {{{ print dirs ['c:\\temp', 'c:\\temp\\foo', 'c:\\temp\\test', 'c:\\temp\\foo\\boo'] }}} notes on learning the language [img[python wiki|http://farm4.static.flickr.com/3026/2514848314_335cab2127.jpg?v=0][Welcome]]python wiki A place to collect sorting tips I've picked up. *[[How can I sort a dictionary by its values?]] *[[How can I sort strings with non-padded numbers on the end?]] *[[How can I sort by my own rules?]] ---- Some examples from: http://docs.python.org/library/operator.html#operator.itemgetter {{{ from operator import itemgetter inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)] getcount = itemgetter(1) map(getcount, inventory) # [3, 2, 5, 1] sorted(inventory, key=getcount) # [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)] }}} http://docs.python.org/library/stdtypes.html#special-attributes {{{ __dict__ __methods__ __members__ __class__ __bases__ __name__ }}} Also: {{{ __file__ }}} A special member of every module is {{{__dict__}}}. This is the dictionary containing the module's symbol table. http://docs.python.org/lib/typesmodules.html ---- Also see: *[[Special Modules]] *[[Understanding __dict__]] I'm starting another section here: [[Operator Overloading]] But I'm keeping this tiddler around until that one is updated. ---- ''Official link#1: http://www.python.org/doc/2.3.5/ref/specialnames.html'' ''Official link#2: http://docs.python.org/ref/specialnames.html'' Link#3: http://www2.lib.uchicago.edu/keith/courses/python/class/5/ Link#4: http://www.siafoo.net/article/57 (good reference) In learning Python, I constantly see names with 'double-underscores' on either side of them. The top link is a good overview of all the 'special methods' relative to [[Class]]es. Some specific notes are below, as I interact with them. ---- {{{ __init__ }}} This is a 'constructor method' or 'initialization function' for a [[Class]]. Usually written like: {{{ def __init__(self, someArg): someAttr = someArg }}} ---- {{{ __str__ }}} When you '{{{print}}}' a [[Class]], it usually spits out something like this: {{{ f = Foo() # make a f object print f <__main__.Foo object at 0x00A0Ba90> }}} But by defining a {{{__str__}}} method in your [[Class]], you can have it print something else: {{{ def __str__(self): rep = "This is a Foo object, yay!" return rep }}} In general, the string returned by {{{__str__}}} is meant for the user of an application to see, while the string returned by {{{__repr__}}} (below) is meant for the programmer to see. ---- {{{ __del__ }}} A {{{__del__}}} method is called when an object is deleted, which is when the garbage collector decides that their are no more references to an object {{{ def __del__(self): #code }}} ---- {{{ __repr__ }}} A {{{__repr__}}} method takes exactly one parameter, self, and must return a string. This string is intended to be a representation of the object (This is sometimes called the 'reproduction string'), suitable for display to the programmer, for instance when working in the interactive interpreter. {{{__repr__}}} will be called anytime the builtin {{{repr}}} function is applied to an object; this function is also called when the backquote operator is used. See {{{__str__}}} above. See Python docs on [[Emulating container types|http://docs.python.org/ref/sequence-types.html]]. ----  ---- http://docs.python.org/lib/module-builtin.html: Provides direct access to all built-in' identifiers of Python {{{ __builtin__ }}} ---- http://docs.python.org/lib/module-main.html Represents the (otherwise anonymous) scope in which the interpreter's main program executes {{{ __main__ }}} It is the top level [[namespace]] for the current execution. ---- http://docs.python.org/lib/module-future.html {{{ __future__ }}} ---- Also see: *[[Special Attributes]] I had need of a solution, found one over on [[Stack Overflow|http://stackoverflow.com/questions/3149440/python-splitting-list-based-on-missing-numbers-in-a-sequence]], some of it is pasted below: {{{ >>> from operator import itemgetter >>> from itertools import * >>> seq2 = [1, 2, 4, 5, 6, 8, 9, 10] >>> chunks = [] >>> for k, g in groupby(enumerate(seq2), lambda (i,x):i-x): ... chunks.append(map(itemgetter(1), g)) ... >>> print chunks [[1, 2], [4, 5, 6], [8, 9, 10]] }}} Or with a list comprehension: {{{ >>> chunks = [map(itemgetter(1), g) for k, g in groupby(enumerate(seq2), lambda (i,x):i-x)] [[1, 2], [4, 5, 6], [8, 9, 10]] }}} How slick is that? http://docs.python.org/library/itertools.html#itertools.groupby http://docs.python.org/library/operator.html#operator.itemgetter Obviously Python comes with a {{{sqrt}}} function in the {{{math}}} module. But what if you want to 'make your own'? The below example, and descriptions for the code can be found here: http://radiantbytes.com/books/python-latex/src/chap9.html {{{ def abs(val): #Could just import abs from the math module of course. if val < 0: val *= -1 return val def average(a, b): return (a + b) / 2.0 def improve(guess, x): return average(guess, x/guess) def good_enough(guess, x): d = abs(guess*guess - x) return (d < 0.001) def square_root(guess, x): while(not good_enough(guess, x)): guess = improve(guess, x) return guess def sqrt(x): r = square_root(1, x) return r }}} Statements are one or more lines of Python code ''that give Python orders''. *[[Simple Statements|http://docs.python.org/reference/simple_stmts.html]] *[[Compound Statements|http://docs.python.org/reference/compound_stmts.html]] Statements can span multiple lines with the 'line continuation character' '{{{\}}}' (but I hear that's gone out of flavor these days): {{{ # single line: print "happy" #multi-line: print\ "happy" }}} Statements are made up of two parts, the ''command'', and the ''expression''. Given the following example: {{{ print 2+3 }}} *The ''command'' is {{{print}}}. Tells the computer to take action. *The ''expression'' is the string {{{2+3}}}. It is what the command acts upon. An expression is a piece of syntax which can be evaluated to some value. {{{2+3}}} is an expression, since it evaluates to {{{5}}}. ---- This is called an 'assignment statement': {{{ name = "juffo wup" # we are assigning the value on the right to the variable on the left }}} ---- To try and clear things up a bit... When you 'import something', '{{{import}}}' is a statement: {{{ import spam }}} However, to differentiate this, the {{{import}}} //statement// actually calls to the {{{__import__}}} //function//: {{{ __import__("spam") }}} So in this case, the //statement command// {{{import}}} invokes the {{{__import__}}} //function//, and the //statement expression// {{{spam}}} is passed in as a //string argument// to the //function//. http://docs.python.org/library/functions.html#staticmethod Static methods are designed to be invoked through a //class//, and not a //object//. In a sense, //methods// are to //objects// what //static methods// are to //classes//. Objects don't see static methods, but classes do. You'll notice when authoring a static method, that you //don't// use {{{self}}} as the first argument, since an instanced object //won't// be passing a reference of itself to the static method. And after you define the 'new static method function', you need to call the {{{staticmethod()}}} function //on it//. This (apparently) 'registers' the function //as// a static method with the class. {{{ # let's make a static method, previous to Python v2.4: class FooClass(): total = 0 def status(): print "Total number is", FooClass.total status = staticmethod(status) # etc... }}} {{{ # let's make a static method via a decorator, Python v2.4 or later: class FooClass(object): total = 0 # This is a 'declaration syntax', that describes the function # that follows. The @ symbol is known as a 'decorator': @staticmethod def status(): print "Total number is", FooClass.total # etc... }}} Python supports stream redirection. Not a special feature of Python, it just supports what the OS already provides. {{{ # spam.py print "I can print to a file, really!" }}} Then, at the prompt: {{{ % python spam.py > saveFile.txt }}} {{{saveFile.txt}}} now has the output from {{{spam.py}}} Using the {{{print}}} statement in your [[module|Module]] will allow the output of that data to a text file. Strings are effectively sequences (tupples) of one character strings. They are //immutable//. Meaning after creation, their individual indicies (characters) can't be changed. They support many list operations though. {{{ myString = "foo" # could be similarly written as a tupple: myTupple = ("f", "o", "o") print myString[1] >>> o print myTupple[1] >>> o myString[2] = "g" # fail: It is an immutable sequence Traceback (most recent call last): File "<string>", line 1, in <string> TypeError: 'str' object does not support item assignment # BUT, you can *redefine* it successfully: myString = myString[:-1] + "g" print myString >>> fog }}} Also see *[[String methods]] *[[Sequence Types info|http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange]] {{{ >>> "hello"+"world" "helloworld" # concatenation }}} {{{ >>> "hello"*3 "hellohellohello" # repetition }}} {{{ >>> "hello"[0] "h" # indexing }}} {{{ >>> "hello"[-1] "o" # (from end) }}} {{{ >>> "hello"[1:4] "ell" # slicing }}} {{{ >>> len("hello") 5 # size }}} {{{ >>> "hello" < "jello" 1 # comparison }}} {{{ >>> "e" in "hello" 1 # search }}} {{{ """ multi line text """ }}} {{{ >>> mystring = "foo foo" >>> print mystring.replace("foo", "goo") # search / replace goo goo }}} By default, a string will add a 'newline' character at the end. However, if you want to //suppress// this newline character, you can just add a comma '{{{,}}}' after your string: {{{ >>> print "no newline", }}} ---- [[String Formatting|http://docs.python.org/library/stdtypes.html#string-formatting]] {{{ # very simple example: >>> foo = "happy" >>> goo = "sad" >>> print "%s is not %s" % (foo, goo) happy is not sad }}} Using a dictionary ({{{locals()}}}): {{{ foo = "happy" goo = "sad" print "%(foo)s is not %(goo)s" % locals() }}}  Documentation for string methods? http://docs.python.org/library/stdtypes.html#string-methods Or, get it in Python: {{{ >>> stringFoo = "this is a string" >>> for thing in dir(stringFoo): >>> print thing capitalize center count decode encode endswith expandtabs find index isalnum isalpha isdigit islower isspace istitle isupper join ljust lower lstrip partition replace rfind rindex rjust rpartition rsplit rstrip split splitlines startswith strip swapcase title translate upper zfill }}}  http://www.python.org/dev/peps/pep-0008/ If you have questions about the proper and acceptable ways to write you code, look there. good stuff. Google has their own Python style guide: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html  WikiEditingInfo Found a couple different solutions online. One had already been converted to Python, and I converted the other. I wanted to see which was faster, so I did a simple unit test that can be performed. My result are shown at the bottom, but it looks like {{{pntInPoly()}}} is about 20% faster than {{{point_inside_polygon()}}}. {{{ # 2dPointInPoly.py def point_inside_polygon(point, poly): """ Copied from here: http://www.ariel.com.au/a/python-point-int-poly.html Which was adapted from the c code here: http://paulbourke.net/geometry/insidepoly/ point : (x,y) point to test poly : list of (x,y) vertex positions return : True/False """ x,y = point n = len(poly) inside = False p1x,p1y = poly[0] for i in range(n+1): p2x,p2y = poly[i % n] if y > min(p1y,p2y): if y <= max(p1y,p2y): if x <= max(p1x,p2x): if p1y != p2y: xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x if p1x == p2x or x <= xinters: inside = not inside p1x,p1y = p2x,p2y return inside def pntInPoly(point, verts): """ Adapted from c code here: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html point : (x,y) point to test verts : list of (x,y) vertex positions return : True/False """ c = False vertx = [xy[0] for xy in verts] verty = [xy[1] for xy in verts] j = len(verts) -1 for i in range(len(verts)): if (verty[i] > point[1]) != (verty[j]>point[1]) and \ (point[0] < (vertx[j]-vertx[i]) * (point[1]-verty[i]) / (verty[j]-verty[i]) + vertx[i]): c = not(c) j = i return c if __name__ == "__main__": """ Test! """ pointA = (.5, .5) # middle of both pointB = (-2, 0) # outside of both pointC = (.2, .8) # inside square, outside diamond square = ((0,0), (0,1), (1,1), (1,0)) diamond = ((0,.5), (.5,1), (1,.5), (.5,0)) print "--------------------" print "pointA (middle of both):", pointA print "pointB (outside of both):", pointB print "pointC (inside square, outside diamond):", pointC print "square:", square print "diamond:", diamond print "--------------------" print "point_inside_polygon:" print "A in square:", point_inside_polygon(pointA, square) print "B in square:", point_inside_polygon(pointB, square) print "C in square:", point_inside_polygon(pointC, square) print "A in diamond:", point_inside_polygon(pointA, diamond) print "B in diamond:", point_inside_polygon(pointB, diamond) print "C in diamond:", point_inside_polygon(pointC, diamond) print "--------------------" print "pntInPoly:" print "A in square:", pntInPoly(pointA, square) print "B in square:", pntInPoly(pointB, square) print "C in square:", pntInPoly(pointC, square) print "A in diamond:", pntInPoly(pointA, diamond) print "B in diamond:", pntInPoly(pointB, diamond) print "C in diamond:", pntInPoly(pointC, diamond) print "--------------------" import time start = time.time() for i in range(1000): point_inside_polygon(pointA, square) end = time.time() total = end - start print ("Completed 1000 iterations of point_inside_polygon() in %.3f seconds, %.5f sec. per test."+ "")%(total, total/1000.0) start = time.time() for i in range(1000): pntInPoly(pointA, square) end = time.time() total = end - start print ("Completed 1000 iterations of pntInPoly() in %.3f seconds, %.5f sec. per test."+ "")%(total, total/1000.0) }}} My results: (Macbook Air - 2.13 ~GHz Intel Core 2 Duo, 4 gb ram) {{{ -------------------- pointA (middle of both): (0.5, 0.5) pointB (outside of both): (-2, 0) pointC (inside square, outside diamond): (0.2, 0.8) square: ((0, 0), (0, 1), (1, 1), (1, 0)) diamond: ((0, 0.5), (0.5, 1), (1, 0.5), (0.5, 0)) -------------------- point_inside_polygon: A in square: True B in square: False C in square: True A in diamond: True B in diamond: False C in diamond: False -------------------- pntInPoly: A in square: True B in square: False C in square: True A in diamond: True B in diamond: False C in diamond: False -------------------- Completed 1000 iterations of point_inside_polygon() in 0.050 seconds, 0.00005 sec. per test. Completed 1000 iterations of pntInPoly() in 0.042 seconds, 0.00004 sec. per test. }}} Python docs: *http://docs.python.org/library/tkinter.html#module-Tkinter : Main docs *http://docs.python.org/library/tix.html#module-Tix : Tix is a Tkinter extension *http://docs.python.org/library/turtle.html#module-turtle : Turtle graphics in Tkinter ---- Other web: *[[Tk in Wikipedia|http://en.wikipedia.org/wiki/Tk_%28computing%29]] *http://effbot.org/tkinterbook/ *http://wiki.python.org/moin/TkInter *http://www.pythonware.com/library/tkinter/introduction/ *http://infohost.nmt.edu/tcc/help/pubs/tkinter/ (recommended) *http://tkinter.unpythonic.net/wiki/ /*** Contains the stuff you need to use Tiddlyspot Note you must also have UploadPlugin installed ***/ //{{{ // edit this if you are migrating sites or retrofitting an existing TW config.tiddlyspotSiteId = 'pythonwiki'; // make it so you can by default see edit controls via http config.options.chkHttpReadOnly = false; window.readOnly = false; // make sure of it (for tw 2.2) window.showBackstage = true; // show backstage too // disable autosave in d3 if (window.location.protocol != "file:") config.options.chkGTDLazyAutoSave = false; // tweak shadow tiddlers to add upload button, password entry box etc with (config.shadowTiddlers) { SiteUrl = 'http://'+config.tiddlyspotSiteId+'.tiddlyspot.com'; SideBarOptions = SideBarOptions.replace(/(<<saveChanges>>)/,"$1<<tiddler TspotSidebar>>");
OptionsPanel = OptionsPanel.replace(/^/,"<<tiddler TspotOptions>>");
DefaultTiddlers = DefaultTiddlers.replace(/^/,"[[WelcomeToTiddlyspot]] ");
}

// create some shadow tiddler content

'WelcomeToTiddlyspot':[
"This document is a ~TiddlyWiki from tiddlyspot.com.  A ~TiddlyWiki is an electronic notebook that is great for managing todo lists, personal information, and all sorts of things.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //What now?// &nbsp;&nbsp;@@ Before you can save any changes, you need to enter your password in the form below.  Then configure privacy and other site settings at your [[control panel|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/controlpanel]] (your control panel username is //" + config.tiddlyspotSiteId + "//).",
"<<tiddler TspotControls>>",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Working online// &nbsp;&nbsp;@@ You can edit this ~TiddlyWiki right now, and save your changes using the \"save to web\" button in the column on the right.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Working offline// &nbsp;&nbsp;@@ A fully functioning copy of this ~TiddlyWiki can be saved onto your hard drive or USB stick.  You can make changes and save them locally without being connected to the Internet.  When you're ready to sync up again, just click \"upload\" and your ~TiddlyWiki will be saved back to tiddlyspot.com.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Help!// &nbsp;&nbsp;@@ Find out more about ~TiddlyWiki at [[TiddlyWiki.com|http://tiddlywiki.com]].  Also visit [[TiddlyWiki.org|http://tiddlywiki.org]] for documentation on learning and using ~TiddlyWiki. New users are especially welcome on the [[TiddlyWiki mailing list|http://groups.google.com/group/TiddlyWiki]], which is an excellent place to ask questions and get help.  If you have a tiddlyspot related problem email [[tiddlyspot support|mailto:support@tiddlyspot.com]].",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Enjoy :)// &nbsp;&nbsp;@@ We hope you like using your tiddlyspot.com site.  Please email [[feedback@tiddlyspot.com|mailto:feedback@tiddlyspot.com]] with any comments or suggestions."
].join("\n"),

'TspotControls':[
"| links:|[[tiddlyspot.com|http://tiddlyspot.com/]], [[FAQs|http://faq.tiddlyspot.com/]], [[blog|http://tiddlyspot.blogspot.com/]], email [[support|mailto:support@tiddlyspot.com]] & [[feedback|mailto:feedback@tiddlyspot.com]], [[donate|http://tiddlyspot.com/?page=donate]]|"
].join("\n"),

'TspotSidebar':[
].join("\n"),

'TspotOptions':[
""
].join("\n")

});
//}}}

See [[Sequence Types|http://www.python.org/doc/current/lib/typesseq.html]]
A tuple is an arbitrary collection of values which can be treated as a unit. In many ways a tuple is like a list, but with the significant difference that tuples are immutable which is to say that you can’t change them nor append to them once created. Tuples are represented by parentheses containing a comma separated list of values:
{{{
>>> key = (lastname, firstname)
>>> point = x, y, z # parenthesis optional
>>> x, y, z = point
>>> singleton = (1,) # trailing comma!
>>> empty = () # parentheses!
}}}
tuples vs. lists: tuples, like strings, are immutable (Can't be changed in-place)
Often times I'll want to type-check an object to make sure its what I'm expecting.  In the past I'd do something like this:
{{{
myVar = []
if type(myVar) is type([]):
# If myVar is the same type as a list, do something...
print type(myVar)

# myVar is a list!
}}}
This always seemed a bit clunky though, comparing an object to a new one built at the time of comparison.
This also breaks down if your object is a sublcass of what you're trying to compare against:
{{{
import types
class MyList(types.ListType):
pass
foo = MyList()
foo.append("index0")
print foo
if type(foo) is type([]):
print "foo is a list!"
else:
print "foo is not a list..."

# ['index0']
# foo is not a list...
}}}
The {{{isinstance}}} built-in function combined with the {{{types}}} module handles this much better:
{{{
import types
class MyList(types.ListType):
pass
foo = MyList()
if isinstance(foo, types.ListType):
print "foo is a list!"

# foo is a list!
}}}
----
Also see:
*[[string type comparisons]]

There's more than one program you can use to make UI's with Python, but Tkinter ships with it, and appears to be the most common.

How to pronounce?  Good question.  I've been searching and it seems to be all over the place:
*Tee-kinter
*Tink-ter
*~Tee-Kay-inter - I'm leaning towards this one, since this is the '~Tk-Interface'
''Concepts:''
*You create UI elements by instancing objects from the classes of the Tkinter module.
*There can only be one root window in a Tkinter program.
*//Widgets// are customizable classes ({{{Button}}}, {{{Frame}}}, etc).
*//Options// are 'keyword arguments':  {{{text="spam"}}}
*//Compositions// refer to object embedding, not pathnames: {{{Label(Top,...)}}}
*UI's are 'event driven' programs.
*'event':  Things that happen when accessing the program's objects.  For example, 'pressing a button' could be an event.
*'event handler':  Code that runs when an event occurs.  For example, the code that executes when the button is pressed.
*You 'bind' (or, associate) //events// to //event handlers//.
*The program runs in an 'event loop', where it waits for the event's you've authored to occur.
*When you execute a UI, it will also open an accompanying console window.  If you //don't// want this to happen, (on Windows) rename the extension from {{{.py}}} to {{{.pyw}}}
*When making a widget, you //don't// have to assign it to a variable name:  You only need to if you later need to access the data.
*Widgets are positioned with one of the three //geometry managers//: {{{Place}}}, {{{Pack}}}, or {{{Grid}}}.  These managers can be called with methods {{{place}}}, {{{pack}}}, {{{grid}}}, available in every Widget.
''UI Tree''
*root window
**frame (a widget, that can hold other widgets)
*** widgets (labels, buttons, etc)
''Example UI #1'' (very simple)':
{{{
# import all the functions from Tkinter into the default
# namespace.  This is ok, it's designed to do that:
from Tkinter import *

# create root window
root = Tk()
root.title("My Window")
root.geometry("200x85")

# create a frame in the window to hold other widgets
frm = Frame(root)

# grid is a method all widgets have, which is associated
# with a layout manager.  Accessing a .grid method
# makes the widget visible
frm.grid()

# create a button in the frame
bttn1 = Button(frm, text = "Button1")
bttn1.grid()

# create a second button in the frame, using
# the .configure method of Button, which lets
# you modify it after the fact:
bttn2 = Button(frm)
bttn2.grid()
bttn2.configure(text = "Button2")

# create a third button in the frame using
# Buttons "text" option, which acts like
# a dictionary:
bttn3 = Button(frm)
bttn3.grid()
bttn3["text"] = "Button3"

# kick off the window's event loop
root.mainloop()
}}}
''Example UI #2'' (Class based)
I pulled and modified this example from 'Python Programming for the absolute beginner".  It makes a window that has a field to enter some text, and when you press the button, it fills the lower text box with that text.
{{{
# Many places say it's just fine to 'import *', but I'm sucker for namespaces:
import Tkinter as tk

class App(tk.Tk):
"""
This class is a subclass of the Tk object.
"""

def __init__(self):
"""
Create the window, dispaly its contents, start the event loop.
"""
tk.Tk.__init__(self)
self.title("TkWin 02")
self.geometry("250x150")
self.populate()
self.mainloop()

def populate(self):
"""
The guts of the window go in here:
"""
self.frame = tk.Frame(self)
self.frame.grid()

self.label = tk.Label(self.frame, text="This is a top label that spans multiple columns")
self.label.grid(row=0, column=0, columnspan=2, sticky=tk.W)

self.someText = tk.Label(self.frame, text="Enter something:")
self.someText.grid(row=1, column=0, sticky=tk.W)

self.entry = tk.Entry(self.frame)
self.entry.grid(row=1, column=1, sticky=tk.W)

self.b1 = tk.Button(self.frame, text="Press!", command=self.update_text)
self.b1.grid(row=2, column=0, sticky=tk.W)

self.textBox = tk.Text(self.frame, width=35, height=5, wrap=tk.WORD)
self.textBox.grid(row=3, column=0, columnspan=2, sticky=tk.W)

def update_text(self):
text = self.entry.get()
self.textBox.insert(0.0, text)

if __name__ == "__main__":
App()
}}}
Tk widgets have a {{{grid()}}} method (in addition to {{{place}}} and {{{pack}}} methods), based on the {{{Grid}}} //geometry manager//.  This helps display the widget in the {{{Frame}}}.  Common values include:
*{{{row}}} :  which row the widget lives in (0 based index)
*{{{column}}} :  which column the widget lives in (0 based index)
*{{{columnspan}}} :  how many columns the widget spans
*{{{sticky}}} :  How to justify a widget in a cel (N, S, E, W.  These stand for North, South, East, & West)
*...more...
Example:
{{{
but = Button(someFrame, row = 0, column = 0, columnspan = 2, sticky = W)
}}}
Questions:
*Are grids 3x3 by default?
Also see:
*[[UI basics with Tkinter]]
*[[Program]]  :  Are the //concepts// behind implementing a idea. They are usually made up of one or more Modules
*[[module]] (a file saved on disk, usually with a {{{.py}}} extension)
**[[Statement]]
**[[Function]] (executed)
**[[Function]] (defined)
***[[argument]]
***[[return]]
***[[Statement]]
***[[Function]] (executed)
***[[Function]] (defined.  Yup, you can have functions nested in functions)
**[[Class]]   (also see [[Class - advanced]])
***[[Method]] (defined.  Methods //are// functions.  But when defined in a Class, the become 'methods')
***[[Function]] (executed)

Since as of this authoring I'm an xml noob, overview:
----
http://cnx.org/eip-help/terminology
http://www.xmlnews.org/docs/xml-basics.html
----
Some Python docs on dom.minidom {{{Document}}} objects:
http://docs.python.org/library/xml.dom.html#document-objects
----
''Tags, Attributes, & Elements'':
>"XML __tags__ begin with the less-than character (“{{{<}}}”) and end with the greater-than character (“{{{>}}}”). You use tags to mark the start and end of __elements__, which are the logical units of information in an XML document."
>"An __element__ consists of a start __tag__, possibly followed by text and other complete elements, followed by an end __tag__."
{{{tags}}} in action:
{{{
<tagA>
<tagB> text </tagB>
</tagA>
}}}
Above, {{{tagA}}} in its completeness is an //element//.  {{{tagB}}} is also an //element//.
Introducing __{{{attribute}}}__ names, and values:
{{{
<tagA>
<tagB attrName="attrValue"> text </tagB>
</tagA>
}}}
Attribute names are never in quotes, while attribute values are //always// in quotes.  Also, attributes and elements are //case sensitive//.
Tags and text can be intermixed (but tags //can't// be overlapped with other tags):
{{{
<tagA>
some text <tagB> tagB text </tagB>
<tagC> <tagD> stuff </tagD> </tagC> text text text
</tagA>
}}}
The above could be viewed as such:
*(tagA)
**some text
**(tagB)
***text
**(tagC)
***(tagD)
****stuff
**text text text
There can only ever be one root element.  The next example is invalid:
{{{
<tagA> text </tagA>
<tagB> text </tagB>
}}}
This would fix it:
{{{
<root>
<tagA> text </tagA>
<tagB> text </tagB>
</root>
}}}
----
Surrounding text with {{{<!--  -->}}} will cause the xml to igore the markup:
{{{
<!-- This is a comment that will be ignored -->
}}}
----
''Declaration'':
{{{
<?xml version="1.0"?>
}}}
----
Pulled from:
http://www.flightlab.com/~joe/sgml/faq-not.txt
----
{{{
Q.  I've tried reading the (XML | SGML | XSL | XPATH | DSSSL | ...)
specification, but it doesn't make any sense!  There's too
much jargon!

A.  Specification authors deliberately obfuscate the text of
ISO and W3C standards to ensure that normal people
(e.g., Perl programmers) can't use the technology without
assistance from the so-called "experts" who designed the
specs.

Fortunately, there is a handy translation table you can use:

--------------------------------------------------
ISO/W3C terminology			Common name
--------------------------------------------------
attribute				tag
attribute value			tag
attribute value literal		tag
attribute value specification	tag
character reference			tag
comment				tag
comment declaration 		tag
declaration				tag
document type declaration		tag
document type definition		tag
element				tag
element type			tag
element type name			tag
entity				tag
entity reference			tag
general entity			tag
generic identifier			tag
literal				tag
numeric character reference		tag
parameter entity			tag
parameter literal			tag
processing instruction		tag
tag					command
--------------------------------------------------

With the help of this table, even Visual Basic programmers
should have no trouble deciphering ISO prose.
}}}
According to the [[Python Docs|http://docs.python.org/lib/specialattrs.html]],  {{{__dict__}}} is:
*"A dictionary or other mapping object used to store an object's (writable) attributes."
I believe this could also be considered a 'symbol table'.
But how is it used, and what for?

Like expressed above, {{{__dict__}}} holds information (attributes) on a given //object//.  It is a dictionary of //all// the available data the object exposes to the user.  Simple example:
{{{
class MyObject(object):
def __init__(self):
self.first = 1
self.second = 2
third = 42

larry = MyObject()
print larry.__dict__
# {'second': 2, 'first': 1}
larry.goof = 52
print larry.__dict__
# {'goof': 52, 'second': 2, 'first': 1}
}}}
We create an object called {{{MyObject}}}.  We give it two attributes, {{{.first}}} and {{{.second}}} and one //local variable// {{{third}}} (see notes on [[variable scope|Python variable scope]]).  After we instance {{{MyObject}}} as {{{larry}}}, we print {{{larry}}}'s {{{__dict__}}}, exposing the inside of our object, to the attributes it contains.  We then create a //new// attribute {{{.goof}}}, and then reprint {{{__dict__}}} proving it exists.
----
Also see:
*[[Special Attributes]]
*[[How can I query the variable names of a defined scope?]]  ( {{{vars()}}} )
{{{__repr__}}} & {{{__str__}}} are special methods that objects possess.

I have read in some cases {{{__repr__}}} is the result given from a function that has "full precision", to be 'passed on' to other functions', while {{{__str__}}} is designed to be more human-readable.

But, this isn't what the [[official docs say|http://docs.python.org/reference/datamodel.html#object.__repr__]] for {{{__repr__}}}:
<<<
"Called by the {{{repr()}}} built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, ''this should look like a valid Python expression that could be used to recreate an object with the same value'' (given an appropriate environment). If this is not possible, a string of the form {{{<...some useful description...>}}} should be returned. The return value must be a string object. If a class defines {{{__repr__()}}}  but not {{{__str__()}}}, then {{{__repr__()}}}  is also used when an “informal” string representation of instances of that class is required."
<<<
So to me, {{{__repr__}}} really means 'representation', and I've heard it called the 'reproduction string'.

And, [[official docs|http://docs.python.org/reference/datamodel.html#object.__str__]] for {{{__str__}}}:
<<<
"Called by the {{{str()}}} built-in function and by the {{{print}}} statement to compute the “informal” string representation of an object. This differs from {{{__repr__()}}}  in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object."
<<<

What does that actually mean?  Example:
{{{
class Spam(object):
def __init__(self, val):
self.val = val
def __repr__(self):
return "<Spam(%s)>"%self.val
def __str__(self):
return str(self.val)

eggs = Spam(4)
print repr(eggs)
print eggs.__repr__()
print eggs
print eggs
}}}
{{{
<Spam(4)>
<Spam(4)>
<Spam(4)>
4
}}}
{{{repr}}} can also be called to via back-tick: {{{}}} around the variable.
----
Also see:
*[[Special Methods]]

From the docs http://docs.python.org/library/exceptions.html:
>Exceptions should be class objects. The exceptions are defined in the module {{{exceptions}}}.
Python "Tutorial" on Exceptions [[here|http://docs.python.org/tut/node10.html]].
''User defined exception'' docs: http://docs.python.org/tutorial/errors.html#user-defined-exceptions

In a nutshell, an exception is a way to catch an error, and do something with that knowledge.

Python docs on the {{{try}}} statement [[here|http://docs.python.org/reference/compound_stmts.html#the-try-statement]]
I read this in some [[Python docs|http://docs.python.org/howto/doanddont.html#exceptions]], and I think it's a pretty good thing to go by:
>Exceptions are a useful feature of Python. You should learn to raise them whenever something unexpected occurs, and catch them only where you can do something about them.
{{{
try:
python code
except:
do something else
}}}
The base {{{except}}} will handle ALL different kinds of exceptions. It can get a lot more detailed, and you can raise an exception on very specific things. Here's another example, from the tutorial:
{{{
while True:
try:
x = int(raw_input("Please enter a number: "))
break
except ValueError:
print "Oops!  That was no valid number.  Try again..."
}}}
As you can see, in this case the exception will only happen if it's a "~ValueError".

You can also use {{{raise(<someException(<someDescription>)>)}}} in your code:
{{{
if(True):
raise(Exception("This is true, and I'm tossing an exception"))
}}}
The except clause can also take a variable after the exception name.  See the 'Tutorial' link above for more on this:
{{{
try:
# something...
except Exception, inst:
print inst
}}}
----
You can make your own exception types very easily.  Here is about as simple as it can get:
{{{
class MyException(Exception):
pass
}}}
And to detect for it:
{{{
try:
foo = 23
except MyException:
print "custom exception!"
}}}
----
Also see:
*[[How do I catch \ handle exceptions?]]

{{{
filter(function or None, sequence) -> list, tuple, or string
}}}
Returns those items in the provided sequence that are true based on the passed in function. If no function is given (None), then the data type is compared against true|false testing.
{{{
# return the numbers divisible by three:
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print filter(lambda x: x % 3 ! 0, foo)
[18, 9, 24, 12, 27]
}}}
So, if our lambda function returns back something cleanly divisible by zero, the filter is true, so the current item in foo we're checking is added to our list of things to return.

Docs:
*http://docs.python.org/reference/expressions.html#yieldexpr
*http://docs.python.org/reference/simple_stmts.html#yield
Generators are created by adding 'yield' to a function loop:
The function itself looks something like this:
{{{
def gen(things):
for thing in things:
# ...do stuff with things...
yield newThing
}}}
or:
{{{
def gen(someArgs):
while someCondition:
# ...do stuff with things...
yield someItem
}}}
Generators have a default .next method you can call to advance in the loop and 'yield' (return) the next item:
{{{
stuff = [my, list, of, stuff]
g = gen(stuff)
g.next()
}}}
When the generator runs past the last item to iterate on, it raises a {{{StopIteration}}} exception.
----
For example, make a generator that returns the square of a number, counting up from {{{1}}}, each time you call to it.
{{{
def squares(start=1):
i=start
while True:
number = i
square = i**2
i += 1
yield square, number
}}}
{{{
squared = squares()
print squared.next()
print squared.next()
print squared.next()
}}}
{{{
(1, 1)
(4, 2)
(9, 3)
}}}
Generator objects are also iterators which you can loop over:
{{{
squared = squares()
num = 0
sq = []
for item in squared:  # this will run forever...
sq.append(item[0])
num += 1
if num >= 10:
break  # So we need a way to stop it:
print sq
}}}
{{{
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
}}}
Or:
{{{
squared = squares()
sq = []
for i in range(10):
sq.append(squared.next()[0])
print sq
}}}
{{{
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
}}}

Referenced I pulled from is [[here|http://www.secnetix.de/~olli/Python/lambda_functions.hawk]]
{{{
lambda arg(s): 'code arg(s)'
}}}
Why use lambda's?  The next part makes a bit of sense:
>Lambda's are [[expressions|Statement]] that generate a new [[Function]] to be called later (//similar// to a function definition statement (def), but different).  Since they are [[expressions|Statement]], they can be used in places that [[Function]] definitions can not, such as within list and dictionaries.
{{{
double = lambda x: 2*x
double(4)
8
}}}
So as you can see, 'lambda' is the name of your virtual function, the next character is one or more [[argument]]s to be passed in, and after the colon ':' comes the bulk of the function.
{{{
# our lambda:
lFunc = lambda x, y: x*y
print lFunc(2,5)
# 10

# do it with a function def:
def dFunc(x,y):
return x*y
print dFunc(2,5)
# 10
}}}
From above, in the lambda we assign the anonymous function to variable {{{lFunc}}}, then call to {{lFfunc}}} with arguments.
In the def, we create a //function objec//t called {{{dFunc}}}, then call to {{{dFunc}}} with arguments.
----
Other Examples:
{{{
sentence = 'It is raining cats and dogs'
words = sentence.split()
print words
['It', 'is', 'raining', 'cats', 'and', 'dogs']

# this uses lambda in map:
lengths = map(lambda word: len(word), words)
print lengths
[2, 2, 7, 4, 3, 4]
}}}
----
To expand on my reason behind using Lambda's above, here is an example that creates a list of functions (thanks to lambda) that will raise the passed in arg to the power of the index of the list.  Each index of the list is its own function:
{{{
def makeFuncs():
ret = []
for i in range(4):
ret.append(lambda x, i=i: i ** x)
return ret
funcs = makeFuncs()

print funcs[0](2)
# 0
print funcs[1](2)
# 1
print funcs[2](2)
# 4
print funcs[3](2)
# 9
print funcs[4](2)
# 16
}}}
The key to this working is the '{{{, i=i: }}}' part:  If it was just '{{{i}}}', every Lambda would have the same {{{i}}} value when {{{makeFuncs()}}} loop was complete, since they'd all be pointing to the //same// {{{i}}} object.  But by setting up a [[default argument|Default Arguments]], it makes sure that each loop has its own unique value.
----
Lambda's can also be used in callbacks in Tkinter widgets:  When authoring callbacks for things like buttons, you can author a lambda as the command, rather than having an external function being called too:
{{{
import sys
# insert window code...
bttn1 = Button(text = "U press now", command=(lambda:sys.stdout.write('Spam\n')))
}}}
{{{
map(function, sequence[, sequence, ...]) -> list
}}}
Map runs a function on a list, and returns back a new list. I am told this is faster than a loop.
{{{
numbers = [1, 2, 3, 4]
return x + x
[2, 4, 6, 8]
}}}
Map can also take more than one sequence arugment.
{{{
letters = ["a", "b", "c", "d"]
nums = ["1", "2", "3", "4"]
return x+y
['1a', '2b', '3c', '4d']
}}}
Finally, if no function argument is listed ({{{None}}}), it will combine the arguments as tuples in the result:
{{{
map(None, nums, letters)
[('1', 'a'), ('2', 'b'), ('3', 'c'), ('4', 'd')]
}}}
Here's one that will print the length of every word in a sentence.. It 'splits' the string into it's individual words, then map passes that to the lambda to find it's length, which is then added to our returned list:
{{{
print map(lambda w: len(w), 'It is raining cats and dogs'.split())
[2, 2, 7, 4, 3, 4]
}}}
Also see:
*[[How can I loop through multiple lists?]]
{{{
reduce(function, sequence[, initial]) -> value
}}}
Reduce is a tricky one. From the docs: Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value.
{{{
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print reduce(lambda x, y: x + y, foo)
139
}}}
This is equal to
{{{
((((((((2+18)+9)+22)+17)+24)+8)+12)+27)
}}}
Also, if the 'initial' argument is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |
| 13/05/2013 11:58:45 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 13/05/2013 13:20:03 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . | ok |
| 13/05/2013 13:48:18 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . | ok |
| 13/05/2013 13:48:39 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . | ok |
| 13/05/2013 14:04:12 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 13/05/2013 15:28:35 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 17/05/2013 09:27:41 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . | ok |
| 17/05/2013 09:27:54 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 17/05/2013 10:26:11 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 17/05/2013 13:36:43 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
/***
|''Description:''|Extends TiddlyWiki options with non encrypted password option.|
|''Version:''|1.0.2|
|''Date:''|Apr 19, 2007|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''~CoreVersion:''|2.2.0 (Beta 5)|
***/
//{{{
major: 1, minor: 0, revision: 2,
date: new Date("Apr 19, 2007"),
author: 'BidiX (BidiX (at) bidix (dot) info',
coreVersion: '2.2.0 (Beta 5)'
};

merge(config.macros.option.types, {
'pas': {
elementType: "input",
valueField: "value",
eventName: "onkeyup",
className: "pasOptionInput",
create: function(place,type,opt,className,desc) {
config.macros.option.genericCreate(place,'pas',opt,className,desc);
config.macros.option.genericCreate(place,'chk','chk'+opt,className,desc);
},
onChange: config.macros.option.genericOnChange
}
});

merge(config.optionHandlers['chk'], {
get: function(name) {
// is there an option linked with this chk ?
var opt = name.substr(3);
if (config.options[opt])
return config.options[name] ? "true" : "false";
}
});

merge(config.optionHandlers, {
'pas': {
get: function(name) {
if (config.options["chk"+name]) {
} else {
return "";
}
},
}
});

/*

merge(config.optionsDesc,{
});
*/
//}}}

/***
|''Description:''|Save to web a TiddlyWiki|
|''Version:''|4.1.0|
|''Date:''|May 5, 2007|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''~CoreVersion:''|2.2.0 (#3125)|
***/
//{{{
major: 4, minor: 1, revision: 0,
date: new Date("May 5, 2007"),
author: 'BidiX (BidiX (at) bidix (dot) info',
coreVersion: '2.2.0 (#3125)'
};

//
// Environment
//

if (!window.bidix) window.bidix = {}; // bidix namespace
bidix.debugMode = false;	// true to activate both in Plugin and UploadService

//
//

// default values
defaultBackupDir: '',	//no backup
defaultStoreScript: "store.php",
defaultToFilename: "index.html",
authenticateUser: true	// UploadService Authenticate User
};

promptParamMacro: "Save and Upload this TiddlyWiki in %0",
saveLabel: "save to web",
saveToDisk: "save to disk",
};

noStoreUrl: "No store URL in parmeters or options",
};

return;
var label;
if (document.location.toString().substr(0,4) == "http")
label = this.label.saveLabel;
else
var prompt;
if (params[0]) {
prompt = this.label.promptParamMacro.toString().format([this.destFile(params[0],
(params[1] ? params[1]:bidix.basename(window.location.toString())), params[3])]);
} else {
prompt = this.label.promptOption;
}
createTiddlyButton(place, label, prompt, function() {config.macros.upload.action(params);}, null, null, this.accessKey);
};

{
// for missing macro parameter set value from options
var storeUrl = params[0] ? params[0] : config.options.txtUploadStoreUrl;
var toFilename = params[1] ? params[1] : config.options.txtUploadFilename;
var backupDir = params[2] ? params[2] : config.options.txtUploadBackupDir;
// for still missing parameter set default value
if ((!storeUrl) && (document.location.toString().substr(0,4) == "http"))
if (storeUrl.substr(0,4) != "http")
storeUrl = bidix.dirname(document.location.toString()) +'/'+ storeUrl;
if (!toFilename)
toFilename = bidix.basename(window.location.toString());
if (!toFilename)
if (!backupDir)
if (!storeUrl) {
clearMessage();
return false;
}
clearMessage();
return false;
}
return false;
};

{
if (!storeUrl)
return null;
var dest = bidix.dirname(storeUrl);
dest = dest + '/' + uploadDir;
dest = dest + '/' + toFilename;
return dest;
};

//
//

handler: function(place,macroName,params) {
var wizard = new Wizard();
wizard.createWizard(place,this.wizardTitle);
var markList = wizard.getElement("markList");
var listWrapper = document.createElement("div");
markList.parentNode.insertBefore(listWrapper,markList);
wizard.setValue("listWrapper",listWrapper);
this.refreshOptions(listWrapper,false);
if (document.location.toString().substr(0,4) == "http")
else

wizard.setButtons([
{caption: this.cancelButton, tooltip: this.cancelButtonPrompt, onClick: this.onCancel}

]);
},
refreshOptions: function(listWrapper) {
]
var opts = [];
var opt = {};
opts.push()
opt.option = "";
opt.name = n;
opt.lowlight = !config.optionsDesc[n];
opt.description = opt.lowlight ? this.unknownDescription : config.optionsDesc[n];
opts.push(opt);
}
var listview = ListView.create(listWrapper,opts,this.listViewTemplate);
for(n=0; n<opts.length; n++) {
var type = opts[n].name.substr(0,3);
var h = config.macros.option.types[type];
if (h && h.create) {
h.create(opts[n].colElements['option'],type,opts[n].name,opts[n].name,"no");
}
}

},
onCancel: function(e)
{
backstage.switchTab(null);
return false;
},

step1Html: "<input type='hidden' name='markList'></input><br>",
cancelButton: "Cancel",
cancelButtonPrompt: "Cancel prompt",
listViewTemplate: {
columns: [
{name: 'Description', field: 'description', title: "Description", type: 'WikiText'},
{name: 'Option', field: 'option', title: "Option", type: 'String'},
{name: 'Name', field: 'name', title: "Name", type: 'String'}
],
rowClasses: [
{className: 'lowlight', field: 'lowlight'}
]}
}

//
//

//from saving
invalidFileError: "The original file '%0' does not appear to be a valid TiddlyWiki",
backupSaved: "Backup saved",
backupFailed: "Failed to upload backup file",
emptyFailed: "Failed to upload empty template file",
mainFailed: "Failed to upload main TiddlyWiki file. Your changes have not been saved",
};

{
if (!status) {
return;
}
if (bidix.debugMode)
// Locate the storeArea div's
var posDiv = locateStoreArea(original);
if((posDiv[0] == -1) || (posDiv[1] == -1)) {
return;
}
};

if(onlyIfDirty && !store.isDirty())
return;
clearMessage();
// save on localdisk ?
if (document.location.toString().substr(0,4) == "file") {
var path = document.location.toString();
var localPath = getLocalPath(path);
saveChanges();
}
// get original
var originalPath = document.location.toString();
// If url is a directory : add index.html
if (originalPath.charAt(originalPath.length-1) == "/")
originalPath = originalPath + "index.html";
if (bidix.debugMode)
if (typeof r == "string")
displayMessage(r);
return r;
};

{
var callback = function(status,params,responseText,url,xhr) {
if(status) {
var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
} else {
}
};
} else {
}
};

{
var callback = function(status,params,responseText,url,xhr) {
if(status) {
// if backupDir specified
if ((params[3]) && (responseText.indexOf("backupfile:") > -1))  {
var backupfile = responseText.substring(responseText.indexOf("backupfile:")+11,responseText.indexOf("\n", responseText.indexOf("backupfile:")));
}
var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
store.setDirty(false);
} else {
}
};
};

{
var localCallback = function(status,params,responseText,url,xhr) {
url = (url.indexOf("nocache=") < 0 ? url : url.substring(0,url.indexOf("nocache=")-1));
if (xhr.status == httpStatus.NotFound)
if ((bidix.debugMode) || (responseText.indexOf("Debug mode") >= 0 )) {
if (responseText.indexOf("Debug mode") >= 0 )
responseText = responseText.substring(responseText.indexOf("\n\n")+2);
} else if (responseText.charAt(0) != '0')
if (responseText.charAt(0) != '0')
status = null;
callback(status,params,responseText,url,xhr);
};
var boundary = "---------------------------"+"AaB03x";
sheader += "--" + boundary + "\r\nContent-disposition: form-data; name=\"";
if (bidix.debugMode)
sheader += "\r\n" + "--" + boundary + "\r\n";
sheader += "Content-Type: text/html;charset=UTF-8" + "\r\n";
sheader += "Content-Length: " + data.length + "\r\n\r\n";
// compose trailer data
var strailer = new String();
strailer = "\r\n--" + boundary + "--\r\n";
data = sheader + data + strailer;
if (typeof r == "string")
displayMessage(r);
return r;
};

// same as Saving's updateOriginal but without convertUnicodeToUTF8 calls
{
if (!posDiv)
posDiv = locateStoreArea(original);
if((posDiv[0] == -1) || (posDiv[1] == -1)) {
return;
}
var revised = original.substr(0,posDiv[0] + startSaveArea.length) + "\n" +
store.allTiddlersAsHtml() + "\n" +
original.substr(posDiv[1]);
var newSiteTitle = getPageTitle().htmlEncode();
revised = revised.replaceChunk("<title"+">","</title"+">"," " + newSiteTitle + " ");
revised = updateMarkupBlock(revised,"PRE-BODY","MarkupPreBody");
revised = updateMarkupBlock(revised,"POST-SCRIPT","MarkupPostBody");
return revised;
};

//
//
//		false : no logging
//		true : logging
//		-1 : no limit
//      0 :  no Log lines but UploadLog is still in place
//		n :  the last n lines are only kept
//		NaN : no limit (-1)

return; // this.tiddler = null
if (!this.tiddler) {
this.tiddler = new Tiddler();
this.tiddler.text = "| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |";
this.tiddler.created = new Date();
this.tiddler.modified = new Date();
}
return this;
};

if (!this.tiddler)
return;
// retrieve maxLine when we need it
if (isNaN(maxLine))
maxLine = -1;
if (maxLine != 0)
this.tiddler.text = this.tiddler.text + text;
// Trunck to maxLine
if (maxLine >= 0) {
var textArray = this.tiddler.text.split('\n');
if (textArray.length > maxLine + 1)
textArray.splice(1,textArray.length-1-maxLine);
this.tiddler.text = textArray.join('\n');
}
// update tiddler fields
this.tiddler.modified = new Date();
// refresh and notifiy for immediate update
story.refreshTiddler(this.tiddler.title);
store.notify(this.tiddler.title, true);
};

if (!this.tiddler)
return;
var now = new Date();
var text = "\n| ";
var filename = bidix.basename(document.location.toString());
if (!filename) filename = '/';
text += now.formatString("0DD/0MM/YYYY 0hh:0mm:0ss") +" | ";
text += config.options.txtUserName + " | ";
text += "[["+filename+"|"+location + "]] |";
text += " [[" + bidix.basename(storeUrl) + "|" + storeUrl + "]] | ";
text += uploadDir + " | ";
text += "[[" + bidix.basename(toFilename) + " | " +toFilename + "]] | ";
text += backupDir + " |";
};

if (!this.tiddler)
return;
};

//
// Utilities
//

bidix.checkPlugin = function(plugin, major, minor, revision) {
var ext = version.extensions[plugin];
if (!
(ext  &&
((ext.major > major) ||
((ext.major == major) && (ext.minor > minor))  ||
((ext.major == major) && (ext.minor == minor) && (ext.revision >= revision))))) {
// write error in PluginManager
if (pluginInfo)
pluginInfo.log.push("Requires " + plugin + " " + major + "." + minor + "." + revision);
eval(plugin); // generate an error : "Error: ReferenceError: xxxx is not defined"
}
};

bidix.dirname = function(filePath) {
if (!filePath)
return;
var lastpos;
if ((lastpos = filePath.lastIndexOf("/")) != -1) {
return filePath.substring(0, lastpos);
} else {
return filePath.substring(0, filePath.lastIndexOf("\\"));
}
};

bidix.basename = function(filePath) {
if (!filePath)
return;
var lastpos;
if ((lastpos = filePath.lastIndexOf("#")) != -1)
filePath = filePath.substring(0, lastpos);
if ((lastpos = filePath.lastIndexOf("/")) != -1) {
return filePath.substring(lastpos + 1);
} else
return filePath.substring(filePath.lastIndexOf("\\")+1);
};

bidix.initOption = function(name,value) {
if (!config.options[name])
config.options[name] = value;
};

//
// Initializations
//

// require PasswordOptionPlugin 1.0.1 or better

// styleSheet

//optionsDesc
merge(config.optionsDesc,{
txtUploadBackupDir: "Relative Directory where to backup the file. If empty no backup. (default: ''(empty))",
});

// Options Initializations

/* don't want this for tiddlyspot sites

// Backstage
});

*/

//}}}


Python docs on this subject are found in the [[site|http://docs.python.org/library/site.html]] module.

The {{{site}}} module is automatically imported when Python launches (this can be suppressed). From the docs, "Importing this module will append site-specific paths to the module search path and add a few builtins."  This includes the parsing of {{{.pth}}} files (see notes [[here|How can I set Python's path?]]).

After all that happens, from the docs:
<<<
After these path manipulations, an attempt is made to import a module named {{{sitecustomize}}}, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an ~ImportError exception, it is silently ignored.
<<<
Then
<<<
After this, an attempt is made to import a module named {{{usercustomize}}}, which can perform arbitrary user-specific customizations, if {{{site.ENABLE_USER_SITE}}} is true (which it is by default). This file is intended to be created in the user site-packages directory (see below), which is part of {{{sys.path}}} unless disabled by -s. An {{{ImportError}}} will be silently ignored.
<<<
Which means, if you have {{{sitecustomize.py}}} or {{{usercustomize.py}}} modules somewhere in your path, they will be automatically imported when Python launches.  This is a great way to have code you always want ran executed when Python launches.

Conceptually, {{{sitecustomize}}} would be be a team-wide module, that everyone shares, while {{{usercustomize}}} is available for the individual user to modify and make use of.
----
Also see:
*[[How can I have Python execute a module before it starts?]]
*[[How can I append to Python's site-packages dir?]]
Python seems to have no built in libraries for doing vector math.  I've done some research into external libraries [[here|Are there vector math libraries in Python?]].

Based on a pile of info I got from the below website, I made a few functions to do this without the overhead of the external libraries.  This is not an exhaustive list of vector functions, just some of the more popular ones.
http://www.geocities.com/SiliconValley/2151/math3d.html
http://mathworld.wolfram.com/topics/VectorAlgebra.html
http://en.wikipedia.org/wiki/Euclidean_vector
[[Autodesk University: ICE Design Tools|http://area.autodesk.com/userdata/blogs/marks/ToddAkita_ICE_Design_Tools.pdf]] (pdf) - This is for Softimage's ICE, but the reference it has for trig and vectors is great.
{{{
import math

vec1 = [1,0,0]
vec2 = [0,1,0]
point1 = [0,0,0]
point2 = [0,1,0]
point3 = [1,0,0]

def mag(vector):
"""
Return the length of a vector.
"""
val = 0
for v in vector:
val += pow(v,2)
m = math.sqrt(val)
return m

def unit(vector):
"""
Find unit vector.
Vector if length = 1
"""
len= mag(vector)
u = [vec/len for vec in vector]
return u

def dot(v1, v2):
"""
Find dot product of two vectors

Easy description:  If v1 & v2 are pointing the same direction, this value
is 1.0.  If they're pointing opposite directions, this value is -1.  If they
are at right angles, the value is 0.  So you can use this test to see how well
two vectors align.

More complex description:
It can be interpreted as the *length* of the projection of
the unit of v1 onto v2 when the two vectors are placed so that
their tails coincide.
http://mathworld.wolfram.com/DotProduct.html

Your right index finger is V1, your left index finger is V2.  Place
both knuckles on top of one another, pointing in different directions.
Find the unit vector of V1 (right finger), and mark that point on your
right finger.  Then...
Draw a perpendicular line from the mark on your right finger (V1) to your
left finger (V2), and mark a second point.  The dot product is the distance
from your left knuckle to that second point.  GOT IT?
"""
v1 = unit(v1)
v2 = unit(v2)
d=0
for i in range(len(v1)):
d = d + v1[i]*v2[i]
return d

def cross(v1, v2):
"""
Find cross product of two vectors.
Vector which is perpendicular to the two.
"""
v = [v1[1]*v2[2] - v1[2]*v2[1],
v1[2]*v2[1] - v1[1]*v2[2],
v1[0]*v2[1] - v1[1]*v2[0]]
return v

def normal(p1, p2, p3):
"""Given three 3d points, find the vector normal to those points"""
va = [p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2]]
vb = [p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]]
n = cross(va, vb)
return n

def sum(v1, v2):
"""
"""
ret = []
for i in range(len(v1)):
ret.append(v1[i] + v2[i])
return ret

def difference(v1, v2):
"""
Subtract v2 from v1.
"""
ret = []
for i in range(len(v1)):
ret.append(v1[i] - v2[i])
return ret

print "Length vec1:", mag(vec1)
print "Unit vec1:", unit(vec1)
print "Dot vec1, vec2:", dot(vec1, vec2)
print "Cross vec1, vec2:", cross(vec1, vec2)
print "Normal of point1, point2, point3:", normal(point1, point2, point3)
print "Sum of vec1, vec2:", sum(vec1, vec2)
print "Difference of vec1, vec2:", difference(vec1, vec2)

# Length vec1: 1.0
# Unit vec1: [1.0, 0.0, 0.0]
# Dot vec1, vec2: 0
# Cross vec1, vec2: [0, 0, 1]
# Normal of point1, point2, point3: [0, 0, 1]
# Sum of vec1, vec2:  [1, 1, 0]
# Difference of vec1, vec2: [1, -1, 0]
}}}
I find it helpful when making UI's to have a visual guide to go off of.  Below is a visual guide of various Tkinter widgets.  I won't say this is exhaustive, but I'll update it as I find new ones.  I pulled the majority of these from the examples listed here:
http://effbot.org/tkinterbook/tkinter-index.htm#class-reference
The module I made to make all of these examples is located here:
http://www.akeric.com/python/TkImages/TkWidgets.py
----
[img[http://www.akeric.com/python/TkImages/BitmapImage.jpg]]
[img[http://www.akeric.com/python/TkImages/Button.jpg]]
[img[http://www.akeric.com/python/TkImages/Canvas.jpg]]
[img[http://www.akeric.com/python/TkImages/Checkbutton.jpg]]
[img[http://www.akeric.com/python/TkImages/Entry.jpg]]
[img[http://www.akeric.com/python/TkImages/Frame.jpg]]
[img[http://www.akeric.com/python/TkImages/Label.jpg]]
[img[http://www.akeric.com/python/TkImages/LabelFrame.jpg]]
[img[http://www.akeric.com/python/TkImages/Listbox.jpg]]
[img[http://www.akeric.com/python/TkImages/Message.jpg]]
[img[http://www.akeric.com/python/TkImages/PanedWindow.jpg]]
[img[http://www.akeric.com/python/TkImages/PhotoImage.jpg]]
[img[http://www.akeric.com/python/TkImages/Scale.jpg]]
[img[http://www.akeric.com/python/TkImages/Scrollbar.jpg]]
[img[http://www.akeric.com/python/TkImages/Spinbox.jpg]]
[img[http://www.akeric.com/python/TkImages/Text.jpg]]
One of my co-workers pointed me to this forum:

The code was a bit buggy to get working off the bat (based on how they import PIL), but once we got it working it had some good results.

Moar:
*http://code.activestate.com/recipes/577353-voronoi-diagram/
*http://jtauber.com/blog/2008/11/07/voronoi_diagrams/
*http://code.activestate.com/recipes/578459-worley-noise-generator/
<<gradient horiz #ffffff  #ddddff #8888ff>>
[img[warpcat|http://farm3.static.flickr.com/2017/2118148943_75636dd96c.jpg?v=0]] Yes, that is me playing [[Rock Band|http://www.rockband.com/]].
''Eric Pavey''
*Email:  -  warpcat {{{(at)}}} sbcglobal {{{(dot)}}} net  -
>>
My other Tiddlywiki's:
*[[PyGame wiki|http://pygamewiki.tiddlyspot.com/]]
*[[mel wiki|http://mayamel.tiddlyspot.com/]]
*[[Processing wiki|http://processingwiki.tiddlyspot.com/]]
*[[CG OpenSource wiki|http://cgoswiki.tiddlyspot.com/]]
<<gradient horiz #ffffff  #ddddff #8888ff >>
[[About python tiddlywiki]] (go here first)
[[Instructions for use]] (go here second)
[[Check out the latest updates|History]] (go here third. //Hit ''F5'' to refresh your cache to see latest stuff.//)

<<gradient horiz #ddddff  #8888ff >>''Browse \ Search using:''
----
{{{<---}}} Major ''//Categories//'' (or 'All Subjects') in the Left column
Key-word ''//Tags//'' tab in the Right column (press Ctrl+f to open your browsers search) {{{--->}}}
----
>>>>
^^Running [[tiddlywiki|http://www.tiddlywiki.com]] v<<version>>^^
^^If you find this wiki useful, let the [[author|WarpCat]] know!^^

*{{{PYTHONPATH}}}
*{{{PYTHONSTARTUP}}}  :  If set to the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode
*{{{PYTHONHOME}}}
*{{{PYTHONCASEOK}}}
*{{{PYTHONDEBUG}}}
*{{{PYTHONINSPECT}}}
*{{{PYTHONOPTIMIZE}}}
*{{{PYTHONBUFFERED}}}
*{{{PYTHONVERBOSE}}}
Find by code:
{{{
import types
for d in dir(types):
print d
}}}
prints:
{{{
BooleanType
BufferType
BuiltinFunctionType
BuiltinMethodType
ClassType
CodeType
# ...
}}}
This is by no means an exhaustive list, but it covers most of the basics.  Each type is an //object//, that a variable can point to.
http://docs.python.org/library/types.html#l2h-858
http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy
http://docs.python.org/library/stdtypes.html
{{{
print type(True)
# <type 'bool'>

print type(33)
# <type 'int'>

print type(33L)
#<type 'long'>

print type(3.33)
#<type 'float'>

print type(3.33J)
# <type 'complex'>

print type("thirtyThree")
# <type 'str'>

print type(u"thirtyThree")
# <type 'unicode'>

print type([3.33, "three"])
# <type 'list'>

print type((3.33, "three"))
# <type 'tuple'>

print type({"three" : 3})
# <type 'dict'>

print type(open(myFile))
# <type 'file'>

print type(set('three'))
# <type 'set'>

print type(type)
# <type 'type'>

print type(None)
# <type 'NoneType'>

import datetime
print type(datetime)
# <type 'module'>

from decimal import Decimal
print type(Decimal('3.33'))
# <class 'decimal.Decimal'>

def func():
pass
print type(func)
# <type 'function'>

print type(lambda x:x)
# <type 'function'>

class MyObj(object):
pass
print type(MyObj)
# <type 'type'>
print type(MyObj())
# <class '__main__.MyObj'>
}}}
Also see:
*[[How are Pythons built-in types organized?]]
''All very WIP.  Just a starting point''
----
In the interactive command line.  Pretty much the simplest way:
{{{
c:\Python25>Python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
>>> print "spam!"
spam!
>>>
}}}
Same thing can be done via an [[IDE's|Wing IDE]] interactive commandline obviously.
----
On Windows, double-clicking a .py file
----
In a module, [[import]]ing a module the first time will execute any code in it, not defined as a function.  Subsequent imports won't execute the code.  If you want to re-execute it, you need to {{{reload}}} it.
----


http://www.alobbs.com/pykyra
''~PyKyra'': "a fast game development framework for Python"
----
http://www.pygame.org
"''Pygame'' is a set of Python modules designed for writing games. Pygame adds functionality on top of the excellent [[SDL library|http://www.libsdl.org/]]. This allows you to create fully featured games and multimedia programs in the python language. Pygame is highly portable and runs on nearly every platform and operating system."
*[[Pygame Documentation|http://www.pygame.org/docs/]]
*''I have started my own 'PyGame wiki' here: http://pygamewiki.tiddlyspot.com/''
----
http://www.livewires.org.uk/python/home
"The ''~LiveWires'' Python Course is intended to teach the Python programming language to people who have never programmed before."
You can consider ~LiveWires a wrapper for Pygame, to reduce the complexity.


If you're like me, at a certain point in my life I didn't know much about xml, or DOM objects (and of this writing, I still don't know that much).
DOM stands for '''Document Object Model'''.  Couple of links:
http://en.wikipedia.org/wiki/Document_Object_Model
http://www.w3.org/DOM/
>The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page.
From they [[Python Docs|http://www.python.org/doc/current/lib/module-xml.dom.html]]:
>The Document Object Model, or 'DOM', is a cross-language API from the World Wide Web Consortium ([[W3C|http://www.w3.org/]]) for accessing and modifying XML documents. A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch. It then gives access to the structure through a set of objects which provided well-known interfaces.
Some good tutorials for XML over at [[W3Schools|http://www.w3schools.com/default.asp]]
Example code:
{{{
class Foo(object):

def __init__(self, namer, num):
self.name = namer
self.number = num

# etc...

# then, when building the object:
myObject = Foo("doof", 3)
}}}
*You create an object by instancing the class by calling to the //constructor method// (which is the {{{__init__}}} method).  It takes at minimum, one argument, which is a string {{{self}}}.  What IS {{{self}}}?  The {{{self}}} parameter represents the object 'itself', the object that you created when instancing the class.  It provides a way for a method to refer to the object itself.
*The actual string '{{{self}}}' can really be anything as long as it's consistent, but in Python everyone uses {{{self}}}.  For example, in the language Processing (which is higher-level a wrapper for Java), they use the term '{{{this}}}' when making their objects.
*When you call to make a new object, {{{self}}} //becomes// the name of the new object (from the above example) "{{{myObject}}}".  So again, from the example, {{{self}}} = "{{{myObject}}}"!  In a sense, the constructor method {{{__init__}}} 'takes in' the name {{{myObject}}}, and inserts it into argument {{{self}}}.  Considering that, you could //conceptually visualize// the example from above as such:
{{{
# substitute 'self' for the user defined object name 'myObject':
def __init__(myObject, namer, num):
myObject.name = namer
myObject.number = num

# etc...
}}}
Fore more info on classes, see [[Class]] and [[Class - advanced]]
Updated note:
<<<
After sitting in a cave for a week meditating on this subject, I came up with something that helped me understand it better:
*If when you see@@ {{{__name__}}}@@, you replace it with '@@[[namespace]]@@', it seems to give the concepts that surround a bit more clarity.
Basically, the {{{__name__}}} attribute holds the name of the current namespace for the given object.
In the below examples try it, and I'll think you'll agree...
<<<
----

Sometimes you'll see this a the bottom of some modules:
{{{
if __name__ == "__main__":
# do some code
}}}
When you see things with double-underscores "{{{__}}}" on either side of their name, these refer to 'special attributes'.  So given an module called {{{foo.py}}}, it has a special attribute called {{{foo.__name__}}}.  If you //[[import]]// that module into a different module, {{{foo.__name__}}} will end up equaling the name of the module, "{{{foo}}}" :
{{{
import foo
print foo.__name__
foo
}}}
However, if {{{foo}}} is execute from a shell, not being imported into another module, then it's {{{__name__}}} attribute ends up equaling "{{{__main__}}}":
{{{
# This is the contents of 'foo.py':
if __name__ == "__main__":
print "foo.__name__ = " + __name__
raw_input("Press Enter to continue")
}}}
And when executed from a shell:
{{{
foo.__name__ = __main__
Press Enter to continue
}}}
__So what's the point of all of this?__  It allows the author to create different behaviors for the module depending on how it's executed.  If the module is imported into another one, then nothing special happens.  But if the module is executed from a shell, then a cusom UI could pop up, querying the user for input before code execution.
----
Also see:
*[[namespace]]


http://svn.python.org/view/python/trunk/

Coming from the PC side of Python instalation, dealing with it on the Mac seems much more confusing.

The Mac comes with Python pre-installed, living under the ''/System'' folder here:
{{{
/System/Library/Frameworks/Python.framework/Versions/
}}}
But there's a good chance it's out of date.  I'm currently not sure how these are updated, but they seem to somehow, over time.

You can install your own versions.  They'll live in their own location under the ''/Library'' folder:
{{{
/Library/Frameworks/Python.framework/Versions/
}}}
That area is safe to delete.

But there also appears to be an Applications location for each custom install:
{{{
/Applications/Python 2.6
}}}
Which is where the {{{IDLE.app}}} lives

From the above example, the {{{/site-packages}}} dir of a custom Python 2.6 install would live in the below directory.  Things installed via [[pip|Installing Python Pacakges]] end up in here:
{{{
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
}}}
However... there seems to be YET ANOTHER site-packages dir living here:
{{{
/Library/Python/2.6/site-packages/
}}}

All that being said, where the actual //executable// lives seems to live in an confusing state.  See notes on that here:
*[[How can I query the python executable location?]]
Notes on editing wiki:
*[[text formatting]]
*[[list of macros|http://www.tiddlywiki.org/wiki/What_macros_are_built_in_to_TiddlyWiki%3F]]
*[[DateFormatString arguments|http://www.tiddlywiki.com/#DateFormatString]]
I've been looking for a LONG time, trying to find an IDE for Python that mirrored //one// important feature from Maya's Script Editor:  ''Evaluate Selection'', which is the ability to highlight blocks of code, and execute just that block interactively.

Maya's implementation of Python in the Script Editor can do this (since version 8.5), but what if you want to author Python //outside// of Maya (like most folks), but still have this functionality?

After asking a lot of people, and doing a lot of searching, I finally found one:
http://www.wingware.com/
It has three versions:
*Wing IDE 101 (free, but //doesn't// have 'Evaluate Selection')
*Wing IDE Personal ($35), *Wing IDE Professional ($179)
The Personal features section:
http://www.wingware.com/wingide-personal/index
----
Something nice the //Professional// version adds is python calls to the Wing API.  This means (among other things) you can write your own scripts to run in Wing.  A practical example would be a new hotkey:  I wanted a way to hilight a word in Wing, run a hotkey, and pull up help on the topic.  The Wing support crew was very quick in firing back this as a solution, which I've modified for my own uses:
{{{
# customWingHotkeys.py

import wingapi

def search_docs(site):
"""Do a search on the Python documentation for the selected text
in the current editor"""

editor = wingapi.gApplication.GetActiveEditor()
if editor is None:
return
doc = editor.GetDocument()
start, end = editor.GetSelection()
txt = doc.GetCharRange(start, end)
import urllib
url = urllib.urlencode((('q', txt + ' site:'+site), ('ie', 'utf-8')))
wingapi.gApplication.OpenURL(url)

def search_pygame_docs():
search_docs("www.pygame.org/docs/ref/")

def search_python_docs():
search_docs("docs.python.org")
}}}
I found that you can't pass args into hokeys via Wings hotkey editor, so I made master function ({{{search_docs()}}}) that does the work, and then I make unique hotkey functions ({{{search_python_docs()}}} & {{{search_pygame_docs()}}}) that I map to specific hotkeys, based on the type of search I want.
You save that as a module in your {{{..\Application Data\Wing Personal 3\}}} dir (the Wing 'Settings Directory' found via the Help->About... menu).  Then you can add that has a 'custom key binding', and map it to a hotkey.  Slick.
>Wing itself runs a different version of Python than you may have installed.  The current version of Wing (as of this update) runs Python 2.5.4.  This is only really important if you get the "Pro" version, and start making your own 'plugin scripts':  The plugins you author will be running Wings version of Python, not your installed version of Python.  Furthermore, the Wing distribution isn't the full install of Python, so not all modules are available.  This isn't a huge deal, but it's important to be aware of if your plugins don't work.  For example, I had one that called to the {{{webbrowser}}} module:  That module isn't distributed with Wing.  Luckily, they have their own function ({{{wingapi.gApplication.OpenURL(url)}}}) that could be used in its place.
*Wing API Command Reference: http://www.wingware.com/doc/commands/index

As an alternative to {{{xml.dom.minidom}}} (since it seems so clunky), notes on {{{ElementTree}}}.
Docs:
*http://docs.python.org/library/xml.etree.elementtree.html
*http://effbot.org/zone/element.htm (nice tutorials, go read this now)
*http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html
{{{
import xml.etree.ElementTree as ET
}}}
''Examples:''
----
Search some xml on disk called foo.xml
In it, find the first child element of the root called "firstTagElement"
Under that, find all elements (could be more than one) that are called "subTagElement"
If none of those sub-elements have the text defined by {{{searchText}}}, then add a new sub-element, with that text.
And save to disk (and print)
{{{
import xml.etree.ElementTree as ET

xmlPath = "c:/temp/foo.xml"

noMatch = 1
tree = ET.parse(xmlPath)
root = tree.getroot()
firstTagElement = root.find("firstTagElement")

for subElement in firstTagElement.findall("subTagElement"):
if subElement.text == searchText:
noMatch = 0
break
if noMatch:
element = ET.SubElement(firstTagElement ,"subTagElement")
element.text = searchText
tree.write(xmlPath, "UTF-8")
# or:
print ET.tostring(root)
}}}
The xml being searched (before modification) could look like this:
{{{
<?xml version="1.0" encoding='UTF-8'?>
<root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
</firstTagElement>
</root>
}}}
After modification and save:
(Actually, this is a lie, it's formatting will be screwed up.  Please see my notes here: [[Poorly formatted xml problems]])
{{{
<?xml version="1.0" encoding='UTF-8'?>
<root>
<firstTagElement>
<subTagElement>some text</subTagElement>
<subTagElement>some more text</subTagElement>
</firstTagElement>
</root>
}}}
----
Note #1:  This is mainly a scratchpad until I figure out what the heck I'm doing...
Note #2:  I found minidom to be a real pain, and have mainly switched to ~ElementTree.  Check out my docs here: [[Working with xml and ElementTree]]
----
Online Examples:
*http://acaciaecho.wordpress.com/2011/04/02/python-xml-basic/
----
In this simple example, an xml file is loded into a dom, and then it is searched:
First, it finds all {{{tags}}} ({{{elements}}}) with the name "material".
Then, for each of those {{{tags}}}, it finds the value of the {{{attribute}}} "id", and prints them.
{{{
import xml.dom.minidom

doc = r"c:\temp\some.xml"
mDom= xml.dom.minidom.parse(doc)

for i in mDom.getElementsByTagName("material"):
# Find the value of attribute "id" under tag "material":
print i.getAttribute("id")
# Set the value of attribute "foo" under tag "material":
i.setAttribute("foo", "someVal")
}}}
Find the root tag value:
{{{
print mDom.documentElement.tagName
}}}
Make a //new// dom object from scratch.
{{{
import xml.dom.minidom

impl = xml.dom.minidom.getDOMImplementation()
newdoc = impl.createDocument(None, "ROOT", None)

top_element = newdoc.documentElement
text = newdoc.createTextNode('Some data')
top_element.appendChild(text)

ft = newdoc.createElement("firstTag")
top_element.appendChild(ft)

attr = newdoc.createAttribute("someAttr")
ft.setAttributeNode(attr)

print newdoc.toprettyxml()
}}}
{{{
<?xml version="1.0" ?>
<ROOT>
Some data
<firstTag someAttr=""/>
</ROOT>
}}}
Make a new dom object from a pre-exsting xml file or string.  Both functions return a {{{Document}}} object representing the content of the document.
{{{
# from a file:
xml.dom.minidom.parse
# from a string:
xml.dom.minidom.parseString
}}}
If you want to query they type of node a node is:
{{{
nt = Node.nodeType
}}}
And you can compare that against the list of valid node types:
http://docs.python.org/library/xml.dom.html#xml.dom.Node.nodeType

Presuming you already have a element, how can you find the text of it?
{{{
for tag in self.mDom.getElementsByTagName("someTag"):
for cn in tag.childNodes:
if cn.nodeType == cn.TEXT_NODE:
print cn.data
}}}
That seems like a lot of effort to find the "text"
----
Sometimes (all the time?) when I parse a pre-existing xml and add new elements, when I write out the xml, the formatting gets all screwed up for the elements I add.  It has to do with the fact that new return characters are introduced in addition to the pre-existing ones.  Here's a solution to help solve for this, but it can modify the formatting of the original xml:
{{{
xmlText = mDom.toxml("UTF-8")
lines = xmlText.split("\n")
writer = [i for i in lines if len(i.rstrip()) != 0]
for j in writer:
print j
}}}
----
Based on the above examples:
*minidom docs:
**http://docs.python.org/library/xml.dom.minidom.html
*If you want to process the '{{{Document}}} object' you can uses these commands:
**http://docs.python.org/library/xml.dom.html#document-objects
*If you want to process an '{{{Element}}} object' from the dom (the object returned from one of the above commands):
**http://docs.python.org/library/xml.dom.html#element-objects
*If you want to process an '{{{Atribute}}} object':
**http://docs.python.org/library/xml.dom.html#attr-objects
*For any node object, here is info you can pull from it:
**http://docs.python.org/library/xml.dom.html#node-objects
----
Given a simple xml file '{{{note.xml}}}':
{{{
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
}}}
Load it in Python, and then print it:
{{{
from xml.dom import minidom

DOMTree = minidom.parse("note.xml")
print DOMTree.toxml()
}}}
(prints the above xml...)
----
Another Example:
{{{
import xml.dom.minidom

doc = """\
<?xml version="1.0" encoding="ISO-8859-1"?>
<tagA>
<tagB tagB_attr="tagB attr value"> tagB text </tagB>
<tagC tagC_attr="tagC attr value"> tagC text </tagC>
</tagA>
"""

dom = xml.dom.minidom.parseString(doc)
childNodes = dom.childNodes
nodeList = childNodes[0].getElementsByTagName("tagC")
for n in nodeList:
print n.toxml()

# <tagC tagC_attr="tagC attr value"> tagC text </tagC>
}}}
The above example accesses a string directly as a xml file via the {{{.parseString()}}} method.  Then using the {{{.getElementsByTagName()}}} method, we search for //all// child elements with the tag name {{{tagC}}}.
----

{{{minidom.parse}}} creates {{{Document}}} object.  Then you can call the {{{.toxml()}}} method which returns a string containing the text from the xml.
----
Python docs for {{{xml.dom}}}:
http://www.python.org/doc/current/lib/module-xml.dom.html
Docs for {{{xml.dom.minidom}}}:
http://docs.python.org/lib/module-xml.dom.minidom.html
{{{minidom}}} examples:
http://docs.python.org/lib/dom-example.html
----
All of the below docs call to attributes and methods that can act on the objects created by {{{minidom.parse()}}}:
Docs for {{{DOM}}} objects:
http://docs.python.org/lib/dom-objects.html
Docs for {{{Node}}} objects:
http://docs.python.org/lib/dom-node-objects.html
Docs for {{{Document}}} objects:
http://docs.python.org/lib/dom-document-objects.html


I have other subjects on these, so why not make another one!  As I continue to try to pick apart how all of these relate :)
*http://docs.python.org/library/__main__.html
*http://docs.python.org/library/__builtin__.html
Notes on {{{__builtins__}}}:
*http://mail.python.org/pipermail/python-dev/2005-December/058652.html
*http://mail.python.org/pipermail/python-3000/2007-March/006161.html
----
*{{{__main__}}} is the most top level [[scope|Python variable scope]] in Python.
*{{{__builtin__}}} is a module, that has all the 'built-in' python functions you use.
*{{{__builtins__}}} //appears// to be a [[namespace]] that {{{__builtin__}}} is imported into during Python launch, giving you access to all of {{{__builtin__}}} functions (this is a //theory// of mine).  This allows you (again, theory) to call to them without having to fully quality them, like:
<<<
{{{
# If __builtin__ *wasn't* imported into __builtins__,
# you'd need to do stuff like this:
import __builtin__
stuff = __builtin__.sorted(foo)
}}}
{{{
# But since __builtins__ is by default imported into __main__,
# we can do stuff like this instead:
stuff = sorted(foo)
}}}
<<<
*And from one of the links on {{{__builtins__}}} above, I think this is a good analysis:
<<<
In module {{{__main__}}}:
*{{{__builtins__}}} is a reference to module {{{__builtin__}}}.
*{{{__builtin__}}} only exists if you import it.
In any other module:
*{{{__builtins__}}} is a reference to module {{{__builtin__}}}'s {{{__dict__}}}.
*{{{__builtin__}}} only exists if you import it.
<<<
----
Let's illustrate this in action:
Make a module called {{{scopeTest.py}}}:
{{{
# scopeTest.py

def scopeTestMain():
# Print everything in the __main__ namespace
import __main__
for m in sorted(__main__.__dict__):
print m

def scopeTestBuiltin():
# Print everything in the __builtin__ module
import __builtin__
for bi in sorted(__builtin__.__dict__):
print bi

def scopeTestBuiltins():
# Print everything in the __builtins__ namespace
import __main__
for bis in sorted(__main__.__builtins__.__dict__):
print bis
}}}
In action:
{{{
# These are being entered in the Python shell, which is
# the __main__ scope:

import scopeTest
# just a test var
var = 23
}}}
{{{
scopeTest.scopeTestMain()
# prints:
__builtins__
__doc__
__file__
__name__
scopeTest
var
}}}
A brief tangent:  Executing this in the same Python shell will print the same data, since {{{dir()}}} without any arguments will return the list of names in the current local scope... which in this case is the {{{__main__}}} scope (since it's in the Python shell):
{{{
for d in sorted(dir()):
print d
# prints:
__builtins__
__doc__
__file__
__name__
scopeTest
var
}}}
Continuing test...
{{{
scopeTest.scopeTestBuiltin()
# prints:
ArithmeticError
AssertionError
AttributeError
BaseException
# ... a whole lotta other stuff...
type
unichr
unicode
vars
xrange
zip
}}}
{{{
scopeTest.scopeTestBuiltins()
# prints:
ArithmeticError
AssertionError
AttributeError
BaseException
# ... a whole lotta other stuff...
type
unichr
unicode
vars
xrange
zip
}}}
----
Things of note:
*{{{__main__}}} collectes all our 'most globalist' (I made up that word) variables:  'local' vars refer to variables inside functions. 'global' variables refer to variables in the top level of a module.  I tend to call {{{__main__}}} the 'universal' scope.  Note (below) the variables collected there in our above example.  This is because they were either imported into, or declared at, the 'top most' {{{__main__}}} scope.
**{{{scopeTest}}}
**{{{var}}}
*{{{__builtin__}}} and {{{__builtins__}}} both have the exact same data in them.
*If you run {{{type()}}} on {{{__main__}}}, {{{__builtin__}}}, and {{{__builtins__}}}, Python say they are all {{{<type 'module'>}}}.

''Note'':  I wrote the below section before I really grasped the difference in concept between 'parameters' and 'arguments'.  So //everything// I discuss below this note is refers to 'arguments only'.  However, parameters and arguments are two different things, which I'll describe here first, and maybe get around to re-authoring the below text later ;)

A function can take a series of identifiers, also known as //parameters//:
{{{
def foo(parameter1, parameter2="defaultArgument"):
# do work
}}}
And when you call to the function you pass values, known as //arguments//, to the parameters:
{{{
foo('argument1')
}}}
Based on this example, the //parameter// {{{parameter2}}} has a //[[default argument|Default Arguments]]// with the string //value// of {{{"defaultArgument"}}}.  If you don't pass an argument to {{{parameter2}}} when the function is called, then the //default argument// is used instead.  The //parameter// {{{paramter1}}} has the argument {{{'argument1'}}} passed in.

So when 'parameters' are discussed, they refer to the 'identifier name' the function expects, and when 'arguments' are discussed, it refers to the 'values' passed into the function.

----
----

[[Function]] @@//definitions//@@ can take arguments.  This allows data to be passed into them, that would otherwise be external to their universe.
There are four different ways of assigning arguments to a function definition, and they need to be arranged in a specific order:
*{{{arg}}}  :  The function matches normal arguments by //position//, left to right, when passed in.
<<<
{{{
def foo(argA, argB):
print argA, argB
foo("b", "a")
# b a
}}}
<<<
*{{{arg = value}}}  :  The function has a //default value// assigned to the keyword.  But the user can override this with their own value.
<<<
{{{
def foo(argA = "", argB = "23", argC = "happy"):
print argA, argB, argC
foo(argB="b", argA="a")
# a b happy
}}}
<<<
*{{{*args}}}  :  Collects any number of unmatched //positional// arguments into a new {{{tupple}}} for processing.  The variable name is arbitrary, it just needs to be preceded with a single asterix '{{{*}}}'.
<<<
{{{
def foo(*args):
print "made a tupple:  ", args
for a in args:
print a, "!!!"
foo("A", "B", "C")
# made a tupple:  ("A", "B", "C")
# A !!!
# B !!!
# C !!!
}}}
<<<
*{{{**kwargs}}}  :  Collects any number of unmatched //keyword// arguments into a new {{{dictionary}}} for processing.  The variable name is arbitrary, it just needs to be preceded with double asterix '{{{**}}}'.
<<<
{{{
def foo(**kwargs):
print "made a dictionary: ", kwargs
for key in kwargs:
print key + "  :  " + kwargs[key]
foo(keyA="valA", keyB="valB", keyC="valC")
# made a dictionary:  {'keyC': 'valC', 'keyB': 'valB', 'keyA': 'valA'}
# keyC  :  valC
# keyB  :  valB
# keyA  :  valA
}}}
<<<
An example function using all four methods, listing them in the correct order:
{{{
def argTest(positional, defaultValue="notArg1", *args, **kwargs):
print "positional1:", positional
print "defaultValue:", defaultValue
print "*args:", args
print "**kwargs:", kwargs

argTest("ARG1",
"replaced default val",
"unmatched positional A", "unmatched positional B",
keywordA="A",
keywordB="B")
}}}
prints:
{{{
positional1: ARG1
defaultValue: replaced default val
*args: ('unmatched positional A', 'unmatched positional B')
**kwargs: {'keywordB': 'B', 'keywordA': 'A'}
}}}
You can have as many '//positional//' {{{arg}}}'s as you want, as long as they exist before any 'keyword/default value' args ({{{arg = value}}}).  You can have as many 'keyword/default value' args as you want, as long as they're listed before {{{*args}}} and {{{**kwargs}}}.  There is only a single call to both {{{*args}}} and {{{*kwargs}}}.
----
When @@//calling//@@ to a defined function with arguments, similar rules apply:
*Normal arguments are matched by //position//.
<<<
{{{
def foo(argA, argB):
print argA, argB
foo("b", "a")
# b a
}}}
<<<
*Keyword variables that have default values can be entered in any order, or not at all:
<<<
{{{
def foo(argA = "", argB = "", argC = "C!!!"):
print argA, argB, argC
foo(argB="b", argA="a")
# a b C!!!
}}}
<<<
*You can pass all objects in a list\tupple as individual positional arguments, by prefixing the variable name with an asterix '{{{*}}}'.  The function will unpack the list\tupple.  This is sort of the inverse of function argument //assignment// {{{*args}}} (above).  It's important that the list\tupple has the same number of items as the function has expectant arguments, or an exception will be raised.
<<<
{{{
def foo(argA, argB, argC, argD):
print argA, argB, argC, argD
args = ("a", "b", "c", "d")
foo(*args)
# a b c d
}}}
<<<
*You can pass all the key\value pairs from a dictionary via the double-asterix '{{{**}}}'.  The function will unpack the dictionary.  This is similar to the inverse of function argument //assignment// {{{**kwargs}}} (above).  It's important that the dictionary has the same number\names of keys as the function has arguments, or an exception will be raised.
<<<
{{{
def foo(argA, argB, argC, argD):
print argA, argB, argC, argD
kwargs = {"argA":1, "argB":2, "argC":3, "argD":4}
foo(**kwargs)
# 1 2 3 4
}}}
<<<
----
*Arguments are passed by 'automatically assigning objects' to local names.
**In the above //definition// example, {{{argA}}} is in the local scope of {{{def foo()}}}
*Assigning to immutable argument names inside a function doesn't affect the caller.
**If in {{{foo()}}} you decide to set {{{argA}}} to {{{42}}}, that won't effect the caller that passed in the object, since the integer 42 is immutable.  See next:
*''Immutable'' arguments are passed in 'by value'.
**Which in effect is like passing in a duplicate copy, since they //can't// be changed in-place.
*''Mutable'' objects are passed in 'by reference'.
*Changing a mutable object argument //in// a function may impact the caller.  An example of this would be passing in a list:  Changing the list in the function can effect the list outside the function, since lists are mutable.
**Meaning, if you change a passed in mutable object in the function, it //can and will// modify that object in place.
**Mutable arguments are passed in 'by pointer'.  As above:  The function that is receiving them has a pointer to the original object, so it can be changed in-place by the function.
Example of the last three bullets:
{{{
l = ["a", "s", "d", "f"] # mutable list (that's a lower-case 'L', fyi)
n = 42 # immutable integer
def foo(lister, number):
lister.append("snuff") # change in-place via object method
number = 23 # define new local variable.
print l
# ['a', 's', 'd', 'f']
print n
# 42
foo(l,n)
print l
# ['a', 's', 'd', 'f', 'snuff']
print n
# 42
}}}
{{{l}}} is a mutable sequence, so the function //did// change its value.  But the number {{{n}}} is immutable, so no change happened.
----
Also see:
*[[Default Arguments]]
*Another [[blog post|http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/]] describing {{{*args}}} and {{{**kwargs}}}.

I keep forgetting this simple syntax... so I write it down:

{{{
>>> assert 1==2, "Sorry, those don't equal"
AssertionError: Sorry, those don't equal
}}}
If the {{{assert}}} fails, it raises an {{{AssertionError}}}
The term attribute in Python has several meanings, as I'm coming to learn.  Attributes are tied very closly to [[namespace]]s, so be sure to see the notes on that.
Also see [[Special Attributes]]
----
''Importing Attributes''
When you [[import]] one module into another, that module is imported into a [[namespace]].  The functions, variables, and classes defined in that module (basically, all the names declared at the //top level// of a module) become accessible as attributes by the importing module via the [[namespace]]'s 'dot notation': '{{{namespace dot attribute}}}'
{{{
# myFile.py
def myFunc():
return 23
someVal = "spam!"
}}}
{{{
# myOtherFile.py
#  Imported the variables and functions from myFile
#  into a namespace 'myFile':
import myFile
#  Access via 'dot notation':
print myFile.someVal
print myFile.myFunc()
}}}
Then, when executing {{{myOtherFile.py}}}
{{{
spam!
23
}}}
{{{myOtherFile.py}}} could also be authored like this:
{{{
from myFile import someVal
print someVal
}}}
This imports someVal directly into the namespace of {{{myOtherFile}}}, so the variable no longer has to be access via dot notation.  See notes on [[import]].
----
There are a variety of ways to access attributes.  In the below example I use a string variable, but it could just as easily be a [[module]] or [[Class]]
{{{
# module.py
value = "spam!"
}}}
{{{
import sys
import module

foo = module.value
foo = module.__dict__['value']
foo = sys.modules['module'].value
foo = getattr(module, 'value')

# all return the same value to foo:  "spam!"
}}}
From above
*{{{module.value}}}  :  The most common way of accessing the attribute (IMO).
*{{{module.__dict__['value']}}}  :  Acces the module object's {{{__dict__}}} attribute, and query the current value of variable {{{value}}}.
*{{{sys.modules['module'].value}}}  :  {{{sys.modules}}} is a dictionary of the loaded modules.  Via this example, we query the value of attribute {{{.value}}} on object {{{module}}} by looking into that dictionary.
*{{{getattr(module, 'value')}}}  :  Use the built-in function to pull the value.
Furthermore, you can add arbitrary attributes to objects as well:
{{{
module.attr = "attribute!"
print module.attr
# attribute!
}}}
----
''Attributes in Classes''
Attributes can be variables that are associated with an object, as instanced via a [[Class]].  (Also see [[Class - advanced]] & [[Class Attributes]])
Attributes are accessed via 'dot notation'.
Simple example:
{{{
class Foo(object):
def __init__(self, namer, num):
self.name = namer  # make an attr called .name
self.__number = num # make an private class-attr called .__number.
# continue...
}}}
Once you make an object, any non-private attrs you can access:
{{{
f = Foo("bob", 3)
print f.name
# "bob"
}}}
You can still access the private attr, but you //shouldn't//, since it //is private//, and probably very shy.
----
From the [[PEP8 style guide|http://www.python.org/dev/peps/pep-0008/]]:
*{{{__double_leading_and_trailing_underscore__}}}: "magic" objects or attributes that live in user-controlled namespaces.  E.g. {{{__init__}}}, {{{__import__}}} or {{{__file__}}}.  Never invent such names; only use them as documented.
*{{{__double_leading_underscore}}}: when naming a class attribute, invokes name mangling (inside {{{class FooBar}}}, {{{__boo}}} becomes {{{_FooBar__boo}}}).  -- This means it's a //private attribute//
*Use one leading underscore only for non-public methods and instance variables (see notes on [[import]]).
>Docs on [[ctype array creation|http://docs.python.org/library/ctypes.html#arrays]], which showcase much more compactly what I'm describing below (found these docs after I authored the below stuff...)
I'm not even sure the title accurately explains what I'm trying to... explain... :)  I currently only have the most rudimentary conceptualization behind how [[ctypes|http://docs.python.org/library/ctypes.html]] work, but while working in [[pyglet|http://www.pyglet.org]] the other day I saw this line of code (from page 14 of the [[programming_guide.pdf|http://www.pyglet.org/doc/programming_guide.pdf]])
{{{
vertices_gl = (GLfloat * len(vertices))(*vertices)
}}}
Initially, it looked like it was multiplying one tuple {{{(GLfloat * len(vertices))}}} by another {{{(*vertices)}}}.  But this didn't seem right.  Since {{{GLfloat}}} was from the {{{pyglet}}} GL lib, I wanted to recreate this using something builtin, so I did with {{{ctypes.c_double}}}.
Example below is a modified version from the programming guide with that change:
{{{
import ctypes
vertices = [0,0, 0,1, 1,1, 1,0]
vertices_gl = (ctypes.c_double * len(vertices))(*vertices)
print vertices_gl
}}}
{{{
<__main__.c_double_Array_8 object at 0x00000000028FA9C8>
}}}
So the end result is a {{{c_double_Array_8}}} //object//.  And the '8' will change dynamically based on how many items are put in the {{{vertices}}} list.

But this still didn't help my understanding much, so I broke it down a bit more:
{{{
import ctypes
vertices = [0,0, 0,1, 1,1, 1,0]
cDoubleArray = (ctypes.c_double * len(vertices))
print cDoubleArray
vertices_gl = cDoubleArray(*vertices)
print vertices_gl
}}}
{{{
<class '__main__.c_double_Array_8'>
<__main__.c_double_Array_8 object at 0x00000000028FAEC8>
}}}
*That makes a bit more sense: {{{cDoubleArray =  (ctypes.c_double * len(vertices))}}} returns a dynamically created {{{c_double_Array_8}}} ''//class//''.  Meaning, the type of class (the {{{_8}}} part) will change based on how many items are in the {{{vertices}}} list.  Didn't know you could do that.
*When you put parenthesis immediately after it, you're calling to the class to create an ''//object//'': {{{vertices_gl = cDoubleArray(*vertices)}}}
*And by putting the asterisk in front of the argument {{{cDoubleArray(*vertices)}}}, you directly unpack the {{{vertices}}} variable into the expectant positional args of the class's {{{__init__}}} func.  Meaning, rather than passing in a list of eight items, you directly pass in eight items.
So in a nutshell the below line:
{{{
vertices_gl = (GLfloat * len(vertices))(*vertices)
}}}
Is dynamically creating a ctypes 'double array' class, and instancing an object from it in one line.
decimal is a module that can be imported to deal with (among other things) floating point precision issues.
In the below example, we multiply a number by 2.  The result seems to be ok, but behind the scenes actually, it's slightly off:
{{{
import decimal

num =  3.1415 * 2
print num
>>> 6.283   # is this really true?
print num.__repr__()
>>> 6.2830000000000004  # sigh, floating point badness
# note, you need to turn it into a string first:
d = decimal.Decimal(str(num))
print d
print d.__repr__()
>>> 6.283
>>> Decimal('6.283')
}}}
But turning this back into a float seems to bring back the problems again :(
{{{
num = float(d)
print num.__repr__()
>>> 6.2830000000000004
}}}
Official docs:
http://docs.python.org/glossary.html#term-decorator
http://docs.python.org/reference/compound_stmts.html#function

Some documentation
*[[Python Decorators|http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/]]  : Probably the best one I've found.
*[[Decorators I: Introduction to Python Decorators|http://www.artima.com/weblogs/viewpost.jsp?thread=240808]]
*[[Python Decorators Don't Have to be (that) Scary|http://www.siafoo.net/article/68]]
*[[Python Decorator Library|http://wiki.python.org/moin/PythonDecoratorLibrary]]
See a simple example here: [[How can I write a program timer?]]

Decorators simply wrap one function in another with the syntax '{{{@wrapperFunction}}}'.  From the above docs:
{{{
# implementation A (python 2.4 and earlier):
def f(...):
...
f = staticmethod(f)
}}}
{{{
# implementation B (decorator implementation):
@staticmethod
def f(...):
...
}}}
Both have the same result.  The {{{staticmethod()}}} function obviously does something important, and it wrappers another function {{{f()}}} to pull off its results.  But as you can see, there are are two different ways to implement this functionality:

In the first (A),  you create your arbitrary function {{{f()}}}, then pass {{{f}}} directly into the {{{staticmethod()}}} function returning an 'updated' version of {{{f}}}.

In the second (B), you physically use the 'at sign' //decorator// {{{@}}} in front of your wrapper function, and place it on the line above the function to be decorated.
----
Common uses for decorators include the functions [[property]], [[classmethod|Class Methods]], [[staticmethod|Static Methods]], ...?

{{{import}}} is a statement ([[python docs|http://docs.python.org/ref/import.html]]).
Also see the built-in {{{__import__}}} //function//, which the {{{import}}} //statement// actually runs.  - [[Python Docs|http://docs.python.org/lib/built-in-funcs.html]]

[[Modules|module]] need to be //imported// before they can be used in another module:
{{{
# moduleA.py
return x + y
def foo():
print "foo!"
}}}
Importing {{{moduleA.py}}} into {{{moduleB.py}}} puts an //instance// (rather than a copy, see notes below) of  {{{moduleA.py}}} into the {{{moduleA}}} [[namespace]] by default (see notes on [[__name__]]):
{{{
# moduleB.py
import moduleA
print num
# 4
}}}
You can also use the {{{from}}} statement to extract certain [[attribute]]s (functions, classes, variables) from a module.  These attributes are //copied// (rather than instanced, see notes below) directly to the current namespace, stomping the values of any previously named variables (this is usually bad, so use with care).  In the below example, since {{{adder()}}} //isn't// imported, it //can'//t be executed.
{{{
# moduleC.py
from moduleA import foo
foo()
# foo!

# Traceback (most recent call last):
#   File "<string>", line 1, in <string>
#NameError: name 'adder' is not defined
}}}
You can also use '{{{as}}}' in the {{{import}}} line to control the [[namespace]] the module is added to:
{{{
# import just moduleA.adder() into "a"
from moduleA import adder as a
num = a(4,4)

# import all of moduleA into namespace "a"
import moduleA as a
num = a.(4,4)
a.foo()
}}}
Finally, you can {{{import}}} 'everything' via the asterix '{{{*}}}' notation.  This means you can now access the [[attribute]]s without having to use dot notation (name dot attribute).  It  should be noted that for the most part, this is [[frowned upon|http://docs.python.org/howto/doanddont.html#from-module-import]].
{{{
from moduleA import *
print num
foo()
# 10
# foo!
}}}
As with the '{{{from module import attribute}}}' example (above), there is a danger with this:  It will stomp any pre-existing variable names with the new defintions.  Presume you had defined a function called {{{sys}}} in {{{moduleA.py}}}, then executed the above example.  The built-in {{{sys}}} command would no longer work within the current module (probably not the effect one is after).  However, I'm told some libraries\modules //are// acceptable to import this way, notably the built-in {{{Tkinter}}}.
----
''Differences in importing:''
Compare these two import statements (and additional equivalent statements sub-listed under them):
#{{{import someModule}}}
##{{{import someModule as sm}}}
#{{{from someModule import *}}}
##{{{from someModule import someAttr}}}
##{{{from someModule import someAttr as someName}}}
In the first example, one instance of {{{someModule}}} is shared (instanced) among every other module that imports it (in that fashion).  Any change you make in it or to it will appear in all modules importing it.
In the second example, a //copy// of the module is brought into the current scope.  Change made in the source {{{someModule}}} won't be be seen by other modules.  Basically, if you have {{{from}}} in the import statement, you're making a copy of the data.

Example:  Make the following modules:
{{{testGlobals.py}}} holds a few attributes that we can later change:
{{{
# testGlobals.py
foo = "foo"
spam = "spam"
}}}
{{{updateGlobals.py}}} imports (instances) testGlobals, and modifies its attributes (modifies the values in the source module).
{{{
# updateGlobals.py
import testGlobals
def update():
testGlobals.foo = "new FOO"
testGlobals.spam = "new SPAM"
}}}
{{{module_importInstance.py}}} imports an instance of {{{testGlobals.py}}} into its namespace, keeping a "live connection" to any changes that happen to the source module.
{{{
# module_importInstance.py
import testGlobals
import updateGlobals

print "Imported foo and spam values:"
print testGlobals.foo
print testGlobals.spam
updateGlobals.update()
print "foo and spam after source module update:"
print testGlobals.foo
print testGlobals.spam
}}}
{{{module_importCopy.py}}} //copies// the data from {{{testGlobals.py}}} into its namespace.  It no longer has any connection to the source module.
{{{
# module_importCopy.py
from testGlobals import *
import updateGlobals

print "Imported foo and spam values:"
print foo
print spam
updateGlobals.update()
print "foo and spam after source module update:"
print foo
print spam
}}}
Now, to see them in action, execute from the command prompt (Windows example):
{{{
c:\myPython\module_importInstance.py
Imported foo and spam values:
foo
spam
foo and spam after source module update:
new FOO
new SPAM
}}}
Above, you can see that the change made to the source {{{testGlobals.py}}} DID have a result in our module.
Next up...:
{{{
c:\myPython\module_importCopy.py
foo
spam
foo and spam after source module update:
foo
spam
}}}
Notice now that modifying the source module's values had no effect here, since a //copy// of that data was imported.
----
''Relative Imports''
If you have a [[package|Packages]] setup, you can do relative imports. Here are [[docs|http://docs.python.org/tutorial/modules.html#intra-package-references]]
{{{
# myModule.py

# These two are the same:
import someModule
from . import someModule

# This looks a dir up:
from .. import someOtherModule
}}}
The above examples we're looking in directories relative to {{{myModule.py}}}.
'single dot' says look in the current dir, 'double-dot' says look a dir up, etc.
----
''Private attributes and importing:''
There are two ways to limit how things are imported from a module, based on the '{{{from module import *}}}' statement:
#Prefixing names with a single underscore '{{{_}}}' will prevent that data being copied out
#Create a list variable called {{{__all__}}} with the names (as strings) of the attributes that //are// acceptable to be copied out.  This is sort of the opposite of the previous solution.
{{{
# secret.py
__all__ = ["notSecretVar"]

_secretVar = "secret!"
def _secretFunc():
print "very secret!"

notSecretVar = "not so secret"
def notSecretFunc():
print "not secret at all"
}}}
Execution:
{{{
from secret import *

print notSecretVar
# not so secret

notSecretFunc()
# Traceback (most recent call last):
#   File "<string>", line 1, in <string>
# NameError: name 'notSecretFunc' is not defined

print _secretVar
#Traceback (most recent call last):
#  File "<string>", line 1, in <string>
#NameError: name '_secretVar' is not defined

_secretFunc()
# Traceback (most recent call last):
#   File "<string>", line 1, in <string>
# NameError: name '_secretFunc' is not defined
}}}
None of the [[attribute]]s that had leading underscores were copied with the '{{{from module import *}}}' statement, so trying to execute them failed.
Furthermore, even though the function {{{notSecretFunc()}}} has no leading underscore, it //wasn't// added to the '{{{__all__}}}' list, so it wasn't exported out either.
Realize that these rules //only// matters when using the {{{from module import *}}} statement.  Doing a regular {{{import secret}}} //would// expose all of our 'secret' attributes.
----
When importing a module, Python has to go find it first.  This is the order it uses for the search:
#Home dir of the program (the location of the top-level module that was executed).
#{{{PYTHONPATH}}} dirs, if any are set.
#The Standard Library dirs:  {{{<drive>:\Python<ver>\Lib\*}}}
#Dirs defined by any {{{.pth}}} files (if they exist).
The result of these four become what {{{sys.path}}} returns.
----
Concepts:
*When imported, modules live in a [[namespace]].
*All of the names declared at the //top level// of a module become [[attribute]]s of the imported 'module object'.
*Importing gives access to names ([[attribute]]s) defined in the module's global [[scope|Python variable scope]]
Iteration is a common thing to do in Python:  A bunch of stuff, loop (iterate) over it.  The {{{for}}} statement is one of the most common ways to iterate over something:  Those objects know how how to respond to the {{{for}}} statement and iterate accordingly.
Python has a variety of different ways to do this based on different data types:

''File Objects'':
Iterating in a for loop:
{{{
for line in open('myfile.txt'):
print line
# the above example is better than the next (older) example,
# because it's easier to code, easier to run, and uses less memory:
print line
}}}
Using the file object's {{{.readline()}}} method:
{{{
f = open('myfile.txt')
# line one
# line two
f.close()
}}}
Same as above, but using the {{{.next()}}} method, which will raise a built-in {{{StopIteration}}} when the end is reached:
{{{
f = open('myfile.txt')
print f.next()
# line one
print f.next()
# line two
f.close()
}}}
''Lists'':
{{{
lister = ["a", "b", "c"]
it = iter(lister)
print it.next()
# a
}}}
The {{{iter}}} function will raise a {{{StopIteration}}} exception when it reaches the end of the list.  This is //awfully// similar to [[generator functions|Function]].

''Dictionary'':
The 'older' method of looping through 'key:value' pairs was to do this:
{{{
D = {"key1":"value1", "key2":"value2"}
for key in D.keys():
print key, D[key]
}}}
But more recent version of Python no longer require you to call the {{{.keys()}}} method, dictionaries now have a built-in iterator to do this for you:
{{{
D = {"key1":"value1", "key2":"value2"}
for key in D:
print D, D[key]
}}}
''List Comprehensions'':
{{{
# strip off all the return chars in a file, and put into a list,
# while iterating over a file:
clean = [line.rstrip() for line in open('myfile.txt')]
}}}
''OOP''
When creating your //own// objects via a Class, you can define a special {{{__iter__}}} method to support iteration.  Meaning, when you call:
{{{
for thing in myObjectList:
# doStuff...
}}}
it'll look to that objects {{{__iter__}}} method for what exactly should be happening...

In addition to sequence operations and list methods, you can //really// 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...
List comprehensions take lists, and create new lists from them.  They are enclosed in square brackets: {{{[]}}}
Python docs:
http://docs.python.org/tutorial/datastructures.html#list-comprehensions
(See notes on ''generator expressions'' at the bottom)
----
Given two list of numbers (of the same length, in this case they're vectors), add each index from each list toether:
{{{
vec1 = [1,0,0]
vec2 = [0,1,0]
sum = [vec1[i] + vec2[i] for i in range(len(vec1))]
# [1, 1, 0]
}}}
----
Some examples:  (I pulled a bunch of notes from 'Learning Python Third Edition by Mark Lutz')
{{{
# 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
>>> [2, 5, 8]
}}}
{{{
# add a value to the middle column (but don't actually modify the matrix)
print [row[1] + 1 for row in mx]
>>> [3, 6, 9]
}}}
{{{
# get only even numbers from the middle column:
print [row[1] for row in mx if row[1] % 2 == 0]
>>> [2, 8]
}}}
{{{
# pull diagonal values:
diagonal = [mx[i][i] for i in [0,1,2]]
print diagonal
>>> [1, 5, 9]
}}}
{{{
# Repeat chars in a string
word = "foo"
double = [i * 2 for i in word]
print double
>>> ['ff', 'oo', 'oo']
}}}
{{{
# Comparing list comprehensions, and for loops:

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

# For Loop equivalent:
squares = []
for i in range(1,6):
squares.append(i**2)
print squares
>>> [1, 4, 9, 16, 25]
}}}
----
Turn a list into a grouping of sublists.
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.
(Pulled from [[this post|http://www.garyrobinson.net/2008/04/splitting-a-pyt.html]])
{{{
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)]
# [[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]]
}}}
----
{{{
# return number, and cube root of number 1-5:
cubeRoots = [[x, x ** 3] for x in range(1,6)]
print cubeRoots
>>> [[1, 1], [2, 8], [3, 27], [4, 64], [5, 125]]
}}}
----
More examples I pulled directly from the [[Python reference|http://docs.python.org/tut/node7.html#SECTION007140000000000000000]]:
As you can see, you can also inlude {{{if}}} clauses in the list comprehension for testing:
{{{
# strip whitespace:
freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
print [weapon.strip() for weapon in freshfruit]
>>> ['banana', 'loganberry', 'passion fruit']
}}}
{{{
vec = [2, 4, 6]

print [3*x for x in vec]
>>> [6, 12, 18]
print [3*x for x in vec if x > 3]
>>> [12, 18]
print [3*x for x in vec if x < 2]
>>> []
print [[x,x**2] for x in vec]
>>> [[2, 4], [4, 16], [6, 36]]
print [(x, x**2) for x in vec]
>>> [(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]
>>> [8, 6, -18, 16, 12, -36, 24, 18, -54]
print [x+y for x in vec1 for y in vec2]
>>> [6, 5, -7, 8, 7, -5, 10, 9, -3]
print [vec1[i]*vec2[i] for i in range(len(vec1))]
>>> [8, 12, -54]
}}}
----
Working on multiple lists at once:
{{{
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
}}}
prints:
{{{
('a0', 'b0', 'c0')
('a0', 'b0', 'c1')
('a0', 'b1', 'c0')
('a0', 'b1', 'c1')
('a1', 'b0', 'c0')
('a1', 'b0', 'c1')
('a1', 'b1', 'c0')
('a1', 'b1', 'c1')
}}}

----
While there doesn't seem to be such thing as a '{{{tuple comprehension}}}', you can turn any list comprehension into a {{{tuple}}}:
{{{
foo = tuple([3*x for x in vec])
# (6, 12, 18)
}}}
I've seen examples of this:
{{{
foo = tuple(3*x for x in vec)
# (6, 12, 18)
}}}
Which creates a //generator expressions// (see below), and turns that into a {{{tuple}}}, but I've read it's actually slower that way.
----
You can also do a mashup of list comprehensions and [[generator functions|Function]] to make '''generator expressions'''.  They act like list comprehensions, but are surrounded in parenthesis instead of square brackets.
{{{
genEx = (x * 2 for x in ["a", "b", "c"])
print genEx.next()
# aa
print genEx.next()
# bb
# etc...
}}}
They raise a {{{StopIteration}}} exception when complete.
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.
See the [[generator function|Function]] notes for more info on their methods.
http://matplotlib.sourceforge.net/
*"matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala matlab or mathematica), web application servers, and six graphical user interface toolkits.

matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code."
A ''text file'' containing one or more Python  [[Statement]]s and/or definitions (of [[Function]]s, [[Class]]es & variables). Most modules also contain comments.  They are the 'highest level' organization unit in Python.
Modules are //processed// with two [[Statement]]s, and a built-in function:
*{{{import}}} - [[Python Docs|http://docs.python.org/ref/import.html]].
**Also see the built-in {{{__import__}}} function, which the {{{import}}} statement actually runs.  - [[Python Docs|http://docs.python.org/lib/built-in-funcs.html]]
*{{{from}}} - (same docs as above)
There are (at least) three kinds of modlues in Python:
*modules written in Python (.py)  [[Global Module Index|http://docs.python.org/modindex.html]]
*modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc)
*modules written in C and linked with the interpreter; to get a list of these, type:
<<<
{{{
import sys
print sys.builtin_module_names
}}}
<<<
Modules need to be [[import]]ed before they can be used in another module.
Once a module is [[import]]ed, you can use this to see a list of its [[attribute]]s:
{{{
dir(modulename)
}}}
Also after import, a special attribute '{{{.__name__}}}' is set on the module:  {{{__name__}}} holds the value of the current [[namespace]] that the object lives in.  If the module is the 'top level' being executed (meaning, it //hasn't// been imported, it's being directly executed) '{{{.__name__}}}' becomes the string {{{"__main__"}}}.  If the module has been imported, then '{{{.__name__}}}' is assigned to the [[namespace]] that the module was imported in to.  Check out '[[What's up with: if __name__ == "__main__":]]' for more on this.
Concepts:
*When imported, modules live in a [[namespace]].
*All of the names declared at the //top level// of a module become [[attribute]]s of the imported 'module object'.
*Importing gives access to names ([[attribute]]s) defined in the module's global [[scope|Python variable scope]]
----
''Special Module Attributes'':
*{{{__name__}}} is the module’s name
*{{{__doc__}}} is the module’s documentation string, or None if unavailable
*{{{__file__}}} is the pathname of the file from which the module was loaded, if it was loaded from a file.
----
General module contents:
{{{
"""module docstring"""

# imports
# constants
# exception classes
# interface functions
# classes
# internal functions & classes

def main(...):
...

if __name__ == '__main__':
status = main()
sys.exit(status)
}}}
----
Also see:
*[[Python File Formats]]

Namespaces are a fairly important concept to understand in Python.  Where to begin...

*Most commonly you author Python code that is saved in a module.  That module is in a namespace, which is normally the module name.  If you import //other// modules into it, each of the new modules exist in their own namespaces, defined by their own names (this can be changed though via the {{{import}}} command, more on this below).  Data in namespaces is accessed via 'dot notation', which basically is:
{{{
namespace dot attribute
}}}
*Which brings up the question, what is an [[attribute]]?  When you assign (bind) data (variables, functions, classes, etc) to a name (like {{{foo = 23}}}), that data becomes an //attribute// of the //module// it was authored in.  So that attribute now lives in the //namespace// of its parental module:
{{{
# someModule.py

#  Create a variable called foo.
#  It is now an attribute of someModule.py,
#  And lives in someModule's namespace.
foo = 23
}}}
**Inside of {{{someModule.py}}}, you can simply reference the attribute by name ({{{foo}}}) throughout it, since the parental namespace is assumed and need not be provided.  If {{{someModule.py}}} was imported into //another// module (thus placing it in the {{{someModule}}} namespace by default), you would need to reference the attribute via 'dot notation'.  See below examples below.
*All modules have a //special attribute// called {{{__name__}}} that holds the current namespace that the module exists in.  {{{__name__}}} can have two different values, depending on the state of the module:  If the module is being executed directly (Meaning, not imported into another module;  being executed directly from an icon, or from the command line), then {{{__name__}}} == {{{"__main__"}}}.  This tells the module that //it// is currently the 'top most' \ 'root' namespace.  However, if moduleA has been //imported// into moduleB, when querying {{{__name__}}} of moduleA, it will return the moduleA's name, thus showing the namespace that the module currently lives in.  Uh... what?  Examples!:
**Make two modules, and import one into another:
{{{
# moduleA.py

print "moduleA.__name__ = " + __name__
someVal = "spam!"
}}}
{{{
# moduleB.py

# This imports moduleA.py into the 'moduleA' namespace
import moduleA

# This will print the attribute .someVal from the
# namespace 'moduleA'
print moduleA.someVal
}}}
**And when {{{moduleB.py}}} is executed:
{{{
moduleA.__name__ = moduleA
23
}}}
** Since {{{moduleA.py}}} was imported into {{{moduleB.py}}}, {{{moduleA}}}'s {{{__name__}}} attr became //its own name//, since that was the name of the namespace it existed in.  Now, if you simply ran {{{moduleA}}} from the commandline, this is what it would print:
{{{
% python moduleA.py
moduleA.__name__ = __main__
}}}
** You can see that since {{{moduleA.py}}} //wasn't// imported, //it// then defines the 'top most namespace', thus it's {{{__name__}}} attribute became {{{"__main__"}}}
*These namespaces allow code to exist relative to other code without clashing into one another.  You could imagine that two different modules had functions with the same names.  If you imported both modules into a third module, without having a namespace to put them in, there would be hideous nameclashing.  But you can actually control what the namespace name is that you import something in to via the {{{import}}} command:
{{{
import moduleA as ma
}}}
*Simply tells the importing module that {{{moduleA}}} is now in the namespace {{{ma}}}.  Furthermore, you can import specific attributes from a module if you want, rather than the whole thing:
{{{
# moduleC.py
from moduleA import someVal
print someVal
}}}
*{{{someVal}}} has been imported directly into the namespace of moduleC, so it no longer needs to be access via dot notation.
If you're wondering how to get a list of all the attributes that exist in a namespace, that's what the {{{dir}}} function is for:
{{{
print dir(myfile)
['__builtins__', '__doc__', '__file__', '__name__', 'getNamespace', 'myFunc', 'someVal']
}}}
It returns a list containing all the attributes.  The ones with underscores are other 'special' attributes.
----
Also see:
*[[What's up with: if __name__ == "__main__":]]
*[[Python variable scope]]
http://docs.python.org/library/pickle.html
I recently had an issue where I'd {{{pickle}}} some data to disk, and add the data to Perforce (P4) so others could access.  But when the other team members would sync to the data, they couldn't use it.  They'd get an exception with this info:
{{{
No module named copy_reg
}}}
Looking online the common fix was to make sure your data was pickled in binary format. But this didn't help.
Doing some research, I found that the size of the file would get bigger after checking into P4, although when I'd diff the file it would say there were no differences.  The plot thickens....

Talking with our IT department, they came to the idea that maybe P4 was changing the line ending of the files:  If you open your Clientspec in P4, there is a '~LineEnd' drop-down, mine was set to 'local'.  Changing this to 'unix' and resubmitting the file suddenly fixed the problem.  But we couldn't have our whole team make this change, especially not knowing what other repercussions it may have, so I set it back.

So we took a look at the {{{pickle}}} docs.  Come to find out, introduced in Python 2.3, there was a new 'protocol' arg provided (protocol '2') which according to the docs: "...provides much more efficient pickling of new-style classes.".

{{{
outf = open(dataFile, 'wb')
pickle.dump(data, outf)
outf.close()
}}}
The new code is this:
{{{
outf = open(dataFile, 'wb')
pickle.dump(data, outf, 2)
outf.close()
}}}
Setting that new protocol value fixed the problem:  The data could be added to P4, other team-members could access it no problem.


The {{{print}}} statement seems pretty obvious.
{{{
print "foo"
# foo
}}}
But it holds untapped power...
First off, print is related to {{{sys.stdout}}}.  What was executed above could also be done this way:
{{{
import sys
sys.stdout.write("foo\n")
# foo
}}}
It's just a lot easier to do with {{{print}}} ;).  {{{print}}} sends things to the 'standard output' ({{{stdout}}}) with some formatting added, which normally includes the addition of a return character '{{{\n}}}'.
You can however, redirect the output of print, so it goes elsewhere rather than {{{stdout}}}:
{{{
myfile = open('myfile.txt', 'w')
print >> myfile, "foo!"
myfile.close()
}}}
Will send the text {{{"foo"}}} to the file object {{{myfile}}}.
You can also redirect {{{sys.stdout}}} to dump its data elsewhere, via something like this:
{{{
import sys
orig = sys.stdout
sys.stdout = open('output.txt', 'a')
# do you printing
print "this is a test"
# reset back to orig system
sys.stdout = orig
}}}
But that's a lot of hoop-jumping required, when you can just use the {{{print}}} redirection via {{{>>}}}.
http://docs.python.org/library/functions.html#property

Properties in Python give you a way to control the way attributes on a class are accessed, set, and deleted.  The above link goes through several different ways of doing it, both via the {{{property}}} //function//, and implementing the {{{property}}} function via a [[decorator|decorator]], {{{@property}}}.  Below I'll show an example using both.

''Why would you want to use properties?''  Properties give you control over attribute access.  The expose methods to look like attributes, so when that the user access that attribute, in fact a method is being called, and can provide an additional level of logic before the value is returned, set, or deleted.

For example, say you want to give the user the ability to modify an object's attribute, but want to make sure its within a given range (from the example below, acceptable values are from 0 -> 10).  //Without// the property, if the user executed '{{{v.val = 20}}}', Python would happily oblige the request.  But by making {{{val}}} a property, it's forced to use the given get, set, and delete functions, thus putting some security into your code.

This is a 'decorator property' example.  This can only be used in Python 2.6 and newer.
{{{
class Foo(object):

def __init__(self):
# We put an underscore in front of the attr to tell any
# users that this should be considered 'private'...
self._val = 10

@property
def val(self):
"I'm the 'val' property"
return self._val

@val.setter
def val(self, val):
if 10 >= val >= 0:
self._val = val
else:
raise ValueError("Out of range")

@val.deleter
def delVal(self):
del self._val

v = Foo()
v.val = 5  # This calls Foo.val.setter
}}}
And this is a 'property function' example, introduced in Python 2.2.
{{{
class Foo(object):

def __init__(self):
# We put an underscore in front of the attr to tell any
# users that this should be considered 'private'...
self._val = 10

def getVal(self):
return self._val

def setVal(self, val):
if 10 >= val >= 0:
self._val = val
else:
raise ValueError("Out of range")

def delVal(self):
del self._val

val = property(getVal, setVal, delVal, "I'm the 'val' property")

v = Foo()
v.val = 20

# ValueError: Out of range
}}}

http://www.pyglet.org/
*"A cross-platform windowing and multimedia library for Python."
*"pyglet provides an object-oriented programming interface for developing games and other visually-rich applications for Windows, Mac OS X and Linux."
*http://steveasleep.com/pyglettutorial.html
*http://code.google.com/p/daftpython/source/browse/trunk/Starfield/starpyglet.py
I use [[Wing|Wing IDE]] as my Python IDE.  It has the ability to use ''pylint''.  What is pylint?
*pylint... (from their page):
>"...analyzes Python source code looking for bugs and signs of poor quality"
>"...a python tool that checks if a module satisfies a coding standard. Pylint is similar to ~PyChecker but offers more features, like checking line-code's length, checking if variable names are well-formed according to your coding standard, or checking if declared interfaces are truly implemented, and much more..."
*pylint user manual:  http://www.logilab.org/card/pylint_manual
*pylint features: http://www.logilab.org/card/pylintfeatures
*Wing-pylint install docs: http://www.wingware.com/doc/edit/pylint
!!!How to install, configure, & run:
I found the docs for installing pylint a bit... scattered, so when I first installed it it failed to run.  Maybe there's an easier way I completely missed, but here are the steps I used to get it working:
!!!!pylint install
**http://www.logilab.org/project/pylint
**I had to rename the extracted pylint dir from the 'version numbered version' to just "pylint".
!!!!pylint dependencies install
*In Python's {{{\site-packages}}} dir, make a {{{\logilab}}} dir.  Place an empty {{{__init__.py}}} inside it (to make it a '[[package|Packages]]')
**http://www.logilab.org/project/logilab-common
**Extract to the new {{{\logilab}}} package dir. Rename extracted root dir to just "{{{common}}}".
**http://www.logilab.org/project/logilab-astng
**Extract to the new {{{\logilab}}} package dir. Rename extracted root dir to just "{{{astng}}}".
!!!!Configure pylint in Wing
*Tools Menu -> press {{{PyLint}}} to open that ui.
*RMB in the UI, -> Configure
**This will open the {{{pylintpanel.cfg}}} Wing file.
*On the line that has '{{{command = }}}', paste in the full path to the {{{pylint.bat}}} file.  Mine was here:
**{{{C:\Python26\Lib\site-packages\pylint\bin\pylint.bat}}}
*Close the cfg file.
!!!!Run pylint in Wing
*Open a module in Wing.
*In the ~PyLint window, RMB -> 'Update for <moduleName>.py'
*After doing some thinking, it should populate the various pylint 'Error', 'Warnings', & 'Info' tabs.
Should hopefully be good to go!  pylint has a lot of ways to be configured, and by default it appears to be 'the most verbose'.  There are options to change this, but I'm just not learning it myself...
----
I should note that after all of this I found an EasyInstall for pylint, but running that confused me even more (didn't include the pylint.bat file), so I reverted back to the above method.


{{{pywin32}}} is a package letting Python more easily interface with Windows.
*http://sourceforge.net/projects/pywin32/
Resources:
*http://win32com.goermezer.de/
[[Function]]s and [[Method]]s can both return data.
{{{
def funcA(x, y):
a = x * y
return a
foo = funcA(5.0, 10.0)
# 50.0
}}}
However, the {{{return}}} statement can actually return back multiple objects, but it returns them as a single {{{tupple}}}:
{{{
def funcB(x, y):
a = x / y
b = y * x
return a, b
foo = funcB(5.0, 10.0)
# (0.5, 50.0)
}}}
But if we expect the tupple, we can unpack it:
{{{
foo, goo = funcB(5.0, 10.0)
print foo
# 0.5
print goo
# 50.0
}}}
[[Python docs|http://docs.python.org/ref/return.html]] for {{{return}}}.
http://scikit-learn.org/stable/
<<<
scikit-learn is a Python module integrating classic machine learning algorithms in the tightly-knit scientific Python world (numpy, scipy, matplotlib). It aims to provide simple and efficient solutions to learning problems, accessible to everybody and reusable in various contexts: machine-learning as a versatile tool for science and engineering.
<<<
Since Python 2.4, they've introduced the built-in types {{{set}}} and {{{frozenset}}}:
Docs:
http://docs.python.org/library/stdtypes.html#set
I often do type checks against strings, and do things if strings are found:
{{{
if type(myVar) == type(""):
# do something
}}}
However, there are different types of strings, an the above example can easily fail:
{{{
print type(u"")
print type(r"")
print type("")

<type 'unicode'>
<type 'str'>
<type 'str'>
}}}
To solve for this, we can compare against the superclass of all strings, {{{basestring}}}:
{{{
if issubclass(type(myVar), basestring):
# do stuff
}}}
Some notes to remind me the basics of the '{{{with}}}' statement.
In a nutshell, it lets you wrap a block of code in a 'context' that controls how that block will be entered, and exited.  It also catches exceptions for you, and returns any that occurred, so it takes care of the {{{try}}}\{{{except}}} clause automatically.
----
http://docs.python.org/reference/datamodel.html#context-managers
http://docs.python.org/reference/compound_stmts.html#with
http://docs.python.org/library/stdtypes.html#context-manager-types
http://www.python.org/dev/peps/pep-0343/
{{{
class CtxManager(object):
"""
A context manager object.  Has two methods, each which support a special
type of return.
"""
def __enter__(self):
"""
Called when the context is entered.

return : Can return itself (or another object related to the context) to
provide data from this context to the enclosed block, and will be
persistent when the with clause exits.
"""
print "Entering Context!"
self.data = 23
# If you don't return self, you can't access this object inside the with clause:
return self

def __exit__(self, exc_type, exc_value, traceback):
"""
Called when the contex is exited.

exc_type : If there is an Exception, the type.  Otherwise None
exec_value : If there is an Exception, the value.  Otherwise None
traceback : If there is an Exception, the traceback.  Otherwise None

return : True \ False : If False, and there was an exception, re-raise the
exception.  If True and there was an exception, handle the exception
silently.
"""
if exc_type:
print "Exiting Context, caught exceptions:", exc_type, exc_value
else:
print "Exiting Context!"
return True
}}}
And to put it in action, you use this type of syntax.  Notice how you can access the instanced object {{{a}}} //outside// of the with clause?  This allows a context manager to (among other things) track the data it's managing, and expose the results to you later.
{{{
with CtxManager() as a:
print "\tInside 'CtxManager()'"
# Access data from the context itself:
print "\tContext data:", a.data
a.data = "42"
print "Outside data update:", a.data
}}}
prints:
{{{
Entering Context!
Inside 'CtxManager()'
Context data: 23
Exiting Context!
Outside data update: 42
}}}