Skip to content

Commit

Permalink
FitFileDataProcessor cache methods not just method names
Browse files Browse the repository at this point in the history
  • Loading branch information
xmedeko committed Mar 7, 2018
1 parent 9d8740d commit 04d51db
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions fitparse/processors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime

from fitparse.utils import scrub_method_name

# Datetimes (uint32) represent seconds since this UTC_REFERENCE
Expand All @@ -19,11 +20,11 @@ class FitFileDataProcessor(object):
#def process_units_<unit_name> (field_data)
#def process_message_<mesg_name / mesg_type_num> (data_message)

# Used to memoize scrubbed method names
_scrubbed_method_names = {}
# Used to memoize scrubbed methods
_method_cache = {}

def _scrub_method_name(self, method_name):
"""Scrubs a method name, returning result from local cache if available.
def _get_scrubbed_method(self, method_name):
"""Scrubs a method name and cache it _method_cache.
This method wraps fitparse.utils.scrub_method_name and memoizes results,
as scrubbing a method name is expensive.
Expand All @@ -32,36 +33,36 @@ def _scrub_method_name(self, method_name):
method_name: Method name to scrub.
Returns:
Scrubbed method name.
Scrubbed method (unbounded).
"""
if method_name not in self._scrubbed_method_names:
self._scrubbed_method_names[method_name] = (
scrub_method_name(method_name))

return self._scrubbed_method_names[method_name]
method = self._method_cache.get(method_name)
if method is not False:
scrubbed_method_name = scrub_method_name(method_name)
try:
method = getattr(self, scrubbed_method_name).__func__
except AttributeError:
method = False
self._method_cache[method_name] = method
return method

def _run_processor(self, method_name, data):
method = self._get_scrubbed_method(method_name)
if method is False:
return
method(self, data)

def run_type_processor(self, field_data):
self._run_processor(self._scrub_method_name(
'process_type_%s' % field_data.type.name), field_data)
self._run_processor('process_type_%s' % field_data.type.name, field_data)

def run_field_processor(self, field_data):
self._run_processor(self._scrub_method_name(
'process_field_%s' % field_data.name), field_data)
self._run_processor('process_field_%s' % field_data.name, field_data)

def run_unit_processor(self, field_data):
if field_data.units:
self._run_processor(self._scrub_method_name(
'process_units_%s' % field_data.units), field_data)
self._run_processor('process_units_%s' % field_data.units, field_data)

def run_message_processor(self, data_message):
self._run_processor(self._scrub_method_name(
'process_message_%s' % data_message.def_mesg.name), data_message)

def _run_processor(self, processor_name, data):
try:
getattr(self, processor_name)(data)
except AttributeError:
pass
self._run_processor('process_message_%s' % data_message.def_mesg.name, data_message)

def process_type_bool(self, field_data):
if field_data.value is not None:
Expand Down

0 comments on commit 04d51db

Please sign in to comment.