This content originally appeared on DEV Community and was authored by IO_Node
As mentioned in my previous post, I’ve been working to create a locking mechanism for a class in python and it seems the test initially made for synthax ruling is now passing all the tests!! Here’s the output results of using both the UniversalObject and LocalObject classes:
=== UniversalObject Examples ===
Dict creation (normal): {'name': 'John', 'age': 30}
Index keys: ['name', 'age']
Name from index: John
Dict creation (constant): {'name': 'John', 'age': 30}
Index keys: ['name', 'age']
Name from index (should be tuple): ('John',)
List creation (normal): [1, 2, 3]
Index keys: ['arg_0', 'arg_1', 'arg_2']
arg_0 from index: 1
List creation (constant): [1, 2, 3]
Index keys: ['const_0', 'const_1', 'const_2']
const_0 from index (should be tuple): (1,)
Single value (normal): hello
Index keys: ['arg_0']
Single value (constant): hello
Index keys: ['arg_0']
arg_0 from index (should be tuple): ('hello',)
=== Demonstrating Parent Display ===
With show_parent=True: {'test': 'value'}
=== LocalObject Examples ===
Dict creation (normal): <LocalObject:locked>
Index keys: ['name', 'age']
Dict creation (constant): <LocalObject:locked>
Index keys: ['name', 'age']
name from index (should be tuple): ('Jane',)
List creation (normal): <LocalObject:locked>
Index keys: ['arg_0', 'arg_1', 'arg_2']
List creation (constant): <LocalObject:locked>
Index keys: ['const_0', 'const_1', 'const_2']
Single value (normal): <LocalObject:locked>
Index keys: ['arg_0']
Single value (constant): <LocalObject:locked>
Index keys: ['arg_0']
arg_0 from index (should be tuple): ('world',)
=== Demonstrating Constant Protection ===
Constant values are stored as single-element tuples:
u4.const_0 = 1
type(u4.const_0) = <class 'int'>
l4.const_0 = 10
type(l4.const_0) = <class 'int'>
=== Demonstrating LocalObject Locking Behavior ===
Locked LocalObject created:
After unlocking, l7.locked = True
Not only does the naming is standardized, it support real constant / variables and all logical / mathematical operations as if they are regular container instead of class objects. It also has a dict index of all value fed to the class for easy access (.arg_index).
Step 1 completed, will now work on the multi-lock system using conditions now that I can do normal boolean checks with the objects XD This type() check on constants that gives back the actual type of the value stored instead of always giving a tuple is chef kiss.
If you are interested in my Magic Pythong journey, feel free to drop a comment about what you would like to see implemented in the future public repo.
This content originally appeared on DEV Community and was authored by IO_Node