Re: [Twisted-Python] RPC design questions
I thought I can strictly stick to the advice (only keep one variant -- I thought I keep lambda), but today Python surprised me (which happens not so often). Check out these snippets: (1) def get(self, keys): for key in keys: self.call(keyvalue:get, key).addCallback(lambda value: self.show(key, value)) (2) def get(self, keys): for key in keys: self.call(keyvalue:get, key).addCallback(partial(self.show, key)) (3) def get(self, keys): for key in keys: self.call(keyvalue:get, key).addCallback(lambda value, key = key: self.show(key, value)) == (2) and (3) will output the same. Not (1). The reason is, that lambda establishes a closure over the reference key, which changes with loop iteration, and when the RPCs come back, that key will have the last value of the list iterated over. The result of (1) was not what I expected, and it took me some googling to find the answers. (3) is somewhat a hack on lambda to establish closures over the value when the closure is constructed, not the reference. (2) does that by default. I have some background in Erlang, where everything is immutable, and thus the issue is non. This is where the half-functional character of Python has bitten me badly. It may be nothing new to many, but it certainly was for me. http://blog.markfeeney.com/2009/11/python-lambda-partial-and-scopes.html http://stackoverflow.com/questions/938429/scope-of-python-lambda-functions-and-their-parameters http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/ == Anyway, perhaps it's still easier for users to grasp (3) than having another construct (2). What do you think? Von: twisted-python-boun...@twistedmatrix.com [mailto:twisted-python-boun...@twistedmatrix.com] Im Auftrag von Glyph Lefkowitz Gesendet: Dienstag, 23. August 2011 20:30 An: Twisted general discussion Betreff: Re: [Twisted-Python] RPC design questions On Aug 23, 2011, at 10:37 AM, Tobias Oberstein wrote: class AutobahnDeferred(Deferred): def call(self, *args): return self.addCallback(self.protocol.rcall, *args) Pro:most terse Con:only supports single callback no errback Con: subclassing in general is a bad idea. Subclassing Deferred is an even worse idea. What if Deferred one day gets a new method called 'call' that does something different? Your code would all break. Anything why I shouldn't do? Providing three different ways to do the same thing just so that you can use different syntax depending on your mood is a recipe for making your code hard to read. Pick one style, stick to it as much as possible - consistency is more important than the benefits of any one particular style :). (Except the subclassing one. Don't do that.) -glyph ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
[Twisted-Python] Weekly Bug Summary
Bug summary __ Summary for 2011-08-21 through 2011-08-28 Opened Closed Total Change Enhancements: 3 1746 +2 Defects: 3 3511 +0 Tasks: 1 0 75 +1 Regressions: 0 0 3 +0 Total: 7 4 1335 +3 |== Type Changes |== Priority Changes |== Component Changes |Defect: +0 |Normal: +2 |Conch:+1 |Enhancement: +2 |Low: +1 |Core: +0 |Task: +1 |Web: +2 |Website: +0 Total Tickets Open Tickets New / Reopened Bugs __ = Normal = [#5235] Expand point 3.13 of the FAQ (opened by mathias) taskcore http://twistedmatrix.com/trac/ticket/5235 [#5237] Add SFTP example (opened by thijs) enhancement conch http://twistedmatrix.com/trac/ticket/5237 [#5238] LimitTotalConnectionsFactory allows connections when limitConnections is 0 (opened by brutal_chaos) (CLOSED, invalid) defect core http://twistedmatrix.com/trac/ticket/5238 [#5239] bodyProducer docs for Agent#request should contain POST data example (opened by aljosa) enhancement webhttp://twistedmatrix.com/trac/ticket/5239 [#5240] Endpoints howto contains broken links (opened by teratorn) (CLOSED, duplicate) defect websitehttp://twistedmatrix.com/trac/ticket/5240 [#5241] twisted.conch: publickey auth not working against gerrit/Jsch server (opened by toidinamai) (CLOSED, fixed) defect conch http://twistedmatrix.com/trac/ticket/5241 = Low = [#5236] t.web.util.redirectTo should reject unicode URLs (opened by ivank) enhancement webhttp://twistedmatrix.com/trac/ticket/5236 Closed Bugs __ = Normal = [#5238] LimitTotalConnectionsFactory allows connections when limitConnections is 0 (opened by brutal_chaos, closed by brutal_chaos, invalid) defect core http://twistedmatrix.com/trac/ticket/5238 [#5240] Endpoints howto contains broken links (opened by teratorn, closed by exarkun, duplicate) defect websitehttp://twistedmatrix.com/trac/ticket/5240 [#2994] Revisit collections in AMP (opened by bigdog, closed by exarkun, invalid) enhancement core http://twistedmatrix.com/trac/ticket/2994 [#5241] twisted.conch: publickey auth not working against gerrit/Jsch server (opened by toidinamai, closed by exarkun, fixed) defect conch http://twistedmatrix.com/trac/ticket/5241 Ticket Lifetime Stats __ Oldest open ticket - [#50] conch command-line client doesn't work in win32 (since 2003-07-12 16:41:06). Newest open ticket - [#5239] bodyProducer docs for Agent#request should contain POST data example (since 2011-08-25 09:07:43). Mean open ticket age: 1081 days, 5:02:11.690375. Median: 953 days, 14:27:13.626705. Standard deviation: 770 days, 10:26:34.547629. Interquartile range: 1224 days, 22:34:15. Mean time between ticket creation and ticket resolution: 732 days, 23:39:59.832940. Median: 166 days, 18:53:39. Standard deviation is 946 days, 15:34:27.137065. The interquartile range is 1367 days, 15:02:22. Mean time spent in review: 86 days, 8:55:04.990501. Median: 4 days, 11:27:37. Standard deviation: 344 days, 6:34:57.955091. Interquartile range: 16 days, 11:12:58. Mean number of times a ticket is reviewed: 1.97905181918. Median: 1 Standard deviation: 1.58639543592. Interquartile range: 1. Contributor Stats __ In the last 4 weeks, 14 unique ticket reporters 6 unique ticket reviewers 2 unique ticket resolvers In the last 24 weeks, 86 unique ticket reporters 26 unique ticket reviewers 20 unique ticket resolvers In the last 48 weeks, 172 unique ticket reporters 31 unique ticket reviewers 24 unique ticket resolvers ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python