def get_field_markup_with_helper(self, id): x = getattr(fixtures, id) if request.method == 'POST': response.headers['Content-Type'] = 'application/json' out = {tuple(k.split('.')): v for k, v in request.params.items()} out = unflatten(out) return to_json(out) else: params = dict(request.params) k = params.pop('field', 'title') field = x.get_field(k) action = params.pop('action', 'edit') prefix = params.pop('prefix', id) extra_vars = copy.deepcopy(params) extra_vars.update({ 'helper': True, 'field': field, 'action': str(action), 'name_prefix': str(prefix), }) return render('tests/field.html', extra_vars=extra_vars)
def get_field_markup_with_helper(self, id): x = getattr(fixtures, id) if request.method == 'POST': response.headers['Content-Type'] = 'application/json' out = { tuple(k.split('.')): v for k, v in request.params.items() } out = unflatten(out) return to_json(out) else: params = dict(request.params) k = params.pop('field', 'title') field = x.get_field(k) action = params.pop('action', 'edit') prefix = params.pop('prefix', id) extra_vars = copy.deepcopy(params) extra_vars.update({ 'helper': True, 'field': field, 'action': str(action), 'name_prefix': str(prefix), }) return render('tests/field.html', extra_vars=extra_vars)
def get_field_markup(self, id): x = getattr(fixtures, id) if request.method == 'POST': response.headers['Content-Type'] = 'application/json' out = { tuple(k.split('.')): v for k, v in request.params.items() } out = unflatten(out) return to_json(out) else: params = dict(request.params) k = params.pop('field', 'title') action = params.pop('action', 'edit') prefix = params.pop('prefix', id) field = x.get_field(k) field_markup = markup_for_field(str(action), field, name_prefix=str(prefix), errors=None, data=params) return render('tests/field.html', extra_vars={ 'field_markup': field_markup })
def get_field_markup(self, id): x = getattr(fixtures, id) if request.method == 'POST': response.headers['Content-Type'] = 'application/json' out = {tuple(k.split('.')): v for k, v in request.params.items()} out = unflatten(out) return to_json(out) else: params = dict(request.params) k = params.pop('field', 'title') action = params.pop('action', 'edit') prefix = params.pop('prefix', id) field = x.get_field(k) field_markup = markup_for_field(str(action), field, name_prefix=str(prefix), errors=None, data=params) return render('tests/field.html', extra_vars={'field_markup': field_markup})
def from_dict(self, d, is_flat=None, opts={}): '''Convert (i.e. load) from a (flat or nested) dict. The supported options (opts) are: * update: bool, str (default: False) Indicate whether input data should update self's relevant attributes. If this option is: - not set: self will be fully reloaded from scratch - set to True or 'shallow': self will be updated in shallow mode - set to 'deep': self will be updated in deep (recursive) mode * use-defaults: bool (default: True) Indicate whether a missing/None value should be initialized to a field-wise default value (or instead, be set to None). This option has no effect if an update is carried out (i.e it only affects a reload). * unserialize-keys: bool (default: False) Indicate whether keys need to be unserialized before anything else happens. This option has no effect if we are loading from a nested dict. * key-prefix: str (default: None) The string prefix for keys if they are to be unserialized. * unserialize-values: str (default: False) Indicate whether values are to unserialized. If given as a string, will be interpreted as a serialization format (see serializers.supported_formats) ''' assert isinstance(d, dict) cls = type(self) # Preprocess and sanitize options opts = copy.copy(opts) # to modify it unserialize_values = opts.get('unserialize-values', False) if unserialize_values: if isinstance(unserialize_values, (bool, int)): unserialize_values = 'default' else: unserialize_values = str(unserialize_values) opts['unserialize-values'] = unserialize_values update = opts.get('update', False) if update: if isinstance(update, (bool, int)): update = 'shallow' else: update = str(update) if not update in ['shallow', 'deep']: update = 'shallow' opts['update'] = update # Decide if input is a flattened dict if is_flat is None: is_flat = isinstance(d.iterkeys().next(), tuple) if is_flat: unserialize_keys = opts.get('unserialize-keys', False) if unserialize_keys: key_prefix = opts.get('key-prefix') kser = serializer_for_key_tuple(key_prefix) is_key = kser.get_key_predicate(basestring, strict=True) d = {kser.loads(k): v for k, v in d.iteritems() if is_key(k)} d = dictization.unflatten(d) # Load self self.load(d, opts) # Allow method chaining return self
def from_dict(self, d, is_flat=None, opts={}): '''Convert (i.e. load) from a (flat or nested) dict. The supported options (opts) are: * update: bool, str (default: False) Indicate whether input data should update self's relevant attributes. If this option is: - not set: self will be fully reloaded from scratch - set to True or 'shallow': self will be updated in shallow mode - set to 'deep': self will be updated in deep (recursive) mode * use-defaults: bool (default: True) Indicate whether a missing/None value should be initialized to a field-wise default value (or instead, be set to None). This option has no effect if an update is carried out (i.e it only affects a reload). * unserialize-keys: bool (default: False) Indicate whether keys need to be unserialized before anything else happens. This option has no effect if we are loading from a nested dict. * key-prefix: str (default: None) The string prefix for keys if they are to be unserialized. * unserialize-values: str (default: False) Indicate whether values are to unserialized. If given as a string, will be interpreted as a serialization format (see serializers.supported_formats) ''' assert isinstance(d, dict) cls = type(self) # Preprocess and sanitize options opts = copy.copy(opts) # to modify it unserialize_values = opts.get('unserialize-values', False) if unserialize_values: if isinstance(unserialize_values, (bool, int)): unserialize_values = 'default' else: unserialize_values = str(unserialize_values) opts['unserialize-values'] = unserialize_values update = opts.get('update', False) if update: if isinstance(update, (bool, int)): update = 'shallow' else: update = str(update) if not update in ['shallow', 'deep']: update = 'shallow' opts['update'] = update # Decide if input is a flattened dict if is_flat is None: is_flat = isinstance(d.iterkeys().next(), tuple) if is_flat: unserialize_keys = opts.get('unserialize-keys', False) if unserialize_keys: key_prefix = opts.get('key-prefix') kser = serializer_for_key_tuple(key_prefix) is_key = kser.get_key_predicate(basestring, strict=True) d = { kser.loads(k): v for k, v in d.iteritems() if is_key(k) } d = dictization.unflatten(d) # Load self self.load(d, opts) # Allow method chaining return self
"a": [1.4, 7.6, 9.7, 5.9, 5.0, 9.1, 11.3], "b": [4.9], "c": { # Here, unflatten() should detect a list u"0": 99, u"1": 100, u"2": 199, }, }, }, u"author": u"lalakis", } d1 = dictization.flatten(d) d2 = dictization.unflatten(d1) def test_flattened_1(): for k in sorted(d1): v = d1.get(k) assert isinstance(v, basestring) or isinstance(v, float) or isinstance(v, int), "%r is not scalar" % (v) @raises(AssertionError) def test_inversed_1(): s0 = json.dumps(d) s2 = json.dumps(d2) # Should fail because d[u'measurements'][u'samples']['c'] is converted to a list eq_(s0, s2)
'a': [ 1.4 ,7.6, 9.7, 5.9, 5.0, 9.1, 11.3, ], 'b': [ 4.9 ], 'c': { # Here, unflatten() should detect a list u'0': 99, u'1': 100, u'2': 199, }, }, }, u'author': u'lalakis', } d1 = dictization.flatten(d) d2 = dictization.unflatten(d1) def test_flattened_1(): for k in sorted(d1): v = d1.get(k) assert isinstance(v, basestring) or isinstance(v, float) or isinstance(v, int), \ '%r is not scalar' %(v) @raises(AssertionError) def test_inversed_1(): s0 = json.dumps(d) s2 = json.dumps(d2) # Should fail because d[u'measurements'][u'samples']['c'] is converted to a list eq_(s0, s2) def test_inversed_2():