Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ChainDefInMenu.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2 
3 import typing
4 from typing import List
5 import dataclasses
6 from dataclasses import dataclass, field
7 
8 @dataclass(frozen=True)
9 class ChainProp:
10  """Class to hold chain properties with (limited) type-checking"""
11  name: str # mandatory
12  groups: List[str] # mandatory
13  monGroups: List[str] = field(default_factory = list)
14  l1SeedThresholds: List[str] = field(default_factory = list)
15  stream: List[str] = field(default_factory = lambda : ['Main'])
16  mergingStrategy: str = 'auto'
17  mergingOrder: List[str] = field(default_factory = list)
18  mergingOffset: int = -1
19 
20  # currently we don't have a type-checker in LCG so we
21  # implement a limited version ourselves:
22  def _check_types(self):
23  """Check field types"""
24  for f in dataclasses.fields(self):
25  actual_type = typing.get_origin(f.type) or f.type
26  value = getattr(self, f.name)
27  if actual_type == list:
28  if value:
29  for el in value:
30  if not isinstance(el, typing.get_args(f.type)):
31  return f
32  elif not isinstance(value, actual_type):
33  return f
34  return None
35 
36  def __post_init__(self):
37  f = self._check_types()
38  if f is not None:
39  raise TypeError(f"Expected type of '{f.name}' to be {f.type}, "
40  f"got {repr(getattr(self, f.name))}")
41 
42 if __name__=='__main__':
43  ch = ChainProp(name='abc', groups=['g1'])
ChainDefInMenu.ChainProp._check_types
def _check_types(self)
Definition: ChainDefInMenu.py:22
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
ChainDefInMenu.ChainProp.__post_init__
def __post_init__(self)
Definition: ChainDefInMenu.py:36
ChainDefInMenu.ChainProp
Definition: ChainDefInMenu.py:9