Conditions | 9 |
Total Lines | 51 |
Lines | 0 |
Ratio | 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:
1 | from __future__ import print_function |
||
124 | @pytest.mark.skipif(not hasattr(os, 'fork') or PYPY, reason="os.fork not available or PYPY") |
||
125 | def test_realsocket_makefile(): |
||
126 | buf = StringIO() |
||
127 | p = socket.socket() |
||
128 | p.bind(('127.0.0.1', 0)) |
||
129 | p.listen(1) |
||
130 | p.settimeout(1) |
||
131 | pid = os.fork() |
||
132 | |||
133 | if pid: |
||
134 | with aspectlib.weave( |
||
135 | ['socket._fileobject' if aspectlib.PY2 else 'socket.SocketIO'] + |
||
136 | (['socket.socket', 'socket._realsocket'] if aspectlib.PY2 else ['socket.socket']), |
||
137 | aspectlib.debug.log(print_to=buf, stacktrace=False), |
||
138 | lazy=True, |
||
139 | methods=aspectlib.ALL_METHODS, |
||
140 | ): |
||
141 | s = socket.socket() |
||
142 | s.settimeout(1) |
||
143 | s.connect(p.getsockname()) |
||
144 | if aspectlib.PY3: |
||
145 | fh = s.makefile('rwb', buffering=0) |
||
146 | else: |
||
147 | fh = s.makefile(bufsize=0) |
||
148 | fh.write(b"STUFF\n") |
||
149 | fh.readline() |
||
150 | |||
151 | with dump_on_error(buf.getvalue): |
||
152 | wait_for_strings( |
||
153 | buf.getvalue, 0, |
||
154 | "}.connect", |
||
155 | "}.makefile", |
||
156 | "}.write(", |
||
157 | "}.send", |
||
158 | "}.write =>", |
||
159 | "}.readline()", |
||
160 | "}.recv", |
||
161 | "}.readline => ", |
||
162 | ) |
||
163 | else: |
||
164 | try: |
||
165 | c, _ = p.accept() |
||
166 | c.settimeout(1) |
||
167 | if aspectlib.PY3: |
||
168 | f = c.makefile('rw', buffering=1) |
||
169 | else: |
||
170 | f = c.makefile(bufsize=1) |
||
171 | while f.readline(): |
||
172 | f.write('-\n') |
||
173 | finally: |
||
174 | os._exit(0) |
||
175 | |||
188 |