Skip to content

Commit

Permalink
adding tables
Browse files Browse the repository at this point in the history
  • Loading branch information
cootshk committed Nov 1, 2023
1 parent 10a22f7 commit e3d0d1b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ def __init__(self, *list_values: Optional[Any]): ...
def __init__(self, **dict_values: Optional[Any]): ...
@overload
def __init__(self, *list_values: Optional[Any], **dict_values: Optional[Any]): ...

@overload
def __delitem__(self, key: KeyValue): ...
@overload
def __delitem__(self, key: Iterable[KeyValue]): ...

@overload
def __setitem__(self, key: KeyValue, value: Any): ...
@overload
def __setitem__(self, key: Iterable[KeyValue], value: Iterable[Any]): ...

@overload
def find_keys(self, value: Any, /) -> KeyValue: ...
@overload
Expand All @@ -44,7 +44,7 @@ def find_keys[default: Any](self, value: Any,/,*, default:default=None) -> list[
# KeyValue: str | int
# TODO: Ensure that all lines have type hinting
###############################################

@override
def __init__(self, *args: Optional[Any], **kwargs: Optional[Any]):
if ( #Table([], {})
Expand All @@ -56,7 +56,7 @@ def __init__(self, *args: Optional[Any], **kwargs: Optional[Any]):
self.list = args[0]
self.dict = args[1]
return

elif ( #Table([])
len(args) == 1 and
len(kwargs) == 0 and
Expand Down Expand Up @@ -178,7 +178,15 @@ def __eq__(self, other):
if isinstance(other, Table):
return self.list == other.list and self.dict == other.dict
return False

def __add__(self, other: Any) -> "Table":
if isinstance(other, Table):
return Table(self.list + other.list, self.dict | other.dict)
elif isinstance(other, list):
return Table(self.list + other, self.dict)
elif isinstance(other, dict):
return Table(self.list, self.dict | other)
else:
raise TypeError(f"unsupported operand type(s) for +: 'Table' and '{type(other).__name__}'")

###############################################################################################################
if __name__ == "__main__" and not __debug__: # definitions
Expand Down Expand Up @@ -216,5 +224,6 @@ def test_Table():
assert x.list == [1,2,3], "Test 5 failed!"
assert x.dict == {"foo":"bar", "spam":"eggs"}, "Test 6 failed!"
assert x == Table(1,2,3, foo="bar", spam="eggs"), "Test 7 failed!"
assert x + Table(4,5,6) == Table(1,2,3,4,5,6, foo="bar", spam="eggs"), "Test 8 failed!"
#congrats, the code works!
print("All tests passed!")

0 comments on commit e3d0d1b

Please sign in to comment.