• (cs) in reply to Principal Skinner
    Principal Skinner:
    jspenguin:
    That's a list comprehension.

    Interesting, though I find this concept kind of hard to... um... understand.

    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.

  • (cs) in reply to makomk
    makomk:
    Yes, but that's Perl - backticked calls to external programs are a long-standing tradition (albeit in this case a WTF-worthy one). Python makes you jump through slightly more hoops to do the same thing (and probably with good reason).


    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. 
  • (cs) in reply to dzR

    dzR, are you somehow confused about what kind of site this is?

  • Jago (unregistered) in reply to masklinn

    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.)

  • neek (unregistered) in reply to Frederik

    <font size="2">the real wtf here is that he's using mysql.</font>

  • Reed (unregistered)

    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



  • DWalker (unregistered) in reply to pmagill
    Anonymous:
    Wow, did the author of one of the WTF's get pissed at Alex or something?

    Regretfully changing the software won't keep this from happening.  Even if you are required to sign up first this guy can sign up, post get banned and sign up again using yet another msn or hotmail address.

    ____________
    I am that signature virus, propogating in an assited manner.
     
    That signature virus needs spell-check...
  • (cs) in reply to Stian Soiland
    Anonymous:
    Of course.. the weakref module. It is seldemly used by Python programmers, though, usually only for backend maintenance stuff.


    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)
  • (cs) in reply to trollable
    trollable:
    Anonymous:
    Of course.. the weakref module. It is seldemly used by Python programmers, though, usually only for backend maintenance stuff.


    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)

    What would "soft references" mean?

  • Anonymous coward (unregistered) in reply to Frederik

    Anonymous:
    Well, at least it worked... kind of...

    WTF?

     

    Heh, the captcha is "random"

  • pseudonymous (unregistered) in reply to masklinn

    I think the original poster is asking about weak references.

    I think.

  • Anonymous coward (unregistered) in reply to pseudonymous

    Anonymous:
    I think the original poster is asking about weak references. I think.

     

    I don't see an "original post".  The first post I see is "Well, at least it worked"

    [Both IE & Firefox]   

  • (cs) in reply to masklinn
    masklinn:
    trollable:
    Anonymous:
    Of course.. the weakref module. It is seldemly used by Python programmers, though, usually only for backend maintenance stuff.


    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)

    What would "soft references" mean?

    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?)

  • Andrew (unregistered) in reply to Randolpho

    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 :)

  • (cs) in reply to makomk
    makomk:
    masklinn:
    trollable:
    Anonymous:
    Of course.. the weakref module. It is seldemly used by Python programmers, though, usually only for backend maintenance stuff.


    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)

    What would "soft references" mean?

    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?)

    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)

  • (cs) in reply to neek

    Anonymous:
    <FONT size=2>the real wtf here is that he's using mysql.</FONT>

    Indeed it is.

  • James Schend (unregistered) in reply to jwenting

    Where's the WTF?  It's missing, all I see is the comments.

  • Zygo Blaxell (unregistered) in reply to Frederik

    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.

  • (cs) in reply to Principal Skinner
    Principal Skinner:
    Python is my favorite general-purpose scripting language (rather fond of JavaScript, but it has pretty narrow applications), but I didn't know about this syntax which still kind of blows my mind:

    db_list_names = [a[1] for a in db]

    This is definitely not typical Bourne-shell style.  Seems like the bass-ackwards kind of thing that might be possible in Perl, however.

    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:

    new_list = []
    for item in foo:
        if item:
            new_list.append(item)

    or list comprehension stz

    new_list = [item for item in foo if item]
    Not abundantly clear, but generally goes.
    new_list = [variable, loop, condition/loop]
  • (cs) in reply to Cyresse
    Cyresse:
    Principal Skinner:
    Python is my favorite general-purpose scripting language (rather fond of JavaScript, but it has pretty narrow applications), but I didn't know about this syntax which still kind of blows my mind:

    db_list_names = [a[1] for a in db]

    This is definitely not typical Bourne-shell style.  Seems like the bass-ackwards kind of thing that might be possible in Perl, however.

    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:

    new_list = []
    for item in foo:
        if item:
            new_list.append(item)

    or list comprehension stz

    new_list = [item for item in foo if item]
    Not abundantly clear, but generally goes.
    new_list = [variable, loop, condition/loop]

    Well, for your example I think i'd use filter and a lambda ;)

  • my name (unregistered) in reply to Frederik

    WTF ?

  • John Hensley (unregistered) in reply to Cyresse
    Cyresse:
    List comprehension, stolen from Haskell. Haskell does them better, but once you learn em they're very useful.

    And I suppose Haskell stole them from Fortran, or whatever language first had a FOR statement.

  • - (unregistered) in reply to Amanjit Gill

    I doubt the script runs on Windows.

  • Ed (unregistered)

    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.

Leave a comment on “Real Coders Don't Need Drivers”

Log In or post as a guest

Replying to comment #60061:

« Return to Article