python

timeit versus timing decorator

I'm trying to time some code. First I used a timing decorator: #!/usr/bin/env python import time from itertools import izip from random import shuffle def timing_val(func): def wrapper(*arg,**kw): '''source: http://www.daniweb.com/code/snippet368.html''' t1 = time.time() res = func(*arg,**kw) t2 = t...

Python debugging tips

What are your best tips for debugging Python? Please don't just list a particular debugger without saying what it can actually do. Related What is the best way to debug my Python code? - This actually discusses minimising errors rather than finding them (someone should do a rename) ...

Execute and monitor multiple instances of external program in Python

Main program is like this: PREPARE PARAMETERS FOR CHILD PROCESSES subprocess.Popen('python child.py param=example1'.split(' ')) subprocess.Popen('python child.py param=example2'.split(' ')) ... How to make main program to monitor each instances of child process it launched and restart it with its corresponding parameters if it's not r...

PAMIE and lxml related question

Hello, im making web scraper now. i was received many help from here Stackoverflow. now almost finished my scraper except some related with serveral problem :) i was uploaded my script source to http://elca.pastebin.com/m52e7d8e0 current problem is , if you see my script source line 74, you can see this line "thepage = urllib.urlopen(the...

Restrict access to images on my website except through my own htmls

Hi, On my website I store user pictures in a simple manner such as: "image/user_1.jpg". I don't want visitors to be able to view images on my server just by trying user_ids. (Ex: www.mydomain.com/images/user_2.jpg, www.mydomain.com/images/user_3.jpg, so on...) So far I have three solutions in mind: I tried using .htaccess to passwor...

Adventure game - walking around inside a room.

I'm working on an adventure game in Python using Pygame. My main problem is how I am going to define the boundaries of the room and make the main character walk aroud without hitting a boundary every time. Sadly, I have never studied algorithms so I have no clue on how to calculate a path. I know this question is quite general and hard t...

Pylons importing Psycopg2 error

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.6/site-packages/psycopg2/__init__.py", line 60, in <module> from _psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID ImportError: dlopen(/Library/Python/2.6/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID R...

Does using Psyco with django make any sense ?

I know the benefits of Psyco for a Desktop app, but in a Web app where a process ( = a web page or an AJAX call) dies immediately after been fired, isn't it pointless ? ...

Escaping '<' and '>' in xml when using xml.dom.minidom

Hello all, I am stuck while escaping "<" and ">" in the xml file using xml.dom.minidom. I tried to get the unicode hex value and use that instead http://slayeroffice.com/tools/unicode%5Flookup/ Tried to use the standard "<" and ">" but still with no success. from xml.dom.minidom import Document doc = Document() e = doc.createElem...

SQLAlchemy: Shallow copy avoiding lazy loading

Hi everyone! I'm trying to automatically build a shallow copy of a SA-mapped object.. At the moment my function is just: newobj = src.__class__() for prop in class_mapper(src.__class__).iterate_properties: setattr(newobj, prop.key, getattr(src, prop.key)) but I'm having troubles with lazy relations... Obviously getattr triggers th...

Fastest way to zero out low values in array?

So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this should be set to 0. What would be the fastest way to do this in Python? Order must be maintained. Most of the elements are already set to 0. sample variables: a...

Problem in importing classes from models file in GAE

Hi, I am very new to python and after a brief intro on to python + Google app engine, I've started to work on a pilot project. I have bulkloaded 2 entities UserDetails and PhoneBook with data onto the app engine. Now in my UI I try to first take in the user name then query it to get the user id from UserDetails, then using the retrieved...

Python - RegExp - Modify text files

Hi All, Newbie to Python.... help requested with the following task :-) I have tree of various files, some of them are C source code. I would like to modify these C files with python script. The C code has 4 defines - #define ZR_LOG0(Id, Class, Seveity, Format) #define ZR_LOG1(Id, Class, Seveity, Format, Attr0) #define ZR_LOG2(Id, Cl...

virtualenv confusion

So I open a terminal, cd to my desktop, and run: virtualenv test_env I then create the following file in my normal environment: /home/jesse/.local/lib/python2.6/site-packages/foo_package/__init__.py This file contains one line: print "importing from normal env" In the test_env I create: /home/jesse/Desktop/test_env/lib/python2.6/s...

How do I close the stdout-pipe when killing a process started with python subprocess Popen?

I wonder if it is possible to shut down the communication pipe when killing a subprocess started in a different thread. If I do not call communicate() then kill() will work as expected, terminating the process after one second instead of five. I found a discussion of a similar problem here, but I got no real answers. I assume that I eit...

Django or similar for composite primary keys

I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per http://code.djangoproject.com/ticket/373, I can't use Django, at least not a released version. Can anyone help ...

Removing Array Elements in Python while keeping track of their position

Hi! I'v got two numpy arrays. The first array contains some zeros (which are distributed randomly over the length of the array), which I would like to remove. My issue is that I would also like to remove the entries of the second array at the index positions where the first array elements are zero. I only came up with a very cumbers...

How to avoid repetition of exception handling?

I've moved IOError handling to a separate function to avoid boilerplate when opening files for reading. But what if IOError fires when the file is being read? If sshfs disconnects, or file is deleted by root, etc.? def safe_open(*args): try: return open(*args) except IOError: quit('Error when opening file \'{0}\...

GUI + multithreading support + regex support. Which language? JAVA / Python / Ruby ??

I'm interested in learning a programming language with support for GUI, multithreading and easy test manipulation (support for regex). Mainly on Windows but preferably cross-platform. What does the Stack Overflow community suggest? ...

Django model fields validation

Where should the validation of model fields go in django? I could name at least two possible choices: in the overloaded .save() method of the model or in the .to_python() method of the models.Field subclass (obviously for that to work you must write custom fields). Possible use cases: when it is absolutely neccessary to ensure, that ...