def json_to_dated_python(self): return conv.pipe( conv.condition( conv.test_isinstance(datetime.date), conv.noop, conv.condition( conv.test_isinstance(int), conv.pipe( conv.test_between(1870, 2099), conv.function(lambda year: datetime.date(year, 1, 1)), ), conv.pipe( conv.test_isinstance(basestring_type), conv.test(year_or_month_or_day_re.match, error = N_('Invalid date')), conv.function(lambda birth: '-'.join((birth.split('-') + ['01', '01'])[:3])), conv.iso8601_input_to_date, ), ), ), conv.test_between(datetime.date(1870, 1, 1), datetime.date(2099, 12, 31)), )
def validate_dated_slice_json(slice, state = None): if slice is None: return None, None state = conv.add_ancestor_to_state(state, slice) validated_slice, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( base = conv.pipe( validate_dated_value_json, conv.test_greater_or_equal(0), ), constant_amount = validate_dated_value_json, comment = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), rate = conv.pipe( validate_dated_value_json, conv.test_between(0, 1), ), threshold = conv.pipe( validate_dated_value_json, conv.test_greater_or_equal(0), ), date = conv.pipe( validate_dated_value_json, conv.test_between(0, 1), #TODO: changer ), ), constructor = collections.OrderedDict, drop_none_values = 'missing', keep_value_order = True, ), )(slice, state = state) conv.remove_ancestor_from_state(state, slice) return validated_slice, errors
def validate_dated_slice_json(slice, state=None): if slice is None: return None, None state = conv.add_ancestor_to_state(state, slice) validated_slice, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( base=conv.pipe( validate_dated_value_json, conv.test_greater_or_equal(0), ), constant_amount=validate_dated_value_json, comment=conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), rate=conv.pipe( validate_dated_value_json, conv.test_between(0, 1), ), threshold=conv.pipe( validate_dated_value_json, conv.test_greater_or_equal(0), ), date=conv.pipe( validate_dated_value_json, conv.test_between(0, 1), #TODO: changer ), ), constructor=collections.OrderedDict, drop_none_values='missing', keep_value_order=True, ), )(slice, state=state) conv.remove_ancestor_from_state(state, slice) return validated_slice, errors
def define_scenario(year, column_code): scenario = base.tax_benefit_system.new_scenario() column = base.tax_benefit_system.column_by_name[column_code] entity = column.entity start = 1990 if column.start is None else column.start.year end = 2050 if column.end is None else column.end.year value = 1500 if conv.test_between(start, end)(year)[1] is None else 0 parent1 = { "activite": u'Actif occupé', "birth": 1970, "sali": 24000, "statmarit": u'Célibataire', } enfants = [ # dict( # activite = u'Étudiant, élève', # birth = '2002-02-01', # ), # dict( # activite = u'Étudiant, élève', # birth = '2000-04-17', # ), ] famille = dict() menage = dict() foyer_fiscal = dict() if entity == 'ind': parent1[column_code] = value elif entity == 'foy': foyer_fiscal[column_code] = value elif entity == 'fam': famille[column_code] = value elif entity == 'men': menage[column_code] = value scenario.init_single_entity( period = year, parent1 = parent1, # parent2 = dict(), enfants = enfants, famille = famille, menage = menage, foyer_fiscal = foyer_fiscal, ) scenario.suggest() return scenario
def define_scenario(year, column_code): scenario = base.tax_benefit_system.new_scenario() column = base.tax_benefit_system.column_by_name[column_code] entity = column.entity start = 1990 if column.start is None else column.start.year end = 2050 if column.end is None else column.end.year value = 1500 if conv.test_between(start, end)(year)[1] is None else 0 parent1 = { "activite": u'Actif occupé', "birth": 1970, "sali": 24000, "statmarit": u'Célibataire', } enfants = [ # dict( # activite = u'Étudiant, élève', # birth = '2002-02-01', # ), # dict( # activite = u'Étudiant, élève', # birth = '2000-04-17', # ), ] famille = dict() menage = dict() foyer_fiscal = dict() if entity == 'ind': parent1[column_code] = value elif entity == 'foy': foyer_fiscal[column_code] = value elif entity == 'fam': famille[column_code] = value elif entity == 'men': menage[column_code] = value scenario.init_single_entity( period=year, parent1=parent1, # parent2 = dict(), enfants=enfants, famille=famille, menage=menage, foyer_fiscal=foyer_fiscal, ) scenario.suggest() return scenario
def input_to_period_tuple(value, state=None): """Convert an input string to a period tuple. .. note:: This function doesn't return a period, but a tuple that allows to construct a period. >>> input_to_period_tuple('2014') (('year', 2014), None) >>> input_to_period_tuple('2014:2') (('year', 2014, 2), None) >>> input_to_period_tuple('2014-2') (('month', (2014, 2)), None) >>> input_to_period_tuple('2014-3:12') (('month', (2014, 3), 12), None) >>> input_to_period_tuple('2014-2-3') (('day', (2014, 2, 3)), None) >>> input_to_period_tuple('2014-3-4:2') (('day', (2014, 3, 4), 2), None) >>> input_to_period_tuple('year:2014') (('year', '2014'), None) >>> input_to_period_tuple('year:2014:2') (('year', '2014', '2'), None) >>> input_to_period_tuple('year:2014-2:2') (('year', '2014-2', '2'), None) """ if value is None: return value, None if state is None: state = conv.default_state split_value = tuple( clean_fragment for clean_fragment in (fragment.strip() for fragment in value.split(':')) if clean_fragment) if not split_value: return None, None if len(split_value) == 1: split_value = tuple( clean_fragment for clean_fragment in (fragment.strip() for fragment in split_value[0].split('-')) if clean_fragment) if len(split_value) == 1: return conv.pipe( conv.input_to_strict_int, conv.test_greater_or_equal(0), conv.function(lambda year: ('year', year)), )(split_value[0], state=state) if len(split_value) == 2: return conv.pipe( conv.struct(( conv.pipe( conv.input_to_strict_int, conv.test_greater_or_equal(0), ), conv.pipe( conv.input_to_strict_int, conv.test_between(1, 12), ), ), ), conv.function(lambda month_tuple: ('month', month_tuple)), )(split_value, state=state) if len(split_value) == 3: return conv.pipe( conv.struct(( conv.pipe( conv.input_to_strict_int, conv.test_greater_or_equal(0), ), conv.pipe( conv.input_to_strict_int, conv.test_between(1, 12), ), conv.pipe( conv.input_to_strict_int, conv.test_between(1, 31), ), ), ), conv.function(lambda day_tuple: ('day', day_tuple)), )(split_value, state=state) return split_value, state._( 'Instant string contains too much "-" for a year, a month or a day' ) if len(split_value) == 2: split_start = tuple( clean_fragment for clean_fragment in (fragment.strip() for fragment in split_value[0].split('-')) if clean_fragment) size, error = conv.input_to_int(split_value[1], state=state) if error is None: if len(split_start) == 1: start, error = conv.pipe( conv.input_to_strict_int, conv.test_greater_or_equal(0), )(split_start[0], state=state) if error is None: return ('year', start, size), None elif len(split_start) == 2: start, error = conv.struct(( conv.pipe( conv.input_to_strict_int, conv.test_greater_or_equal(0), ), conv.pipe( conv.input_to_strict_int, conv.test_between(1, 12), ), ), )(split_start, state=state) if error is None: return ('month', start, size), None elif len(split_start) == 3: start, error = conv.struct(( conv.pipe( conv.input_to_strict_int, conv.test_greater_or_equal(0), ), conv.pipe( conv.input_to_strict_int, conv.test_between(1, 12), ), conv.pipe( conv.input_to_strict_int, conv.test_between(1, 31), ), ), )(split_start, state=state) if error is None: return ('day', start, size), None return split_value, None
def validate_node_xml_json(node, state=None): validated_node, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( code=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, conv.not_none, ), color=conv.pipe( conv.test_isinstance(basestring_type), conv.function(lambda colors: colors.split(',')), conv.uniform_sequence( conv.pipe( conv.input_to_int, conv.test_between(0, 255), conv.not_none, ), ), conv.test( lambda colors: len(colors) == 3, error=N_('Wrong number of colors in triplet.')), conv.function(lambda colors: ','.join( to_unicode(color) for color in colors)), ), desc=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, conv.not_none, ), NODE=conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_node_xml_json, drop_none_items=True, ), conv.empty_to_none, ), shortname=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, conv.not_none, ), tail=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_text, ), text=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_text, ), typevar=conv.pipe( conv.test_isinstance(basestring_type), conv.input_to_int, conv.test_equals(2), ), ), constructor=collections.OrderedDict, drop_none_values='missing', keep_value_order=True, ), )(node, state=state or conv.default_state) if errors is not None: return validated_node, errors if not validated_node.get('NODE'): validated_node, errors = conv.struct( dict(code=conv.test_in(tax_benefit_system.variables), ), default=conv.noop, )(validated_node, state=state) return validated_node, errors