| Conditions | 11 |
| Total Lines | 70 |
| Code Lines | 63 |
| 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:
Complex classes like tabpy.tabpy_server.handlers.arrow_client.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the Apache Software Foundation (ASF) under one |
||
| 127 | def main(): |
||
| 128 | parser = argparse.ArgumentParser() |
||
| 129 | subcommands = parser.add_subparsers() |
||
| 130 | |||
| 131 | cmd_list = subcommands.add_parser('list') |
||
| 132 | cmd_list.set_defaults(action='list') |
||
| 133 | _add_common_arguments(cmd_list) |
||
| 134 | cmd_list.add_argument('-l', '--list', action='store_true', |
||
| 135 | help="Print more details.") |
||
| 136 | |||
| 137 | cmd_do = subcommands.add_parser('do') |
||
| 138 | cmd_do.set_defaults(action='do') |
||
| 139 | _add_common_arguments(cmd_do) |
||
| 140 | cmd_do.add_argument('action_type', type=str, |
||
| 141 | help="The action type to run.") |
||
| 142 | |||
| 143 | cmd_put = subcommands.add_parser('put') |
||
| 144 | cmd_put.set_defaults(action='put') |
||
| 145 | _add_common_arguments(cmd_put) |
||
| 146 | cmd_put.add_argument('file', type=str, |
||
| 147 | help="CSV file to upload.") |
||
| 148 | |||
| 149 | cmd_get = subcommands.add_parser('get') |
||
| 150 | cmd_get.set_defaults(action='get') |
||
| 151 | _add_common_arguments(cmd_get) |
||
| 152 | cmd_get_descriptor = cmd_get.add_mutually_exclusive_group(required=True) |
||
| 153 | cmd_get_descriptor.add_argument('-p', '--path', type=str, action='append', |
||
| 154 | help="The path for the descriptor.") |
||
| 155 | cmd_get_descriptor.add_argument('-c', '--command', type=str, |
||
| 156 | help="The command for the descriptor.") |
||
| 157 | |||
| 158 | args = parser.parse_args() |
||
| 159 | if not hasattr(args, 'action'): |
||
| 160 | parser.print_help() |
||
| 161 | sys.exit(1) |
||
| 162 | |||
| 163 | commands = { |
||
| 164 | 'list': list_flights, |
||
| 165 | 'do': do_action, |
||
| 166 | 'get': get_flight_by_path, |
||
| 167 | 'put': push_data, |
||
| 168 | } |
||
| 169 | host, port = args.host.split(':') |
||
| 170 | port = int(port) |
||
| 171 | scheme = "grpc+tcp" |
||
| 172 | connection_args = {} |
||
| 173 | if args.tls: |
||
| 174 | scheme = "grpc+tls" |
||
| 175 | if args.tls_roots: |
||
| 176 | with open(args.tls_roots, "rb") as root_certs: |
||
| 177 | connection_args["tls_root_certs"] = root_certs.read() |
||
| 178 | if args.mtls: |
||
| 179 | with open(args.mtls[0], "rb") as cert_file: |
||
| 180 | tls_cert_chain = cert_file.read() |
||
| 181 | with open(args.mtls[1], "rb") as key_file: |
||
| 182 | tls_private_key = key_file.read() |
||
| 183 | connection_args["cert_chain"] = tls_cert_chain |
||
| 184 | connection_args["private_key"] = tls_private_key |
||
| 185 | client = pyarrow.flight.FlightClient(f"{scheme}://{host}:{port}", |
||
| 186 | **connection_args) |
||
| 187 | while True: |
||
| 188 | try: |
||
| 189 | action = pyarrow.flight.Action("healthcheck", b"") |
||
| 190 | options = pyarrow.flight.FlightCallOptions(timeout=1) |
||
| 191 | list(client.do_action(action, options=options)) |
||
| 192 | break |
||
| 193 | except pyarrow.ArrowIOError as e: |
||
| 194 | if "Deadline" in str(e): |
||
| 195 | print("Server is not ready, waiting...") |
||
| 196 | commands[args.action](args, client, connection_args) |
||
| 197 | |||
| 202 |