Conditions | 9 |
Total Lines | 65 |
Lines | 0 |
Ratio | 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 | #!/usr/bin/env python |
||
24 | def main(args=None): |
||
25 | """Entry point for `fuel-convert` script. |
||
26 | |||
27 | This function can also be imported and used from Python. |
||
28 | |||
29 | Parameters |
||
30 | ---------- |
||
31 | args : iterable, optional (default: None) |
||
32 | A list of arguments that will be passed to Fuel's conversion |
||
33 | utility. If this argument is not specified, `sys.argv[1:]` will |
||
34 | be used. |
||
35 | |||
36 | """ |
||
37 | built_in_datasets = dict(converters.all_converters) |
||
38 | if fuel.config.extra_converters: |
||
39 | for name in fuel.config.extra_converters: |
||
40 | extra_datasets = dict( |
||
41 | importlib.import_module(name).all_converters) |
||
42 | if any(key in built_in_datasets for key in extra_datasets.keys()): |
||
43 | raise ValueError('extra converters conflict in name with ' |
||
44 | 'built-in converters') |
||
45 | built_in_datasets.update(extra_datasets) |
||
46 | parser = argparse.ArgumentParser( |
||
47 | description='Conversion script for built-in datasets.') |
||
48 | subparsers = parser.add_subparsers() |
||
49 | parent_parser = argparse.ArgumentParser(add_help=False) |
||
50 | parent_parser.add_argument( |
||
51 | "-d", "--directory", help="directory in which input files reside", |
||
52 | type=str, default=os.getcwd()) |
||
53 | convert_functions = {} |
||
54 | for name, fill_subparser in built_in_datasets.items(): |
||
55 | subparser = subparsers.add_parser( |
||
56 | name, parents=[parent_parser], |
||
57 | help='Convert the {} dataset'.format(name)) |
||
58 | subparser.add_argument( |
||
59 | "-o", "--output-directory", help="where to save the dataset", |
||
60 | type=str, default=os.getcwd(), action=CheckDirectoryAction) |
||
61 | # Allows the parser to know which subparser was called. |
||
62 | subparser.set_defaults(which_=name) |
||
63 | convert_functions[name] = fill_subparser(subparser) |
||
64 | |||
65 | args = parser.parse_args(args) |
||
66 | args_dict = vars(args) |
||
67 | convert_function = convert_functions[args_dict.pop('which_')] |
||
68 | try: |
||
69 | output_paths = convert_function(**args_dict) |
||
70 | except MissingInputFiles as e: |
||
71 | intro = "The following required files were not found:\n" |
||
72 | message = "\n".join([intro] + [" * " + f for f in e.filenames]) |
||
73 | message += "\n\nDid you forget to run fuel-download?" |
||
74 | parser.error(message) |
||
75 | |||
76 | # Tag the newly-created file(s) with H5PYDataset version and command-line |
||
77 | # options |
||
78 | for output_path in output_paths: |
||
79 | h5file = h5py.File(output_path, 'a') |
||
80 | interface_version = H5PYDataset.interface_version.encode('utf-8') |
||
81 | h5file.attrs['h5py_interface_version'] = interface_version |
||
82 | fuel_convert_version = converters.__version__.encode('utf-8') |
||
83 | h5file.attrs['fuel_convert_version'] = fuel_convert_version |
||
84 | command = [os.path.basename(sys.argv[0])] + sys.argv[1:] |
||
85 | h5file.attrs['fuel_convert_command'] = ( |
||
86 | ' '.join(command).encode('utf-8')) |
||
87 | h5file.flush() |
||
88 | h5file.close() |
||
89 | |||
93 |