Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Variable and Constant object to support typing
- Loading branch information
1 parent
788dd6e
commit a102d47
Showing
14 changed files
with
144 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import Optional, Union, List | ||
|
||
|
||
class Variable: | ||
__slots__ = "name", "type" | ||
|
||
def __init__(self, name: str, type: Optional[str] = None): | ||
self.name = name | ||
self.type = type | ||
|
||
def __str__(self) -> str: | ||
if self.type is not None: | ||
return f"{self.type}:{self.name}" | ||
return f"{self.name}" | ||
|
||
def __getitem__(self, item) -> List["Variable"]: | ||
if not isinstance(item, slice): | ||
raise ValueError("Variable range can be only defined by a slice") | ||
|
||
if item.step is not None: | ||
return [Variable(f"{self.name}{i}", self.type) for i in range(item.start, item.stop, item.step)] | ||
|
||
return [Variable(f"{self.name}{i}", self.type) for i in range(item.start, item.stop)] | ||
|
||
def __call__(self, item: str) -> "Variable": | ||
if isinstance(item, str): | ||
return Variable(self.name, item) | ||
raise ValueError("Type can be only of type str") | ||
|
||
def __eq__(self, other: Union["Variable", str]) -> bool: | ||
return str(other) == str(self) | ||
|
||
def __hash__(self): | ||
return str(self).__hash__() | ||
|
||
def __lt__(self, other): | ||
return str(self).__lt__(str(other)) | ||
|
||
|
||
class Constant: | ||
__slots__ = "name", "type" | ||
|
||
def __init__(self, name: str, type: Optional[str] = None): | ||
self.name = name | ||
self.type = type | ||
|
||
def __str__(self) -> str: | ||
if self.type is not None: | ||
return f"{self.type}:{self.name}" | ||
return f"{self.name}" | ||
|
||
def __call__(self, item: str) -> "Constant": | ||
if isinstance(item, str): | ||
return Constant(self.name, item) | ||
raise ValueError("Type can be only of type str") | ||
|
||
def __eq__(self, other: Union["Constant", str]) -> bool: | ||
return str(other) == str(self) | ||
|
||
def __hash__(self): | ||
return str(self).__hash__() | ||
|
||
def __lt__(self, other): | ||
return str(self).__lt__(str(other)) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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