python

localhost django dev server vs. postgres slow on mac os?

Hi StackO, Long time fan, first time posting. Hi! Anyone notice slowness from a django dev server running on Mac OS and connecting to a remote (postgres) db? It doesn't seem to be the DNS problem referenced elsewhere. We've got a staging instance running the exact same code on the same remote staging box that's hosting the db, and the ...

Python: Escaping strings for use in XML

I'm using Python's xml.dom.minidom to create an XML document. (Logical structure -> XML string, not the other way around.) How do I make it escape the strings I provide so they won't be able to mess up the XML? ...

Manually giving the twisted (web) network stack a packet to process?

I am running an HTTP server using the twisted framework. Is there any way I can "manually" ask it to process some payload? For example, if I've constructed some Ethernet frame can I ask twisted's reactor to handle it just as if it had just arrived on my network card? ...

How to create a specific if condition templatetag with Django ?

Hello, My problem is a if condition. I would like somethings like that but cannot figure out how to do it. {% if restaurant.is_favorite_of(user) %} <img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" /> {% else %} <img src="{{MEDIA_URL}}images/favorite_off.png" alt="Th...

Str in Python's map and sum

Why do you need to use the function 'str' in the following code? I am trying to count the sum of digits in a number. My code for i in number: sum(map(int, str(i)) where number is the following array [7,79,9] I read my code as follows loop though the array such that count sum of the integer digits by getting given digits in ...

Workflow for maintaining different versions of codebase for different versions of Python

I'm developing an open source application called GarlicSim. Up to now I've been developing it only for Python 2.6. It seems not to work on any other version. I decided it's important to produce versions of it that will support other versions of Python. I'm thinking I'll make a version for 2.5, 3.1 and maybe 2.4. So I have several ques...

Defining private module functions in python

According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html: Like most languages, Python has the concept of private elements: Private functions, which can't be called from outside their module However, if I define two files: #a.py __num=1 and: #b.py import a print a.__num when i run b.py it pr...

Finding perfect square

I have this python code: def sqrt(x): ans = 0 if x >= 0: while ans*ans < x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is not a positive number.' return None y =...

Prevent decorator from being used twice on the same function in python

I have a decorator: from functools import wraps def d(f): @wraps(f) def wrapper(*args,**kwargs): print 'Calling func' return f(*args,**kwargs) return wrapper And I want to prevent it from decorating the same function twice, e.g prevent things such as: @d @d def f(): print 2 Only possible solution I co...

Python 3.1.1 with --enable-shared : will not build any extensions

Summary: Building Python 3.1 on RHEL 5.3 64 bit with --enable-shared fails to compile all extensions. Building "normal" works fine without any problems. Please note that this question may seem to blur the line between programming and system administration. However, I believe that because it has to deal directly with getting language s...

A database for python 3?

I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time. Since it's a threaded server, SQLite doesn't work. It complains about threads like this: ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was ...

Forms in Django--cannot get past "cleaned_data"

I have a form that allows users to upload text AND a file. However, I'd like to make it valid even if the user doesn't upload the file (file is optional). However, in Django, it is not allowing me to get past "clean(self)". I just want it simple--if text box, pass. If no text , return error. class PieceForm(forms.Form): text = ...

Check if a parameter is a Python module?

How can I (pythonically) check if a parameter is a Python module? There's no type like module or package. >>> os <module 'os' from '/usr/lib/python2.6/os.pyc'> >>> isinstance(os, module) Traceback (most recent call last): File "/usr/lib/gedit-2/plugins/pythonconsole/console.py", line 290, in __run r = eval(command, self.namespace...

How to do nested Django SELECT?

class Friendship(models.Model): from_friend = models.ForeignKey(User, related_name='friend_set') to_friend = models.ForeignKey(User, related_name='to_friend_set') I'd like to SELECT all to_friends that have from_friend = a certain User. Then, I'd like to pass to_friends to inside filter in another .objects.filter(). Is this th...

A minimalist, non-enterprisey approach for a SOAP server in Python

I need to implement a small test utility which consumes extremely simple SOAP XML (HTTP POST) messages. This is a protocol which I have to support, and it's not my design decision to use SOAP (just trying to prevent those "why do you use protocol X?" answers) I'd like to use stuff that's already in the basic python 2.6.x installation. ...

Improvizing a drop-in replacement for the "with" statement for Python 2.4

Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4? It would be a hack, but it would allow me to port my project to Python 2.4 more nicely. EDIT: Removed irrelevant metaclass sketch ...

General printing raster and/or vector images

Hi, I'm looking for some API for printing. Basically what I want to achieve is to print set of pixels(monochromatic bitmap which I store in memory) onto the generic paper format (A4,A5..etc.). What I think that would be minimum API is: printer devices list printer buffer where I could send my in-memory pixmap (ex. like winXP printer...

How to sort this list in Python?

[ {'time':33}, {'time':11}, {'time':66} ] How to sort by the "time" element, DESC. ...

Mini-languages in Python

I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that. Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? I'm not going to give specific details of what I'm after because at the moment ...

Crazy python behaviour

I have a little piece of python code in the server script for my website which looks a little bit like this: console.append([str(x) for x in data]) console.append(str(max(data))) quite simple, you might think, however the result it's outputting is this: ['3', '12', '3'] 3 for some reason python thinks 3 is the max of [3,12,3]! So ...