misc: use a single word for each time unit (#88822) #1330
|
@ -20,7 +20,7 @@ from wcs.fields import StringField
|
|||
from wcs.qommon import evalutils, force_str
|
||||
from wcs.qommon.form import FileSizeWidget
|
||||
from wcs.qommon.http_request import HTTPRequest
|
||||
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration
|
||||
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration, timewords
|
||||
from wcs.qommon.misc import (
|
||||
_http_request,
|
||||
date_format,
|
||||
|
@ -108,6 +108,10 @@ def test_humantime_short(seconds, expected):
|
|||
assert seconds2humanduration(seconds, short=True) == expected
|
||||
|
||||
|
||||
def test_humantime_timewords():
|
||||
assert timewords() == ['day(s)', 'hour(s)', 'minute(s)', 'second(s)', 'month(s)', 'year(s)']
|
||||
|
||||
|
||||
def test_parse_mimetypes():
|
||||
assert FileTypesDirectory.parse_mimetypes('application/pdf') == ['application/pdf']
|
||||
assert FileTypesDirectory.parse_mimetypes('.pdf') == ['application/pdf']
|
||||
|
|
|
@ -34,21 +34,20 @@ def list2human(stringlist):
|
|||
|
||||
|
||||
_humandurations = (
|
||||
((_('day'), _('days')), _day),
|
||||
((_('hour'), _('hours')), _hour),
|
||||
((_('month'), _('months')), _month),
|
||||
((_('year'), _('years')), _year),
|
||||
((_('minute'), _('minutes')), _minute),
|
||||
((_('second'), _('seconds')), 1),
|
||||
((_('day'), _('days'), _('day(s)')), _day),
|
||||
((_('hour'), _('hours'), _('hour(s)')), _hour),
|
||||
((_('minute'), _('minutes'), _('minute(s)')), _minute),
|
||||
((_('second'), _('seconds'), _('second(s)')), 1),
|
||||
((_('month'), _('months'), _('month(s)')), _month),
|
||||
((_('year'), _('years'), _('year(s)')), _year),
|
||||
|
||||
)
|
||||
|
||||
|
||||
def timewords():
|
||||
'''List of words one can use to specify durations'''
|
||||
result = []
|
||||
for words, dummy in _humandurations:
|
||||
for word in words:
|
||||
result.append(str(word)) # str() to force translation
|
||||
for (dummy, dummy, word), dummy in _humandurations:
|
||||
result.append(str(word)) # str() to force translation
|
||||
return result
|
||||
|
||||
|
||||
|
@ -56,12 +55,11 @@ def humanduration2seconds(humanduration):
|
|||
if not humanduration:
|
||||
raise ValueError()
|
||||
seconds = 0
|
||||
for words, quantity in _humandurations:
|
||||
for word in words:
|
||||
m = re.search(r'(\d+)\s*\b%s\b' % word, humanduration)
|
||||
if m:
|
||||
seconds = seconds + int(m.group(1)) * quantity
|
||||
break
|
||||
for (word1, word2, dummy), quantity in _humandurations:
|
||||
# look for number then singular or plural forms of unit
|
||||
pmarillonnet
commented
La PR venant complexifier un peu cette expression régulière, j’aurais bien vu une petite ligne de commentaire juste au dessus, pour expliquer que l’unité peut être exprimée au singulier ou au pluriel, les deux étant d’intérêt ici. La PR venant complexifier un peu cette expression régulière, j’aurais bien vu une petite ligne de commentaire juste au dessus, pour expliquer que l’unité peut être exprimée au singulier ou au pluriel, les deux étant d’intérêt ici.
fpeters
commented
En effet, j'ai ajouté un petit commentaire. En effet, j'ai ajouté un petit commentaire.
|
||||
m = re.search(r'(\d+)\s*\b(%s|%s)\b' % (word1, word2), humanduration)
|
||||
if m:
|
||||
seconds = seconds + int(m.group(1)) * quantity
|
||||
return seconds
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
En passant je modifie un peu l'ordre des unités, pour faire moins rollercoaster.
jours ↘ heures ↗mois ↗années ↘minutes ↘ secondes
→ jours ↘ heures ↘ minutes ↘ secondes ↗mois ↗années