python

XCode 3.2 Ruby and Python templates

Under xcode 3.2 my ObjectiveC + Python/Ruby projects can still be opened updated and compiled, but you cannot create new projects. Given that all traces of ruby and python are missing from xcode 3.2 (ie create project and add new ruby/python file), is there an easy way to get the templates installed again? I found some info about copy...

sqlalchemy easy way to insert or update?

I have a sequence of new objects. They all look like similar to this: Foo(pk_col1=x, pk_col2=y, val='bar') Some of those are Foo that exist (i.e. only val differs from the row in the db) and should generate update queries. The others should generate inserts. I can think of a few ways of doing this, the best being: pk_cols = Foo.table...

Debian packaging of a Python package.

I need to write (or find) a script to create a Debian package (using python-support) from a Python package. The Python package will be pure Python (no C extensions). The Python package (for testing purposes) will just be a directory with an empty __init__.py file and a single Python module, package_test.py. The packaging script must us...

Which PEP's are must reads?

I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read? ...

String formatting

Hi, I have a list filter = ['a', 'b', 'c']. I need to frame the following string out of the list "item -a item -b item -c". Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 - 150. Wouldn't that lead to overflow? And what is the maximum length of the string supp...

How can I create a numpy array holding values of a multi-variable function?

I want to create an array holding a function f(x,y,z). If it were a function of one variable I'd do, for instance: sinx = numpy.sin(numpy.linspace(-5,5,100)) to get sin(x) for x in [-5,5] How can I do the same to get, for instance sin(x+y+z)? ...

Dynamically attaching a method to an existing Python object generated with swig?

I am working with a Python class, and I don't have write access to its declaration. How can I attach a custom method (such as __str__) to the objects created from that class without modifying the class declaration? EDIT: Thank you for all your answers. I tried them all but they haven't resolved my problem. Here is a minimal example tha...

latin-1 to ascii

I have a unicode string with accented latin chars e.g. n=unicode('Wikipédia, le projet d’encyclopédie','utf-8') I want to convert it to plain ascii i.e. 'Wikipedia, le projet dencyclopedie', so all acute/accent,cedilla etc should get removed What is the fastest way to do that, as it needed to be done for matching a long autocomplete ...

Django/Python Beginner: Error when executing python manage.py syncdb - psycopg2 not found

Hello, I have Pythong2.6, psycopg2 and pgAdmin3 installed using Macports. My settings.py is: DATABASE_ENGINE = 'postgresql_psycopg2' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. DATABASE_NAME = 'mysite' # Or path to database file if using sqlite3. DATABASE_USER = 'postgres' # ...

Can I you use __init__.py to define global variables?

I want to define a constant that should be available in all of the submodules of a package. I've thought that the best place would be in in the __init__.py file of the root package. But I don't know how to do this. Suppose I have a few subpackages and each with several modules. How can I access that variable from these modules? Of cours...

Logging, StreamHandler and standard streams

Hi, I can't figure out how to log info-level messages to stdout, but everything else to stderr. I already read this http://docs.python.org/library/logging.html. Any suggestion? ...

Code organization in Python: Where is a good place to put obscure methods?

I have a class called Path for which there are defined about 10 methods, in a dedicated module Path.py. Recently I had a need to write 5 more methods for Path, however these new methods are quite obscure and technical and 90% of the time they are irrelevant. Where would be a good place to put them so their context is clear? Of course I ...

You don't have permission to access /index.py on this server.

I am setting up a simple test page in Python. I only have two files: .htaccess and index.py. I get a 403 Forbidden error when trying to view the page - how can I fix this? .htaccess: RewriteEngine On AddHandler application/x-httpd-cgi .py DirectoryIndex index.py index.py: #!/usr/bin/python print "Content-type: text/html\n\n" print "...

remove inner border on gtk.Button

Hi, I would like to remove border on a gtk.Button and also set its size fixed. How can I accomplish that? My button looks like that: b = gtk.Button() b.set_relief(gtk.RELIEF_NONE) Thanks! p.d: I'm using pygtk ...

Python data structure for a collection of objects with random access based on an attribute

Hello, I need a collection of objects which can be looked up by a certain (unique) attribute common to each of the objects. Right now I am using a dicitionary assigning the dictionary key to the attribute. Here is an example of what I have now: class Item(): def __init__(self, uniq_key, title=None): self.key = uniq_key ...

OS X - multiple python versions, PATH and /usr/local

If you install multiple versions of python (I currently have the default 2.5, installed 3.0.1 and now installed 2.6.2), it automatically puts stuff in /usr/local, and it also adjusts the path to include the /Library/Frameworks/Python/Versions/theVersion/bin, but whats the point of that when /usr/local is already on the PATH, and all inst...

(re)Using dictionaries in django views

Hello I have this dictionary in my apps model file: TYPE_DICT = ( ("1", "Shopping list"), ("2", "Gift Wishlist"), ("3", "test list type"), ) model, which uses this dict is this: class List(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) type = models.PositiveIntegerFi...

python variable scope issue

i am stuck at scope resolution in python. let me explain a code first: class serv_db: def __init__(self, db): self.db = db self.dbc = self.db.cursor() def menudisp (self): print"Welcome to Tata Motors" print"Please select one of the options to continue:" print"1. Insert Car Info" print"2. Display Car Info" print"3. Update Car I...

Serving file download with python

Hey gang, I'm trying to convert a legacy php script over to python and not having much luck. The intent of the script is to serve up a file while concealing it's origin. Here's what's working in php: <?php $filepath = "foo.mp3"; $filesize = filesize($filepath); header("Pragma: no-cache"); header("Expires: 0"); header("Cache-Control:...

Python: convert seconds to hh:mm:ss

Hi, Please, how to convert an int (number a seconds) to these formats: mm:ss or hh:mm:ss ? I need to do this with Python code (and if possible in a Django template ?). Thank you very much ;-) ...