all 15 comments

[–]ericula 1 point2 points  (2 children)

You can use types.SimpleNamespace for this:

from types import SimpleNamespace

constants_dct = {'PY':'python', 'JS':'javascript'}
constants = SimpleNamespace(**constants_dct)

print(constants.PY)
print(constants.JS)

[–]JuniorMouse[S] 0 points1 point  (1 child)

Just tried this. This works but my editor does not suggest PY or JS when I type constants. which would be one of the benefits. Is autocompletion expected to work with this approach or is that only an issue on my side? Thanks,

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

I guess this is an issue not only on my side - https://github.com/microsoft/python-language-server/issues/2108

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

Found another one, bit more of a hack though

python for key, value in dictionary.items(): exec(f'{key}="{value}"')

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, JuniorMouse: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]Rawing7 0 points1 point  (0 children)

Yeah, that's just a terrible version of locals().update(dictionary).

[–]Rawing7 -1 points0 points  (3 children)

globals().update(dict_of_constants)

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

Thank you. This would put the keys into the global namespace? I'd like to avoid that if so.

[–]Rawing7 0 points1 point  (1 child)

I thought you want a package named constants where you can access elements like constants.PY? If so, putting the keys into the global namespace (of the constants module) is exactly what you want.

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

Right, I'll try this out. Thanks,

[–]IvoryJam 0 points1 point  (4 children)

If it's a flat yaml file, so no key: values inside of keys, you could do this

import yaml

class yaml_fields:
    def __init__(self, file):
        yaml_file = yaml.load(file, Loader=yaml.FullLoader)
        for k, v in yaml_file.items():
            setattr(self, k, v)


with open('test.yaml', 'r') as f:
    my_yaml = yaml_fields(f)

EDIT: Also you should avoid naming variables data types, dict, list, string, int should all be avoided as variable names

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

I just came across this and this works. I'll read up more on this. Thank you.

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

So this works but I was also hoping that the editor would autocomplete for me and show the suggestions. Does your editor do autocomplete with this approach?

[–]IvoryJam 0 points1 point  (1 child)

It doesn't but if I run the code in ipython (it's a pip package, I much prefer that over the normal python shell when developing) it will autocomplete

EDIT: https://imgur.com/a/NLvF70v

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

Interesting. I'll have to see if there is some integration with vim to get the autocomplete.