Conditions | 1 |
Total Lines | 53 |
Code Lines | 33 |
Lines | 53 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import os |
||
72 | View Code Duplication | def main(): |
|
73 | """ |
||
74 | Example Usage: |
||
75 | |||
76 | $ text-dump append --file-path domains.txt --data "projects.localhost" --split ' ' --unique |
||
77 | $ text-dump write --file-path domains.txt --data "dev.hpadesign.com staging.hpadesign.com beta.hpadesign.com public.localhost" --split ' ' --skip 'localhost' |
||
78 | $ text-dump read --file-path domains.txt |
||
79 | public.localhost" |
||
80 | """ |
||
81 | # Declare argparse argument descriptions |
||
82 | usage = 'Text dump utility.' |
||
83 | description = 'Read, write and append text files.' |
||
84 | helpers = { |
||
85 | 'file-path': "Path to text file to read/write to.", |
||
86 | 'data': "Data to write/append to the text file.", |
||
87 | 'split': "Character used separate a plain text list.", |
||
88 | 'unique': "Only write unique values to the text file.", |
||
89 | 'skip': "Skip writing a datapoint if the 'skip string' is found.", |
||
90 | 'return-type': "Type to return data in.", |
||
91 | } |
||
92 | |||
93 | # construct the argument parse and parse the arguments |
||
94 | parser = ArgumentParser(usage=usage, description=description) |
||
95 | sub_parser = parser.add_subparsers() |
||
96 | |||
97 | # Read |
||
98 | parser_read = sub_parser.add_parser('read') |
||
99 | parser_read.add_argument('-f', '--file-path', help=helpers['file-path'], type=str) |
||
100 | parser_read.add_argument('-t', '--return-type', help=helpers['return-type'], type=str) |
||
101 | parser_read.set_defaults(func=reader) |
||
102 | |||
103 | # Write |
||
104 | parser_write = sub_parser.add_parser('write') |
||
105 | parser_write.add_argument('-f', '--file-path', help=helpers['file-path'], type=str) |
||
106 | parser_write.add_argument('-d', '--data', help=helpers['data']) |
||
107 | parser_write.add_argument('-s', '--split', help=helpers['split'], type=str, default=None) |
||
108 | parser_write.add_argument('-u', '--unique', help=helpers['unique'], action='store_true', default=False) |
||
109 | parser_write.add_argument('--skip', help=helpers['skip'], type=str, default=False) |
||
110 | parser_write.set_defaults(func=writer) |
||
111 | |||
112 | # Append |
||
113 | parser_write = sub_parser.add_parser('append') |
||
114 | parser_write.add_argument('-f', '--file-path', help=helpers['file-path'], type=str) |
||
115 | parser_write.add_argument('-d', '--data', help=helpers['data']) |
||
116 | parser_write.add_argument('-s', '--split', help=helpers['split'], type=str, default=None) |
||
117 | parser_write.add_argument('-u', '--unique', help=helpers['unique'], action='store_true', default=False) |
||
118 | parser_write.add_argument('--skip', help=helpers['skip'], type=str, default=False) |
||
119 | parser_write.set_defaults(func=appender) |
||
120 | |||
121 | # Parse Arguments |
||
122 | args = vars(parser.parse_args()) |
||
123 | func = args.pop('func') |
||
124 | return func(**args) |
||
125 | |||
129 |