60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Self, Optional
|
|
|
|
class Options:
|
|
|
|
OVERWRITING:int = 0
|
|
OVERWRITE:int = 1 * 3 ** OVERWRITING
|
|
NO_OVERWRITE:int = 2 * 3 ** OVERWRITING
|
|
|
|
NULLISH:int = 1
|
|
ALLOW_NULLS:int = 1 * 3 ** NULLISH
|
|
NOT_NULLS:int = 2 * 3 ** NULLISH
|
|
|
|
DEFAULT:Self = Self(NO_OVERWRITE + ALLOW_NULLS)
|
|
|
|
@classmethod
|
|
def get_value(cls:type[Self], value:int, option:int) -> bool|None:
|
|
|
|
ternary:int = value // 3 ** option % 3
|
|
|
|
return (
|
|
True if ternary == 1 else
|
|
False if ternary == 2 else
|
|
None)
|
|
|
|
def __init__(self:Self, options:int = 0, default:Optional[Self] = None) -> None:
|
|
|
|
self.overwrite:bool|None
|
|
self.allow_null:bool|None
|
|
|
|
if default is None:
|
|
default = self.DEFAULT
|
|
|
|
for key, option in (
|
|
("overwrite", self.OVERWRITE),
|
|
("allow_null", self.NULLISH)
|
|
):
|
|
|
|
value:bool|None = self.get_value(options, option)
|
|
|
|
setattr(self, key, getattr(default, key) if value is None and default is not None else value)
|
|
|
|
def get(self:Self) -> int:
|
|
|
|
code:int = 0
|
|
option:int
|
|
value:bool|None
|
|
|
|
for option, value in (
|
|
(self.OVERWRITING, self.overwrite),
|
|
(self.NULLISH, self.allow_null)
|
|
):
|
|
code += (
|
|
1 if value is True else
|
|
2 if value is False else
|
|
0) * 3 ** option
|
|
|
|
return code |