# coding: utf-8

from datetime import datetime
from random import randrange
from unipath import Path


def r_name():
    color = ["red", "Blue", "BLACK", "O-ran-ge"]
    tool = ["Hammer", "drill", "Poça", "KNI?FE"]
    r_color = color[randrange(0, len(color))]
    r_tool = tool[randrange(0, len(tool))]
    return '{} {}'.format(r_color, r_tool)


def create_line(i):
    key = '{0:0>6}'.format(i)
    return '{},{}'.format(r_name(), key)


def create_csv(lines):
    content = map(lambda x: create_line(x + 1), range(0, lines))
    return '\n'.join(content)


def filesize(size):
    sizes = {
        9: 'Gb',
        6: 'Mb',
        3: 'Kb',
        0: 'bytes'
    }
    for i in [9, 6, 3, 0]:
        if size >= 10 ** i:
            return '{:.1f}'.format(size / (10.0 ** i)) + sizes[i]
    return '0 %s' % sizes[0]

date_time = datetime.now()
filename = 'sample-{}.csv'.format(date_time.strftime('%Y%m%d%H%M%S'))
handler = Path(filename)
lines = randrange(50 * 10 ** 3, 150 * 10 ** 3)
content = create_csv(lines)
handler.write_file(content)
size = filesize(handler.size())

print '==> {} was created with {} lines ({})'.format(filename, lines, size)

@@ -1,118 +1,143 @@
-# coding: utf-8%0A%0Afrom datetime import datetime%0Afrom random import randrange%0Afrom unipath import Path%0A%0A%0Adef r_name(
+from argparse import ArgumentParser%0Afrom csv import writer%0Afrom os import path%0Afrom random import choice, randrange%0A%0A%0Adef random_line(index
 ):%0A 
@@ -148,45 +148,46 @@
 olor
+s
  = 
-%5B%22red%22, %22
+('red', '
 Blue
-%22, %22
+', '
 BLACK
-%22, %22
+', '
 O-ran-ge
 %22%5D%0A 
@@ -182,18 +182,18 @@
 O-ran-ge
-%22%5D
+')
 %0A    too
@@ -197,179 +197,141 @@
 tool
+s
  = 
-%5B%22
+('
 Hammer
-%22, %22
+', '
 drill
-%22, %22
+', '
 Po%C3%A7a
-%22, %22
+', '
 KNI?FE
-%22%5D
+')
 %0A    
-r_color = color%5Brandrange(0, len(color))%5D%0A    r_tool = tool%5Brandrange(0, len(tool))%5D%0A    return '%7B%7D %7B%7D'.format(r_
+index = str(index + 1).zfill(6)%0A    return index, ' '.join(map(choice, (
 color
+s
 , 
-r_
 tool
+s))
 )%0A%0A%0A
@@ -345,206 +345,195 @@
 ate_
+csv(file_path, 
 line
-(i
+s
 ):%0A    
-key = '%7B0:0%3E6%7D'.format(i)%0A    return '%7B%7D,%7B%7D'.format(r_name(), key)%0A%0A%0Adef create_csv(lines):%0A    content = map(lambda x: create_line(x + 1),
+with open(file_path, 'w') as handler:%0A        csv_writer = writer(handler)%0A        for index in
  range(
-0, 
 lines)
-)
+:
 %0A    
-return '%5Cn'.join(content
+        csv_writer.writerow(random_line(index)
 )%0A%0A%0A
@@ -569,33 +569,16 @@
  = %7B
-%0A        9: 'Gb',%0A       
+9: 'Gb',
  6: 
@@ -582,24 +582,16 @@
 6: 'Mb',
-%0A       
  3: 'Kb'
@@ -591,24 +591,16 @@
 3: 'Kb',
-%0A       
  0: 'byt
@@ -602,21 +602,16 @@
  'bytes'
-%0A    
 %7D%0A    fo
@@ -616,25 +616,33 @@
 for 
-i in %5B9, 6, 3, 0%5D
+power in range(9, -1, -3)
 :%0A  
@@ -664,17 +664,21 @@
 = 10 ** 
-i
+power
 :%0A      
@@ -687,61 +687,83 @@
     
-return '%7B:.1f%7D'.format(size / (10.0 ** i)) + sizes%5Bi%5D
+size = size // 10 ** power%0A            return f'%7Bsize:,%7D%7Bsizes.get(power)%7D'
 %0A   
@@ -774,156 +774,361 @@
 urn 
+f
 '0
- %25s' %25 sizes%5B0%5D%0A%0Adate_time = datetime.now()%0Afilename = 'sample-%7B%7D.csv'.format(date_time.strftime('%25Y%25m%25d%25H%25M%25S'))%0Ahandler = Path(filename)%0Alines =
+%7Bsizes.get(0)%7D'%0A%0A%0Aif __name__ == '__main__':%0A%0A    parser = ArgumentParser(description='A tool to create CSV samples')%0A    parser.add_argument('filename')%0A    parser.add_argument('-l', '--lines')%0A%0A    args = parser.parse_args()%0A    if path.exists(args.filename):%0A        raise RuntimeError('%7Bargs.filename%7D already exists.')%0A%0A    lines = args.lines or
  ran
@@ -1140,23 +1140,14 @@
 e(50
- * 10 ** 3
 , 150
+)
  * 1
@@ -1156,19 +1156,12 @@
 ** 3
-)%0Acontent =
+%0A   
  cre
@@ -1172,88 +1172,108 @@
 csv(
-lines)%0Ahandler.write_file(content)%0Asize = filesize(handler.size())%0A%0Aprint '==%3E %7B
+args.filename, lines)%0A    size = filesize(path.getsize(args.filename))%0A    print(f'%F0%9F%91%8D  %7Bargs.filename
 %7D wa
@@ -1292,48 +1292,30 @@
 th %7B
-%7D 
 lines
- (%7B%7D)'.format(filename,
+:,%7D
  lines
-,
  
+(%7B
 size
+%7D)'
 )%0A

Refactor CSV sample generator