Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
0f56e90f
Kaydet (Commit)
0f56e90f
authored
Agu 14, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Support cache sizes.
üst
a8939274
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
1 deletion
+49
-1
functools.py
Lib/functools.py
+1
-1
test_functools.py
Lib/test/test_functools.py
+48
-0
No files found.
Lib/functools.py
Dosyayı görüntüle @
0f56e90f
...
@@ -144,7 +144,7 @@ def lfu_cache(maxsize=100):
...
@@ -144,7 +144,7 @@ def lfu_cache(maxsize=100):
wrapper
.
misses
+=
1
wrapper
.
misses
+=
1
if
len
(
cache
)
>
maxsize
:
if
len
(
cache
)
>
maxsize
:
# purge the 10% least frequently used entries
# purge the 10% least frequently used entries
for
key
,
_
in
nsmallest
(
maxsize
//
10
,
for
key
,
_
in
nsmallest
(
maxsize
//
10
or
1
,
use_count
.
items
(),
use_count
.
items
(),
key
=
itemgetter
(
1
)):
key
=
itemgetter
(
1
)):
del
cache
[
key
],
use_count
[
key
]
del
cache
[
key
],
use_count
[
key
]
...
...
Lib/test/test_functools.py
Dosyayı görüntüle @
0f56e90f
...
@@ -482,6 +482,30 @@ class TestLRU(unittest.TestCase):
...
@@ -482,6 +482,30 @@ class TestLRU(unittest.TestCase):
self
.
assertEqual
(
f
.
hits
,
0
)
self
.
assertEqual
(
f
.
hits
,
0
)
self
.
assertEqual
(
f
.
misses
,
1
)
self
.
assertEqual
(
f
.
misses
,
1
)
# test size zero (which means "never-cache")
f_cnt
=
0
@functools.lru_cache
(
0
)
def
f
():
nonlocal
f_cnt
f_cnt
+=
1
return
20
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f_cnt
,
3
)
# test size one
f_cnt
=
0
@functools.lru_cache
(
1
)
def
f
():
nonlocal
f_cnt
f_cnt
+=
1
return
20
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f_cnt
,
1
)
def
test_lfu
(
self
):
def
test_lfu
(
self
):
def
orig
(
x
,
y
):
def
orig
(
x
,
y
):
return
3
*
x
+
y
return
3
*
x
+
y
...
@@ -503,6 +527,30 @@ class TestLRU(unittest.TestCase):
...
@@ -503,6 +527,30 @@ class TestLRU(unittest.TestCase):
self
.
assertEqual
(
f
.
hits
,
0
)
self
.
assertEqual
(
f
.
hits
,
0
)
self
.
assertEqual
(
f
.
misses
,
1
)
self
.
assertEqual
(
f
.
misses
,
1
)
# test size zero (which means "never-cache")
f_cnt
=
0
@functools.lfu_cache
(
0
)
def
f
():
nonlocal
f_cnt
f_cnt
+=
1
return
20
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f_cnt
,
3
)
# test size one
f_cnt
=
0
@functools.lfu_cache
(
1
)
def
f
():
nonlocal
f_cnt
f_cnt
+=
1
return
20
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f
(),
20
)
self
.
assertEqual
(
f_cnt
,
1
)
def
test_main
(
verbose
=
None
):
def
test_main
(
verbose
=
None
):
test_classes
=
(
test_classes
=
(
TestPartial
,
TestPartial
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment