python

How to iterate over a the attributes of a class, in the order they were defined?

Hi all, Pyhon comes with the handy dir() function that would list the content of a class for you. For example, for this class: class C: i = 1 a = 'b' dir(C) would return ['__doc__', '__module__', 'a', 'i'] This is great, but notice how the order of 'a' and 'i' is now different then the order they were defined in. How can I...

Python: Do (explicit) string parameters hurt performance?

Suppose some function that always gets some parameter s that it does not use. def someFunc(s): # do something _not_ using s, for example a=1 now consider this call someFunc("the unused string") which gives a string as a parameter that is not built during runtime but compiled straight into the binary (hope thats right). The que...

Why/When in Python does `x==y` call `y.__eq__(x)`?

The Python docs clearly state that x==y calls x.__eq__(y). However it seems that under many circumstances, the opposite is true. Where is it documented when or why this happens, and how can I work out for sure whether my object's __cmp__ or __eq__ methods are going to get called. Edit: Just to clarify, I know that __eq__ is called in ...

Removing empty items from a list (Python)

I'm reading a file in Python that isn't well formatted, values are separated by multiple spaces and some tabs too so the lists returned has a lot of empty items, how do I remove/avoid those? This is my current code: import re f = open('myfile.txt','r') for line in f.readlines(): if re.search(r'\bDeposit', line): print l...

Django ModelForm Template?

I want to learn how can I add to template to my ModelForm i'm newbie. Below you can see my models.py, url.py and views.py: My model.py looks like that: from django.db import models from django.forms import ModelForm from django.contrib.auth.models import User class Yazilar(models.Model): yazi = models.CharField(max_length=200)...

Are there more ways to define a tuple with only one item?

I know this is one way, by placing a comma: >>> empty = () >>> singleton = 'hello', # <-- note trailing comma >>> len(empty) 0 >>> len(singleton) 1 >>> singleton ('hello',) Source: http://docs.python.org/tutorial/datastructures.html Are there more ways to define a tuple with only 1 item? ...

Is it a good idea to have a syntax sugar to function composition in Python?

Some time ago I looked over Haskell docs and found it's functional composition operator really nice. So I've implemented this tiny decorator: from functools import partial class _compfunc(partial): def __lshift__(self, y): f = lambda *args, **kwargs: self.func(y(*args, **kwargs)) return _compfunc(f) def __rshi...

Timeout function if it takes too long to finish

Hi, I have a shell script that loops through a text file containing URL:s that I want to visit and take screenshots of. All this is done and simple. The script initializes a class that when run creates a screenshot of each site in the list. Some sites take a very, very long time to load, and some might not be loaded at all. So I want t...

Reload Method or Object in IDLE

when using idle, I know you can reload a module if it's changed like this: import foo reload(foo) if I only import part of a module, is there a way to reload it in a similar matter? from foo import bar ...

Setting up authentication in Trac

Hi, I am in the works of setting up a Trac server for my (small) company and need a bit of help/guidance with the authentication mechanism. We have for some time developed our own web application which our users access in their day to day work. It is build in php5.3 and includes a users database stored in a mysql database. I have been ...

Trouble importing modules in Python IDEs

Right I'm getting a bit tired of this so hopefully you can help me sort it out once and for all. I'm really confused about what's going on with Python on my MacBook. I'm running OS X 10.6.2 and have installed python from the website (the package that includes IDLE). This works absolutely fine, and in fact IDLE will run everything I want...

Adding an entry to a python tuple

I have a list of tuples representing x,y points. I also have a list of values for each of these points. How do I combine them into a list of lists (i.e one entry for each point [x,y,val]) or a list of tuples? Thanks ...

Help for novice choosing between Java and Python for app with sql db

I'm going to write my first non-Access project, and I need advice on choosing the platform. I will be installing it on multiple friends' and family's computers, so (since I'm sure many, many platforms would suffice just fine for my app), my highest priority has two parts: 1) ease of install for the non-technical user and, 2) minimizing c...

How Do I Get the Module Name of an Object's Class Definition Rather Than the Module Name of the Object's Instantiation?

In python 2.5, I have the following code in a module called modtest.py: def print_method_module(method): def printer(self): print self.__module__ return method(self) return printer class ModTest(): @print_method_module def testmethod(self): pass if __name__ == "__main__": ModTest().testmeth...

draw texts using maya api

Hello there, Is it possible to draw a scalable openGL generated text in maya viewport using maya api, I tried to use the function 'drawText' in M3DView class but it wont draw a scalable text, the result seems to be like a maya annotation texts also it does not response to other openGL functions except glColor. I want to scale the g...

Is there a cleaner or more efficient to do this Python assignment?

Here's the code I have now: lang = window.get_active_document().get_language() if lang != None: lang = lang.get_name() Is there a better way to do that? I'm new to Pythonic and was wondering if there's a more Python way to say "something equals this if x is true, else it equals that." Thanks. ...

Python, trying to run a program from the command prompt

Hi All, I am trying to run a program from the command prompt in windows. I am having some issues. The code is below: commandString = "'C:\Program Files\WebShot\webshotcmd.exe' //url '" + columns[3] + "' //out '"+columns[1]+"~"+columns[2]+".jpg'" os.system(commandString) time.sleep(10) So with the single quotes I get "The filename, di...

strcmp for python or how to sort substrings efficiently (without copy) when building a suffix array

Here's a very simple way to build an suffix array from a string in python: def sort_offsets(a, b): return cmp(content[a:], content[b:]) content = "foobar baz foo" suffix_array.sort(cmp=sort_offsets) print suffix_array [6, 10, 4, 8, 3, 7, 11, 0, 13, 2, 12, 1, 5, 9] However, "content[a:]" makes a copy of content, which becomes very...

Suggestions wanted: learning material for Django

I am looking for advice on best resources to learn how to develop webapps with Django [the python framework]. Here's a few information to help responders to narrow-down the gazillion options "out there". Where I stand I know python (2.x series) and I have developed a few applications/scripts with it. I wouldn't define myself a python-...

How to see if code is backwards compatible for Python?

Okay basically i have some code that i am trying to make it play nicely with ESRI's geoprocessor. However ESRI's geoprocessor runs on Python 2.2, 2.3, 2.4, 2.5. We need to make our tools work on any version. So I've spent a lot of time working and coding workarounds for different versions, such that the wrapper geoprocessor has identical...