python

Invoking Wine From Apache

I have Apache/2.2.11 using mod_python 3.3.1/Python 2.5 running under Gentoo linux. In my python script I invoke a win32 exe using wine (os.popen2 call). This works fine outside of Apache but under mod_python I get: wine: cannot open /root/.wine : Permission denied in /var/log/apache/error_log. My apache install is not running as the...

pure web based versioning system

My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. I am looking for a pure php/python/ruby version control system (not just a client for a version control system) that does not require any services...

Various Python datetime issues

I have two methods that I'm using as custom tags in a template engine: # Renders a <select> form field def select_field(options, selected_item, field_name): options = [(str(v),str(v)) for v in options] html = ['<select name="%s">' % field_name] for k,v in options: tmp = '<option ' if k == selected_item: tmp +...

Newline characters in non ASCII encoded files

I'm using Python 2.6 to read latin2 encoded file with windows line endings ('\r\n'). import codecs file = codecs.open('stackoverflow_secrets.txt', encoding='latin2', mode='rt') line = file.readline() print(repr(line)) outputs : u'login: yabcok\n' file = codecs.open('stackoverflow_secrets.txt', encoding='latin2', mode='r') line = fil...

Delete file from zipfile with the ZipFile Module

Hello pythonians, The only way i came up for deleting a file from a zipfile was to create a temporary zipfile without the file to be deleted and then rename it to the original filename. In python 2.4 the ZipInfo class had an attribute file_offset, so it was possible to create a second zip file and copy the data to other file without de...

How to distribute script using gdata-python-client?

I've written several scripts that make use of the gdata API, and they all (obviously) have my API key and client ID in plain-text. How am I supposed to distribute these? ...

Python: List vs Dict for look up table

I have about 10million values that I need to put in some type of look up table, so I was wondering which would be more efficient a list or dict? I know you can do something like this for both: if something in dict_of_stuff: pass and if something in list_of_stuff: pass My thought is the dict will be faster and more efficien...

How to split a string by using [] in Python

So from this string: "name[id]" I need this: "id" I used str.split ('[]'), but it didn't work. Does it only take a single delimiter? ...

Elegant ways to return multiple values from a function

It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing. The typical solutions are to make either a struct or a plain old data class and return that, or to pass at least some of the parameters by reference or pointer instead of returning them. Using references/poin...

Extension methods in Python

Does Python have extension methods like C#? Is it possible to call a method like: MyRandomMethod() on existing types like int? myInt.MyRandomMethod() ...

What's the bad magic number error?

What's the "Bad magic number" ImportError in python, and how do I fix it? The only thing I can find online suggests this is caused by compiling a .py -> .pyc file and then trying to use it with the wrong version of python. In my case, however, the file seems to import fine some times but not others, and I'm not sure why. The informatio...

Python: loop over consecutive characters?

In Python (specifically Python 3.0 but I don't think it matters), how do I easily write a loop over a sequence of characters having consecutive character codes? I want to do something like this pseudocode: for Ch from 'a' to 'z' inclusive: # f(Ch) Example: how about a nice "pythonic" version of the following? def Pangram(Str): ...

Help building a regular expression in python using the re module

Hi guys, im writing a simple propositional logic formula parser in python which uses regular expressions re module and the lex/yacc module for lexing/parsing. Originally my code could pick out implication as ->, but adding logical equivalence (<->) caused issues with the compiled expressions IMPLICATION = re.compile('[\s]*\-\>[\s]*') EQ...

Random name generator strategy - help me improve it

I have a small project I am doing in Python using web.py. It's a name generator, using 4 "parts" of a name (firstname, middlename, anothername, surname). Each part of the name is a collection of entites in a MySQL databse (name_part (id, part, type_id), and name_part_type (id, description)). Basic stuff, I guess. My generator picks a ra...

Translating Python Regexp to Shell

Hello, friends I'm writing an Applescript playlist generator. Part of the process is to read the iTunes Library XML file to get a list of all of the genres in a user's library. This is the python implementation, which works as I'd like: #!/usr/bin/env python # script to get all of the genres from itunes import re,sys,sets ## ...

Web based wizard with Python

What is a good/simple way to create, say a five page wizard, in Python, where the web server component composes the wizard page content mostly dynamically by fetching the data via calls to a XML-RPC back-end. I have experienced a bit with the XML-RPC Python module, but I don't know which Python module would be providing the web server, h...

How best to pass database objects to a turbogears WidgetList?

I am trying to set up form widgets for adding some objects to the database but I'm getting stuck because it seems impossible to pass any arguments to Widgets contained within a WidgetList. To clarify that, here is my WidgetList: class ClientFields(forms.WidgetsList): """Form to create a client""" name = forms.TextField(validato...

Detect key press combination in Linux with Python?

I'm trying to capture key presses so that when a given combination is pressed I trigger an event. I've searched around for tips on how to get started and the simplest code snippet I can find is in Python - I grabbed the code below for it from here. However, when I run this from a terminal and hit some keys, after the "Press a key..." st...

Most pythonic way to extend a potentially incomplete list

What I'm looking for is the best way to say, 'If this list is too short, lengthen it to 9 elements and add 'Choice 4', 'Choice 5', etc, as the additional elements. Also, replace any 'None' elements with 'Choice x'.' It is ok to replace "" and 0 too. An example transformation would be ['a','b',None,'c'] to ['a','b','Choice 3','c','Ch...

Is it possible to google search with the gdata API?

I might be just thick (nothing new), but I can't seem to find anything related to an old-fashioned, vanilla google search in the gdata API docs. Anyone know if it's possible? (I know it probably is with a little tinkering, but I already have a Python web-scraping class created that does it for me, but I was wondering if using gdata would...