Reference Guide  2.5.0
psyclone.domain.lfric.lfric_types.LFRicTypes Class Reference

Public Member Functions

def __new__ (cls, name)
 
def __call__ (self)
 

Static Public Member Functions

def init ()
 

Detailed Description

This class implements a singleton that manages LFRic types.
Using the 'call' interface, you can query the data type for
LFRic types, e.g.:

>>> from psyclone.configuration import Config
>>> from psyclone.domain.lfric import LFRicTypes
>>> config = Config.get()
>>> num_dofs_class = LFRicTypes("NumberOfUniqueDofsDataSymbol")
>>> my_var = num_dofs_class("my_num_dofs")
>>> print(my_var.name)
my_num_dofs

It uses the __new__ function to implement the access to the internal
dictionary. This is done to minimise the required code for getting a
value, e. g. compared with ``LFRicTypes.get()("something")``, or
``LFRicType.get("something")``.

Definition at line 52 of file lfric_types.py.

Member Function Documentation

◆ __call__()

def psyclone.domain.lfric.lfric_types.LFRicTypes.__call__ (   self)
This function is only here to trick pylint into thinking that
the object returned from __new__ is callable, meaning that code like:
``LFRicTypes("LFRicIntegerScalarDataType")()`` does not trigger
a pylint warning about not being callable.

Definition at line 109 of file lfric_types.py.

109  def __call__(self):
110  '''This function is only here to trick pylint into thinking that
111  the object returned from __new__ is callable, meaning that code like:
112  ``LFRicTypes("LFRicIntegerScalarDataType")()`` does not trigger
113  a pylint warning about not being callable.
114  '''
115 

◆ __new__()

def psyclone.domain.lfric.lfric_types.LFRicTypes.__new__ (   cls,
  name 
)
The class creation function __new__ is used to actually provide
the object the user is querying. It is well documented that __new__
can return a different instance. This function will first make sure
that the static internal dictionary is initialised (so it acts like
a singleton in that regard). Then it will return the value the user
asked for.

:param str name: the name to query for.

:returns: the corresponding object, which can be a class or an \
    instance.
:rtype: object (various types)

:raises InternalError: if there specified name is not a name for \
    an object managed here.

Definition at line 80 of file lfric_types.py.

80  def __new__(cls, name):
81  '''The class creation function __new__ is used to actually provide
82  the object the user is querying. It is well documented that __new__
83  can return a different instance. This function will first make sure
84  that the static internal dictionary is initialised (so it acts like
85  a singleton in that regard). Then it will return the value the user
86  asked for.
87 
88  :param str name: the name to query for.
89 
90  :returns: the corresponding object, which can be a class or an \
91  instance.
92  :rtype: object (various types)
93 
94  :raises InternalError: if there specified name is not a name for \
95  an object managed here.
96 
97  '''
98  if not LFRicTypes._name_to_class:
99  LFRicTypes.init()
100 
101  try:
102  return LFRicTypes._name_to_class[name]
103  except KeyError as err:
104  raise InternalError(f"Unknown LFRic type '{name}'. Valid values "
105  f"are {LFRicTypes._name_to_class.keys()}") \
106  from err
107 

◆ init()

def psyclone.domain.lfric.lfric_types.LFRicTypes.init ( )
static
This method constructs the required classes and instances, and
puts them into the dictionary.

Definition at line 118 of file lfric_types.py.

118  def init():
119  '''This method constructs the required classes and instances, and
120  puts them into the dictionary.
121  '''
122 
123  # The global mapping of names to the corresponding classes or instances
124  LFRicTypes._name_to_class = {}
125 
126  LFRicTypes._create_precision_from_const_module()
127  LFRicTypes._create_generic_scalars()
128  LFRicTypes._create_lfric_dimension()
129  LFRicTypes._create_specific_scalars()
130  LFRicTypes._create_fields()
131 
132  # Generate LFRic vector-field-data symbols as subclasses of
133  # field-data symbols
134  const = LFRicConstants()
135  for intrinsic in const.VALID_FIELD_INTRINSIC_TYPES:
136  name = f"{intrinsic.title()}VectorFieldDataSymbol"
137  baseclass = LFRicTypes(f"{intrinsic.title()}FieldDataSymbol")
138  LFRicTypes._name_to_class[name] = type(name, (baseclass, ), {})
139 

References psyclone.psyir.nodes.intrinsic_call.IntrinsicCall.intrinsic(), psyclone.psyir.symbols.datatypes.ScalarType.intrinsic, psyclone.psyir.symbols.datatypes.ArrayType.intrinsic(), and psyclone.psyir.symbols.intrinsic_symbol.IntrinsicSymbol.intrinsic().

Here is the call graph for this function:

The documentation for this class was generated from the following file: