def test_midway_branch(): # midway branch, but then continues actual = _make_stack(Match(Switch([(1, 1), ('a', 'a'), ([None], T.a)]))) expected = """\ Traceback (most recent call last): File "test_error.py", line ___, in _make_stack glom(target, spec) File "core.py", line ___, in glom raise err glom.core.PathAccessError: error raised while processing, details below. Target-spec trace (most recent last): - Target: [None] - Spec: Match(Switch([(1, 1), ('a', 'a'), ([None], T.a)])) + Spec: Switch([(1, 1), ('a', 'a'), ([None], T.a)]) |\\ Spec: 1 |X glom.matching.MatchError: [None] does not match 1 |\\ Spec: 'a' |X glom.matching.MatchError: [None] does not match 'a' |\\ Spec: [None] || Spec: T.a glom.core.PathAccessError: could not access 'a', part 0 of T.a, got error: AttributeError("'list' object has no attribute 'a'") """ if _PY2: # see https://github.com/pytest-dev/pytest/issues/1347 assert len(actual.split("\n")) == len(expected.split("\n")) else: assert actual == expected # branch and another branch actual = _make_stack( Match( Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), ([None], T.a)]))]))) expected = """\ Traceback (most recent call last): File "test_error.py", line ___, in _make_stack glom(target, spec) File "core.py", line ___, in glom raise err glom.core.PathAccessError: error raised while processing, details below. Target-spec trace (most recent last): - Target: [None] - Spec: Match(Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', '... + Spec: Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), (... |\\ Spec: 1 |X glom.matching.MatchError: [None] does not match 1 |\\ Spec: 'a' |X glom.matching.MatchError: [None] does not match 'a' |\\ Spec: [None] |+ Spec: Switch([(1, 1), ('a', 'a'), ([None], T.a)]) ||\\ Spec: 1 ||X glom.matching.MatchError: [None] does not match 1 ||\\ Spec: 'a' ||X glom.matching.MatchError: [None] does not match 'a' ||\\ Spec: [None] ||| Spec: T.a glom.core.PathAccessError: could not access 'a', part 0 of T.a, got error: AttributeError("'list' object has no attribute 'a'") """ if _PY2: # see https://github.com/pytest-dev/pytest/issues/1347 assert len(actual.split("\n")) == len(expected.split("\n")) else: assert actual == expected
def generer_fichier_circonscriptions_legislatives(source, dest): """À partir du fichier des circonscriptions parlementaires de Sciences Po""" with source.open() as f: circos = json.load(f) with id_from_file("departements.csv") as id_dep, id_from_file( "circonscriptions_legislatives.csv") as id_circ: spec = { "id": ("properties", code_circonscription, Invoke(id_circ).specs(code=T)), "code": ("properties", code_circonscription), "departement_id": ( "properties.code_dpt", Invoke(INTERIEUR_VERS_DEPARTEMENT.get).specs(T, T), Match( Switch({ Not(Regex(NON_DEPARTEMENT)): Invoke(id_dep).specs(code=T) }), default=NULL, ), ), "geometry": ("geometry", geometrie_circonscription), } with lzma.open(dest, "wt") as f: w = csv.DictWriter(f, fieldnames=spec) w.writeheader() w.writerows( sorted(glom(circos["features"], [spec]), key=itemgetter("code"))) for i in range(1, 12): code = f"99-{i:02d}" w.writerow({ "id": id_circ(code=code), "code": code, "departement_id": NULL, "geometry": NULL, })
def test_switch(): data = {'a': 1, 'b': 2} cases = [('c', lambda t: 3), ('a', 'a')] cases2 = dict(cases) assert glom(data, Switch(cases)) == 1 assert glom(data, Switch(cases2)) == 1 assert glom({'c': None}, Switch(cases)) == 3 assert glom({'c': None}, Switch(cases2)) == 3 assert glom(None, Switch(cases, default=4)) == 4 assert glom(None, Switch({'z': 26}, default=4)) == 4 with pytest.raises(MatchError): glom(None, Switch(cases)) with pytest.raises(ValueError): Switch({}) with pytest.raises(TypeError): Switch("wrong type") assert glom(None, Switch({S(a=lambda t: 1): S['a']})) == 1 repr(Switch(cases))