python

How to change the stdin encoding on python

Hi, I'm using windows and linux machines for the same project. The default encoding for stdin on windows is cp1252 and on linux is utf-8. I would like to change everything to uft-8. Is it possible? How can I do it? Thanks Eduardo ...

itertools.islice compared to list slice

I've been trying to apply an algorithm to reduce a python list into a smaller one based on a certain criteria. Due to the large volume of the original list, in the order of 100k elements, I tried to itertools for avoiding multiple memory allocations so I came up with this: reducedVec = [ 'F' if sum( 1 for x in islice(vec, i, i+ratio) if...

Adding CSRF protection to simple comment forms in Django

I have blog comment forms in Django and I would like to know the following: Should I add CSRF to the forms? If I want to use the simple "render_comment_form" method, how do I add it? If I can't add it like that, what is the best practice for doing it? Each tutorial or discussion on the subject seems to have a different approach, and ...

Python: Created nested dictionary from list of paths

I have a list of tuples the looks similar to this (simplified here, there are over 14,000 of these tuples with more complicated paths than Obj.part) [ (Obj1.part1, {<SPEC>}), (Obj1.partN, {<SPEC>}), (ObjK.partN, {<SPEC>}) ] Where Obj goes from 1 - 1000, part from 0 - 2000. These "keys" all have a dictionary of specs associated with th...

Exposing members or make them private in Python?

Is there a general convention about exposing members in Python classes? I know that this is a case of "it depends", but maybe there is a rule of thumb. Private member: class Node: def __init__(self): self.__children = [] def add_children(self, *args): self.__children += args node = Node() node.add_children("one", "two") ...

trying to run werkzeug on apache (wsgi error)

My data_site.wsgi file: import main application = application() Error i get at apache: [Thu Apr 29 07:07:41 2010] [error] [client 81.167.201.136] Traceback (most recent call last): [Thu Apr 29 07:07:41 2010] [error] [client 81.167.201.136] File "/var/www/vhosts/data.oddprojects.net/htdocs/data_site.wsgi", line 1, in <module> [Thu A...

access a file in python that is created from SunGridEngine

Hi guys i have a python script, that submits an job to the SGE (Sun Grid Engine). When the job is done i want to access the output file, generated from the SGE job. i see with "ls" in the directory that the file is already existing and the job is done, but python needs about 20-30 seconds to get access to that file... is there a way to ...

Error when running mmap related function in python 2.6

I tried to run the following code from http://docs.python.org/library/mmap.html import mmap # write a simple example file with open("hello.txt", "wb") as f: f.write("Hello Python!\n") with open("hello.txt", "r+b") as f: # memory-map the file, size 0 means whole file map = mmap.mmap(f.fileno(), 0) # read content via st...

Conditional Regular Expressions

I'm using Python and I want to use regular expressions to check if something "is part of an include list" but "is not part of an exclude list". My include list is represented by a regex, for example: And.* Everything which starts with And. Also the exclude list is represented by a regex, for example: (?!Andrea) Everything, but no...

What's the advantage of using 'with .. as' statement in Python?

with open("hello.txt", "wb") as f: f.write("Hello Python!\n") seems to be the same as f = open("hello.txt", "wb") f.write("Hello Python!\n") f.close() What's the advantage of using open .. as instead of f = ? Is it just syntactic sugar? Just saving one line of code? ...

compare two windows paths, one containing tilde, in python

I'm trying to use the TMP environment variable in a program. When I ask for tmp = os.path.expandvars("$TMP") I get C:\Users\STEVE~1.COO\AppData\Local\Temp Which contains the old-school, tilde form. A function I have no control over returns paths like C:\Users\steve.cooper\AppData\Local\Temp\file.txt My problem is this; I'd lik...

Testing Python Decorators?

I'm writing some unit tests for a Django project, and I was wondering if its possible (or necessary?) to test some of the decorators that I wrote for it. Here is an example of a decorator that I wrote: class login_required(object): def __init__(self, f): self.f = f def __call__(self, *args): request = args[0...

In Windows shell scripting (cmd.exe) how do you assign the stdout of a program to an environment variable?

In UNIX you can assign the output of a script to an environment variable using the technique explained here - but what is the Windows equivalent? I have a python utility which is intended to correct an environment variable. This script simply writes a sequence of chars to stdout. For the purposes of this question, the fact that my utili...

Lisp's "some" in Python?

I have a list of strings and a list of filters (which are also strings, to be interpreted as regular expressions). I want a list of all the elements in my string list that are accepted by at least one of the filters. Ideally, I'd write [s for s in strings if some (lambda f: re.match (f, s), filters)] where some is defined as def so...

What's the equivalence of os.fork() on Windows with Python?

This code works well in Mac/Linux, but not in Windows. import mmap import os map = mmap.mmap(-1, 13) map.write("Hello world!") pid = os.fork() if pid == 0: # In a child process print 'child' map.seek(0) print map.readline() map.close() else: print 'parent' What's the equivalent function of os.fork() on Window...

Cannot import PyQt4.QtGui

I have a working Python 2.6 install and just installed the PyQt4 built for Python 2.6 (available at http://www.riverbankcomputing.co.uk/software/pyqt/download). When I try to import PyQt4.QtGui I get the following error: ImportError: DLL load failed: The specified procedure could not be found. I'm on Windows 2k8 64-bit, but my Python ...

Python - do big doc strings waste memory?

I understand that in Python a string is simply an expression and a string by itself would be garbage collected immediately upon return of control to a code's caller, but... Large class/method doc strings in your code: do they waste memory by building the string objects up? Module level doc strings: are they stored infinitely by the int...

Python - question regarding the concurrent use of `multiprocess`.

I want to use Python's multiprocessing to do concurrent processing without using locks (locks to me are the opposite of multiprocessing) because I want to build up multiple reports from different resources at the exact same time during a web request (normally takes about 3 seconds but with multiprocessing I can do it in .5 seconds). My ...

retrieve the 2 highest item from a list containing 100 000 integers

Hi everyone, How can retrieve the 2 highest item from a list containing 100 000 integers. You do understand without having to sort the entire list. ...

Creating a simple command line interface (CLI) using a python server (TCP sock) and few scripts

I have a Linux box and I want to be able to telnet into it (port 77557) and run few required commands without having to access to the whole Linux box. So, I have a server listening on that port, and echos the entered command on the screen. (for now) Telnet 192.168.1.100 77557 Trying 192.168.1.100... Connected to 192.168.1....