www.fgks.org   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
Fix lcc:E--* queries failing
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini committed Feb 16, 2021
1 parent 8560de2 commit 305a3d3
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions openlibrary/utils/lcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,33 @@ def normalize_lcc_prefix(prefix):
:param str prefix: An LCC prefix
:return: Prefix transformed to be a prefix for sortable LCC
:rtype: str|None
>>> normalize_lcc_prefix('A123')
'A--0123'
>>> normalize_lcc_prefix('A123.')
'A--0123'
>>> normalize_lcc_prefix('A123.0')
'A--0123.0'
>>> normalize_lcc_prefix('A123.C')
'A--0123.00000000.C'
>>> normalize_lcc_prefix('A123.C0')
'A--0123.00000000.C0'
>>> normalize_lcc_prefix('E--')
'E--'
>>> normalize_lcc_prefix('PN-')
'PN-'
"""
if re.match(r'^[A-Z]+$', prefix, re.I):
return prefix
else:
# A123* should be normalized to A--0123*
# A123.* should be normalized to A--0123.*
# A123.C* should be normalized to A--0123.00000000.C*
lcc_norm = short_lcc_to_sortable_lcc(prefix.rstrip('.'))
if lcc_norm:
result = lcc_norm.rstrip('0')
if '.' in prefix and prefix.endswith('0'):
zeros_to_add = len(prefix) - len(prefix.rstrip('0'))
result += '0' * zeros_to_add
elif result.endswith('-0000.'):
result = result.rstrip('0.')
return result.rstrip('.')
else:
return None
Expand Down

0 comments on commit 305a3d3

Please sign in to comment.