Conditions | 11 |
Total Lines | 54 |
Code Lines | 30 |
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 senaite.core.schema.uidreferencefield.UIDReferenceField.set() 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 -*- |
||
107 | def set(self, object, value): |
||
108 | """Set UID reference |
||
109 | |||
110 | :param object: the instance of the field |
||
111 | :param value: object/UID/SuperModel |
||
112 | :type value: list/tuple/str |
||
113 | """ |
||
114 | |||
115 | # always handle all values internally as a list |
||
116 | if isinstance(value, six.string_types): |
||
117 | value = [value] |
||
118 | elif api.is_object(value): |
||
119 | value = [value] |
||
120 | |||
121 | # check if the fields accepts single values only |
||
122 | if not self.multi_valued and len(value) > 1: |
||
123 | raise TypeError("Single valued field accepts at most 1 value") |
||
124 | |||
125 | # check if the type is allowed |
||
126 | allowed_types = self.get_allowed_types() |
||
127 | if allowed_types: |
||
128 | objs = filter(None, map(self.get_object, value)) |
||
129 | types = set(map(api.get_portal_type, objs)) |
||
130 | if not types.issubset(allowed_types): |
||
131 | raise TypeError("Only the following types are allowed: %s" |
||
132 | % ",".join(allowed_types)) |
||
133 | |||
134 | # convert to UIDs |
||
135 | uids = [] |
||
136 | for v in value: |
||
137 | uid = self.get_uid(v) |
||
138 | if uid is None: |
||
139 | continue |
||
140 | uids.append(uid) |
||
141 | |||
142 | # current set UIDs |
||
143 | existing = self.get_raw(object) |
||
144 | |||
145 | # filter out new/removed UIDs |
||
146 | added_uids = [u for u in uids if u not in existing] |
||
147 | added_objs = filter(None, map(self.get_object, added_uids)) |
||
148 | |||
149 | removed_uids = [u for u in existing if u not in uids] |
||
150 | removed_objs = filter(None, map(self.get_object, removed_uids)) |
||
151 | |||
152 | # link backreferences of new uids |
||
153 | for added_obj in added_objs: |
||
154 | self.link_bref(added_obj, object) |
||
155 | |||
156 | # unlink backreferences of removed UIDs |
||
157 | for removed_obj in removed_objs: |
||
158 | self.unlink_bref(removed_obj, object) |
||
159 | |||
160 | super(UIDReferenceField, self).set(object, uids) |
||
161 | |||
240 |