![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Useful code snippets
Here's a few Python code snippets I've used in several programs, that might, possibly, maybe, be useful for others. Does anyone else have any pieces of code that might make life easier for other Python programmers?
def all(s):
"Return true if all elements in an iterable sequence are true."
for x in s:
if not x:
return False
return True
def any(s):
"Return true if any element in an iterable sequence is true."
for x in s:
if x:
return True
return False
class Cache:
"""
Creates a functor that caches the output of a function.
e.g.
image_load = Cache(pygame.image.load)
ball1 = image_load("ball.png")
ball2 = image_load("ball.png") # ball.png only loaded from disk once
"""
def __init__(self, func):
self.store = {}
self.func = func
def __call__(self, key):
try:
return self.store[key]
except KeyError:
self.store[key] = self.func(key)
return self.store[key] |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,785
Rep Power: 5
![]() |
Your all/any function could be easily generalised and used in more contexts like so:
def get_instances(list, instance):
i = 0
for item in list: i += item==instance
return i
print get_instances( [0, 1, False, True], True )As well, that "any" function can be summarized in a simple condition: if True in list: print "At least one true is in the list." Nice Cache function though.
__________________
Waterloo's Canadian Computing Competition (CCC) - Stage 2 Problems, Solutions, and Test Data Last edited by Sane; Nov 25th, 2005 at 3:53 PM. |
|
|
|
|
|
#3 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
assert not any(x > 5 for x in s): assert all(x <= 5 for x in s) Quote:
Still, (True in (x > 5 for x in s)) doesn't seem as intuitive as any(x > 5 for x in s). def any(s): return True in s def all(s): return not False in s Last edited by Arevos; Nov 25th, 2005 at 4:36 PM. |
||
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
A general-purpose event queue that I sometimes use:
class EventQueue:
"""
Contains a queue of events. When polled, the event queue calls the
functions associated with the events' classes.
e.g.
class Event:
pass
queue = EventQueue()
@queue.register(Event)
def foobar(event):
print event
queue.push(Event())
queue.poll()
"""
def __init__(self):
self.map = {}
self.queue = []
def register(self, event_type, function = None):
"""
Use as a function: register(event_type, function)
or as a decorator: @register(event_type)
"""
def decorate(func):
try:
self.map[event_type].add(func)
except KeyError:
self.map[event_type] = set([func])
return func
if function != None:
return decorate(function)
return decorate
def push(self, event):
"Push an event onto the queue."
self.queue.append(event)
def poll(self):
"Poll the queue."
events = self.queue[:]
self.queue = []
for event in events:
for func in self.map[event.__class__]:
func(event) |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,785
Rep Power: 5
![]() |
I've noticed I've used this one quite often in my code:
def get_between(string, start, end):
return string.split(start)[-1].split(end)[0]example: print get_between("blah <a href='http://www.google.com'>blah</a> blah",
"href='", "'>")
>>> http://www.google.com |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,785
Rep Power: 5
![]() |
This snippet is useful for about every important program that you'd need to distribute to the public, for when needs to be kept updated (and is potentially full of bugs):
def show_error(caption, fileName):
from tkMessageBox import showwarning # error dialog box
from sys import exc_info, exit # exception info, system exit
from time import ctime # current time and day
error = '\n'.join(str(i) for i in exc_info()) # change all elements of exc_info to str, then join with newline
stream = open(fileName, 'a') # open stream with append
stream.write("%s: %s\n\n"%( ctime(), error )) # append error and time to an error log
stream.close() # close stream
showwarning( caption, error ) # show error
exit() # exit programdef main():
print "This is our main function"
print "Oops, this line is an error: %s"%(x)
try:
main()
except SystemExit: pass
except:
show_error('Program Error', 'errors.log')And if your program is not using tkinter, then you just replace the 'showwarning' with a call to win32api.MessageBox(0, message, caption)
__________________
Waterloo's Canadian Computing Competition (CCC) - Stage 2 Problems, Solutions, and Test Data Last edited by Sane; Nov 27th, 2005 at 5:00 PM. |
|
|
|
|
|
#7 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
On the debug topic, I had a generic debug function that worked some magic to find the name of the function that just called it and other interesting things. Will look around to see if I have it still. |
|
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,785
Rep Power: 5
![]() |
Here's a semi-useful function I created when I ran into a problem while parsing html.
def splitwithout(text, s, void):
res = []
lt = -1
on = (False,'')
for t in range(len(text)):
if text[t] in void:
if not on[0]:
on = (True, text[t])
elif text[t] == on[1]:
on = (False, '')
if text[t] == s and not on[0]:
res += [text[lt+1:t]]
lt = t
res += [text[lt+1:]]
return resIt does what its name says, splits without (the instance of the items passed through void). e.g. print splitwithout("""a b "c d" e f 'g h i' j k l""",
" ",
('"',"'"))>>> ['a', 'b', '"c d"', 'e', 'f', "'g h i'", 'j', 'k', 'l'] Notice how the spaces in between "c d" and 'g h i' were not split as split() would normally do (since I passed '"', "'" through void)? The point of this function is so I can do split an html tag by its spacing, without taking a space that could be inside the paramaters as well. I wonder if this is actually faster then split() is. ![]() Edit: If you still don't see the point, here's what I was using it for. def parse_tag(text, open = '<', close = '>'):
first = splitwithout(text.split(open)[1].split(close)[0], ' ', ('"',"'"))
tag = first[0]
if tag == ' ':
return False
params = {}
for p in first[1:]:
pp = p.split('=')
try:
param = pp[0]
val = pp[1].split(pp[1][0])[1]
except IndexError: return False
params[pp[0]] = val
try:
content = text.split(open+"/"+tag+close)[0].split(close)[1]
except IndexError:
return False
return tag, params, content
tag, params, content = parse_tag("""<a href='http://google.ca' title="Hyperlink to Google.ca">Hyperlink</a>""")
print tag #'a'
print params #{'href': 'http://google.ca', 'title': 'Hyperlink to Google.ca'}
print content #HyperlinkLast edited by Sane; Mar 9th, 2006 at 5:17 PM. |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
(the documentation for it is fairly awful, though, so you'd have to google for examples of how to use it) --OH. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|