python

Python list.index question

Why does list.index throws an exception instead of returning a value like say -1? What's the idea behind this? To me it looks cleaner to deal with special values, etc than exceptions. EDIT: I didn't realize -1 to be usable, but in that case, how about a value of None? ...

Python's use of __new__ and __init__ ?

I'm just trying to streamline one of my classes and have introduced some functionality in the same style as the flyweight design pattern. However, I'm a bit confused as to why __init__ is always called after __new__. I wasn't expecting this. Can anyone tell me why this is happening and how I implement this functionality otherwise? (apar...

problems Wrapping Patricia Tries using Swig, python

Hello all! I'm trying to wrap the Patricia Tries (Perl's NET::Patricia) to be exposed in python. I am having difficulty with one of the classes. So instances the patricia node (below) as viewed from python have a "data" property. Reading it goes fine, but writing to it breaks. typedef struct _patricia_node_t { u_int bit; /* ...

How do I iterate over a Python dictionary, ordered by values?

I've got a dictionary like: { 'a': 6, 'b': 1, 'c': 2 } I'd like to iterate over it by value, not by key. In other words: (b, 1) (c, 2) (a, 6) What's the most straightforward way? ...

How can I convert a Python dictionary to a list of tuples?

If I have a dictionary like: { 'a': 1, 'b': 2, 'c': 3 } How can I convert it to this? [ ('a', 1), ('b', 2), ('c', 3) ] And how can I convert it to this? [ (1, 'a'), (2, 'b'), (3, 'c') ] ...

Python, __init__ and self confusion

Alright, so I was taking a look at some source when I came across this: >>> def __parse(self, filename): ... "parse ID3v1.0 tags from MP3 file" ... self.clear() ... try: ... fsock = open(filename, "rb", 0) ... try: ... fsock.seek(-128, 2) ... tagdata = fsock...

In Python 2.6, How Might You Pass a List Object to a Method Which Expects A List of Arguments?

I have a list full of various bits of information that I would like to pass to several strings for inclusion via the new string format method. As a toy example, let us define thelist = ['a', 'b', 'c'] I would like to do a print statement like print '{0} {2}'.format(thelist) and print '{1} {2}'.format(thelist) When I run this, I rece...

benchmarking PHP vs Pylons

I want to benchmark PHP vs Pylons. I want my comparison of both to be as even as possible, so here is what I came up with: PHP 5.1.6 with APC, using a smarty template connecting to a MySQL database Python 2.6.1, using Pylons with a mako template connecting the the same MySQL database Is there anything that I should change in that set...

Examples for string find in Python

I am trying to find some examples but no luck. Anyone knows some examples on the net? I want to know what it returns when it can't find, and how to specify from start to end, which I guess is gonna be 0, -1. ...

OCR for sheet music

Im considering doing a small project as a part of my masters for doing ocr just for sheetmusic instead of text. I think PIL and Python would be fine for simple proof of concept O"notes"R. My question is: Has anyone got any "dont do it with PIL use xyz instead" or something in that alley? EDIT: My delicius links regarding the subject i...

TLS connection with timeouts (and a few other difficulties)

I have a HTTP client in Python which needs to use TLS. I need not only to make encrypted connections but also to retrieve info from the remote machine, such as the certificate issuer. I need to make connection to many HTTP servers, often badly behaved, so I absolutely need to have a timeout. With non-TLS connections, mysocket.settimeout(...

python class __getitem__ negative index handling

Is there a common function to do this? Right now I'm just doing the following (and overriding __len__) idx < 0: idx = len(self) + idx if idx < 0 or idx >= len(self): raise IndexError, "array index (%d) out of range [0, %d)" %(idx, len(self)) ...

Is it possible to implement properties in languages other than C#?

During a bout of C# and WPF recently, I got to like C#'s properties: public double length_inches { get { return length_metres * 39.0; } set { length_metres = value/39.0; } } Noticing, of course, that length_metres may change from being a field to a property, and the code need not care. WPF can also bind UI elements to object p...

Tab-completion in Python interpreter in OS X Terminal

Several months ago, I wrote a blog post detailing how to achieve tab-completion in the standard Python interactive interpreter--a feature I once thought only available in IPython. I've found it tremendously handy given that I sometimes have to switch to the standard interpreter due to IPython unicode issues. Recently I've done some work...

Comment out a python code block

Is there any mechanism to comment out large blocks of Python code? Right now the only ways I can see of commenting out code are to either start every line with a #, or to enclose the code in """ (triple quotes), except that actually makes it show up in various doc tools. Edit--After reading the answers (and referring to the "duplicate")...

learning python 3.0 on ubuntu

[resolved] I tweaked the preferences in komodo edit and ended up with: don't auto indent don't allow file contents to override tab settings prefer tab characters over spaces 4 spaces per indent 4 width of each tab char I also set komodo to show whitespace and tabs, which eneded up revealing the screwed up sections. yeah - it's a ...

Create static graphics files (png, gif, jpg) using Ruby or Python

I'd like to create a graphic image on the fly based on user input, and then present that image as a PNG file (or jpg or gif if necessary, but PNG is preferred). This is actually for an astrology application; what I'd like to do is generate the chart in PNG for display. Python or Ruby is fine; in fact, the library available may determin...

Full examples of using Pyserial package

Can someone please show me a full python sample code that uses pyserial, i have the package and am wondering how to send the AT commands and read them back! H-e-l-p ! ...

python syntax newbie question

A equation takes values in the following form : x = [0x02,0x00] # which is later internally converted to in the called function to 0x300 y = [0x01, 0xFF] z = [0x01, 0x0F] How do I generate a series of test values for this function ? for instance I want to send a 100 odd values from a for loop for i in range(0,300): ...

Python and text manipulation

Hi, I want to learn a text manipulation language and I have zeroed in on Python. Apart from text manipulation Python is also used for numerical applications, machine learning, AI, etc. My question is how do I approach the learning of Python language so that I am quickly able to write sophisticated text manipulation utilities. Apart from...