python

pythonic way of selecing a random value that satisfies a certain predicate

Suppose I have a list of elements and I want to randomly select an element from the list that satisfies a predicate. What is the pythonic way of doing this? I currently do a comprehension followed by a random.choice() but that is unnecessarily inefficient : intlist = [1,2,3,4,5,6,7,8,9] evenlist = [ i for i in intlist if i % 2 == 0 ]...

How does polymorphism work in Python?

I'm new to Python... and coming from a mostly Java background, if that accounts for anything. I'm trying to understand polymorphism in Python. Maybe the problem is that I'm expecting the concepts I already know to project into Python. But I put together the following test code: class animal(object): "empty animal class" class dog(...

Oauth + Aeoid +Python + Google App Engine + Google documents question

I am trying to complete a story assignment system for my school newspaper in Google App Engine. It'll track deadlines for writers, allow writers to pick up stories, and give an "at a glance" view of the weeks stories. My partner and I are trying to fully integrate it with our newspapers Google Apps installation. Oh, and we have to use...

If I already know Perl Is Python worth learning?

I'm all for learning and continual improving one’s self, and I believe you should have as many tools as possible in your toolbox. However, I was wondering if it was worth it learning Python, since I already know a couple of dynamic interpreted languages, including Perl. My background is mostly C/C++/Java/C#, but I’ve programmed in Perl q...

Python modify an xml file

I have this xml model. link text So I have to add some node (see the text commented) to this file. How I can do it? I have writed this partial code but it doesn't work: xmldoc=minidom.parse(directory) child = xmldoc.createElement("map") for node in xmldoc.getElementsByTagName("Environment"): node.appendChild(child) Thanks in ad...

Eclipse + PyDev becomes extremely slow with large import

I'm trying to use Eclipse + PyDev for studying OpenGL programming but when I type from OpenGL.GL import * from OpenGL.GLUT import * IDE becomes extremely slow! Ok. It isn't a smart idea import to much useless things but it's so useful for learning a new library! Any help? PS: I use Ubuntu with Eclipse Galileo. ...

Change web service url for a suds client on runtime (keeping the wsdl)

Hi. First of all, my question is similar to this one But it's a little bit different. What we have is a series of environments, with the same set of services. For some environments (the local ones) we can get access to the wsdl, and thus generating the suds client. For external environment, we cannot access the wsdl. But being the same,...

How to have localized style when writing cell with xlwt

I'm writing an Excel spreadsheet with Python's xlwt and I need numbers to be formatted using "." as thousands separator, as it is in brazilian portuguese language. I have tried: style.num_format_str = r'#,##0' And it sets the thousands separator as ','. If I try setting num_format_str to '#.##0', I'll get number formatted as 1234.000 ...

Working with html generated from javascript

Hello, I have some html-page. There is a javascript which generates some content. I have to parse this content from python-script. I have saved copy of file on the computer. Are there any ways to work with 'already generated' html? Like I can see in the browser after opening page-file. As I understand, I have to work with DOM (maybe, xml...

tail -f in a webbrowser

I've created a Python script that monitors a logfile for changes (like tail -f) and displays it on a console. I would like to access the output of the Python script in a webbrowser. What would I need to create this? I was thinking about using Django and jQuery. Any tips or examples are greatly appreciated. ...

Adjective Nominalization in Python NLTK

Hi, Is there a way to obtain Wordnet adjective nominalizations using NLTK? For example, for 'happy' the desired output would be 'happiness'. I tried to dig around, but couldn't find anything. Thanks! ...

Django & custom auth backend (web service) + no database. How to save stuff in session?

I've been searching here and there, and based on this answer I've put together what you see below. It works, but I need to put some stuff in the user's session, right there inside authenticate. How would I store acme_token in the user's session, so that it will get cleared if they logged out? The request object is not available in this ...

Sphinx automodule not willing to import Translation

I have a sneaking suspicion that Sphinx doesn't like my class Translation. When I try to do an automethod: .. automethod:: translations.models.Translation.new I get this warning: /Users/dash/Projects/zamboni/zamboni/docs/topics/translations.rst:39: (WARNING/2) autodoc can't import/find method 'translations.models.Translation.new',...

Permission to access network drives when running Python CGI?

I have a Python script running on the default OSX webserver, stored in /Library/WebServer/CGI-Executables. That script spits out a list of files on a network drive using os.listdir. If I just execute this from the terminal, it works as expected, but when I try to access it through a browser (computer.local/cgi-bin/test.py), I get a perm...

python popen command.....wait until the command is finished

Hi, I have a script where I launch with popen a shell command. The problem is that the script don't wait that popen command is finished and go forward. om_points = os.popen(command, "w") ..... How can I tell to my python script to wait until the shell command has finished? Thanks. ...

Django: Save data from form in DB

I have a model: class Cost(models.Model): project = models.ForeignKey(Project) cost = models.FloatField() date = models.DateField() For the model I created a class form: class CostForm(ModelForm): class Meta: model = Cost fields = ['date', 'cost'] view.py: def cost(request, offset): if request.method ==...

Is there a better way of replacing duplicates in a list (Python)

Given a list: l1: ['a', 'b', 'c', 'a', 'a', 'b'] output: ['a', 'b', 'c', 'a_1', 'a_2', 'b_1' ] I created the following code to get the output. It's messyyy.. for index in range(len(l1)): counter = 1 list_of_duplicates_for_item = [dup_index for dup_index, item in enumerate(l1) if item == l1[index] and l1.count(l1[index]) > 1]...

What could cause xmlrpclib.ResponseError: ResponseError() ?

Hi, I am experimenting with XML-RPC. I have the following server script (python): from SimpleXMLRPCServer import SimpleXMLRPCServer server = SimpleXMLRPCServer(('localhost', 9000)) def return_input(someinput): return someinput server.register_function(return_input) try: print 'ctrl-c to stop server' server.serve_forever(...

Help with parsing lxml

Hi To implement a college project, I need to handle XML files. For this I choose lxml after doing some research. However I can't seem to find some nice tutorial to help me get started. I can't choose most specifically which type of parsing I need to use. My XML files don't have that much data but speed is main concern, not memory. Can...

Animate pygame sprite in elliptical path

This is pygame 1.9 on python 2.6.. Here is a screenshot of what is currently being drawn in my "game" to give some context. Here is the code. It's supposed to be the moon orbiting around the earth (I'm not trying to make a real simulation or anything, I'm just using the setting to play around and learn pygame). It's 2 circles, and the ...