def flag_words(u): ''' clp.flag_words yields the persistent map of optional flag words recognized by the given command-line parser clp. ''' if pimms.is_pmap(u): return u elif pimms.is_map(u): return pyr.pmap(u) else: raise ValueError('flag_words must be a mapping')
def default_values(dv): ''' clp.default_values yields the persistent map of default values for the given command-line parser clp. ''' if pimms.is_pmap(dv): return dv elif pimms.is_map(dv): return pyr.pmap(dv) else: raise ValueError('default_value must be a mapping')
def option_characters(u): ''' clp.option_characters yields the persistent map of optional characters recognized by the given command-line parser clp. ''' if pimms.is_pmap(u): return u elif pimms.is_map(u): return pyr.pmap(u) else: raise ValueError('option_characters must be a mapping')
def area_name_to_id(vai): ''' mdl.area_name_to_id is a persistent map whose keys are area names (such as 'V1' or 'hV4') and whose values are the area id (a number greater than 0) for that area. mdl.area_name_to_id is a parameter which may be provided as a lsit of area names, in which case the first is assumed to be area 1, the next area 2, etc. ''' if vai is None: return None if not pimms.is_map(vai): return pyr.pmap({nm:(ii+1) for (ii,nm) in enumerate(vai)}) elif pimms.is_pmap(vai): return vai else: return pyr.pmap(vai)
def parameters(params): ''' mdl.parameters is a persistent map of the parameters for the given SchiraModel object mdl. ''' if not pimms.is_pmap(params): params = pyr.pmap(params) # do the translations that we need... scale = params['scale'] if pimms.is_number(scale): params = params.set('scale', (scale, scale)) elif not is_tuple(scale): params = params.set('scale', tuple(scale)) shear = params['shear'] if pimms.is_number(shear) and np.isclose(shear, 0): params = params.set('shear', ((1, 0), (0, 1))) elif shear[0][0] != 1 or shear[1][1] != 1: raise RuntimeError('shear matrix diagonal elements must be 1!') elif not is_tuple(shear) or not all(is_tuple(s) for s in shear): params.set('shear', tuple([tuple(s) for s in shear])) center = params['center'] if pimms.is_number(center) and np.isclose(center, 0): params = params.set('center', (0.0, 0.0)) return pimms.persist(params, depth=None)
def test_persist(self): ''' test_persist() tests pimms persist() function. ''' from .lazy_complex import LazyComplex z = LazyComplex((1.0, 2.0)) self.assertFalse(z.is_persistent()) self.assertTrue(z.is_transient()) z.persist() self.assertTrue(z.is_persistent()) self.assertFalse(z.is_transient()) z = LazyComplex((1.0, 2.0)) self.assertFalse(z.is_persistent()) self.assertTrue(z.is_transient()) zp = pimms.persist(z) self.assertTrue(zp.is_persistent()) self.assertFalse(zp.is_transient()) self.assertFalse(z.is_persistent()) self.assertTrue(z.is_transient()) m0 = { 'a': [1, 2, 3], 'b': (2, 3, 4), 'c': { 'd': 'abc', 'e': set(['def', 'ghi']), 'f': frozenset([10, 11, 12]) }, 'z': z, 'zp': zp, 'q': (1, 2, [3, 4]), 't': pimms.itable({ 'c1': range(10), 'c2': range(1, 11), 'c3': range(2, 12) }) } m = pimms.persist(m0) self.assertIs(m['b'], m0['b']) self.assertIsNot(m['a'], m0['a']) self.assertTrue(all(ai == bi for (ai, bi) in zip(m['a'], m0['a']))) self.assertTrue(pimms.is_pmap(m['c'])) self.assertIs(m['c']['d'], m0['c']['d']) self.assertTrue(isinstance(m['c']['e'], frozenset)) self.assertTrue(isinstance(m['c']['f'], frozenset)) self.assertTrue( all(ai == bi for (ai, bi) in zip(m['c']['f'], m0['c']['f']))) self.assertTrue(m['z'].is_persistent()) self.assertIs(m['zp'], m0['zp']) self.assertIs(m['q'], m0['q']) self.assertIs(m['q'][2], m0['q'][2]) self.assertTrue(pimms.is_itable(m['t'])) self.assertTrue(m['t'].is_persistent()) m = pimms.persist(m0, depth=1) self.assertIs(m['b'], m0['b']) self.assertIsNot(m['a'], m0['a']) self.assertTrue(all(ai == bi for (ai, bi) in zip(m['a'], m0['a']))) self.assertTrue(pimms.is_pmap(m['c'])) self.assertIs(m['c']['d'], m0['c']['d']) self.assertTrue(isinstance(m['c']['e'], set)) self.assertTrue(isinstance(m['c']['f'], frozenset)) self.assertTrue( all(ai == bi for (ai, bi) in zip(m['c']['f'], m0['c']['f']))) self.assertTrue(m['z'].is_persistent()) self.assertIs(m['zp'], m0['zp']) self.assertIs(m['q'], m0['q']) self.assertIs(m['q'][2], m0['q'][2]) self.assertTrue(pimms.is_itable(m['t'])) self.assertTrue(m['t'].is_persistent())
def meta_data(md): ''' obj.meta_data is a persistent map of meta-data provided to the given object, obj. ''' if md is None: return pyr.m() return md if pimms.is_pmap(md) else pyr.pmap(md)