I'm using python 3.6
As the title suggests thisThis code is about instantiating object attributes Just in Time.
I'm working on a REST API, where I currently create pythonPython objects from the JSON data returned by the server. Though, though not all attributes of the created objects will be called upon in user code.
Hence only instantiating the attributes the user access via dot notation seems like a valid way to improve performance.
The core of the idea is to use pythonPython scoping to mutate a list in the scope outside the function evaluate. I dislike this the most.
Do you think it's a good idea?
Do you have a better solution?
Do you think the complexity is worth it?
EDIT
- Do you think it's a good idea?
- Do you have a better solution?
- Do you think the complexity is worth it?
I will try anand create more context here as to why I want this. I have implemented this idea in my package async_v20 found here. I have written docs here.
whenWhen the server sends jsonJSON:
In some cases there may be 5000 objects to instantiate. Or or objects may be nested around 3 deep
objects. Objects get instantiated in this module
This example runs a benchmark.
CODE: This example runs a benchmark:
RUNNING JitAttributes
TOOK 0.2956113815307617 seconds
RUNNING Attributes
TOOK 4.880501985549927 seconds
RUNNING JitAttributes
TOOK 0.2956113815307617 seconds
RUNNING Attributes
TOOK 4.880501985549927 seconds
>>> jit_instance = JitAttributes(
... lazy_evaluate(list, range(a)),
... lazy_evaluate(list, range(b)),
... lazy_evaluate(list, range(c)),
... )
>>> jit_instance.foo
# EVALUATING <- Only evaluates once :)
# [0, 1, 2, 3, ... ]
>>> jit_instance.foo
# [0, 1, 2, 3, ... ]
>>> non_jit_instance = Attributes(
... list(range(a)),
... list(range(b)),
... list(range(c)),
... )
>>> non_jit_instance.foo
# [0, 1, 2, 3, ...]
>>> non_jit_instance.foo
# [0, 1, 2, 3, ...]
>>> jit_instance = JitAttributes(
... lazy_evaluate(list, range(a)),
... lazy_evaluate(list, range(b)),
... lazy_evaluate(list, range(c)),
... )
>>> jit_instance.foo
# EVALUATING <- Only evaluates once :)
# [0, 1, 2, 3, ... ]
>>> jit_instance.foo
# [0, 1, 2, 3, ... ]
>>> non_jit_instance = Attributes(
... list(range(a)),
... list(range(b)),
... list(range(c)),
... )
>>> non_jit_instance.foo
# [0, 1, 2, 3, ...]
>>> non_jit_instance.foo
# [0, 1, 2, 3, ...]