python

"Pythonic" equivalent for handling switch and multiple string compares

Alright, so my title sucked. An example works better: input = 'check yahoo.com' I want to parse input, using the first word as the "command", and the rest of the string as a parameter. Here's the simple version of how my non-Pythonic mind is coding it: if len(input) > 0: a = input.split(' ') if a[0] == 'check': if l...

Can 3D OpenGL game written in Python look good and run fast?

I am planning to write an simple 3d(isometric view) game in Java using jMonkeyEngine - nothing to fancy, I just want to learn something about OpenGL and writing efficient algorithms (random map generating ones). When I was planning what to do, I started wondering about switching to Python. I know that Python didn't come into existence ...

Highlighting trailing whitespace in Textmate for Python?

I would like to do something like this Textmate tip, so that trailing whitespace are always highlighted in some way when I code something in Python - it makes it easier to correct it immediately and other editors such as Emacs can do it. Unfortunately the discussion after that post seems to suggest it's difficult to do. For me the inval...

Rename files, Python/Jython

I have a directory full of files, some which have an ampersand in their names. I would like to rename all the files with ampersands and replace each ampersand with a plus (+). I am working with around 10k files. What would be the best method to do this? ...

How to convert strings into integers in python?

Hello there, I have a tuple of tuples from MySQL query like this: T1 = (('13', '17', '18', '21', '32'), ('07', '11', '13', '14', '28'), ('01', '05', '06', '08', '15', '16')) I'd like to convert all the string elements into integers and put it back nicely to list of lists this time: T2 = [[13, 17, 18, 21, 32], [7, 11, 13,...

How do I format positional argument help using Python's optparse?

As mentioned in the docs the optparse.OptionParser uses an IndentedHelpFormatter to output the formatted option help, for which which I found some API documentation. I want to display a similarly formatted help text for the required, positional arguments in the usage text. Is there an adapter or a simple usage pattern that can be used f...

Is it possible to replace a function/method decorator at runtime? [ python ]

If I have a function : @aDecorator def myfunc1(): # do something here if __name__ = "__main__": # this will call the function and will use the decorator @aDecorator myfunc1() # now I want the @aDecorator to be replaced with the decorator @otherDecorator # so that when this code executes, the function no longer goes through ...

Python - Intersection of two lists

Hi, I know how to get an intersection of two flat lists: b1 = [1,2,3,4,5,9,11,15] b2 = [4,5,6,7,8] b3 = [val for val in b1 if val in b2] or def intersect(a, b): return list(set(a) & set(b)) print intersect(b1, b2) But when I have to find intersection for nested lists then my problems starts: c1 = [1, 6, 7, 10, 13, 28, 32, ...

WinXP button-style with wxPython

Hi, guys. I noticed that my programs written with wxPython have Win98 button style. But Boa Constructor (that is written using wxPython too) got pretty buttons. How to make buttons look like current Windows buttons style? ...

Python script at Visual C++ 2005 build step not spawning other processes.

I have the following post-build step in a VC++ 2005 project that calls a Python 2.5.1 script: postbuild.py postbuild.py does: import os os.system('cd') # cd is just a test, could be anything The process never starts, and it's the same with any other process I try, even using subprocess.call or Popen instead of os.system. Does anyo...

regular expression help with converting exp1^exp2 to pow(exp1, exp2)

I am converting some matlab code to C, currently I have some lines that have powers using the ^, which is rather easy to do with something along the lines \(?(\w*)\)?\^\(?(\w*)\)? works fine for converting (glambda)^(galpha),using the sub routine in python pattern.sub(pow(\g<1>,\g<2>),'(glambda)^(galpha)') My problem comes with nested ...

How can I auto-fill a paragraph in Eclipse?

I would like to auto-fill a paragraph to 80 characters (or some other fixed width) in Eclipse. Is this possible via a keyboard command like in Emacs? Or is there maybe a plugin (I did not find anything on google)? Edit: I am not sure if this is relevant, but I need this for docstrings in Python code (using the PyDev plugin). ...

Anyone use Pyjamas (pyjs) python to javascript compiler (like GWT..)

Has any one used this? I don't have a large background in Javascript and this lib looks like it may speed things along. www.pyjs.org ...

How can I use numpy.correlate to do autocorrelation?

Hi, I need to do auto-correlation of a set of numbers, which as I understand it is just the correlation of the set with itself. I've tried it using numpy's correlate function, but I don't believe the result, as it almost always gives a vector where the first number is not the largest, as it ought to be. So, this question is really tw...

Does python's pip support http authentication?

As the title says, does pip support http authentication, like easy_install does? If not, are there any (better) alternatives to running a private package repository? I see pip can access source repositories (git,svn etc.), but can version requirements be used with this? ...

Python - list transformation

Hello, Does anyone knows what magic I have to use to change x list: x = [1,2,3,4,5,11] into y list? y = ['01','02','03','04','05','11'] Thank you all in advance for helping me... ...

Using "like" in a cursor/query with a parameter in python (django)

Hello, I know this may be something stupid but I decided to ask any way. I've been trying to query something like: cursor.execute("select col1, col2 \ from my_tablem \ where afield like '%%s%' and secondfield = %s order by 1 desc " % (var1, var2) ) Bu...

How do I rewrite $x = $hash{blah} || 'default' in Python?

How do I pull an item out of a Python dictionary without triggering a KeyError? In Perl, I would do: $x = $hash{blah} || 'default' What's the equivalent Python? ...

What's the best way to replace the ternary operator in Python?

If I have some code like: x = foo ? 1 : 2 How should I translate it to Python? Can I do this? if foo: x = 1 else: x = 2 Will x still be in scope outside the if / then blocks? Or do I have to do something like this? x = None if foo: x = 1 else: x = 2 ...

Concurrency implications of EAFP/LBYL

When writing concurrent/multithreaded code in Python, is it especially important to follow the "Easier to Ask for Forgiveness than Permission" (EAFP) idiom, rather than "Look Before You Leap" (LBYL)? Python's exceptionally dynamic nature means that almost anything (e.g., attribute removal) can happen between looking and leaping---if so,...