| 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 | 1 | mac = sys.platform == 'darwin' |
|
| 88 | |||
| 89 | # Shared keyword arguments |
||
| 90 | 1 | kw_f = {'padding': 5} # constructor arguments for frames |
|
| 91 | 1 | kw_gp = {'padx': 5, 'pady': 5} # grid arguments for padded widgets |
|
| 92 | 1 | kw_gs = {'sticky': tk.NSEW} # grid arguments for sticky widgets |
|
| 93 | 1 | kw_gsp = dict(chain(kw_gs.items(), kw_gp.items())) # grid arguments for sticky padded widgets |
|
| 94 | |||
| 95 | # Configure grid |
||
| 96 | 1 | frame = ttk.Frame(root, **kw_f) |
|
|
|
|||
| 97 | 1 | frame.rowconfigure(0, weight=0) |
|
| 98 | 1 | frame.rowconfigure(2, weight=1) |
|
| 99 | 1 | frame.rowconfigure(4, weight=1) |
|
| 100 | 1 | frame.columnconfigure(0, weight=1) |
|
| 101 | |||
| 102 | # Create widgets |
||
| 103 | 1 | def frame_settings(master): |
|
| 104 | """Frame for the settings.""" |
||
| 105 | 1 | frame = ttk.Frame(master, **kw_f) |
|
| 106 | |||
| 107 | # Configure grid |
||
| 108 | 1 | frame.rowconfigure(0, weight=1) |
|
| 109 | 1 | frame.rowconfigure(1, weight=1) |
|
| 110 | 1 | frame.columnconfigure(0, weight=0) |
|
| 111 | 1 | frame.columnconfigure(1, weight=1) |
|
| 112 | 1 | frame.columnconfigure(2, weight=0) |
|
| 113 | |||
| 114 | # Place widgets |
||
| 115 | 1 | ttk.Label(frame, text="Shared:").grid(row=0, column=0, sticky=tk.W, **kw_gp) |
|
| 116 | 1 | ttk.Entry(frame, state='readonly', textvariable=self.path_root).grid(row=0, column=1, columnspan=2, **kw_gsp) |
|
| 117 | 1 | ttk.Label(frame, text="Downloads:").grid(row=1, column=0, sticky=tk.W, **kw_gp) |
|
| 118 | 1 | ttk.Entry(frame, state='readonly', textvariable=self.path_downloads).grid(row=1, column=1, **kw_gsp) |
|
| 119 | 1 | ttk.Button(frame, text="...", width=0, command=self.browse_downloads).grid(row=1, column=2, ipadx=5, **kw_gp) |
|
| 120 | |||
| 121 | 1 | return frame |
|
| 122 | |||
| 123 | 1 | def frame_incoming(master): |
|
| 124 | """Frame for incoming songs.""" |
||
| 125 | 1 | frame = ttk.Frame(master, **kw_f) |
|
| 126 | |||
| 127 | # Configure grid |
||
| 128 | 1 | frame.rowconfigure(0, weight=1) |
|
| 129 | 1 | frame.rowconfigure(1, weight=0) |
|
| 130 | 1 | frame.columnconfigure(0, weight=0) |
|
| 131 | 1 | frame.columnconfigure(1, weight=1) |
|
| 132 | 1 | frame.columnconfigure(2, weight=1) |
|
| 133 | |||
| 134 | # Place widgets |
||
| 135 | 1 | self.listbox_incoming = tk.Listbox(frame, selectmode=tk.EXTENDED if mac else tk.MULTIPLE) |
|
| 136 | 1 | self.listbox_incoming.grid(row=0, column=0, columnspan=3, **kw_gsp) |
|
| 137 | 1 | scroll_incoming = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=self.listbox_incoming.yview) |
|
| 138 | 1 | self.listbox_incoming.configure(yscrollcommand=scroll_incoming.set) |
|
| 139 | 1 | scroll_incoming.grid(row=0, column=2, sticky=(tk.N, tk.E, tk.S)) |
|
| 140 | 1 | ttk.Button(frame, text="\u21BB", width=0, command=self.update).grid(row=1, column=0, sticky=tk.SW, ipadx=5, **kw_gp) |
|
| 141 | 1 | ttk.Button(frame, text="Ignore Selected", command=self.do_ignore).grid(row=1, column=1, sticky=tk.SW, ipadx=5, **kw_gp) |
|
| 142 | 1 | ttk.Button(frame, text="Download Selected", command=self.do_download).grid(row=1, column=2, sticky=tk.SE, ipadx=5, **kw_gp) |
|
| 143 | 1 | return frame |
|
| 144 | |||
| 145 | 1 | def frame_outgoing(master): |
|
| 146 | """Frame for outgoing songs.""" |
||
| 147 | 1 | frame = ttk.Frame(master, **kw_f) |
|
| 148 | |||
| 149 | # Configure grid |
||
| 150 | 1 | frame.rowconfigure(0, weight=1) |
|
| 151 | 1 | frame.rowconfigure(1, weight=0) |
|
| 152 | 1 | frame.columnconfigure(0, weight=0) |
|
| 153 | 1 | frame.columnconfigure(1, weight=1) |
|
| 154 | 1 | frame.columnconfigure(2, weight=1) |
|
| 155 | |||
| 156 | # Place widgets |
||
| 157 | 1 | self.listbox_outgoing = tk.Listbox(frame, selectmode=tk.EXTENDED if mac else tk.MULTIPLE) |
|
| 158 | 1 | self.listbox_outgoing.grid(row=0, column=0, columnspan=3, **kw_gsp) |
|
| 159 | 1 | scroll_outgoing = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=self.listbox_outgoing.yview) |
|
| 160 | 1 | self.listbox_outgoing.configure(yscrollcommand=scroll_outgoing.set) |
|
| 161 | 1 | scroll_outgoing.grid(row=0, column=2, sticky=(tk.N, tk.E, tk.S)) |
|
| 162 | 1 | ttk.Button(frame, text="\u21BB", width=0, command=self.update).grid(row=1, column=0, sticky=tk.SW, ipadx=5, **kw_gp) |
|
| 163 | 1 | ttk.Button(frame, text="Remove Selected", command=self.do_remove).grid(row=1, column=1, sticky=tk.SW, ipadx=5, **kw_gp) |
|
| 164 | 1 | ttk.Button(frame, text="Share Songs...", command=self.do_share).grid(row=1, column=2, sticky=tk.SE, ipadx=5, **kw_gp) |
|
| 165 | |||
| 166 | 1 | return frame |
|
| 167 | |||
| 168 | 1 | def separator(master): |
|
| 169 | """Widget to separate frames.""" |
||
| 170 | 1 | return ttk.Separator(master) |
|
| 171 | |||
| 172 | # Place widgets |
||
| 173 | 1 | frame_settings(frame).grid(row=0, **kw_gs) |
|
| 174 | 1 | separator(frame).grid(row=1, padx=10, pady=5, **kw_gs) |
|
| 175 | 1 | frame_outgoing(frame).grid(row=2, **kw_gs) |
|
| 176 | 1 | separator(frame).grid(row=3, padx=10, pady=5, **kw_gs) |
|
| 177 | 1 | frame_incoming(frame).grid(row=4, **kw_gs) |
|
| 178 | |||
| 179 | 1 | 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.