Conditions | 18 |
Total Lines | 73 |
Code Lines | 54 |
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 msgfmt.make() 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 | #! /usr/bin/env python2 |
||
104 | # Compute .mo name from .po name and arguments |
||
105 | if filename.endswith('.po'): |
||
106 | infile = filename |
||
107 | else: |
||
108 | infile = filename + '.po' |
||
109 | if outfile is None: |
||
110 | outfile = os.path.splitext(infile)[0] + '.mo' |
||
111 | |||
112 | try: |
||
113 | lines = open(infile).readlines() |
||
114 | except IOError, msg: |
||
115 | print >> sys.stderr, msg |
||
116 | sys.exit(1) |
||
117 | |||
118 | section = None |
||
119 | fuzzy = 0 |
||
120 | |||
121 | # Parse the catalog |
||
122 | lno = 0 |
||
123 | for l in lines: |
||
124 | lno += 1 |
||
125 | # If we get a comment line after a msgstr, this is a new entry |
||
126 | if l[0] == '#' and section == STR: |
||
127 | add(msgid, msgstr, fuzzy) |
||
128 | section = None |
||
129 | fuzzy = 0 |
||
130 | # Record a fuzzy mark |
||
131 | if l[:2] == '#,' and l.find('fuzzy'): |
||
132 | fuzzy = 1 |
||
133 | # Skip comments |
||
134 | if l[0] == '#': |
||
135 | continue |
||
136 | # Now we are in a msgid section, output previous section |
||
137 | if l.startswith('msgid'): |
||
138 | if section == STR: |
||
139 | add(msgid, msgstr, fuzzy) |
||
140 | section = ID |
||
141 | l = l[5:] |
||
142 | msgid = msgstr = '' |
||
143 | # Now we are in a msgstr section |
||
144 | elif l.startswith('msgstr'): |
||
145 | section = STR |
||
146 | l = l[6:] |
||
147 | # Skip empty lines |
||
148 | l = l.strip() |
||
149 | if not l: |
||
150 | continue |
||
151 | # XXX: Does this always follow Python escape semantics? |
||
152 | l = eval(l) |
||
153 | if section == ID: |
||
154 | msgid += l |
||
155 | elif section == STR: |
||
156 | msgstr += l |
||
157 | else: |
||
158 | print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \ |
||
159 | 'before:' |
||
160 | print >> sys.stderr, l |
||
161 | sys.exit(1) |
||
162 | # Add last entry |
||
163 | if section == STR: |
||
164 | add(msgid, msgstr, fuzzy) |
||
165 | |||
166 | # Compute output |
||
167 | output = generate() |
||
168 | |||
169 | try: |
||
170 | open(outfile,"wb").write(output) |
||
171 | except IOError,msg: |
||
172 | print >> sys.stderr, msg |
||
173 | |||
174 | |||
175 | |||
176 | def main(): |
||
177 | try: |
||
205 |