python

In Django, How Do I Move Images When A Dynamic Path Changes?

I have a Django app with an image field (a custom ThumbnailImageField type) that auto-generates the file path for an image based on the title, type, and country of the item the image is attached to (upload_ to = get_ image_path). Here's how: def get_image_path(instance, filename): dir = 'images' subdir = instance.get_type_disp...

How to select an item from a list with known percentages in Python

I wish to select a random word from a list where the is a known chance for each word, for example: Fruit with Probability Orange 0.10 Apple 0.05 Mango 0.15 etc How would be the best way of implementing this? The actual list I will take from is up to 100 items longs and the % do not all tally to 100 % they do fall short to account...

python: run a process with timeout and capture stdout, stderr and exit status

Possible Duplicate: subprocess with timeout What is the easiest way to do the following in Python: Run an external process Capture stdout in a string, stderr, and exit status Set a timeout. I would like something like this: import proc try: status, stdout, stderr = proc.run(["ls", "-l"], timeout=10) except proc.Timeout...

Circular import dependency in Python

Let's say I have the following directory structure: a\ __init__.py b\ __init__.py c\ __init__.py c_file.py d\ __init__.py d_file.py In the a package's __init__.py, the c package is imported. But c_file.py imports a.b.d. The program fails, saying b doesn't...

jinja2: html escape variables

how do I html-escape dangerous unsanitized input in jinja2? Can I do it inside the template or must it be done in python code? I have a variable that may contain da<ngero>u&s chars. How do I escape it in jinja2 ...

Recommendations for perl-to-python interoperation?

We have a sizable code base in Perl. For the forseeable future, our codebase will remain in Perl. However, we're looking into adding a GUI-based dashboard utility. We are considering writing the dashboard in Python (using tkinter or wx). The problem, however, is that we would like to leverage our existing Perl codebase in the Python GUI....

Should you import all classes you use in Python?

Python's lack of static typing makes it possible to use and rely on classes without importing them. Should you import them anyway? Does it matter? Example someclass.py class SomeClass: def __init__(self, some_value): self.some_value = some_value someclient.py class SomeClient: def __init__(self, some_class_instance)...

3d Math Library For Python

i'm looking for a 3d math library in python or with python bindings. it needs to handle rotation, translation, perspective projection, everything basically. what im NOT looking for is a library aimed at drawing on the screen, googling for hours only led to 3d libraries bent on rendering something to the screen. i dont want any visuali...

non-blocking read/log from an http stream

I have a client that connects to an HTTP stream and logs the text data it consumes. I send the streaming server an HTTP GET request... The server replies and continuously publishes data... It will either publish text or send a ping (text) message regularly... and will never close the connection. I need to read and log the data it c...

python: delete non-empty dir

How do I delete a possibly non-empty dir in Python. The directory may have nested subdirectories many levels deep. ...

how to get time of a python program execution?

I have a command line program in Python, that takes quite a while to finish. I want to know the exact time it takes to finish running. I've looked at the timeit module, but it seems it's only for small snippets of code. I want to time the whole program. ...

PIL vs Python-GD for crop and resize

Dear Stackoverflow'ers, I am creating custom images that I later convert to an image pyramid for Seadragon AJAX. The images and image pyramid are created using PIL. It currently take a few hours to generate the images and image pyramid for approximately 100 pictures that have a combined width and height of about 32,000,000 by 1000 (ye...

How to make complex contains queries in Django?

Hi, I need to make query like this: WHERE Comment like '%ev% 3628%' or Comment like '%ew% 3628%' the number '3628' is a parametr. So I've tried in my view: First try: wherestr = "Comment like '%%ev%% %s%%' or Comment like '%%ew%% %s%%'" % (rev_number, rev_number) comment_o = Issuecomments.objects.extra(where=[wherestr]) ...

PyQt: Displaying QTextEdits over the window

I want to display some QTextEdits over my main window at arbitrary locations. Below is my first attempt. It doesn't quite work. If I create the text edits before I show the window, the text edits appear, but if I create them after I have shown the window they don't appear. What's up with that? How can I get the ones created later to...

Getting multiple properties at the same time in Appscript

I am using Appscript - a Python interface to AppleScript - in a project of mine that basically gets data from a Mac application. Here is a sample code: asobj = app('Things').to_dos()[0] self.id = asobj.id() self.name = asobj.name() self.status = asobj.status() Every invocation of the properties (id, name, status) does...

Install custom modules in a python virtual enviroment

I am doing some pylons work in a virtual python enviorment, I want to use MySQL with SQLalchemy but I can't install the MySQLdb module on my virtual enviorment, I can't use easyinstall because I am using a version that was compiled for python 2.6 in a .exe format, I tried running the install from inside the virtual enviorment but that d...

relevant query to what is the best python method for encryption

I tried to use the gnupg.py module encryption decryption function named " def test_encryption_and_decryption(self): " Could i use this function by passing the key or fingerprint retrieved from public key server. I am getting the key by this : retk = urllib.urlopen('http://pool.sks-keyservers.net:11371/pks/lookup op=get&search=hex f...

How can I distribute python programs?

My application looks like this: main.py windows/ __init__.py mainwindow.py ... model/ __init__.py orders.py ... resources/ image1.png logo.jpg ... The program is started with main.py. Is there a good way to create a 'final' application out of it? I'm thinking of something like py2exe/py2app, but wi...

how do I get only the time in models

How do I format datetime to give me only the time in my model ...

Convert from hex string to unicode

How can i convert the 'dead' string to an unicode string u'\xde\xad'? Doing this: from binascii import unhexlify out = ''.join(x for x in [unhexlify('de'), unhexlify('ad')]) creates a <type 'str'> string '\xde\xad' Trying to use the Unicode.join() like this: from binascii import unhexlify out = ''.join(x for x in [u'', unhexlify('d...