From ad58b03f2d31daf33849b141570dd0fb5e0a262e Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sat, 21 Mar 2020 17:38:30 -0700 Subject: [PATCH] Added __str__ to place --- osxphotos/_version.py | 2 +- osxphotos/placeinfo.py | 24 +++++++++++++++++++++++- tests/test_places_catalina_10_15_1.py | 13 +++++++++++++ tests/test_places_mojave_10_14_6.py | 12 ++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/osxphotos/_version.py b/osxphotos/_version.py index 07890667..f7a645ba 100644 --- a/osxphotos/_version.py +++ b/osxphotos/_version.py @@ -1,3 +1,3 @@ """ version info """ -__version__ = "0.23.1" +__version__ = "0.23.2" diff --git a/osxphotos/placeinfo.py b/osxphotos/placeinfo.py index 4bf16e56..0749aa48 100644 --- a/osxphotos/placeinfo.py +++ b/osxphotos/placeinfo.py @@ -3,8 +3,9 @@ Provides reverse geolocation info for photos """ from abc import ABC, abstractmethod -from collections import namedtuple +from collections import namedtuple # pylint: disable=syntax-error +import yaml from bpylist import archiver # postal address information, returned by PlaceInfo.address @@ -362,6 +363,15 @@ class PlaceInfo4(PlaceInfo): def __ne__(self, other): return not self.__eq__(other) + def __str__(self): + info = { + "name": self.name, + "names": self.names, + "country_code": self.country_code, + } + strval = "PlaceInfo(" + ", ".join([f"{k}='{v}'" for k, v in info.items()]) + ")" + return strval + class PlaceInfo5(PlaceInfo): """ Reverse geolocation place info for a photo (Photos >= 5) """ @@ -431,3 +441,15 @@ class PlaceInfo5(PlaceInfo): def __ne__(self, other): return not self.__eq__(other) + + def __str__(self): + info = { + "name": self.name, + "names": self.names, + "country_code": self.country_code, + "ishome": self.ishome, + "address_str": self.address_str, + "address": str(self.address), + } + strval = "PlaceInfo(" + ", ".join([f"{k}='{v}'" for k, v in info.items()]) + ")" + return strval diff --git a/tests/test_places_catalina_10_15_1.py b/tests/test_places_catalina_10_15_1.py index c8b879c9..a387bb0f 100644 --- a/tests/test_places_catalina_10_15_1.py +++ b/tests/test_places_catalina_10_15_1.py @@ -92,3 +92,16 @@ def test_place_no_place_info(): photo = photosdb.photos(uuid=[UUID_DICT["no_place"]])[0] assert photo.place is None + + +def test_place_str(): + # test __str__ + import osxphotos + + photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB) + photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0] + + assert ( + str(photo.place) + == "PlaceInfo(name='2038 18th St NW', names='['2038 18th St NW', 'Adams Morgan', 'Washington', 'Washington', 'Washington', 'District of Columbia', 'United States']', country_code='US', ishome='False', address_str='2038 18th St NW, Washington, DC 20009, United States', address='PostalAddress(street='2038 18th St NW', sub_locality='Adams Morgan', city='Washington', sub_administrative_area=None, state='DC', postal_code='20009', country='United States', iso_country_code='US')')" + ) diff --git a/tests/test_places_mojave_10_14_6.py b/tests/test_places_mojave_10_14_6.py index edcd957a..6230b936 100644 --- a/tests/test_places_mojave_10_14_6.py +++ b/tests/test_places_mojave_10_14_6.py @@ -45,3 +45,15 @@ def test_place_no_place_info(): photo = photosdb.photos(uuid=[UUID_DICT["no_place"]])[0] assert photo.place is None + + +def test_place_str(): + # test __str__ + import osxphotos + + photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB) + photo = photosdb.photos(uuid=[UUID_DICT["place_uk"]])[0] + assert ( + str(photo.place) + == "PlaceInfo(name='St James's Park', names='[\"St James's Park\", 'Westminster', 'London', 'England', 'United Kingdom']', country_code='GB')" + )