Conditions | 9 |
Total Lines | 53 |
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 | """CLI for accessing the gtk/tickit UIs implemented by this package.""" |
||
42 | def detach_proc(workdir='.', umask=0): |
||
43 | """Detach a process from the controlling terminal and run it in the |
||
44 | background as a daemon. |
||
45 | """ |
||
46 | |||
47 | # Default maximum for the number of available file descriptors. |
||
48 | MAXFD = 1024 |
||
49 | |||
50 | # The standard I/O file descriptors are redirected to /dev/null by default. |
||
51 | if (hasattr(os, "devnull")): |
||
52 | REDIRECT_TO = os.devnull |
||
53 | else: |
||
54 | REDIRECT_TO = "/dev/null" |
||
55 | |||
56 | try: |
||
57 | pid = os.fork() |
||
58 | except OSError, e: |
||
59 | raise Exception, "%s [%d]" % (e.strerror, e.errno) |
||
60 | |||
61 | if (pid == 0): |
||
62 | os.setsid() |
||
63 | |||
64 | try: |
||
65 | pid = os.fork() |
||
66 | |||
67 | except OSError, e: |
||
68 | raise Exception, "%s [%d]" % (e.strerror, e.errno) |
||
69 | |||
70 | if (pid == 0): |
||
71 | os.chdir(workdir) |
||
72 | os.umask(umask) |
||
73 | else: |
||
74 | os._exit(0) |
||
75 | else: |
||
76 | os._exit(0) |
||
77 | |||
78 | maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] |
||
79 | if (maxfd == resource.RLIM_INFINITY): |
||
80 | maxfd = MAXFD |
||
81 | |||
82 | # Iterate through and close all file descriptors. |
||
83 | for fd in range(0, maxfd): |
||
84 | try: |
||
85 | os.close(fd) |
||
86 | except OSError: |
||
87 | pass |
||
88 | |||
89 | os.open(REDIRECT_TO, os.O_RDWR) |
||
90 | |||
91 | os.dup2(0, 1) |
||
92 | os.dup2(0, 2) |
||
93 | |||
94 | return(0) |
||
95 | |||
166 |