![]() |
subcommands as functions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: subcommands as functions (/thread-12603.html) Pages:
1
2
|
subcommands as functions - Skaperen - Sep-02-2018 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? RE: subcommands as functions - buran - Sep-02-2018 Maybe check click and what it has to offer http://click.pocoo.org/5/quickstart/#nesting-commands http://click.pocoo.org/5/commands/ RE: subcommands as functions - Skaperen - Sep-02-2018 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? RE: subcommands as functions - Skaperen - Sep-03-2018 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(). RE: subcommands as functions - Gribouillis - Sep-03-2018 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. RE: subcommands as functions - buran - Sep-03-2018 (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 RE: subcommands as functions - Skaperen - Sep-03-2018 none of those articles clarifies post #5. RE: subcommands as functions - buran - Sep-03-2018 (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. RE: subcommands as functions - Skaperen - Sep-04-2018 like, for example, the various names that follow the @ were not defined. RE: subcommands as functions - Gribouillis - Sep-04-2018 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. |