def __repr__(self): return "Token({token_type}, {lexeme}, ({location[0]}, {location[1]}))".format( token_type=Enum.__str__( self.token_type), # Bypass overriden __str__ lexeme=repr(self.lexeme), location=self.location, )
from enum import Enum from pathlib import Path from typing import List from pkg_resources import resource_filename def resource_path(name: str) -> Path: return Path(resource_filename(__name__, name)) def scenarios() -> List[str]: schema_files = { p.stem for p in resource_path("schema").iterdir() if p.is_file() and p.suffix == ".sql" } workload_files = { p.stem for p in resource_path("workload").iterdir() if p.is_file() and p.suffix == ".sql" } return sorted(schema_files.intersection(workload_files)) Scenario = Enum("Scenario", {scenario: scenario for scenario in scenarios()}) Scenario.__str__ = lambda self: self.value Scenario.schema_path = lambda self: resource_path(f"schema/{self}.sql") Scenario.workload_path = lambda self: resource_path(f"workload/{self}.sql")
from enum import Enum from copy import deepcopy import os AminoAcid = Enum("AminoAcid", "A R N D C E Q G H I L K M F P S T W Y V B Z X") # Make the str() function print only the amino acid letter by redefining __str__ AminoAcid.__str__ = lambda self: self.name[-1] class Sequence: """Represents a sequence of amino acids. It is suited to compute the best alignment between two sequences.""" def __init__(self, filename, sequence_id): """Constructor. Loads the sequence from a file. Parameters: filename: the filename of the file containing the sequence. sequence_id: the identifier of the sequence in the file. This is the string between the two first bars "|" on the line preceding the sequence. """ self.load_from_file(filename, sequence_id) def __getitem__(self, index): """Returns the elements at the specified index. Allows the syntax sequence[i]. """ return self.sequence[index] def __len__(self): """Returns the lenght of the sequence. Allows the syntax len(sequence)."""
def __str__(self): return Enum.__str__(self).split('.', 1)[1]
)) Color = Enum("Color", __named_colors) def normal_name(self): return self.value.name def hexcode(self): return self.value.hexcode def rgb(self) -> Tuple: raw_value = self.hexcode().replace("#", "") return tuple(int(raw_value[i:i + 2], 16) for i in (0, 2, 4)) def from_name(access_name: str): return Color[access_name] def color_string_name(self): return self.name Color.normal_name = normal_name Color.hexcode = hexcode Color.from_name = from_name Color.__str__ = color_string_name Color.rgb = rgb
from collections import namedtuple from enum import Enum import itertools import sys Seat = Enum("Seat", zip("NESW", range(4))) Seat.N._s, Seat.E._s, Seat.S._s, Seat.W._s = ("North", "East", "South", "West") Seat.__str__ = lambda self: self._s Seat.__index__ = lambda self: self.value Seat.__add__ = lambda self, val: Seat((self.value + val) % len(Seat)) class Suit(Enum): S = 0, " S", "♠" H = 1, " H", "♡" D = 2, " D", "♢" C = 3, " C", "♣" def __init__(self, value, sym, unicode_sym): self._value_ = value self._sym = (unicode_sym if sys.stdout.encoding.lower() == "utf-8" else sym) self._unicode_sym = unicode_sym def __str__(self): return self._unicode_sym if SUITS_FORCE_UNICODE else self._sym def __index__(self): return self.value # Yes, the order is reversed.
def attach_functions(enum_type: enum.Enum) -> enum.Enum: enum_type.__str__ = lambda self: self.name enum_type.__html__ = lambda self: self.name return enum_type
if self.value == 'resign' or self.value == 'pass': return True move_x = board_columns.index(self.value[0].lower()) + 1 move_y = int(self.value[1:]) if move_x > board_size or move_y > board_size: return False return True @property def row_index(self): return int(self.value[1]) - 1 @property def col_index(self): return board_columns.index(self.value[0].lower()) Move.is_valid = is_valid Move.row_index = row_index Move.col_index = col_index Move.__str__ = lambda self: self.value class StoneColor(Enum): B = 'b' BLACK = 'b' W = 'w' WHITE = 'w'
def __str__(self): return os.path.splitext(os.path.basename(Enum.__str__(self)))[1][1:]