Re: [perl-python] string pattern matching
Perhaps a message to the effect of "These messages are specifically disowned by the groups to which they are posted, have historically been riddled with blatant errors, and are assumed to continue in the same quality." should be posted as a follow-up to each of these messages by XL in order to avoid having to spend the time to find the inaccuracies in each one individually. Considering the response so far, a list of frequent posters and not-so-frequent posters who will vouch for the accuracy of the disclaimer might even be added. Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] string pattern matching
- Henry Wadsworth Longfellow "Erik Max Francis" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Dan Perl wrote: > > > Perhaps someone will write a program to automatically follow up on every > > [perl-python] posting? The follow-up could just contain a statement like > > the one Daniel mentions. Obviously the program would be written in python. > > ;-) > > I'm not really sure that such a disclaimer is explicitly necessary. > Anyone looking at Xah Lee's posts will also see the threads they > generate, which involve people pointing out all their errors. Granted > this won't happen with every single post, but since he's posting this > stuff once a day, I don't think the chances of someone finding his posts > and not seeing the related discussion and refutations is a big risk. > > For the rest of us, we can just killfile the threads easily enough. It seems to me that application of one of these solutions reduces the effectiveness of the other. If enough persons killfile the threads, who warns the newbies? And so those who don't killfile the threads to ensure that somebody is still guarding against misleading information to newbies continue dealing with it manually. Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] text pattern matching, and expressiveness
"John Bokma" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Xah Lee wrote: > > > Perl is a language of syntactical variegations. Python on the other > > hand, does not even allow changes in code's indentation, but its > > efficiency and power in expression, with respect to semantics (i.e. > > algorithms), showcases Perl's poverty in specification. > > Clarify :-D. Clarification: He (XL) is a troll and admits it. If only he would include that information up-front in his (IMO worthless) [perl-python] posts, it would save a lot of effort. Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: assert 0, "foo" vs. assert(0, "foo")
> "Thomas Guettler" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > Hi, > Python 2.3.3 (#1, Feb 5 2005, 16:22:10) [GCC 3.3.3 (SuSE Linux)] on linux2 > >>> assert 0, "foo" Assert that 0 is true. If that fails, raise AssertionError("foo"). > Traceback (most recent call last): > File "", line 1, in ? > AssertionError: foo > >>> assert(0, "foo") Assert that the tuple (0, "foo") is true. Non-empty tuples are always true. > >>> > > If you use parenthesis for the assert statement, it never > raises an exception. > Up to now I raised strings, but since this is deprecated, > I switched to use the second argument for the assert > statement. > Is it possible to change future python versions, that > assert accept parenthesis? As shown above, it does, but it doesn't do quite what you expected. For further enlightenment, try the following and think through why each one gives the results it does: assert (), 'spam' assert [], 'eggs' assert {}, 'spam and eggs' assert (0,), 'spam, spam, and eggs' assert (0, "foo"), 'spam, spam, eggs, and spam' assert 0, "foo", 'shrubbery' The last will give a syntax error. Can you spot why? > Thomas > -- > Thomas Güttler, http://www.thomas-guettler.de/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Wrong with this script?
"R.Meijer" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, I've been busy with an experimental script, and I can't seem to > see what is wrong with it, can somebody tell me? > > Here it is: > > a = 0 > b = 1 > mainloop = 1 > > print "Welcome to pyFibo" > print "For more information type \'help\'" > while mainloop==1: > limit = input("Until what number do you want to see the > Fibonacci series?") > if limit=="help": > print "The Fibonacci series is a worldfamous series of > numbers.\ > Each consecutive number is calculated by adding the previous two > numbers to\ > each other." > else: > while b < limit: > print b > a, b = b, a+b > print "Want to do another series?" > again = input("(Type yes for another series, or anything > else to quit.)" You need to close the () for input here. After doing that, if you run it you will notice that you get an exception for most inputs, including "yes". IIRC, input() is scheduled for removal in some future version of Python because it doesn't do what you would expect and it is generally a bad idea to use it. The functionality is along the lines of: eval(raw_input('your string here')) You undoubtedly want raw_input() instead here. > if again!="yes": > mainloop = 0 This last line needs indented. And a couple of minor points: 1. Choose an amount of indentation per level and stick to it. 4 is rather common in Python code. 2. When posting to the list, make sure that the lines in your code are short enough that they will not wrap and be posted as broken code. 70 chars is usually safe. Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: Wrong with this script?
"R.Meijer" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Thank you very much for the help and the tips :-) This is my very first python > script, and I knew it would have some stupid mistakes; but it's doing something > weird right now...I did all the stuff you told, me, and now it'll at least run. > But whenI enter a number as a limit, the loop keeps going on forever, nad the > numbers won't stop rolling. I'm guessing this is because it sees limit as a > string, how can I let it see it as an integer? My mistake. I was only looking at the last input() call you were using. For the other one, when you change it to raw_input(), you will get a string that you must convert to an integer in order to use it for numerical calculations or comparisons. int(raw_input('your message here')) will do this. After you make this change, try entering a string that cannot be parsed as an integer, and you will see another exception (ValueError) is raised. In order to properly handle this, I would wrap the int(raw_input()) in a try: except: block inside a loop. When you get a valid integer, you can then "break" out of the loop and continue executing. You may also want to look at the rest of your script for another place you can use "break" in order to eliminate a flag. Happy scripting, and welcome to the bliss that is Python. Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: Perl to Python conversion
On Dec 9, 1:33 pm, martin.sch...@gmail.com (Martin Schöön) wrote: > First off: I am new here and this is my first post after > lurking for quite some time. > > Second off: I don't know much Python---yet. > > Problem: I have come across a small open source application > that I find quite useful. It does have one major flaw though. > Its output is in imperial units. Converting isn't a big deal > for occasional use but if I start to use this stuff on a > regular basis... > > So I down-loaded the source code and found this thing is written > in Perl. > > Should I learn enough Perl to add the conversion? Probably > but this may be a nice excuse to get my Python education > going and if I do I might as well re-do the user interface. > > If I do re-write this thing in Python I might need to learn both > Perl and Python... > > Hence, are there any Perl to Python converters? So far I > have only found bridgekeeper which really is (was?) consultancy. > Apart from that I only find people recommending a manual re-write. > > Any thoughts/recommendations? > > TIA, > > /Martin Martin, A full answer will depend a lot on several different factors, including the length of the Perl code, what style it was written in (there seem to be uncountably many possibilities), your experience with languages in general, and with that style in particular. In general, though, if your primary purpose is to learn Python and ending up with a useful tool is secondary, I'd recommend rewriting the tool from scratch, possibly keeping the Perl source handy. If the existing tool is command-line based, you might also be able to write a short script through which you can pipe the output of the original program to handle the conversion. Intchanter Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing json where object keys are not quoted?
On Dec 9, 3:51 pm, Wells wrote: > Is there some way to finagle the json module to parse JSON (well, > almost JSON) where the object keys are not in quotes? I know it's not > 100% valid JSON, but I'm just curious. > > I don't have control over the data, so I can't make it fit the spec :) Hopefully this won't be a recurring problem, because maintenance of any solution could very well be a nightmare if you have to keep it up. The JSON library that ships with Python doesn't appear to be built for malformed JSON like what you mention, and making it handle it will take a bit of work on your part, but here's a start (based on my 2.6.4 installation): In /path_to_python_standard_library/json/decoder.py (please back this up before making any changes), comment out the try/except block that tries to load scanstring from _json and duplicate the last line (c_scanstring = None), removing its indentation. You'll then need to modify py_scanstring() to meet your needs, but be sure you understand what it's doing first. You'll need to track whether you found the leading '"' for the key and look for the other one if you did, but just look for the ':' otherwise. Again, this isn't an advisable solution, and it won't work in all cases even if you have the best of luck, but it may just work in enough cases. It's pretty amazing that the incoming document doesn't match the spec, though. The only correct solution would be to fix the library that generated it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immediate Help with python program!
On Dec 9, 5:18 pm, Jon Clements wrote: > Someone's homework assignment is overdue/due very soon? And, I don't > believe for a second this is your code. In fact, just searching for > (the obvious Java based) function names leads me to believe you've > 'butchered' it from Java code (do you not think your teacher/lecturer > can do the same?). > > Someone might well help out, but I'd be surprised if you got a "here's > how to fix it response", as from my POV you haven't done any work. > > Of course, I'm occasionally wrong, > > Jon. I also got the impression that it was a homework assignment. I'll just add that trying to bring attention to your request for help with exclamation points and words that indicate urgency is pretty bad form in a volunteer support community. -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing gmail
http://mail.google.com/support/bin/answer.py?hl=en&answer=77654 -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd json encoding erro
On Dec 15, 3:03 pm, Wells wrote: > I get this exception when decoding a certain JSON string: > > 'ascii' codec can't encode character u'\u2019' in position 8: ordinal > not in range(128) > > The JSON data in question: > > http://mlb.com/lookup/json/named.player_info.bam?sport_code=%27mlb%27... > > It's in the 'high_school' key. Is there some string function I can run > on the information before I decode it to avoid this? In my test using this same data, I did not get such an error. Here's my code: data = '{"player_info": {"queryResults": { "row": { "active_sw": "Y", "bats": "R", "birth_city": "Baltimore", "birth_country": "USA", "birth_date": "1987-08-31T00:00:00", "birth_state": "MD", "college": "", "death_city": "", "death_country": "", "death_date": "", "death_state": "", "end_date": "", "file_code": "sf", "gender": "M", "height_feet": "6", "height_inches": "1", "high_school": "St. Paul \u2019s School For Boys (MN) HS", "jersey_number": "", "name_display_first_last": "Steve Johnson", "name_display_first_last_html": "Steve Johnson", "name_display_last_first": "Johnson, Steve", "name_display_last_first_html": "Johnson, Steve", "name_display_roster": "Johnson, S", "name_display_roster_html": "Johnson, S", "name_first": "Steven", "name_full": "Johnson, Steve", "name_last": "Johnson", "name_matrilineal": "", "name_middle": "David", "name_nick": "", "name_prefix": "", "name_title": "", "name_use": "Steve", "player_id": "489002", "primary_position": "1", "primary_position_txt": "P", "primary_sport_code": "", "pro_debut_date": "", "start_date": "2009-12-10T00:00:00", "status": "Active", "status_code": "A", "status_date": "2009-12-10T00:00:00", "team_abbrev": "SF", "team_code": "sfn", "team_id": "137", "team_name": "San Francisco Giants", "throws": "R", "weight": "200" }, "totalSize": "1" }}}' import json print json.loads(data) (I'm running 2.6.4 on Mac OS X) Intchanter Daniel Fackrell -- http://mail.python.org/mailman/listinfo/python-list
Re: regex help
On Dec 16, 10:22 am, r0g wrote: > Gabriel Rossetti wrote: > > Hello everyone, > > > I'm going nuts with some regex, could someone please show me what I'm > > doing wrong? > > > I have an XMPP msg : > > > > > Does someone know what is wrong with my expression? Thank you, Gabriel > > Gabriel, trying to debug a long regex in situ can be a nightmare however > the following technique always works for me... > > Use the interactive interpreter and see if half the regex works, if it > does your problem is in the second half, if not it's in the first so try > the first half of that and so on an so forth. You'll find the point at > which it goes wrong in a snip. > > Non-trivial regexes are always best built up and tested a bit at a time, > the interactive interpreter is great for this. > > Roger. I'll just add that the "now you have two problems" quip applies here, especially when there are very good XML parsing libraries for Python that will keep you from having to reinvent the wheel for every little change. See sections 20.5 through 20.13 of the Python Documentation for several built-in options, and I'm sure there are many community projects that may fit the bill if none of those happen to. Personally, I consider regular expressions of any substantial length and complexity to be bad practice as it inhibits readability and maintainability. They are also decidedly non-Zen on at least "Readability counts" and "Sparse is better than dense". Intchanter Daniel Fackrell P.S. I'm not sure how any of these libraries are implemented yet, but I'd hope they're using a finite state machine tailored to the parsing task rather than using regexes, but even if they do the latter, having that abstracted out in a mature library with a clean interface is still a huge win. -- http://mail.python.org/mailman/listinfo/python-list