xotl.tools.names – Utilities for handling objects names¶
A protocol to obtain or manage object names.
-
xotl.tools.names.nameof(*objects, depth=1, inner=False, typed=False, full=False, safe=False)[source]¶ Obtain the name of each one of a set of objects.
New in version 1.4.0.
-
Changed in version 1.6.0:
Keyword arguments are now keyword-only arguments.
Support for several objects
Improved the semantics of parameter
full.Added the
safekeyword argument.
If no object is given, None is returned; if only one object is given, a single string is returned; otherwise a list of strings is returned.
The name of an object is normally the variable name in the calling stack.
If the object is not present calling frame, up to five frame levels are searched. Use the
depthkeyword argument to specify a different starting point and the search will proceed five levels from this frame up.If the same object has several good names a single one is arbitrarily chosen.
Good names candidates are retrieved based on the keywords arguments
full,inner,safeandtyped.If
typedis True and the object is not a type name or a callable (seexotl.tools.future.inspect.safe_name()), then thetypeof the object is used instead.If
inneris True we try to extract the name by introspection instead of looking for the object in the frame stack.If
fullis True the full identifier of the object is preferred. In this case ifinneris False the local-name for the object is found. Ifinneris True, find the import-name.If
safeis True, returned value is converted -if it is not- into a valid Python identifier, though you should not trust this identifier resolves to the value.
-
xotl.tools.names.identifier_from(obj)[source]¶ Build an valid identifier from the name extracted from an object.
New in version 1.5.6.
First, check if argument is a type and then returns the name of the type prefixed with
_if valid; otherwise callsnameoffunction repeatedly until a valid identifier is found using the following order logic:inner=True, without arguments looking-up a variable in the calling stack, andtyped=True. Returns None if no valid value is found.Examples:
>>> identifier_from({}) 'dict'
Use cases for getting the name of an object¶
The function nameof() is useful for cases when you get a value and you
need a name. This is a common need when doing framework-level code that tries
to avoid repetition of concepts.
Solutions with nameof()¶
Properly calculate the tasks’ name in Celery applications¶
Celery warns about how to import the tasks. If in a module you import your
task using an absolute import, and in another module you import it using a
relative import, Celery regards them as different tasks. You must either use
a consistent import style, or give a name for the task. Using nameof()
you can easily fix this problem.