469,312 Members | 2,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Style question

I have the following line:

remoteMD5hash = Socket.recv(256)

But what I really want is remoteMD5hash.strip()

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

remoteKD5hash = string.strip(Socket.recv(256)) ?

Or something better?

-Kamus

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

Jul 18 '05 #1
3 1277
Kamus of Kadizhar wrote:
I have the following line:

remoteMD5hash = Socket.recv(256)

But what I really want is remoteMD5hash.strip()

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

remoteKD5hash = string.strip(Socket.recv(256)) ?

Or something better?


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

Peter
Jul 18 '05 #2
Kamus of Kadizhar <ya*@NsOeSiPnAeMr.com> writes:
remoteKD5hash = string.strip(Socket.recv(256)) ?

Or something better?


I think the preferred syntax these days is Socket.recv(256).strip()
but either way works.
Jul 18 '05 #3
Kamus of Kadizhar wrote:

I have the following line:

remoteMD5hash = Socket.recv(256)

But what I really want is remoteMD5hash.strip()

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

remoteKD5hash = string.strip(Socket.recv(256)) ?


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 receiveAllDataFromSocket(sock):
# insert fancy logic here... check the archives or examples
# on, say, the Python Cookbook site to learn how

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

# logic in your own code
data = receiveAllDataFromSocket(sock)
remoteKD5hash = cleanMd5Hash(data)

....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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by David MacQuigg | last post: by
15 posts views Thread by Christopher Benson-Manica | last post: by
33 posts views Thread by amerar | last post: by
1 post views Thread by amerar | last post: by
83 posts views Thread by rahul8143 | last post: by
39 posts views Thread by jamilur_rahman | last post: by
18 posts views Thread by pocmatos | last post: by
6 posts views Thread by MatthewS | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.