| Conditions | 17 |
| Total Lines | 67 |
| Lines | 14 |
| Ratio | 20.9 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 patch_qcombobox() 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 | # The code below, as well as the associated test were adapted from |
||
| 35 | def patch_qcombobox(QComboBox): |
||
| 36 | """ |
||
| 37 | In PySide, using Python objects as userData in QComboBox causes |
||
| 38 | Segmentation faults under certain conditions. Even in cases where it |
||
| 39 | doesn't, findData does not work correctly. Likewise, findData also does not |
||
| 40 | work correctly with Python objects when using PyQt4. On the other hand, |
||
| 41 | PyQt5 deals with this case correctly. We therefore patch QComboBox when |
||
| 42 | using PyQt4 and PySide to avoid issues. |
||
| 43 | """ |
||
| 44 | |||
| 45 | from ..QtGui import QIcon |
||
| 46 | from ..QtCore import Qt, QObject |
||
| 47 | |||
| 48 | class userDataWrapper(): |
||
| 49 | """ |
||
| 50 | This class is used to wrap any userData object. If we don't do this, |
||
| 51 | then certain types of objects can cause segmentation faults or issues |
||
| 52 | depending on whether/how __getitem__ is defined. |
||
| 53 | """ |
||
| 54 | def __init__(self, data): |
||
| 55 | self.data = data |
||
| 56 | |||
| 57 | _addItem = QComboBox.addItem |
||
| 58 | |||
| 59 | View Code Duplication | def addItem(self, *args, **kwargs): |
|
|
|
|||
| 60 | if len(args) == 3 or (not isinstance(args[0], QIcon) |
||
| 61 | and len(args) == 2): |
||
| 62 | args, kwargs['userData'] = args[:-1], args[-1] |
||
| 63 | if 'userData' in kwargs: |
||
| 64 | kwargs['userData'] = userDataWrapper(kwargs['userData']) |
||
| 65 | _addItem(self, *args, **kwargs) |
||
| 66 | |||
| 67 | _insertItem = QComboBox.insertItem |
||
| 68 | |||
| 69 | View Code Duplication | def insertItem(self, *args, **kwargs): |
|
| 70 | if len(args) == 4 or (not isinstance(args[1], QIcon) |
||
| 71 | and len(args) == 3): |
||
| 72 | args, kwargs['userData'] = args[:-1], args[-1] |
||
| 73 | if 'userData' in kwargs: |
||
| 74 | kwargs['userData'] = userDataWrapper(kwargs['userData']) |
||
| 75 | _insertItem(self, *args, **kwargs) |
||
| 76 | |||
| 77 | _setItemData = QComboBox.setItemData |
||
| 78 | |||
| 79 | def setItemData(self, index, value, role=Qt.UserRole): |
||
| 80 | value = userDataWrapper(value) |
||
| 81 | _setItemData(self, index, value, role=role) |
||
| 82 | |||
| 83 | _itemData = QComboBox.itemData |
||
| 84 | |||
| 85 | def itemData(self, index, role=Qt.UserRole): |
||
| 86 | userData = _itemData(self, index, role=role) |
||
| 87 | if isinstance(userData, userDataWrapper): |
||
| 88 | userData = userData.data |
||
| 89 | return userData |
||
| 90 | |||
| 91 | def findData(self, value): |
||
| 92 | for i in range(self.count()): |
||
| 93 | if self.itemData(i) == value: |
||
| 94 | return i |
||
| 95 | return -1 |
||
| 96 | |||
| 97 | QComboBox.addItem = addItem |
||
| 98 | QComboBox.insertItem = insertItem |
||
| 99 | QComboBox.setItemData = setItemData |
||
| 100 | QComboBox.itemData = itemData |
||
| 101 | QComboBox.findData = findData |