11#!/usr/bin/env python3
22"""Test various methods for converting free text to a timestring"""
33
4+ from collections import namedtuple
45from datetime import timedelta
6+ from csv import writer
57from timeit import default_timer
8+ from re import compile as re_compile
9+
610from timetracker .epoch .epoch import _get_dt_ampm
711from timetracker .epoch .epoch import _conv_timedelta
812from timetracker .epoch .epoch import _conv_datetime
913from tests .pkgtttest .timestrs import TIMESTRS
1014from tests .pkgtttest .timestrs import NOW
1115
1216
13- def test_tt_getdt ():
17+ def test_tt_getdt (fcsv = 'timetrials_datatime.csv' ):
1418 """Test various methods for converting free text to a timestring"""
19+ nto = namedtuple ('RunTimes' , 'DVK DVK_matched dateparser dateparser_matched txt' )
20+ timedata = _run (nto )
21+ #_prt_timedata(timedata)
22+ _wr_timedata (fcsv , timedata , nto )
23+
24+
25+ def _run (nto ):
26+ timedata = []
27+ #cmp_time = re_compile(r'((\d{1,2}):){0,2}(\d{1,2})\s*(?P<AM_PM>[aApP][mM])')
28+ # pylint: disable=line-too-long
29+ cmp_time = re_compile (r'((?P<hour>\d{1,2})[^-/_](:(?P<minute>\d{1,2}))?[^-/_](:(?P<second>\d{1,2}))?\s*(?P<AM_PM>[aApP][mM])?)' )
30+ cmp_date = re_compile (r'((?P<year>\d{4})[-/_]?)?(?P<month>\d{1,2})[-/_](?P<day>\d{1,2})' )
1531 print (f'NOW: { NOW } ' )
1632 for timestr , expdct in TIMESTRS .items ():
1733 print (f'\n TIMESTR({ timestr } )' )
34+ tic = default_timer ()
35+ print ("SEARCH FOR TIME:" , cmp_time .search (timestr ))
36+ print ("SEARCH FOR DATE:" , cmp_date .search (timestr ))
37+ tt0 = timedelta (seconds = default_timer ()- tic )
38+ print (f'{ tt0 } re ({ timestr } )' ) # {dta}')
1839
1940 tic = default_timer ()
2041 dta = _get_dt_ampm (timestr , NOW )
@@ -25,7 +46,7 @@ def test_tt_getdt():
2546 tic = default_timer ()
2647 dtb = _conv_datetime (timestr , NOW )
2748 ttb = timedelta (seconds = default_timer ()- tic )
28- print (f'{ ttb } _conv_datetime({ timestr } ) { dtb } ' )
49+ # print(f'{ttb} _conv_datetime({timestr}) {dtb}')
2950
3051 tic = default_timer ()
3152 dtc = _conv_timedelta (timestr )
@@ -40,6 +61,24 @@ def test_tt_getdt():
4061 if timestr not in {'12' , '13' }:
4162 assert dta == dtb , f'DVK != DTP\n TXT({ timestr } )\n DVK({ dta } )\n DTP({ dtb } )'
4263
64+ timedata .append (nto (
65+ txt = timestr ,
66+ DVK = tta .total_seconds ()* 1_000_000 , DVK_matched = dta is not None ,
67+ dateparser = ttb .total_seconds ()* 1_000_000 , dateparser_matched = dtb is not None ))
68+
69+ return timedata
70+
71+ def _prt_timedata (timedata ):
72+ for ntd in timedata :
73+ print (ntd )
74+
75+ def _wr_timedata (fcsv , timedata , nto ):
76+ with open (fcsv , 'w' , encoding = 'utf-8' ) as ostrm :
77+ wrobj = writer (ostrm )
78+ wrobj .writerow (nto ._fields )
79+ for ntd in timedata :
80+ wrobj .writerow (ntd )
81+ print (f' WROTE: { fcsv } ' )
4382
4483if __name__ == '__main__' :
4584 test_tt_getdt ()
0 commit comments