python

Getting another program's output as input on the fly

I've two programs I'm using in this way: $ c_program | python_program.py c_program prints something using printf() and python_program.py reads using sys.stdin.readline() I'd like to make the python_program.py process c_program's output as it prints, immediately, so that it can print its own current output. Unfortunately python_prog...

Trimming Python Runtime

We've got a (Windows) application, with which we distribute an entire Python installation (including several 3rd-party modules that we use), so we have consistency and so we don't need to install everything separately. This works pretty well, but the application is pretty huge. Obviously, we don't use everything available in the runtim...

Getting the the keyword arguments actually passed to a Python method

I'm dreaming of a Python method with explicit keyword args: def func(a=None, b=None, c=None): for arg, val in magic_arg_dict.items(): # Where do I get the magic? print '%s: %s' % (arg, val) I want to get a dictionary of only those arguments the caller actually passed into the method, just like **kwargs, but I don't want ...

django templates: include and extends

I would like to provide the same content inside 2 different base files. So I'm trying to do this: page1.html: {% extends "base1.html" %} {% include "commondata.html" %} page2.html: {% extends "base2.html" %} {% include "commondata.html" %} The problem is that I can't seem to use both extends and include. Is there some way to d...

How do I hide the field label for a HiddenInput widget in Django Admin?

I've got a bit of Django form code that looks like this: class GalleryAdminForm(forms.ModelForm): auto_id=False order = forms.CharField(widget=forms.HiddenInput()) And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use: order = forms.CharField(widget=forms.HiddenInput(), labe...

Auto-generate form fields for a Form in django

I have some models and I want to generate a multi-selection form from this data. So the form would contain an entry for each category and the choices would be the skills in that category. models.py class SkillCategory(models.Model): name = models.CharField(max_length=50) class Skill(models.Model): name = models.CharField(max_l...

Set function signature in Python

Suppose I have a generic function f. I want to programmatically create a function f2 that behaves the same as f, but has a customised signature. More detail Given a list l and and dictionary d I want to be able to: Set the non-keyword arguments of f2 to the strings in l Set the keyword arguments of f2 to the keys in d and the default...

Python App Engine uploaded image content-type

I know that I can accept image uploads by having a form that POSTs to App Engine like so: <form action="/upload_received" enctype="multipart/form-data" method="post"> <div><input type="file" name="img"/></div> <div><input type="submit" value="Upload Image"></div> </form> Then in the Python code I can do something like image = self.re...

Python code readability

I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class Host: class Host(object): def __init__(self, name, network_interface): self.name = name self.network_interface = network_interface I don't understand from this definiti...

Borg pattern problem when used in two different modules

I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter. Is there a way to use the Borg in that case without reworking the modul...

## in python using notepad++ syntax coloring

In my editor (notepad++) in Python script edit mode, a line ## is this a special comment or what? Turns a different color (yellow) than a normal #comment. What's special about a ##comment vs a #comment? ...

Inverse Dict in Python

I am trying to create a new dict using a list of values of an existing dict as individual keys. So for example: dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}) and I would like to obtain: dict2 = dict({1:['a','b','c'], 2:['a','b','c'], 3:['a','b'], 4:['b']}) So far, I've not been able to do this in a very clean way. Any sugg...

Returning a c++ array (pointer) from boost python

I'm currently writing python bindings for a c++ library I'm working on. The library reads some binary file format and reading speed is very important. While optimizing the library for speed, I noticed that std::vector (used in the instances I'm reading) was eating up a lot of processing time, so I replaced those with simple arrays alloca...

How to implement Unicode string matching by folding in python

I have an application implementing incremental search. I have a catalog of unicode strings to be matched and match them to a given "key" string; a catalog string is a "hit" if it contains all of the characters in the key, in order, and it is ranked better if the key characters cluster in the catalog string. Anyway, this works fine and m...

What are the pros and cons of PyRo and RPyC python libs ?

I am looking for a remote procedure call engine for Python and I've found that PyRo (Python Remote Object) and RPyC (Remote Python Call) are both the kind of thing I am searching for. However, I am curious to know how they compare to each other and what are their pros and cons ? ...

Python image mirroring

Hey I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal. I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel and set the same color to...

Checking Python code correctness

In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time. Thanks ...

copy.deepcopy vs pickle

I have tree structure of widgets e.g. collection contains models and model contains widgets I wan to copy whole collection, copy.deepcopy is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so so why shouldn't I(we) always be using cPickle instead of deepcopy? Is there any ...

Is there a way to reopen a socket ?

I create many "short-term" sockets in some code that look like that : nb=1000 for i in range(nb): sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sck.connect((adr, prt) sck.send('question %i'%i) sck.shutdown(SHUT_WR) answer=sck.recv(4096) print 'answer %i : %s' % (%i, answer) sck.close() This works ...

manipulating pipe buffer size in C or python

hello, I have a general question about popen (and all related functions), applicable to all operating systems, when I write a python script or some c code and run the resulting executable from the console (win or linux), i can immediately see the output from the process. However, if I run the same executable as a forked process with its ...