Python Forum

Full Version: subcommands as functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
it make sense to implement each subcommand as a function. but i want to be able to easily add more subcommands. i'm thinking that i would name the functions for subcommands in a special way and use the namespace dictionary from globals() to find the subcommand function to be called. is this something that will get lots of rotten tomatoes thrown at me for doing?
too many things use or depend on these decorators which still make no sense to me. so i would like to ask for more independent tutorials, blogs, posts, etc, that explain them without any presumption of existing knowledge. in particular, what does the @ actually do?
i am writing an example of how i was thinking about doing subcommands. each subcommand is defined as a function with a name that begins with "sc_". a list of subcommands is extracted from the global namespace returned by a call to globals(). i am wondering if someone was going to tell me that i need to construct and maintain a (separate) table of subcommand names and references to the functions instead of looking at the dictionary from globals().
So you prefer
def sc_bacon():
    ...
over
@sc
def bacon():
    ...
However, the latter is more powerful and you don't have to write the code that extracts the subcommands from the global namespace.
(Sep-02-2018, 10:21 PM)Skaperen Wrote: [ -> ]so i would like to ask for more independent tutorials, blogs, posts, etc, that explain them without any presumption of existing knowledge.
Decorators demystified

Plenty of good tutorials and two videos from EuroPython 2015 and PyCon SG conference
none of those articles clarifies post #5.
(Sep-03-2018, 04:49 PM)Skaperen Wrote: [ -> ]none of those articles clarifies post #5.
I don't see what has to be clarified in post #5. Obviously it is purely rhetorical question... Do whatever you want... My post was in response to
(Sep-02-2018, 10:21 PM)Skaperen Wrote: [ -> ]so i would like to ask for more independent tutorials, blogs, posts, etc, that explain them without any presumption of existing knowledge.
like, for example, the various names that follow the @ were not defined.
Here is an example illustrating the two approaches. Both scripts collect subcommands in a dictionary.

First your approach, which I would call 'static decoration', or even 'lexical decoration'

# foobar.py

def spam():
    pass

def sc_scramble():
    pass

def sc_eat():
    pass

subcommands = {}
for name, value in globals().items():
    if name.startswith('sc_'):
        subcommands[name] = value

if __name__ == '__main__':
    print(subcommands)
Now the pythonic way, or 'dynamic decoration'

# barbaz.py

subcommands = {}

def sc(func):
    subcommands[func.__name__] = func
    return func

def spam():
    pass

@sc
def scramble():
    pass

@sc
def eat():
    pass

if __name__ == '__main__':
    print(subcommands)
It is easy to guess that dynamic is better than static.
Pages: 1 2