python

Python. Output of crypto.cipher.blowfish and .AES can be translated neither into unicode (as sqlite wants), nor into hex.

Okay, I'm totally new to Python, so I decided to make a simple app. Here is my encryption function: from Crypto.Cipher import AES def encPass(login, password): keyPhr=os.environ['HOME']+login hashObj = hashlib.md5() hashObj.update(keyPhr) keyPhr=hashObj.hexdigest() keyObj=AES.new(keyPhr) encPwd=keyObj.encrypt(pas...

Performance differences between Python and C

Working on different projects I have the choice of selecting different programming languages, as long as the task is done. I was wondering what the real difference is, in terms of performance, between writing a program in Python, versus doing it in C. The tasks to be done are pretty varied, e.g. sorting textfiles, disk access, network...

Equivalent of assertRegexMatches in python 2.4

Hi ! Say I have a regex REGEX = re.compile('.*foo{') How would you write a unit test that matches a set of string with python 2.4 ? I know in python 2.7 I can use assertRegexMatches, unfortunately this doesn't work in 2.4 :/ I use self.assertEqual for the rest of my tests. Cheers, M ...

gtk: detect click on a cell in a TreeView

I'm displaying some data as a TreeView. How can I detect a click on a particular tree-view cell, so that I know which column of which row was clicked on? This is what I want to do, so maybe there's a better way: Part of the data is a series of True/False values indicating a particular set of options. For example, the options might be pi...

GAE self.request.environ and server host

I'm trying to obtain the base URL (hostname) of the server in which my appengine app is running on. Ie something along the lines of wsgiref.util.application_uri(self.request.environ) However it's returning the PATH_INFO which I do not want. Perhaps I'm missing something but even this article states the path info should be omitted. h...

How can I inject an object into another namespace in python?

I'm writing some unittests for code written by someone else here at the office. Python is not my strongest language. While I've been successful with basic unit tests, mocking in python is throwing me for a loop. What I need to do is override a call to ConfigObj and inject my own mock config/fixture into any ConfigObj call. settings.py ...

Setting the default value of a function input to equal another input in Python

Hello, Consider the following function, which does not work in Python, but I will use to explain what I need to do. def exampleFunction(a, b, c = a): ...function body... That is I want to assign to variable c the same value that variable a would take, unless an alternative value is specified. The above code does not work in pytho...

Python regex, matching pattern over multiple lines.. why isn't this working?

I know that for parsing I should ideally remove all spaces and linebreaks but I was just doing this as a quick fix for something I was trying and I can't figure out why its not working.. I have wrapped different areas of text in my document with the wrappers like "####1" and am trying to parse based on this but its just not working no ma...

Building mod_wsgi using python 2.5 on Snow Leopard

I'm using the Python 2.5 that came with Mac OS X Snow Leopard (10.6). I've set the defaults value: defaults write com.apple.versioner.python Version 2.5 and normally I get python 2.5 as it suggests. However when I try to build mod_wsgi, that doesn't seem to adhere. I've used the --with-python=/usr/bin/python2.5 option to configure to fo...

gtk: trouble modifying TreeView model on CellRendererCombo 'changed' signal

I have a treeview with a CellRendererCombo in a given column. I use the following code to set up the column: crc = gtk.CellRendererCombo() crc.set_property('model', comboModel) crc.set_property('text-column', 0) crc.set_property('editable', True) crc.set_property('has_entry', False) cl = gtk.TreeViewColumn(ctitle, crc, text=i) def chan...

Python idiom: List comprehension with limit of items

I'm basically trying to do this (pseudo code, not valid python): limit = 10 results = [xml_to_dict(artist) for artist in xml.findall('artist') while limit--] So how could I code this in a concise and efficient way? The XML file can contain anything between 0 and 50 artists, and I can't control how many to get at a time, and AFAIK, the...

python equivalent to perl's qw()

I do this a lot in Perl: printf "%8s %8s %8s\n", qw(date price ret); However, the best I can come up with in Python is print '%8s %8s %8s' % (tuple("date price ret".split())) I'm just wondering if there is a more elegant way of doing it? I'm fine if you tell me that's it and no improvement can be made. ...

Python MIT Open Courseware Stock Market Simulation Incomplete?

Hello, I just copied this code from the MIT video lecture that is posted online: (Lec 23 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008). Since I had to copy it from a video lecture, I'm not sure I got the complete program. It is not working as is, I could use some guidance. Thanks. import pylab, random clas...

Unpythonic way of printing variables in Python?

Someone has recently demonstrated to me that we can print variables in Python like how Perl does. Instead of: print("%s, %s, %s" % (foo, bar, baz)) we could do: print("%(foo)s, %(bar)s, %(baz)s" % locals()) Is there a less hacky looking way of printing variables in Python like we do in Perl? I think the 2nd solution actually looks...

Is there a way to check a function's signature in Python?

I'm looking for a way to check the number of arguments that a given function takes in Python. The purpose is to achieve a more robust method of patching my classes for tests. So, I want to do something like this: class MyClass (object): def my_function(self, arg1, arg2): result = ... # Something complicated return ...

how to keep count of replaced strings

I have a massive string im trying to parse as series of tokens in string form, and i found a problem: because many of the strings are alike, sometimes doing string.replace()will cause previously replaced characters to be replaced again. say i have the string being replaced is 'goto' and it gets replaced by '41' (hex) and gets converted ...

How to make a selective RNG for a game in Python?

This is almost certainly a very novice question, but being as I am a complete novice, I'm fine with that. To put it simply, I'd like to know how to make a loot drop system in a simple game, where when you achieve a certain objective, you have a chance of getting certain objects more than others. If there are any open-source python game...

Does Google have any official Image API for Python?

I want to search for images using a Python script and pull them off the web. Is there an official API for this? What would be the best way to do this in case there's no API for it? ...

Unique constraint using data in multiple tables (SQL / SQLAlchemy)

A top class called Parametric is used to create objects which can have parameters associated with them: class Parametric(object): def __init__(self, name): self.name = name self.pars = [] class Foo(Parametric): def __init__(self, name, prop): self.prop = prop Parametric.__init__(self, name) class Bar(Parametric): def __init...

Uploaded and not converted files do not appear in the document feed in Google Docs

I trying to retrieve a complete list of files from a given directory with code like this uri = '%s' % fentry.content.src feed = gd_client.GetDocumentListFeed(uri=uri) for r in feed.entry: print r.title.text.decode("utf-8") It works except that it only return "real" Google Documents files and does not return files, which were uploa...