ATLAS Offline Software
Loading...
Searching...
No Matches
atlascp.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2#
3# @author Giordon Stark
4
5# Syntactic sugar for creating CP tools by class name rather than raw
6# type strings. Module-level __getattr__ dispatches attribute access
7# to a constructor factory so that, e.g.:
8#
9# from ColumnarToolWrapperPython import atlascp
10# tool = atlascp.MuonEfficiencyScaleFactors('myTool')
11#
12# is equivalent to:
13#
14# Tool("CP::MuonEfficiencyScaleFactors/myTool")
15
16from ColumnarToolWrapperPython.tool import Tool
17
18_class_cache = {}
19
20
21def _make_tool_class(tool_type_name):
22 """Return a named Tool subclass for tool_type_name, creating it if needed.
23
24 Parameters
25 ----------
26 tool_type_name:
27 The C++ class name without namespace, e.g. "MuonEfficiencyScaleFactors".
28
29 Returns
30 -------
31 type
32 A subclass of Tool whose ``__name__`` equals ``tool_type_name``.
33 Repeated calls with the same name return the same class object.
34 """
35 if tool_type_name not in _class_cache:
36 _class_cache[tool_type_name] = type(tool_type_name, (Tool,), {})
37 return _class_cache[tool_type_name]
38
39
40def __getattr__(name):
41 """Return a constructor for the named CP tool type.
42
43 Called when code accesses ``atlascp.<ToolTypeName>``. Returns a callable
44 that creates and initializes a ``Tool`` subclass instance.
45
46 Parameters
47 ----------
48 name:
49 Tool type name without namespace, e.g. "MuonEfficiencyScaleFactors".
50
51 Returns
52 -------
53 callable
54 A constructor with signature
55 ``(instance_name, /, *, namespace="CP", properties=None, rename_containers=None)``.
56 ``instance_name`` is positional-only; all other arguments are keyword-only.
57 """
58 def constructor(instance_name, /, *, namespace="CP", properties=None, rename_containers=None):
59 type_and_name = f"{namespace}::{name}/{instance_name}"
60 cls = _make_tool_class(name)
61 instance = cls.__new__(cls)
62 Tool.__init__(instance, type_and_name, properties=properties, rename_containers=rename_containers)
63 return instance
64 constructor.__name__ = name
65 constructor.__qualname__ = name
66 return constructor
__getattr__(name)
Definition atlascp.py:40
_make_tool_class(tool_type_name)
Definition atlascp.py:21