473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1394
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
33
by: amerar | last post by:
Hi All, I can make a page using a style sheet, no problem there. However, if I make an email and send it out to my list, Yahoo & Hotmail totally ignore the style tags. It looks fine in...
1
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I...
4
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
83
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
39
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A...
18
by: pocmatos | last post by:
Hi all, While I was programming 5 minutes ago a recurring issue came up and this time I'd like to hear some opinions on style. Although they are usually personal I do think that in this case as...
3
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It...
6
by: MatthewS | last post by:
I've seen the question raised several times here, but apparently never answered. Since PyInstance_Check returns False for new-style class instances, is there a standard procedure for testing this...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.