GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — v1 (#16)
by Maksim
03:39
created

dict_property()   F

Complexity

Conditions 11

Size

Total Lines 71

Duplication

Lines 60
Ratio 84.51 %

Code Coverage

Tests 28
CRAP Score 12.7405

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
dl 60
loc 71
ccs 28
cts 37
cp 0.7568
crap 12.7405
rs 3.5526
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B decorator_other() 31 31 5
B decorator_str() 29 29 4

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like dict_property() 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 -*-
2
3 1
import six
4 1
import logging
5
6 1
import pydash
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
def _handle_auto_doc_for_property(doc, typename):
12 1
	if doc is None:
13
		doc = '<AUTO>'
14
15 1
	if '<AUTO>' in doc:
16 1
		subst = (
17
			':getter: Get\n'
18
			'{pref}:setter: Set\n'
19
			'{pref}:rtype: {typename}'
20
		).format(
21
			typename=typename,
22
			pref='\t\t',
23
		)
24 1
		doc = doc.replace('<AUTO>', subst)
25
26 1
	return doc
27
28
29 1
def dict_enum_property(path, enumtype):
30 1
	def decorator(fn):
31
32 1
		def _get(self):
33 1
			v = pydash.get(self.raw, path)
34
35 1
			if v is None:
36
				return None
37
38 1
			return enumtype(v)
39
40 1
		def _set(self, value):
41 1
			if isinstance(value, enumtype):
42 1
				value = value.value
43 1
			value = fn(self, value)
44 1
			pydash.set_(self.raw, path, value)
45
46 1
			return enumtype(value)
47
48 1
		doc = _handle_auto_doc_for_property(
49
			fn.__doc__,
50
			'~{mod}.{nm}'.format(
51
				mod=enumtype.__module__,
52
				nm=enumtype.__name__,
53
			)
54
		)
55
56 1
		p = property(_get, _set, None, doc)
57 1
		return p
58 1
	return decorator
59
60
61
# TODO: test over unicode in python 2
62 1
def dict_property(path, anytype):
63
	"""
64
	Creates new strict-typed PROPERTY for classes inherited from :class:`dict`
65
	"""
66
67 1 View Code Duplication
	def decorator_str(fn):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
68
69 1
		def _get(self):
70 1
			v = pydash.get(self.raw, path)
71
72 1
			if v is None:
73 1
				return None
74
75 1
			return six.text_type(v)
76
77 1
		def _set(self, value):
78
			v = six.text_type(value)
79
			v = fn(self, v)
80
			pydash.set_(self.raw, path, v)
81
82
			return v
83
84 1
		doc = fn.__doc__
85
86 1
		typename = type(six.text_type('')).__name__
87
88 1
		doc = _handle_auto_doc_for_property(
89
			fn.__doc__,
90
			typename
91
		)
92
93 1
		p = property(_get, _set, None, doc)
94
95 1
		return p
96
97 1 View Code Duplication
	def decorator_other(fn):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
99 1
		def _get(self):
100 1
			v = pydash.get(self.raw, path)
101
102 1
			if v is None:
103
				return None
104
105 1
			return anytype(v)
106
107 1
		def _set(self, value):
108
			v = fn(self, value)
109
			pydash.set_(self.raw, path, v)
110
111
			return v
112
113 1
		doc = fn.__doc__
114
115 1
		if doc is None:
116
			doc = '<AUTO>'
117
118 1
		typename = anytype.__name__
119
120 1
		doc = _handle_auto_doc_for_property(
121
			fn.__doc__,
122
			typename
123
		)
124
125 1
		p = property(_get, _set, None, doc)
126
127 1
		return p
128
129 1
	if anytype == str:
130 1
		return decorator_str
131
132
	return decorator_other
133