Skip to content

Commit

Permalink
DEV: Update API calls to datetime functionality (#117)
Browse files Browse the repository at this point in the history
* DEV: Update API calls to datetime functionality

* DEV: Use newer APIs for Python >= 3.11

* DEV: Use datetime API without depending upon Python version

* MISC: Remove unused import

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Brigitta Sipőcz <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2024
1 parent 960ee31 commit a5efd20
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions jupyter_cache/cache/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextlib import contextmanager
from datetime import datetime
import datetime
import os
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -51,6 +51,10 @@ def get_version(path: Union[str, Path]) -> Optional[str]:
return version_file.read_text().strip()


def datetime_utcnow():
return lambda: datetime.datetime.now(datetime.timezone.utc)


@contextmanager
def session_context(engine: Engine):
"""Open a connection to the database."""
Expand Down Expand Up @@ -128,7 +132,7 @@ class NbProjectRecord(OrmBase):
"""A list of file assets required for the notebook to run."""
exec_data = Column(JSON(), nullable=True)
"""Data on how to execute the notebook."""
created = Column(DateTime, nullable=False, default=datetime.utcnow)
created = Column(DateTime, nullable=False, default=datetime_utcnow())
traceback = Column(Text(), nullable=True, default="")
"""A traceback is added if a notebook fails to execute fully."""

Expand Down Expand Up @@ -288,9 +292,9 @@ class NbCacheRecord(OrmBase):
description = Column(String(255), nullable=False, default="")
data = Column(JSON())
"""Extra data, such as the execution time."""
created = Column(DateTime, nullable=False, default=datetime.utcnow)
created = Column(DateTime, nullable=False, default=datetime_utcnow())
accessed = Column(
DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow
DateTime, nullable=False, default=datetime_utcnow(), onupdate=datetime_utcnow()
)

def __repr__(self):
Expand Down Expand Up @@ -368,7 +372,7 @@ def touch(pk, db: Engine):
record = session.query(NbCacheRecord).filter_by(pk=pk).one_or_none()
if record is None:
raise KeyError(f"Cache record not found for NB with PK: {pk}")
record.accessed = datetime.utcnow()
record.accessed = datetime_utcnow()()
session.commit()

def touch_hashkey(hashkey, db: Engine):
Expand All @@ -379,7 +383,7 @@ def touch_hashkey(hashkey, db: Engine):
)
if record is None:
raise KeyError(f"Cache record not found for NB with hashkey: {hashkey}")
record.accessed = datetime.utcnow()
record.accessed = datetime_utcnow()()
session.commit()

@staticmethod
Expand Down

0 comments on commit a5efd20

Please sign in to comment.