[Checkins] SVN: z3c.gibberish/trunk/z3c/gibberish/ Optimize some
loops.
Ross Patterson
me at rpatterson.net
Mon Dec 17 04:12:29 EST 2007
Log message for revision 82307:
Optimize some loops.
Test random ranges including zero.
Fix a bug when randomly accessing the end of the dictionary.
Changed:
U z3c.gibberish/trunk/z3c/gibberish/README.txt
U z3c.gibberish/trunk/z3c/gibberish/__init__.py
-=-
Modified: z3c.gibberish/trunk/z3c/gibberish/README.txt
===================================================================
--- z3c.gibberish/trunk/z3c/gibberish/README.txt 2007-12-17 04:04:30 UTC (rev 82306)
+++ z3c.gibberish/trunk/z3c/gibberish/README.txt 2007-12-17 09:12:28 UTC (rev 82307)
@@ -34,8 +34,8 @@
Make a simple file with one line and one column containing one word::
- >>> import StringIO, csv
- >>> result = list(csv.reader(StringIO.StringIO(
+ >>> import cStringIO, csv
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
... system(gibberish+' 1 1'))))
>>> len(result)
1
@@ -51,8 +51,7 @@
With two words in the column::
- >>> import StringIO, csv
- >>> result = list(csv.reader(StringIO.StringIO(
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
... system(gibberish+' 1 2'))))
>>> len(result)
1
@@ -63,8 +62,7 @@
With a random number of words in the column::
- >>> import StringIO, csv
- >>> result = list(csv.reader(StringIO.StringIO(
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
... system(gibberish+' 1 1-10'))))
>>> len(result)
1
@@ -75,8 +73,7 @@
With 10 lines::
- >>> import StringIO, csv
- >>> result = list(csv.reader(StringIO.StringIO(
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
... system(gibberish+' 10 2'))))
>>> len(result)
10
@@ -87,8 +84,7 @@
With a random number of lines::
- >>> import StringIO, csv
- >>> result = list(csv.reader(StringIO.StringIO(
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
... system(gibberish+' 1-10 2'))))
>>> 1 <= len(result) <= 10
True
@@ -99,8 +95,7 @@
With two columns::
- >>> import StringIO, csv
- >>> result = list(csv.reader(StringIO.StringIO(
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
... system(gibberish+' 1 2 3'))))
>>> len(result)
1
@@ -110,3 +105,26 @@
2
>>> len(result[0][1].split())
3
+
+With a random number of words including zero in the column::
+
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
+ ... system(gibberish+' 1 0-1'))))
+ >>> len(result)
+ 1
+ >>> len(result[0])
+ 1
+ >>> len(result[0][0].split()) in (0, 1)
+ True
+
+With a small dictionary to test exhausting the dictionary::
+
+ >>> import tempfile
+ >>> _, tmp_path = tempfile.mkstemp()
+ >>> tmp = file(tmp_path, 'w')
+ >>> tmp.write('foo')
+ >>> tmp.close()
+ >>> result = tuple(csv.reader(cStringIO.StringIO(
+ ... system(gibberish+' -w %s 1 1' % tmp_path))))
+ >>> result
+ (['foo'],)
Modified: z3c.gibberish/trunk/z3c/gibberish/__init__.py
===================================================================
--- z3c.gibberish/trunk/z3c/gibberish/__init__.py 2007-12-17 04:04:30 UTC (rev 82306)
+++ z3c.gibberish/trunk/z3c/gibberish/__init__.py 2007-12-17 09:12:28 UTC (rev 82307)
@@ -1,4 +1,6 @@
-import sys, os, string, random, optparse, csv, itertools
+import sys, os, string, optparse, csv
+from random import randint
+from itertools import islice
parser = optparse.OptionParser(
usage="usage: %prog [options] LINES COLUMN [COLUMN ...]",
@@ -23,7 +25,7 @@
def get_num(range):
range = [int(i) for i in range.split('-')]
if len(range) == 2:
- return random.randint(*range)
+ return randint(*range)
else:
num, = range
return num
@@ -35,19 +37,19 @@
length += 1
words.seek(0)
+ seek = words.seek
while True:
- yield itertools.islice(
- words, random.randint(0, length), length
- ).next().strip()
- words.seek(0)
+ yield islice(words, randint(0, length-1), None
+ ).next().strip()
+ seek(0)
-def main():
- options, args = parser.parse_args()
+def main(args=sys.argv[1:], out=sys.stdout):
+ options, args = parser.parse_args(args)
lines = get_num(args[0])
columns = args[1:]
words = random_words(options.words)
- csv.writer(sys.stdout).writerows(
- [' '.join(itertools.islice(words, get_num(column)))
- for column in columns]
+ csv.writer(out).writerows(
+ [' '.join(islice(words, get_num(column)))
+ for column in columns]
for _ in xrange(lines))
More information about the Checkins
mailing list