r/pythoncoding • • Feb 21 '24

Command pattern

Hello,
I wanna implement a configurationpage with Flask so I can configure specific LED effects. What i wanna do is writing specific effects-methods in different files and the methods will be loaded to the mainpage. So I dont need to manually add new methods and an new if..else. I saw this on DiscordJS which implements the command handler pattern. I read somethinge about command pattern in python but it seems to that is more difficult to implement than in javascript. How would you implement this or does someone know a good tutorial about this task?

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/frnzprf Mar 26 '24 edited Mar 26 '24

Yeah, it's probably too late and not very practical. I just stumbled upon your post today and this is what I could contribute.

Other general tip: If you have lot's of "if ... else ...", maybe you can think about using a dictionary instead. There should never be a reason to repeat yourself. (Sometimes it's more work to get rid of redundance than to just write something twice.)

Multiple files or a single file shouldn't matter much. Just put your LED effects inside of functions and import the functions where you use them.