Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Albert-Jan Roskam
> This is all fun, but what about the context?  Your original function > took an integer, not a string, and thus wasn't charged with measuring > string length, possibly multiple times.  Even so, each of these tests is > taking around a microsecond.  So are you expecting to do anything with > t

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Dave Angel
On 10/05/2012 07:23 AM, Albert-Jan Roskam wrote: > - Original Message - > >> From: Asokan Pichai >> To: tutor@python.org >> Cc: >> Sent: Friday, October 5, 2012 11:06 AM >> Subject: Re: [Tutor] rounding up to the nearest multiple of 8 >> >>

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Albert-Jan Roskam
- Original Message - > From: Asokan Pichai > To: tutor@python.org > Cc: > Sent: Friday, October 5, 2012 11:06 AM > Subject: Re: [Tutor] rounding up to the nearest multiple of 8 > > If you are doing so many times, it may be worth precomputing the padding for a >

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread eryksun
On Fri, Oct 5, 2012 at 4:24 AM, Albert-Jan Roskam wrote: > > import timeit > ver1 = timeit.timeit(""" > import math > value = "1234" > value = "%-*s" % (int(math.ceil(len(value)/8.0)*8), value) > """) > ver2 = timeit.timeit(""" > value = "1234" > value = value.ljust( len(value) + (-len(value) % 8)

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Asokan Pichai
If you are doing so many times, it may be worth precomputing the padding for a given length and adding it by a look up on the length. For example: SPACE = ' ' MAX = 1000 TAB = 8 paddding = [ SPACE * (n % TAB) for n in range(MAX) ] . s = padding[len(s)] + s . -

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread eryksun
On Fri, Oct 5, 2012 at 2:54 AM, Steven D'Aprano wrote: > > py> from __future__ import division > py> from math import ceil > py> "%*s" % (int(ceil(len(mystring)/8)*8), mystring) > '123412341234' > > > Or left-justified: > > py> "%-*s" % (int(ceil(len(mystring)/8)*8), mystring) > '123412341234

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-05 Thread Albert-Jan Roskam
- Original Message - > From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Friday, October 5, 2012 8:54 AM > Subject: Re: [Tutor] rounding up to the nearest multiple of 8 > > On Thu, Oct 04, 2012 at 05:26:13PM -0400, eryksun wrote: >> On T

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread Steven D'Aprano
On Thu, Oct 04, 2012 at 05:26:13PM -0400, eryksun wrote: > On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick > wrote: > > > my_string = "123" > pad = 8 - len(my_string) % 8 > my_string = my_string + " " * pad > my_string > > '123 ' > > If len(my_string) is already a multiple

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread eryksun
On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick wrote: > my_string = "123" pad = 8 - len(my_string) % 8 my_string = my_string + " " * pad my_string > '123 ' If len(my_string) is already a multiple of 8, the above sets pad to 8: >>> s = "12345678" >>> pad = 8 - len(

Re: [Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread Joel Goldstick
On Thu, Oct 4, 2012 at 3:47 PM, Albert-Jan Roskam wrote: > Hi, > > The function below works, but it's such a kludge! Is there a way to make this > more elegant? > As said in the docstring, I want to round up a given integer to the nearest > multiple of 8. I was thinking > of something like math.

[Tutor] rounding up to the nearest multiple of 8

2012-10-04 Thread Albert-Jan Roskam
Hi,   The function below works, but it's such a kludge! Is there a way to make this more elegant? As said in the docstring, I want to round up a given integer to the nearest multiple of 8. I was thinking of something like math.ceil.   def _getPadding(charlen):     """ Helper function to replace n