Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to improve this simple block of code

Reply
Thread Tools

how to improve this simple block of code

 
 
Matt Hammond
Guest
Posts: n/a
 
      01-11-2006
> How about:
>
> if "." in x:
> x, frac = x.split(".")
> frac = frac.rstrip("0")
> if frac:
> x = x + "." + frac


Or simpler still:

if "." in x:
x = x.rstrip("0")
x = x.rstrip(".")


More concise, but slightly less readable IMO:

if "." in x:
x = x.rstrip("0").rstrip(".")

--

| Matt Hammond
| R&D Engineer, BBC Research & Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      01-11-2006
py wrote:
> hanz wrote:
>>x = x.rstrip('0.') # removes trailing zeroes and dots

>
> knew there had to be a way, thanks.


But that's not it.

This is a wonderful opportunity for you to learn about unit testing, and
begin the long process of developing good testing habits. Of all the
ideas posted, I believe only Mark Hammond's would correctly pass the
basic obvious test cases, and I don't think anyone (without actually
having checked with tests) should be saying even his is clearly correct.

import unittest
from yourmodule import stripZeros # or whatever you have

class Tests(unittest.TestCase):
def test01(self):
'check zero-stripper'
for input, expected in [
('', ''),
('0', '0'),
('0.0', '0'),
('0.', '0'),
('000.', '000'),
('10', '10'),
('10.0', '10'),
('foo', 'foo'),
('foo.', 'foo'), # ??
('132.15', '132.15'),
('132.60', '132.6'),
('132.00', '132'),
('132.00000', '132'),
('132.000001', '132.000001'),
# add others to taste
]:
self.assertEquals(expected, stripZeros(input))

unittest.main()


Change the above test cases to match what you really want if they're not
correct, then run the script and make sure everything passes.

-Peter

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-11-2006
Peter Hansen wrote:

> Of*all*the ideas posted, I believe only Mark Hammond's would correctly
> pass the basic obvious test cases


Too bad he didn't post at all

Peter

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-11-2006
Peter Otten wrote:
> Peter Hansen wrote:
>>Of all the ideas posted, I believe only Mark Hammond's would correctly
>>pass the basic obvious test cases

>
> Too bad he didn't post at all


D'oh! There was a typo in my message above. Naturally, I meant to
write "M. Hammond" instead of "Mark Hammond".

Sorry Matt!

-Peter

 
Reply With Quote
 
Mel Wilson
Guest
Posts: n/a
 
      01-11-2006
py wrote:
> Say I have...
> x = "132.00"
>
> but I'd like to display it to be "132" ...dropping the trailing
> zeros...


print '%g' % (float(x),)

might work.

Mel.

 
Reply With Quote
 
Xavier Morel
Guest
Posts: n/a
 
      01-11-2006
Mel Wilson wrote:
> py wrote:
>> Say I have...
>> x = "132.00"
>>
>> but I'd like to display it to be "132" ...dropping the trailing
>> zeros...

>
> print '%g' % (float(x),)
>
> might work.
>
> Mel.
>

The input is a string, %g expects a float, TypeError exception.
 
Reply With Quote
 
Xavier Morel
Guest
Posts: n/a
 
      01-11-2006
Forget about the previous mail, i just saw you were converting the
string to float beforehand, in which case he would more than likely run
into the good ol' float imprecision issue sooner than later.

Not to mention that %g formats to scientific notation (e.g. exponential
format with the exponent always being a multiple of 3), he'd probably
use "%f".
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-14-2006
On Wed, 11 Jan 2006 05:58:05 -0800, py wrote:

> Say I have...
> x = "132.00"
>
> but I'd like to display it to be "132" ...dropping the trailing
> zeros...I currently try this


Mucking about with the string is one solution. Here is another:

print int(float(x))


> I do it like this because if
> x = "132.15" ...i dont want to modify it. But if
> x = "132.60" ...I want it to become "132.6"


Then you want:

x = float("123.60") # full precision floating point value
r = round(x, 1) # rounded to one decimal place



--
Steven.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
how to improve simple python shell script (to compile list of files) Jari Aalto Python 4 10-15-2005 06:48 PM
* Simple ways to improve photography * Simple Digital Photography 1 07-29-2003 08:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57