From e4af28c4a398cd87378d3833b1d25db0f291ac9e Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 9 Oct 2026 11:35:11 +0500 Subject: upgrade to StrEnum --- pyproject.toml | 3 ++- vk/api.py | 24 ++++++------------------ vk/last_seen.py | 11 ++++------- vk/mutuals.py | 11 ++++------- vk/platform.py | 7 ++----- vk/tracking/db/format.py | 13 +++++-------- vk/tracking/sessions.py | 40 +++++++++++++++++----------------------- vk/user.py | 30 ++++++++++++------------------ 8 files changed, 52 insertions(+), 87 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3a19e1c..44bff40 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,8 @@ license-files = ["LICENSE.txt"] dynamic = ["version"] authors = [{name = "Egor Tensin", email = "egor@tensin.name"}] readme = "README.md" -requires-python = ">=3.6" +requires-python = ">=3.11" +# ^^^ For enum.StrEnum, at least? dependencies = [ 'importlib-metadata ~= 4.0 ; python_version < "3.8"', diff --git a/vk/api.py b/vk/api.py index 8a55711..6b6cae1 100644 --- a/vk/api.py +++ b/vk/api.py @@ -4,7 +4,7 @@ # Distributed under the MIT License. from collections.abc import Iterable, Mapping -from enum import Enum +from enum import auto, StrEnum import json from urllib.error import URLError import urllib.parse @@ -69,39 +69,27 @@ def _join_path(base, url): ACCESS_TOKEN = "3d242bac3d242bac3d242bacd03e66eae033d243d242bac5755894cf6163d769859232d" -class Version(Enum): +class Version(StrEnum): # https://dev.vk.com/en/reference/versions V5_199 = "5.199" DEFAULT = V5_199 - def __str__(self): - return self.value - -class Language(Enum): - EN = "en" +class Language(StrEnum): + EN = auto() DEFAULT = EN - def __str__(self): - return self.value - -class Method(Enum): +class Method(StrEnum): USERS_GET = "users.get" FRIENDS_GET = "friends.get" - def __str__(self): - return self.value - -class CommonParameters(Enum): +class CommonParameters(StrEnum): ACCESS_TOKEN = "access_token" VERSION = "v" LANGUAGE = "lang" - def __str__(self): - return self.value - class API: _ROOT_URL = "https://api.vk.com/method/" diff --git a/vk/last_seen.py b/vk/last_seen.py index 757a5d7..225f107 100644 --- a/vk/last_seen.py +++ b/vk/last_seen.py @@ -6,7 +6,7 @@ from collections import OrderedDict from collections.abc import MutableMapping from datetime import datetime, timezone -from enum import Enum +from enum import auto, StrEnum from numbers import Integral, Real from .platform import Platform @@ -30,12 +30,9 @@ def _parse_platform(x): return Platform(x) -class LastSeenField(Enum): - TIME = "time" - PLATFORM = "platform" - - def __str__(self): - return self.value +class LastSeenField(StrEnum): + TIME = auto() + PLATFORM = auto() class LastSeen(MutableMapping): diff --git a/vk/mutuals.py b/vk/mutuals.py index 5f4adb4..d291b28 100644 --- a/vk/mutuals.py +++ b/vk/mutuals.py @@ -6,7 +6,7 @@ import abc import argparse from collections import OrderedDict -from enum import Enum +from enum import auto, StrEnum import sys from vk.api import API @@ -56,12 +56,9 @@ class OutputSinkJSON(OutputSinkMutualFriends): self._writer.write(friend_list) -class OutputFormat(Enum): - CSV = "csv" - JSON = "json" - - def __str__(self): - return self.value +class OutputFormat(StrEnum): + CSV = auto() + JSON = auto() @staticmethod def open_file(path=None): diff --git a/vk/platform.py b/vk/platform.py index d10d424..389e757 100644 --- a/vk/platform.py +++ b/vk/platform.py @@ -3,10 +3,10 @@ # For details, see https://github.com/egor-tensin/vk-scripts # Distributed under the MIT License. -from enum import Enum +from enum import IntEnum -class Platform(Enum): +class Platform(IntEnum): # https://dev.vk.com/en/reference/objects/user#last_seen MOBILE = 1 IPHONE = 2 @@ -20,9 +20,6 @@ class Platform(Enum): def from_string(s): return Platform(int(s)) - def __str__(self): - return str(self.value) - @property def descr(self): return f"the {_PLATFORM_DESCRIPTIONS[self]}" diff --git a/vk/tracking/db/format.py b/vk/tracking/db/format.py index 7c5ff33..29f5c4c 100644 --- a/vk/tracking/db/format.py +++ b/vk/tracking/db/format.py @@ -3,7 +3,7 @@ # For details, see https://github.com/egor-tensin/vk-scripts # Distributed under the MIT License. -from enum import Enum +from enum import auto, StrEnum import sys from vk.utils import io @@ -11,13 +11,10 @@ from vk.utils import io from . import backend -class Format(Enum): - CSV = "csv" - LOG = "log" - NULL = "null" - - def __str__(self): - return self.value +class Format(StrEnum): + CSV = auto() + LOG = auto() + NULL = auto() def create_writer(self, fd=sys.stdout): if self is Format.CSV: diff --git a/vk/tracking/sessions.py b/vk/tracking/sessions.py index 7f54a14..ef9e8a8 100644 --- a/vk/tracking/sessions.py +++ b/vk/tracking/sessions.py @@ -8,7 +8,7 @@ import argparse from collections import OrderedDict from collections.abc import MutableMapping from datetime import datetime, timedelta, timezone -from enum import Enum +from enum import auto, IntEnum, StrEnum import sys from vk.tracking.db import Format as DatabaseFormat @@ -18,14 +18,14 @@ from vk.utils import io import vk.version -class Weekday(Enum): +class Weekday(IntEnum): MONDAY = 0 - TUESDAY = 1 - WEDNESDAY = 2 - THURSDAY = 3 - FRIDAY = 4 - SATURDAY = 5 - SUNDAY = 6 + TUESDAY = auto() + WEDNESDAY = auto() + THURSDAY = auto() + FRIDAY = auto() + SATURDAY = auto() + SUNDAY = auto() def __str__(self): return self.name[0] + self.name[1:].lower() @@ -143,14 +143,11 @@ class OnlineSessionEnumerator(MutableMapping): return session -class GroupBy(Enum): - USER = "user" - DATE = "date" - WEEKDAY = "weekday" - HOUR = "hour" - - def __str__(self): - return self.value +class GroupBy(StrEnum): + USER = auto() + DATE = auto() + WEEKDAY = auto() + HOUR = auto() def group(self, db_reader, time_from=None, time_to=None): online_streaks = OnlineSessionEnumerator(time_from, time_to) @@ -373,13 +370,10 @@ class OutputSinkPlot(OutputSinkOnlineSessions): bar_chart.save(self._fd) -class OutputFormat(Enum): - CSV = "csv" - JSON = "json" - PLOT = "plot" - - def __str__(self): - return self.value +class OutputFormat(StrEnum): + CSV = auto() + JSON = auto() + PLOT = auto() def create_sink(self, fd=sys.stdout): if self is OutputFormat.CSV: diff --git a/vk/user.py b/vk/user.py index 010a198..26d9cf1 100644 --- a/vk/user.py +++ b/vk/user.py @@ -5,7 +5,7 @@ from collections import OrderedDict from collections.abc import Hashable, Mapping, MutableMapping -from enum import Enum +from enum import auto, StrEnum from .last_seen import LastSeen @@ -36,27 +36,21 @@ def _parse_online_flag(x): return _parse_bool(x) -class UserField(Enum): +class UserField(StrEnum): UID = "id" - FIRST_NAME = "first_name" - LAST_NAME = "last_name" - DEACTIVATED = "deactivated" - HIDDEN = "hidden" + FIRST_NAME = auto() + LAST_NAME = auto() + DEACTIVATED = auto() + HIDDEN = auto() - DOMAIN = "domain" - ONLINE = "online" - LAST_SEEN = "last_seen" + DOMAIN = auto() + ONLINE = auto() + LAST_SEEN = auto() - def __str__(self): - return self.value - -class DeactivationReason(Enum): - DELETED = "deleted" - BANNED = "banned" - - def __str__(self): - return self.value +class DeactivationReason(StrEnum): + DELETED = auto() + BANNED = auto() def _parse_deactivated(s): -- cgit v1.3