| Conditions | 17 |
| Total Lines | 69 |
| 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 nopen() 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 | """Utilities used in Gene Ontology Enrichment Analyses.""" |
||
| 23 | def nopen(f, mode="r"): |
||
| 24 | r""" |
||
| 25 | open a file that's gzipped or return stdin for '-' |
||
| 26 | if f is a number, the result of nopen(sys.argv[f]) is returned. |
||
| 27 | >>> nopen('-') == sys.stdin, nopen('-', 'w') == sys.stdout |
||
| 28 | (True, True) |
||
| 29 | >>> nopen(sys.argv[0]) |
||
| 30 | <...file...> |
||
| 31 | # expands user and vars ($HOME) |
||
| 32 | >>> nopen("~/.bashrc").name == nopen("$HOME/.bashrc").name |
||
| 33 | True |
||
| 34 | # an already open file. |
||
| 35 | >>> nopen(open(sys.argv[0])) |
||
| 36 | <...file...> |
||
| 37 | >>> nopen(0) |
||
| 38 | <...file...> |
||
| 39 | Or provide nicer access to Popen.stdout |
||
| 40 | >>> files = list(nopen("|ls")) |
||
| 41 | >>> assert 'setup.py\n' in files or b'setup.py\n' in files, files |
||
| 42 | """ |
||
| 43 | if isinstance(f, int_types): |
||
| 44 | return nopen(sys.argv[f], mode) |
||
| 45 | |||
| 46 | if not isinstance(f, basestring): |
||
| 47 | return f |
||
| 48 | if f.startswith("|"): |
||
| 49 | # using shell explicitly makes things like process substitution work: |
||
| 50 | # http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection |
||
| 51 | # use sys.stderr so we dont have to worry about checking it... |
||
| 52 | p = Popen(f[1:], stdout=PIPE, stdin=PIPE, |
||
| 53 | stderr=sys.stderr if mode == "r" else PIPE, |
||
| 54 | shell=True, bufsize=-1, # use system default for buffering |
||
| 55 | preexec_fn=prefunc, |
||
| 56 | close_fds=False, executable=os.environ.get('SHELL')) |
||
| 57 | if sys.version_info[0] > 2: |
||
| 58 | import io |
||
| 59 | p.stdout = io.TextIOWrapper(p.stdout) |
||
| 60 | p.stdin = io.TextIOWrapper(p.stdin) |
||
| 61 | if mode != "r": |
||
| 62 | p.stderr = io.TextIOWrapper(p.stderr) |
||
| 63 | |||
| 64 | if mode and mode[0] == "r": |
||
| 65 | return process_iter(p, f[1:]) |
||
| 66 | return p |
||
| 67 | |||
| 68 | if f.startswith(("http://", "https://", "ftp://")): |
||
| 69 | fh = urlopen(f) |
||
| 70 | if f.endswith(".gz"): |
||
| 71 | return ungzipper(fh) |
||
| 72 | if sys.version_info[0] < 3: |
||
| 73 | return fh |
||
| 74 | import io |
||
| 75 | return io.TextIOWrapper(fh) |
||
| 76 | f = op.expanduser(op.expandvars(f)) |
||
| 77 | if f.endswith((".gz", ".Z", ".z")): |
||
| 78 | fh = gzip.open(f, mode) |
||
| 79 | if sys.version_info[0] < 3: |
||
| 80 | return fh |
||
| 81 | import io |
||
| 82 | return io.TextIOWrapper(fh) |
||
| 83 | elif f.endswith((".bz", ".bz2", ".bzip2")): |
||
| 84 | fh = bz2.BZ2File(f, mode) |
||
| 85 | if sys.version_info[0] < 3: |
||
| 86 | return fh |
||
| 87 | import io |
||
| 88 | return io.TextIOWrapper(fh) |
||
| 89 | |||
| 90 | return {"r": sys.stdin, "w": sys.stdout}[mode[0]] if f == "-" \ |
||
| 91 | else open(f, mode) |
||
| 92 | |||
| 217 |