Re: [Tutor] Really miss the C preprocessor!!
On Sat, 6 Mar 2010 07:17:43 pm you wrote: > I thought about suggesting using decorators for this, since I've done > something similar (not exactly exponential backoff, but retrying a > few times on exception). However, as I started writing the example, I > got stuck at expressing a generic way to pass the exception and > numretries. I was just wondering is there some way to do this sort of > thing with decorators ?? [...] > is there a way to decorate a function alongwith additional parameters > which will be passed to the function ? Yes, you need a decorator factory -- a function which returns a decorator. def factory(exception, numretries): def decorator(func): @functools.wraps(func) def inner(*args, **kwargs): t = 1 for i in range(numretries): try: return func(*args, **kwargs) except exception: time.sleep(t) t *= 2 msg = "no connection after %d attempts" % numretries raise exception(msg) return inner return decorator @factory(HTTPError, 8) def check_some_website(url, x): ... I haven't tested the above code, but it should do the trick. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Instantiating a list of strings into a list of classes
On Fri, 5 Mar 2010 17:38:08 -0800 Daryl V wrote: > I have a csv list of data, of the form: > plot, utmN83_X, utmN83_Y, plot_radius_m > Spring1,348545,3589235,13.2 > etc. [...] > What I want to do is use the first entry in that row (row[0]) as the > variable name for the instantiated class. There are several solution, for design & implementation. (1) You can do some trick to create vars as you explain. But it's ugly and in my opinion wrong: because the set of data build a kind of whole, thus should have global name, say "data". (2) Build a dictionary which keys are the names: name = row[0] data[name] = value ... v = data[name] This is much better because its standard programming and the data are properly packed. Right? (3) Build a custom composite object which attributes are named the way you want. Python does not have a syntax to set attributes with variable names, but provides a builtin func for this: name = row[0] setattr(data, name, value) ... v = data.name It may look a bit contorsed, but again it's because python does not have a syntax for this. I would go for the latter -- it may be a question of style. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding
2010/3/5 Dave Angel I'm not angry, and I'm sorry if I seemed angry. Tone of voice is hard to > convey in a text message. Ok, sorry. I've misunderstood your mail :D > I'm still not sure whether your confusion is to what the rules are, or why > the rules were made that way. WHY the rules are made that way. But now it's clear. 2010/3/6 Mark Tolonen > > > Maybe this will help: > > # coding: utf-8 > > a = "ciao è ciao" > b = u"ciao è ciao".encode('latin-1') > > a is a UTF-8 string, due to #coding line in source. > b is a latin-1 string, due to explicit encoding. > Oh, right. And, if i'm not wrong B is an UTF8 string decoded to unicode (due to the coding: statement at the top of the file) and re-encoded to latin1 > -Mark Thankyou again Giorgio -- -- AnotherNetFellow Email: anothernetfel...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
On Fri, Mar 5, 2010 at 10:48 PM, Hugo Arts wrote: > > Except the OP requested pairs (1, 2), (3, 4), i.e. with no duplicate > elements. Here is a generator that does what you need: > > def pairs(seq): > it = iter(seq) > try: > while True: > yield it.next(), it.next() > except StopIteration: > return > > Hugo > Just Noticed this tiny caveat: If the length of the sequence is uneven, the last element will not be yielded by this generator. Whether that's a problem, and if it is, how to handle it, depends on the application you're working with. I did make a tiny modification to yield the last element in a pair with None as the second value. The benefit is that it doesn't skip any values, but you'll need to handle the possible None value in your code. Of course, if you want a generator that yields consecutive pairs, passing it a sequence of uneven length is sort of problematic to begin with. So for most applications the simple version should be fine. def pairs(seq): it = iter(seq) try: while True: a = it.next() b = it.next() yield a, b except StopIteration: if len(seq) % 2: yield a, None It's not as pretty as the simple version, unfortunately. in my experience, corner cases are rarely handleable by elegant code :( Compare output of the first and second version: >>> # first (elegant) version of pairs >>> list(pairs(range(5))) [(0, 1), (2, 3)] >>> # second version >>> list(pairs_2(range(5))) [(0, 1), (2, 3), (4, None)] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Instantiating a list of strings into a list of classes
"Daryl V" wrote What I want to do is use the first entry in that row (row[0]) as the variable name for the instantiated class. Thats usually a very bad idea. Not least because all the code that comes after it would somehow, magically, have to know about this brand new variable that has appeared. Its generally better to store these kinds of things in a list or dictionary. You can use the name as the key into the dictionary if you like. Then you can process the dictionary like any other collection of objects. Or you can refer to specific ones using the name. And the previously written code doen't get confused aboutt unexpected names appearing. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Process list elements as consecutive pairs
Rüdiger Wolf wrote: > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 >>> listin = [1,2,3,4,5,6,7,8,9,10] >>> it = iter(listin) >>> zip(it, it) [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)] If listin as an odd length the last item will be lost. Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor