From b95c348dcbd6d3b1eabe688e3f987e3b42976e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Thu, 23 Jul 2015 10:48:26 +0200 Subject: [PATCH 1/7] Raised version number. --- cwr/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwr/__init__.py b/cwr/__init__.py index 52bf2c1..2e70e49 100644 --- a/cwr/__init__.py +++ b/cwr/__init__.py @@ -7,5 +7,5 @@ :license: MIT, see LICENSE for more details. """ -__version__ = '0.0.30' +__version__ = '0.0.31' __license__ = 'MIT' From 2288e0e91c4c9589df91787ac640ec08079593b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Thu, 23 Jul 2015 11:57:03 +0200 Subject: [PATCH 2/7] Fixed VISAN encoders and decoders --- cwr/parser/decoder/dictionary.py | 5 +---- cwr/parser/encoder/dictionary.py | 7 +------ tests/parser/dictionary/decoder/other/test_visan.py | 7 ++----- .../parser/dictionary/decoder/record/test_work_origin.py | 7 ++----- tests/parser/dictionary/encoder/other/test_visan.py | 9 ++------- .../parser/dictionary/encoder/record/test_work_origin.py | 8 ++------ 6 files changed, 10 insertions(+), 33 deletions(-) diff --git a/cwr/parser/decoder/dictionary.py b/cwr/parser/decoder/dictionary.py index 23c3ce6..e86ae12 100644 --- a/cwr/parser/decoder/dictionary.py +++ b/cwr/parser/decoder/dictionary.py @@ -942,7 +942,4 @@ def __init__(self): super(VISANDictionaryDecoder, self).__init__() def decode(self, data): - return VISAN(data['version'], - data['isan'], - data['episode'], - data['check_digit']) + return data diff --git a/cwr/parser/encoder/dictionary.py b/cwr/parser/encoder/dictionary.py index 288839d..d015616 100644 --- a/cwr/parser/encoder/dictionary.py +++ b/cwr/parser/encoder/dictionary.py @@ -259,12 +259,7 @@ def __init__(self): def encode(self, visan): encoded = {} - encoded['version'] = visan.version - encoded['isan'] = visan.isan - encoded['episode'] = visan.episode - encoded['check_digit'] = visan.check_digit - - return encoded + return visan class TransactionHeaderDictionaryEncoder(Encoder): diff --git a/tests/parser/dictionary/decoder/other/test_visan.py b/tests/parser/dictionary/decoder/other/test_visan.py index ebc4330..3b1ecb0 100644 --- a/tests/parser/dictionary/decoder/other/test_visan.py +++ b/tests/parser/dictionary/decoder/other/test_visan.py @@ -27,9 +27,6 @@ def test_encoded(self): data['episode'] = 3 data['check_digit'] = 4 - record = self._decoder.decode(data) + record = self._decoder.decode(1234) - self.assertEqual(1, record.version) - self.assertEqual(2, record.isan) - self.assertEqual(3, record.episode) - self.assertEqual(4, record.check_digit) + self.assertEqual(1234, record) diff --git a/tests/parser/dictionary/decoder/record/test_work_origin.py b/tests/parser/dictionary/decoder/record/test_work_origin.py index 9dcfbac..2dce7ca 100644 --- a/tests/parser/dictionary/decoder/record/test_work_origin.py +++ b/tests/parser/dictionary/decoder/record/test_work_origin.py @@ -32,7 +32,7 @@ def test_encoded(self): data['cut_number'] = 5 data['library'] = 'LIB467' data['bltvr'] = 'BLTVR' - data['visan'] = VISAN(1234567, 12345678912, 123, 1) + data['visan'] = 1234567123456789121231 data['production_n'] = 'PROD145' data['episode_title'] = 'EPISODE' data['episode_n'] = 'EP145' @@ -50,10 +50,7 @@ def test_encoded(self): self.assertEqual(5, record.cut_number) self.assertEqual('LIB467', record.library) self.assertEqual('BLTVR', record.bltvr) - self.assertEqual(1, record.visan.check_digit) - self.assertEqual(123, record.visan.episode) - self.assertEqual(12345678912, record.visan.isan) - self.assertEqual(1234567, record.visan.version) + self.assertEqual(1234567123456789121231, record.visan) self.assertEqual('PROD145', record.production_n) self.assertEqual('EPISODE', record.episode_title) self.assertEqual('EP145', record.episode_n) diff --git a/tests/parser/dictionary/encoder/other/test_visan.py b/tests/parser/dictionary/encoder/other/test_visan.py index 778662d..0f98cce 100644 --- a/tests/parser/dictionary/encoder/other/test_visan.py +++ b/tests/parser/dictionary/encoder/other/test_visan.py @@ -21,11 +21,6 @@ def setUp(self): self._encoder = VISANDictionaryEncoder() def test_encoded(self): - data = VISAN(1, 2, 3, 4) + encoded = self._encoder.encode(1234) - encoded = self._encoder.encode(data) - - self.assertEqual(1, encoded['version']) - self.assertEqual(2, encoded['isan']) - self.assertEqual(3, encoded['episode']) - self.assertEqual(4, encoded['check_digit']) + self.assertEqual(1234, encoded) diff --git a/tests/parser/dictionary/encoder/record/test_work_origin.py b/tests/parser/dictionary/encoder/record/test_work_origin.py index acef568..1165990 100644 --- a/tests/parser/dictionary/encoder/record/test_work_origin.py +++ b/tests/parser/dictionary/encoder/record/test_work_origin.py @@ -22,7 +22,6 @@ def setUp(self): self._encoder = WorkOriginDictionaryEncoder() def test_encoded(self): - visan = VISAN(1234567, 12345678912, 123, 1) avi = AVIKey(123, 'ABC') data = WorkOriginRecord(record_type='ORN', @@ -34,7 +33,7 @@ def test_encoded(self): cut_number=5, library='LIB467', bltvr='BLTVR', - visan=visan, + visan=1234567123456789121231, production_n='PROD145', episode_title='EPISODE', episode_n='EP145', @@ -57,10 +56,7 @@ def test_encoded(self): self.assertEqual('EP145', encoded['episode_n']) self.assertEqual(1994, encoded['year_production']) - self.assertEqual(1, encoded['visan']['check_digit']) - self.assertEqual(123, encoded['visan']['episode']) - self.assertEqual(12345678912, encoded['visan']['isan']) - self.assertEqual(1234567, encoded['visan']['version']) + self.assertEqual(1234567123456789121231, encoded['visan']) self.assertEqual(123, encoded['audio_visual_key']['society_code']) self.assertEqual('ABC', encoded['audio_visual_key']['av_number']) From d0a9e5dbf27fa8a68b2a2dbd6b6d512e9d2e7e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Thu, 23 Jul 2015 11:57:30 +0200 Subject: [PATCH 3/7] Raised version number. --- cwr/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwr/__init__.py b/cwr/__init__.py index 2e70e49..bf5c9e6 100644 --- a/cwr/__init__.py +++ b/cwr/__init__.py @@ -7,5 +7,5 @@ :license: MIT, see LICENSE for more details. """ -__version__ = '0.0.31' +__version__ = '0.0.32' __license__ = 'MIT' From 6100be7ed4df7ed3d016a0d462e00bd50d65cf4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Fri, 24 Jul 2015 10:28:55 +0200 Subject: [PATCH 4/7] Reduced the number of uses of isinstance --- cwr/grammar/factory/rule.py | 7 ++++--- cwr/grammar/field/basic.py | 7 +++++-- cwr/parser/encoder/cwrjson.py | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cwr/grammar/factory/rule.py b/cwr/grammar/factory/rule.py index 3eed134..ba6a7ea 100644 --- a/cwr/grammar/factory/rule.py +++ b/cwr/grammar/factory/rule.py @@ -191,9 +191,9 @@ def _build_terminal_rule(self, rule): rule_type = rule.rule_type # TODO: This is a patch for an error which should not be happening - if isinstance(modifiers, pp.ParseResults): + try: modifiers = modifiers.asList() - elif isinstance(modifiers, str): + except AttributeError: modifiers = [] if rule_type == 'field': @@ -212,7 +212,8 @@ def _build_terminal_rule(self, rule): return rule - def _apply_modifiers(self, rule, modifiers): + @staticmethod + def _apply_modifiers(rule, modifiers): if 'grouped' in modifiers: rule = pp.Group(rule) diff --git a/cwr/grammar/field/basic.py b/cwr/grammar/field/basic.py index 60cf09f..ee10e43 100644 --- a/cwr/grammar/field/basic.py +++ b/cwr/grammar/field/basic.py @@ -465,8 +465,11 @@ def lookup(values, name=None): raise ValueError('The values can no be None') # TODO: This should not be needed, it is just a patch. Fix this. - if isinstance(values, ParseResults): - values = values.asList() + try: + v = values.asList() + values = v + except AttributeError: + values = values # Only the specified values are allowed lookup_field = pp.oneOf(values) diff --git a/cwr/parser/encoder/cwrjson.py b/cwr/parser/encoder/cwrjson.py index 7959e1a..5926810 100644 --- a/cwr/parser/encoder/cwrjson.py +++ b/cwr/parser/encoder/cwrjson.py @@ -61,9 +61,9 @@ def _unicode_handler(obj): :param obj: object to transform into it's UTF-8 equivalent :return: the UTF-8 equivalent of the string """ - if isinstance(obj, str): + try: result = obj.isoformat() - else: + except AttributeError: raise TypeError("Unserializable object {} of type {}".format(obj, type(obj))) From 7b22c5e7eb4a247d2a5ca58dcbd39a8a2959f041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Fri, 31 Jul 2015 10:13:55 +0200 Subject: [PATCH 5/7] The JSON creation test shows times. --- .../test_default_rule_factory_stress.py | 6 +++--- tests/visual/file_json.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/grammar/factory/test_default_rule_factory_stress.py b/tests/grammar/factory/test_default_rule_factory_stress.py index ac82ab8..2ce0d13 100644 --- a/tests/grammar/factory/test_default_rule_factory_stress.py +++ b/tests/grammar/factory/test_default_rule_factory_stress.py @@ -18,18 +18,18 @@ class TestDefaultRuleFactory(unittest.TestCase): def setUp(self): self._factory = default_grammar_factory() - def test_10000(self): + def test_10(self): grammar = self._factory.get_rule('transactions') record = '' if sys.version_info[0] == 2: - for x in xrange(10): + for x in xrange(35): if len(record) == 0: record = _agreement_full() elif len(record) > 0: record = record + '\n' + _agreement_full() else: - for x in range(10): + for x in range(35): if len(record) == 0: record = _agreement_full() elif len(record) > 0: diff --git a/tests/visual/file_json.py b/tests/visual/file_json.py index 1490115..584731c 100644 --- a/tests/visual/file_json.py +++ b/tests/visual/file_json.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import codecs import os +import time from cwr.parser.decoder.file import default_file_decoder from cwr.parser.encoder.cwrjson import JSONEncoder @@ -34,11 +35,31 @@ data['filename'] = os.path.basename(path) data['contents'] = codecs.open(path, 'r', 'latin-1').read() + print('Begins parsing CWR at %s' % time.ctime()) + start = time.clock() data = decoder.decode(data) + end = time.clock() + time_parse = (end - start) + + print('Parsed the file in %s seconds' % time_parse) + print('\n') encoder = JSONEncoder() + + print('Begins creating JSON at %s' % time.ctime()) + start = time.clock() result = encoder.encode(data) + end = time.clock() + time_parse = (end - start) + print('Created the JSON in %s seconds' % time_parse) + print('\n') + + start = time.clock() output = codecs.open(output, 'w', 'latin-1') + end = time.clock() + time_parse = (end - start) + + print('Saved the JSON in %s seconds' % time_parse) output.write(result) From 0398eb8bb6114fd6fd6709906465798dc0f30b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Fri, 31 Jul 2015 10:29:19 +0200 Subject: [PATCH 6/7] Raised version number. --- cwr/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwr/__init__.py b/cwr/__init__.py index bf5c9e6..ed88061 100644 --- a/cwr/__init__.py +++ b/cwr/__init__.py @@ -7,5 +7,5 @@ :license: MIT, see LICENSE for more details. """ -__version__ = '0.0.32' +__version__ = '0.0.33' __license__ = 'MIT' From 60d7d6fe6eb3de46b07b2cdec432e8d27f065348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20Mart=C3=ADnez=20Garrido?= Date: Fri, 31 Jul 2015 10:40:40 +0200 Subject: [PATCH 7/7] Raised test time range. --- tests/grammar/factory/test_default_rule_factory_stress.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/grammar/factory/test_default_rule_factory_stress.py b/tests/grammar/factory/test_default_rule_factory_stress.py index 2ce0d13..5949475 100644 --- a/tests/grammar/factory/test_default_rule_factory_stress.py +++ b/tests/grammar/factory/test_default_rule_factory_stress.py @@ -41,7 +41,7 @@ def test_10(self): time_parse = (end - start) - self.assertTrue(time_parse < 1) + self.assertTrue(time_parse < 1.5) def _agreement_full():