python

How to create a list or tuple of empty lists in Python?

I need to incrementally fill a list or a tuple of lists. Something that looks like this: result = [] firstTime = True for i in range(x): for j in someListOfElements: if firstTime: result.append([f(j)]) else: result[i].append(j) In order to make it less verbose an more elegant, I thought I wi...

Problem with MPICH2 & mpi4py Installation

Hello, I'm on Windows XP2 32-bit machine. I'm trying to install MPICH2 & mpi4py. I've downloaded & installed MPICH2-1.2.1p1 I've downloaded & mpi4py When I run python setup.py install in mpi4pi\ directory. I get running install running build running build_py running build_ext MPI configuration: directory 'C:\Program Files\MPICH2' M...

how to send the output of pprint module to a log file

I have the following code: logFile=open('c:\\temp\\mylogfile'+'.txt', 'w') pprint.pprint(dataobject) how can i send the contents of dataobject to the log file on the pretty print format ? ...

Need Help on Python + Outlook

Hello All, I am developing Outlook addins using Python and I am not able to fetch Message-ID of the selected Mail. I have googled and read lot but didn’t get any satisfying solutions and I don't want to use any other Property except Message-ID. My whole Outlook Addin is Based on logic of the MessageID cause I have my own server and I wan...

Add repository url to install_requires in project's setup.py

Hi, I'm developing a django app which depends on an app in a private bitbucket repository, for example ssh:/[email protected]/username/my-django-app. is it possible to add this url to the list of install_requires in my setup.py? tried various possibilities, but none worked. ...

closing files properly opened with urllib2.urlopen()

I have following code in a python script try: # send the query request sf = urllib2.urlopen(search_query) search_soup = BeautifulSoup.BeautifulStoneSoup(sf.read()) sf.close() except Exception, err: print("Couldn't get programme information.") print(str(err)) return I'm concerned because if I encounter a...

Arithmetic Progression in Python without storing all the values

Hello, I'm trying to represent an array of evenly spaced floats, an arithmetic progression, starting at a0 and with elements a0, a0 + a1, a0 + 2a1, a0 + 3a1, ... This is what numpy's arange() method does, but it seems to allocate memory for the whole array object and I'd like to do it using an iterator class which just stores a0, a1 and ...

using function attributes to store results for lazy (potential) processing.

I'm doing some collision detection and I would very much like to use the same function in two different contexts. In one context, I would like for it to be something like def detect_collisions(item, others): return any(collides(item, other) for other in others) and in another, I would like it to be def get_collisions(item, others...

How to import external module in python macro for OpenOffice?

Hello :) I'm writing a macro for OpenOffice in python, and I have a problem. I want to import external module (gdata) so i write import uno import gdata.calendar.service but when I try execute macro (Tools -> Marcos -> Run Macro..) I always get ImportError :/ I have gdata module in my PYTHONPATH, it's working when I test it in interp...

use sqlalchemy entity isolately

i just want to use an entity modify it to show something,but don't want to change to the db, but after i use it ,and in some other place do the session.commit() it will add this entity to db,i don't want this happen, any one could help me? ...

Future and stability of IronPython

I am currently looking for a possible way to integrate my C++/C# application with some of my Python scripts. At this point, IronPython seems like the way to go. However, before proceeding, I would like to ask the following: How stable is IronPython right now? Is it ready for production use? Are there any known major quirks/bugs? What ...

Difference between returns and printing in python?

In python I don't seem to be understanding the return function. Why use it when I could just print it? def maximum(x, y): if x > y: print(x) elif x == y: print('The numbers are equal') else: print(y) maximum(2, 3) This code gives me 3. But using return it does the same exact thing. def maximum(x, ...

Numpy - add row to array

Hi, How does one add rows to a numpy array? I have an array A: A = array([[0, 1, 2], [0, 2, 0]]) I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition. Numpy arrays do not have a method 'append' like that of lists, or so it seems. If A and X were lists I would merely...

boost::python: howto call a function that expects a pointer?

Hi all, I have a function that takes an int-pointer and exposed it via boost::python. How can I call this function from python? in C++ with boost::python: void foo(int* i); ... def("foo", foo); in python: import foo_ext i = 12 foo_ext.foo(i) results in Traceback (most recent call last): File "<stdin>", line 1, in <module> Boos...

Authenticating on Web.py - will this code be unsafe for production?

I am making a simple web-app which requires login for the admin page. I came across this incantation on the web.py site (http://webpy.org/cookbook/userauth) : import hashlib import web def POST(self): i = web.input() authdb = sqlite3.connect('users.db') pwdhash = hashlib.md5(i.password).hexdigest() check = authdb.e...

Set python virtualenv in vim

I use vim for coding and for python coding in particular. Often I want to execute the current buffer with python interpreter. (for example to run unittests), usually I do this with :!python % <Enter> This scenatio will work works fine with global python, but I want to run virtualenv python instead. How do I enable virtualenv within vim...

Require help in Django 'local variable 'form' referenced before assignment'

Hello, I am having problem in django. I have created a form in my app where I can take details of a client. Now I want to create a form which can allow me to edit a form. However I am having some problems when I go to /index/edit_client/1, I get this error. local variable 'form' referenced before assignment I do not know what the reas...

how to add dynamic property for python object

site=object() for k,v in dict.iteritems(): setattr(site,k,v) print site.a#it won't works i use this code but it didn't work,any one could help?thanks ...

figuring out how to get all of the public ips of a machine

I am running my code on multiple VPSes (with more than one IP, which are set up as aliases to the network interfaces) and I am trying to figure out a way such that my code acquires the IP addresses from the network interfaces on the fly and bind to it. Any ideas on how to do it in python without adding a 3rd party library ? Edit I know ...

IE7 vs. A Python Pickle Object

I have an issue with IE7 not wanting to pass a pickled object through a ajax call using HTMLTMPL. It works in IE8 (and in compatibility mode) as well as in Firefox. I have pickled an object using the command: newhash['pickled'] = pickle.dumps(hash) Because JS didn't like the newlines, i regex them out using: newhash['pickled'] = re...