-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New features in version 0.1.0 #2
Comments
Current view of graph generation: https://opendilab.github.io/treevalue/dev/graphviz/api_doc/tree/tree.html#graphics |
Current sample of treevalue cli: https://opendilab.github.io/treevalue/dev/graphviz/tutorials/quick_start/index.html |
Task 2: Dump of the tree value graph. import pickle
t = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
binary = picke.dumps(t) # dump to binary
tx = pickle.loads(binary) # load from binary
assert t == tx |
Task 1: Dump of the graph
|
Task 3 & 4: Dump and load to binary from treevalue import dump, load, FastTreeValue
t = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
with open('dump_file.btv', 'wb') as file:
dump(t, file=file) # dump data to file without treevalue's type. Not the same as pickle.dump
with open('dump_file.btv', 'rb') as file:
t1 = load(file=file, type_=FastTreeValue) # type_ is necessary when loaded
assert t1 == t |
Task 7: Self operator with original address from treevalue import FastTreeValue
t = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
t2 = FastTreeValue({'a': 11, 'b': 20, 'x': {'c': 33, 'd': 48}})
if __name__ == '__main__':
print('t:')
print(t)
print('t2:')
print(t2)
t += t2
print('t += t2:')
print(t) Output should be
In the previous version, the self-added |
Task 6: Self calculation with original address from treevalue import FastTreeValue, method_treelize
class MyTreeValue(FastTreeValue):
@method_treelize(self_copy=True) # Enable of self_copy argument will make the method be a self calculation
def append(self, x):
return self + x
t = MyTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
t2 = MyTreeValue({'a': 11, 'b': 20, 'x': {'c': 33, 'd': 48}})
if __name__ == '__main__':
print('t:')
print(t)
print('t2:')
print(t2)
rt = t.append(t2)
print('t after t.append(t2):')
print(t)
assert rt is t # rt is t, chain calling is supported, such as t.append(xxx).append(yyy) Output should be
|
Task 8: Add more features in An example of extension for from treevalue import general_tree_value
class NonDefaultTreeNumber(general_tree_value(base=dict(), methods=dict(
__add__=dict(missing=0, mode='outer'),
__radd__=dict(missing=0, mode='outer'),
__iadd__=dict(missing=0, mode='outer')
))):
pass
t = NonDefaultTreeNumber({'a': 1, 'b': 2, 'x': {'c': 3, 'e': 9}})
t2 = NonDefaultTreeNumber({'b': 21, 'd': 18, 'x': {'c': 3}, 'y': {'t': 4}})
if __name__ == '__main__':
print('t + t2:')
print(t + t2) The result should be
Convenient, isn't it? Another example of disabling/modifying some of the functions or operators. import sys
from treevalue import general_tree_value
from treevalue.utils import str_traceback
class BanAndOverrideTreeNumber(general_tree_value(methods=dict(
__add__=NotImplemented,
__radd__=NotImplemented,
__iadd__=NotImplemented,
__mul__=KeyError("lksdjfkl"),
__rmul__=KeyError("lsdfjkldks"),
__imul__=KeyError("dklfgjsl"),
__matmul__=KeyError,
__rmatmul__=KeyError,
__imatmul__=KeyError,
__pos__=(lambda sp: sp // 2),
__truediv__=(lambda sp: sp - 2),
))):
pass
# the structure of t and t2 are exactly the same
t = BanAndOverrideTreeNumber({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 9}})
t2 = BanAndOverrideTreeNumber({'a': 21, 'b': 18, 'x': {'c': 3, 'd': 4}})
if __name__ == '__main__':
try:
print('t + t2:')
print(t + t2)
except Exception as err: # __add__ is banned on operators' level
print(str_traceback(err), file=sys.stderr)
try:
print('t * t2:')
print(t * t2)
except Exception as err: # raise KeyError when __mul__ is called
print(str_traceback(err), file=sys.stderr)
print('+t:')
print(+t) # __pos__ is modified
print('t / 2:')
print(t / 2) # __truediv__ is modified The output (including stdout and stderr) is
|
Task 5 & 9: Cli Support, tree dump and graph drawing Example of graph drawing via cli: https://opendilab.github.io/treevalue/5/merge/tutorials/quick_start/index.html Example of dump binary data of tree value ( treevalue export -t 'demo.*' The listdir after this command
And the 33 from treevalue import load, FastTreeValue
if __name__ == '__main__':
with open('t1.btv', 'rb') as file:
t = load(file, type_=FastTreeValue)
print(t) The output of loaded
|
Pull request #5 has been merged. |
Implemented in #5 .
pickle.dumps
ofTreeValue
objectsgeneral_treevalue
, such asNotImplement
The text was updated successfully, but these errors were encountered: