| Conditions | 9 |
| Total Lines | 59 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| 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 | #!/usr/bin/env python |
||
| 90 | @cli.command(short_help="View command map graph") |
||
| 91 | @click.option( |
||
| 92 | "--xdot", help="Use xdot for nicer displaying", is_flag=True, default=False |
||
| 93 | ) |
||
| 94 | def cmdmap(xdot): |
||
| 95 | """Generates a command map""" |
||
| 96 | # TODO: Integrate the output into documentation |
||
| 97 | |||
| 98 | from copy import copy |
||
| 99 | |||
| 100 | def generate_command_graph(command, map_output, groups=None, depth=0): |
||
| 101 | """Generate a strict digraph (as indented representation) of all known |
||
| 102 | subgroups and commands""" |
||
| 103 | |||
| 104 | if groups is None: |
||
| 105 | groups = [] |
||
| 106 | if "commands" in command.__dict__: |
||
| 107 | if len(groups) > 0: |
||
| 108 | if xdot: |
||
| 109 | line = ' "%s" -> "%s" [weight=1.0];\n' % ( |
||
| 110 | groups[-1], |
||
| 111 | command.name, |
||
| 112 | ) |
||
| 113 | else: |
||
| 114 | line = " " * (depth - 1) + "%s %s\n" % (groups[-1], command.name) |
||
| 115 | map_output.append(line) |
||
| 116 | |||
| 117 | for item in command.commands.values(): |
||
| 118 | subgroups = copy(groups) |
||
| 119 | subgroups.append(command.name) |
||
| 120 | generate_command_graph(item, map_output, subgroups, depth + 1) |
||
| 121 | else: |
||
| 122 | if xdot: |
||
| 123 | line = ' "%s" -> "%s" [weight=%1.1f];\n' % ( |
||
| 124 | groups[-1], |
||
| 125 | command.name, |
||
| 126 | len(groups), |
||
| 127 | ) |
||
| 128 | else: |
||
| 129 | line = " " * (len(groups) - 3 + depth) + "%s %s\n" % ( |
||
| 130 | groups[-1], |
||
| 131 | command.name, |
||
| 132 | ) |
||
| 133 | map_output.append(line) |
||
| 134 | |||
| 135 | output = [] |
||
| 136 | generate_command_graph(cli, output) |
||
| 137 | |||
| 138 | output = [line.replace("cli", "isomer") for line in output] |
||
| 139 | |||
| 140 | if xdot: |
||
| 141 | with open("iso-tool.dot", "w") as f: |
||
| 142 | f.write("strict digraph {\n") |
||
| 143 | f.writelines(sorted(output)) |
||
| 144 | f.write("}") |
||
| 145 | |||
| 146 | run_process(".", ["xdot", "iso-tool.dot"]) |
||
| 147 | else: |
||
| 148 | print("".join(output)) |
||
| 149 |