Skip to content

Fill array from fields around #293

@XVincentX

Description

@XVincentX

Hello fellows, 👋

I have a structure like this:

{
  
  "q1": "No",
  "q2": "Yes"
}

And I am looking to get out a collection where, based on q1 and q2, there will be elements , like [1, 2].

I have been able to get to the solution I want by basically accumulating all the items as a dictionary, then converting to a collection and filtering out foreign values, but the code is objectively a bit weird and was wondering if you have any recommendations to make it better @kurtbrose @mahmoud

(
    {
        "c1": (T["q1"].upper(), And((Match(M == "YES")), Val(1), default=SKIP)),
        "c2": (T["q2"].upper(), And((Match(M == "YES")), Val(2), default=SKIP)),
        "c3": (T["q2"].upper(), And((Match(M == "NO")), Val(3), default=SKIP)),
    },
    T.items(),
    [T[1]],
    lambda t: [i for i in t if isinstance(i, int)],
)

Or a little bit better:

(
    {
        "c1": (T["q1"].upper(), And((Match(M == "YES")), Val(1), default=SKIP)),
        "c2": (T["q2"].upper(), And((Match(M == "YES")), Val(2), default=SKIP)),
        "c3": (T["q2"].upper(), And((Match(M == "NO")), Val(3), default=SKIP)),
    },
    T.values(),
    [lambda x: x if isinstance(x, int) else SKIP],
)

Or even better, for this specific case:

(
    {
        "c1": (T["c-29649"].upper(), And((Match("YES")), Val(756), default=SKIP)),
        "c2": (
            T["c-25895"].upper(),
            Match(
                Switch(
                    [
                        (And("YES"), Val(1)),
                        (And("NO"), Val(2)),
                    ],
                    default=SKIP,
                )
            ),
        ),
    },
    T.values(),
    [lambda t: t if isinstance(t, int) else SKIP],
)

...but I am still going through an intermediate dictionary which seems unnecessary

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions