Conditions | 22 |
Total Lines | 120 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 exabgp.application.control.Control.loop() 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 | """ |
||
121 | def loop (self): |
||
122 | try: |
||
123 | self.r_pipe = os.open(self.recv, os.O_RDWR | os.O_NONBLOCK | os.O_EXCL) |
||
124 | except OSError: |
||
125 | self.terminate() |
||
126 | |||
127 | standard_in = sys.stdin.fileno() |
||
128 | standard_out = sys.stdout.fileno() |
||
129 | |||
130 | def monitor (function): |
||
131 | def wrapper (*args): |
||
132 | # print >> sys.stderr, "%s(%s)" % (function.func_name,','.join([str(_).replace('\n','\\n') for _ in args])) |
||
133 | r = function(*args) |
||
134 | # print >> sys.stderr, "%s -> %s" % (function.func_name,str(r)) |
||
135 | return r |
||
136 | return wrapper |
||
137 | |||
138 | @monitor |
||
139 | def std_reader (number): |
||
140 | try: |
||
141 | return os.read(standard_in,number) |
||
142 | except OSError as exc: |
||
143 | if exc.errno in error.block: |
||
144 | return '' |
||
145 | sys.exit(1) |
||
146 | |||
147 | @monitor |
||
148 | def std_writer (line): |
||
149 | try: |
||
150 | return os.write(standard_out,line) |
||
151 | except OSError as exc: |
||
152 | if exc.errno in error.block: |
||
153 | return 0 |
||
154 | sys.exit(1) |
||
155 | |||
156 | @monitor |
||
157 | def fifo_reader (number): |
||
158 | try: |
||
159 | return os.read(self.r_pipe,number) |
||
160 | except OSError as exc: |
||
161 | if exc.errno in error.block: |
||
162 | return '' |
||
163 | sys.exit(1) |
||
164 | |||
165 | @monitor |
||
166 | def fifo_writer (line): |
||
167 | pipe,nb = None,0 |
||
168 | try: |
||
169 | pipe = os.open(self.send, os.O_WRONLY | os.O_NONBLOCK | os.O_EXCL) |
||
170 | self.no_buffer(pipe) |
||
171 | except OSError: |
||
172 | time.sleep(0.05) |
||
173 | return 0 |
||
174 | if pipe is not None: |
||
175 | try: |
||
176 | nb = os.write(pipe,line) |
||
177 | except OSError: |
||
178 | pass |
||
179 | try: |
||
180 | os.close(pipe) |
||
181 | except OSError: |
||
182 | pass |
||
183 | return nb |
||
184 | |||
185 | read = { |
||
186 | standard_in: std_reader, |
||
187 | self.r_pipe: fifo_reader, |
||
188 | } |
||
189 | |||
190 | write = { |
||
191 | standard_in: fifo_writer, |
||
192 | self.r_pipe: std_writer, |
||
193 | } |
||
194 | |||
195 | backlog = { |
||
196 | standard_in: deque(), |
||
197 | self.r_pipe: deque(), |
||
198 | } |
||
199 | |||
200 | store = { |
||
201 | standard_in: b'', |
||
202 | self.r_pipe: b'', |
||
203 | } |
||
204 | |||
205 | def consume (source): |
||
206 | if not backlog[source] and b'\n' not in store[source]: |
||
|
|||
207 | store[source] += read[source](1024) |
||
208 | else: |
||
209 | backlog[source].append(read[source](1024)) |
||
210 | # assuming a route takes 80 chars, 100 Mb is over 1Millions routes |
||
211 | # something is really wrong if it was not consummed |
||
212 | if len(backlog) > 100*mb: |
||
213 | sys.stderr.write('using too much memory - exiting') |
||
214 | sys.exit(1) |
||
215 | |||
216 | reading = [standard_in, self.r_pipe] |
||
217 | |||
218 | while True: |
||
219 | ready = self.read_on(reading) |
||
220 | |||
221 | # command from user |
||
222 | if self.r_pipe in ready: |
||
223 | consume(self.r_pipe) |
||
224 | if standard_in in ready: |
||
225 | consume(standard_in) |
||
226 | |||
227 | for source in reading: |
||
228 | while b'\n' in store[source]: |
||
229 | line,_ = store[source].split(b'\n',1) |
||
230 | # sys.stderr.write(str(line).replace('\n','\\n') + '\n') |
||
231 | # sys.stderr.flush() |
||
232 | sent = write[source](line + b'\n') |
||
233 | # sys.stderr.write('sent %d\n' % sent) |
||
234 | # sys.stderr.flush() |
||
235 | if sent: |
||
236 | store[source] = store[source][sent:] |
||
237 | continue |
||
238 | break |
||
239 | if backlog[source]: |
||
240 | store[source] += backlog[source].popleft() |
||
241 | |||
273 |