Created
June 15, 2012 07:11
-
-
Save isann/2935134 to your computer and use it in GitHub Desktop.
CSV形式でデータを出力する
This file contains 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
# -*- coding: utf-8 -*- | |
import random | |
import time | |
import sys | |
__author__ = 'isann' | |
TYPE_NUMBER = '0' | |
TYPE_ALPNUM = '1' | |
TYPE_DATE = '2' | |
def random_ascii_char(cnt=1): | |
""" | |
this method generate ascii chars. | |
:param cnt: generate num of chars | |
:return: ascii string | |
""" | |
charmap = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', | |
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ] | |
chars = '' | |
for i in range(0, cnt): | |
chars += charmap[random.randint(0, len(charmap) - 1)] | |
return chars | |
class DataModel(): | |
model_type = None | |
digit_min = 0 | |
digit_max = 0 | |
minus_flag = False | |
date_format = '' | |
def __init__(self, model_type=None, digit_min=0, digit_max=0, minus_flag=False, date_format='%Y-%m-%d %H:%M:%S'): | |
self.model_type = model_type | |
self.digit_min = digit_min | |
self.digit_max = digit_max | |
self.minus_flag = minus_flag | |
self.date_format = date_format | |
def create_number(model): | |
ret = '' | |
if model.digit_min == model.digit_max: | |
for i in range(0, model.digit_max): | |
ret += str(random.randint(0, 9)) | |
else: | |
loop = random.randint(model.digit_min, model.digit_max) | |
for i in range(0, loop): | |
ret += str(random.randint(0, 9)) | |
if len(ret) != 0 and model.minus_flag and random.randint(0, 1) is 1: | |
ret = '-' + ret | |
return ret | |
def create_alpnum(model): | |
loop = 0 | |
if model.digit_min == model.digit_max: | |
loop = model.digit_max | |
else: | |
loop = random.randint(model.digit_min, model.digit_max) | |
return random_ascii_char(loop) | |
def create_date(model): | |
current_millisec = int(time.time()) | |
epoch = random.randint(0, current_millisec) | |
time_localtime = time.localtime(epoch) | |
return time.strftime(model.date_format, time_localtime) | |
def process(models): | |
""" | |
:param models: | |
:return: | |
""" | |
global TYPE_NUMBER, TYPE_ALPNUM, TYPE_DATE | |
chars = '' | |
if len(sys.argv) <= 1: | |
return chars | |
else: | |
rows = int(sys.argv[1]) | |
for i in range(0, rows): | |
for model in models: | |
if model.model_type == TYPE_NUMBER: | |
chars += create_number(model) | |
elif model.model_type == TYPE_ALPNUM: | |
chars += create_alpnum(model) | |
elif model.model_type == TYPE_DATE: | |
chars += create_date(model) | |
chars += ',' | |
chars = chars[:-1] + '\n' | |
return chars | |
def main(): | |
global TYPE_NUMBER, TYPE_ALPNUM, TYPE_DATE | |
models = ( | |
DataModel(TYPE_NUMBER, 0, 4, True), # 半角数字で0-4桁までの負の符号付き文字列 | |
DataModel(TYPE_NUMBER, 4, 4, True), # 半角数字で4桁の府の符号付き文字列 | |
DataModel(TYPE_DATE, date_format='%Y%m%d%H%M%S'), # 日付文字列を指定フォーマットで出力 | |
DataModel(TYPE_ALPNUM, 1, 12), # 半角英数字で1-12桁までの文字列 | |
) | |
print process(models) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment