|
3 | 3 | Common functions for interfacing with python primitives and directories/files.
|
4 | 4 | """
|
5 | 5 |
|
6 |
| -import ast |
7 | 6 | import contextlib
|
8 | 7 | import errno
|
9 | 8 | import fnmatch
|
|
59 | 58 | "tensors_export",
|
60 | 59 | "json_to_jsonl",
|
61 | 60 | "deprecation_warning",
|
62 |
| - "parse_kwarg_tuples", |
63 | 61 | "is_package_available",
|
64 | 62 | "import_from_path",
|
65 | 63 | "getattr_chain",
|
@@ -882,83 +880,6 @@ def deprecation_warning(message: str):
|
882 | 880 | )
|
883 | 881 |
|
884 | 882 |
|
885 |
| -def parse_kwarg_tuples(kwargs: tuple) -> Dict: |
886 |
| - """ |
887 |
| - Convert a tuple of kwargs to a dict of kwargs. |
888 |
| - This function is used to enable the click parsing of kwargs. |
889 |
| -
|
890 |
| - Example use: |
891 |
| - ``` |
892 |
| - @click.command( |
893 |
| - context_settings=dict( |
894 |
| - ignore_unknown_options=True) |
895 |
| - ) |
896 |
| - @click.argument(...) |
897 |
| - @click.option(...) |
898 |
| - ... |
899 |
| - @click.argument("kwargs", nargs=-1, type=click.UNPROCESSED) |
900 |
| - def main(..., kwargs): |
901 |
| - ... |
902 |
| - kwargs: Dict[str, Any] = parse_kwarg_tuples(kwargs: Tuple) |
903 |
| - ``` |
904 |
| -
|
905 |
| - Example inputs, outputs: |
906 |
| - ``` |
907 |
| - input = ('--arg1', 1, 'arg2', 2, '-arg3', 3) |
908 |
| - output = parse_kwarg_tuples(input) |
909 |
| - output = {'arg1': 1, 'arg2': 2, 'arg3': 3} |
910 |
| - ``` |
911 |
| -
|
912 |
| - ``` |
913 |
| - input = ('--arg1', 1, '--args1', 2 , 'arg2', 2, '-arg3', 3) |
914 |
| - output = parse_kwarg_tuples(input) |
915 |
| - output = {'arg1': [1, 2], 'arg2': 2, 'arg3': 3} |
916 |
| - ``` |
917 |
| -
|
918 |
| - :param kwargs: The kwargs to convert. Should be a tuple of alternating |
919 |
| - kwargs names and kwargs values e.g.('--arg1', 1, 'arg2', 2, -arg3', 3). |
920 |
| - The names can optionally have a '-' or `--` in front of them. |
921 |
| - :return: The converted kwargs as a dict. |
922 |
| - """ |
923 |
| - if len(kwargs) == 0: |
924 |
| - return {} |
925 |
| - if len(kwargs) % 2 != 0: |
926 |
| - raise ValueError( |
927 |
| - "kwargs must be a tuple of alternating names and values " |
928 |
| - "i.e. the length of kwargs tuple must be even. Received " |
929 |
| - f"kwargs: {kwargs}" |
930 |
| - ) |
931 |
| - # names are uneven indices, values are even indices |
932 |
| - kwargs_names = kwargs[0::2] |
933 |
| - kwargs_values = kwargs[1::2] |
934 |
| - # by default kwargs values are strings, so convert them |
935 |
| - # to the appropriate type if possible |
936 |
| - kwargs_values = list(kwargs_values) |
937 |
| - for i, value in enumerate(kwargs_values): |
938 |
| - try: |
939 |
| - kwargs_values[i] = ast.literal_eval(value) |
940 |
| - except Exception as e: # noqa E841 |
941 |
| - logger.debug( |
942 |
| - f"Failed to infer non-string type" |
943 |
| - f"from kwarg value: {value}. It will" |
944 |
| - f"be left as a string." |
945 |
| - ) |
946 |
| - pass |
947 |
| - # remove any '-' or '--' from the names |
948 |
| - kwargs_names = [name.lstrip("-") for name in kwargs_names] |
949 |
| - processed_kwargs = {} |
950 |
| - for kwarg_name, kwarg_value in zip(kwargs_names, kwargs_values): |
951 |
| - if kwarg_name in processed_kwargs: |
952 |
| - # if the kwarg name is already in the processed kwargs, |
953 |
| - # then we should convert the value to a list |
954 |
| - if not isinstance(processed_kwargs[kwarg_name], list): |
955 |
| - processed_kwargs[kwarg_name] = [processed_kwargs[kwarg_name]] |
956 |
| - processed_kwargs[kwarg_name].append(kwarg_value) |
957 |
| - else: |
958 |
| - processed_kwargs[kwarg_name] = kwarg_value |
959 |
| - return processed_kwargs |
960 |
| - |
961 |
| - |
962 | 883 | def is_package_available(
|
963 | 884 | package_name: str,
|
964 | 885 | return_version: bool = False,
|
|
0 commit comments