here is my python code i am trying. I am expecting Stored Metadata list. Memory Decorator Purpose is the code where functions are "decorated" to store metadata.
import functools
memory_storage = []
def memory(func_names, description):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Store the metadata before function execution
memory_storage.append({
"func_names": func_names,
"description": description,
"function": func
})
return func(*args, **kwargs)
return wrapper
return decorator
@memory(
func_names=['print_weep'],
description="This method will print the weep"
)
def print_weep():
print("weep")
@memory(
func_names=['print_hello'],
description="This method will print hello"
)
def print_hello():
print("hello")
def execute_functions():
for entry in memory_storage:
print(f"Executing function {entry['func_names'][0]}: {entry['description']}")
entry['function']() # Call the stored function
print("Stored Metadata:", memory_storage)
execute_functions()
here is my python code i am trying. I am expecting Stored Metadata list. Memory Decorator Purpose is the code where functions are "decorated" to store metadata.
import functools
memory_storage = []
def memory(func_names, description):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Store the metadata before function execution
memory_storage.append({
"func_names": func_names,
"description": description,
"function": func
})
return func(*args, **kwargs)
return wrapper
return decorator
@memory(
func_names=['print_weep'],
description="This method will print the weep"
)
def print_weep():
print("weep")
@memory(
func_names=['print_hello'],
description="This method will print hello"
)
def print_hello():
print("hello")
def execute_functions():
for entry in memory_storage:
print(f"Executing function {entry['func_names'][0]}: {entry['description']}")
entry['function']() # Call the stored function
print("Stored Metadata:", memory_storage)
execute_functions()
Share
Improve this question
edited Feb 2 at 6:52
chinna
asked Feb 2 at 6:44
chinnachinna
192 bronze badges
3
|
1 Answer
Reset to default 1I'm taking a guess that you want to store the functions and call them by iterating through the memory_storage
list. If that's the case you are putting the append
call in the wrong function: the memory would be appended when the print_weep
/print_hello
functions are called, not when the decorator is applied. Try this:
import functools
memory_storage = []
def memory(func_names, description):
def decorator(func):
# This should be put here
memory_storage.append({
"func_names": func_names,
"description": description,
"function": func
})
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
return decorator
@memory(
func_names=['print_weep'],
description="This method will print the weep"
)
def print_weep():
print("weep")
@memory(
func_names=['print_hello'],
description="This method will print hello"
)
def print_hello():
print("hello")
def execute_functions():
for entry in memory_storage:
print(f"Executing function {entry['func_names'][0]}: {entry['description']}")
entry['function']() # Call the stored function
print("Stored Metadata:", memory_storage)
execute_functions()
Output:
Stored Metadata: [{'func_names': ['print_weep'], 'description': 'This method will print the weep', 'function': <function print_weep at 0x1007432e0>}, {'func_names': ['print_hello'], 'description': 'This method will print hello', 'function': <function print_hello at 0x100743920>}]
Executing function print_weep: This method will print the weep
weep
Executing function print_hello: This method will print hello
hello
memory
to be triggered without triggeringprint_weep
andprint_weep
? – Guy Commented Feb 2 at 6:53