typeddfs.utils.sort_utils

Tools for sorting.

Module Contents

class typeddfs.utils.sort_utils.SortUtils
classmethod _ns_info_from_int_flag(cls, val: int) NatsortFlagsAndValue
classmethod all_natsort_flags(cls) Mapping[str, int]

Returns all flags defined by natsort, including combined and default flags. Combined flags are, e.g., ns_enum.ns.REAL ns_enum.nsFLOAT | ns_enum.ns.SIGNED.. Default flags are, e.g., ns_enum.ns.UNSIGNED.

See also

std_natsort_flags()

Returns

A mapping from flag name to int value

classmethod core_natsort_flags(cls) Mapping[str, int]

Returns natsort flags that are not combinations or defaults.

Returns

A mapping from flag name to int value

classmethod exact_natsort_alg(cls, flags: Union[None, int, Collection[Union[int, str]]]) NatsortFlagsAndValue

Gets the flag names and combined alg= argument for natsort.

Examples

  • exact_natsort_alg({"REAL"}) == ({"FLOAT", "SIGNED"}, ns.FLOAT | ns.SIGNED)

  • exact_natsort_alg({}) == ({}, 0)

  • exact_natsort_alg(ns.LOWERCASEFIRST) == ({"LOWERCASEFIRST"}, ns.LOWERCASEFIRST)

  • exact_natsort_alg({"localenum", "numafter"}) == ({"LOCALENUM", "NUMAFTER"}, ns.LOCALENUM | ns.NUMAFTER)

Parameters

flags – Can be either: - a single integer alg argument - a set of flag ints and/or names in natsort.ns

Returns

A tuple of the set of flag names, and the corresponding input to natsorted Only uses standard flag names, never the β€œcombined” ones. (E.g. exact_natsort_alg({"REAL"}) will return ({"FLOAT", "SIGNED"}, ns.FLOAT | ns.SIGNED).

classmethod guess_natsort_alg(cls, dtype: Type[Any]) NatsortFlagsAndValue

Guesses a good natsorted flag for the dtype.

Here are some specifics:
  • integers β‡’ INT and SIGNED

  • floating-point β‡’ FLOAT and SIGNED

  • strings β‡’ COMPATIBILITYNORMALIZE and GROUPLETTERS

  • datetime β‡’ GROUPLETTERS (only affects β€˜Z’ vs. β€˜z’; shouldn’t matter)

Parameters

dtype – Probably from pd.Series.dtype

Returns

A tuple of (set of flags, int) – see exact_natsort_alg()

classmethod natsort(cls, lst: Iterable[T], dtype: Type[T], *, alg: Union[None, int, Set[str]] = None, reverse: bool = False) Sequence[T]

Perform a natural sort consistent with the type dtype. Uses natsort.

Parameters
  • lst – A sequence of things to sort

  • dtype – The type; must be a subclass of each element in lst

  • alg – A specific natsort algorithm or set of flags

  • reverse – Sort in reverse (e.g. Z to A or 9 to 1)