Conditions | 11 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like meme() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/python |
||
52 | @run_async |
||
53 | def meme(bot, update): |
||
54 | current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
||
55 | filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
||
56 | |||
57 | if len(update.message.photo) > 0: |
||
58 | args = update.message.caption.split(" ") |
||
59 | elif update.message.reply_to_message is not None: |
||
60 | if len(update.message.reply_to_message.photo) > 0: |
||
61 | args = update.message.text.split(" ") |
||
62 | else: |
||
63 | update.message.reply_text("You need an image for this!") |
||
64 | return |
||
65 | else: |
||
66 | update.message.reply_text("You need an image for this!") |
||
67 | return |
||
68 | |||
69 | args = args[1:] |
||
70 | |||
71 | if len(args) < 1: |
||
72 | update.message.reply_text("Type in some text!") |
||
73 | return |
||
74 | |||
75 | font = fonts_dict["impact"] |
||
76 | for i in fonts_dict: |
||
77 | if "-"+i in args[0] or "-"+i[0] in args[0]: |
||
78 | font = fonts_dict[i] |
||
79 | args = args[1:] |
||
80 | break |
||
81 | |||
82 | if len(args) < 1: |
||
83 | update.message.reply_text("Type in some text!") |
||
84 | return |
||
85 | |||
86 | initial_text = " ".join(args) |
||
87 | split_text = initial_text.split("@", maxsplit=1) |
||
88 | |||
89 | update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
||
90 | try: |
||
91 | extension = get_image(bot, update, path, filename) |
||
92 | except: |
||
93 | update.message.reply_text("Can't get the image! :(") |
||
94 | return |
||
95 | if extension not in extensions: |
||
96 | update.message.reply_text("Unsupported file, onii-chan!") |
||
97 | return |
||
98 | |||
99 | top_text, bottom_text = text_format(update, split_text) |
||
|
|||
100 | make_meme(top_text, bottom_text, filename, extension, path, font) |
||
101 | update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
||
102 | send_image(update, path, filename+"-meme", extension) |
||
103 | print (current_time, ">", "/meme", ">", update.message.from_user.username) |
||
104 | log_command(bot, update, current_time, "meme") |
||
105 |