linkchecker/linkcheck/containers.py

105 lines
3.4 KiB
Python
Raw Normal View History

# Copyright (C) 2004-2014 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2009-07-24 21:58:20 +00:00
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Special container classes.
"""
2020-05-30 16:01:36 +00:00
class LFUCache(dict):
2010-03-09 23:00:12 +00:00
"""Limited cache which purges least frequently used items."""
def __init__(self, size=1000):
2011-02-17 18:59:02 +00:00
"""Initialize internal LFU cache."""
2020-06-03 19:06:36 +00:00
super().__init__()
2010-03-09 23:00:12 +00:00
if size < 1:
raise ValueError("invalid cache size %d" % size)
self.size = size
def __setitem__(self, key, val):
2010-03-09 23:00:12 +00:00
"""Store given key/value."""
if key in self:
# store value, do not increase number of uses
2020-06-03 19:06:36 +00:00
super().__getitem__(key)[1] = val
2010-03-09 23:00:12 +00:00
else:
2020-06-03 19:06:36 +00:00
super().__setitem__(key, [0, val])
2010-03-09 23:00:12 +00:00
# check for size limit
if len(self) > self.size:
self.shrink()
def shrink(self):
2010-03-09 23:00:12 +00:00
"""Shrink ca. 5% of entries."""
2020-05-30 16:01:36 +00:00
trim = int(0.05 * len(self))
2010-03-09 23:00:12 +00:00
if trim:
2020-06-03 19:06:36 +00:00
items = super().items()
# sorting function for items
Fix remaining flake8 violations in linkcheck/ linkcheck/better_exchook2.py:28:89: E501 line too long (90 > 88 characters) linkcheck/better_exchook2.py:155:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:166:9: E722 do not use bare 'except' linkcheck/better_exchook2.py:289:13: E741 ambiguous variable name 'l' linkcheck/better_exchook2.py:299:9: E722 do not use bare 'except' linkcheck/containers.py:48:13: E731 do not assign a lambda expression, use a def linkcheck/ftpparse.py:123:89: E501 line too long (93 > 88 characters) linkcheck/loader.py:46:47: E203 whitespace before ':' linkcheck/logconf.py:45:29: E231 missing whitespace after ',' linkcheck/robotparser2.py:157:89: E501 line too long (95 > 88 characters) linkcheck/robotparser2.py:182:89: E501 line too long (89 > 88 characters) linkcheck/strformat.py:181:16: E203 whitespace before ':' linkcheck/strformat.py:181:43: E203 whitespace before ':' linkcheck/strformat.py:253:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:254:9: E731 do not assign a lambda expression, use a def linkcheck/strformat.py:341:89: E501 line too long (111 > 88 characters) linkcheck/url.py:102:32: E203 whitespace before ':' linkcheck/url.py:277:5: E741 ambiguous variable name 'l' linkcheck/url.py:402:5: E741 ambiguous variable name 'l' linkcheck/checker/__init__.py:203:1: E402 module level import not at top of file linkcheck/checker/fileurl.py:200:89: E501 line too long (103 > 88 characters) linkcheck/checker/mailtourl.py:122:60: E203 whitespace before ':' linkcheck/checker/mailtourl.py:157:89: E501 line too long (96 > 88 characters) linkcheck/checker/mailtourl.py:190:89: E501 line too long (109 > 88 characters) linkcheck/checker/mailtourl.py:200:89: E501 line too long (111 > 88 characters) linkcheck/checker/mailtourl.py:249:89: E501 line too long (106 > 88 characters) linkcheck/checker/unknownurl.py:226:23: W291 trailing whitespace linkcheck/checker/urlbase.py:245:89: E501 line too long (101 > 88 characters) linkcheck/configuration/confparse.py:236:89: E501 line too long (186 > 88 characters) linkcheck/configuration/confparse.py:247:89: E501 line too long (111 > 88 characters) linkcheck/configuration/__init__.py:164:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:184:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:190:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:195:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:198:9: E266 too many leading '#' for block comment linkcheck/configuration/__init__.py:435:89: E501 line too long (90 > 88 characters) linkcheck/director/aggregator.py:45:43: E231 missing whitespace after ',' linkcheck/director/aggregator.py:178:89: E501 line too long (106 > 88 characters) linkcheck/logger/__init__.py:29:1: E731 do not assign a lambda expression, use a def linkcheck/logger/__init__.py:108:13: E741 ambiguous variable name 'l' linkcheck/logger/__init__.py:275:19: F821 undefined name '_' linkcheck/logger/__init__.py:342:16: F821 undefined name '_' linkcheck/logger/__init__.py:380:13: F821 undefined name '_' linkcheck/logger/__init__.py:384:13: F821 undefined name '_' linkcheck/logger/__init__.py:387:13: F821 undefined name '_' linkcheck/logger/__init__.py:396:13: F821 undefined name '_' linkcheck/network/__init__.py:1:1: W391 blank line at end of file linkcheck/plugins/locationinfo.py:89:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/locationinfo.py:91:9: E731 do not assign a lambda expression, use a def linkcheck/plugins/markdowncheck.py:112:89: E501 line too long (111 > 88 characters) linkcheck/plugins/markdowncheck.py:141:9: E741 ambiguous variable name 'l' linkcheck/plugins/markdowncheck.py:165:23: E203 whitespace before ':' linkcheck/plugins/viruscheck.py:95:42: E203 whitespace before ':'
2020-05-30 16:01:36 +00:00
def keyfunc(x): return x[1][0]
values = sorted(items, key=keyfunc)
2011-12-17 18:13:54 +00:00
for item in values[0:trim]:
del self[item[0]]
2010-03-09 23:00:12 +00:00
def __getitem__(self, key):
2011-02-17 18:59:02 +00:00
"""Update key usage and return value."""
2020-06-03 19:06:36 +00:00
entry = super().__getitem__(key)
entry[0] += 1
return entry[1]
2010-03-09 23:00:12 +00:00
def uses(self, key):
2010-03-09 23:00:12 +00:00
"""Get number of uses for given key (without increasing the number of
uses)"""
2020-06-03 19:06:36 +00:00
return super().__getitem__(key)[0]
2010-03-09 23:00:12 +00:00
def get(self, key, def_val=None):
2011-02-17 18:59:02 +00:00
"""Update key usage if found and return value, else return default."""
2010-03-09 23:00:12 +00:00
if key in self:
return self[key]
return def_val
def setdefault(self, key, def_val=None):
2011-02-17 18:59:02 +00:00
"""Update key usage if found and return value, else set and return
default."""
2010-03-09 23:00:12 +00:00
if key in self:
return self[key]
self[key] = def_val
return def_val
def items(self):
2011-02-17 18:59:02 +00:00
"""Return list of items, not updating usage count."""
2020-06-03 19:06:36 +00:00
return [(key, value[1]) for key, value in super().items()]
2010-03-09 23:00:12 +00:00
def iteritems(self):
2011-02-17 18:59:02 +00:00
"""Return iterator of items, not updating usage count."""
2020-06-03 19:06:36 +00:00
for key, value in super().items():
2010-03-09 23:00:12 +00:00
yield (key, value[1])
def values(self):
2011-02-17 18:59:02 +00:00
"""Return list of values, not updating usage count."""
2020-06-03 19:06:36 +00:00
return [value[1] for value in super().values()]
2010-03-09 23:00:12 +00:00
def itervalues(self):
2011-02-17 18:59:02 +00:00
"""Return iterator of values, not updating usage count."""
2020-06-03 19:06:36 +00:00
for value in super().values():
2010-03-09 23:00:12 +00:00
yield value[1]
def popitem(self):
2011-02-17 18:59:02 +00:00
"""Remove and return an item."""
2020-06-03 19:06:36 +00:00
key, value = super().popitem()
2010-03-09 23:00:12 +00:00
return (key, value[1])
def pop(self):
2011-02-17 18:59:02 +00:00
"""Remove and return a value."""
2020-06-03 19:06:36 +00:00
value = super().pop()
2010-03-09 23:00:12 +00:00
return value[1]