8000 Add support for hashing dates in pn.cache · holoviz/panel@5d39268 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d39268

Browse files
committed
Add support for hashing dates in pn.cache
1 parent 9a5d7e2 commit 5d39268

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

panel/io/cache.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Implements memoization for functions with arbitrary arguments
33
"""
4+
import datetime as dt
45
import functools
56
import hashlib
67
import inspect
@@ -134,6 +135,7 @@ def _io_hash(obj):
134135
functools.partial : _partial_hash,
135136
unittest.mock.Mock : lambda obj: _int_to_bytes(id(obj)),
136137
(io.StringIO, io.BytesIO): _io_hash,
138+
dt.date : lambda obj: f'{type(obj).__name__}{obj}'.encode('utf-8'),
137139
# Fully qualified type strings
138140
'numpy.ndarray' : _numpy_hash,
139141
'pandas.core.series.Series' : _pandas_hash,

panel/tests/io/test_cache.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime as dt
12
import io
23
import pathlib
34
import time
@@ -41,6 +42,14 @@ def test_bytes_hash():
4142
assert hashes_equal(b'0', b'0')
4243
assert not hashes_equal(b'0', b'1')
4344

45+
def test_date_hash():
46+
assert hashes_equal(dt.date(1988, 4, 14), dt.date(1988, 4, 14))
47+
assert not hashes_equal(dt.date(1988, 4, 14), dt.date(1988, 4, 15))
48+
49+
def test_datetime_hash():
50+
assert hashes_equal(dt.datetime(1988, 4, 14, 12, 3, 2, 1), dt.datetime(1988, 4, 14, 12, 3, 2, 1))
51+
assert not hashes_equal(dt.datetime(1988, 4, 14, 12, 3, 2, 1), dt.datetime(1988, 4, 14, 12, 3, 2, 2))
52+
4453
def test_list_hash():
4554
assert hashes_equal([0], [0])
4655
assert hashes_equal(['a', ['b']], ['a', ['b']])

0 commit comments

Comments
 (0)
0