Conditions | 7 |
Total Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Tests | 64 |
CRAP Score | 7 |
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:
1 | #!/usr/bin/env python |
||
82 | 1 | def init(self, root): |
|
83 | """Initialize frames and widgets.""" |
||
84 | |||
85 | # pylint: disable=line-too-long |
||
86 | |||
87 | mac = sys.platform == 'darwin' |
||
88 | |||
89 | # Shared keyword arguments |
||
90 | kw_f = {'padding': 5} # constructor arguments for frames |
||
91 | kw_gp = {'padx': 5, 'pady': 5} # grid arguments for padded widgets |
||
92 | kw_gs = {'sticky': tk.NSEW} # grid arguments for sticky widgets |
||
93 | kw_gsp = dict(chain(kw_gs.items(), kw_gp.items())) # grid arguments for sticky padded widgets |
||
94 | |||
95 | # Configure grid |
||
96 | frame = ttk.Frame(root, **kw_f) |
||
|
|||
97 | frame.rowconfigure(0, weight=0) |
||
98 | frame.rowconfigure(2, weight=1) |
||
99 | frame.rowconfigure(4, weight=1) |
||
100 | frame.columnconfigure(0, weight=1) |
||
101 | |||
102 | # Create widgets |
||
103 | def frame_settings(master): |
||
104 | """Frame for the settings.""" |
||
105 | frame = ttk.Frame(master, **kw_f) |
||
106 | |||
107 | # Configure grid |
||
108 | frame.rowconfigure(0, weight=1) |
||
109 | frame.rowconfigure(1, weight=1) |
||
110 | frame.columnconfigure(0, weight=0) |
||
111 | frame.columnconfigure(1, weight=1) |
||
112 | frame.columnconfigure(2, weight=0) |
||
113 | |||
114 | # Place widgets |
||
115 | ttk.Label(frame, text="Shared:").grid(row=0, column=0, sticky=tk.W, **kw_gp) |
||
116 | ttk.Entry(frame, state='readonly', textvariable=self.path_root).grid(row=0, column=1, columnspan=2, **kw_gsp) |
||
117 | ttk.Label(frame, text="Downloads:").grid(row=1, column=0, sticky=tk.W, **kw_gp) |
||
118 | ttk.Entry(frame, state='readonly', textvariable=self.path_downloads).grid(row=1, column=1, **kw_gsp) |
||
119 | ttk.Button(frame, text="...", width=0, command=self.browse_downloads).grid(row=1, column=2, ipadx=5, **kw_gp) |
||
120 | |||
121 | return frame |
||
122 | |||
123 | View Code Duplication | def frame_incoming(master): |
|
124 | """Frame for incoming songs.""" |
||
125 | frame = ttk.Frame(master, **kw_f) |
||
126 | |||
127 | # Configure grid |
||
128 | frame.rowconfigure(0, weight=1) |
||
129 | frame.rowconfigure(1, weight=0) |
||
130 | frame.columnconfigure(0, weight=0) |
||
131 | frame.columnconfigure(1, weight=1) |
||
132 | frame.columnconfigure(2, weight=1) |
||
133 | |||
134 | # Place widgets |
||
135 | self.listbox_incoming = tk.Listbox(frame, selectmode=tk.EXTENDED if mac else tk.MULTIPLE) |
||
136 | self.listbox_incoming.grid(row=0, column=0, columnspan=3, **kw_gsp) |
||
137 | scroll_incoming = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=self.listbox_incoming.yview) |
||
138 | self.listbox_incoming.configure(yscrollcommand=scroll_incoming.set) |
||
139 | scroll_incoming.grid(row=0, column=2, sticky=(tk.N, tk.E, tk.S)) |
||
140 | ttk.Button(frame, text="\u21BB", width=0, command=self.update).grid(row=1, column=0, sticky=tk.SW, ipadx=5, **kw_gp) |
||
141 | ttk.Button(frame, text="Ignore Selected", command=self.do_ignore).grid(row=1, column=1, sticky=tk.SW, ipadx=5, **kw_gp) |
||
142 | ttk.Button(frame, text="Download Selected", command=self.do_download).grid(row=1, column=2, sticky=tk.SE, ipadx=5, **kw_gp) |
||
143 | return frame |
||
144 | |||
145 | View Code Duplication | def frame_outgoing(master): |
|
146 | """Frame for outgoing songs.""" |
||
147 | frame = ttk.Frame(master, **kw_f) |
||
148 | |||
149 | # Configure grid |
||
150 | frame.rowconfigure(0, weight=1) |
||
151 | frame.rowconfigure(1, weight=0) |
||
152 | frame.columnconfigure(0, weight=0) |
||
153 | frame.columnconfigure(1, weight=1) |
||
154 | frame.columnconfigure(2, weight=1) |
||
155 | |||
156 | # Place widgets |
||
157 | self.listbox_outgoing = tk.Listbox(frame, selectmode=tk.EXTENDED if mac else tk.MULTIPLE) |
||
158 | self.listbox_outgoing.grid(row=0, column=0, columnspan=3, **kw_gsp) |
||
159 | scroll_outgoing = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=self.listbox_outgoing.yview) |
||
160 | self.listbox_outgoing.configure(yscrollcommand=scroll_outgoing.set) |
||
161 | scroll_outgoing.grid(row=0, column=2, sticky=(tk.N, tk.E, tk.S)) |
||
162 | ttk.Button(frame, text="\u21BB", width=0, command=self.update).grid(row=1, column=0, sticky=tk.SW, ipadx=5, **kw_gp) |
||
163 | ttk.Button(frame, text="Remove Selected", command=self.do_remove).grid(row=1, column=1, sticky=tk.SW, ipadx=5, **kw_gp) |
||
164 | ttk.Button(frame, text="Share Songs...", command=self.do_share).grid(row=1, column=2, sticky=tk.SE, ipadx=5, **kw_gp) |
||
165 | |||
166 | return frame |
||
167 | |||
168 | def separator(master): |
||
169 | """Widget to separate frames.""" |
||
170 | return ttk.Separator(master) |
||
171 | |||
172 | # Place widgets |
||
173 | frame_settings(frame).grid(row=0, **kw_gs) |
||
174 | separator(frame).grid(row=1, padx=10, pady=5, **kw_gs) |
||
175 | frame_outgoing(frame).grid(row=2, **kw_gs) |
||
176 | separator(frame).grid(row=3, padx=10, pady=5, **kw_gs) |
||
177 | frame_incoming(frame).grid(row=4, **kw_gs) |
||
178 | |||
179 | return frame |
||
180 | |||
332 |
Generally, there is nothing wrong with usage of
*
or**
arguments. For readability of the code base, we suggest to not over-use these language constructs though.For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.