Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subcommands as functions
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Maybe check click and what it has to offer
http://click.pocoo.org/5/quickstart/#nesting-commands
http://click.pocoo.org/5/commands/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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.
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
none of those articles clarifies post #5.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
like, for example, the various names that follow the @ were not defined.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020