Skip to content

Commit 03b031b

Browse files
committed
Cut long lines and simplify hard to read sections
1 parent 0431c32 commit 03b031b

18 files changed

+187
-131
lines changed

bandicoot/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def __init__(self, interaction=None, direction=None, correspondent_id=None,
6262
self.position = position
6363

6464
def __repr__(self):
65-
return "Record(" + ", ".join(["%s=%r" % (x, getattr(self, x)) for x in self.__slots__]) + ")"
65+
attr = ["%s=%r" % (x, getattr(self, x)) for x in self.__slots__]
66+
return "Record(" + ", ".join(attr) + ")"
6667

6768
def __eq__(self, other):
6869
if isinstance(other, self.__class__) and self.__slots__ == other.__slots__:
@@ -476,7 +477,8 @@ def __equals__(self, other):
476477
(self.retailer_id == other.retailer_id)
477478

478479
def __repr__(self):
479-
return "Recharge(" + ", ".join(["%s=%r" % (x, getattr(self, x)) for x in self.__slots__]) + ")"
480+
attr = ["%s=%r" % (x, getattr(self, x)) for x in self.__slots__]
481+
return "Recharge(" + ", ".join(attr) + ")"
480482

481483
def __hash__(self):
482484
return hash(str(self))

bandicoot/helper/group.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def filter_user(user, using='records', interaction=None,
8484
lambda r: r.datetime.isoweekday() in user.weekend, records)
8585
elif part_of_week != 'allweek':
8686
raise KeyError(
87-
"{} is not a valid value for part_of_week. it should be 'weekday', 'weekend' or 'allweek'.".format(part_of_week))
87+
"{} is not a valid value for part_of_week. it should be 'weekday', "
88+
"'weekend' or 'allweek'.".format(part_of_week))
8889

8990
if user.night_start < user.night_end:
9091
night_filter = lambda r: user.night_end > r.datetime.time(
@@ -230,20 +231,24 @@ def infer_type(data):
230231
return 'distribution_summarystats'
231232

232233
raise TypeError(
233-
"{} is not a valid input. It should be a number, a SummaryStats object, or None".format(data[0]))
234+
"{} is not a valid input. It should be a number, a SummaryStats "
235+
"object, or None".format(data[0]))
234236

235237
raise TypeError(
236-
"{} is not a valid input. It should be a number, a SummaryStats object, or a list".format(data))
238+
"{} is not a valid input. It should be a number, a SummaryStats "
239+
"object, or a list".format(data))
237240

238241

239242
def statistics(data, summary='default', datatype=None):
240243
"""
241-
Return statistics (mean, standard error, standard error and median, min and max) on data metrics.
244+
Return statistics (mean, standard error, standard error and median,
245+
min and max) on data metrics.
242246
243247
Examples
244248
--------
245249
Given a list of integers or floating point numbers,
246-
``statistics`` computes the mean and standard error of the mean, and the min and max.
250+
``statistics`` computes the mean and standard error of the mean,
251+
and the min and max.
247252
248253
>>> statistics([0, 1, 2, 3])
249254
{'mean': 1.5, 'std': 1.2910, 'min': 0, 'max': 3}

bandicoot/helper/maths.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,18 @@ class SummaryStats(object):
152152
'skewness', 'kurtosis', 'distribution']
153153

154154
def __init__(self, mean, std, min, max, median, skewness, kurtosis, distribution):
155-
self.mean, self.std, self.min, self.max, self.median, self.skewness, self.kurtosis, self.distribution = mean, std, min, max, median, skewness, kurtosis, distribution
155+
self.mean = mean
156+
self.std = std
157+
self.min = min
158+
self.max = max
159+
self.median = median
160+
self.skewness = skewness
161+
self.kurtosis = kurtosis
162+
self.distribution = distribution
156163

157164
def __repr__(self):
158-
return "SummaryStats(" + ", ".join(["%s=%r" % (x, getattr(self, x)) for x in self.__slots__]) + ")"
165+
attrs = ["%s=%r" % (x, getattr(self, x)) for x in self.__slots__]
166+
return "SummaryStats(" + ", ".join(attrs) + ")"
159167

160168
def __eq__(self, other):
161169
if isinstance(other, self.__class__) and self.__slots__ == other.__slots__:
@@ -191,7 +199,8 @@ def summary_stats(data):
191199
_skewness = skewness(data)
192200
_distribution = data
193201

194-
return SummaryStats(_mean, _std, _minimum, _maximum, _median, _skewness, _kurtosis, _distribution)
202+
return SummaryStats(_mean, _std, _minimum, _maximum,
203+
_median, _skewness, _kurtosis, _distribution)
195204

196205

197206
def entropy(data):
@@ -210,7 +219,8 @@ def entropy(data):
210219

211220
def great_circle_distance(pt1, pt2):
212221
"""
213-
Return the great-circle distance in kilometers between two points, defined by a tuple (lat, lon).
222+
Return the great-circle distance in kilometers between two points,
223+
defined by a tuple (lat, lon).
214224
215225
Examples
216226
--------

bandicoot/individual.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ def call_duration(records, direction=None):
155155

156156
def _conversations(group, delta=datetime.timedelta(hours=1)):
157157
"""
158-
Group texts into conversations. The function returns an iterator over records grouped by conversations.
158+
Group texts into conversations. The function returns an iterator over
159+
records grouped by conversations.
159160
160-
See :ref:`Using bandicoot <conversations-label>` for a definition of conversations.
161+
See :ref:`Using bandicoot <conversations-label>` for a definition of
162+
conversations.
161163
162-
A conversation begins when one person sends a text-message to the other and ends when one of them makes a phone call
163-
or there is no activity between them for an hour.
164+
A conversation begins when one person sends a text-message to the other and
165+
ends when one of them makes a phone call or there is no activity between
166+
them for an hour.
164167
"""
165168
last_time = None
166169
results = []
@@ -196,8 +199,9 @@ def response_rate_text(records):
196199
"""
197200
The response rate of the user (between 0 and 1).
198201
199-
Considers text-conversations which began with an incoming text. Response rate
200-
is the fraction of such conversations in which the user sent a text (a response).
202+
Considers text-conversations which began with an incoming text. The response
203+
rate is the fraction of such conversations in which the user sent a text
204+
(a response).
201205
202206
The following sequence of messages defines four conversations (``I`` for an
203207
incoming text, ``O`` for an outgoing text): ::
@@ -207,9 +211,11 @@ def response_rate_text(records):
207211
I-I-I-I => Started with an incoming text but doesn't have outgoing texts
208212
O-O-I-O => Not starting with an incoming text
209213
210-
Here, the ratio would be 2/3 as we have 3 conversations starting with an incoming text and 2 of them have at least one outgoing text.
214+
Here, the ratio would be 2/3 as we have 3 conversations starting with an
215+
incoming text and 2 of them have at least one outgoing text.
211216
212-
See :ref:`Using bandicoot <conversations-label>` for a definition of conversations.
217+
See :ref:`Using bandicoot <conversations-label>` for a definition of
218+
conversations.
213219
"""
214220
if len(records) == 0:
215221
return None
@@ -257,9 +263,10 @@ def response_delay_text(records):
257263
258264
Notes
259265
-----
260-
See :ref:`Using bandicoot <conversations-label>` for a definition of conversations.
261-
Conversation are defined to be a series of text messages each sent no more than an hour
262-
after the previous. The response delay can thus not be greater than one hour.
266+
See :ref:`Using bandicoot <conversations-label>` for a definition of
267+
conversations. Conversation are defined to be a series of text messages each
268+
sent no more than an hour after the previous. The response delay can thus
269+
not be greater than one hour.
263270
"""
264271
interactions = defaultdict(list)
265272
for r in records:

bandicoot/io.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
from __future__ import with_statement, division
2828

29-
from .core import User, Record, Position, Recharge
30-
from .helper.tools import OrderedDict, percent_overlapping_calls, percent_records_missing_location, antennas_missing_locations, ColorHandler
3129
from .utils import flatten
30+
from .core import User, Record, Position, Recharge
31+
from .helper.tools import OrderedDict, percent_overlapping_calls, \
32+
percent_records_missing_location, antennas_missing_locations, ColorHandler
3233

3334
from datetime import datetime
3435
from json import dumps
@@ -180,7 +181,8 @@ def _map_position(data):
180181
direction=data['direction'],
181182
correspondent_id=data['correspondent_id'],
182183
datetime=_tryto(
183-
lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"), data['datetime']),
184+
lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"),
185+
data['datetime']),
184186
call_duration=_tryto(_map_duration, data['call_duration']),
185187
position=_tryto(_map_position, data))
186188

bandicoot/network.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ def _count_interaction(user, interaction=None, direction='out'):
6767

6868
def _interaction_matrix(user, interaction=None, default=0, missing=None):
6969
generating_fn = partial(_count_interaction, interaction=interaction)
70-
71-
# Just in case, we remove the user from user.network (self records can happen)
7270
neighbors = matrix_index(user)
7371

7472
def make_direction(direction):
@@ -267,7 +265,9 @@ def assortativity_indicators(user):
267265

268266
for i, u_name in enumerate(matrix_index(user)):
269267
correspondent = user.network.get(u_name, None)
270-
if correspondent is None or u_name == user.name or matrix[0][i] == 0: # Non reciprocated edge
268+
269+
# Non reciprocated edge
270+
if correspondent is None or u_name == user.name or matrix[0][i] == 0:
271271
continue
272272

273273
neighbor_indics = all(correspondent, flatten=True)
@@ -328,12 +328,8 @@ def network_sampling(n, filename, directory=None, snowball=False, user=None):
328328
File to export to.
329329
directory: string
330330
Directory to select users from if using the default random selection.
331-
332-
Selection options
333-
-----------------
334-
random (default): selects n users at random
335-
336-
snowball: starts from a specified user, iterates over neighbors, and does a BFS until n neighbors are reached
331+
snowball: starts from a specified user, iterates over neighbors, and does a
332+
BFS until n neighbors are reached
337333
"""
338334
if snowball:
339335
if user is None:

bandicoot/tests/samples/regressions/ego.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"reporting__attributes_path": "samples/attributes",
66
"reporting__recharges_path": "samples/attributes",
77
"reporting__version": "0.5.2",
8-
"reporting__code_signature": "c4c6a317c309bb87382ed96a8cd4472e65528dde",
8+
"reporting__code_signature": "6d0f6c0556a33bd29d051b0a782573652141ef54",
99
"reporting__groupby": "week",
1010
"reporting__split_week": true,
1111
"reporting__split_day": true,

bandicoot/tests/samples/regressions/empty_user.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"reporting__attributes_path": null,
66
"reporting__recharges_path": null,
77
"reporting__version": "0.5.2",
8-
"reporting__code_signature": "c4c6a317c309bb87382ed96a8cd4472e65528dde",
8+
"reporting__code_signature": "6d0f6c0556a33bd29d051b0a782573652141ef54",
99
"reporting__groupby": "week",
1010
"reporting__split_week": true,
1111
"reporting__split_day": true,

bandicoot/tests/samples/regressions/manual_a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"reporting__attributes_path": null,
66
"reporting__recharges_path": null,
77
"reporting__version": "0.5.2",
8-
"reporting__code_signature": "c4c6a317c309bb87382ed96a8cd4472e65528dde",
8+
"reporting__code_signature": "6d0f6c0556a33bd29d051b0a782573652141ef54",
99
"reporting__groupby": "week",
1010
"reporting__split_week": true,
1111
"reporting__split_day": true,

bandicoot/tests/samples/regressions/manual_a_orange_network.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"reporting__attributes_path": "samples/attributes",
66
"reporting__recharges_path": "samples/attributes",
77
"reporting__version": "0.5.2",
8-
"reporting__code_signature": "c4c6a317c309bb87382ed96a8cd4472e65528dde",
8+
"reporting__code_signature": "6d0f6c0556a33bd29d051b0a782573652141ef54",
99
"reporting__groupby": "week",
1010
"reporting__split_week": true,
1111
"reporting__split_day": true,

0 commit comments

Comments
 (0)