Welcome to TiddlyWiki created by Jeremy Ruston, Copyright © 2007 UnaMesa Association
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
**All about special {{{__methods__}}}
*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.
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.
I originally had this page hosted at these locations (it moved around):
*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.
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]]
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:
***{{{prototype(address)}}}
****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'
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)
----
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
tempfile = os.path.join(os.getenv("TMP"), "tempfile.txt")
print tempfile
}}}
On Vista:
{{{
C:\Users\<USERNAME>\AppData\Local\Temp\tempfile.txt
}}}
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>>
The python {{{math}}} module is missing calls to any kind of vector math. :-( I've currently found a few solutions, {{{PyEuclid}}}, {{{NumPy}}}, {{{VPython}}} and {{{gameobjects}}}. I've done a little looking over {{{VPython}}}, havn't delved too deep into {{{NumPy}}}, {{{PyEuclid}}} or {{{gameobjects}}} yet (notes at the bottom). I also have some notes on doing it by hand [[here|Vector math]].
!gameobjects
*Main Page: http://code.google.com/p/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/
*Source: http://code.google.com/p/pyeuclid/
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
*Download: http://vpython.org/download.html
*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
<<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!
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.
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).
#The value printed from the {{{bar}}} method.
----
''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
*{{{Menu}}} : Options associated with a {{{Menubutton}}} or top-level window
*{{{Menubutton}}} : Button that opens a Menu of selectable options/submenus
*{{{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]]
Constants in Python have variable names in ALL CAPS:
{{{
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//....
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.
When authoring a [[function|Function]] or [[method|Method]] definition, you have the option of creating [[argument]]s (which is usually done). 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(arg1 = "first", arg2 = "second"):
print arg1
print arg2
>> foo()
first
second
}}}
In the above example, the arguments had default values, 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 arguments have default values. FYI, if any arguments //won't// have default values, they need to be listed //first//.
{{{
def foo(arg1, arg2 = "second"):
print arg1
print arg2
>> foo(1)
1
second
}}}
Since no default argument was provided for the first argument, I had to provide one. If I had just executed {{{foo()}}}, I would have got a {{{TypeError}}}.
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.
''Tips and Tricks:''
{{{
# Simple creation:
d = {"duck": "eend", "water": "water"}
}}}
Create by zipping two lists:
{{{
keys = ["key1", "key2"]
values = [23, 53]
D = dict(zip(keys, values))
# {'key2': 53, 'key1': 23}
}}}
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 = {}
# add our sub-dictionary
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 ({})
}}}
For the longest time I heard about 'Python Eggs' and '~EasyInstall', finally decided to dig into it!
*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.
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.
----
Also see:
*[[How can I reduce the precision of a floating point number?]]
*http://docs.python.org/library/stdtypes.html#string-formatting
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
}}}
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]].
!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:
{{{
C:\Python26\Lib>python pydoc.py -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
}}}
!Epydoc:
http://epydoc.sourceforge.net/
!docutils
http://docutils.sourceforge.net/
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__":]]
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.
(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\}}}
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."
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',)
}}}
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'>
}}}
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.
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')
}}}
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)
}}}
{{{
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]]
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()
}}}
Python 2.6 and //newer// ({{{os.popen}}} is deprecated). Use {{{subprocess}}}:
http://docs.python.org/library/subprocess.html
{{{
import subprocess
}}}
----
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
[[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]]
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...
}}}
{{{
import modulefinder
}}}
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'>
}}}
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:\\')
}}}
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!
{{{
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
}}}
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 Maya, or different implementations.
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
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?]]
{{{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?]]
{{{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?]]
{{{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")
}}}
{{{
import os
os.listdir("c:/temp")
}}}
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.
{{{
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?]]
{{{
import socket
help(socket)
}}}
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.
In Windows:
{{{
from win32com.client import Dispatch
# Open Photoshop
photoshop = Dispatch("Photoshop.Application")
# Open a file
photoshop.Open("c:/temp/tempfile.bmp")
# Run an action on it
result = photoshop.PlayAction("actionname")
# insert additional code #
}}}
Running this from Maya's integration of Python //doesn't// seem to work :(
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
def __add__(self, obj):
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.
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...
}}}
{{{
# code...
raw_input("Press the enter key to exit.")
}}}
{{{
# Windows only:
import os
os.startfile("notepad")
}}}
----
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
{{{
import os
os.chmod("c:/temp/foo.txt", 0777)
}}}
If you're wondering: http://www.perforce.com/
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?]]
''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)
See Python [[docs|http://docs.python.org/library/_winreg.html]]
{{{
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]]
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.
In Windows:
{{{
name = os.getenv('USERNAME')
profile = os.getenv('USERPROFILE')
}}}
On Vista:
{{{
yourname
C:\Users\yourname
}}}
Not that I run into this very often, BUT, it's nice to know :)
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 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
}}}
{{{
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?
}}}
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]]
{{{
import datetime
print datetime.date.today()
# 2008-05-19
print datetime.datetime.now()
# 2009-06-24 15:35:37.787000
}}}
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.....
{{{os.environ}}} returns a [[Dictionary|DICTIONARY]]
{{{
import os
var = os.getenv("PATH")
}}}
*Also see: [[How can I get a list of Environment Variables?]]
{{{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]]
And strip off the newline characters in the process?
{{{
f = open('myfile.txt', 'r').readlines()
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...]
}}}
{{{
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?]]
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:
{{{
reload(myModule)
}}}
{{{
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'
}}}
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 already exists:
{{{
import os
if not os.path.isdir("c:/myPath"):
os.mkdir("c:/myPath")
}}}
You can also use for the test:
{{{
os.path.lexists("c:/myPath")
}}}
But if you're trying to build a whole new tree of dirs, use {{{os.makedirs()}}}.
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
}}}
----
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]]
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
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
----
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.
{{{
pather = r"c:\temp\foo.txt"
print os.path.splitext(pather)
# ('c:\\temp\\foo', '.txt')
print os.path.splitext(pather)[0]
# c:\temp\foo
}}}
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"
}}}
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.
{{{
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
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]]
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
}}}
----
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?]]
{{{
import os
os.mkdir("c:/temp")
}}}
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
}}}
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]]
<<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:''
#You can do key-word searches in the search-field in the top of the //right column//.
#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()
}}}
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]
}}}
Add lists:
{{{
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
}}}
http://www.livewires.org.uk/python/home
Common ~LiveWire modules, their Classes & Methods:
*{{{games}}} : "The module for use with the games worksheets in the ~LiveWires Python Course."
**{{{Games_Object}}} : Foundation //class// from which classes {{{Text}}}, {{{Message}}}, and {{{Sprite}}} are based. It is an //abstract class//, never directly instanced. It has many methods that they inherit.
***{{{Text}}} : A graphics object for text displayed on a {{{Screen}}} object.
***{{{Message}}} : A graphics object that is a special kind of {{{Text}}} object that disappears after a set period of time.
***{{{Sprite}}} : A graphics object for images that can be displayed on a {{{Screen}}} object.
****{{{init_sprite(screen, x, y, image)}}}
**{{{Screen}}} : Creates a region on which graphics objects my exist, move and interact. The top left corner is x:0, y:0. Methods:
***{{{set_background(image)}}} : First need to load an image via {{{games.load_image()}}}
***{{{mouse_pos()}}}
***{{{mouse_visible(on)}}}
***{{{mainloop([fps])}}}
***{{{all_objects()}}}
***{{{clear()}}}
***{{{quit()}}}
**{{{load_image}}} : Load a background image. Can be bmp, gif, png, pcx, or tga. By default, transparency is on: The color in the upper left hand corner (0,0) will define the transparent color. Otherwise, you can set {{{transparent = False}}}
*{{{colour}}} : Note the //~English-European// spelling ;-)
**{{{black}}} : One of the many [[Constants]] that can be easily accessed to give other objects color.
*{{{beginners}}} : This library provides a simple graphics API to allow procedural programming using (in this instance) Tk to provide an output window. It is intended, however, to be graphics-subsystem independent.
*{{{boards}}} : "A module for producing board games with a regular grid. This is a new module for release 2.1 and effectively still under development."
**{{{GameBoard}}} :
**{{{GameCell}}} :
Note: Like with [[Tkinter|UI basics with Tkinter]], don't run a {{{livewires}}} program (that creates its own window) from your IDE (like IDLE).
Instead, run the program directgly by double-clicking the programs icon in Windows.
----
Also see:
*[[What help do I have to author games in Python?]]
/***
|''Name:''|LoadRemoteFileThroughProxy (previous LoadRemoteFileHijack)|
|''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|
|''Source:''|http://tiddlywiki.bidix.info/#LoadRemoteFileHijack|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0|
***/
//{{{
version.extensions.LoadRemoteFileThroughProxy = {
major: 1, minor: 1, revision: 0,
date: new Date("mar 17, 2007"),
source: "http://tiddlywiki.bidix.info/#LoadRemoteFileThroughProxy"};
if (!window.bidix) window.bidix = {}; // bidix namespace
if (!bidix.core) bidix.core = {};
bidix.core.loadRemoteFile = loadRemoteFile;
loadRemoteFile = function(url,callback,params)
{
if ((document.location.toString().substr(0,4) == "http") && (url.substr(0,4) == "http")){
url = store.getTiddlerText("SiteProxy", "/proxy/") + url;
}
return bidix.core.loadRemoteFile(url,callback,params);
}
//}}}
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():
# made local first thing:
shoe = 32
}}}
!!!Make a function to explicitly modify the //global var//, and call to it:
{{{
def modShoe():
return shoe * 2
def main():
var = modShoe()
}}}
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]]
*[[About python tiddlywiki]]
*[[Instructions for use]]
*[[Latest Updates|History]]
*[[Python Links]]
*[[Blog]]
*[[About author|WarpCat]]
----
''Subscribe'':
''[[RSS|http://pythonwiki.tiddlyspot.com/index.xml]]'' [img[http://farm1.static.flickr.com/197/492915948_b4a9f3233e.jpg?v=0][http://pythonwiki.tiddlyspot.com/index.xml]]
----
[[All Subjects]]
----
''Categories:''
[[DICTIONARY]]
[[ENVIRONMENT]]
[[EXECUTION]]
[[EXTERNAL APPS]]
[[FILESYSTEMS]]
[[FUNDAMENTALS]]
[[INFO]]
[[I/O]]
[[LIST]]
[[MATH]]
[[MEDIA]]
[[NUMBERS]]
[[OOP]]
[[PYGAME]]
[[STRING]]
[[UI]]
[[VARIABLES]]
[[WEB]]
[[XML]]
http://docs.python.org/dev/3.0/library/tempfile.html
http://blog.doughellmann.com/2008/02/pymotw-tempfile.html
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:}}}.
''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
}}} '
*{{{_single_leading_underscore}}}
**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')}}}
*{{{__double_leading_underscore}}}
**when naming a [[class|Class]] attribute, invokes name mangling (inside {{{class FooBar}}}, {{{__boo}}} becomes {{{_FooBar__boo}}}). See [[Private Attributes]] & [[Private Methods]]
*{{{__double_leading_and_trailing_underscore__}}}
**"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 ;)
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:
__add__
__class__
__contains__
__delattr__
#... many, many more....
}}}
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__}}}
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):
def __init__(self, answer):
# Executed to when an object is first instanced
self.value = answer
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 "CallMe('"+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`
# CallMe('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?]]
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.
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
Here's the root installer download page, to grab the latest version:
*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
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])
}}}
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!
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
''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/
http://code.google.com/p/pymt/
*"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]]
*{{{.py}}} -- Standard text-based Python file ([[module]]), authored //in Python//. To be considered Python 'source code'.
*{{{.pyc}}} -- 'Python Compiler Script'. A 'byte-code 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'. 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
http://code.google.com/p/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:
***[[2.6.2|http://www.python.org/download/releases/2.6.2/NEWS.txt]] [[2.6.1|http://www.python.org/download/releases/2.6.1/NEWS.txt]] [[2.6|http://www.python.org/download/releases/2.6/NEWS.txt]] [[2.5.4|http://www.python.org/download/releases/2.5.4/NEWS.txt]] [[2.5.3|http://www.python.org/download/releases/2.5.3/NEWS.txt]] [[2.5.2|http://www.python.org/download/releases/2.5.2/NEWS.txt]] [[2.5.1|http://www.python.org/download/releases/2.5.1/NEWS.txt]] [[2.5|http://www.python.org/download/releases/2.5/NEWS.txt]]
**[[Download the documentation|http://docs.python.org/download.html]]
**[[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:
*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.
----
''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.
!!Online Books (free):
*[[Dive Into Python - Mark Pilgrim|http://diveintopython.org/]] - I have yet to spend much time with this, but the whole book is available to download for free, nice!
*[[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."
!!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.
[[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 entirely 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/
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
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.
Places to paste Python code for good quality sharing:
*http://codepad.org/
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__
# give user direct access to all modules in __builtin__
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__]]
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)
}}}
[[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]]
{{{
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?]]>>
----
{{{
>>> 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.
!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:
outf = open(dataFile, 'w')
cPickle.dump(data, outf)
outf.close()
# Read/unpickle/unserialize/load the data:
inf = open(dataFile)
loadedData = cPickle.load(inf)
inf.close()
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
!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
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
Simple pseudo-code example, working with serial communication on ~COM5 (Which on my machine, is where my [[Arduino|http://www.arduino.cc/]] lives...)
{{{
import serial
ser = serial.Serial('COM5', timeout=1)
# Clean the buffer before we read it, so we only get the
# most recently received item:
ser.flushInput()
serialValue = ser.readline().strip()
}}}
I've found that putting this into a while loop, to continuously print data just locks up the shell. However, this code //does// work in Pygame, which has better control over the framerate.
<<search>><<closeAll>><<permaview>><<newTiddler>><<newJournal 'YYYY 0MM 0DD '>><<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>>
notes on learning the language
[img[python wiki|http://farm4.static.flickr.com/3026/2514848314_335cab2127.jpg?v=0][Welcome]]python wiki
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, 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]]
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 "flying saucer"
}}}
The ''command'' is {{{print}}}. Tells the computer to take action.
The ''expression'' is the string {{{"flying saucer"}}}. It is what the command acts upon.
----
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
}}}
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.
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]] ");
MainMenu = MainMenu.replace(/^/,"[[WelcomeToTiddlyspot]] ");
}
// create some shadow tiddler content
merge(config.shadowTiddlers,{
'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?// @@ 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>>",
"See also GettingStarted.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Working online// @@ 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// @@ 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!// @@ 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 :)// @@ 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':[
"| tiddlyspot password:|<<option pasUploadPassword>>|",
"| site management:|<<upload http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/store.cgi index.html . . " + config.tiddlyspotSiteId + ">>//(requires tiddlyspot password)//<br>[[control panel|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/controlpanel]], [[download (go offline)|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/download]]|",
"| 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':[
"<<upload http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/store.cgi index.html . . " + config.tiddlyspotSiteId + ">><html><a href='http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/download' class='button'>download</a></html>"
].join("\n"),
'TspotOptions':[
"tiddlyspot password:",
"<<option pasUploadPassword>>",
""
].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)
For our button command, I wrapper the command with {{{lambda}}}. While in this case it actually isn't needed, {{{lambda}}} //is// needed if you want to pass any kind of arguments to your button command. 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.
{{{
from Tkinter import *
class App(object):
def __init__(self):
self.run()
def run(self):
self.root = Tk()
self.root.title("TkWin 02")
self.root.geometry("250x150")
self.display()
self.root.mainloop()
def display(self):
self.frame = Frame(self.root)
self.frame.grid()
self.label = Label(self.frame, text="This is a top label that spans multiple columns")
self.label.grid(row=0, column=0, columnspan=2, sticky=W)
self.someText = Label(self.frame, text="Enter something:")
self.someText.grid(row=1, column=0, sticky=W)
self.entry = Entry(self.frame)
self.entry.grid(row=1, column=1, sticky=W)
self.b1 = Button(self.frame,
text="Press!",
command=lambda:self.update_text())
self.b1.grid(row=2, column=0, sticky=W)
self.textBox = Text(self.frame, width=35, height=5, wrap=WORD)
self.textBox.grid(row=3, column=0, columnspan=2, sticky=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:
----
Some xml terminology links:
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>
}}}
----
''Comments'':
Surrounding text with {{{<!-- -->}}} will cause the xml to igore the markup:
{{{
<!-- This is a comment that will be ignored -->
}}}
----
''Declaration'':
Xml files can optionally start with a 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. As I have read, {{{__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. What does that actually mean? Example:
{{{
num = 3.1415 * 2
print num
>>> 6.283 # print calls to the __str__ method of num!
print num.__repr__() # complete with floating-point goofiness
>>> 6.2830000000000004
print num.__str__()
>>> 6.283
}}}
----
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]].
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
}}}
----
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.
Generators are created by adding 'yield' to a function loop. To create a "generator object", you must call it with parenthesis after it:
{{{
gen()
}}}
The function itself looks something like this:
{{{
def gen(things):
for thing in things:
// ...do stuff with things...
yield newThing
}}}
Generators have a default .next method you can call:
{{{
gen.next()
}}}
Need more info on this
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]
def adder(x):
return x + x
map(adder, numbers)
[2, 4, 6, 8]
}}}
Map can also take more than one sequence arugment.
{{{
letters = ["a", "b", "c", "d"]
nums = ["1", "2", "3", "4"]
def addtwo(x, y):
return x+y
map(addtwo, nums, letters)
['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 |
| 28/01/2010 09:14:11 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 08/02/2010 08:46:29 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 08/02/2010 08:47:59 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 09/02/2010 10:32:31 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 12/02/2010 08:17:26 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 19/02/2010 13:47:39 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . | ok |
| 19/02/2010 13:53:48 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 04/03/2010 12:05:50 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 15/03/2010 14:52:08 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
| 18/03/2010 08:33:15 | WarpCat | [[/|http://pythonwiki.tiddlyspot.com/]] | [[store.cgi|http://pythonwiki.tiddlyspot.com/store.cgi]] | . | [[index.html | http://pythonwiki.tiddlyspot.com/index.html]] | . |
/***
|''Name:''|PasswordOptionPlugin|
|''Description:''|Extends TiddlyWiki options with non encrypted password option.|
|''Version:''|1.0.2|
|''Date:''|Apr 19, 2007|
|''Source:''|http://tiddlywiki.bidix.info/#PasswordOptionPlugin|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0 (Beta 5)|
***/
//{{{
version.extensions.PasswordOptionPlugin = {
major: 1, minor: 0, revision: 2,
date: new Date("Apr 19, 2007"),
source: 'http://tiddlywiki.bidix.info/#PasswordOptionPlugin',
author: 'BidiX (BidiX (at) bidix (dot) info',
license: '[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D]]',
coreVersion: '2.2.0 (Beta 5)'
};
config.macros.option.passwordCheckboxLabel = "Save this password on this computer";
config.macros.option.passwordInputType = "password"; // password | text
setStylesheet(".pasOptionInput {width: 11em;}\n","passwordInputTypeStyle");
merge(config.macros.option.types, {
'pas': {
elementType: "input",
valueField: "value",
eventName: "onkeyup",
className: "pasOptionInput",
typeValue: config.macros.option.passwordInputType,
create: function(place,type,opt,className,desc) {
// password field
config.macros.option.genericCreate(place,'pas',opt,className,desc);
// checkbox linked with this password "save this password on this computer"
config.macros.option.genericCreate(place,'chk','chk'+opt,className,desc);
// text savePasswordCheckboxLabel
place.appendChild(document.createTextNode(config.macros.option.passwordCheckboxLabel));
},
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])
saveOptionCookie(opt);
return config.options[name] ? "true" : "false";
}
});
merge(config.optionHandlers, {
'pas': {
get: function(name) {
if (config.options["chk"+name]) {
return encodeCookie(config.options[name].toString());
} else {
return "";
}
},
set: function(name,value) {config.options[name] = decodeCookie(value);}
}
});
// need to reload options to load passwordOptions
loadOptionsCookie();
/*
if (!config.options['pasPassword'])
config.options['pasPassword'] = '';
merge(config.optionsDesc,{
pasPassword: "Test password"
});
*/
//}}}
/***
|''Name:''|UploadPlugin|
|''Description:''|Save to web a TiddlyWiki|
|''Version:''|4.1.0|
|''Date:''|May 5, 2007|
|''Source:''|http://tiddlywiki.bidix.info/#UploadPlugin|
|''Documentation:''|http://tiddlywiki.bidix.info/#UploadPluginDoc|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0 (#3125)|
|''Requires:''|PasswordOptionPlugin|
***/
//{{{
version.extensions.UploadPlugin = {
major: 4, minor: 1, revision: 0,
date: new Date("May 5, 2007"),
source: 'http://tiddlywiki.bidix.info/#UploadPlugin',
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
//
// Upload Macro
//
config.macros.upload = {
// default values
defaultBackupDir: '', //no backup
defaultStoreScript: "store.php",
defaultToFilename: "index.html",
defaultUploadDir: ".",
authenticateUser: true // UploadService Authenticate User
};
config.macros.upload.label = {
promptOption: "Save and Upload this TiddlyWiki with UploadOptions",
promptParamMacro: "Save and Upload this TiddlyWiki in %0",
saveLabel: "save to web",
saveToDisk: "save to disk",
uploadLabel: "upload"
};
config.macros.upload.messages = {
noStoreUrl: "No store URL in parmeters or options",
usernameOrPasswordMissing: "Username or password missing"
};
config.macros.upload.handler = function(place,macroName,params) {
if (readOnly)
return;
var label;
if (document.location.toString().substr(0,4) == "http")
label = this.label.saveLabel;
else
label = this.label.uploadLabel;
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);
};
config.macros.upload.action = function(params)
{
// 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;
var uploadDir = params[3] ? params[3] : config.options.txtUploadDir;
var username = params[4] ? params[4] : config.options.txtUploadUserName;
var password = config.options.pasUploadPassword; // for security reason no password as macro parameter
// for still missing parameter set default value
if ((!storeUrl) && (document.location.toString().substr(0,4) == "http"))
storeUrl = bidix.dirname(document.location.toString())+'/'+config.macros.upload.defaultStoreScript;
if (storeUrl.substr(0,4) != "http")
storeUrl = bidix.dirname(document.location.toString()) +'/'+ storeUrl;
if (!toFilename)
toFilename = bidix.basename(window.location.toString());
if (!toFilename)
toFilename = config.macros.upload.defaultToFilename;
if (!uploadDir)
uploadDir = config.macros.upload.defaultUploadDir;
if (!backupDir)
backupDir = config.macros.upload.defaultBackupDir;
// report error if still missing
if (!storeUrl) {
alert(config.macros.upload.messages.noStoreUrl);
clearMessage();
return false;
}
if (config.macros.upload.authenticateUser && (!username || !password)) {
alert(config.macros.upload.messages.usernameOrPasswordMissing);
clearMessage();
return false;
}
bidix.upload.uploadChanges(false,null,storeUrl, toFilename, uploadDir, backupDir, username, password);
return false;
};
config.macros.upload.destFile = function(storeUrl, toFilename, uploadDir)
{
if (!storeUrl)
return null;
var dest = bidix.dirname(storeUrl);
if (uploadDir && uploadDir != '.')
dest = dest + '/' + uploadDir;
dest = dest + '/' + toFilename;
return dest;
};
//
// uploadOptions Macro
//
config.macros.uploadOptions = {
handler: function(place,macroName,params) {
var wizard = new Wizard();
wizard.createWizard(place,this.wizardTitle);
wizard.addStep(this.step1Title,this.step1Html);
var markList = wizard.getElement("markList");
var listWrapper = document.createElement("div");
markList.parentNode.insertBefore(listWrapper,markList);
wizard.setValue("listWrapper",listWrapper);
this.refreshOptions(listWrapper,false);
var uploadCaption;
if (document.location.toString().substr(0,4) == "http")
uploadCaption = config.macros.upload.label.saveLabel;
else
uploadCaption = config.macros.upload.label.uploadLabel;
wizard.setButtons([
{caption: uploadCaption, tooltip: config.macros.upload.label.promptOption,
onClick: config.macros.upload.action},
{caption: this.cancelButton, tooltip: this.cancelButtonPrompt, onClick: this.onCancel}
]);
},
refreshOptions: function(listWrapper) {
var uploadOpts = [
"txtUploadUserName",
"pasUploadPassword",
"txtUploadStoreUrl",
"txtUploadDir",
"txtUploadFilename",
"txtUploadBackupDir",
"chkUploadLog",
"txtUploadLogMaxLine",
]
var opts = [];
for(i=0; i<uploadOpts.length; i++) {
var opt = {};
opts.push()
opt.option = "";
n = uploadOpts[i];
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;
},
wizardTitle: "Upload with options",
step1Title: "These options are saved in cookies in your browser",
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'}
]}
}
//
// upload functions
//
if (!bidix.upload) bidix.upload = {};
if (!bidix.upload.messages) bidix.upload.messages = {
//from saving
invalidFileError: "The original file '%0' does not appear to be a valid TiddlyWiki",
backupSaved: "Backup saved",
backupFailed: "Failed to upload backup file",
rssSaved: "RSS feed uploaded",
rssFailed: "Failed to upload RSS feed file",
emptySaved: "Empty template uploaded",
emptyFailed: "Failed to upload empty template file",
mainSaved: "Main TiddlyWiki file uploaded",
mainFailed: "Failed to upload main TiddlyWiki file. Your changes have not been saved",
//specific upload
loadOriginalHttpPostError: "Can't get original file",
aboutToSaveOnHttpPost: 'About to upload on %0 ...',
storePhpNotFound: "The store script '%0' was not found."
};
bidix.upload.uploadChanges = function(onlyIfDirty,tiddlers,storeUrl,toFilename,uploadDir,backupDir,username,password)
{
var callback = function(status,uploadParams,original,url,xhr) {
if (!status) {
displayMessage(bidix.upload.messages.loadOriginalHttpPostError);
return;
}
if (bidix.debugMode)
alert(original.substr(0,500)+"\n...");
// Locate the storeArea div's
var posDiv = locateStoreArea(original);
if((posDiv[0] == -1) || (posDiv[1] == -1)) {
alert(config.messages.invalidFileError.format([localPath]));
return;
}
bidix.upload.uploadRss(uploadParams,original,posDiv);
};
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 uploadParams = Array(storeUrl,toFilename,uploadDir,backupDir,username,password);
var originalPath = document.location.toString();
// If url is a directory : add index.html
if (originalPath.charAt(originalPath.length-1) == "/")
originalPath = originalPath + "index.html";
var dest = config.macros.upload.destFile(storeUrl,toFilename,uploadDir);
var log = new bidix.UploadLog();
log.startUpload(storeUrl, dest, uploadDir, backupDir);
displayMessage(bidix.upload.messages.aboutToSaveOnHttpPost.format([dest]));
if (bidix.debugMode)
alert("about to execute Http - GET on "+originalPath);
var r = doHttp("GET",originalPath,null,null,null,null,callback,uploadParams,null);
if (typeof r == "string")
displayMessage(r);
return r;
};
bidix.upload.uploadRss = function(uploadParams,original,posDiv)
{
var callback = function(status,params,responseText,url,xhr) {
if(status) {
var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
displayMessage(bidix.upload.messages.rssSaved,bidix.dirname(url)+'/'+destfile);
bidix.upload.uploadMain(params[0],params[1],params[2]);
} else {
displayMessage(bidix.upload.messages.rssFailed);
}
};
// do uploadRss
if(config.options.chkGenerateAnRssFeed) {
var rssPath = uploadParams[1].substr(0,uploadParams[1].lastIndexOf(".")) + ".xml";
var rssUploadParams = Array(uploadParams[0],rssPath,uploadParams[2],'',uploadParams[4],uploadParams[5]);
bidix.upload.httpUpload(rssUploadParams,convertUnicodeToUTF8(generateRss()),callback,Array(uploadParams,original,posDiv));
} else {
bidix.upload.uploadMain(uploadParams,original,posDiv);
}
};
bidix.upload.uploadMain = function(uploadParams,original,posDiv)
{
var callback = function(status,params,responseText,url,xhr) {
var log = new bidix.UploadLog();
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:")));
displayMessage(bidix.upload.messages.backupSaved,bidix.dirname(url)+'/'+backupfile);
}
var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
displayMessage(bidix.upload.messages.mainSaved,bidix.dirname(url)+'/'+destfile);
store.setDirty(false);
log.endUpload("ok");
} else {
alert(bidix.upload.messages.mainFailed);
displayMessage(bidix.upload.messages.mainFailed);
log.endUpload("failed");
}
};
// do uploadMain
var revised = bidix.upload.updateOriginal(original,posDiv);
bidix.upload.httpUpload(uploadParams,revised,callback,uploadParams);
};
bidix.upload.httpUpload = function(uploadParams,data,callback,params)
{
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)
alert(bidix.upload.messages.storePhpNotFound.format([url]));
if ((bidix.debugMode) || (responseText.indexOf("Debug mode") >= 0 )) {
alert(responseText);
if (responseText.indexOf("Debug mode") >= 0 )
responseText = responseText.substring(responseText.indexOf("\n\n")+2);
} else if (responseText.charAt(0) != '0')
alert(responseText);
if (responseText.charAt(0) != '0')
status = null;
callback(status,params,responseText,url,xhr);
};
// do httpUpload
var boundary = "---------------------------"+"AaB03x";
var uploadFormName = "UploadPlugin";
// compose headers data
var sheader = "";
sheader += "--" + boundary + "\r\nContent-disposition: form-data; name=\"";
sheader += uploadFormName +"\"\r\n\r\n";
sheader += "backupDir="+uploadParams[3] +
";user=" + uploadParams[4] +
";password=" + uploadParams[5] +
";uploaddir=" + uploadParams[2];
if (bidix.debugMode)
sheader += ";debug=1";
sheader += ";;\r\n";
sheader += "\r\n" + "--" + boundary + "\r\n";
sheader += "Content-disposition: form-data; name=\"userfile\"; filename=\""+uploadParams[1]+"\"\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 (bidix.debugMode) alert("about to execute Http - POST on "+uploadParams[0]+"\n with \n"+data.substr(0,500)+ " ... ");
var r = doHttp("POST",uploadParams[0],data,"multipart/form-data; boundary="+boundary,uploadParams[4],uploadParams[5],localCallback,params,null);
if (typeof r == "string")
displayMessage(r);
return r;
};
// same as Saving's updateOriginal but without convertUnicodeToUTF8 calls
bidix.upload.updateOriginal = function(original, posDiv)
{
if (!posDiv)
posDiv = locateStoreArea(original);
if((posDiv[0] == -1) || (posDiv[1] == -1)) {
alert(config.messages.invalidFileError.format([localPath]));
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-HEAD","MarkupPreHead");
revised = updateMarkupBlock(revised,"POST-HEAD","MarkupPostHead");
revised = updateMarkupBlock(revised,"PRE-BODY","MarkupPreBody");
revised = updateMarkupBlock(revised,"POST-SCRIPT","MarkupPostBody");
return revised;
};
//
// UploadLog
//
// config.options.chkUploadLog :
// false : no logging
// true : logging
// config.options.txtUploadLogMaxLine :
// -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)
bidix.UploadLog = function() {
if (!config.options.chkUploadLog)
return; // this.tiddler = null
this.tiddler = store.getTiddler("UploadLog");
if (!this.tiddler) {
this.tiddler = new Tiddler();
this.tiddler.title = "UploadLog";
this.tiddler.text = "| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |";
this.tiddler.created = new Date();
this.tiddler.modifier = config.options.txtUserName;
this.tiddler.modified = new Date();
store.addTiddler(this.tiddler);
}
return this;
};
bidix.UploadLog.prototype.addText = function(text) {
if (!this.tiddler)
return;
// retrieve maxLine when we need it
var maxLine = parseInt(config.options.txtUploadLogMaxLine,10);
if (isNaN(maxLine))
maxLine = -1;
// add text
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.modifier = config.options.txtUserName;
this.tiddler.modified = new Date();
store.addTiddler(this.tiddler);
// refresh and notifiy for immediate update
story.refreshTiddler(this.tiddler.title);
store.notify(this.tiddler.title, true);
};
bidix.UploadLog.prototype.startUpload = function(storeUrl, toFilename, uploadDir, backupDir) {
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 + " |";
this.addText(text);
};
bidix.UploadLog.prototype.endUpload = function(status) {
if (!this.tiddler)
return;
this.addText(" "+status+" |");
};
//
// 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
bidix.checkPlugin("PasswordOptionPlugin", 1, 0, 1);
// styleSheet
setStylesheet('.txtUploadStoreUrl, .txtUploadBackupDir, .txtUploadDir {width: 22em;}',"uploadPluginStyles");
//optionsDesc
merge(config.optionsDesc,{
txtUploadStoreUrl: "Url of the UploadService script (default: store.php)",
txtUploadFilename: "Filename of the uploaded file (default: in index.html)",
txtUploadDir: "Relative Directory where to store the file (default: . (downloadService directory))",
txtUploadBackupDir: "Relative Directory where to backup the file. If empty no backup. (default: ''(empty))",
txtUploadUserName: "Upload Username",
pasUploadPassword: "Upload Password",
chkUploadLog: "do Logging in UploadLog (default: true)",
txtUploadLogMaxLine: "Maximum of lines in UploadLog (default: 10)"
});
// Options Initializations
bidix.initOption('txtUploadStoreUrl','');
bidix.initOption('txtUploadFilename','');
bidix.initOption('txtUploadDir','');
bidix.initOption('txtUploadBackupDir','');
bidix.initOption('txtUploadUserName','');
bidix.initOption('pasUploadPassword','');
bidix.initOption('chkUploadLog',true);
bidix.initOption('txtUploadLogMaxLine','10');
/* don't want this for tiddlyspot sites
// Backstage
merge(config.tasks,{
uploadOptions: {text: "upload", tooltip: "Change UploadOptions and Upload", content: '<<uploadOptions>>'}
});
config.backstageTasks.push("uploadOptions");
*/
//}}}
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
{{{
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):
"""
Find the length of a vector.
"""
m = math.sqrt(pow(vector[0], 2) + pow(vector[1], 2) + pow(vector[2], 2))
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
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?
"""
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):
"""
Add two vectors together
"""
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:", length(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]
}}}
*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."
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/Menu.jpg]]
[img[http://www.akeric.com/python/TkImages/Message.jpg]]
[img[http://www.akeric.com/python/TkImages/OptionMenu.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/RadioButton.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]]
<<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 -
*[[Blog|http://www.akeric.com/blog/]] - [[LinkedIn|http://www.linkedin.com/in/pavey]] - [[Flickr|http://www.flickr.com/photos/8064698@N03/collections/]] - [[Youtube|http://www.youtube.com/profile?user=warpcat]] - [[MobyGames|http://www.mobygames.com/developer/sheet/view/developerId,76979/]] - [[RSWarrior|http://rswarrior.com/photos/Warpcat/default.aspx]]
>>
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 {{{--->}}}
The ''//Search//'' box in the Right column (top) {{{--->}}}
----
>>>>
Best viewed in ''[[Firefox|http://www.mozilla.com]]''
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
Type "help", "copyright", "credits" or "license" for more information.
>>> 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.
See the tiddler [[LiveWires]] for more notes.
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/
Notes on editing wiki:
*[[Add custom graphics to the header|http://www.tiddlywiki.org/wiki/How_To/Header_Macro/Plugin_%28for_Custom_Graphic_Header%29]]
*[[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/
[img[https://wingware.com/images/wingide2-personal-screenshot-qtr.png]]
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')))
url = "http://www.google.com/search?" + url
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"
searchText = "addMe"
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>
<subTagElement>add me</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]]
----
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>
<heading>Reminder</heading>
<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'>}}}.
[[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 foo(posA, posB, defA="spam", defB="maps", *args, **kwargs):
# ... code ...
}}}
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
}}}
<<<
----
Additional Info:
*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 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.
*''Immutable'' arguments are passed in 'by value'.
**They're passed in 'by reference', which in effect is like passing in a duplicate copy, since they //can't// be changed in-place.
*Changing a ''mutable'' object argument //in// a function may impact the caller.
**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 (that's a lower-case 'L', fyi)
n = 42 # immutable
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
# 23
foo(l,n)
print l
# ['a', 's', 'd', 'f', 'snuff']
print n
# 23
}}}
{{{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}}}.
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]]).
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
>>> 6.283
}}}
But turning this back into a float seems to bring back the problems again :(
{{{
num = float(d)
print num.__repr__()
>>> 6.2830000000000004
}}}
http://docs.python.org/glossary.html#term-decorator
http://docs.python.org/reference/compound_stmts.html#function
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
def adder(x, y):
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
num = moduleA.adder(2,2)
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!
num = adder(2,3)
# 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 *
num = adder(4,6)
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.
----
''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:
for line in open('myfile.txt').readlines():
print line
}}}
Using the file object's {{{.readline()}}} method:
{{{
f = open('myfile.txt')
print f.readline()
# line one
print f.readline()
# 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)]
print mtxQuad
# [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
}}}
----
{{{
# 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)
*{{{reload}}} - [[Python Docs|http://docs.python.org/lib/built-in-funcs.html]]
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.
----
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]]
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 the //decorator// {{{@property}}}. Below I'll show an example using the {{{property}}} //function//.
''Why would you want to use properties?'' 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.
{{{
class Foo(object):
def __init__(self):
# We put an underscore in front of the attr to tell
# 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."
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
*Download pylint, and put in site-packages:
**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]]')
*Download 'logilab-common':
**http://www.logilab.org/project/logilab-common
**Extract to the new {{{\logilab}}} package dir. Rename extracted root dir to just "{{{common}}}".
*Download 'logilab-astng'
**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.
[[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}}}.
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
}}}