Skip to content

Commit a502914

Browse files
committed
Add black code linter
1 parent 19e7ed2 commit a502914

File tree

11 files changed

+232
-219
lines changed

11 files changed

+232
-219
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ jobs:
2727
- run: python -m pip install pydocstyle
2828
- run: pydocstyle .
2929

30+
black:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/setup-python@v1
34+
- uses: actions/checkout@v2
35+
- run: python -m pip install black
36+
- run: black --check --diff .
37+
3038
docs:
3139
runs-on: ubuntu-latest
3240
steps:
@@ -51,6 +59,7 @@ jobs:
5159
- isort
5260
- pydocstyle
5361
- flake8
62+
- black
5463
strategy:
5564
matrix:
5665
os:

django_measurement/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
from appconf import AppConf
33
from django.conf import settings
44

5-
__all__ = ('settings',)
5+
__all__ = ("settings",)
66

77

88
class DjangoMeasurementConf(AppConf):
99
"""Settings for django-measurement."""
1010

11-
BIDIMENSIONAL_SEPARATOR = '/'
11+
BIDIMENSIONAL_SEPARATOR = "/"
1212
"""
1313
For measurement classes subclassing a BidimensionalMeasure, this .
1414
"""
1515

1616
class Meta:
17-
prefix = 'measurement'
17+
prefix = "measurement"

django_measurement/forms.py

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@
99

1010

1111
class MeasurementWidget(forms.MultiWidget):
12-
def __init__(self, attrs=None, float_widget=None,
13-
unit_choices_widget=None, unit_choices=None, *args, **kwargs):
12+
def __init__(
13+
self,
14+
attrs=None,
15+
float_widget=None,
16+
unit_choices_widget=None,
17+
unit_choices=None,
18+
*args,
19+
**kwargs
20+
):
1421

1522
self.unit_choices = unit_choices
1623

1724
if not float_widget:
1825
float_widget = forms.TextInput(attrs=attrs)
1926

2027
if not unit_choices_widget:
21-
unit_choices_widget = forms.Select(
22-
attrs=attrs,
23-
choices=unit_choices
24-
)
28+
unit_choices_widget = forms.Select(attrs=attrs, choices=unit_choices)
2529

2630
widgets = (float_widget, unit_choices_widget)
2731
super(MeasurementWidget, self).__init__(widgets, attrs)
2832

2933
def decompress(self, value):
3034
if value:
31-
choice_units = set([
32-
u
33-
for u, n in self.unit_choices
34-
])
35+
choice_units = set([u for u, n in self.unit_choices])
3536

3637
unit = value.STANDARD_UNIT
3738
if unit not in choice_units:
@@ -44,42 +45,56 @@ def decompress(self, value):
4445

4546

4647
class MeasurementField(forms.MultiValueField):
47-
def __init__(self, measurement, max_value=None, min_value=None,
48-
unit_choices=None, validators=None,
49-
bidimensional_separator=settings.MEASUREMENT_BIDIMENSIONAL_SEPARATOR,
50-
*args, **kwargs):
48+
def __init__(
49+
self,
50+
measurement,
51+
max_value=None,
52+
min_value=None,
53+
unit_choices=None,
54+
validators=None,
55+
bidimensional_separator=settings.MEASUREMENT_BIDIMENSIONAL_SEPARATOR,
56+
*args,
57+
**kwargs
58+
):
5159

5260
if not issubclass(measurement, (MeasureBase, BidimensionalMeasure)):
53-
raise ValueError(
54-
"%s must be a subclass of MeasureBase" % measurement
55-
)
61+
raise ValueError("%s must be a subclass of MeasureBase" % measurement)
5662

5763
self.measurement_class = measurement
5864
if not unit_choices:
5965
if issubclass(measurement, BidimensionalMeasure):
60-
assert isinstance(bidimensional_separator, str), \
61-
"Supplied bidimensional_separator for %s must be of string/unicode type;" \
62-
" Instead got type %s" % (measurement, str(type(bidimensional_separator)),)
63-
unit_choices = tuple((
66+
assert isinstance(bidimensional_separator, str), (
67+
"Supplied bidimensional_separator for %s must be of string/unicode type;"
68+
" Instead got type %s"
69+
% (measurement, str(type(bidimensional_separator)),)
70+
)
71+
unit_choices = tuple(
6472
(
65-
'{0}__{1}'.format(primary, reference),
66-
'{0}{1}{2}'.format(
67-
getattr(measurement.PRIMARY_DIMENSION, 'LABELS', {}).get(
68-
primary, primary),
69-
bidimensional_separator,
70-
getattr(measurement.REFERENCE_DIMENSION, 'LABELS', {}).get(
71-
reference, reference)),
73+
(
74+
"{0}__{1}".format(primary, reference),
75+
"{0}{1}{2}".format(
76+
getattr(
77+
measurement.PRIMARY_DIMENSION, "LABELS", {}
78+
).get(primary, primary),
79+
bidimensional_separator,
80+
getattr(
81+
measurement.REFERENCE_DIMENSION, "LABELS", {}
82+
).get(reference, reference),
83+
),
84+
)
85+
for primary, reference in product(
86+
measurement.PRIMARY_DIMENSION.get_units(),
87+
measurement.REFERENCE_DIMENSION.get_units(),
88+
)
7289
)
73-
for primary, reference in product(
74-
measurement.PRIMARY_DIMENSION.get_units(),
75-
measurement.REFERENCE_DIMENSION.get_units(),
76-
)
77-
))
90+
)
7891
else:
79-
unit_choices = tuple((
80-
(u, getattr(measurement, 'LABELS', {}).get(u, u))
81-
for u in measurement.get_units()
82-
))
92+
unit_choices = tuple(
93+
(
94+
(u, getattr(measurement, "LABELS", {}).get(u, u))
95+
for u in measurement.get_units()
96+
)
97+
)
8398

8499
if validators is None:
85100
validators = []
@@ -99,16 +114,17 @@ def __init__(self, measurement, max_value=None, min_value=None,
99114
float_field = forms.FloatField(*args, **kwargs)
100115
choice_field = forms.ChoiceField(choices=unit_choices)
101116
defaults = {
102-
'widget': MeasurementWidget(
117+
"widget": MeasurementWidget(
103118
float_widget=float_field.widget,
104119
unit_choices_widget=choice_field.widget,
105-
unit_choices=unit_choices
120+
unit_choices=unit_choices,
106121
),
107122
}
108123
defaults.update(kwargs)
109124
fields = (float_field, choice_field)
110-
super(MeasurementField, self).__init__(fields, validators=validators,
111-
*args, **defaults)
125+
super(MeasurementField, self).__init__(
126+
fields, validators=validators, *args, **defaults
127+
)
112128

113129
def compress(self, data_list):
114130
if not data_list:
@@ -118,8 +134,4 @@ def compress(self, data_list):
118134
if value in self.empty_values:
119135
return None
120136

121-
return utils.get_measurement(
122-
self.measurement_class,
123-
value,
124-
unit
125-
)
137+
return utils.get_measurement(self.measurement_class, value, unit)

django_measurement/models.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import forms
1010
from .utils import get_measurement
1111

12-
logger = logging.getLogger('django_measurement')
12+
logger = logging.getLogger("django_measurement")
1313

1414

1515
class MeasurementField(FloatField):
@@ -20,44 +20,51 @@ class MeasurementField(FloatField):
2020
MeasureBase,
2121
)
2222
default_error_messages = {
23-
'invalid_type': _(
24-
"'%(value)s' (%(type_given)s) value"
25-
" must be of type %(type_wanted)s."
23+
"invalid_type": _(
24+
"'%(value)s' (%(type_given)s) value" " must be of type %(type_wanted)s."
2625
),
2726
}
2827

29-
def __init__(self, verbose_name=None, name=None, measurement=None,
30-
measurement_class=None, unit_choices=None, *args, **kwargs):
28+
def __init__(
29+
self,
30+
verbose_name=None,
31+
name=None,
32+
measurement=None,
33+
measurement_class=None,
34+
unit_choices=None,
35+
*args,
36+
**kwargs
37+
):
3138

3239
if not measurement and measurement_class is not None:
3340
warnings.warn(
34-
"\"measurement_class\" will be removed in version 4.0",
35-
DeprecationWarning
41+
'"measurement_class" will be removed in version 4.0', DeprecationWarning
3642
)
3743
measurement = getattr(measures, measurement_class)
3844

3945
if not measurement:
40-
raise TypeError('MeasurementField() takes a measurement'
41-
' keyword argument. None given.')
46+
raise TypeError(
47+
"MeasurementField() takes a measurement"
48+
" keyword argument. None given."
49+
)
4250

4351
if not issubclass(measurement, self.MEASURE_BASES):
4452
raise TypeError(
45-
'MeasurementField() takes a measurement keyword argument.'
46-
' It has to be a valid MeasureBase subclass.'
53+
"MeasurementField() takes a measurement keyword argument."
54+
" It has to be a valid MeasureBase subclass."
4755
)
4856

4957
self.measurement = measurement
5058
self.widget_args = {
51-
'measurement': measurement,
52-
'unit_choices': unit_choices,
59+
"measurement": measurement,
60+
"unit_choices": unit_choices,
5361
}
5462

55-
super(MeasurementField, self).__init__(verbose_name, name,
56-
*args, **kwargs)
63+
super(MeasurementField, self).__init__(verbose_name, name, *args, **kwargs)
5764

5865
def deconstruct(self):
5966
name, path, args, kwargs = super(MeasurementField, self).deconstruct()
60-
kwargs['measurement'] = self.measurement
67+
kwargs["measurement"] = self.measurement
6168
return name, path, args, kwargs
6269

6370
def get_prep_value(self, value):
@@ -75,7 +82,7 @@ def get_prep_value(self, value):
7582
return super(MeasurementField, self).get_prep_value(value)
7683

7784
def get_default_unit(self):
78-
unit_choices = self.widget_args['unit_choices']
85+
unit_choices = self.widget_args["unit_choices"]
7986
if unit_choices:
8087
return unit_choices[0][0]
8188
return self.measurement.STANDARD_UNIT
@@ -94,10 +101,10 @@ def value_to_string(self, obj):
94101
value = self.value_from_object(obj)
95102
if not isinstance(value, self.MEASURE_BASES):
96103
return value
97-
return '%s:%s' % (value.value, value.unit)
104+
return "%s:%s" % (value.value, value.unit)
98105

99106
def deserialize_value_from_string(self, s: str):
100-
parts = s.split(':', 1)
107+
parts = s.split(":", 1)
101108
if len(parts) != 2:
102109
return None
103110
value, unit = float(parts[0]), parts[1]
@@ -118,23 +125,22 @@ def to_python(self, value):
118125

119126
return_unit = self.get_default_unit()
120127

121-
msg = "You assigned a %s instead of %s to %s.%s.%s, unit was guessed to be \"%s\"." % (
122-
type(value).__name__,
123-
str(self.measurement.__name__),
124-
self.model.__module__,
125-
self.model.__name__,
126-
self.name,
127-
return_unit,
128+
msg = (
129+
'You assigned a %s instead of %s to %s.%s.%s, unit was guessed to be "%s".'
130+
% (
131+
type(value).__name__,
132+
str(self.measurement.__name__),
133+
self.model.__module__,
134+
self.model.__name__,
135+
self.name,
136+
return_unit,
137+
)
128138
)
129139
logger.warning(msg)
130-
return get_measurement(
131-
measure=self.measurement,
132-
value=value,
133-
unit=return_unit,
134-
)
140+
return get_measurement(measure=self.measurement, value=value, unit=return_unit,)
135141

136142
def formfield(self, **kwargs):
137-
defaults = {'form_class': forms.MeasurementField}
143+
defaults = {"form_class": forms.MeasurementField}
138144
defaults.update(kwargs)
139145
defaults.update(self.widget_args)
140146
return super(MeasurementField, self).formfield(**defaults)

django_measurement/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
def get_measurement(measure, value, unit=None, original_unit=None):
55
unit = unit or measure.STANDARD_UNIT
66

7-
m = measure(
8-
**{unit: value}
9-
)
7+
m = measure(**{unit: value})
108
if original_unit:
119
m.unit = original_unit
1210
if isinstance(m, BidimensionalMeasure):

setup.cfg

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,18 @@ addopts =
7878
show_missing = True
7979

8080
[flake8]
81-
max-line-length = 99
82-
max-complexity = 10
83-
statistics = true
84-
show-source = true
85-
exclude = */migrations/*,docs/*,env/*,.tox/*,.eggs/*
81+
max-line-length=88
82+
select = C,E,F,W,B,B950
83+
ignore = E203, E501, W503
84+
exclude = venv,.tox,.eggs
8685

8786
[pydocstyle]
88-
add_ignore = D1
87+
add-ignore = D1
8988

9089
[isort]
9190
atomic = true
92-
line_length = 79
93-
multi_line_output=5
91+
line_length = 88
92+
multi_line_output=3
9493
include_trailing_comma=True
9594
force_grid_wrap=0
9695
use_parentheses=True

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
from setuptools import setup
33

4-
setup(name='django-measurement', use_scm_version=True)
4+
setup(name="django-measurement", use_scm_version=True)

0 commit comments

Comments
 (0)