python

Django FileField: how to set default value (auto-create empty file)?

I've got a model like this: class MyModel(models.Model): name = models.CharField(max_length=255) code = models.FileField() When a new MyModel is submitted, I want to allow for the code field to be left empty, in which case I need Django to create an empty file (with arbitrary name). Question is: what is the right way to do it...

Fuzzy runtime search without using database\index

Hello, I need to filter stream of text articles by checking every entry for fuzzy matches of predefined string(I am searching for misspelled product names, sometime they have different order of words and extra non letter characters like ":" or ","). I get excellent results by putting this articles in sphinx index and performing search...

Python Unicode Encode Error

I'm reading and parsing an Amazon XML file and while the XML file shows a ' , when I try to print it I get the following error: 'ascii' codec can't encode character u'\u2019' in position 16: ordinal not in range(128) From what I've read online thus far, the error is coming from the fact that the XML file is in UTF-8, but Python wants...

Python: Indexing a list of lists

Simple question, i have a list if lists x = [['1','2','3'],['4','5','6'],['7','8','9']] whats the simpliest way of indexing through each list in a single for loop? For example for i in x: print 1st_list_in_list print 2nd_list_in_list print 3rd_list_in_list EDIT Let me elaborate further, I wish to print each list how ...

Pass request to model form using generic view in Django

I using Django and a generic view "django.views.generic.create_update.create_object" I have a model form wich i pass to the generic view: url(r'^add$', create_object, {'template_name':'tpl.html','form_class':MyModelForm,'post_save_redirect':'/'},name = 'add'), I need to get current user in my ModelForm.save method.. But i can't find w...

Removing HTML tags from a unicode string in Python

Hey all, I have a strong that I scraped from an XML file and It contains some HTML formatting tags (<b>, <i>, etc) Is there a quick and easy way to remove all of these tags from the text? I tried str = str.replace("<b>","") and applied it several times to other tags, but that doesn't work ...

Learning Python, is there a better way to write this?

I am learning Python (2.7) and to test what I have learned so far I wrote a temperature converter that converts Celsius to Fahrenheit and I wanted to know if my code could be written better to be faster or something more Pythonic. And could someone tell me if there is an actual name for the if __name__ == '__main__': main() (out of curio...

Python: Sanitize a string for unicode?

I have a string that I'm trying to make safe for the unicode() function: >>> s = " foo “bar bar ” weasel" >>> s.encode('utf-8', 'ignore') Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> s.encode('utf-8', 'ignore') UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 5: ordinal not in ran...

Installing PIL on Snow Leopard -- NOTHING WORKS

I'm trying to install PIL on Snow Leopard, using Python 2.6.1, GCC 4.2.1, PIL 1.1.7, and have tried with both libjpeg6b and libjpeg7 -- nothing works. I've cleared out every trace of libjpeg/pil/zlib from fink, tried various compiler options, etc. and used http://jetfar.com/libjpeg-and-python-imaging-pil-on-snow-leopard/ and http:// www....

Python: list of lists anf HTML table help

I'm having trouble appending the values from a list of lists into a html table, for example my list if lists contains: food_list = [['A','B'], ['Apple','banana'], ['Fruit','Fruit']] How would i append each value into a correspondong HTML table? So the code looks like: <table> <tr><td>A</td><td>Apple</td><td>Fruit</td></tr> <tr><td>B<...

Is there a python module that provides deeper analysis of a data structure than pprint?

Is there anything in python that lets me dump out a random object in such a way as to see its underlying data representation? I am coming from Perl where Data::Dumper does a reasonable job of letting me see how a data structure is laid out. Is there anything that does the same thing in python? Thanks! ...

Change the gcc version that distutils uses

I'm on Snow Leopard, and want distutils to use gcc 4.0 and not 4.2, can anyone tell me how to make it do that? I've tried changing the /usr/bin/g* symlinks, and setting the C* environment vars -- but to no avail. Any thoughts? ...

Detect data type from XML string using python

I have some XML tagged string as follows. <Processor>AMD Athlon(tm) 64 X2 Dual Core Processor 4400+ 2.31 GHz</Processor> <ClockSpeed>2.31</ClockSpeed> <NumberOfCores>2</NumberOfCores> <InstalledMemory>2.00</InstalledMemory> <OperatingSystem>Windows 7 Professional</OperatingSystem> How can I detect the data type automatically using pyt...

Get Request Using PyCurl after logging into website

After doing a post to log into my website, I try to do a get on my the site and I get a bunch of garbage "�0������`&)��붋...." instead of the data from my site. Why is that? How do I fix that? ...

Matplotlib: plot multiple graphs using same figure, without them overlapping

I have a class which I use to plot things then save them to a file. Here's a simplified version of it: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt class Test(): def __init__(self, x, y, filename): fig = plt.figure(1) ax = fig.add_subplot(111) ax.plot(x, y, 'D', color='red') ...

Pango error after a minute of running

I have the following python modules. Sorry if the code is ugly. This is my first python GUI app and I'm fairly new to python as well. It's some sort of a count down timer with a todo list. It works kinda well except that after two minutes after running the program, it crashes with the following error: Pango:ERROR:/build/buildd/pango1.0-...

How to check if an element of a list is a number?

How can I check if the first element of the list (below) is a number (using some sort of regular expression) in python: temp = ['1', 'abc', 'XYZ', 'test', '1'] Many thanks. ...

GAE: Best way to determine how many of a Kind is stored?

What is the best way to determine how many models of a certain kind are in my app's datastore? The documentation says that MyKind.all().count() is only marginally better than retrieving all of the data, and has a limit of 1000. This is not helpful, because I am expecting to have 6000+ instances of MyKind stored. Is there a better way to...

Google App Engine: How to use the task queue for this processing?

I'm using the Python GAE SDK. I have some processing that needs to be done on 6000+ instances of MyKind. It is too slow to be done in a single request, so I'm using the task queue. If I make a single task process only one entity, then it should take only a few seconds. The documentation says that only 100 tasks can be added in a "batc...

tokyo-python Install Issues

Hey guys, I'm trying to use Tokyo Cabinet in Python via the tokyo-python package. I've installed Cabinet, and the Cabinet development library. However, whenever I try to install tokyo-python with pip or easy_install, I get this ugliness. matt@amalgam:~/$ bin/pip install tokyo-python Downloading/unpacking tokyo-python Downloading tokyo-...