This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import timeit | |
| for klass in (Color, ColorAttr, ColorProp, ColorAttrCustom, | |
| ColorDesc, ColorData, ColorTuple): | |
| print(f"Class: {klass.__name__}") | |
| code = f"{klass.__name__}(128, 128, 128)" | |
| setup = f"from __main__ import {klass.__name__}" | |
| print(timeit.timeit(stmt=code, number=1000_000, setup=setup)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import namedtuple | |
| class ColorTuple(namedtuple("ColorBase", "r g b")): | |
| # prevents creation of instance __dict__ | |
| __slots__ = () | |
| def __new__(cls, r=0, g=0, b=0): | |
| for v in (r, g, b): | |
| if not isinstance(v, int): | |
| raise TypeError("RGB values must be integers") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import attrs | |
| def rgb_validator(inst, attr, value): | |
| # Validate type | |
| if type(value) is not int: | |
| raise ValueError("RGB values must be integers") | |
| # Validate range | |
| if not value in range(0, 256): | |
| raise ValueError("RGB values must be in range {0...255}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class IntRange: | |
| """ Custom integer range class using descriptors """ | |
| def __init__(self, min_val, max_val): | |
| self._range = range(min_val, max_val) | |
| def __set_name__(self, owner, name): | |
| self.name = "_" + name | |
| def __get__(self, obj, objtype=None): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RGBColor(int): | |
| def __new__(cls, value): | |
| if not value in range(0, 256): | |
| raise ValueError("RGB values must be in range {0...255}") | |
| # Create the integer instance | |
| return super().__new__(cls, value) | |
| class ColorAttrCustom(Color): | |
| """ Color class with full attribute validation and custom color type """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ColorProp(Color): | |
| """ RGB Color class using properties """ | |
| def validate_rgb(self, value): | |
| # Validate type | |
| if type(value) is not int: | |
| raise ValueError("RGB values must be integers") | |
| # Validate range | |
| if not value in range(0, 256): | |
| raise ValueError("RGB values must be in range {0...255}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ColorAttr(Color): | |
| """ Color class with attribute validation """ | |
| def __init__(self, r=0, g=0, b=0): | |
| self.r = r | |
| self.g = g | |
| self.b = b | |
| def __setattr__(self, name, value): | |
| if name in ("r", "g", "b"): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| @dataclass(frozen=True) | |
| class ColorData: | |
| r: int = 0 | |
| g: int = 0 | |
| b: int = 0 | |
| def __post_init__(self): | |
| for v in (self.r, self.g, self.b): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ExceptionCounter: | |
| """ Custom context manager class counting exceptions """ | |
| def __init__(self): | |
| self.count = 0 | |
| def __enter__(self): | |
| pass | |
| def __exit__(self, exc_type, exc_value, traceback): | |
| if exc_type and exc_value: | |
| self.count += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Color: | |
| """ A class representing RGB colour """ | |
| def __init__(self, r=0, g=0, b=0): | |
| for val in (r, g, b): | |
| # Validate type | |
| if type(val) is not int: | |
| raise ValueError("RGB values must be integers") | |
| # Validate range | |
| if not val in range(0, 256): |
NewerOlder