python

Python Pickling Slots Error

I have a large instance that I've been pickling just fine, but recently I started getting this error when I tried to dump it: File "/usr/lib/python2.6/copy_reg.py", line 77, in _reduce_ex raise TypeError("a class that defines __slots__ without " TypeError: a class that defines __slots__ without defining __getstate__ cannot be pick...

How to get the name of dir from where a python script is called (not exaclty where it ran).

I have a Python script, named script.py. It's located on ~/scripts/script.py. I have an alias in ~/.bash_aliases: alias script='python ~/scripts/script.py' I have some directories in a checked out repository, for example: ~/repository/project_dir/module_name/ I run on my terminal, inside ~/repository/project_dir/module_name/, the ...

Handling race condition in model.save() (Django)

How should one handle a possible race condition in a model's save() method? For example, the following example implements a model with an ordered list of related items. When creating a new Item the current list size is used as the its position. From what I can tell, this can go wrong if multiple Items are created concurrently (will it...

Using Numpy arrays as lookup tables

I have a 2D array of Numpy data read from a .csv file. Each row represents a data point with the final column containing a a 'key' which corresponds uniquely to 'key' in another Numpy array - the 'lookup table' as it were. What is the best (most Numponic) way to match up the lines in the first table with the values in the second? ...

python add to a list of tuples

I have the following list of tuple: my_choices=( ('1','first choice'), ('2','second choice'), ('3','third choice') ) and I want to add another tuple to the start of it another_choice = ('0', 'zero choice') How can I do this? the result would be: final_choices=( ('0', 'zero choice') ...

Python string.replace() not replacing characters

Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the "normal" extensions (.doc, .xls, .ppt). They are all named based on some sort of arbitrary ID number (i.e. 1245.doc). We're switching to SharePoint and I need to rename all of these f...

raw_input in python without pressing enter.

I'm using raw_input in Python to interact with user in shell. c = raw_input('Press s or n to continue:') if c.upper() == 'S': print 'YES' It works as intended, but the user has to press enter in the shell after pressing 's'. Is there a way to accomplish what I need from an user input without needing to press enter in the shell? I'...

Problems accessing a variable which is casted into pointer to array of int with ctypes in python.

Hi there, I have C code which uses a variable data, which is a large 2d array created with malloc with variable size. Now I have to write an interface, so that the C functions can be called from within Python. I use ctypes for that. C code: FOO* pytrain(float **data){ FOO *foo = foo_autoTrain((float (*)[])data); return foo; } ...

How do I use Twitter Streaming API to track new followers of other accounts I don't have login information for?

I'm heavily basing my code off of this excellent tutorial at Ars Technica, so I am able to track my own new followers because my login information is hard-coded in. However, I'd like to track new followers of other people's accounts too. How can I do this without their passwords? import pycurl, json, StringIO STREAM_URL = "http://strea...

MySQL database variable not python variable in queries not holding or able to define 2 set commands before single execute?

Using python's MySQLdb library to execute a mysql query that needs a couple of database variables using the SET command to be defined before the query will work properly and cannot apparently provide multiple SQL statements into a single python execute command using semicolons for separating each query. Python or MySQLdb wants a single S...

Python module search path

I have a project like that : foo/ | main.py | bar/ | | module1.py | | module2.py | | __init__.py with main.py doing import bar.module1 and module1.py doing import module2. This works using python 2.6 but not with python 3.1 (ImportError: No module named module2) Why did the behaviour change ? How to restore it ? ...

Writing certain lines to a file in python

I have a large file called fulldataset. I would like to write lines from fulldataset to a new file called newdataset. I only want to write the lines from fulldataset though that contain the id numbers present in the listfile. Also all the id numbers start with XY. The id numbers occur in the middle of each line though. Here is an examp...

Python assertion error, converting string to int

Hey, I am trying to rewrite my lib written in PHP into python. It handles all sphinx requests. In the init function, I am trying to set default search and match modes, but I have run into a little problem. I get the modes from a config file. In PHP, you need to use a constant as an input: $this->sphinx->SetMatchMode(constant($this->c...

Is there a way to iterate a specified number of times without introducing an unnecessary variable?

If I want to iterate n times in Java, I write: for (i = 0; i < n; i++) { // do stuff } In Python, it seems the standard way to do this is: for x in range(n): # do stuff As always, Python is more concise and more readable. But the x bothers me, as it is unnecessary, and PyDev generates a warning, since x is never used. Is ...

web2py list reference

I am trying to get the list:reference field type to work for web2py, but for some reason I am getting an error. I am trying the example on http://web2py.com/book/default/chapter/06: db.define_table('tag',Field('name'),format='%(name)s') db.define_table('product', Field('name'), Field('tags','list:reference tag')) When I try th...

how do i get a python module's version number through code?

I remember someone showed me a way but its eluded me so far. I'm trying to get the version number of a specific few modules that I use. Something that I can store in a variable. ...

Trouble setting up sqlite3 with django! :/

I'm in the settings.py module, and I'm supposed to add the directory to the sqlite database. How do I know where the database is and what the full directory is? I'm using Windows 7. ...

How to handle a one line for loop without putting it in a List?

Maybe the question is a bit vague, but what I mean is this code: 'livestream' : [cow.legnames for cow in listofcows] Now the problem is cow.legnames is also a list so I will get a list in a list when I try to return it with Json. How should I make it to return a single list. This is the json that would be returned. 'livestream' : [[...

Regular expression to match closing HTML tags

Hello! I'm working on a small Python script to clean up HTML documents. It works by accepting a list of tags to KEEP and then parsing through the HTML code trashing tags that are not in the list I've been using regular expressions to do it and I've been able to match opening tags and self-closing tags but not closing tags. The patter...

Are twisted RPCs guaranteed to arrive in order?

I'm using twisted to implement a client and a server. I've set up RPC between the client and the server. So on the client I do protocol.REQUEST_UPDATE_STATS(stats), which translates into sending a message with transport.write on the client transport that is some encoded version of ["update_stats", stats]. When the server receives this me...