FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffly/diffly/_cache.py at main · Quantco/diffly · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Quantco
/
diffly
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
45
Code
Issues
6
Pull requests
1
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
diffly
/
diffly
/
_cache.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (24 loc) · 979 Bytes
Breadcrumbs
diffly
/
diffly
/
_cache.py
Copy path
File metadata and controls
32 lines (24 loc) · 979 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Copyright (c) QuantCo 2025-2026
# SPDX-License-Identifier: BSD-3-Clause
import
functools
from
collections
.
abc
import
Callable
from
typing
import
Any
,
Concatenate
,
ParamSpec
,
TypeVar
P
=
ParamSpec
(
"P"
)
T
=
TypeVar
(
"T"
)
def
cached_method
(
fn
:
Callable
[
Concatenate
[
Any
,
P
],
T
],
)
->
Callable
[
Concatenate
[
Any
,
P
],
T
]:
"""Cache all results from the executions from an instance method."""
cache_name
=
f"_
{
fn
.
__name__
}
_cache"
kwd_mark
=
object
()
# sentinel for separating args from kwargs
@
functools
.
wraps
(
fn
)
def
wrapped
(
self
:
Any
,
*
args
:
P
.
args
,
**
kwargs
:
P
.
kwargs
)
->
T
:
key
=
args
+
(
kwd_mark
,)
+
tuple
(
sorted
(
kwargs
.
items
()))
if
not
hasattr
(
self
,
cache_name
):
setattr
(
self
,
cache_name
, {})
if
key
in
getattr
(
self
,
cache_name
):
return
getattr
(
self
,
cache_name
)[
key
]
result
=
fn
(
self
,
*
args
,
**
kwargs
)
getattr
(
self
,
cache_name
)[
key
]
=
result
return
result
return
wrapped
Back
|
FazBrowse Home
|
New Git URL