-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (74 loc) · 3.52 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from __future__ import annotations
from typing import final, Final, Optional, Collection, Iterator
from bidict import bidict
@final
class Symbol(Collection[str]):
"""
`Symbol` is a class whose constructor returns a `Symbol` value or just
a `Symbol` — that's guaranteed to be unique. Symbols are often used to add
unique property keys to an object that won't collide with keys any other
code might add to the object.
Every `Symbol()` call is guaranteed to return a unique `Symbol`.
Every `Symbol.for("key")` call will always return the same `Symbol` for a
given value of `"key"`. When `Symbol.for("key")` is called, if a `Symbol`
with the given key can be found in the global `Symbol` registry, that
`Symbol` is returned. Otherwise, a new Symbol is created, added to the
global `Symbol` registry under the given key, and returned.
"""
__shared: Final[bidict[str, Symbol]] = bidict()
def __init__(self, description: str = "", /) -> None:
"""
Creates a new Symbol object.
:param description: A string. A description of the symbol which can be
used for debugging but not to access the symbol itself. Defaults to an
empty string.
"""
self.__description: Final[str] = description
self.__hash: Final[int] = id(self)
@classmethod
def for_key(cls, key: str, /) -> Symbol:
"""
The `Symbol.for_key()` (`Symbol.for()` in JavaScript) static method
searches for existing symbols in a runtime-wide symbol registry with
the given key and returns it if found. Otherwise a new symbol gets
created in the global symbol registry with this key.
In contrast to `Symbol()`, the `Symbol.for_key()` function creates
a symbol available in a global symbol registry list.
`Symbol.for_key()` does also not necessarily create a new symbol on
every call, but checks first if a symbol with the given key is already
present in the registry. In that case, that symbol is returned. If no
symbol with the given key is found, `Symbol.for_key()` will create
a new global symbol.
:param key: The key for the symbol (and also used for the
description of the symbol).
:return: An existing symbol with the given key if found; otherwise,
a new symbol is created and returned.
"""
if key not in cls.__shared:
cls.__shared[key] = cls(key)
return cls.__shared[key]
@classmethod
def key_for(cls, sym: Symbol, /) -> Optional[str]:
"""
The `Symbol.key_for()` (`Symbol.keyFor()` in JavaScript) static method
retrieves a shared symbol key from the global symbol registry for the
given symbol.
:param sym: The symbol to find a key for.
:return: A string representing the key for the given symbol if one is
found on the global registry; otherwise, `None`.
"""
return cls.__shared.inverse.get(sym, None)
def __str__(self) -> str:
return self.__description
def __repr__(self) -> str:
return f"{self.__class__.__name__}({repr(self.__description)})"
def __eq__(self, other: object) -> bool:
return self is other
def __hash__(self) -> int:
return self.__hash
def __len__(self) -> int:
return len(self.__description)
def __iter__(self) -> Iterator[str]:
return iter(self.__description)
def __contains__(self, item: object) -> bool:
return type(item) is str and item in self.__description