Style question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kamus of Kadizhar

    Style question

    I have the following line:

    remoteMD5hash = Socket.recv(256 )

    But what I really want is remoteMD5hash.s trip()

    What's the best way to say that on one line?

    remoteKD5hash = string.strip(So cket.recv(256)) ?

    Or something better?

    -Kamus

    --
    o__ | If you're old, eat right and ride a decent bike.
    ,>/'_ | Q.
    (_)\(_) | Usenet posting`

  • Peter Otten

    #2
    Re: Style question

    Kamus of Kadizhar wrote:
    [color=blue]
    > I have the following line:
    >
    > remoteMD5hash = Socket.recv(256 )
    >
    > But what I really want is remoteMD5hash.s trip()
    >
    > What's the best way to say that on one line?
    >
    > remoteKD5hash = string.strip(So cket.recv(256)) ?
    >
    > Or something better?[/color]

    remoteMD5hash = Socket.recv(256 ).strip()

    Peter

    Comment

    • Paul Rubin

      #3
      Re: Style question

      Kamus of Kadizhar <yan@NsOeSiPnAe Mr.com> writes:[color=blue]
      > remoteKD5hash = string.strip(So cket.recv(256)) ?
      >
      > Or something better?[/color]

      I think the preferred syntax these days is Socket.recv(256 ).strip()
      but either way works.

      Comment

      • Peter Hansen

        #4
        Re: Style question

        Kamus of Kadizhar wrote:[color=blue]
        >
        > I have the following line:
        >
        > remoteMD5hash = Socket.recv(256 )
        >
        > But what I really want is remoteMD5hash.s trip()
        >
        > What's the best way to say that on one line?
        >
        > remoteKD5hash = string.strip(So cket.recv(256)) ?[/color]

        Given that a socket.recv() call makes no guarantees about how
        many bytes of data it returns, other than being somewhere between
        one byte (if the socket is still open) and 256 bytes, neither the
        suggested alternatives nor the above approach are "better style"
        since both are potentially quite buggy.

        Instead, you need to separate the two things you are doing.
        One is receiving data from a TCP (?) socket, which requires
        more logic than just .recv(256). The other thing is massaging
        your data, in this case a simple strip operation.

        Therefore your code should look something more like this:

        def receiveAllDataF romSocket(sock) :
        # insert fancy logic here... check the archives or examples
        # on, say, the Python Cookbook site to learn how

        def cleanMd5Hash(da ta):
        return data.strip()

        # logic in your own code
        data = receiveAllDataF romSocket(sock)
        remoteKD5hash = cleanMd5Hash(da ta)

        ....or something like that.

        (Okay, I'm probably going overboard on the second part, since nobody
        really needs to encapsulate a strip() call in a subroutine, but you
        for sure aren't safe just doing one .recv() on a socket, and in any
        case mixing the two operations -- receiving data and massaging it --
        is not good style in any case.)

        -Peter

        Comment

        Working...