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