Skip to content

Error when setting a Timer text formatted as a json #31

@kleysonr

Description

@kleysonr

I would like to log the elapsed time with the function's parameters as a json.

If I run the code below:

import time
from codetiming import Timer
import sys

def get_text(requestId, funcContext, parameters_to_exclude):
	_data = {
		'requestId': requestId,
		'exec_time_ms': '{:.0f}',
		'exec': {k:v for k,v in funcContext.f_locals.items() if k not in parameters_to_exclude}
	}
	return '{}'.format(_data)

def principal(name, age, city):
    
    print(get_text('111', sys._getframe(), []))

    with Timer():

        time.sleep(2)

principal(name='lara', age=30, city='boston')

I will get the following output:

{'requestId': '111', 'exec_time_ms': '{:.0f}', 'exec': {'name': 'lara', 'age': 30, 'city': 'boston'}}
Elapsed time: 2.0022 seconds

But I would like to get the following one:

{'requestId': '111', 'exec_time_ms': 2002, 'exec': {'name': 'lara', 'age': 30, 'city': 'boston'}}

Then I changed from Timer() to:

with Timer(text=get_text('111', sys._getframe(), ['request', 'info'])):

But after the change, I'm getting the following error:

Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/kleyson/.vscode/extensions/ms-python.python-2021.9.1191016588/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/kleyson/.vscode/extensions/ms-python.python-2021.9.1191016588/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
    run()
  File "/home/kleyson/.vscode/extensions/ms-python.python-2021.9.1191016588/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "/usr/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/data/detran/dev/face-recognition/software/app/test.py", line 24, in <module>
    principal(name='lara', age=30, city='boston')
  File "/data/detran/dev/face-recognition/software/app/test.py", line 21, in principal
    time.sleep(2)
  File "/home/kleyson/.virtualenvs/face-recognition/lib/python3.7/site-packages/codetiming/_timer.py", line 74, in __exit__
    self.stop()
  File "/home/kleyson/.virtualenvs/face-recognition/lib/python3.7/site-packages/codetiming/_timer.py", line 60, in stop
    text = self.text.format(self.last, **attributes)
KeyError: "'requestId'"

How to do that ? Is that possible ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions