Skip to content

Commit

Permalink
Speed up PhotoFile conversion to and from dict
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkollasch committed Jun 14, 2021
1 parent 6df225f commit 499c944
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/photomanager/photofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from os.path import getsize
from datetime import datetime, tzinfo, timezone, timedelta
from dataclasses import dataclass, asdict, fields
from typing import Union, Optional, Type, TypeVar
from typing import Union, Optional, Type, TypeVar, ClassVar

from photomanager.pyexiftool import ExifTool
from photomanager.hasher import file_checksum, DEFAULT_HASH_ALGO, HashAlgorithm
Expand Down Expand Up @@ -50,10 +50,18 @@ class PhotoFile:

@property
def __dict__(self):
d = {f.name: getattr(self, f.name) for f in fields(self)}
d = {name: getattr(self, name) for name in self.field_names()}
d["chk"] = d["chk"].hex()
return d

FIELD_NAMES: ClassVar = None

@classmethod
def field_names(cls):
if cls.FIELD_NAMES is None:
cls.FIELD_NAMES = tuple(f.name for f in fields(cls))
return cls.FIELD_NAMES

@property
def local_datetime(self):
tz = timezone(timedelta(seconds=self.tzo)) if self.tzo is not None else None
Expand Down Expand Up @@ -143,7 +151,7 @@ def from_file_cached(
@classmethod
def from_json_dict(cls: Type[PF], d: dict) -> PF:
d["chk"] = bytes.fromhex(d["chk"])
return cls.from_dict(d)
return cls(**d)

@classmethod
def from_dict(cls: Type[PF], d: dict) -> PF:
Expand Down

0 comments on commit 499c944

Please sign in to comment.