Skip to content

Commit

Permalink
redesigned _clean methods on Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Fee committed May 28, 2013
1 parent 22899a2 commit 5770fd0
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions analytics/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import threading
from datetime import datetime, timedelta
import collections
from datetime import datetime, timedelta
import json
import logging
import numbers
import threading

from dateutil.tz import tzutc
import requests
Expand All @@ -15,7 +16,6 @@


logging_enabled = True
import logging
logger = logging.getLogger('analytics')


Expand Down Expand Up @@ -174,28 +174,33 @@ def _check_for_secret(self):
raise Exception('Please set analytics.secret before calling ' +
'identify or track.')

def _clean(self, d):
# TODO: reduce the complexity (mccabe)
to_delete = []
for key in d.iterkeys():
val = d[key]
if isinstance(val, (dict)):
self._clean(val)
elif isinstance(val, (list, tuple, set)):
self._clean(val)
elif not isinstance(val, (str, unicode, int, long, float, bool,
numbers.Number, datetime)):
try:
# coerce values to unicode
d[key] = unicode(d[key])
except TypeError, e: # TODO: e is assigned, but unused
log('warn', 'Dictionary values must be serializeable to ' +
'JSON "{0}" value {1} of type {2} is ' +
'unsupported.'.
format(key, d[key], type(d[key])))
to_delete.append(key)
for key in to_delete:
del d[key]
def _coerce_unicode(self, cmplx):
return unicode(cmplx)

def _clean_list(self, l):
return [self._clean(item) for item in l]

def _clean_dict(self, d):
data = {}
for k, v in d.iteritems():
try:
data[k] = self._clean(v)
except TypeError:
log('warn', 'Dictionary values must be serializeable to ' +
'JSON "%s" value %s of type %s is unsupported.'
% (k, v, type(v)))
return data

def _clean(self, item):
if isinstance(item, (str, unicode, int, long, float, bool,
numbers.Number, datetime)):
return item
elif isinstance(item, (set, list, tuple)):
return self._clean_list(item)
elif isinstance(item, dict):
return self._clean_dict(item)
else:
return self._coerce_unicode(item)

def on_success(self, callback):
self.success_callbacks.append(callback)
Expand Down Expand Up @@ -244,10 +249,10 @@ def identify(self, user_id=None, traits={}, context={}, timestamp=None):
else:
timestamp = guess_timezone(timestamp)

self._clean(traits)
cleaned_traits = self._clean(traits)

action = {'userId': user_id,
'traits': traits,
'traits': cleaned_traits,
'context': context,
'timestamp': timestamp.isoformat(),
'action': 'identify'}
Expand Down Expand Up @@ -307,12 +312,12 @@ def track(self, user_id=None, event=None, properties={}, context={},
else:
timestamp = guess_timezone(timestamp)

self._clean(properties)
cleaned_properties = self._clean(properties)

action = {'userId': user_id,
'event': event,
'context': context,
'properties': properties,
'properties': cleaned_properties,
'timestamp': timestamp.isoformat(),
'action': 'track'}

Expand Down

0 comments on commit 5770fd0

Please sign in to comment.