Commit 1592ef46 authored by abuddenberg's avatar abuddenberg
Browse files

Found yet another bug in chapter processing (during merges). Changed merge...

Found yet another bug in chapter processing (during merges). Changed merge behavior to favor incoming object structure. Refactored base class merge to not rely on __dict__
parent 9c007088
......@@ -6,7 +6,7 @@ from copy import deepcopy
import json
from dateutil.parser import parse
import re
import inspect
class Gcisbase(object):
def __init__(self, data, fields=[], trans={}):
......@@ -40,9 +40,13 @@ class Gcisbase(object):
setattr(self, k, data[k])
def merge(self, other):
for k in self.__dict__:
if self.__dict__[k] in (None, '') and hasattr(other, k):
self.__dict__[k] = getattr(other, k)
#This sucks
attrs_we_care_about = [(attr, v) for attr, v in inspect.getmembers(self, lambda a: not (inspect.isroutine(a)))
if not attr.startswith('__')]
for attr, value in attrs_we_care_about:
if value in (None, '') and hasattr(other, attr):
setattr(self, attr, value)
return self
def as_json(self, indent=0, omit_fields=[]):
......@@ -69,7 +73,7 @@ class Figure(Gcisbase):
#Special case for chapter
chap_tree = data.pop('chapter', None)
self.chapter = Chapter(chap_tree) if chap_tree else None
self.chapter = Chapter(chap_tree) if chap_tree else self.chapter
#Special case for images
image_list = data.pop('images', None)
......@@ -83,7 +87,7 @@ class Figure(Gcisbase):
if isinstance(self.chapter, Chapter) and self.chapter.number and self.ordinal:
return '{0}.{1}'.format(self.chapter.number, self.ordinal)
return self.ordinal
return '{0}.{1}'.format(self.chapter, self.ordinal)
#TODO: Ordinal handling is unnecessarily complex
......@@ -114,7 +118,21 @@ class Figure(Gcisbase):
return string
def __repr__(self):
return super(Figure, self).__repr__()
# return super(Figure, self).__repr__()
return self.__str__()
def merge(self, other):
#Special handling for Chapters
if isinstance(other.chapter, Chapter) and isinstance(self.chapter, Chapter):
#This might want to move to Chapter's merge()
elif isinstance(other.chapter, Chapter) and not isinstance(self.chapter, Chapter):
chapter_num = self.chapter
self.chapter = other.chapter
self.chapter.number = chapter_num
return super(Figure, self).merge(other)
class Chapter(Gcisbase):
__author__ = 'abuddenberg'
......@@ -55,14 +55,16 @@ def test_chapter_parsing():
gcis_fig = Figure(json.loads(test_figure_json))
assert isinstance(webform_fig.ordinal, int)
assert webform_fig.figure_num == 17
assert webform_fig.figure_num == '2.17'
assert webform_fig.ordinal == 17
assert webform_fig.chapter is None
assert webform_fig.chapter == 2
merged_figure = webform_fig.merge(gcis_fig)
#FYI, these are identical; I just wanted the variable name to reflect the merge
assert id(merged_figure) == id(webform_fig)
assert isinstance(gcis_fig.chapter, Chapter)
assert merged_figure.figure_num == '2.17'
assert merged_figure.ordinal == 17
assert isinstance(merged_figure.chapter, Chapter)
......@@ -92,4 +94,4 @@ def test_dataset_special_properties():
if __name__ == '__main__':
\ No newline at end of file
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment