Conditions | 10 |
Total Lines | 55 |
Code Lines | 41 |
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 bika.lims.browser.get_date() 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 | # -*- coding: utf-8 -*- |
||
40 | :param time: The time to process |
||
41 | :type time: str/DateTime |
||
42 | :param long_format: If True, return time in ling format |
||
43 | :type portal_type: boolean/null |
||
44 | :param time_only: If True, only returns time. |
||
45 | :type title: boolean/null |
||
46 | :param context: The current context |
||
47 | :type context: ATContentType |
||
48 | :param request: The current request |
||
49 | :type request: HTTPRequest object |
||
50 | :returns: The formatted date as string |
||
51 | :rtype: string |
||
52 | """ |
||
53 | # if time is a string, we'll try pass it through strptime with the various |
||
54 | # formats defined. |
||
55 | time = api.to_date(time) |
||
56 | if not time or not isinstance(time, DateTime): |
||
57 | return '' |
||
58 | |||
59 | # no printing times if they were not specified in inputs |
||
60 | if time.second() + time.minute() + time.hour() == 0: |
||
61 | long_format = False |
||
62 | try: |
||
63 | time_str = _ut(time, long_format, time_only, context, 'senaite.core', request) |
||
64 | except ValueError: |
||
65 | err_msg = traceback.format_exc() + '\n' |
||
66 | logger.warn( |
||
67 | err_msg + '\n' + |
||
68 | "Error converting '{}' time to string in {}." |
||
69 | .format(time, context)) |
||
70 | time_str = '' |
||
71 | return time_str |
||
72 | |||
73 | |||
74 | class BrowserView(BaseBrowserView): |
||
75 | security = ClassSecurityInfo() |
||
76 | |||
77 | logger = logger |
||
|
|||
78 | |||
79 | def __init__(self, context, request): |
||
80 | self.context = context |
||
81 | self.request = request |
||
82 | super(BrowserView, self).__init__(context, request) |
||
83 | |||
84 | security.declarePublic('ulocalized_time') |
||
85 | |||
86 | def ulocalized_time(self, time, long_format=None, time_only=None): |
||
87 | return ulocalized_time(time, long_format, time_only, |
||
88 | context=self.context, request=self.request) |
||
89 | |||
90 | @lazy_property |
||
91 | def portal(self): |
||
92 | return getToolByName(self.context, 'portal_url').getPortalObject() |
||
93 | |||
94 | @lazy_property |
||
95 | def portal_url(self): |
||
210 |