Use cached_property instead of memoize+property
MODIFY
plextraktsync/path.py
plextraktsync/path.py
from os import getenv, makedirs
from os.path import abspath, dirname, exists, join
-from plextraktsync.decorators.memoize import memoize
+from plextraktsync.decorators.cached_property import cached_property
class Path:
@@ -21,33 +21,28 @@ def __init__(self):
self.env_file = join(self.config_dir, ".env")
self.log_file = join(self.log_dir, "last_update.log")
- @property
- @memoize
+ @cached_property
def config_dir(self):
d = self.app_dir.user_config_dir if self.installed else self.app_path
return getenv("PTS_CONFIG_DIR", d)
- @property
- @memoize
+ @cached_property
def cache_dir(self):
d = self.app_dir.user_cache_dir if self.installed else self.app_path
return getenv("PTS_CACHE_DIR", d)
- @property
- @memoize
+ @cached_property
def log_dir(self):
d = self.app_dir.user_log_dir if self.installed else self.app_path
return getenv("PTS_LOG_DIR", d)
- @property
- @memoize
+ @cached_property
def app_dir(self):
from appdirs import AppDirs
return AppDirs(self.app_name)
- @property
- @memoize
+ @cached_property
def installed(self):
"""
Return true if this package is installed to site-packages

MODIFY
plextraktsync/plex_api.py
plextraktsync/plex_api.py
from plexapi.video import Episode, Movie, Show
from trakt.utils import timestamp
+from plextraktsync.decorators.cached_property import cached_property
from plextraktsync.decorators.flatten import flatten_dict, flatten_list
from plextraktsync.decorators.memoize import memoize
from plextraktsync.decorators.nocache import nocache
@@ -52,13 +53,11 @@ def __init__(self, guid: str, type: str, pm: Optional[PlexLibraryItem] = None):
self.type = type
self.pm = pm
- @property
- @memoize
+ @cached_property
def media_type(self):
return f"{self.type}s"
- @property
- @memoize
+ @cached_property
def provider(self):
if self.guid_is_imdb_legacy:
return "imdb"
@@ -76,8 +75,7 @@ def provider(self):
return x
- @property
- @memoize
+ @cached_property
def id(self):
if self.guid_is_imdb_legacy:
return self.guid
@@ -85,8 +83,7 @@ def id(self):
x = x.split("?")[0]
return x
- @property
- @memoize
+ @cached_property
def is_episode(self):
"""
Return true of the id is in form of <show>/<season>/<episode>
@@ -97,8 +94,7 @@ def is_episode(self):
return False
- @property
- @memoize
+ @cached_property
def show_id(self):
if not self.is_episode:
raise ValueError("show_id is not valid for non-episodes")
@@ -109,8 +105,7 @@ def show_id(self):
return show
- @property
- @memoize
+ @cached_property
def guid_is_imdb_legacy(self):
guid = self.guid
@@ -154,8 +149,7 @@ def is_legacy_agent(self):
def get_guids(self):
return self.item.guids
- @property
- @memoize
+ @cached_property
def guids(self):
# return early if legacy agent
# accessing .guids for legacy agent
@@ -178,18 +172,15 @@ def guids(self):
ordered = sorted(guids, key=lambda guid: sort_order[guid.provider])
return ordered
- @property
- @memoize
+ @cached_property
def media_type(self):
return f"{self.type}s"
- @property
- @memoize
+ @cached_property
def type(self):
return self.item.type
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit(retries=1)
def rating(self):
@@ -351,13 +342,11 @@ def episodes(self):
def _get_episodes(self):
return self.item.episodes()
- @property
- @memoize
+ @cached_property
def season_number(self):
return self.item.seasonNumber
- @property
- @memoize
+ @cached_property
def episode_number(self):
return self.item.index
@@ -465,8 +454,7 @@ class PlexApi:
def __init__(self, plex: PlexServer):
self.plex = plex
- @property
- @memoize
+ @cached_property
def plex_base_url(self):
return f"https://app.plex.tv/desktop/#!/server/{self.plex.machineIdentifier}"
@@ -510,20 +498,17 @@ def search(self, title: str, **kwargs):
for media in result:
yield PlexLibraryItem(media)
- @property
- @memoize
+ @cached_property
@nocache
def version(self):
return self.plex.version
- @property
- @memoize
+ @cached_property
@nocache
def updated_at(self):
return self.plex.updatedAt
- @property
- @memoize
+ @cached_property
@nocache
@flatten_dict
def library_sections(self) -> Dict[int, PlexLibrarySection]:
@@ -547,8 +532,7 @@ def system_device(self, device_id: int) -> SystemDevice:
def system_account(self, account_id: int) -> SystemAccount:
return self.plex.systemAccount(account_id)
- @property
- @memoize
+ @cached_property
def ratings(self):
return PlexRatingCollection(self)

MODIFY
plextraktsync/sync.py
plextraktsync/sync.py
from plextraktsync.config import Config
+from plextraktsync.decorators.cached_property import cached_property
from plextraktsync.decorators.measure_time import measure_time
-from plextraktsync.decorators.memoize import memoize
from plextraktsync.logging import logger
from plextraktsync.media import Media
from plextraktsync.trakt_list_util import TraktListUtil
@@ -17,8 +17,7 @@ def __getitem__(self, key):
def __contains__(self, key):
return key in self.config
- @property
- @memoize
+ @cached_property
def trakt_to_plex(self):
return {
"watched_status": self.get("trakt_to_plex", "watched_status"),
@@ -27,8 +26,7 @@ def trakt_to_plex(self):
"watchlist": self.get("trakt_to_plex", "watchlist"),
}
- @property
- @memoize
+ @cached_property
def plex_to_trakt(self):
return {
"watched_status": self.get("plex_to_trakt", "watched_status"),
@@ -36,13 +34,11 @@ def plex_to_trakt(self):
"collection": self.get("plex_to_trakt", "collection"),
}
- @property
- @memoize
+ @cached_property
def sync_ratings(self):
return self.trakt_to_plex["ratings"] or self.plex_to_trakt["ratings"]
- @property
- @memoize
+ @cached_property
def sync_watched_status(self):
return self.trakt_to_plex["watched_status"] or self.plex_to_trakt["watched_status"]

MODIFY
plextraktsync/trakt_api.py
plextraktsync/trakt_api.py
from trakt.tv import TVEpisode, TVSeason, TVShow
from plextraktsync import pytrakt_extensions
+from plextraktsync.decorators.cached_property import cached_property
from plextraktsync.decorators.memoize import memoize
from plextraktsync.decorators.nocache import nocache
from plextraktsync.decorators.rate_limit import rate_limit
@@ -69,8 +70,7 @@ def device_auth(client_id: str, client_secret: str):
return trakt.init(client_id=client_id, client_secret=client_secret, store=True)
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def me(self):
@@ -80,15 +80,13 @@ def me(self):
logger.fatal("Trakt authentication error: {}".format(str(e)))
raise e
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def liked_lists(self):
return pytrakt_extensions.get_liked_lists()
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def watched_movies(self):
@@ -96,15 +94,13 @@ def watched_movies(self):
map(lambda m: m.trakt, self.me.watched_movies)
)
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def movie_collection(self):
return self.me.movie_collection
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def show_collection(self):
@@ -118,29 +114,25 @@ def remove_from_library(self, media: Union[Movie, TVShow, TVSeason, TVEpisode]):
raise ValueError("Must be valid media type")
media.remove_from_library()
- @property
- @memoize
+ @cached_property
def movie_collection_set(self):
return set(
map(lambda m: m.trakt, self.movie_collection)
)
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def watched_shows(self):
return pytrakt_extensions.allwatched()
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def watchlist_movies(self):
return self.me.watchlist_movies
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def movie_ratings(self):
@@ -149,8 +141,7 @@ def movie_ratings(self):
ratings[r['movie']['ids']['trakt']] = r['rating']
return ratings
- @property
- @memoize
+ @cached_property
@nocache
@rate_limit()
def episode_ratings(self):

MODIFY
plextraktsync/walker.py
plextraktsync/walker.py
from plexapi.video import Episode, Movie, Show
+from plextraktsync.decorators.cached_property import cached_property
from plextraktsync.decorators.measure_time import measure_time
-from plextraktsync.decorators.memoize import memoize
from plextraktsync.media import Media, MediaFactory
from plextraktsync.plex_api import (PlexApi, PlexGuid, PlexLibraryItem,
PlexLibrarySection)
@@ -186,8 +186,7 @@ def __init__(self, plex: PlexApi, trakt: TraktApi, mf: MediaFactory, config: Wal
self.mf = mf
self.config = config
- @property
- @memoize
+ @cached_property
def plan(self):
return WalkPlanner(self.plex, self.config).plan()

Use cached_property instead of memoize+property