mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54:09 -04:00
Avoid parsing the undefined number sort key tweak for every undefined number
This commit is contained in:
parent
bd7dbfec43
commit
cd42a1c644
@ -6,6 +6,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
import sys
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from collections import defaultdict, Counter
|
from collections import defaultdict, Counter
|
||||||
from functools import partial
|
from functools import partial
|
||||||
@ -33,15 +34,15 @@ def sort_value_for_undefined_numbers():
|
|||||||
if t == 'maximum':
|
if t == 'maximum':
|
||||||
return float('inf')
|
return float('inf')
|
||||||
return float(t)
|
return float(t)
|
||||||
except:
|
except Exception:
|
||||||
print('***** Bad value in undefined sort number tweak', t)
|
print('***** Bad value in undefined sort number tweak', t, file=sys.stderr)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def numeric_sort_key(x):
|
def numeric_sort_key(defval, x):
|
||||||
# It isn't clear whether this function can ever be called with a non-numeric
|
# It isn't clear whether this function can ever be called with a non-numeric
|
||||||
# argument, but we check just in case
|
# argument, but we check just in case
|
||||||
return x if type(x) in (int, float) else sort_value_for_undefined_numbers()
|
return x if type(x) in (int, float) else defval
|
||||||
|
|
||||||
|
|
||||||
IDENTITY = lambda x: x
|
IDENTITY = lambda x: x
|
||||||
@ -75,7 +76,7 @@ class Field:
|
|||||||
|
|
||||||
if dt in {'int', 'float', 'rating'}:
|
if dt in {'int', 'float', 'rating'}:
|
||||||
self._default_sort_key = sort_value_for_undefined_numbers()
|
self._default_sort_key = sort_value_for_undefined_numbers()
|
||||||
self._sort_key = numeric_sort_key
|
self._sort_key = partial(numeric_sort_key, self._default_sort_key)
|
||||||
elif dt == 'bool':
|
elif dt == 'bool':
|
||||||
self._default_sort_key = None
|
self._default_sort_key = None
|
||||||
self._sort_key = bool_sort_key(bools_are_tristate)
|
self._sort_key = bool_sort_key(bools_are_tristate)
|
||||||
@ -247,6 +248,7 @@ class CompositeField(OneToOneField):
|
|||||||
composite_sort = m.get('display', {}).get('composite_sort', None)
|
composite_sort = m.get('display', {}).get('composite_sort', None)
|
||||||
if composite_sort == 'number':
|
if composite_sort == 'number':
|
||||||
self._default_sort_key = 0
|
self._default_sort_key = 0
|
||||||
|
self._undefined_number_sort_key = sort_value_for_undefined_numbers()
|
||||||
self._sort_key = self.number_sort_key
|
self._sort_key = self.number_sort_key
|
||||||
elif composite_sort == 'date':
|
elif composite_sort == 'date':
|
||||||
self._default_sort_key = UNDEFINED_DATE
|
self._default_sort_key = UNDEFINED_DATE
|
||||||
@ -277,7 +279,7 @@ class CompositeField(OneToOneField):
|
|||||||
val = val[:(-2 if p > 1 else -1)].strip()
|
val = val[:(-2 if p > 1 else -1)].strip()
|
||||||
val = atof(val) * p
|
val = atof(val) * p
|
||||||
except (TypeError, AttributeError, ValueError, KeyError):
|
except (TypeError, AttributeError, ValueError, KeyError):
|
||||||
val = sort_value_for_undefined_numbers()
|
val = self._undefined_number_sort_key
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def date_sort_key(self, val):
|
def date_sort_key(self, val):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user