run_the_race
run_the_race

Reputation: 2368

Python: How to conditionally add item to list within dictionary

Say I have this function that returns a dictionary, where each value in the dictionary is actually a list:

def get_base_examples(gui, dense=False):
    return {
        f'{gui.capitalize()} Label': [
            f'<label {gui}-form="label">Cannon ball</label>',
            f'<label {gui}-form="label dense">Pogo stick</label>',
        ],
        f'{gui.capitalize()} Help-text': [
            f'<div {gui}-form="help-text">Orange sherbert</div>',
        ],
        f'{gui.capitalize()} Errors': [
            f'<div {gui}-form="errors">Cabbage patch</div>',
        ],

If the arg dense is False, then I would not like this item to be appened to the lsit

f'<label {gui}-form="label dense">Pogo stick</label>'

I know I could break apart the items and conditionally build it like this:

def get_base_examples(gui, dense=False):
    output = {}
    l = [f'<label {gui}-form="label">Cannon ball</label>']
    if dense:
        l.append(f'<label {gui}-form="label dense">Pogo stick</label>')
    f'{gui.capitalize()} Label': l,
    f'{gui.capitalize()} Help-text': [
        f'<div {gui}-form="help-text">Orange sherbert</div>',
    ],
    f'{gui.capitalize()} Errors': [
        f'<div {gui}-form="errors">Cabbage patch</div>',
    ],

Although this is fine for this toy example, say one has multiple pieces lists that depend on the variable dense, then the flow of information is disjointed, the conditional bits of info at the top, and the remaineder of the info in order.

Is there a neater way? Something like this:

def get_base_examples(gui, dense=False):
    return {
        f'{gui.capitalize()} Label': [
            f'<label {gui}-form="label">Cannon ball</label>',
            if dense:
                f'<label {gui}-form="label dense">Pogo stick</label>',
        ],
        f'{gui.capitalize()} Help-text': [
            f'<div {gui}-form="help-text">Orange sherbert</div>',
        ],
        f'{gui.capitalize()} Errors': [
            f'<div {gui}-form="errors">Cabbage patch</div>',
        ],

This is the best I have come up with so far:

    return {
        f'{gui.capitalize()} Label': [
            f'<label {gui}-form="label">Lease duration</label>',
            f'<label {gui}-form="label dense">Lease duration</label>',
        ][:(2 if dense else -1)],
        ...

Upvotes: 0

Views: 58

Answers (1)

pcoates
pcoates

Reputation: 2307

maybe something like...

class ItemsBuilder:
    def __init__(self):
        self.items = []

    def with_item(self, txt, wanted=True):
        if wanted:
            self.items.append(txt)
        return self

    def build(self):
        return self.items
    

def get_base_examples(gui, dense=False):
    return {
        f'{gui.capitalize()} Label': ItemsBuilder()
                .with_item(f'<label {gui}-form="label">Cannon ball</label>')
                .with_item(f'<label {gui}-form="label dense">Pogo stick</label>', dense)
                .build(),
        f'{gui.capitalize()} Help-text': ItemsBuilder()
                .with_item(f'<div {gui}-form="help-text">Orange sherbert</div>')
                .build(),
        f'{gui.capitalize()} Errors': ItemsBuilder()
                .with_item(f'<div {gui}-form="errors">Cabbage patch</div>')
                .build()
    }

Upvotes: 2

Related Questions