python

Rolling my own __repr__

I want to write my own __repr__ for some class that I define. I want it to be similar to the default <__main__.O object at 0x00D229D0>, except have a few other details in there. How do I reproduce that <__main__.O object at 0x00D229D0> thing? ...

Parsing XML - right scripting languages / packages for the job?

I know that any language is capable of parsing XML; I'm really just looking for advantages or drawbacks that you may have come across in your own experiences. Perl would be my standard go to here, but I'm open to suggestions. Thanks! UPDATE: I ended up going with XML::Simple which did a nice job, but I have one piece of advice if you ...

NumPy: Comparing Elements in Two Arrays

Anyone ever come up to this problem? Let's say you have two arrays like the following a = array([1,2,3,4,5,6]) b = array([1,4,5]) Is there a way to compare what elements in a exist in b? For example, c = a == b # Wishful example here print c array([1,4,5]) # Or even better array([True, False, False, True, True, False]) I'm trying t...

In python, how can I do a non-blocking system call?

In Python, is it possible to do a non-blocking system call without forking off a thread? i.e., can I avoid: import thread thread.start_new_thread(os.system,('cmd',)) ...

Accessing Facebook Connect FBML cookie via PyFacebook?

Is it possible, having logged in via a Facebook Connect FBML button, to retrieve and use the session details via PyFacebook? Can i use auth.getSession() in the same way as if I'd fired the login via Python? Basically I'm trying to work out if it's possible to replace the stages up to and including raw_input() in the example below: imp...

Python Debugging: code editing on the fly

Hi There I am new to python and haven't been able to find out whether this is possible or not. I am using the PyDev plugin under Eclipse, and basically all I want to find out is, is it possible to edit code whilst you're sitting at a breakpoint? I.e. Edit code whilst you're debugging. It allows me to do this at present, but it seems t...

How to make Python speak ?

How could I make Python say some text ? I could use Festival with subprocess but I won't be able to control it (or maybe in interactive mode, but it won't be clean) Is there a Python tts library ? Like an API for Festival, eSpeak, ... ? ...

How does `setup.py sdist` work?

I'm trying to make a source distribution of my project with setup.py sdist. I already have a functioning setup.py that I can install with. But when I do the sdist, all I get is another my_project folder inside my my_project folder, a MANIFEST file I have no interest in, and a zip file which contains two text files, and not my project. W...

How do I add text describing the code into a Python source file?

When writing code in Python, how can you write something next to it that explains what the code is doing, but which doesn't affect the code? ...

In Python, how to I convert all items in a list to floats?

I have a script which reads a text file, and pulls decimal numbers out of it as strings, and places them into a list. So I have this list: ['0.49', '0.54', '0.54', '0.54', '0.54', '0.54', '0.55', '0.54', '0.54', '0.54', '0.55', '0.55', '0.55', '0.54', '0.55', '0.55', '0.54', '0.55', '0.55', '0.54'] How do I convert each value in the li...

Dice Roller using Tkinter

Thank you to everybody who helped answer my last question: My friend took the code, and has attempted to use Tkinter to make a box that we could use to make things nicer-looking, but he has been unable to integrate the dice roller from the last question and the Tkinter. Any help or ideas in getting the dice roller into the code below w...

How does indexing a list with a tuple work?

I am learning Python and came across this example: W = ((0,1,2),(3,4,5),(0,4,8),(2,4,6)) b = ['a','b','c','d','e','f','g','h','i'] for row in W: print b[row[0]], b[row[1]], b[row[2]] which prints: a b c d e f a e i c e g I am trying to figure out why! I get that for example the first time thru the expanded version is: print...

ImportError: No module named Foundation

I am trying to follow the instructions for the accepted answer to "PyObjC development with Xcode 3.2". I will repost them here since I don't have enough rep to comment on the actual question: Here's what I have done to get PyObjC working in Snow Leopard: Using the Finder, I went to Go > Connect to Server... and connected to http://s...

Copying values from a dictionary into an object in Python

I have a dictionary that I would like to iterate through and copy the key-value pairs into an object in Python. The dictionary is POST, and the object is a Model (in Django, perhaps Django has better way to do this). In PHP, I'd be able to use variable assignments: foreach($post as $key => $value) { $my_model->$key = $value; } A...

How do I do 'from foo import *' using Python's __import__ function

I want to replace settings.py in my Django system with a module implemented as a directory settings containing __init__.py. This will try to import a module named after the server, thus allowing for per-server settings. If I don't know the name of a module before I import it then I can't use the import keyword but must instead use the _...

Running Python from the Windows Command Line

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time? ...

Running commands from a file in PDB

I would like to run a set of python commands from a file in the PDB debugger. Related to this, can I set up a file that is automatically run when PDB starts? ...

Python strategy for extracting text from malformed html pages

I'm trying to extract text from arbitrary html pages. Some of the pages (which I have no control over) have malformed html or scripts which make this difficult. Also I'm on a shared hosting environment, so I can install any python lib, but I can't just install anything I want on the server. pyparsing and html2text.py also did not seem ...

Python POST XML not executed

The Headers post fine but the associated XML seems to be taken as string data only, XML is not processed. XML string is of the form: params = '''<?xml version="1.0" encoding"="UTF-8 "?> <MainRequest> <requestEnvelope><errorLanguage>en_US</errorLanguage> </requestEnvelope></MainRequest>''' The POST is of the form: enc_params = urllib....

What is causing "unbound method __init__() must be called with instance as first argument" from this Python code?

I have this class: from threading import Thread import time class Timer(Thread): def __init__(self, interval, function, *args, **kwargs): Thread.__init__() self.interval = interval self.function = function self.args = args self.kwargs = kwargs self.start() def run(self): ...