|
Sylink posted:Can someone explain parsing with elementtree to me, it makes no sense and the effbot stuff seems to all be about making xml, I just want to get information. gently caress element tree, learn xpath - it's a standard simple query language for xml documents that is supported on a number of platforms, and it makes xml significantly less painful. http://codespeak.net/lxml/xpathxslt.html http://en.wikipedia.org/wiki/XPath_1.0 if you have code:
It's fairly terse, readable and expressive - and a lot simpler to use than things like dom or sax or object models.
|
# ? May 9, 2009 19:59 |
|
|
# ? May 14, 2024 15:12 |
|
I'm having a hard time wrapping my head around how Python handles string concatenation. Here's some code I'm working with:pre:def getHigh(self, event): """Called when high score list is to be displayed""" filename = "high.txt" scores = [] message = "" FILE = open(filename,"r") while FILE: line = FILE.readline() # format: '[name]_[score]' where _ = space s = line.split() n = len(s) if n == 0: break scores.append(s) # assemble the list to be readable for i in range(len(scores)): >> message += str(i),". %s %s\n" % (scores[i][0], scores[i][1]) dialog = wx.MessageDialog(None, message, "Biggest Losers", wx.OK) dialog.ShowModal() I've tried casting with str() and some other stuff, but nothing seems to take. How do I convert items from a 2D array to drop them into a string? (the wx stuff is a GUI package)
|
# ? May 9, 2009 21:01 |
|
Clanpot Shake posted:I'm having a hard time wrapping my head around how Python handles string concatenation. Here's some code I'm working with: Look at what you are doing on that line. If the variable to the left of the += operator is a string then the expression on the right hand side has to be a string as well. You have a string with a comma after it followed by a format string and variables as if you are combining it with the syntax for the print statement. code:
code:
|
# ? May 9, 2009 21:17 |
|
I looked at my course notes and the instructor never seems to use + to concatenate things. What exactly is the difference between , and +?
|
# ? May 9, 2009 21:32 |
|
sw-itch posted:A wrapper for this was actually posted yesterday on http://planet.python.org , maybe they read this thread. This is awesome. I'm guessing the easiest way of using this be to just copy-paste this code into whatever Python I'm writing?
|
# ? May 9, 2009 21:38 |
|
Clanpot Shake posted:I looked at my course notes and the instructor never seems to use + to concatenate things. What exactly is the difference between , and +? comma doesn't concatenate things, its for declaring tuples. The print statement interprets tuples specially. When you have something like this: code:
You can also use format strings with the print statement like you may be used to doing with printf() in c. You are merely concatenating strings however, not printing them. You shouldn't be placing tuples or format strings after the += operator. If you want to concatenate two strings in an expression, you can use the + operator. In python the plus operator does different things depending on the type of its operands- given two integers or two floats, it will return their sum; given two lists it will return the second list appended to the first list; given two strings, it will return the second concatenated with the first. A += B is the same thing as saying A = A + B. For consistencies sake, python 3000 is moving away from the print statement and uses a print function instead. tripwire fucked around with this message at 21:58 on May 9, 2009 |
# ? May 9, 2009 21:54 |
|
Centipeed posted:This is awesome. I'm guessing the easiest way of using this be to just copy-paste this code into whatever Python I'm writing? Even easier: save it as "whatever.py" in the same directory as your current python project. Import "whatever" and use as needed.
|
# ? May 9, 2009 21:55 |
|
tef posted:gently caress element tree, learn xpath - it's a standard simple query language for xml documents that is supported on a number of platforms, and it makes xml significantly less painful. This is much better, the loving dom models are annoying as gently caress, I was tempted just to make my own xml library.
|
# ? May 9, 2009 22:36 |
|
Clanpot Shake posted:
also you can loop over a file with code:
code:
|
# ? May 10, 2009 00:07 |
|
Lonely Wolf posted:Or you could just use message += "%s. %s %s\n" (str(i), scores[i], scores[i][1]) Why would you cast i to a string? message += "%d. %s %s\n" % (i, scores[i], scores[i][1]) Just go with the printf-esque replacement strings and don't waste time casting.
|
# ? May 10, 2009 03:03 |
|
Sylink posted:It just seems like every time I want to parse xml its completely non-intuitive and voodoo. Not to mention they don't have practical examples most of the time it seems. I completely agree. While it's probably not practical for most applications of XML, I prefer the lazy man's way of using JSON with a simple call to parse it into an associative array or whatever. I personally haven't used JSON with Python (only PHP) but I would imagine it dumps the data into a dictionary.
|
# ? May 10, 2009 03:09 |
|
spankweasel posted:Why would you cast i to a string? I was just keeping it close to his code there. If you'll notice the second time I didn't, though I did keep it as %s in the format string, implicitly casting it to repr, because I was far too lazy to remember that it was supposed to be %d.
|
# ? May 10, 2009 03:25 |
|
I don't get it... Doesn't ElementTree support xpath? What's the problem?
|
# ? May 10, 2009 03:27 |
|
Does anyone have a retard-proof, step-by-step guide for getting ANYTHING running on python running under lighttpd? web.py was the only deployment guide that I can find, and I just can NOT get it working. I've been trying for about 12 hours at this point. code:
2) /tmp/fastcgi.socket exists, and is generated and owned by lighttpd. 3) hello.py is the python file copy/pasted from the web.py tutorial code:
code:
The only guides I have found say something like "oh, just point it to your fcgi socket and then..." -- too advanced. I don't have a socket. I don't have anything. :cries from frustration:
|
# ? May 10, 2009 06:12 |
|
You probably don't want Lighttpd running on port 0. I'm honestly not even sure if that's a valid port number. You should be setting a port number, etc. in lighttpd.conf.
|
# ? May 10, 2009 06:22 |
|
This is going to sound super-stupid if I've missed something, because I've never gotten into Python web programming, but don't you need mod_python installed? I know I can't get into it personally because my host won't install mod_python.
|
# ? May 10, 2009 11:33 |
|
ShizCakes posted:Finally, I can find NOTHING of use on google at this point. Any suggestions? Perhaps point me to a guide that explains the ultra-basic concepts of how this stuff is supposed to work? Could hello.py be in the wrong format? I.E. Windows format instead of UNIX format. That is usually the case for me when pasting things directly from the docs don't work. Easiest way I know to change it is to install notepad++ and Format>Convert to UNIX format, save then re-upload.
|
# ? May 10, 2009 15:18 |
|
Centipeed posted:This is going to sound super-stupid if I've missed something, because I've never gotten into Python web programming, but don't you need mod_python installed? Not really. mod_python is just the python interpreter as an apache module with some exposure of apache internals in python to allow clever things to be done. Centipeed posted:I know I can't get into it personally because my host won't install mod_python.
|
# ? May 10, 2009 18:21 |
|
Centipeed posted:This is going to sound super-stupid if I've missed something, because I've never gotten into Python web programming, but don't you need mod_python installed? I know I can't get into it personally because my host won't install mod_python. mod_python is for Apache (and it sucks, use mod_wsgi) Shiz: I've never used lighttpd or web.py, but I found this basic configuration through Google. It works when I run lighttpd -f lighttpd.conf -D -- does it work for you? code:
code:
|
# ? May 10, 2009 19:02 |
|
Janin posted:Shiz: I've never used lighttpd or web.py, but I found this basic configuration through Google. It works when I run lighttpd -f lighttpd.conf -D -- does it work for you? Thank you for this to check my conf against - the guides I found on google had me set up my conf the same way. I appreciate the work in throwing that out there to help me double check. sw-itch posted:Could hello.py be in the wrong format? I.E. Windows format instead of UNIX format. That is usually the case for me when pasting things directly from the docs don't work. Holy poo poo. Holy loving poo poo. So obvious, yet I wasn't even CLOSE to thinking along those lines. You are completely correct. One dos2unix command later, BAM functioning server. In fact, I appreciate this so much I would be happy to purchase a custom title for you or a platinum upgrade - whichever you prefer. My email is shizcakes - gmail.com.
|
# ? May 10, 2009 23:25 |
|
Am I crazy or does Python not support increment/decrement? Is i += 1 the only way to do it?
|
# ? May 11, 2009 12:52 |
|
The Evan posted:Am I crazy or does Python not support increment/decrement? Is i += 1 the only way to do it? I think that is fairly accurate and agree with the choice, although I certainly do not disagree that the operator can be (and most commonly is) used in non-cryptic ways. supster fucked around with this message at 13:07 on May 11, 2009 |
# ? May 11, 2009 13:04 |
|
Fair enough; thanks.
|
# ? May 11, 2009 14:09 |
|
I would like to return the elements of an array that satisfy a given condition. I know that I can do this with the following:code:
code:
Any suggestions would be appreciated.
|
# ? May 11, 2009 18:09 |
|
code:
|
# ? May 11, 2009 18:16 |
|
Avenging Dentist posted:
Wow, completely missed that one. Thanks.
|
# ? May 11, 2009 18:17 |
|
Avenging Dentist posted:
wow.... i had no idea you could do that... insanity and very powerful
|
# ? May 11, 2009 22:36 |
|
ATLbeer posted:wow.... i had no idea you could do that... Only with numpy, as far as I can tell.
|
# ? May 11, 2009 23:05 |
|
No Safe Word posted:Only with numpy, as far as I can tell. Isn't it the exact same thing as doing filter( lambda something: something > 5, whatever_sequence)?
|
# ? May 11, 2009 23:10 |
|
tripwire posted:Isn't it the exact same thing as doing filter( lambda something: something > 5, whatever_sequence)? Not really. code:
code:
|
# ? May 11, 2009 23:25 |
|
Avenging Dentist posted:Not really. If you aren't using numpy you can still assign conditionally to elements in a range pretty easily by using a loop like so: code:
tripwire fucked around with this message at 23:47 on May 11, 2009 |
# ? May 11, 2009 23:42 |
|
tripwire posted:The original question was how to see the contents of a sequence that match a given condition, and thats exactly what filter is for. Not if you read the rest of his post: quote:I have multiple arrays that need to be sliced using the same indeces and feel that the bool array could be determined by one and applied to the others.
|
# ? May 11, 2009 23:46 |
|
Avenging Dentist posted:Not if you read the rest of his post: Touché. Carry on then
|
# ? May 11, 2009 23:47 |
|
Avenging Dentist posted:Not really. Don't mean to nitpick, but: code:
|
# ? May 12, 2009 07:13 |
|
jupo posted:Don't mean to nitpick, but: I always forget that syntax, but in my defense it is because that is the worst version of the ternary operator I can think of. It's not even in the right order. (Also, the really important thing in my opinion is that numpy doesn't make a copy of the array, which would kill the performance of the software I work on.* Honestly, I would hate Python if it weren't for Numpy, which makes it at least somewhat tolerable. Of course, like nearly every scripting language I know, I'm more familiar with the C API than the language itself. The C API is for Python is a huge pain in the rear end, by the way.) * Granted, it has to make an instance of the boolean array, but at least that's a C array under the hood, and it's not so bad if you're reusing it as the original question was talking about. Avenging Dentist fucked around with this message at 07:40 on May 12, 2009 |
# ? May 12, 2009 07:26 |
|
Avenging Dentist posted:I always forget that syntax, but in my defense it is because that is the worst version of the ternary operator I can think of. It's not even in the right order. Well in your defense I believe it's a relatively new addition to the language, 2.5 if I recall. I also remember when they added it to the language they ran a bunch of test cases across the standard library and found that most of the time the condition would evaluate to the same value for each test case which is the reasoning for the (apparently) weird order: code:
|
# ? May 12, 2009 08:49 |
|
Okay, I'm learning recursion and this is my first real function using it. I'm trying to create a recursvie function that takes decimal and converts to binary. Here is what I have. I'm doing something wrong because it loops forever and then ends with "RuntimeError: maximum recursion depth exceeded". I think I might just be doing recursion completely wrong. code:
darknife fucked around with this message at 01:29 on May 13, 2009 |
# ? May 13, 2009 00:54 |
|
Everything. That code is confusing, without the recursion. What the heck is x and bstr for when you could just compute them in the function? Why is it returning None, "return", for the number zero? I thought it was "0" for zero. Here is a working recursive version:code:
|
# ? May 13, 2009 01:53 |
|
Python 3 rules, just finished testing the port of a library I wrote and it's about 25% faster. I was also able to simplify the code immensely.code:
|
# ? May 13, 2009 04:52 |
|
|
# ? May 14, 2024 15:12 |
|
hlfrk414 posted:Everything. That code is confusing, without the recursion. What the heck is x and bstr for when you could just compute them in the function? Why is it returning None, "return", for the number zero? I thought it was "0" for zero. Here is a working recursive version: Yea, I didn't really know how it worked either. How does yours work?
|
# ? May 13, 2009 05:04 |