Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
K
kwant
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joseph Weston
kwant
Commits
ba8b6ea8
Commit
ba8b6ea8
authored
5 years ago
by
Joseph Weston
Browse files
Options
Downloads
Patches
Plain Diff
move 'memoize' decorator to kwant/common.py
this decorator will be needed in kwant/builder.py in the next commit.
parent
a3548f5f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
kwant/_common.py
+25
-0
25 additions, 0 deletions
kwant/_common.py
kwant/wraparound.py
+5
-29
5 additions, 29 deletions
kwant/wraparound.py
with
30 additions
and
29 deletions
kwant/_common.py
+
25
−
0
View file @
ba8b6ea8
...
@@ -13,6 +13,7 @@ import inspect
...
@@ -13,6 +13,7 @@ import inspect
import
warnings
import
warnings
import
importlib
import
importlib
import
functools
import
functools
import
collections
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
__all__
=
[
'
KwantDeprecationWarning
'
,
'
UserCodeError
'
]
__all__
=
[
'
KwantDeprecationWarning
'
,
'
UserCodeError
'
]
...
@@ -191,3 +192,27 @@ class lazy_import:
...
@@ -191,3 +192,27 @@ class lazy_import:
package
=
sys
.
modules
[
self
.
__package
]
package
=
sys
.
modules
[
self
.
__package
]
setattr
(
package
,
self
.
__module
,
mod
)
setattr
(
package
,
self
.
__module
,
mod
)
return
getattr
(
mod
,
name
)
return
getattr
(
mod
,
name
)
def
_hashable
(
obj
):
return
isinstance
(
obj
,
collections
.
abc
.
Hashable
)
def
memoize
(
f
):
"""
Decorator to memoize a function that works even with unhashable args.
This decorator will even work with functions whose args are not hashable.
The cache key is made up by the hashable arguments and the ids of the
non-hashable args. It is up to the user to make sure that non-hashable
args do not change during the lifetime of the decorator.
This decorator will keep reevaluating functions that return None.
"""
def
lookup
(
*
args
):
key
=
tuple
(
arg
if
_hashable
(
arg
)
else
id
(
arg
)
for
arg
in
args
)
result
=
cache
.
get
(
key
)
if
result
is
None
:
cache
[
key
]
=
result
=
f
(
*
args
)
return
result
cache
=
{}
return
lookup
This diff is collapsed.
Click to expand it.
kwant/wraparound.py
+
5
−
29
View file @
ba8b6ea8
...
@@ -20,36 +20,12 @@ from . import builder, system, plotter
...
@@ -20,36 +20,12 @@ from . import builder, system, plotter
from
.linalg
import
lll
from
.linalg
import
lll
from
.builder
import
herm_conj
,
HermConjOfFunc
from
.builder
import
herm_conj
,
HermConjOfFunc
from
.lattice
import
TranslationalSymmetry
from
.lattice
import
TranslationalSymmetry
from
._common
import
get_parameters
from
._common
import
get_parameters
,
memoize
__all__
=
[
'
wraparound
'
,
'
plot_2d_bands
'
]
__all__
=
[
'
wraparound
'
,
'
plot_2d_bands
'
]
def
_hashable
(
obj
):
return
isinstance
(
obj
,
collections
.
abc
.
Hashable
)
def
_memoize
(
f
):
"""
Decorator to memoize a function that works even with unhashable args.
This decorator will even work with functions whose args are not hashable.
The cache key is made up by the hashable arguments and the ids of the
non-hashable args. It is up to the user to make sure that non-hashable
args do not change during the lifetime of the decorator.
This decorator will keep reevaluating functions that return None.
"""
def
lookup
(
*
args
):
key
=
tuple
(
arg
if
_hashable
(
arg
)
else
id
(
arg
)
for
arg
in
args
)
result
=
cache
.
get
(
key
)
if
result
is
None
:
cache
[
key
]
=
result
=
f
(
*
args
)
return
result
cache
=
{}
return
lookup
def
_set_signature
(
func
,
params
):
def
_set_signature
(
func
,
params
):
"""
Set the signature of
'
func
'
.
"""
Set the signature of
'
func
'
.
...
@@ -103,7 +79,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
...
@@ -103,7 +79,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
format. It will be deprecated in the 2.0 release of Kwant.
format. It will be deprecated in the 2.0 release of Kwant.
"""
"""
@
_
memoize
@memoize
def
bind_site
(
val
):
def
bind_site
(
val
):
def
f
(
*
args
):
def
f
(
*
args
):
a
,
*
args
=
args
a
,
*
args
=
args
...
@@ -113,7 +89,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
...
@@ -113,7 +89,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
_set_signature
(
f
,
get_parameters
(
val
)
+
momenta
)
_set_signature
(
f
,
get_parameters
(
val
)
+
momenta
)
return
f
return
f
@
_
memoize
@memoize
def
bind_hopping_as_site
(
elem
,
val
):
def
bind_hopping_as_site
(
elem
,
val
):
def
f
(
*
args
):
def
f
(
*
args
):
a
,
*
args
=
args
a
,
*
args
=
args
...
@@ -128,7 +104,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
...
@@ -128,7 +104,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
_set_signature
(
f
,
params
+
momenta
)
_set_signature
(
f
,
params
+
momenta
)
return
f
return
f
@
_
memoize
@memoize
def
bind_hopping
(
elem
,
val
):
def
bind_hopping
(
elem
,
val
):
def
f
(
*
args
):
def
f
(
*
args
):
a
,
b
,
*
args
=
args
a
,
b
,
*
args
=
args
...
@@ -142,7 +118,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
...
@@ -142,7 +118,7 @@ def wraparound(builder, keep=None, *, coordinate_names='xyz'):
_set_signature
(
f
,
params
+
momenta
)
_set_signature
(
f
,
params
+
momenta
)
return
f
return
f
@
_
memoize
@memoize
def
bind_sum
(
num_sites
,
*
vals
):
def
bind_sum
(
num_sites
,
*
vals
):
"""
Construct joint signature for all
'
vals
'
.
"""
"""
Construct joint signature for all
'
vals
'
.
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment