tobgu/pyrsistent

`setdefault`, or some superior functionality as part of `transform()`

Open

#95 aberto em 3 de fev. de 2017

Ver no GitHub
 (5 comments) (0 reactions) (0 assignees)Python (141 forks)batch import
enhancementhelp wanted

Métricas do repositório

Stars
 (1.943 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

Consider the following code:

class ApplicationState(PClass):
    shared_resources = pset_field(Injectable)
    # ...

class ExtractedState(PClass):
    applications = pmap_field(str, ApplicationState)

def extract(tfstate):
    result = {}

    for mod in tfstate['modules']:
        for injectable in _extract_resources_from_module(mod):
            app_state = result.setdefault(injectable.app,
                                          {"shared_resources": set(),
                                           "service_resources": {}})
            app_state["shared_resources"].add(injectable)

   return ExtractedState.create(freeze({"applications": result}))

Constructing the pyrsistent objects is quite annoying, which is why I'm doing it with mutable objects, due to lack of setdefault or "create a default object in transform()". Ideally I'd do:

extracted_state = ExtractedState()
extracted_state = extracted_state.transform(
    ["applications", "newapp", "shared_resources"], lambda ps: ps.add(obj))

but since newapp doesn't exist it blows up...

Guia do colaborador