473,395 Members | 1,676 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,395 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 1399
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.