misc: use a single word for each time unit (#88822) #1330

Merged
fpeters merged 1 commits from wip/88822-single-timewords into main 2024-03-29 08:34:32 +01:00
2 changed files with 18 additions and 16 deletions

View File

@ -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']

View File

@ -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),

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

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
)
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

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.

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