python

posix_memalign within python

I cannot seem to figure it out why the following does not work import ctypes from ctypes.util import find_library libc = ctypes.CDLL(find_library('c')) userpointer = ctypes.c_void_p sizeimage = 320*240*2 if libc.posix_memalign(userpointer, libc.getpagesize(), sizeimage) != 0: raise Exception('ENOMEM') I am trying to capture usin...

saving python variable to .mat file with scipy.io.savemat

Hi all, Here is my python code. >>import numpy as np >>import scipy.io >>exon = [ np.array([[1, 2], [3, 4], [5, 6]]), np.array([[7, 8], [9, 10]]) ] >>obj_arr = np.zeros((2,), dtype=np.object) >>obj_arr[0] = exon[0] >>obj_arr[1] = exon[1] >>scipy.io.savemat('/tmp/out.mat', mdict={'exon': obj_arr}, format='5') But I am getting an err...

Function called three times when in facebook ?

Hi, I'm using the following code to add one to two values in case the user wins a challege. def challengewin(request): uid = 1313693 tempuser = User.objects.get(id=uid) tempuser.challengeswon = tempuser.challengeswon + 1 tempuser.silver = tempuser.silver + 1 tempuser.save() return HttpResponse() this works fin...

Parse .strings file with Python

I'm trying to write a small Python script to parse the .strings file in my iPhone application project and determine which keys might not be in use. I'm, also doing some string matching to filter out some of the results. This is where my problems start :). If I try something like for file_line in strings_file: if 'search_keyword'...

How to get version of python dependencies/modules, including external applications called via subprocess.Popen?

I have a set of python scripts that I run frequently on different machines that depend on a few external libraries as well as some other applications spawned via subprocess.Popen. As expected depending on the version of the installed modules and applications the output varies. To address this I would like to keep track of which versions...

Best way to handle list.index(might-not-exist) in python?

I have code which looks something like this: thing_index = thing_list.index(thing) otherfunction(thing_list, thing_index) ok so that's simplified but you get the idea. Now thing might not actually be in the list, in which case I want to pass -1 as thing_index. In other languages this is what you'd expect index() to return if it couldn...

How to import or include data structures (e.g. a dict) into a Python file from a separate file

I know I can include Python code from a common file using import MyModuleName - but how do I go about importing just a dict? The problem I'm trying to solve is I have a dict that needs to be in a file in an editable location, while the actual script is in another file. The dict might also be edited by hand, by a non-programmer. script....

Is Matlab faster than Python?

I want to compute magnetic fields of some conductors using the biot-savart-law and I want to use a 1000x1000x1000 matrix. Before I use Matlab, but now I want to use Python. Is Python slower than Matlab? How can I make Python faster? EDIT: Maybe the best way is to compute the big array with c/c++ and then transfering them to python. I w...

Paramiko equvalent of pipeline controls and input/output pipes

I need a method of paramiko based file transfer with a lightweight SSH2 server (dropbear) which has no support for SCP or SFTP. Is there a way of achieving a cat and redirect style file transfer, such as: ssh server "cat remote_file" > local_file with paramiko channels? Can paramiko.Transport.open_channel() or Message() do the job? I...

python subprocess communicate() block

i am using subprocess to call a external program plink.exe to login to a server, but when i call communicate to read the output, it blocking. the code is below: import subprocess process = subprocess.Popen('plink.exe [email protected] -pw 123456'.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print process....

django: gettext and coercing to unicode

I have following code in my django application. class Status(object): def __init__(self, id, desc): self.id = id self.desc = desc def __unicode__(self): return self.desc STATUS = Status(0, _(u"Some text")) When I try to display some status (or even coerce it to unicode), I get: TypeError: coercing t...

Is it possible get all context nodes used to evalute xpath result ?

Is it possible get all context nodes used to evalute xpath result ? In below code: test_xml = """ <r> <a/> <a> <b/> </a> <a> <b/> </a> </r> """ test_root = lxml.etree.fromstring(test_xml) res = test_root.xpath("//following-sibling::*[1]/b") for node in res: print test_root.getroottree().getpath(no...

Most Pythonic way to concatenate strings

Given this harmless little list: >>> lst = ['o','s','s','a','m','a'] My goal is to pythonically concatenate the little devils using one of the following ways: A. plain ol' string function to get the job done, short, no imports >>> ''.join(lst) 'ossama' B. lambda, lambda, lambda >>> reduce(lambda x, y: x + y, lst) 'ossama' C. g...

ArcGIS: accessing python list output from listFields geoprocessor

I'm trying to access the output from the listFields geoprocessing object using the following code: sFields = gp.ListFields(linktofeatureclass) for j in range(len(sFields)): print sFields[j] How do I get information about the fields that I have enumerated? Printing them (i.e. sFields in the above) just returns "geoprocessing descri...

XQuery library under Python.

Hey Is there any existing way to run XQuery under python? (not starting to build a parser yourself in other words). I got a ton of legacy XQuery that I want to port to our new system, or rather I want to port the framework and not XQuery. Therefore: Is there any library that allows me to run XQuery under python? ...

Using python graphviz ImportError: No module named _gv

Hi! I'm trying to use graphviz with python and I get the error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/pymodules/python2.6/gv.py", line 7, in <module> import _gv ImportError: No module named _gv The system state: dpkg -l|grep graphviz ii graphviz 2.20.2-3ubuntu5 ...

capture using v4l2 and display preview using gstreamer

How to pass the buffer/userpointer to gstreamer after Q_BUF, STREAM_ON, DQ_BUF. I tried using PIL's method frombuffer, but with no success. so I want to use gst sink now. Should I use gst.parse_launch() and how? Have anybody done it? ...

Pyparsing - where order of tokens in unpredictable

I want to be able to pull out the type and count of letters from a piece of text where the letters could be in any order. There is some other parsing going on which I have working, but this bit has me stumped! input -> result "abc" -> [['a',1], ['b',1],['c',1]] "bbbc" -> [['b',3],['c',1]] "cccaa" -> [['a',2],['c',3]] I could use se...

Multiple Inheritance in Python (Problem Specific)

Can anyone here identify why the TypeError is being raised at the bottom of this example shown below? >>> import threading >>> class SessionManager(threading.Thread, threading._RLock, dict): UPDATE = 60 * 60 def run(self): while True: time.sleep(self.UPDATE) with self: for key in...

python select specific elements from a list

Is there a "pythonic" way of getting only certain values from a list, similar to this perl code: my ($one,$four,$ten) = line.split(/,/)[1,4,10] ...