Skip to content

Commit

Permalink
feat: sort enum member aliases to the end
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Jun 2, 2024
1 parent 7c77a1f commit dcb2c27
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions hearthstone/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2492,19 +2492,31 @@ def as_of_date(self, date=None):
if __name__ == "__main__":
import json
import sys

all_enums = {
k: dict(v.__members__) for k, v in globals().items() if ( # type:ignore
isinstance(v, type) and issubclass(v, IntEnum) and k != "IntEnum"
)
}
from collections import OrderedDict

def get_enum_key(enum, name):
val = enum[name].value
canonical_name = enum[name].name
if canonical_name == name:
return -100_000 + val
return val

# Consistently sort, but keep aliases at the end
all_enums = OrderedDict(sorted(
[
(
k, OrderedDict(sorted(v.__members__.items(), key=lambda x: get_enum_key(v, x[0])))
) for k, v in globals().items()
if isinstance(v, type) and issubclass(v, IntEnum) and k != "IntEnum"
],
key=lambda x: x[0]
))

def _print_enums(enums, format):
ret = []
linefmt = "\t%s = %i,"
for enum in sorted(enums):
sorted_pairs = sorted(enums[enum].items(), key=lambda k: k[1])
lines = "\n".join(linefmt % (name, value) for name, value in sorted_pairs)
for enum in enums:
lines = "\n".join(linefmt % (name, value) for name, value in enums[enum].items())
ret.append(format % (enum, lines))
print("\n\n".join(ret))

Expand All @@ -2518,4 +2530,4 @@ def _print_enums(enums, format):
elif format == "--cs":
_print_enums(all_enums, "public enum %s {\n%s\n}")
else:
print(json.dumps(dict(sorted(all_enums.items(), key=lambda x: x[0])), sort_keys=False))
print(json.dumps(all_enums, sort_keys=False))

0 comments on commit dcb2c27

Please sign in to comment.