python

Extra parameter for Django models

Hello, With Django models, I want to achieve this: class Foo(models.Model): name = models.CharField(max_length=50) #wrapping the save function, including extra tasks def save(self, *args, **kwargs): super(Foo, self).save(*args, **kwargs) if extra_param: ...do task 1 else: ...

Python: Why is comparison between lists and tuples not supported?

When comparing a tuple with a list like ... >>> [1,2,3] == (1,2,3) False >>> [1,2,3].__eq__((1,2,3)) NotImplemented >>> (1,2,3).__eq__([1,2,3]) NotImplemented ... Python does not deep-compare them as done with (1,2,3) == (1,2,3). So what is the reason for this? Is it because the mutable list can be changed at any time (thread-safety ...

how to save/read class wholly in Python

som = SOM_CLASS() # includes many big difficult data structures som.hard_work() som.save_to_disk(filename) #then later or another program som = SOM_CLASS() som.read_from_file(filename) som.do_anythink_else() or som = SOM_CLASS() save(som) #... load(som) som.work() what is easiest way to do this? ...

How can I capture and print packets from the internet on Windows?

How can I capture them? Is there any module/lib to do it? Please if it do, post an example ...

Creating a Persistent Data Object In Django

I have a Python-based maximum entropy classifier. It's large, stored as a Pickle, and takes about a minute to unserialize. It's also not thread safe. However, it runs fast and can classify a sample (a simple Python dictionary) in a few milliseconds. I'd like to create a basic Django web app, so users can submit samples to classify in re...

Pyunit: "Import Site"

Using pyUnit to do what is currently a very small and simple unit test I am getting the message: 'import site' failed; use -v for traceback ... ____________________________________________ Ran 3 tests in 0.094S When I rerun the unit test with the -v parameter, it returns verbose information about each of the 3 tests and has no error o...

Accessing void pointers in Python (using SWIG or something else)

I've been trying to use SWIG to wrap around a simple library that uses ioctl() to populate a structure like the following: struct data { header* hdr; void* data; size_t len; }; data is a pointer to a buffer, len is the length of that buffer. I'm unable to figure out how to convert data to a Python string (or array). Furthermore...

What is the Python code to split up a string so that it prints out normally in an 80-character window without wrapping?

When I run my program (which decrypts a paragraph from a certain document), I have: W E T H E P E O P L E O F T H E U N I T E D S T A T E S I N O R D E R T O F O R M A M O R E P E R F E C T U N I O N E S T A B L I S H J U S T I C E I N S U R E D O M E S T I C T R A N Q U I L I T Y P R O V I D E F O R T H E C O M M O N D E F E N S E P R ...

Why is post_save being raised twice during the save of a Django model?

I am attaching a method to the post_save signal of my Django model. This way I can clear some cached items whenever the model is modified. The problem I am having is that the signal is being triggered twice when the model is saved. It doesn't necessarily hurt anything (the code will just gracefully error out) but it can't be right. A ...

Numpy array memory issue

I believe I am having a memory issue using numpy arrays. The following code is being run for hours on end: new_data = npy.array([new_x, new_y1, new_y2, new_y3]) private.data = npy.row_stack([private.data, new_data]) where new_x, new_y1, new_y2, new_y3 are floats. After about 5 hours of recording this data every second (more t...

what is philosophy of using import in Python?

Do I need always import all I need in the beginning of the script? Use import immediately before using things from this? What to do if import thing uses several times? What is good style of using import in Python? ...

starting Python IDLE from command line to edit scripts

I am working on a project which has about 15 files that I edit often. So in the past, when I start up IDLE, I open those 15 files for editing individually. I simply wonder if there is a way to automate this, via a .bat file or something. I'm not too good with command line, but I did a bit of research. I came across this page: http://doc...

wxPython: Threading GUI --> Using Custom Event Handler

I am trying to learn how to run a thread off the main GUI app to do my serial port sending/receiving while keeping my GUI alive. My best Googling attempts have landed me at the wxpython wiki on: http://wiki.wxpython.org/LongRunningTasks which provides several examples. I have settled on learning the first example, involving starting a wo...

using a key to rearrange string

Using Python I want to randomly rearrange sections of a string based on a given key. I also want to restore the original string with the same key: def rearrange(key, data): pass def restore(key, rearranged_data): pass Efficiency is not important. Any ideas? Edit: can assume key is hashable, but may be multiple types defin...

Does turning a list into a set, then back again, cause problems in Python?

I'm turning a list into a set in Python, like so: request.session['vote_set'] = set(request.session['vote_set']) So I can easily do a if x in set lookup and eliminate duplicates. Then, when I'm done, I reconvert it: request.session['vote_set'] = list(request.session['vote_set']) Is there a better way to do this? Am I potentially do...

Exclude object's field from pickling in python

I would like to avoid pickling of certain fields in an instance of a class. Currently, before pickling I just set those fields to None, but I wonder whether there's more elegant solution? ...

How can you draw a bezier curve given four points with wxPython?

It appears to me that the DC's only support for curves of any sort is with splines. Are there any libraries that add bezier functionality, or is there a way to convert a bezier curve into a spline? ...

Execute sqlite3 "dot" commands from Python or register collation in command line utility

My sqlite3 database contains a "collate" column-constraint. I've placed it in the schema for the table, to prevent accidentally neglecting to use the necessary collation. However this means when running sqlite3 from the command line, and not from my Python code, the collation referenced in the schema is not present, and I'm unable to use...

In python, I need to store one element of the source of an html page as a string. How can I do this?

So far I have managed to write some code that should print the source of the page. The problem is, it doesn't. I tried it with another web site, and it printed it out fine, so I used wget on the page "http://www.whitepages.com/carrier_lookup?carrier=other&number_0=2165138899&response=1" which should download the page for me. It g...

PyGame Set Mouse Cursor from Bitmap

Hello everyone, I'm making an image editor using pyGame and I was wondering if it was possible to change the mouse cursor to something more suitable (for instance a circle or rectangle) for brushes. PyGame has a really weird way of doing it that I'm not sure would work very well. Is there a way I can write to a bitmap and then use that?...