-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to generate and upload batches of resources, rather than one…
… monster bundle.
- Loading branch information
Showing
4 changed files
with
73 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,39 @@ | ||
import logging | ||
|
||
|
||
class ResourceList(object): | ||
"""Holds (ordered) list of FHIR Resources""" | ||
|
||
def __init__(self, parser, adapter): | ||
self.parser = parser | ||
self.adapter = adapter | ||
self.items = None | ||
self.item_count = 0 | ||
self._iteration_complete = False | ||
|
||
def _parse(self): | ||
"""Use parser and adapter, build up list of available resources""" | ||
self.items = [] | ||
def __iter__(self): | ||
"""Use parser and adapter, yield each unique resource""" | ||
keys_seen = set() | ||
for row in self.parser.rows(): | ||
# Adapter may define unique_key() - if defined and a previous | ||
# entry matches, skip over this "duplicate" | ||
if hasattr(self.adapter, 'unique_key'): | ||
key = self.adapter(row).unique_key() | ||
if key in keys_seen: | ||
logging.info("skipping duplicate: {key}") | ||
continue | ||
keys_seen.add(key) | ||
|
||
self.items.append(self.adapter.RESOURCE_CLASS.factory(row, self.adapter)) | ||
|
||
def __iter__(self): | ||
if self.items is None: | ||
self._parse() | ||
|
||
for i in self.items: | ||
yield i | ||
self.item_count += 1 | ||
yield self.adapter.RESOURCE_CLASS.factory(row, self.adapter) | ||
self._iteration_complete = True | ||
|
||
def __len__(self): | ||
if self.items is None: | ||
self._parse() | ||
|
||
return len(self.items) if self.items else 0 | ||
"""Return length (count) of unique resources discovered in generator | ||
NB: as a generator class, the full length is only known after iteration | ||
has been exhausted. Therefore, this raises if iteration hasn't yet | ||
occurred. | ||
""" | ||
if not self._iteration_complete: | ||
raise RuntimeError("request for generator length before complete iteration") | ||
return self.item_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters