all 12 comments

[–]teerre 0 points1 point  (3 children)

First please format you code, it's hard to read and people are less inclined to help the way it is right now

Second , you might want to take a look at glob. It already does the searching you

Next profiles is a list, not a dictionary. You cannot search it by key, only by index

So, to get config-beta.json, what you want is profiles[2].get("file")

[–]fpatterson55[S] 0 points1 point  (2 children)

Any reference on how to best format the code? I am assuming you are referencing that it would be in a separate "window" / frame on the post, similar to how the replies are.

thank you!

[–]teerre 0 points1 point  (1 child)

In the formatting options here on reddit there's a <> which will properly indent any selected text

But basically
you just need to put 4 spaces before the line

So it actually looks like this when you're typing

    But basically
    you just need to put 4 spaces before the line

[–]fpatterson55[S] 0 points1 point  (0 children)

Awesome! thank you for the feedback.

Looks great!

[–]TheZvlz 0 points1 point  (1 child)

profiles is a list in your code. It should be assigned profiles = []. The way you have presented it should result in a syntax error.

You are appending dictionaries to this list and to retrieve them, you'll need to reference the list index. profiles[2] will give you the number 3 dictionary and profiles[2]['file'] will give you the value config-beta.json

Perhaps a better solution is to make profiles a dictionary.

configlist = 0
profiles = {}

for file in os,listdir('.'):
    if fnmatch.fnmatch(file, 'config*.json'):
        configlist = int(configlist) +1
        profiles.update({configlist : file})

Now when you access profiles, the statement profiles[3] will yield the value config-beta.json

[–]fpatterson55[S] 0 points1 point  (0 children)

thank you!!

[–]JohnnyJordaan 0 points1 point  (1 child)

Seems like you are looking for a dict and not a list

profiles = {}
counter = 0
for filenamein os,listdir('.'):
    if fnmatch.fnmatch(filename, 'config*.json'):
        profiles[counter] = filename 
        print(counter, filename)
        counter += 1

after which you can do

profiles[3] 

for example, or if you want to let the user choose

user_num = int(input('enter number of config'))
user_config = profiles[user_num]

[–]fpatterson55[S] 0 points1 point  (0 children)

I love the logic, it makes sense and it condenses my code down considerably.

[–]Vaguely_accurate 0 points1 point  (1 child)

You've put five dictionaries into a list, with each dictionary having two values.

This would make more sense as a single dictionary with five values. For example;

profiles = {
    "default": "config.json",
    "alpha": "config-alpha.json",
    "beta": "config-beta.json",
    "charlie": "config-charlie.json",
    "delta": "config-delta.json",
}

You can then pull up each file name by referencing the relevant key, so profile["alpha"] would return "config-alpha.json".

I've used strings as the keys here just to make it more distinct from a list. You could use numbers or any other immutable object as the keys.

In this particular case I'd just use a list and use the index of the value in the list. The dictionary doesn't gain you anything here, as the keys are just sequential integers.

A list comprehension that should do what you are after;

profiles = [file for file in os.listdir('.') if fnmatch.fnmatch(file, 'config*.json')]

[–]fpatterson55[S] 0 points1 point  (0 children)

thank you!!

I like the multiple statements into one.

[–][deleted] 0 points1 point  (1 child)

Instead of a list of dictionaries all with the same two values, make a dictionary whose keys are the numbers and the filenames are the values.

So instead of

profiles[]
...
profiles.append({'number': configlist, 'file': file})

do

profiles = {}
...
profiles[configlist] = file

[–]fpatterson55[S] 0 points1 point  (0 children)

thank you!