-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse_time.py
1093 lines (853 loc) · 35.3 KB
/
parse_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
parse_time
This module contains strategies to parse time strings of different frequencies.
Up to this momment, all the strategies need to know the frequency of the time
string in advance (yearly, quarterly, monthly..)
The preconditions of all the strategies is that the strings passed to them must
be time values, otherwise an exception will be raised.
IMPORTANT!! Strategies are considered in alphabetical order... so you should
use the numbers for similar strategies, if you need a particular strategy to
be considered first. Remember to change numbers in the test_parse_time.py file
as well.
"""
from pprint import pprint
import arrow
import datetime
import parsley
import collections
from unidecode import unidecode
import sys
from xlseries.utils.time_manipulation import increment_time
import xlseries.utils.strategies_helpers
PYTHON2 = sys.version_info[0] == 2
# EXCEPTIONS
class NoPossibleTimeValue(ValueError):
"""Raised if the value is not a possible time value."""
def __init__(self, value):
msg = " ".join([str(type(value)), str(value),
"is not a possible time value."])
super(NoPossibleTimeValue, self).__init__(msg)
class TimeIsNotComposed(ValueError):
"""Raised if a valid time value try to be parsed as composed.
If a spreadsheet has time values already, time_composed parameter should be
False."""
def __init__(self, value):
msg = " ".join([str(type(value)), str(value),
"is not a composed time, time_composed should be set"
"False."])
super(TimeIsNotComposed, self).__init__(msg)
class NoTimeValue(ValueError):
"""Raised if the value is not an arrow.Arrow time value."""
def __init__(self, value, last_time=None, next_time=None):
msg = " ".join([str(type(value)), str(value),
"is not an arrow.Arrow time value."])
if last_time:
msg += "\nLast:" + str(last_time)
if next_time:
msg += "\nNext:" + str(next_time)
super(NoTimeValue, self).__init__(msg)
class BaseDateMemberOutOfRange(ValueError):
"""Part of the result of a parsing grammar is out of range for a date."""
def __init__(self, curr_time, grammar_result):
msg = " ".join(["Time value doesn't make sense.",
str(curr_time), "has been converted into",
str(grammar_result)])
super(BaseDateMemberOutOfRange, self).__init__(msg)
class DayOutOfRange(BaseDateMemberOutOfRange):
"""Raised if a day in a parsed time value is out of range."""
class MonthOutOfRange(BaseDateMemberOutOfRange):
"""Raised if a month in a parsed time value is out of range."""
# STRATEGIES
class BaseParseTimeStrategy(object):
"""BaseParseTimeStrategy class for all parse time strategies."""
# PUBLIC INTERFACE
@classmethod
def accepts(cls, params, curr_time, last_time=None, next_time=None):
"""Check the inputs to see if the strategy can handle them.
This base accepts() method check that the value is either already a
time value or that could be a string expressing time before. In the
latter it will call the private _accepts() method of the concrete
strategy being called to see if it can be handled.
Args:
params: Parameters of the series being analyzed.
curr_time: Value to be parsed into a date format.
last_time: Last value parsed into time value.
next_time: Next value to be parsed into time value.
Returns:
True or False meaning that a strategy declares it can handle the
input.
"""
# import pdb; pdb.set_trace()
if isinstance(curr_time, str) or isinstance(curr_time, str):
curr_time = unidecode(curr_time).strip()
if cls._already_time_value(curr_time):
if params["time_composed"]:
raise TimeIsNotComposed(curr_time)
else:
return True
else:
if not cls._possible_time_value(curr_time):
raise NoPossibleTimeValue(curr_time)
if not (isinstance(last_time, arrow.Arrow) or last_time is None):
raise NoTimeValue(last_time)
if isinstance(curr_time, float):
float_to_uni = cls._accepts(params, str(curr_time),
last_time, next_time)
float_to_int = cls._accepts(params, str(int(curr_time)),
last_time, next_time)
# print float_to_uni, float_to_int
return float_to_uni or float_to_int
else:
return cls._accepts(params, str(curr_time), last_time,
next_time)
def parse_time(self, params, curr_time, last_time=None, next_time=None):
"""Parse a time string or value into a proper time value.
Args:
params: Parameters of the series being analyzed.
curr_time: Value to be parsed into a date format.
last_time: Last value parsed into time value.
next_time: Next value to be parsed into time value.
Returns:
An arrow.Arrow time value.
"""
if isinstance(curr_time, str):
curr_time = unidecode(curr_time).strip()
# time format is correct
if isinstance(curr_time, arrow.Arrow):
RV = curr_time
elif isinstance(curr_time, datetime.datetime):
RV = arrow.get(curr_time)
elif (isinstance(curr_time, str) or
isinstance(curr_time, int) or
(PYTHON2 and isinstance(curr_time, long))):
RV = self._parse_time(params, str(curr_time), last_time,
next_time)
elif isinstance(curr_time, float):
try:
RV = self._parse_time(params, str(int(curr_time)),
last_time, next_time)
except:
RV = self._parse_time(params, str(curr_time),
last_time, next_time)
else:
assert isinstance(curr_time, str), "Current is not unicode."
time_value = self._parse_time(params, curr_time,
last_time, next_time)
if not isinstance(time_value, arrow.Arrow):
raise NoTimeValue(time_value, last_time, next_time)
RV = time_value
return RV
def _parse_time(self, params, curr_time, last_time=None, next_time=None):
"""Base _parse_time() method.
Most of the concrete strategies subclassing BaseParseTimeStrategy will
use this method and override only make_parsley_grammar() to change the
grammar actually used to parse a time string. If the structure varies
significantly or can be simpler, the entire _parse_time() method should
be overriden.
Args:
curr_time: Time string to be parsed.
last_time: Last time value (already parsed) in the time series
being analyzed.
params: A parameters dictionary with meta-data about the series
being analyzed.
Returns:
An arrow.Arrow time value.
"""
result = self._parse_date_elements(curr_time)
year, month, day = self._fill_parse_date_holes(result, last_time)
# check date make sense
if day not in list(range(1, 32)):
raise DayOutOfRange(curr_time, result)
if month not in list(range(1, 13)):
raise MonthOutOfRange(curr_time, result)
return arrow.get(year, month, day)
@staticmethod
def _dob_year_to_four(dob_year):
"""Convert a two digit year string in a four digits year string."""
if not dob_year:
return None
if isinstance(dob_year, str) or isinstance(dob_year, str):
if len(dob_year) == 4:
return int(dob_year)
else:
return arrow.Arrow.strptime(dob_year, "%y").year
else:
return int(dob_year)
@classmethod
def _already_time_value(cls, value):
"""Check if a value is already of a time value type."""
return (isinstance(value, arrow.Arrow) or isinstance(value, datetime.datetime))
@classmethod
def _possible_time_value(cls, value):
"""Check that a value could be a time value."""
return (value is not None)
@classmethod
def _parse_date_elements(cls, curr_time):
"""Parse any date elements found in curr_time.
Args:
curr_time (str): String time to be parsed.
Returns:
tuple: (year, month, day) At least one element is not None, but the
others could be None.
"""
raise NotImplementedError("This method is implemented in subclasses.")
@classmethod
def _fill_parse_date_holes(cls, result, last_time):
"""Analyze parsed result with last time to complete missings.
Every missing is replaced with the value of the last time value.
Args:
result (tuple): (year, month, day) Each element could be an integer
or None.
last_time (arrow.Arrow): Last time value parsed.
Returns:
tuple: (year, month, day) All the elements are integers, providing
all the information to create a new time value.
"""
# take new date elements found with the grammar
if last_time:
year = int(result[0] or last_time.year)
month = int(result[1] or last_time.month)
day = int(result[2] or last_time.day)
else:
year = int(result[0] or 1)
month = int(result[1] or 1)
day = int(result[2] or 1)
return year, month, day
class ParseSimpleTime(BaseParseTimeStrategy):
"""Parse dates expressed in a standard or very easy string to parse."""
MAX_IMPL = 20
def __init__(self, time_format=None):
self.time_format = time_format
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
return not params["time_composed"]
def _parse_time(self, params, curr_time, last_time=None, next_time=None):
"""Parse a string into a time value.
Try different standard formats and check that the result make sense in
the progression of the time series."""
str_value = curr_time.replace(".", "-").replace("/", "-")
time_value = None
# first try with the last time format that worked
if self.time_format:
try:
time_value = arrow.get(str_value, self.time_format)
if not self._time_make_sense(params, time_value, last_time,
next_time):
time_value = None
if not isinstance(time_value, arrow.Arrow):
raise NoTimeValue(time_value, last_time, next_time)
return time_value
except:
pass
# if it doesn't work, try with everyone
formats = list(self._get_possible_time_formats(str_value))
for time_format in formats:
try:
time_value = arrow.get(str_value, time_format)
except Exception:
continue
if not self._time_make_sense(params, time_value, last_time,
next_time):
time_value = None
continue
else:
break
if not isinstance(time_value, arrow.Arrow):
raise NoTimeValue(time_value, last_time, next_time)
return time_value
def _time_make_sense(self, params, time_value, last_time, next_time):
"""Check that a parsed time value make sense with the previous one.
Args:
params: Parameters of the time series.
time_value: Recently parsed time value.
last_time: Last time value that was parsed.
next_time: Next value to be parsed into a time value.
Returns:
True or False, if the value make sense with the last one and the
next one.
"""
# making sense with the last value
if last_time:
is_after_last = time_value > last_time
max_forth_time_value = increment_time(last_time, self.MAX_IMPL,
params["frequency"])
is_not_too_after_last = time_value <= max_forth_time_value
else:
is_after_last, is_not_too_after_last = True, True
# making sense with the next value
if next_time:
try:
next_time = self.parse_time(params, next_time, time_value)
is_before_next = time_value < next_time
max_forth_time_value = increment_time(time_value,
self.MAX_IMPL,
params["frequency"])
is_not_too_before_next = next_time <= max_forth_time_value
except NoTimeValue:
is_before_next, is_not_too_before_next = False, False
else:
is_before_next, is_not_too_before_next = True, True
return (is_after_last and is_not_too_after_last and is_before_next and
is_not_too_before_next)
@staticmethod
def _get_possible_time_formats(str_value):
"""Generate all possible time formats that could apply to str_value.
Args:
str_value: A string representing time.
Yields:
A possible time format for a given time string value.
"""
reps = list(map(len, str_value.split("-")))
assert len(reps) == 3, "There is no 3 date elements in " + str_value
for order in ["D-M-Y", "M-D-Y", "Y-M-D"]:
time_format = "-".join([char * reps[i] for i, char in
enumerate(order.split("-"))])
yield time_format
class BasePEG(BaseParseTimeStrategy):
def __init__(self):
self.grammar = None
def _parse_date_elements(self, curr_time):
"""Parse any date elements found in curr_time.
Args:
curr_time (str): String time to be parsed.
Returns:
tuple: (year, month, day) At least one element is not None, but the
others could be None.
"""
# create grammar only if not already created
if not self.grammar:
self.grammar = self.make_parsley_grammar()
return self.grammar(curr_time).date()
class BaseComposedQuarter():
"""Parse dates from strings composed by substrings with date info.
Only for quarterly series."""
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
if params["time_composed"] and params["frequency"] == "Q":
try:
cls.make_parsley_grammar()(curr_time).date()
except:
return False
return True
else:
return False
@staticmethod
def _quarter_num_to_month(quarter_number):
"""Convert a quarter number in the number of first month."""
# print quarter_number
if not quarter_number:
return None
replacements = collections.OrderedDict()
replacements["IV"] = "4"
replacements["III"] = "3"
replacements["II"] = "2"
replacements["I"] = "1"
# replace strings and convert to int
if not isinstance(quarter_number, int):
quarter_number = str(quarter_number)
for orig, new in replacements.items():
quarter_number = quarter_number.replace(orig, new)
quarter_number = int(quarter_number.strip())
if quarter_number == 1:
month = 1
elif quarter_number == 2:
month = 4
elif quarter_number == 3:
month = 7
else:
month = 10
return month
class ParseComposedQuarter1(BaseComposedQuarter, BasePEG):
"""Parse quarterly dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["'1986 1º trim.",
... "' 2º trim.",
... "' 3º trim.",
... "' 4º trim.",
... "'1987 I (1) ",
... "' II * ",
... "' III * (2) *",
... "' IV "]
>>> params = {}
>>>
>>> last = None
>>> time_parser = ParseComposedQuarter1()
>>> for str_date in orig:
... new = time_parser.parse_time(params, str_date, last)
... last = new
... print(new)
1986-01-01T00:00:00+00:00
1986-04-01T00:00:00+00:00
1986-07-01T00:00:00+00:00
1986-10-01T00:00:00+00:00
1987-01-01T00:00:00+00:00
1987-04-01T00:00:00+00:00
1987-07-01T00:00:00+00:00
1987-10-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
not_d_or_q = anything:x ?(x not in "0123456789IV()")
q_str = ('I' | 'V'):q -> q
q_int = digit:q ?(q in "1234") -> int(q)
ref = ws '(' digit{1, 3} ')' ws | '*'
q_num_or_let = (<q_str{1, 3}> | q_int):q -> q
quarter = not_d_or_q* q_num_or_let:q not_d_or_q* -> q
year = not_d_or_q* <digit{4}>:y not_d_or_q* -> int(y)
date = year?:y quarter:q ref? not_d_or_q* ref? ->(y, q_to_m(q), 1)
""", {"q_to_m": cls._quarter_num_to_month})
class ParseComposedQuarter2(BasePEG, BaseComposedQuarter):
"""Parse quarterly dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["III.1963",
... "IV.1963",
... "I.1964",
... "II.1964",
... "III.1964"]
>>> last = None
>>> time_parser = ParseComposedQuarter2()
>>> for str_date in orig:
... new = time_parser.parse_time({}, str_date, last)
... last = new
... print(new)
1963-07-01T00:00:00+00:00
1963-10-01T00:00:00+00:00
1964-01-01T00:00:00+00:00
1964-04-01T00:00:00+00:00
1964-07-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
separator = anything:x ?(x in "-/.T ")
not_digit = anything:x ?(x not in "0123456789-/. ")
ref = ws '(' digit{1, 3} ')' ws | '*'
q = <not_digit*>:q -> q
y = <digit{4}>:y -> y
date = ws ref? q:q separator* y:y ref? ws anything{0, 3} -> (int(y), q_to_m(q), 1)
""", {"q_to_m": cls._quarter_num_to_month})
class ParseComposedQuarter3(BasePEG, BaseComposedQuarter):
"""Parse quarterly dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["2° Trim 07",
... "u' 3 Trim 07 2'",
... "4° Trim 07 ",
... "1° Trim 08 "]
>>> last = None
>>> time_parser = ParseComposedQuarter3()
>>> for str_date in orig:
... new = time_parser.parse_time({}, str_date, last)
... last = new
... print(new)
2007-04-01T00:00:00+00:00
2007-07-01T00:00:00+00:00
2007-10-01T00:00:00+00:00
2008-01-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
not_digit = anything:x ?(x not in "0123456789 ")
q = not_digit* ws digit:q not_digit* ws -> int(q)
y = (ws | not_digit) <digit{2}>:y (ws | not_digit) -> y
ref = ws '(' digit{1, 3} ')' ws | '*'
date = q:q not_digit* y:y ref? ws anything{0, 3} -> (dob_year(y), q_to_m(q), 1)
""", {"q_to_m": cls._quarter_num_to_month,
"dob_year": cls._dob_year_to_four})
class ParseComposedQuarter4(BasePEG, BaseComposedQuarter):
"""Parse quarterly dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["III 01",
... "IV 01",
... "I 02",
... "II 02"]
>>> last = None
>>> time_parser = ParseComposedQuarter4()
>>> for str_date in orig:
... new = time_parser.parse_time({}, str_date, last)
... last = new
... print(new)
2001-07-01T00:00:00+00:00
2001-10-01T00:00:00+00:00
2002-01-01T00:00:00+00:00
2002-04-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
separator = anything:x ?(x in "-/.T ")
not_digit = anything:x ?(x not in "0123456789-/. ")
ref = ws '(' digit{1, 3} ')' ws | '*'
q = <not_digit*>:q -> q
y = <digit{2}>:y -> y
date = ws ref? q:q separator* y?:y ref? ws anything{0, 3} -> (dob_year(y), q_to_m(q), 1)
""", {"q_to_m": cls._quarter_num_to_month,
"dob_year": cls._dob_year_to_four})
class ParseComposedYearQuarter1(BasePEG, BaseComposedQuarter):
"""Parse multifrequency YQQQQ time strings like the example below.
>>> orig = ["2008 Año *",
... "Trimestre I *",
... " II * (2)",
... " III *",
... " IV *"]
>>> params = {"time_format": str}
>>>
>>> last = None
>>> time_parser = ParseComposedYearQuarter1()
>>> for str_date in orig:
... new = time_parser.parse_time(params, str_date, last)
... last = new
... print(new)
2008-01-01T00:00:00+00:00
2008-01-01T00:00:00+00:00
2008-04-01T00:00:00+00:00
2008-07-01T00:00:00+00:00
2008-10-01T00:00:00+00:00
"""
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
if not (params["time_composed"] and params["frequency"] == "AQQQQ"):
return False
try:
cls.make_parsley_grammar()(curr_time).date()
return True
except:
return False
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
not_digit = anything:x ?(x not in "0123456789")
not_d_or_q = anything:x ?(x not in "0123456789IV()")
q_letter = ('I' | 'V'):q -> q
ref = ws '(' digit{1, 3} ')' ws | '*'
quarter = not_d_or_q* <q_letter{1, 3}>:q not_d_or_q* -> q
year = not_digit* <digit{4}>:y not_digit* -> int(y)
date = year?:y quarter?:q ref? not_d_or_q* ref? ->(y, q_to_m(q), 1)
""", {"q_to_m": cls._quarter_num_to_month})
class ParseComposedQuarterYear1(ParseComposedYearQuarter1):
"""Parse multifrequency QQQQY time strings like the example below.
>>> orig = ["2003 I",
... "II",
... "III",
... "IV",
... "Año"]
>>> params = {"time_format": str}
>>>
>>> last = None
>>> time_parser = ParseComposedQuarterYear1()
>>> for str_date in orig:
... new = time_parser.parse_time(params, str_date, last)
... last = new
... print(new)
2003-01-01T00:00:00+00:00
2003-04-01T00:00:00+00:00
2003-07-01T00:00:00+00:00
2003-10-01T00:00:00+00:00
2003-01-01T00:00:00+00:00
"""
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
if not (params["time_composed"] and params["frequency"] == "QQQQA"):
return False
try:
cls.make_parsley_grammar()(curr_time).date()
return True
except:
return False
@classmethod
def _fill_parse_date_holes(cls, result, last_time):
"""Analyze parsed result with last time to complete missings.
Every missing is replaced with the value of the last time value,
except if year and month are missing, and the last time value
represents the 4th quarter of a year. This will be interpreted as the
"year" part of the QQQQY multifrequency time index.
Args:
result (tuple): (year, month, day) Each element could be an integer
or None.
last_time (arrow.Arrow): Last time value parsed.
Returns:
tuple: (year, month, day) All the elements are integers, providing
all the information to create a new time value.
"""
# take new date elements found with the grammar
if last_time:
if not result[0] and not result[1] and last_time.month == 10:
year = int(last_time.year)
month = 1
else:
year = int(result[0] or last_time.year)
month = int(result[1] or last_time.month)
else:
year = int(result[0] or 1)
month = int(result[1] or 1)
return year, month, 1
class BaseComposedSemester():
"""Parse dates from strings composed by substrings with date info.
Only for semester series."""
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
if params["time_composed"] and params["frequency"] == "S":
try:
cls.make_parsley_grammar()(curr_time).date()
except:
return False
return True
else:
return False
@staticmethod
def _semester_num_to_month(semester_number):
"""Convert a semester number in the number of first month."""
# print quarter_number
if not semester_number:
return None
replacements = collections.OrderedDict()
replacements["II"] = "2"
replacements["I"] = "1"
# replace strings and convert to int
if not isinstance(semester_number, int):
semester_number = str(semester_number)
for orig, new in replacements.items():
semester_number = semester_number.replace(orig, new)
semester_number = int(semester_number.strip())
if semester_number == 1:
month = 1
else:
month = 7
return month
class ParseComposedSemester(BasePEG, BaseComposedSemester):
"""Parse semester dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["(2)I.S.03",
... "II.S.03*",
... "I.S.04",
... "*II.S.04",
... "I.S.05(1)"]
>>> last = None
>>> time_parser = ParseComposedSemester()
>>> for str_date in orig:
... new = time_parser.parse_time({}, str_date, last)
... last = new
... print(new)
2003-01-01T00:00:00+00:00
2003-07-01T00:00:00+00:00
2004-01-01T00:00:00+00:00
2004-07-01T00:00:00+00:00
2005-01-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
separator = anything:x ?(x in "-/.S ")
not_digit = anything:x ?(x not in "0123456789-/. ")
ref = ws '(' digit{1, 3} ')' ws | '*'
s = <not_digit*>:s -> s
y = <digit{2}>:y -> y
date = ws ref? s:s separator* y?:y ref? ws anything{0, 3} -> (dob_year(y), s_to_m(s), 1)
""", {"s_to_m": cls._semester_num_to_month,
"dob_year": cls._dob_year_to_four})
class BaseComposedMonth():
"""Parse dates from strings composed by substrings with date info.
Only for quarterly series."""
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
if not (params["time_composed"] and params["frequency"] == "M"):
return False
try:
cls.make_parsley_grammar()(curr_time).date()
return True
except:
return False
@classmethod
def _month_str_to_num(cls, month_str):
"""Convert month string in month number.
>>> BaseComposedMonth._month_str_to_num("ene")
1
>>> BaseComposedMonth._month_str_to_num("jan")
1
>>> BaseComposedMonth._month_str_to_num("september")
9
>>> BaseComposedMonth._month_str_to_num("septiembre")
9
"""
loc_iter = (arrow.locales.__dict__[l] for l in vars(arrow.locales)
if l[-6:] == "Locale")
for local in loc_iter:
month_num = local().month_number(month_str.capitalize())
if month_num:
break
return month_num
class ParseComposedMonth1(BasePEG, BaseComposedMonth):
"""Parse quarterly dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["1991 Ene. ",
... "1991 Febrero",
... "1991 Marzo. (1)",
... "Abril. 1991 (2)",
... "Mayo. (3) 1991",
... "Jun. ",
... "Julio. * "]
>>> params = {"time_format": str}
>>>
>>> last = None
>>> time_parser = ParseComposedMonth1()
>>> for str_date in orig:
... new = time_parser.parse_time(params, str_date, last)
... last = new
... print(new)
1991-01-01T00:00:00+00:00
1991-02-01T00:00:00+00:00
1991-03-01T00:00:00+00:00
1991-04-01T00:00:00+00:00
1991-05-01T00:00:00+00:00
1991-06-01T00:00:00+00:00
1991-07-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
not_digit = anything:x ?(x not in "0123456789 ")
not_d_or_p = anything:x ?(x not in "0123456789()")
sep = ws anything:x ws ?(x in ".-/,")
y = (ws | not_digit) <digit{2, 4}>:y (ws | not_digit) -> y
m = ws <letter{3, 50}>:m '.'? -> m
ref = ws '(' digit{1, 3} ')' ws | '*'
y_m = y:y ref? sep? m:m anything* -> (year(y), month(m), 1)
m_y = m:m ref? sep? y:y anything* -> (year(y), month(m), 1)
only_m = m:m anything* -> (None, month(m), 1)
date = y_m | m_y | only_m
""", {"month": cls._month_str_to_num,
"year": cls._dob_year_to_four})
class ParseComposedMonth2(BasePEG, BaseComposedMonth):
"""Parse quarterly dates from strings composed by substrings with date
info of the structure showed in the example.
>>> orig = ["1991,01 ",
... "1991.01 ",
... "1991,02 ",
... "1991.02 ",
... " 03,1991 ",
... "04 ",
... "05 ",
... "06 "]
>>> params = {"time_format": str}
>>>
>>> last = None
>>> time_parser = ParseComposedMonth2()
>>> for str_date in orig:
... new = time_parser.parse_time(params, str_date, last)
... last = new
... print(new)
1991-01-01T00:00:00+00:00
1991-01-01T00:00:00+00:00
1991-02-01T00:00:00+00:00
1991-02-01T00:00:00+00:00
1991-03-01T00:00:00+00:00
1991-04-01T00:00:00+00:00
1991-05-01T00:00:00+00:00
1991-06-01T00:00:00+00:00
"""
@classmethod
def make_parsley_grammar(cls):
"""Return a parsley parsing expression grammar."""
return parsley.makeGrammar("""
not_digit = anything:x ?(x not in "0123456789")
y = (ws | not_digit) <digit{4}>:y (ws | not_digit) -> y
m = (ws | not_digit) <digit{1, 2}>:m (ws | not_digit) -> int(m)
y_m = y:y not_digit* m:m anything* -> (y, m, 1)
m_y = m:m not_digit* y:y anything* -> (y, m, 1)
only_m = m:m anything* -> (None, m, 1)
date = y_m | m_y | only_m
""", {})
class BaseComposedYear():
"""Parse dates from strings composed by substrings with date info.
Only for yearly series."""
@classmethod
def _accepts(cls, params, curr_time, last_time=None, next_time=None):
if not (params["time_composed"] and params["frequency"] == "A"):
return False
try:
cls.make_parsley_grammar()(curr_time).date()
return True