• How to enter multiple, similar, dictionaries?

    From Chris Green@21:1/5 to All on Mon Dec 11 15:16:38 2023
    Is there a way to abbreviate the following code somehow?

    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    It's effectively a 'table' with columns named 'dev', 'input' and
    'name' and I want to access the values of the table using the variable
    name.

    I could, obviously, store the data in a database (sqlite), I have some
    similar data in a database already but the above sort of format in
    Python source is more human readable and accessible. I'm just looking
    for a less laborious way of entering it really.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Mon Dec 11 15:54:43 2023
    Chris Green <cl@isbd.net> writes:
    Is there a way to abbreviate the following code somehow?
    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    s = \
    [ 'dev', 'input', 'name',
    'lv', b:='bbb', '1', ( l:='Leisure', v:=' volts', l + v )[ -1 ],
    'sv', b, '0', ( s:='Starter', s + v )[ -1 ],
    'la', b, '2', ( a:=' Amps', l + a )[ -1 ],
    'sa', b, '3', s + a,
    'bv', 'adc2', 0, 'BowProp' + v.title() ]

    for name in s[ 3:: 4 ]:
    globals()[ name ]= dict()

    for i in range( 4, len( s )):
    key_offset = i % 4
    if 3 == key_offset: continue
    name = s[ i // 4 * 4 - 1 ]
    key = s[ key_offset ]
    value = s[ i ]
    globals()[ name ][ key ]= value

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Chris Green on Mon Dec 11 15:57:48 2023
    Chris Green <cl@isbd.net> wrote:
    Is there a way to abbreviate the following code somehow?

    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    It's effectively a 'table' with columns named 'dev', 'input' and
    'name' and I want to access the values of the table using the variable
    name.

    Or, more sensibly, make the above into a list (or maybe dictionary)
    of dictionaries:-

    adccfg = [
    {'abbr':'lv', 'dev':'bbb', 'input':'1', 'name':'Leisure volts'},
    {'abbr':'sv', 'dev':'bbb', 'input':'0', 'name':'Starter volts'},
    {'abbr':'la', 'dev':'bbb', 'input':'2', 'name':'Leisure Amps'},
    {'abbr':'sa', 'dev':'bbb', 'input':'3', 'name':'Starter Amps'},
    {'abbr':'bv', 'dev':'adc2', 'input':0, 'name':'BowProp Volts'}
    ]

    This pickles nicely, I just want an easy way to enter the data!

    I could, obviously, store the data in a database (sqlite), I have some similar data in a database already but the above sort of format in
    Python source is more human readable and accessible. I'm just looking
    for a less laborious way of entering it really.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Ribbens@21:1/5 to Chris Green on Mon Dec 11 16:23:06 2023
    On 2023-12-11, Chris Green <cl@isbd.net> wrote:
    Chris Green <cl@isbd.net> wrote:
    Is there a way to abbreviate the following code somehow?

    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    It's effectively a 'table' with columns named 'dev', 'input' and
    'name' and I want to access the values of the table using the variable
    name.

    Or, more sensibly, make the above into a list (or maybe dictionary)
    of dictionaries:-

    adccfg = [
    {'abbr':'lv', 'dev':'bbb', 'input':'1', 'name':'Leisure volts'},
    {'abbr':'sv', 'dev':'bbb', 'input':'0', 'name':'Starter volts'},
    {'abbr':'la', 'dev':'bbb', 'input':'2', 'name':'Leisure Amps'},
    {'abbr':'sa', 'dev':'bbb', 'input':'3', 'name':'Starter Amps'},
    {'abbr':'bv', 'dev':'adc2', 'input':0, 'name':'BowProp Volts'}
    ]

    This pickles nicely, I just want an easy way to enter the data!

    adccfg = [
    dict(zip(('abbr', 'dev', 'input', 'name'), row))
    for row in (
    ('lv', 'bbb', '1', 'Leisure volts'),
    ('sv', 'bbb', '0', 'Starter volts'),
    ('la', 'bbb', '2', 'Leisure Amps'),
    ('sa', 'bbb', '3', 'Starter Amps'),
    ('bv', 'adc2', 0, 'BowProp Volts'),
    )
    ]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Jon Ribbens on Mon Dec 11 16:50:24 2023
    Jon Ribbens <jon+usenet@unequivocal.eu> wrote:
    On 2023-12-11, Chris Green <cl@isbd.net> wrote:
    Chris Green <cl@isbd.net> wrote:
    Is there a way to abbreviate the following code somehow?

    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    It's effectively a 'table' with columns named 'dev', 'input' and
    'name' and I want to access the values of the table using the variable
    name.

    Or, more sensibly, make the above into a list (or maybe dictionary)
    of dictionaries:-

    adccfg = [
    {'abbr':'lv', 'dev':'bbb', 'input':'1', 'name':'Leisure volts'},
    {'abbr':'sv', 'dev':'bbb', 'input':'0', 'name':'Starter volts'},
    {'abbr':'la', 'dev':'bbb', 'input':'2', 'name':'Leisure Amps'},
    {'abbr':'sa', 'dev':'bbb', 'input':'3', 'name':'Starter Amps'},
    {'abbr':'bv', 'dev':'adc2', 'input':0, 'name':'BowProp Volts'}
    ]

    This pickles nicely, I just want an easy way to enter the data!

    adccfg = [
    dict(zip(('abbr', 'dev', 'input', 'name'), row))
    for row in (
    ('lv', 'bbb', '1', 'Leisure volts'),
    ('sv', 'bbb', '0', 'Starter volts'),
    ('la', 'bbb', '2', 'Leisure Amps'),
    ('sa', 'bbb', '3', 'Starter Amps'),
    ('bv', 'adc2', 0, 'BowProp Volts'),
    )
    ]

    Neat, I like that, thank you.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Chris Green via Python-list on Mon Dec 11 17:57:58 2023
    On 2023-12-11 15:57, Chris Green via Python-list wrote:
    Chris Green <cl@isbd.net> wrote:
    Is there a way to abbreviate the following code somehow?

    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    It's effectively a 'table' with columns named 'dev', 'input' and
    'name' and I want to access the values of the table using the variable
    name.

    Or, more sensibly, make the above into a list (or maybe dictionary)
    of dictionaries:-

    adccfg = [
    {'abbr':'lv', 'dev':'bbb', 'input':'1', 'name':'Leisure volts'},
    {'abbr':'sv', 'dev':'bbb', 'input':'0', 'name':'Starter volts'},
    {'abbr':'la', 'dev':'bbb', 'input':'2', 'name':'Leisure Amps'},
    {'abbr':'sa', 'dev':'bbb', 'input':'3', 'name':'Starter Amps'},
    {'abbr':'bv', 'dev':'adc2', 'input':0, 'name':'BowProp Volts'}
    ]

    This pickles nicely, I just want an easy way to enter the data!

    I could, obviously, store the data in a database (sqlite), I have some
    similar data in a database already but the above sort of format in
    Python source is more human readable and accessible. I'm just looking
    for a less laborious way of entering it really.

    How about:

    keys = ['abbr', 'dev', 'input', 'name']
    adccfg = [
    ('lv', 'bbb', '1', 'Leisure volts'),
    ('sv', 'bbb', '0', 'Starter volts'),
    ('la', 'bbb', '2', 'Leisure Amps'),
    ('sa', 'bbb', '3', 'Starter Amps'),
    ('bv', 'adc2', '0', 'BowProp Volts'),
    ]
    adccfg = [dict(zip(keys, row)) for row in adccfg]

    or even:

    keys = ['abbr', 'dev', 'input', 'name']
    adccfg = '''\
    lv,bbb,1,Leisure volts
    sv,bbb,0,Starter volts
    la,bbb,2,Leisure Amps
    sa,bbb,3,Starter Amps
    bv,adc2,0,BowProp Volts
    '''
    adccfg = [dict(zip(keys, line.split(','))) for line in adccfg.splitlines()]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Piergiorgio Sartor@21:1/5 to Chris Green on Mon Dec 11 20:05:16 2023
    On 11/12/2023 16.16, Chris Green wrote:
    Is there a way to abbreviate the following code somehow?

    lv = {'dev':'bbb', 'input':'1', 'name':'Leisure volts'}
    sv = {'dev':'bbb', 'input':'0', 'name':'Starter volts'}
    la = {'dev':'bbb', 'input':'2', 'name':'Leisure Amps'}
    sa = {'dev':'bbb', 'input':'3', 'name':'Starter Amps'}
    bv = {'dev':'adc2', 'input':0, 'name':'BowProp Volts'}

    It's effectively a 'table' with columns named 'dev', 'input' and
    'name' and I want to access the values of the table using the variable
    name.

    I could, obviously, store the data in a database (sqlite), I have some similar data in a database already but the above sort of format in
    Python source is more human readable and accessible. I'm just looking
    for a less laborious way of entering it really.


    Maybe a dict of dicts:

    tx = {lv: {'dev':'bbb', 'input':'1', 'name':'Leisure volts'},
    sv: {'dev':'bbb', 'input':'0', 'name':'Starter volts'},
    ...}

    Might have one or two advantages.

    bye,

    --

    piergiorgio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)