FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonVSCode/pythonFiles/preview/jedi/evaluate/cache.py at master · MightyWing/pythonVSCode · GitHub
MightyWing
/
pythonVSCode
Public
forked from
DonJayamanne/pythonVSCode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonVSCode
/
pythonFiles
/
preview
/
jedi
/
evaluate
/
cache.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
58 lines (49 loc) · 1.99 KB
Breadcrumbs
pythonVSCode
/
pythonFiles
/
preview
/
jedi
/
evaluate
/
cache.py
Copy path
File metadata and controls
58 lines (49 loc) · 1.99 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
- the popular ``memoize_default`` works like a typical memoize and returns the
default otherwise.
- ``CachedMetaClass`` uses ``memoize_default`` to do the same with classes.
"""
import
inspect
NO_DEFAULT
=
object
()
def
memoize_default
(
default
=
NO_DEFAULT
,
evaluator_is_first_arg
=
False
,
second_arg_is_evaluator
=
False
):
""" This is a typical memoization decorator, BUT there is one difference:
To prevent recursion it sets defaults.
Preventing recursion is in this case the much bigger use than speed. I
don't think, that there is a big speed difference, but there are many cases
where recursion could happen (think about a = b; b = a).
"""
def
func
(
function
):
def
wrapper
(
obj
,
*
args
,
**
kwargs
):
if
evaluator_is_first_arg
:
cache
=
obj
.
memoize_cache
elif
second_arg_is_evaluator
:
# needed for meta classes
cache
=
args
[
0
].
memoize_cache
else
:
cache
=
obj
.
_evaluator
.
memoize_cache
try
:
memo
=
cache
[
function
]
except
KeyError
:
memo
=
{}
cache
[
function
]
=
memo
key
=
(
obj
,
args
,
frozenset
(
kwargs
.
items
()))
if
key
in
memo
:
return
memo
[
key
]
else
:
if
default
is
not
NO_DEFAULT
:
memo
[
key
]
=
default
rv
=
function
(
obj
,
*
args
,
**
kwargs
)
if
inspect
.
isgenerator
(
rv
):
rv
=
list
(
rv
)
memo
[
key
]
=
rv
return
rv
return
wrapper
return
func
class
CachedMetaClass
(
type
):
"""
This is basically almost the same than the decorator above, it just caches
class initializations. Either you do it this way or with decorators, but
with decorators you lose class access (isinstance, etc).
"""
@
memoize_default
(
None
,
second_arg_is_evaluator
=
True
)
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
super
(
CachedMetaClass
,
self
).
__call__
(
*
args
,
**
kwargs
)
Back
|
FazBrowse Home
|
New Git URL