Dr. Dobb's Python-URL! - weekly Python news and links (Aug 30)
QOTW: "If I wanted to write five lines instead of one everywhere in a Python program, I'd use Java." -- Paul Rubin http://groups.google.com/group/comp.lang.python/msg/6fac4f3022acd1fa?hl=de&; "i think less buggy code is not the main concern for all." -- km http://groups.google.com/group/comp.lang.python/msg/db5acbd68447ae33?hl=de&; Bryan Olson discovers inconsistencies in the slice implementation that lead to a new slicing PEP and a lively discussion about string index semantics: http://groups.google.com/group/comp.lang.python/browse_frm/thread/402d770b6f503c27/66014427182265b9#66014427182265b9 Mark Dickinson finds strange runtime differences depending on the existence of dummy variables - the riddle is solved by Stelios Xanthakis who points out that min() can behave non-deterministically when the compared entities have no well-defined comparision sematics: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2ff00f12499233e5/8b658bc804020f08#8b658bc804020f08 Russell E. Owen tries to get unique ids for bound methods - and discovers that id() can return the same value for two different bound methods. The solution is to keep a reference to the bound method, thus the id() won't be reused - which tkinters _register does: http://groups.google.com/group/comp.lang.python/browse_frm/thread/143a9000bdc9847e/5b2d07b8e5470583#5b2d07b8e5470583 Ramza Brown wants a small footprint python distro - and gets answers which shed light on the subject from varying perspectives: http://groups.google.com/group/comp.lang.python/browse_frm/thread/18e8f91c8e915c50/da5c5a740f7afe87#da5c5a740f7afe87 Diego AndrÂs Sanabria wonders about the apparent lack of a binary tree data type in python: http://groups.google.com/group/comp.lang.python/browse_frm/thread/fb307dbdf7069b30/a0f94ea4a27ddb7c#a0f94ea4a27ddb7c sysfault wants enlightenment about decorators and metaclasses - and hopefully now has it: http://groups.google.com/group/comp.lang.python/browse_frm/thread/6bcb4d9ae934ac64/1060a20aaaf4b89a#1060a20aaaf4b89a Roland Hedberg wants to speed up his client/server implementation - and gets deep insight into the TCP/IP protocol stacks: http://groups.google.com/group/comp.lang.python/browse_frm/thread/507a711c61df7ad5/d6e871a24d8506a6#d6e871a24d8506a6 As often before, Jp Calderone eloquently illustrates the expressiveness Twisted affords those writing multitasking network servers: http://groups.google.com/group/comp.lang.python/browse_thread/thread/dce3370299af324e/ max(01)* needs help on piping commands in python: http://groups.google.com/group/comp.lang.python/browse_frm/thread/5cbe3126675ac0e0/1b3d6b36341e8cad#1b3d6b36341e8cad Pythoneers are nearly unanimous: when you need dynamic names for your variables ... you're probably doing something wrong. Python is plenty dynamic, though, so of course it's *possible*: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d9a99f17e0cad8f2/ Damien Elmes needs advice dealing with locales: http://groups.google.com/group/comp.lang.python/browse_frm/thread/8f44319aee701f71/ce6194ba78934818#ce6194ba78934818 KM compares apples with oranges - and gets told so: http://groups.google.com/group/comp.lang.python/browse_frm/thread/d1c6888eaec9930f/b444a3ec71e0e2b3#b444a3ec71e0e2b3 42 thinks he has new ideas about restricting python's execution - it turns out to be old news again, though: http://groups.google.com/group/comp.lang.python/browse_frm/thread/89e733d60380741b/4831ed5d84026549#4831ed5d84026549 Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Mygale is a news-gathering webcrawler that specializes in (new) World-Wide Web articles related to Python. http://www.awaretek.com/nowak/mygale.html While cosmetically similar, Mygale and the Daily Python-URL are utterly different in their technologies and generally in their results. For far, FAR more Python reading than any one mind should absorb, much of it quite interesting, several pages index much of the universe of Pybloggers. http://lowlife.jp/cgi-bin/moin.cgi/PythonProgrammersWeblog http://www.planetpython.org/ http://mechanicalcat.net/pyblagg.html comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly.
Dr. Dobb's Python-URL! - weekly Python news and links (Aug 30)
QOTW: "If I wanted to write five lines instead of one everywhere in a Python program, I'd use Java." -- Paul Rubin http://groups.google.com/group/comp.lang.python/msg/6fac4f3022acd1fa?hl=de&; "i think less buggy code is not the main concern for all." -- km http://groups.google.com/group/comp.lang.python/msg/db5acbd68447ae33?hl=de&; Bryan Olson discovers inconsistencies in the slice implementation that lead to a new slicing PEP and a lively discussion about string index semantics: http://groups.google.com/group/comp.lang.python/browse_frm/thread/402d770b6f503c27/66014427182265b9#66014427182265b9 Mark Dickinson finds strange runtime differences depending on the existence of dummy variables - the riddle is solved by Stelios Xanthakis who points out that min() can behave non-deterministically when the compared entities have no well-defined comparision sematics: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2ff00f12499233e5/8b658bc804020f08#8b658bc804020f08 Russell E. Owen tries to get unique ids for bound methods - and discovers that id() can return the same value for two different bound methods. The solution is to keep a reference to the bound method, thus the id() won't be reused - which tkinters _register does: http://groups.google.com/group/comp.lang.python/browse_frm/thread/143a9000bdc9847e/5b2d07b8e5470583#5b2d07b8e5470583 Ramza Brown wants a small footprint python distro - and gets answers which shed light on the subject from varying perspectives: http://groups.google.com/group/comp.lang.python/browse_frm/thread/18e8f91c8e915c50/da5c5a740f7afe87#da5c5a740f7afe87 Diego AndrÂs Sanabria wonders about the apparent lack of a binary tree data type in python: http://groups.google.com/group/comp.lang.python/browse_frm/thread/fb307dbdf7069b30/a0f94ea4a27ddb7c#a0f94ea4a27ddb7c sysfault wants enlightenment about decorators and metaclasses - and hopefully now has it: http://groups.google.com/group/comp.lang.python/browse_frm/thread/6bcb4d9ae934ac64/1060a20aaaf4b89a#1060a20aaaf4b89a Roland Hedberg wants to speed up his client/server implementation - and gets deep insight into the TCP/IP protocol stacks: http://groups.google.com/group/comp.lang.python/browse_frm/thread/507a711c61df7ad5/d6e871a24d8506a6#d6e871a24d8506a6 As often before, Jp Calderone eloquently illustrates the expressiveness Twisted affords those writing multitasking network servers: http://groups.google.com/group/comp.lang.python/browse_thread/thread/dce3370299af324e/ max(01)* needs help on piping commands in python: http://groups.google.com/group/comp.lang.python/browse_frm/thread/5cbe3126675ac0e0/1b3d6b36341e8cad#1b3d6b36341e8cad Pythoneers are nearly unanimous: when you need dynamic names for your variables ... you're probably doing something wrong. Python is plenty dynamic, though, so of course it's *possible*: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d9a99f17e0cad8f2/ Damien Elmes needs advice dealing with locales: http://groups.google.com/group/comp.lang.python/browse_frm/thread/8f44319aee701f71/ce6194ba78934818#ce6194ba78934818 KM compares apples with oranges - and gets told so: http://groups.google.com/group/comp.lang.python/browse_frm/thread/d1c6888eaec9930f/b444a3ec71e0e2b3#b444a3ec71e0e2b3 42 thinks he has new ideas about restricting python's execution - it turns out to be old news again, though: http://groups.google.com/group/comp.lang.python/browse_frm/thread/89e733d60380741b/4831ed5d84026549#4831ed5d84026549 Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Mygale is a news-gathering webcrawler that specializes in (new) World-Wide Web articles related to Python. http://www.awaretek.com/nowak/mygale.html While cosmetically similar, Mygale and the Daily Python-URL are utterly different in their technologies and generally in their results. For far, FAR more Python reading than any one mind should absorb, much of it quite interesting, several pages index much of the universe of Pybloggers. http://lowlife.jp/cgi-bin/moin.cgi/PythonProgrammersWeblog http://www.planetpython.org/ http://mechanicalcat.net/pyblagg.html comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly.
Re: Some newbie cgi form questions...
> Traceback (most recent call last): > File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ? > if not form.keys()[key]: > TypeError: list indices must be integers > > As you can see, I am using python 2.3 (my web service provider is > responsible for this - I'd use 2.4.1 if I could) That code above can't work - you want something like if not form.keys() in key: Try reading the error messages. And google them. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Some newbie cgi form questions...
> The second for loop is an attempt at trying to print all the values > that were entered on the for without presenting the hidden values. I'd > really like to do this, but I can't seem to figure out how to make a > special case for hidden form values, nor can I find an example of how > to do it in any of the many python cgi pages I've visited so far. If I You can't find that out by just processing the http request - http doesn't know types in form fields. Only HTML(!) does that. But as you are the one who created the form in the first place, you should know which from fields are the hidden ones. Or - if you are more after a generic approach, use namespaces. You can do that by naming hidden fields e.g. hidden:, where is of course something like foo or bar or whatever. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: substring and regular expression
borges2003xx yahoo.it yahoo.it> writes: > > but in general is there a way to include in a re, in this example > something like...matches iff p , and q in which p==q[::-1] ? A way to > putting a small part of code of python in re? Thanx for your many helps What you are after is a parser - there are plenty available. I prefer spark, but pyparsing has gained lots of attention lately and is AFAIK the quasi-standard. The problem you described is a classical instance of a so-called context-free grammar. There is absolutely _no_ way to teach regular expressions how to detect words created based on a grammar of that kind. Really. It won't work. So - either use a parser, or write a simple one yourself - the code above qualifies as an attempt to do so. Regards, Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex for nested {}
> constructs like a**nn**n, with a={ and b=} in your case. This should have been a**nb**n - an example would be aaabbb. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex for nested {}
Chris cdot.de> writes: > > is something like that possible? No. Not with "pure" regexes. The reason is that the theory behind them doesn't allow to detect syntactic constructs like a**nn**n, with a={ and b=} in your case. What you need is a "real" parser - usually one uses regexes to split the string into so-called tokens. The either a handwritten parser or a parser-generator is used. The following is a simple implementation based on the spark parser generator: import spark class ParsingError: def __init__(self, msg = None): self.msg = msg def __repr__(self): return "Parsing Error: %s" % self.msg class Token: def __init__(self, type, attr=None): self.type = type self.attr = attr def __cmp__(self, o): return cmp(self.type, o) def __repr__(self): return self.attr or self.type class Scanner(spark.GenericScanner): def __init__(_): spark.GenericScanner.__init__(_) def error(self, s, pos): raise ParsingError("Lexical error at %i" % pos) def tokenize(self, input): self.rv = [] spark.GenericScanner.tokenize(self, input) return self.rv def t_whitespace(self, s): r' \s+ ' pass def t_word(self, s): r'[a-z]\w*' t = Token(type='word', attr=s) self.rv.append(t) def t_parentheses(self, s): r' \{| \}' t = Token(type=s) self.rv.append(t) def t_comment(self, s): r'\#.*' pass def t_default(self, s): pass class Parser(spark.GenericParser): def __init__(self, start='expr'): spark.GenericParser.__init__(self, start) def p_expr_1(self, args): ''' expr ::= word block expr ''' return [(args[0], args[1])] + args[2] def p_expr_2(self, args): ''' expr ::= ''' return [] def p_block(self, args): ''' block ::= { expr } ''' return args[1] def error(self, token): raise ParsingError("Syntax error at or near `%s' token" % token) def scan(input): scanner = Scanner() try: return scanner.tokenize(input) except ParsingError,e: print e def parse(tokens): return Parser().parse(tokens) text = """ outer { inner1 { } inner2 { } } simple { } """ def main(): tokens = scan(text) print parse(tokens) if __name__ == "__main__": main() -- http://mail.python.org/mailman/listinfo/python-list
Re: Invoke a method to a specific thread
Varghjärta gmail.com> writes: > If I have "GUIClass":... > And start another thread and from that thread I want to call the > method "PaintSomething()" in "GUIClass" _in_ the same thread that > "GUIClass" lives in. There is no such thing in python, or any programming language for that matter. If a thread wants to run in an endless loop, there is nothing you can do about that - apart from maybe killing it. But even that won 't work all the time and is dangerous. However, in GUIs there is usually some so called event-loop. And for the task you have in mind you need a way to insert a custom event into that loop that will be executed the next time that gui thread is idle. Qt uses QCustomEvents for this. Swing has it's SwingUtilities.invokeLater http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#invokeLater and others have different means. So - whatever GUI you use, it depends on that what to do. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: How do i do this
Amit Regmi neolinuxsolutions.com> writes: > For some commad Linux like (pdbedit) its not possible to supply password > in the command line itself while we add a samba user account into the You might be able to utilize pexpect for this. Go google :) Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordering Products
> I have to admit that I don't understand what you mean with the > 'constant parts' of an expression? >From what I percieved of your example it seemed to me that you wanted to evaluate the constants like 7*9 first, so that an expression like a * 7 * 9 * b with variables a,b is evaluated like this: a * 63 * b So my suggestion was simply to make the *-operator more precedent when in between two constants. What I mean with constants here are of course integer/float literals. The concept of a differing operator precedence can be extended to arbitray elements when their types are known - which should be possible when variable values are known at parsing time. > The associativity of __mul__ is trivially fullfilled for the dummy > class M if an additional __eq__ method is defined by comparing factor > lists because those lists are always flat: I don't care about that, as my approach deosn't use python's built-in parser - it can't, as that wouldn't allow to re-define operator precedence. What you do is to simply collect the factors as list. But what you need (IMHO) is a parsing tree (AST) that reflects your desired behaviour by introducing a different precedence thus that the expression a * 7 *9 * b is not evaluated like ((a*7)*9)*b (which is a tree, and the standard way of evaluationg due to built-in parsers precedence rules) but as a*(7*9)*b which is also a tree. > The sorting ( or better 'grouping' which can be represented by sorting > in a special way ) of factors in question is really a matter of > (non-)commutativity. For more advanced expressions also group > properties are important: No, IMHO associativity is the important thing here - if (a * 7) * 9 yields a different solution than a *(7*9) your reordering can't be done - in the same way as re-arranging factors a*b to b*a only works if the commute - or, to put in in algebraic terms, the group is abelian. > If a,b are in a center of a group G ( i.e. they commute with any > element of G ) and G supplies an __add__ ( besides a __mul__ and is > therefore a ring ) also a+b is in the center of G and (a+b)*c = c*(a+b) > holds for any c in G. > > It would be nice ( and much more efficient ) not to force expansion of > the product assuming distributivity of __add__ and __mul__ and > factorization after the transposition of the single factors but > recognizing immediately that a+b is in the center of G because the > center is a subgroup of G. Well, you don't need to expand that product - the subexpression a+b is evaluated first. If you can sort of "cache" that evaluation's result because the expressions involved are of a constant nature, you can do so. The rason (a+b) is evaluated first (at least in the standard python parser, and in my proposed special parser) is that the parentheses ensure that. To sum things up a little: I propose not using the python built-in parser which results in you having to overload operators and lose control of precedence, but by introducing your own parser, that can do the trick of re-arranging the operators based on not only the "usual" precedence (* binds stronger than +), but by a type-based parser that can even change precedence of the same operator between different argument types is's applied to. That might sound complicated, but I think the grammar I gave in my last post shows the concept pretty well. regards, Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordering Products
Kay Schluehr gmx.net> writes: > Now lets drop the assumption that a and b commute. More general: let be > M a set of expressions and X a subset of M where each element of X > commutes with each element of M: how can a product with factors in M be > evaluated/simplified under the condition of additional information X? > > It would be interesting to examine some sorting algorithms on factor > lists with constrained item transpositions. Any suggestions? I don't think that sorting is the answer here. Firts of all IMHO you have to add an additional constraint - associativity of the operation in question So the problem could be reduced to making the constant parts be more associative than the non-constant parts. which you should be able to do with a parser. The BNF grammar could look like this: expr ::= v_expr "*" v_expr | v_expr v_expr ::= variable | c_expr c_expr ::= l_expr "*" literal | l_expr l_expr ::= literal | "(" expr ")" The trick is to create a stronger-binding multiplication operator on constants than on mixed expressions. This grammar is ambigue of course - so a LL(k) or maybe even LALR won't work. But earley's method implemented in spark should do the trick. If I find the time, I'll write an short implementation tomorrow. Diez -- http://mail.python.org/mailman/listinfo/python-list