python

Can I use Python as a bash replacement?

I currently do my textfile manipulation through a bunch of badly remembered awk, sed, bash and a tiny bit of Perl. I've seen mentioned a few places that python is good for this kind of thing, I know a little and I would like to know more. Is python a good choice for this, and is there a good book or guide to learning how to use python t...

Howto do python command-line autocompletion but NOT only at the beginning of a string

Python, through it's readline bindings allows for great command-line autocompletion (as described in here). But, the completion only seems to work at the beginning of strings. If you want to match the middle or end of a string readline doesn't work. I would like to autocomplete strings, in a command-line python program by matching wha...

Convert hex string to int in Python

How do I convert a hex string to an int in Python? I may have it as "0xffff" or just "ffff". ...

Map two lists into a dictionary in Python

Imagine that you have: keys = ('name', 'age', 'food') values = ('Monty', 42, 'spam') What is the simplest way to produce the following dictionary ? dict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'} This code works, but I'm not really proud of it : dict = {} junk = map(lambda k, v: dict.update({k: v}), keys, values) ...

Tutorial for Python - Should I use 2.x or 3.0?

Python 3.0 is in beta with a final release coming shortly. Obviously it will take some significant time for general adoption and for it to eventually replace 2.x. I am writing a tutorial about certain aspects of programming Python. I'm wondering if I should do it in Python 2.x or 3.0? (not that the difference is huge) a 2.x tutorial ...

Advanced Python FTP - can I control how ftplib talks to a server?

I need to send a very specific (non-standard) string to an FTP server: dir "SYS:\IC.ICAMA." The case is critical, as are the style of quotes and their content. Unfortunately, ftplib.dir() seems to use the 'LIST' command rather than 'dir' (and it uses the wrong case for this application). The FTP server is actually a telephone switch...

py3k RC-1: "LookupError: unknown encoding: uft-8"

I just installed the first release candidate of Python 3.0 and got this error after typing: >>> help('modules foo') [...] LookupError: unknown encoding: uft-8 Notice that it says uft-8 and not utf-8 Is this a py3k specific bug or a misconfiguration on my part? I do not have any other versions of Python installed on this French local...

Distributing a stand-alone Python web-based application to non-technical users

I'm writing a web application in Python, intended for use by teachers and pupils in a classroom. It'll run from a hosted website, but I also want people to be able to download a self-contained application they can install locally if they want more performance or they simply won't have an Internet connection available in the classroom. T...

I need some help with cursor event handling in python+Tkinter

Hey, I'm building a code in which I'd like to be able to generate an event when the user changes the focus of the cursor from an Entry widget to anywhere, for example another entry widget, a button... So far i only came out with the idea to bind to TAB and mouse click, although if i bind the mouse click to the Entry widget i only get m...

Python: unsigned 32 bit bitwise arithmetic

Trying to answer to another post whose solution deals with IP addresses and netmasks, I got stuck with plain bitwise arithmetic. Is there a standard way, in Python, to carry on bitwise AND, OR, XOR, NOT operations assuming that the inputs are "32 bit" (maybe negative) integers or longs, and that the result must be a long in the range [0...

Using os.execvp in Python

I have a question about using os.execvp in Python. I have the following bit of code that's used to create a list of arguments: args = [ "java" , classpath , "-Djava.library.path=" + lib_path() , ea , "-Xmx1000m" , "-server" , "code_swarm" , params ] When I output a string using ...

Create an icon in memory with win32 in python

What's a good way to generate an icon in-memory in python? Right now I'm forced to use pygame to draw the icon, then I save it to disk as an .ico file, and then I load it from disk as an ICO resource... Something like this: if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE ...

Python's __import__ doesn't work as expected

When using __import__ with a dotted name, something like: somepackage.somemodule, the module returned isn't somemodule, whatever is returned seems to be mostly empty! what's going on here? ...

How to build "Tagging" support using CouchDB?

I'm using the following view function to iterate over all items in the database (in order to find a tag), but I think the performance is very poor if the dataset is large. Any other approach? def by_tag(tag): return ''' function(doc) { if (doc.tags.length > 0) { for (var tag in doc.tags) { ...

Python Inverse of a Matrix

How do I get the inverse of a matrix in python? I've implemented it myself, but it's pure python, and I suspect there are faster modules out there to do it. ...

arguments to cryptographic functions

I'm a bit confused that the argument to crypto functions is a string. Should I simply wrap non-string arguments with str() e.g. hashlib.sha256(str(user_id)+str(expiry_time)) hmac.new(str(random.randbits(256))) (ignore for the moment that random.randbits() might not be cryptographically good). edit: I realise that the hmac example is s...

Using SQLite in a Python program

I have created a Python module that creates and populates several SQLite tables. Now, I want to use it in a program but I don't really know how to call it properly. All the tutorials I've found are essentially "inline", i.e. they walk through using SQLite in a linear fashion rather than how to actually use it in production. What I'm try...

Scripting in Java

Me and some friends are writing a MORPG in Java, and we would like to use a scripting language to, eg. to create quests. We have non experience with scripting in Java. We have used Python, but we are very inexperienced with it. One of us also have used Javascript. What scripting language should we use? What scripting language should w...

What is an easy way to create a trivial one-off Python object?

I would like to create a trivial one-off Python object to hold some command-line options. I would like to do something like this: options = ?????? options.VERBOSE = True options.IGNORE_WARNINGS = False # Then, elsewhere in the code... if options.VERBOSE: ... Of course I could use a dictionary, but options.VERBOSE is more readabl...

Turning a GqlQuery result set into a python dictionary

Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar = db.StringProperty() baz = db.StringProperty() And I'm going a GqlQuery like this foos = db.GqlQuery("SELECT * FROM Foo") I want to take the results of the GqlQuery and turn into some sort of JSON string that I can manipulate from diff...