python

Short rot13 function

Hi there! I am searching for an short and cool rot13 function in Python ;-) I've written this function: def rot13(s): chars = "abcdefghijklmnopqrstuvwxyz" trans = chars[13:]+chars[:13] rot_char = lambda c: trans[chars.find(c)] if chars.find(c)>-1 else c return ''.join( rot_char(c) for c in s ) Can anyone make it bett...

Manipulating strings in python - concentrating on part of a user's input

resp = raw_input("What is your favorite fruit?\n") if "I like" in resp: print "%s" - "I like" + " is a delicious fruit." % resp else: print "Bubbles and beans." OK I know this code doesn't work, and I know why. You can't subtract strings from each other like numbers. But is there a way to break apart a string and only u...

Dynamically Linking Python Extension (.pyd) to Another Extension

Python Extension modules are just dynamic libraries, so I assume it's possible to dynamically link a Python extension to another. The problem is on Windows Python Extensions are are given the .pyd extension instead of .dll, so I can't get distutils to link to them when I run the setup script. (I don't think this is a problem on UNIX beca...

Convert three column text file to matrix

Hi I'd like to convert a file that's tab delimited and looks like this: Species Date Data 1 Dec 3 2 Jan 4 2 Dec 6 2 Dec 3 to a matrix like this (species is the row header): 1 2 Dec 3 9 Jan 4 I'm guessing the part of the solution is to create a dictionary with two keys and use defaultdict to...

How to use os.spawnv to send email copy using Python?

First let me say that I know it's better to use the subprocess module, but I'm editing other people's code and I'm trying to make as few changes as possible, which includes avoiding the importing any new modules. So I'd like to stick to the currently-imported modules (os, sys, and paths) if at all possible. The code is currently (in a ...

Copy an app-engine entity

How can I copy an entity created from my Geo model: class Geo(db.Model): title = db.StringProperty() link = db.StringProperty() updated = db.DateTimeProperty(auto_now =True) author = db.ReferenceProperty(MyUser) id = db.StringProperty() entry = db.ListProperty(db.Key) ...

= Try Except Pattern?

I find this design pattern comes up a lot: try: year = int(request.GET['year']) except: year = 0 The try block can either fail because the key doesn't exist, or because it's not an int, but I don't really care. I just need a sane value in the end. Shouldn't there be a nicer way to do this? Or at least a way to do it on one line? Some...

[PyGTK] Saving gtk.TextTags to file?

So I am trying to write a rich text editor in PyGTK, and originally used the older, third party script InteractivePangoBuffer from Gourmet to do this. While it worked alright, there were still plenty of bugs with it which made it frustrating to use at times, so I decided to write my own utilizing text tags. I have got them displaying and...

function in python

In a function, I need to perform some logic that requires me to call a funtion inside a function. what i did with this , like : def dfs(problem): stack.push(bache) search(root) while stack.isEmpty() != 0: def search(vertex): closed.add(vertex) for index in sars: stack.push(index) retu...

Python circular references

Hi, trying to have two class that reference each others, in the same file. What would be the best way to have this working: class Foo(object): other = Bar class Bar(object): other = Foo if __name__ == '__main__': print 'all ok' ? The problem seems to be that since the property is on the class, since it tries to execute...

Getting isMultipartContent = false while using python poster library

Hi, I'm using the python poster library to try to upload a form containing including an image to a servlet. Locally, it runs fine, but when I deploy to app engine, it doesn't recognize it as multipart content. ServletFileUpload.isMultipartContent(request) returns false Here's how I'm using the poster library: register_openers() da...

Python: Problem with if statement

I have problem with a if statement code below: do_blast(x): test_empty = open('/home/rv/ncbi-blast-2.2.23+/db/job_ID/%s.blast' % (z), 'r') if test_empty.read() == '': test_empty.close() return 'FAIL_NO_RESULTS' else: do_something def return_blast(job_ID): if job_ID == 'FAIL_N...

loop until all elements have been accessed N times in python

I have a group of buckets, each with a certain number of items in them. I want to make combinations with one item from each bucket. The loop should keep making different combinations until each item has participated in at least some defined number. I can easily see how to run the loop and stop once a single element has been accessed a c...

Step into subroutine call, but not calls made for parameters

func(a(), b.c) When executing the line above in the pdb debugger, using step will actually step into a, and then into the getter for b.c if its atypical (such as being a property), before actually stepping into func. Generally I find myself using step followed by r to return from the frames I'm not interested in, and often inexplicabl...

How do I make Tkinter support PNG transparency?

I put in a partially transparent PNG image in Tkinter and all I get is this How do I make the dark triangle on the right clear? (like it's supposed to be) This is python 2.6 on Windows 7, btw. ...

Py3k libraries porting

I don't really know if this is a StackOverflow-type question or a Python-dev question. I'd like to host sprints at my place for converting python2.0 libraries to Python3 ; currently I am working on porting Distutils2. Is there any place I could find a graph/log of the libraries(dependencies) which need to be ported to Python3 ranked b...

Good compilers for compiling perl/python/php scripts into linux executables?

I am working on a project that requires reading text files, extracting data from them, and then generating reports (text files). Since there are a lot of string parsing, I decided to do it in Perl or Python or PHP (preference in that order). But I don't want to expose the source code to my client. Is there any good compiler for compiling...

dictionary in python

s = problem.getSuccessors(currNode) print s child = dict((t[0], t[1:]) for t in s) print child output of s = [((5, 4), 'South', 1), ((4, 5), 'West', 1)] output of child = {(4, 5): ('West', 1), (5, 4): ('South', 1)} Why the order has been changed?? 5,4 should be at first position and ( 4, 5) at 2nd position of child...

Strange logic with bool

Hello, I can't understand one thing with logic in python. Here is the code: maxCounter = 1500 localCounter = 0 while True: print str(localCounter) + ' >= ' + str(maxCounter) print localCounter >= maxCounter if localCounter >= maxCounter: break localCounter += 30 And the result output: ... 1440 >= 1500 False 1470 ...

How does Python compare string and int?

The following snippet is annotated with the output (as seen on ideone.com): print "100" < "2" # True print "5" > "9" # False print "100" < 2 # False print 100 < "2" # True print 5 > "9" # False print "5" > 9 # True Can someone explain why the output is as such? Implementation details ...