Skip to content

Commit 04d51db

Browse files
committed
FitFileDataProcessor cache methods not just method names
1 parent 9d8740d commit 04d51db

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

fitparse/processors.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
23
from fitparse.utils import scrub_method_name
34

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

22-
# Used to memoize scrubbed method names
23-
_scrubbed_method_names = {}
23+
# Used to memoize scrubbed methods
24+
_method_cache = {}
2425

25-
def _scrub_method_name(self, method_name):
26-
"""Scrubs a method name, returning result from local cache if available.
26+
def _get_scrubbed_method(self, method_name):
27+
"""Scrubs a method name and cache it _method_cache.
2728
2829
This method wraps fitparse.utils.scrub_method_name and memoizes results,
2930
as scrubbing a method name is expensive.
@@ -32,36 +33,36 @@ def _scrub_method_name(self, method_name):
3233
method_name: Method name to scrub.
3334
3435
Returns:
35-
Scrubbed method name.
36+
Scrubbed method (unbounded).
3637
"""
37-
if method_name not in self._scrubbed_method_names:
38-
self._scrubbed_method_names[method_name] = (
39-
scrub_method_name(method_name))
40-
41-
return self._scrubbed_method_names[method_name]
38+
method = self._method_cache.get(method_name)
39+
if method is not False:
40+
scrubbed_method_name = scrub_method_name(method_name)
41+
try:
42+
method = getattr(self, scrubbed_method_name).__func__
43+
except AttributeError:
44+
method = False
45+
self._method_cache[method_name] = method
46+
return method
47+
48+
def _run_processor(self, method_name, data):
49+
method = self._get_scrubbed_method(method_name)
50+
if method is False:
51+
return
52+
method(self, data)
4253

4354
def run_type_processor(self, field_data):
44-
self._run_processor(self._scrub_method_name(
45-
'process_type_%s' % field_data.type.name), field_data)
55+
self._run_processor('process_type_%s' % field_data.type.name, field_data)
4656

4757
def run_field_processor(self, field_data):
48-
self._run_processor(self._scrub_method_name(
49-
'process_field_%s' % field_data.name), field_data)
58+
self._run_processor('process_field_%s' % field_data.name, field_data)
5059

5160
def run_unit_processor(self, field_data):
5261
if field_data.units:
53-
self._run_processor(self._scrub_method_name(
54-
'process_units_%s' % field_data.units), field_data)
62+
self._run_processor('process_units_%s' % field_data.units, field_data)
5563

5664
def run_message_processor(self, data_message):
57-
self._run_processor(self._scrub_method_name(
58-
'process_message_%s' % data_message.def_mesg.name), data_message)
59-
60-
def _run_processor(self, processor_name, data):
61-
try:
62-
getattr(self, processor_name)(data)
63-
except AttributeError:
64-
pass
65+
self._run_processor('process_message_%s' % data_message.def_mesg.name, data_message)
6566

6667
def process_type_bool(self, field_data):
6768
if field_data.value is not None:

0 commit comments

Comments
 (0)