Skip to content

Commit

Permalink
Merge pull request #46 from aollier/shipdict-ref
Browse files Browse the repository at this point in the history
Shipdict ref - remove duplicates
  • Loading branch information
parmentelat authored Dec 12, 2020
2 parents 355cac2 + a7f4dda commit d8f7e00
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
27 changes: 23 additions & 4 deletions data/shipdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ def __repr__(self):
only used when merger.py is run in verbose mode
"""
return f"<{self.lat_str()} {self.lon_str()} @ {self.timestamp}>"

# required to be stored in a set
# see https://docs.python.org/3/reference/datamodel.html#object.__hash__
def __hash__(self):
return hash((self.latitude, self.longitude, self.timestamp))

# a hashable shall override this special method
def __eq__(self, other):
return (self.latitude == other.latitude
and self.longitude == other.longitude
and self.timestamp == other.timestamp)
# @END@

# @BEG@ name=shipdict more=suite
Expand Down Expand Up @@ -96,9 +107,11 @@ def add_position(self, position):

def sort_positions(self):
"""
sort list of positions by chronological order
sort of positions made unique thanks to the set by chronological order
for this to work, a Position must be hashable
"""
self.positions.sort(key=lambda position: position.timestamp)
self.positions = sorted(set(self.positions),
key=lambda position: position.timestamp)
# @END@

# @BEG@ name=shipdict more=suite
Expand All @@ -117,7 +130,8 @@ def __init__(self):
def __repr__(self):
return f"<ShipDict instance with {len(self)} ships>"

def is_abbreviated(self, chunk):
@staticmethod
def is_abbreviated(chunk):
"""
depending on the size of the incoming data chunk,
guess if it is an abbreviated or extended data
Expand Down Expand Up @@ -159,7 +173,12 @@ def add_chunk(self, chunk):
based on the result of is_abbreviated(),
gets sent to add_extended or add_abbreviated
"""
if self.is_abbreviated(chunk):
# here we retrieve the static method through the class
# this form outlines the fact that we're calling a static method
# note that
# self.is_abbreviated(chunk)
# would work fine just as well
if ShipDict.is_abbreviated(chunk):
self.add_abbreviated(chunk)
else:
self.add_extended(chunk)
Expand Down
Binary file modified data/ships-ref.tar
Binary file not shown.
Binary file modified data/ships-ref.tgz
Binary file not shown.
Binary file modified data/ships-ref.zip
Binary file not shown.
7 changes: 3 additions & 4 deletions w6/w6-s8-x1-classes-merger.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,9 @@ Comparing ALL_SHIPS.kml and ALL_SHIPS.kml.ref -> OK
À noter que dans le mode bavard toutes les positions sont listées dans le résumé au format texte, ce qui le rend beaucoup plus bavard comme vous pouvez le voir en inspectant la taille des deux fichiers de référence :

```python
$ ls -l ALL_SHIPS*txt.ref v2.0
-rw-r--r-- 1 parmentelat staff 1438681 Dec 4 16:20 ALL_SHIPS-v.txt.ref
-rw-r--r-- 1 parmentelat staff 15331 Dec 4 16:20 ALL_SHIPS.txt.ref
-rw-r--r-- 1 parmentelat staff 0 Dec 4 16:21 v2.0
$ ls -l ALL_SHIPS*txt.ref
-rw-r--r-- 1 parmentelat staff 1433373 Dec 4 16:20 ALL_SHIPS-v.txt.ref
-rw-r--r-- 1 parmentelat staff 15273 Dec 4 16:20 ALL_SHIPS.txt.ref
```

+++
Expand Down

0 comments on commit d8f7e00

Please sign in to comment.