ATLAS Offline Software
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  topoStartFrom: bool = False # might be obsolete
20 
21  # currently we don't have a type-checker in LCG so we
22  # implement a limited version ourselves:
23  def _check_types(self):
24  """Check field types"""
25  for f in dataclasses.fields(self):
26  actual_type = typing.get_origin(f.type) or f.type
27  value = getattr(self, f.name)
28  if actual_type == list:
29  if value:
30  for el in value:
31  if not isinstance(el, typing.get_args(f.type)):
32  return f
33  elif not isinstance(value, actual_type):
34  return f
35  return None
36 
37  def __post_init__(self):
38  f = self._check_types()
39  if f is not None:
40  raise TypeError(f"Expected type of '{f.name}' to be {f.type}, "
41  f"got {repr(getattr(self, f.name))}")
42 
43 if __name__=='__main__':
44  ch = ChainProp(name='abc', groups=['g1'])
ChainDefInMenu.ChainProp._check_types
def _check_types(self)
Definition: ChainDefInMenu.py:23
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
ChainDefInMenu.ChainProp.__post_init__
def __post_init__(self)
Definition: ChainDefInMenu.py:37
ChainDefInMenu.ChainProp
Definition: ChainDefInMenu.py:9