python

How can I determine if instance of class from Django model is subclass of another model?

I have a class called BankAccount as base class. I also have CheckingAccount and SavingsAccount classes that inherit from BankAccount. BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes. Then, I execute a query like this: account = BankAccount.objects.get(id=10) How do I know if a...

Why does Django's ModelForm validation think that this is invalid?

I have what looks to me to be a pretty simple setup of model, modelform and a view playing with the two. The only hitch is that the model has a user property that can't be POSTed to the form, rather it should be populated by request.user so I have this: # models.py class Update(models.Model): user = models.ForeignKey(User, ...

Python: Asynchronous http requests sent in order with automatic handling of cookies?

Hello, I am coding a python (2.6) interface to a web service. I need to communicate via http so that : Cookies are handled automatically, The requests are asynchronous, The order in which the requests are sent is respected (the order in which the responses to these requests are received does not matter). I have tried what could be e...

Allowing users to delete their own comments in Django

I am using the delete() function from django.contrib.comments.views.moderation module. The staff-member is allowed to delete ANY comment posts, which is completely fine. However, I would also like to give registered non-staff members the privilege to delete their OWN comment posts, and their OWN only. How can I accomplish this? ...

UTF-8 and upper()

I want to transform UTF-8 strings using built-in functions such as upper() and capitalize(). For example: >>> mystring = "işğüı" >>> print mystring.upper() Işğüı # should be İŞĞÜI instead. How can I fix this? ...

In Django, why does this error say that my column can't be null?

picture_url = models.CharField(max_length=2000) ...

In Python, how do I loop through the dictionary and change the value if it equals something?

If the value is None, I'd like to change it to "" (empty string). I start off like this, but I forget: for k, v in mydict.items(): if v is None: ... right? ...

pyplot.ginput() causes axes to change?

I am encountering some strange behavior with using the matplotlib.pyplot ginput() function to store clicked points. On the first click, the ranges of the axes of the clicked image change to add 200 on each side. The image remains with this border of whitespace until something new is plotted. Example code: import matplotlib.pyplot as pl...

How to sort a collection of objects by an variable they all hold?

I have a class named Individual, which has a variable, self.fitness. I have a collection of these Individual instances and I'd like to sort them by their fitness. How is this done in python? ...

django facebook login-button.

We are using django and django-socialauth , and my view is facebook/login what is the facebook login-button for django, thanks ...

Unescape a string inside a string

I am working with urllib2, and trying to extract the headers in a printable form from a Response object. Presently I am printing str(response.info()), however what is printed, is itself a Python string (at least to my understanding). (Pdb) p str(response.info()) 'Date: Tue, 23 Feb 2010 03:12:26 GMT\r\nServer: Apache\r\nVary: Accept-Enc...

Get number of modified rows after sqlite3 execute

When performing SQL statements such as UPDATE, and INSERT, the usual .fetch*() methods on the Cursor instance obviously don't apply to the number of rows modified. In the event of executing one of the aforementioned statements, what is the correct way to obtain the corresponding row count in Python, and the corresponding API in the Sqli...

How to print line breaks in Python Django template.

{'quotes': u'Live before you die.\n\n"Dream as if you\'ll live forever, live as if you\'ll die today"\n\n"Love one person, take care of them until you die. You know, raise kids. Have a good life. Be a good friend. Try to be completely who you are, figure out what you personally love and go after it with everything you\'ve got no matter h...

python egg development environment setup

hi, I inherited a python project, which has been packaged as egg. Upon check out through SVN, I am seeing package content as: __init__.py scripts/ ptools/ setup.py ... Here, ptools/ hold the source of various modules. scripts/ is bunch of end-user tools that make use of modules provided by the "ptools". The package has been installed...

How to create a dictionary of integer pairs from a file in python

If I have a file of pairs of integer IDs, followed by a value, I'd like to create this into a dictionary. Each separate term is separated by a newline. I want to make sure these are all held as ints. How can I do this? edit: as requested, a sample. 9 120 10 12 11 4 12 1 13 515 14 32 ...

GAE User registry without Google Accounts, want to restric to specific domain

Hello, I am planning on creating an application for the students of my school, and I want to restrict user registration to emails of the form [email protected]. I would prefer to not manually create the user table and do the password hashing and such. Are there any libraries you can recommend for this? Thanks for the help. ...

How to get the original TCP connection hostname in Python Twisted?

With Twisted's TCP mechanisms, when a protocol is created, the only information about the peer is its IP address and port. How can I retrieve the original hostname that I tried to connect with? reactor.connectTCP('somehost.com', 80, MyFactory) How can I ever get 'somehost.com' through a callback somehow? In other words, connectTCP ret...

How do I print to the OS's default printer in Python 3 (cross platform)?

I have a Python 3 script that is going to be doing some regex substitution on some Rich Text Files (rtf) and I would like to be able to print out a whole directory's files on Windows, Linux, and Mac. I have done quite a bit of searching to no avail. Thanks in advance. ...

trouble writing to file in "for line in" iterator in Python script

This seems to be an embarrassingly simple question, but, after a day of reading over How-To's and manuals, it must be asked. I'm writing many lines to a few files using a few nested loops, inserting some static strings and copying lines over from other files over and over again. The output appears to be a single copy of the static strin...

Setting up relations/mappings for a SQLAlchemy many-to-many database

I'm new to SQLAlchemy and relational databases, and I'm trying to set up a model for an annotated lexicon. I want to support an arbitrary number of key-value annotations for the words which can be added or removed at runtime. Since there will be a lot of repetition in the names of the keys, I don't want to use this solution directly, a...