python

How do you programmatically set an attribute in Python?

Suppose I have a python object x and a string s, how do I set the attribute s on x? So: >>> x = SomeObject() >>> attr = 'myAttr' >>> # magic goes here >>> x.myAttr 'magic' What's the magic? The goal of this, incidentally, is to cache calls to x.__getattr__(). ...

With what kind of IDE (if any) you build python GUI projects?

Is there any IDE (like VS) with drag and drop support for building python GUI, connecting to dbs etc? Eventhough I am an emacs guy, I find it much easier to create GUI with VS. ...

What Python tools can I use to interface with a website's API?

Let's say I wanted to make a python script interface with a site like Twitter. What would I use to do that? I'm used to using curl/wget from bash, but Python seems to be much nicer to use. What's the equivalent? (This isn't Python run from a webserver, but run locally via the command line) ...

exit codes in python

I got a message saying "script xyz.py returned exit code 0". What does this mean? What do the exit codes in python mean? How many are there? How many are important? ...

Python module to extract probable dates from strings?

I'm looking for a Python module that would take an arbitrary block of text, search it for something that looks like a date string, and build a DateTime object out of it. Something like Date::Extract in Perl Thank you in advance. ...

How to access a Python global variable from C?

I have some global variables in a Python script. Some functions in that script call into C - is it possible to set one of those variables while in C and if so, how? I appreciate that this isn't a very nice design in the first place, but I need to make a small change to existing code, I don't want to embark on major refactoring of existi...

WX Python and Raw Input on Windows (WM_INPUT)

Does anyone know how to use the Raw Input facility on Windows from a WX Python application? What I need to do is be able to differentiate the input from multiple keyboards. So if there is another way to achieving that, that would work too. ...

Using Regex Plus Function in Python to Encode and Substitute

I'm trying to substitute something in a string in python and am having some trouble. Here's what I'd like to do. For a given comment in my posting: "here are some great sites that i will do cool things with! http://stackoverflow.com/it's a pig & http://google.com" I'd like to use python to make the strings like this: "here are some ...

Decomposing HTML to link text and target

Given an HTML link like <a href="urltxt" class="someclass" close="true">texttxt</a> how can I isolate the url and the text? Updates I'm using Beautiful Soup, and am unable to figure out how to do that. I did soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url)) links = soup.findAll('a') for link in links: print "link co...

Parse HTML via XPath

In .Net, I found this great library, HtmlAgilityPack that allows you to easily parse non-well-formed HTML using XPath. I've used this for a couple years in my .Net sites, but I've had to settle for more painful libraries for my Python, Ruby and other projects. Is anyone aware of similar libraries for other languages? ...

python - decimal place issues with floats

I seem to be losing a lot of precision with floats. For example I need to solve a matrix: 4.0x -2.0y 1.0z =11.0 1.0x +5.0y -3.0z =-6.0 2.0x +2.0y +5.0z =7.0 this is the code i use to import the matrix from a text file: f = open('gauss.dat') lines = f.readlines() f.close() j=0 for line in lines: bits = string.split(line, ',') ...

How to split a web address

So I'm using python to do some parsing of web pages and I want to split the full web address into two parts. Say I have the address http://www.stackoverflow.com/questions/ask. I would need the protocol and domain (e.g. http://www.stackoverflow.com) and the path (e.g. /questions/ask). I figured this might be solved by some regex, however ...

Accounting for a changing path

In relation to another question, how do you account for paths that may change? For example, if a program is calling a file in the same directory as the program, you can simply use the path ".\foo.py" in *nix. However, apparently Windows likes to have the path hard-coded, e.g. "C:\Python_project\foo.py". What happens if the path changes?...

What's the best way to transfer data from python to another application in windows?

I'm developing an application with a team in .Net (C++) and provide a COM interface to interact with python and other languages. What we've found is that pushing data through COM turns out to be pretty slow. I've considered several alternatives: dumping data to a file and sending the file path through com Shared Memory via mmap? Stre...

Any AOP support library for Python ?

I am trying to use some AOP in my Python programming, but I do not have any experience of the various libs that exists. So my question is : What AOP support exists for Python, and what are the advantages of the differents libraries between them ? Edit : I've found some, but I don't know how they compare : Aspyct Lightweight AOP for P...

What does *args and **kwargs mean?

What exactly does *args and **kwargs mean? According to the Python documentation, from what it seems, it passes in a tuple of arguments. def foo(hello, *args): print hello for each in args: print each if __name__ == '__main__': foo("LOVE", ["lol", "lololol"]) This Prints out: LOVE ['lol', 'lololol'] How do you effect...

Python program start

Should I start a Python program with: if__name__ == '__main__': some code... And if so, why? I saw it many times but don't have a clue about it. ...

Tix documentation for Python

I've recently starting playing around with Tix in Python, and I'm distressed at the lack of Python documentation for it online. Both the tutorial and book have plenty of code examples in Tcl and none in Python. Googling has turned up no good Python docs. What do Tix users do for their Python documentation? Do they just learn to read ...

Parsing C++ preprocessor #if statements

I have a C/C++ source file with conditional compilation. Before I ship it to customers I want to remove most of the #if statements, so that my customers do not need to worry about passing the right -D options to the compiler. I have this implemented and working in Python, but it only handles #ifdef and #ifndef statements properly. I n...

Python's ConfigParser unique keys per section

I read the part of the docs and saw that the ConfigParser returns a list of key/value pairs for the options within a section. I figured that keys did not need to be unique within a section, otherwise the parser would just return a mapping. I designed my config file schema around this assumption, then sadly realized that this is not the c...