Python list(?) sending incorrect number of values to other program

Viewed 46

I have been trying to upload close to a thousand SVG files to a particular program (FontForge) and as such searched for a way to automate it. Unfortunately I am really unfamiliar with Python, to the extent that I'm not sure if what I changed about the code ended up changing something fundamental.

The original code you were meant to individually continue the table that the original coder left with the file name and glyph names of the SVG files. This would require doing it manually, as I realized quickly it wouldn't allow loops within the brackets itself. The original code was as follows, albeit with more items:

select = [
       ('null',             'null'),
       ('bs-info-circle',   'infoCircle'),
]

Looking at it, and with a lot of googling and experimentation, I guessed that it was a list of tuples. As such, I created various loops adding onto a toSelect list that I created. Since they are rougly the same I'll just show one here:

for x in svc:
    icon="svc-"+x+"con_lang"
    glyph=x
    #print((icon, glyph))
    toSelect.extend(((icon, glyph),)) #comma necessary to force it to add in a pair, rather than individually

The variable svc is a list of strings: ['ba', 'be', 'bi', 'bo'...] pulled from a TXT file. The variable toSelect, when printed, looks as follows:

[('svc-bacon_lang', 'ba'), ('svc-becon_lang', 'be'), ('svc-bicon_lang', 'bi'), ...]

Long story short, I now have a list that seems to be formatted the same as the contents of the original code. As such, I set it equal in a simple manner:

select = toSelect

However, running the build program that pulls from this code is giving the following error message:

Traceback (most recent call last):
  File "C:\Users\*****\Downloads\ff-batch-main\ff-batch-main\build.py", line 392, in <module>
    run_fontforge(config)
  File "C:\Users\*****\Downloads\ff-batch-main\ff-batch-main\build.py", line 148, in run_fontforge
    for key, name in config.select:
ValueError: not enough values to unpack (expected 2, got 1)

I have tried every variation of declaring select that I can, including a few that cause the following error message:

Traceback (most recent call last):
  File "C:\Users\*****\Downloads\ff-batch-main\ff-batch-main\build.py", line 392, in <module>
    run_fontforge(config)
  File "C:\Users\*****\Downloads\ff-batch-main\ff-batch-main\build.py", line 148, in run_fontforge
    for key, name in config.select:
ValueError: too many values to unpack (expected 2)

Printing the value of select[0] does seem to tell me that that error is caused by all of the entries being considered one entry on the list? So at least I know that.

Still, I can't figure out why it doesn't take my original attempt, as select[0] = ('null', 'null'). I'm worried that select isn't supposed to be a list at all, but is something different that I'm simply unfamiliar with since I do not know python. Is this some sort of function that I broke by adding items sequentially instead of all at once?

I will also show the code from the 'build' program that is flagged as the problem. The only thing that I edited was the 'config' program, as instructed by the coder, but hopefully this at least will give context?

def run_fontforge(config):
    from icon_map import maps
    try:
        from select_cache import maps as icons
    except:
        icons = {}
    print(f"Generating fonts => {config.build_dir}/{config.font_name}.ttf")
    with open('select_cache.py', 'w') as f:
        f.write("# SVG to Font Icon mapping\n")
        f.write(f"# Generated: {datetime.now()}\n")
        f.write("maps = {\n")

        last = config.font_start_code - 1
        for _,m in icons.items():
            last = max(last, m['code'])
        last += 1

        for key, name in config.select:
            icon = icons.get(key)
            src = maps.get(key)

So yeah. Um, any advice or explanations would be greatly appreciated, and I'll do my best to give additional information when possible? Unfortunately I started trying to understand Python yesterday and am coming at this from a rusty knowledge of java so I am not really fluent, might not know terms and whatnot. I just wanted to import some files man...

1 Answers

You've gotten quite far just 1 day into Python.

My hypothesis for the bug is config.select not containing the toSelect data.

Ways to investigate:

  • Interactively run this to verify that toSelect is a list of pairs:

    for k, n in toSelect: pass
    
  • print both variables, or interactively evaluate config.select == toSelect to compare them, or set breakpoints in the PyCharm or VSCode debugger and examine these variables.

Is this some sort of function that I broke by adding items sequentially instead of all at once?

No.

BTW, you can make:

toSelect.extend(((icon, glyph),))

easier to understand by writing it as:

toSelect.append((icon, glyph))

Bonus: The most "Pythonic" way to write that for loop is as a list comprehension:

toSelect = [("svc-"+x+"con_lang", x) for x in svc]
Related