Conditions | 12 |
Total Lines | 56 |
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 ProjectManager.build_module() 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 python |
||
31 | @staticmethod |
||
32 | def build_module(project, env=None): |
||
33 | '''Build project script as module''' |
||
34 | from pyspider.libs import base_handler |
||
35 | assert 'name' in project, 'need name of project' |
||
36 | assert 'script' in project, 'need script of project' |
||
37 | |||
38 | if env is None: |
||
39 | env = {} |
||
40 | # fix for old non-package version scripts |
||
41 | pyspider_path = os.path.join(os.path.dirname(__file__), "..") |
||
42 | if pyspider_path not in sys.path: |
||
43 | sys.path.insert(1, pyspider_path) |
||
44 | |||
45 | env = dict(env) |
||
46 | env.update({ |
||
47 | 'debug': project.get('status', 'DEBUG') == 'DEBUG', |
||
48 | }) |
||
49 | |||
50 | loader = ProjectLoader(project) |
||
51 | module = loader.load_module(project['name']) |
||
52 | |||
53 | # logger inject |
||
54 | module.log_buffer = [] |
||
55 | module.logging = module.logger = logging.Logger(project['name']) |
||
56 | if env.get('enable_stdout_capture', True): |
||
57 | handler = SaveLogHandler(module.log_buffer) |
||
58 | handler.setFormatter(LogFormatter(color=False)) |
||
59 | else: |
||
60 | handler = logging.StreamHandler() |
||
61 | handler.setFormatter(LogFormatter(color=True)) |
||
62 | module.logger.addHandler(handler) |
||
63 | |||
64 | if '__handler_cls__' not in module.__dict__: |
||
65 | BaseHandler = module.__dict__.get('BaseHandler', base_handler.BaseHandler) |
||
66 | for each in list(six.itervalues(module.__dict__)): |
||
67 | if inspect.isclass(each) and each is not BaseHandler \ |
||
68 | and issubclass(each, BaseHandler): |
||
69 | module.__dict__['__handler_cls__'] = each |
||
70 | _class = module.__dict__.get('__handler_cls__') |
||
71 | assert _class is not None, "need BaseHandler in project module" |
||
72 | |||
73 | instance = _class() |
||
74 | instance.__env__ = env |
||
75 | instance.project_name = project['name'] |
||
76 | instance.project = project |
||
77 | |||
78 | return { |
||
79 | 'loader': loader, |
||
80 | 'module': module, |
||
81 | 'class': _class, |
||
82 | 'instance': instance, |
||
83 | 'exception': None, |
||
84 | 'exception_log': '', |
||
85 | 'info': project, |
||
86 | 'load_time': time.time(), |
||
87 | } |
||
224 |