>>> key_split('x') 'x' >>> key_split('x-1') 'x' >>> key_split('x-1-2-3') 'x' >>> key_split(('x-2', 1)) 'x' >>> key_split("('x-2', 1)") 'x' >>> key_split("('x', 1)") 'x' >>> key_split('hello-world-1') 'hello-world' >>> key_split(b'hello-wor
(s)
| 1946 | |
| 1947 | @functools.lru_cache(100000) |
| 1948 | def key_split(s): |
| 1949 | """ |
| 1950 | >>> key_split('x') |
| 1951 | 'x' |
| 1952 | >>> key_split('x-1') |
| 1953 | 'x' |
| 1954 | >>> key_split('x-1-2-3') |
| 1955 | 'x' |
| 1956 | >>> key_split(('x-2', 1)) |
| 1957 | 'x' |
| 1958 | >>> key_split("('x-2', 1)") |
| 1959 | 'x' |
| 1960 | >>> key_split("('x', 1)") |
| 1961 | 'x' |
| 1962 | >>> key_split('hello-world-1') |
| 1963 | 'hello-world' |
| 1964 | >>> key_split(b'hello-world-1') |
| 1965 | 'hello-world' |
| 1966 | >>> key_split('ae05086432ca935f6eba409a8ecd4896') |
| 1967 | 'data' |
| 1968 | >>> key_split('<module.submodule.myclass object at 0xdaf372') |
| 1969 | 'myclass' |
| 1970 | >>> key_split(None) |
| 1971 | 'Other' |
| 1972 | >>> key_split('x-abcdefab') # ignores hex |
| 1973 | 'x' |
| 1974 | >>> key_split('_(x)') # strips unpleasant characters |
| 1975 | 'x' |
| 1976 | """ |
| 1977 | # If we convert the key, recurse to utilize LRU cache better |
| 1978 | if type(s) is bytes: |
| 1979 | return key_split(s.decode()) |
| 1980 | if type(s) is tuple: |
| 1981 | return key_split(s[0]) |
| 1982 | try: |
| 1983 | words = s.split("-") |
| 1984 | if not words[0][0].isalpha(): |
| 1985 | result = words[0].split(",")[0].strip("_'()\"") |
| 1986 | else: |
| 1987 | result = words[0] |
| 1988 | for word in words[1:]: |
| 1989 | if word.isalpha() and not ( |
| 1990 | len(word) == 8 and hex_pattern.match(word) is not None |
| 1991 | ): |
| 1992 | result += "-" + word |
| 1993 | else: |
| 1994 | break |
| 1995 | if len(result) == 32 and re.match(r"[a-f0-9]{32}", result): |
| 1996 | return "data" |
| 1997 | else: |
| 1998 | if result[0] == "<": |
| 1999 | result = result.strip("<>").split()[0].split(".")[-1] |
| 2000 | return sys.intern(result) |
| 2001 | except Exception: |
| 2002 | return "Other" |
| 2003 | |
| 2004 | |
| 2005 | def stringify(obj, exclusive: Iterable | None = None): |