From e113584029b3e7680edbf1cbf54ad77b3a2d5a71 Mon Sep 17 00:00:00 2001 From: ohzakka Date: Sat, 17 Aug 2019 21:45:00 +0900 Subject: [PATCH 1/3] add maybe_useless code --- maybe_useless.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 maybe_useless.py diff --git a/maybe_useless.py b/maybe_useless.py new file mode 100644 index 0000000..63e981c --- /dev/null +++ b/maybe_useless.py @@ -0,0 +1,103 @@ +from copy import deepcopy, copy +import random +import sys +import traceback + + +class CommandError(SyntaxError): pass + + +def print_help(): + print(''' +Usage: python shuffle_me.py [option] +--unit -u\tchar | line +\tchar\tshuffle or sort each characters +\tline\tshuffle or sort each lines +--type -t\tsort | shuffle +\tsort\tremake source to sorting with unicode(utf-8) +\tshuffle\tremake source to randomly shuffle +--help -h\tShow usage message + ''') + + +def get_conf(): + args = copy(sys.argv) + options = {'this_file': args[0]} + + if '--unit' not in args or '-u' not in args: + options['unit'] = 'char' + if '--type' not in args or '-t' not in args: + options['type'] = 'shuffle' + + for idx, arg in enumerate(args): + if 'unit' not in options.keys() and arg == '--unit' or arg == '-u': + options['unit'] = args[idx + 1] + elif 'type' not in options.keys() and arg == '--type' or arg == '-t': + options['type'] = args[idx + 1] + elif arg == '--help' or arg == '-h': + print_help() + exit(0) + + return options + + +def read_code(filename): + rp = open(filename, 'rb') + org_code = deepcopy(rp.read()).decode('utf-8') + rp.close() + + return org_code + + +def write_code(new_code, filename): + new_code = ''.join(new_code).encode('utf-8') + wp = open(filename, 'wb') + wp.write(new_code) + wp.close() + + +def make_sequence_from_source(org_source, unit): + if unit == 'char': + org_source = list(org_source) + elif unit == 'line': + sep = '\r\n' if '\r\n' in org_source else '\n' + org_source = org_source.split(sep) + else: + raise CommandError('Wrong command!!') + + return org_source + + +def do_func(org_seq_source, func): + processed_code = copy(org_seq_source) + if func == 'sort': + processed_code.sort() + elif func == 'shuffle': + random.shuffle(processed_code) + else: + raise CommandError('Wrong command!!') + + return processed_code + + +def main(): + # 명령절에서 옵션을 받아온다. + config = get_conf() + # 자신의 소스코드를 읽어온다. + code = read_code(config['this_file']) + # 읽어온 소스코드를 주어진 단위로 쪼갠다. + code = make_sequence_from_source(code, config['unit']) + # 만들어진 코드 리스트 단위를 주어진 방법으로 바꾼다. + code = do_func(code, config['type']) + # 다시 자신의 소스코드에 덮어씌운다. + write_code(code, config['this_file']) + + +if __name__ == "__main__": + try: + main() + except CommandError as e: + print(str(e)) + print_help() + except Exception as e: + traceback.print_tb(e) From 2e4b41c1af726d4344c3d21d14df83dc043c252c Mon Sep 17 00:00:00 2001 From: Jeffrey Oh Date: Sat, 17 Aug 2019 21:57:18 +0900 Subject: [PATCH 2/3] Rename maybe_useless.py to shuffle_me.py --- maybe_useless.py => shuffle_me.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maybe_useless.py => shuffle_me.py (100%) diff --git a/maybe_useless.py b/shuffle_me.py similarity index 100% rename from maybe_useless.py rename to shuffle_me.py From 8b64d29f1e47bb3b8269fd0f916b43f978ab87ee Mon Sep 17 00:00:00 2001 From: ohzakka Date: Sat, 17 Aug 2019 22:13:38 +0900 Subject: [PATCH 3/3] fix: support line separate symbols and add comments --- maybe_useless.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/maybe_useless.py b/maybe_useless.py index 63e981c..608efbf 100644 --- a/maybe_useless.py +++ b/maybe_useless.py @@ -2,6 +2,7 @@ import random import sys import traceback +import os class CommandError(SyntaxError): pass @@ -49,8 +50,14 @@ def read_code(filename): return org_code -def write_code(new_code, filename): - new_code = ''.join(new_code).encode('utf-8') +def write_code(new_code, filename, unit): + if unit == 'line': + # If unit is line, new_code's elements does not have new line symbol + join_token = os.linesep + else: + # If unit is char, new line symbol is element of new_code + join_token = '' + new_code = join_token.join(new_code).encode('utf-8') wp = open(filename, 'wb') wp.write(new_code) wp.close() @@ -60,7 +67,18 @@ def make_sequence_from_source(org_source, unit): if unit == 'char': org_source = list(org_source) elif unit == 'line': - sep = '\r\n' if '\r\n' in org_source else '\n' + if '\r\n' in org_source: + # For windows or ms-dos + sep = '\r\n' + elif '\n' in org_source: + # For linux ox + sep = '\n' + elif '\r' in org_source: + # For unix or osx or somethings.. + sep = '\r' + else: + # I don't know what did I support any new line symbol + raise SyntaxError('Not supported new line symbol.') org_source = org_source.split(sep) else: raise CommandError('Wrong command!!') @@ -90,7 +108,7 @@ def main(): # 만들어진 코드 리스트 단위를 주어진 방법으로 바꾼다. code = do_func(code, config['type']) # 다시 자신의 소스코드에 덮어씌운다. - write_code(code, config['this_file']) + write_code(code, config['this_file'], config['unit']) if __name__ == "__main__":