embedded scripts debugging
Hi all. I have custom resource editor and wish python to be scripting language in it. But I don't want to lose ability of debugging which I currently have implementing all logic in C++. So the question is: Is there suitable library for simple python gui debugger, or may be there are some other techniques for debugging embedded scripts? -- http://mail.python.org/mailman/listinfo/python-list
python3: 'where' keyword
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable sources. Usage could be something like: >>> res = [ f(i) for i in objects ] where: >>> def f(x): >>> #do something or >>> print words[3], words[5] where: >>> words = input.split() - defining variables in "where" block would restrict their visibility to one expression - it's more easy to read sources when you know which part you can skip, compare to >>> def f(x): >>> #do something >>> res = [ f(i) for i in objects ] in this case you read definition of "f" before you know something about it usage. -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Bengt Richter wrote: It also allows the necessary but uninteresting setup for an expression to be moved "out of the way", bringing the expression that does the real work to prominence. Killer app for this keyword: class C(object): x = property(get, set) where: def get(self): return "Silly property" def set(self, val): self.x = "Told you it was silly" Yes, that is cool and it _is_ an interesting idea. Are suites nestable? E.g., is this legal? ... And, is the whole thing after the '=' an expression? E.g., x = ( foo(x) where: x = math.pi/4.0 ) where: def foo(x): print 'just for illustration', x or is this legal? for y in ([foo(x) for x in bar] where: bar = xrange(5) ): baz(y) where: def baz(arg): return arg*2 Not trying to sabotage the idea, really, just looking for clarification ;-) yes, all your examples are correct. And that's the way I'd like to use this feature. -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Nick Coghlan wrote: It also allows the necessary but uninteresting setup for an expression to be moved "out of the way", bringing the expression that does the real work to prominence. Killer app for this keyword: class C(object): x = property(get, set) where: def get(self): return "Silly property" def set(self, val): self.x = "Told you it was silly" oh, that's great! I can't imagine prettier example -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: accessing the result of 'if'
Carl Banks wrote: As a compromise, howabout: . if m > 20 where m=something(): . do_something_with(m) That's good, but first idea was about 'where' block that contains any expressions, that we need, for example function definition. the syntax you proposed has same problems as 'lambda'. The main problem here (as some would see it) is that you can't do something this: . if m > 20 where (def m(): a(); b()): exactly -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Peter Hansen wrote: >>> print words[3], words[5] where: >>> words = input.split() - defining variables in "where" block would restrict their visibility to one expression Then your example above doesn't work... print takes a sequence of expressions, not a tuple as you seem to think. sorry, I used "expression" carelessly. I mean that >>> print words[3], words[5] is a single expression (and that would be in Python 3, when print would be subtituted with write()/writeln()). -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Paul Rubin wrote: You mean I can't say # compute sqrt(2) + sqrt(3) x = (sqrt(a) where: a = 2.) \ + sqrt (a) where: a = 3. No, I'd prefer to not embed 'where' into expression. -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Nick Coghlan wrote: Current: assignment_stmt ::= (target_list "=")+ expression_list augmented_assignment_stmt ::=target augop expression_list New: assignment_stmt ::= (target_list "=")+ expression_list [where_clause] augmented_assignment_stmt ::=target augop expression_list [where_clause] where_clause ::= "where" ":" suite So the expressions in existing compound statements (for, while, if, elif) would be out of luck. You could conceivably add the 'where' clause to the end of those as well, to give statement local variables that apply to the whole compound statement: Nick, you're good at formalization, thanks again. So it seems that people loved the idea of 'where' keyword, may be it's time to think about PEP draft? I appreciate any help (cause my english is not that good =)). -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Nick Coghlan wrote: sorry, I used "expression" carelessly. I mean that >>> print words[3], words[5] is a single expression (and that would be in Python 3, when print would be subtituted with write()/writeln()). 'statement' is the appropriate word in Python's grammar. thanks ) And I don't think we'd actually have to wait for Python 3 for this, we'd just have to do the __future__ dance in order to introduce the new keyword. resonable, I mentioned Python3 as something in not that near future, that'd be great to think of it earlier -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Paul Rubin wrote: What would be the advantage of that over this? . x = sqrt(a) + sqrt(b) where: . a = 2.0 . b = 3.0 The idea of "where" is to allow re-using variable names instead of having to keep track of which ones are in use. I just tried to give a very simple example of how you might do that more than once in a statement. then I'd write something like this: x = a + b where: a = sqrt(n) where: n = 2. b = sqrt(n) where: n = 3. -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Alex Martelli wrote: Indeed, the fact that many MANY more people are familiar with SQL than with Haskell may be the strongest practical objection to this choice of syntax sugar; the WHERE clause in an SQL SELECT has such wildly different semantics from Haskell's "where" that it might engender huge amounts of confusion. I.e., reasoning by analogy with SQL only, one might reasonably expect that minor syntax variations on: print a, b where a = b could mean roughly the same as "if a==b: print a, b", rather than hmm... SQL-ish associations are good too, if you think a little deeper then common post-effect of using SQL "where" keyword. print a, b where a = b means a==b in statement "print a, b" roughly the same as: a = b print a, b I wonder if 'with', which GvR is already on record as wanting to introduce in 3.0, might not be overloaded instead. using 'with', is some way unnatural for my point of view (may be because translations of 'with' and 'where' into russian, are way different and 'where' translation reads as natural language while 'with' does not). I'd like to keep 'where' keyword for proposal, but semantics are more important, so in case I do not mind for other keyword. -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
And about examples for usage "where" keyword reading http://manatee.mojam.com/~skip/python/fastpython.html I understand that almost every example should use that keyword =) -- http://mail.python.org/mailman/listinfo/python-list
Re: Pygame and pyopengl with py2exe ?
Nyx42 wrote: Second program (pygame + pyopenGL): Py2exe can't import OpenGL.GL and OpenGL.GLU :( about that, may be names of imports are generated in runtime, so you can try to specify them directly options = {"py2exe": {"packages": ["OpenGL.GL","OpenGL.GLU"]}}, -- http://mail.python.org/mailman/listinfo/python-list
Python3: on removing map, reduce, filter
Hi. How does GvR suggestions on removing map(), reduce(), filter() correlate with the following that he wrote himself (afaik): http://www.python.org/doc/essays/list2str.html ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3: on removing map, reduce, filter
Paul Rubin wrote: How does GvR suggestions on removing map(), reduce(), filter() correlate with the following that he wrote himself (afaik): http://www.python.org/doc/essays/list2str.html I think that article was written before list comprehensions were added to Python. anyway list comprehensions are just syntaxic sugar for >>> for var in list: >>> smth = ... >>> res.append(smth) (is that correct?) so there will be no speed gain, while map etc. are C-implemented -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3: on removing map, reduce, filter
Steve Holden wrote: Andrey Tatarinov wrote: Hi. How does GvR suggestions on removing map(), reduce(), filter() correlate with the following that he wrote himself (afaik): http://www.python.org/doc/essays/list2str.html > And note that the summary in the conclusiogn BEGINS with "Rule number one: only optimize when there is a proven speed bottleneck", which seems to adequately imply that straightforward code is to be preferred unless speed requirements override that. My main question was: "how could be this advices applied, when map, reduce and filter would be removed?" but earlier I got answers about speed of list comprehensions, though they need to be proved. -- http://mail.python.org/mailman/listinfo/python-list
Re: python3: 'where' keyword
Nick Coghlan wrote: And about examples for usage "where" keyword reading http://manatee.mojam.com/~skip/python/fastpython.html I understand that almost every example should use that keyword =) I suspect polluting the outer namespace would still be faster, since Python wouldn't have to create the extra level of scoping all the time. sure, but just a little bit slower =) -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement local namespaces summary (was Re: python3: 'where' keyword)
Nick Coghlan wrote: Abstract The proposal is to add the capacity for statement local namespaces to Python. This allows a statement to be placed at the current scope, while the statement's 'setup code' is indented after the statement:: with: I think using 'with' keyword can cause some ambiguity. for example I would surely try to write >>> x = a+b with self: >>> b = member and using with at the end of block brings more ambiguity: >>> stmt1() >>> stmt2() >>> with self: >>> member = stmt3() compare to: >>> stmt1() >>> stmt2() >>> with: >>> variable = stmt3() a way different semantics with just one word added/deleted. -- http://mail.python.org/mailman/listinfo/python-list
Re: exceptions and items in a list
rbt wrote: If I have a Python list that I'm iterating over and one of the objects in the list raises an exception and I have code like this: try: do something to object in list except Exception: pass Does the code just skip the bad object and continue with the other objects in the list, or does it stop? # skip bad object and continue with others for object in objects: try: #do something to object except Exception: pass # stop at first bad object try: for object in objects: #do something to object except Exception: pass -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement local namespaces summary (was Re: python3: 'where' keyword)
Nick Coghlan wrote: Semantics - The code:: with: translates to:: def unique_name(): unique_name() Bleh. Not only was my proposed grammar change wrong, my suggested semantics are wrong, too. Raise your hand if you can see the problem with applying the above semantics to the property descriptor example. So I think the semantics will need to be more along the lines of "pollute the namespace but mangle the names so they're unique, and the programmer can *act* like the names are statement local". This will be much nicer in terms of run-time performance, but getting the locals() builtin to behave sensibly may be a challenge. afair you told yourself that var = where: translates to: def unique_name(): return var = unique_name() in this case class gets unique_name() function? is it that bad? anyway I'd prefer to change semantics deeper. adding new statement-only scope and adding our suite-definitions there. -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement local namespaces summary (was Re: python3: 'where' keyword)
So of the four keywords suggested so far ('where', 'with', 'in', 'using'), I'd currently vote for 'using' with 'where' a fairly close second. My vote goes to 'using' because it has a fairly clear meaning ('execute the statement using this extra information'), and doesn't have the conflicting external baggage that 'where' does. I should agree with you =) Though I love "with" for historical reasons and addiction to functional languages "using" is not that bad and I do not mind using it. =) -- http://mail.python.org/mailman/listinfo/python-list
Re: embedded scripts debugging
Miki Tebeka wrote: So the question is: Is there suitable library for simple python gui debugger, or may be there are some other techniques for debugging embedded scripts? What I usually do is add from pdb import set_trace in the embedded module somewhere and then add a call to set_trace (breakpoint) whenever I with. When the code reaches the call to set_trace, you'll have pdb prompt and you can debug as you like. Note that you can't add breakpoint dynamically this way. Thanks, I gathered pros and cons of embedding and decided to use python extending (i.e. creating python modules) instead of embedding. Happily I have an option to choose -- http://mail.python.org/mailman/listinfo/python-list
Re: else condition in list comprehension
Steve Holden wrote: Nick Coghlan wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I want i to be "i-2" if i%2 is not equal to 0? Hmm: z = [newval(i) for i in range(10)] using: def newval(x): if x % 2: return x - 2 else: return x + 2 Just some more mental twiddling relating to the thread on statement local namespaces. I presume the point of this is to avoid polluting the local namespace with "newval". I further presume you also have plans to do something about "i"? ;-) no, the point is in grouping definition of newval() with place where it is used. -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement local namespaces summary (was Re: python3: 'where' keyword)
Nick Coghlan wrote: Nick Coghlan wrote: Semantics - The code:: with: translates to:: def unique_name(): unique_name() I've come to the conclusion that these semantics aren't what I would expect from the construct. Exactly what I would expect can't really be expressed in current Python due to the way local name bindings work. The main thing to consider is what one would expect the following to print: def f(): a = 1 b = 2 print 1, locals() print 3, locals() using: a = 2 c = 3 print 2, locals() print 4, locals() I think the least suprising result would be: 1 {'a': 1, 'b': 2} # Outer scope 2 {'a': 2, 'c': 3} # Inner scope 3 {'a': 2, 'b': 2, 'c': 3} # Bridging scope 4 {'a': 1, 'b': 2} # Outer scope as for me, I would expect following: 1 {'a': 1, 'b': 2} 2 {'a': 2, 'b': 2, 'c': 3'} 3 {'a': 2, 'b': 2, 'c': 3'} 4 {'a': 1, 'b': 2} otherwise that would be impossible to do calculations based on scope variables and "using:" would be useless =), consider example of usage: current_position = 1 current_environment # = ... current_a_lot_of_other_parameters # = ... scores = [count_score(move) for move in aviable_moves] using: def count_score(move): #walking through current_environment return score -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement local namespaces summary (was Re: python3: 'where' keyword)
Nick Coghlan wrote: # Anonymous functions use res: def f(x): d = {} exec x in d return d in: res = [f(i) for i in executable] as for me, I found construction "use :" unobvious and confusing. Also there is great possibility to forget some of variables names. I think that syntax where: is more obvious. (and we already have defined semantics for it) we have two problems, that we try to solve 1) create method to nest scopes 2) create method to reverse execution order for better readability "using:" solves both at once. but your "use ... in ..." syntax shows, that you want to be able to solve 1) independently i.e. create nested scope without reversing execution order. so, I can suggest one more keyword "do:", which will create nested scope, just as "def f(): ... ; f()" do (and that could be just syntaxic sugar for it. so "use ... in ..." would look the following way: do: res = [f(i) for i in executable] #some more equations here using: def f(x): d = {} exec x in d return d that seems good for me. of course if you want to return something from the nest scope you must show that variable is from parent scope. // while writing that I realized that it's too complex to be implemented in python in that way. consider it as some type of brainstorming. -- http://mail.python.org/mailman/listinfo/python-list
Re: protecting the python code.
nell wrote: First the "10x in advance" means thanks in advance. The main importance of protecting my code is to save headache of customers that want to be smart and change it and then complain on bugs also you can try to use py2exe -- http://mail.python.org/mailman/listinfo/python-list
Re: dot products
Rahul wrote: I want to compute dot product of two vectors stored as lists a and b.a and b are of the same length. one simple way is sum(a[i]*b[i] for i in range(len(a))) btw, imho the most "Pythonic" would be: sum(i*j for (i,j) in zip(a,b)) -- http://mail.python.org/mailman/listinfo/python-list