python

How to manage a CPU intensive process on a server

I need to run a CPU- and memory-heavy Python script (analyzing and altering a lengthy WAV file) as a background process on my web server (a VPS), between HTTP requests. The script takes up to 20 seconds to run and I am concerned about the performance on my server. Is there a good approach to either lower the priority of the process, per...

How do I list all tga files in a directory (non recursive) in Python?

How do I list all tga files in a directory (non recursive) in Python? ...

Python: decorator specific argument (unrelated to wrapped function)?

I'm looking to build a caching decorator that given a function caches the result of the function to a location specified in the decoration. Something like this: @cacheable('/path/to/cache/file') def my_function(a, b, c): return 'something' The argument to the decorator is completely separate from the argument to the function it's ...

Python Django loop logic: Error says 'int' is not iterable - check my syntax?

return sum(jobrecord.get_cost() or 0 for jobrecord in self.project.jobrecord_set.filter( date__lte=date, date__gte=self.start_date) or 0) ...

Django-way of specifying channel image in rss feed

What is the "django-way" of specifying channel image in rss feed? I can do it manually by rolling my own xml, but was looking for a proper way of doing it. Edit dobrych's solution is not quite applicable here because I was asking specifically about RSS not Atom feeds ...

django : using admin datepicker

I'm trying to use the admin datepicker in my own django forms. Roughly following the discussion here : http://www.mail-archive.com/[email protected]/msg72138.html I've a) In my forms.py included the line from django.contrib.admin import widgets b) and used the widget like this : date = forms.DateTimeField(widget=widget...

Overriding python threading.Thread.run()

Given the documentation at http://docs.python.org/library/threading.html which states for Thread.run(): You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwar...

Access to errno from Python?

I am stuck with a fairly complex Python module that does not return useful error codes (it actually fails disturbingly silently). However, the underlying C library it calls sets errno. Normally errno comes in over OSError attributes, but since I don't have an exception, I can't get at it. Using ctypes, libc.errno doesn't work because e...

Can I make pdb start debugging right away?

I want to debug a python project The problem is, I don't know where to set a break point, what I want to do, is be able to call a method SomeClass( some_ctor_arguments ).some_method()` and have the debugger be fired right away How do I do that? I tried pdb.run( string_command ) but it doesn't seem to work right >>> import pdb >>...

Security of Python's eval() on untrusted strings?

If I am evaluating a Python string using eval(), and have a class like: class Foo(object): a = 3 def bar(self, x): return x + a What are the security risks if I do not trust the string? In particular: Is eval(string, {"f": Foo()}, {}) unsafe? That is, can you reach os or sys or something unsafe from a Foo instance? Is eval(s...

Is there a parser/way available to parser Wikipedia dump files using Python?

I have a project where in I collect all the wikipedia articles belonging to a particular category, pull out the dump from the wikipedia, and put it into our db. So I should be parsing wikipedia dump file to get the stuff done. Do we have an efficient parser to do this job. I am a python developer. So I prefer any parser in python. If not...

How do I know if a generator is empty from the start?

Is there a simple way of testing if the generator has no items, like peek, hasNext, isEmpty, something along those lines? ...

Can I use a single file as a buffer? I.e. write to and read from at same time.

I want to have an application writing out information at the same time that a monitor is reading it. The application is "embedded" (and on Win32 XP) and so has restricted memory and I/O functionality. The simplest way I can think to do this is by writing the data to a buffer file from the application, and then read the same file using t...

write a table with empty cells based on dictionary of values

I have this view in my app: def context_detail(request, context_id): c = get_object_or_404(Context, pk=context_id) scs = SherdCount.objects.filter(assemblage__context=c).exclude(count__isnull=True) total = sum(sc.count for sc in scs) table = [] forms = [] for a in c.assemblage_set.all(): for sc in a.sherdcount_set.all(): for...

Preserve order of attributes when modifying with minidom

Is there a way I can preserve the original order of attributes when processing XML with minidom? Say I have: <color red="255" green="255" blue="233" /> when I modify this with minidom the attributes are rearranged alphabetically blue, green, and red. I'd like to preserve the original order. I am processing the file by looping through t...

How to Alter Photographed Document to Look "Scanned"

How can I do this in Python/PIL? I.e., given the four points of an offset rectangle (a photographed document), make it look flat on as if it were scanned. Is there a simple algorithm for it? Also, are there any other manipulations I should do to make it look more "scan-like"? I want to make a simple version of this program for myself ...

Help with subprocess.call on a Windows machine

I am trying to modify a trac plugin that allows downloading of wiki pages to word documents. pagetodoc.py throws an exception on this line: # Call the subprocess using convenience method retval = subprocess.call(command, shell=True, stderr=errptr, stdout=outptr, close_fds = True) Saying that close_fds is not supported on Windows. The ...

Can I write Python web application for Windows and Linux platforms at the same time?

Can I write web application that I can host on Windows(IIS web server) and Linux (Apache or lighttpd) without any changes? CGI? Maybe something new? WSGI | FastCGI ? ...

HTML Entity Codes to Text

Does anyone know an easy way in Python to convert a string with HTML entity codes (e.g. &lt; &amp;) to a normal string (e.g. < &)? cgi.escape() will escape strings (poorly), but there is no unescape(). ...

Is there a way to substring a string in Python?

Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string? Maybe like myString[2:end]? EDIT: If leaving the second part means 'till the end', if you leave the first part, does it start from the start? ...