python

Python: Comparing Lists

I have come across a small problem. Say I have two lists: list_A = ['0','1','2'] list_B = ['2','0','1'] I then have a list of lists: matrix = [ ['56','23','4'], ['45','5','67'], ['1','52','22'] ] I then need to iterate through list_A and list_B and effectively use them as co-ordinates. For example I take the firs number from list A...

How to find the local minima of a smooth multidimensional array in NumPy efficiently?

Say I have an array in NumPy containing evaluations of a continuous differentiable function, and I want to find the local minima. There is no noise, so every point whose value is lower than the values of all its neighbors meets my criterion for a local minimum. I have the following list comprehension which works for a two-dimensional ar...

How two merge several .csv files horizontally with python?

Hey, I've several .csv files (~10) and need to merge them together into a single file horizontally. Each file has the same number of rows (~300) and 4 header lines which are not necessarily identical, but should not be merged (only take the header lines from the first .csv file). The tokens in the lines are comma separated with no space...

Is this usage of python tempfile.NamedTemporaryFile secure?

Is this usage of Python tempfile.NamedTemporaryFile secure (i.e. devoid security issues of deprecated tempfile.mktemp)? def mktemp2(): """Create and close an empty temporary file. Return the temporary filename""" tf = tempfile.NamedTemporaryFile(delete=False) tfilename = tf.name tf.close() return tfilename outfi...

Rename dictionary keys according to another dictionary

(In Python 3) I have dictionary old. I need to change some of its keys; the keys that need to be changed and the corresponding new keys are stored in a dictionary change. What's a good way to do it? Note that there may be an overlap between old.keys() and change.values(), which requires that I'm careful applying the change. The followi...

how to round to higher 10's place in python

Hi, I have a bunch of floats and I want to round them up to the next highest multiple of 10. For example: 10.2 should be 20 10.0 should be 10 16.7 should be 20 94.9 should be 100 I only need it to go from the range 0-100. I tried math.ceil() but that only rounds up to the nearest integer. Thanks in advance. ...

XML "not well-formed (invalid token)" error from FlickrApi

First of all : I'm using the well known (and tested I suppose) flickrapi. I was testing synchronization of flickr photos with my project and everything worked fine till I reached some specific files. Then python's xml parser failed to parse xml to string (and error from topic). Debug gave me line and column in the xml, so I've exported i...

Python: Run function from the command line

Say in my file i have: def hello(): return 'Hi :)' How would I run this from the command line? ...

Google appengine: tornado vs webapp

I need a light/high performance/low cpu usage web framework use at google appengin, I am not sure which if fit for this between the default webapp framework of GAE and tornado web framework. FYI Well, any way, I have decide to use tornado template instead of the django template on GAE. Tornado is fast, but I don't know is it also fast...

Email notification on file change in particular directory with Python

Hello everyone, I would like to script a function wich is looking in a particular directory for files, if there are new files, it should send out an notification email. I already prepared a script which is looking for new files in a directory, it write the notification about a new file into the console. But now I would like to notified...

What is the Pythonic way to create informative comments in Python 2.x?

For further clarification, C# has the '///' directive which invokes the super-secret-styled Comments which allow you to have nice comments built into intellisense. Java has the '@' directive that allows you to have nice comments as well. Does Python have something like this? I hope this question is clear enough, thanks! ...

Beautiful Soup: Get the Contents of Sub-Nodes

Hello, I have following python code: def scrapeSite(urlToCheck): html = urllib2.urlopen(urlToCheck).read() from BeautifulSoup import BeautifulSoup soup = BeautifulSoup(html) tdtags = soup.findAll('td', { "class" : "c" }) for t in tdtags: print t.encode('latin1') This will return me following html code:...

add multiple columns to an sqlite database in python

Hi, I want to create a table with multiple columns, say about 100 columns, in an sqlite database. Is there a better solution than naming each column individually? I am trying the following: conn = sqlite3.connect('trialDB') cur = conn.cursor() listOfVars = ("added0",) for i in range(1,100): newVar = ("added" + str(i),) listOfV...

Redefining python list

is it possible to redefine the behavior of a Python List from Python, I mean, without having to write anything in the Python sourcecode? ...

django template "file name too long"

I'm very new to Python and Django so maybe someone can point me in the right direction. I have the following url.py line url(r'^$', direct_to_template, {'template':'index.html', 'extra_context':{'featured_actors': lambda: User.objects .annotate(avatars_nb=Co...

Custom field's to_python not working? - Django

Hi folks, I'm trying to implement an encrypted char field. I'm using pydes for encryption This is what I have: from pyDes import triple_des, PAD_PKCS5 from binascii import unhexlify as unhex from binascii import hexlify as dohex class BaseEncryptedField(models.CharField): def __init__(self, *args, **kwargs): self.td =...

Matplotlib: Curve touches axis boudary. How to solve this?

I drew this graph using matplotlib using the following code. import matplotlib import matplotlib.pyplot as plt x = [450.0, 450.0, 438.0, 450.0, 420.0, 432.0, 416.0, 406.0, 432.0, 400.0] y = [328.90000000000003, 327.60000000000031, 305.90000000000146, 285.2000000000013, 276.0, 264.0, 244.0, 236.0, 233.5, 236.0] z = [2,4,6,8,10,12,14,1...

Using python sockets to receive large http requests

I am using python sockets to receive web style and soap requests. The code I have is import socket svrsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = socket.gethostname(); svrsocket.bind((host,8091)) svrsocket.listen(1) clientSocket, clientAddress = svrsocket.accept() message = clientSocket.recv(4096) Some of the soa...

Python set and the "in" keyword usage issue

I'm trying to work with a set of django models in an external script. I'm querying the database at a preset interval to get the items that meet the query. When my external script is processing them, it takes a while and I may get the same results in the query if the processing hasn't updated the model yet. I figured I could use a set ...

Can Cython speed up array of object iteration?

I want to speed up the following code using cython: class A(object): cdef fun(self): return 3 class B(object): cdef fun(self): return 2 def test(): cdef int x, y, i, s = 0 a = [ [A(), B()], [B(), A()]] for i in xrange(1000): for x in xrange(2): for y in xrange(2): ...