Created
October 21, 2011 22:35
-
-
Save josher19/1305168 to your computer and use it in GitHub Desktop.
python script to go from '000' to '999'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readline, rlcompleter | |
readline.parse_and_bind("tab: complete") | |
def zeropad(n,zeros=3): | |
"Pad number n with zeros. Example: zeropad(7,3) == '007'" | |
nstr = str(n) | |
while len(nstr) < zeros: | |
nstr = "0" + nstr | |
return nstr | |
def incr(fn=zeropad,arg=[3]): | |
"Iterator that returns from '001' to '999' and beyond." | |
counter = 0 | |
while True: | |
counter = counter + 1 | |
yield fn(counter,*arg) | |
def _test_zeropad(): | |
assert zeropad(7) == "007" | |
assert zeropad(19) == "019" | |
assert zeropad(999) == "999" | |
assert zeropad(12345) == "12345" | |
assert zeropad(1,5) == "00001" | |
return True | |
def _test_incr(): | |
i = incr() | |
assert i.next() == "001" | |
for x in xrange(1,901): | |
i.next() | |
tested = i.next() | |
assert tested == "902", tested | |
return True | |
__all__ = [incr, zeropad] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment