python

Managing resources in a Python project

I have a Python project in which I am using many non-code files. Currently these are all images, but I might use other kinds of files in the future. What would be a good scheme for storing and referencing these files? I considered just making a folder "resources" in the main directory, but there is a problem; Some images are used from w...

Trouble with simple Python Code

Hello I'm learning Python, and I'm having trouble with this simple piece of code: a = raw_input('Enter a number: ') if a > 0: print 'Positive' elif a == 0: print 'Null' elif a < 0: print 'Negative' It works great, apart from the fact that it always prints 'Positive', no matter if i enter a positive or negative number or ...

Pervasive 8 and Python

Is it possible to access a Pervasive 8 (not Pervasive SQL) database from python? ...

Proper way to handle multiple forms on one page in Django

I have a template page expecting two forms. If I just use one form, things are fine as in this typical example: if request.method == 'POST': form = AuthorForm(request.POST,) if form.is_valid(): form.save() # do something. else: form = AuthorForm() If I want to work with multiple forms however, how do I let...

Python Drop into REPL

Is there a way to programmatically force a Python script to drop into a REPL at an arbitrary point in its execution, even if the script was launched from the command line? Edit: I guess I should clarify by specifying why I want to do this. I'm writing a quick and dirty plotting program, which I want to read data from stdin or a file, p...

Unexpected results feeding Django File upload object to Python CSV module

I have no problems getting the file to upload and if I save it to disk, all formatting is intact. I wrote a function to read in the the file within Django using: data = csv.reader(f.read()) where f is the Django file object that I get from 'form.cleaned_data['file']' and yes the file is already bound to the form. When I try to read ...

Efficient Python Data Storage (Abstract Data Types?)

Pardon the ambiguity in the title- I wasn't quite sure how to phrase my question. Given a string: blah = "There are three cats in the hat" and the (I'm not quite sure which data structure to use for this) "userInfo": cats -> ("tim", "1 infinite loop") three -> ("sally", "123 fake st") three -> ("tim", "1 infinite loop") three cats ...

How to using widget PlainTextEdit or TextEdit for output and input text?

I'm interested PyQt4. Thanks ...

ctypes pointer question

Hi everyone - I was reading the ctypes tutorial, and I came across this: s = "Hello, World" c_s = c_char_p(s) print c_s c_s.value = "Hi, there" But I had been using pointers like this: s = "Hello, World!" c_s = c_char_p() c_s = s print c_s c_s.value Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> c...

Customizing how Python's `copy` module treats my objects

From the copy documentation: Classes can use the same interfaces to control copying that they use to control pickling. [...] In order for a class to define its own copy implementation, it can define special methods __copy__() and __deepcopy__() So which one is it? __setstate__() and __getstate__() that are used when pickl...

Why is subtraction faster than addition in Python?

I was optimising some Python code, and tried the following experiment: import time start = time.clock() x = 0 for i in range(10000000): x += 1 end = time.clock() print '+=',end-start start = time.clock() x = 0 for i in range(10000000): x -= -1 end = time.clock() print '-=',end-start The second loop is reliably faster, anyw...

python and mechanize.open()

I have some code that is using mechanize and a password protected site. I can login just fine and get the results I expect. However, once I log in I don't want to "click" links I want to iterate through a list of URLs. Unfortunately each .open() call simply gets a re-direct to the login page, which is the behaviour I would expect if I ha...

Python: Get object by id

Let's say I have an id of a Python object, which I retrieved by doing id(thing). How do I find thing again by the id number I was given? ...

How to install 64-bit Python on Solaris?

I am trying to install Python 2.6 on Solaris by building the source on Solaris machine. I installed one this way and it appears that it is 32-bit. I downloaded some source tar ball as Linux or Unix for this purpose. Everything works well but I need 64-bit Python. I looked up the Python download site and there is no separate installatio...

apt like column output - python library

Debian's apt tool outputs results in uniform width columns. For instance, try running "aptitude search svn" .. and all names appear in the first column of the same width. Now if you resize the terminal, the column width is adjusted accordingly. Is there a Python library that enables one to do this? Note that the library has to be aware...

How Do I Select all Objects via a Relationship Model

Given the Model: class Profile(models.Model): user = models.ForeignKey(User, unique=True) class Thingie(models.Model): children = models.ManyToManyField('self', blank=True, symmetrical=False) class Relation(models.Model): profile = models.ForeignKey(Profile) thingie = models.ForeignKey(Thingie) How would one return ...

Polymorphism in Django

I have the following models. How do I get access to the unicode of the inheriting tables (Team and Athete) from the Entity table? I'm trying to display a list of all the Entities that displays 'name' if Team and 'firstname' and 'lastname' if Athlete. class Entity(models.Model): entity_type_list = (('T', 'Team'), ('A', 'Athlete')) ty...

embedding plot within Qt gui

How do you embed a vpython plot (animated) within your Qt GUI? so that it has its own display area and would not need to created a new window anymore. ...

How to reverse engineer a program which has no documentation...

I have a source of python program which doesn't have any documentation or comments. I did tried twice to understand it but most of the times I am losing my track, because there are many files. What should be the steps to understand that program fully and quickly. ...

how to recover the binary stream(original form) from radix 64 encoding

how to get the actual public key i.e its binary form i.e without radix 64 conversion .i need to extract the public key from radix64 encoding .the pgp server gives me the key in radix 64 format now i have to extract the public key from it. ...