Exemplo n.º 1
0
 def __init__(self, data=None, cfuncs=None, cmems=None):
     if data is None:
         data = {}
     else:
         cfuncs = set(cfuncs)
         cmems = set(cmems)
     cmss = {}
     for (name, mfd) in data.iteritems():
         # mfd: members, funcs, default
         mfd_parsed = dict(
             cfuncs=mfd['funcs'],
             cmems=mfd['members'],
             default=mfd.get('default', False),
             cfuncs_parsed=concatfunc(expand_braces, mfd['funcs']),
         )
         setdiff_mems = set(mfd_parsed['cmems']) - cmems
         setdiff_funcs = set(mfd_parsed['cfuncs_parsed']) - cfuncs
         if setdiff_mems:
             raise ValueError('C-members %s of subset "%s" are not '
                              'recognized' % (strset(setdiff_mems), name))
         if setdiff_funcs:
             raise ValueError('C-funcs %s of subset "%s" are not '
                              'recognized' % (strset(setdiff_funcs), name))
         cmss[name] = mfd_parsed
     # converted data
     self._cmss_ = cmss
     self._cfunc_to_cmss_ = _cmss_inverse(cmss, 'cfuncs_parsed')
     self._cmem_to_cmss_ = _cmss_inverse(cmss, 'cmems')
     # set default flags
     self._set_flags_default()
Exemplo n.º 2
0
 def __init__(self, data=None, cfuncs=None, cmems=None):
     if data is None:
         data = {}
     else:
         cfuncs = set(cfuncs)
         cmems = set(cmems)
     cmss = {}
     for (name, mfd) in data.iteritems():
         # mfd: members, funcs, default
         mfd_parsed = dict(
             cfuncs=mfd['funcs'],
             cmems=mfd['members'],
             default=mfd.get('default', False),
             cfuncs_parsed=concatfunc(expand_braces, mfd['funcs']),
             )
         setdiff_mems = set(mfd_parsed['cmems']) - cmems
         setdiff_funcs = set(mfd_parsed['cfuncs_parsed']) - cfuncs
         if setdiff_mems:
             raise ValueError(
                 'C-members %s of subset "%s" are not '
                 'recognized' % (strset(setdiff_mems), name))
         if setdiff_funcs:
             raise ValueError(
                 'C-funcs %s of subset "%s" are not '
                 'recognized' % (strset(setdiff_funcs), name))
         cmss[name] = mfd_parsed
     # converted data
     self._cmss_ = cmss
     self._cfunc_to_cmss_ = _cmss_inverse(cmss, 'cfuncs_parsed')
     self._cmem_to_cmss_ = _cmss_inverse(cmss, 'cmems')
     # set default flags
     self._set_flags_default()
Exemplo n.º 3
0
 def num(self, *args):
     """
     Get shape of array (Ni, Nj, ...) along given index ('i', 'j', ...)
     """
     if len(args) == 1:
         args = [a.strip() for a in args[0].split(',')]
     if set(args) > self._idxset_:
         istr = strset(set(args) - self._idxset_)
         raise ValueError("index(es) %s doesn't exist" % istr)
     nums = [getattr(self, 'num_%s' % i) for i in args]
     if len(nums) == 1:
         return nums[0]
     else:
         return nums
Exemplo n.º 4
0
def check_multiple_values(args, kwds, keyorder, func_name):
    """
    Raise ValueError if arguments in args and kwds specifies same variables

    >>> check_multiple_values([1, 2], dict(c=1, d=2), ['a', 'b'], 'FUNC_NAME')
    >>> check_multiple_values([1, 2], dict(a=1, d=2), ['a', 'b'], 'FUNC_NAME')
    Traceback (most recent call last):
        ...
    ValueError: FUNC_NAME() got multiple values for variable in {a}
    >>> check_multiple_values([1, 2], dict(a=1, b=2), ['a', 'b'], 'FUNC_NAME')
    Traceback (most recent call last):
        ...
    ValueError: FUNC_NAME() got multiple values for variable in {a, b}

    """
    mval_args = set(keyorder[:len(args)]) & set(kwds)
    if mval_args:
        raise ValueError(
            '%s() got multiple values for variable in %s' %
            (func_name, strset(mval_args)))
Exemplo n.º 5
0
 def _set_all(self, **kwds):
     # decompose keyword arguments into its disjoint subsets
     kwds_scalar = subdict_by_filter(kwds, self._is_cmem_scalar, True)
     kwds_array = subdict_by_filter(kwds, self._is_cmem_array, True)
     kwds_array_alias = subdict_by_filter(kwds, self.array_alias, True)
     kwds_object = subdict_by_filter(kwds, self._is_cmem_object, True)
     if kwds:  # there should not be remaining arguments
         raise ValueError(
             "undefined keyword arguments: %s" % kwds)
     # allocate struct
     self._struct_ = self._struct_type_()
     self._struct_p_ = pointer(self._struct_)
     # set scalar variables including num_*
     scalarvals = dict_override(
         self._cmems_default_scalar_, kwds_scalar, addkeys=True)
     numkeyset = set('num_%s' % i for i in self._idxset_)
     num_lack = numkeyset - set(scalarvals)
     if num_lack:
         raise ValueError("%s are mandatory" % strset(num_lack))
     nums = dict((k[len('num_'):], v) for (k, v) in scalarvals.items()
                 if k in numkeyset)
     nonnum_scalarvals = dict((k, v) for (k, v) in scalarvals.items()
                              if k not in numkeyset)
     self.__set_num(**nums)
     self.setv(**nonnum_scalarvals)
     # allocate C array data and set the defaults
     self._set_cdata()
     cmems_default_array = self._cmems_default_array_
     array_allocated = filter(  # remove if not allocated
         self._cmemsubsets_parsed_.cmem_need_alloc, cmems_default_array)
     cmems_default_array_allocated = dict((k, cmems_default_array[k])
                                          for k in array_allocated)
     self.setv(**dict_override(
         cmems_default_array_allocated, kwds_array, addkeys=True))
     self.setv(**kwds_array_alias)
     self.setv(**kwds_object)