- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Think of it as the combination of map and filter.
They're, in fact, much more powerful than that. But it may help you start understanding the principles behind list comprehensions and generator comprehensions.
Admin
No, they're not. It's a convenience, just like system() in c. perl has DBI and various drivers for DB calls. Don't spread misinformation.
Admin
dzR, are you somehow confused about what kind of site this is?
Admin
And if you like list comprehensions and generator expressions, consider having a look at Haskell, the language Python borrowed them from. As an experimental research language at the cutting edge of computer science, it's full of even more mind-blowing concepts. You probably won't end up actually programming in it, but it's guaranteed to open your mind.
(It's also the language that the only working version of Perl 6 is written in, but don't let that put you off it.)
Admin
<font size="2">the real wtf here is that he's using mysql.</font>
Admin
Actually, the Python code is not bad. At least it actually utilizes some of the most useful parts of the language, which lots of Python scripts fail to do and therefore end up being rather tedious C-programs-written-in-Python.
Yes, calling out to system tools instead of just connecting to the DB is dumb, but I challenge everyone here to truthfully claim that they've never done that (whether out of convenience, ignorance, or neccesity).
Reed
Admin
Admin
Thanks for all the replies (and special mention for the example).
Next question is:
does Python have soft references?
(yes I have checked the doc and google, I found only some kind of emulation using eval)
Admin
What would "soft references" mean?
Admin
WTF?
Heh, the captcha is "random"
Admin
I think the original poster is asking about weak references.
I think.
Admin
I don't see an "original post". The first post I see is "Well, at least it worked"
[Both IE & Firefox]
Admin
I believe they're a bit like weak references, except the object isn't freed until the program runs out of memory. I can't see them working in Python (or indeed C/C++ for that matter) - I think they're a Java idea. (How do you tell when you're out of memory?)
Admin
Every now and then, for whatever weird reason, you really do want to parse a mysql text report. But if you do, there's a better trick than splitting on whitespace; read the second line of the output (the "dashes" between the headers and the data) to find the width of all of the fields :)
Admin
That's some frigging bizarre concept, I have to say that I barely see the point of it (only use i'd see would be memcached data)
Admin
Indeed it is.
Admin
Where's the WTF? It's missing, all I see is the comments.
Admin
I did this once. A Tcl script monitoring a sensor device needed to do three things in an event loop:
1. Send a command to the device, once per second.
2. Read the response(s) to the command. There may be zero or more responses.
3. Decode the response data and record it somewhere. Before I got there it wrote the data to a file, I replaced that with code to insert it into a database.
Parts 1 and 2 were real-time essential tasks, part 3 could crash and burn without causing inconvenience. I wanted to avoid causing any part of the program running parts 1 and 2 to be disrupted by any kind of failure of part 3.
Tcl provides non-blocking, event-driven I/O to remote devices (like the serial port) and child processes (like psql in a child process). Tcl will even automatically queue up data in memory if the child process or device gets slow. This is important because I don't want anything happening in task #3 from interfering with tasks #1 and #2.
If I had used a database driver library in part #3, I would still have placed it in a separate process and fed it data asynchronously from the process that implements tasks #1 and #2, just so it could crash or hang all it likes without breaking anything I actually cared about. Since a separate, isolated process already existed in the form of the "psql" utility, I just used that.
Admin
List comprehension, stolen from Haskell. Haskell does them better, but once you learn em they're very useful.
i.e. for a list
foo = ["a","b",None,"c",None, "d"] to remove the Nones you can either:
or list comprehension stz
Admin
Well, for your example I think i'd use
filter
and a lambda ;)Admin
WTF ?
Admin
And I suppose Haskell stole them from Fortran, or whatever language first had a FOR statement.
Admin
I doubt the script runs on Windows.
Admin
I did something a bit garbage like this once- we needed to automate interactions with a bunch of network devices via SSH and Paramiko seemed to having some stability issues (refusing to timeout under certain conditions despite timeouts being set), so I ended up writing a wrapper for the Linux SSH command using pexepct- it worked okay, but it felt gross.