I also added tzinfo as it come from parse_date, I just copy some code and
make get_fixed_timezone as a FixedTimeZone classmethod.
Regarding our doubts about benchmarks, you'll always find them commented in
the top of the file, I hope to make them as many immediate as possible to
avoid waste of time.
https://github.com/peppelinux/Django-snippets/blob/master/datetime_heuristic_parser.py

this is what I get at this moment:
python3 -m timeit -s "import datetime"
"datetime.datetime.strptime('2019-02-03T17:27:58.645194',
'%Y-%m-%dT%H:%M:%S.%f')"
100000 loops, best of 3: 11.2 usec per loop

python3 -m timeit -s "from django.utils.dateparse import parse_datetime"
"parse_datetime('2019-02-03T17:27:58.645194')"
100000 loops, best of 3: 6.04 usec per loop

python3 -m timeit -s   "import sys, os; sys.path.append(os.getcwd()); from
datetime_heuristic_parser import datetime_heuristic_parser;
print(datetime_heuristic_parser('04/12/2018 09:7:4Z'))"
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
[datetime.datetime(2018, 12, 4, 9, 7, 4, tzinfo=datetime.timezone.utc)]
100000000 loops, best of 3: 0.00878 usec per loop

...as long I'm running it I still can see a good result as a returned and
valid datetime, no exception.
I'll continue to use this code so I'll take care to keep an eye on it, I
also hope to share this with you.

Il giorno lun 4 feb 2019 alle ore 11:07 Tom Forbes <t...@tomforb.es> ha
scritto:

> For me, I get:
>
> In [4]: %timeit  datetime_heuristic_parser('2019-02-03T17:27:58.645194')
> 18.9 µs ± 431 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>
> And for Django:
>
> In [3]: %timeit parse_datetime('2019-02-03T17:27:58.645194')
> 6.97 µs ± 408 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>
> I assume there is something wrong with the way you benchmarked the code.
> Python is not *that* slow, but 0.0241 per loop is way, way too fast.
>
>
>
> On 4 February 2019 at 09:22:03, Giuseppe De Marco (
> giuseppe.dema...@unical.it) wrote:
>
> Hello everyone, first of all I am grateful for your time and your
> attention.
>
> @Tom Forbes
> The first time I runned it I thought the same thing! Please use
> https://github.com/peppelinux/Django-snippets/blob/master/datetime_heuristic_parser.py
> and not the previous pasted one. I'm quite sure that all the tests passes
> well, because of their output. As we can see I deal with a tuple that
> contains ('format', 'compiled_regexp', 'values dictionary'), this obviously
> just for test purpose.
>
> Parsing succesfull on "04/12/2018":
> [('%d/%m/%Y', '(?P<day>\\d{1,2})/(?P<month>\\d{1,2})/(?P<year>\\d{4})$',
> {'year': 2018, 'month': 12, 'day': 4})]
>
> Parsing succesfull on "04/12/2018 3:2:1":
> [('%d/%m/%Y %H:%M:%S',
> '(?P<day>\\d{1,2})/(?P<month>\\d{1,2})/(?P<year>\\d{4})
> (?P<hour>\\d{1,2}):(?P<minute>\\d{1,2}):(?P<second>\\d{1,2})$', {'second':
> 1, 'year': 2018, 'minute': 2, 'month': 12, 'hour': 3, 'day': 4})]
>
> Parsing succesfull on "2018-03-4 09:7:4":
> [('%Y-%m-%d %H:%M:%S',
> '(?P<year>\\d{4})-(?P<month>\\d{1,2})-(?P<day>\\d{1,2})
> (?P<hour>\\d{1,2}):(?P<minute>\\d{1,2}):(?P<second>\\d{1,2})$', {'second':
> 4, 'year': 2018, 'minute': 7, 'month': 3, 'hour': 9, 'day': 4})]
>
> Parsing succesfull on "2018-03-04T09:7:4.645194":
> [('%Y-%m-%dT%H:%M:%S.%f',
> '(?P<year>\\d{4})-(?P<month>\\d{1,2})-(?P<day>\\d{1,2})T(?P<hour>\\d{1,2}):(?P<minute>\\d{1,2}):(?P<second>\\d{1,2}).(?P<microsecond>\\d{6})$',
> {'second': 4, 'year': 2018, 'minute': 7, 'month': 3, 'microsecond': 645194,
> 'hour': 9, 'day': 4})]
>
> Parsing succesfull on "20180304121940.948000Z":
> [('%Y%m%d%H%M%S.%fZ',
> '(?P<year>\\d{4})(?P<month>\\d{1,2})(?P<day>\\d{1,2})(?P<hour>\\d{1,2})(?P<minute>\\d{1,2})(?P<second>\\d{1,2}).(?P<microsecond>\\d{6})Z$',
> {'second': 40, 'year': 2018, 'minute': 19, 'month': 3, 'microsecond':
> 948000, 'hour': 12, 'day': 4})]
>
> Yesterday I coded it on a tablet, this morning from my laptop I got this
> performance:
> python -m timeit -s "from datetime_heuristic_parser import
> datetime_heuristic_parser;
> datetime_heuristic_parser('2019-02-03T17:27:58.645194')"
> 100000000 loops, best of 3: 0.00891 usec per loop
>
> I also added a simple raise Exception in the case it should return an
> error, thank you for your suggestion.
>
> @Augustin
> Regarding your questions:
>
> - would this be useful?
> I think yes, for the following reasons:
> 1. We have an authentic regexp compiler based on DATE_FORMATS and
> DATETIME_FORMATS
> 3. We don't have to write datetime regexp anymore, this code will compile
> a regexp from a format, indipendently of its delimiter char (if -, / or
> whatever)
> 4. We get generalized function that returns datetime objects, no
> try/except and datetime.strptime, It's faster then other implementations!
> 5. It's settings.py focused, all we have to worry is a correct settings.py
> configuration. In other words We just have to collect all the possibile
> date/datetime formats that could be used in the project, even if they are
> used in forms or in model.fields
> 6. We don't need anymore to hardcode datetime regexp pattern in our code,
> the regexp compiler will work on top of date formats strings!
>
> - would a Form not be a better choice?
> Sure, I'm tring to generalize a method that could be a stop application
> for all the date and datetime approaches. It could be used for forms, in
> DATETIME_INPUT_FORMATS and DATE_INPUT_FORMAT. These could generate form
> specialized regexp compilations if this approach will be implemented.
>
> The main goal is to give a tool that will work well and in every
> conditions,and be funny too!
>
>
> Il giorno lun 4 feb 2019 alle ore 01:30 Tom Forbes <t...@tomforb.es> ha
> scritto:
>
>> I’m pretty sure 0.0241 usec per loop is either a typo or a mistake
>> during benchmarking. I’ve got no comment what you’re proposing but correct
>> and valid benchmarks are important, so I would double check that.
>>
>>
>> On 3 February 2019 at 23:37:14, Giuseppe De Marco (
>> giuseppe.dema...@unical.it) wrote:
>>
>> Regarding the previous example,
>> better to read it here (my fault: I mistaken the format
>> '%Y-%m-%dT%H:%M:%S.%f'):
>>
>> https://github.com/peppelinux/Django-snippets/blob/master/datetime_heuristic_parser.py
>>
>> and also, it should came also with tzinfo regexp and other functions as
>> well, like parse_date time_duration... it's only an example to share our
>> experiences.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-developers+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-developers@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-developers.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-developers/CABms%2BYpcU3gbv7MqV1FkwU5mt9xhTRBoPeG6bPoYCnLvTJLGAw%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-developers/CABms%2BYpcU3gbv7MqV1FkwU5mt9xhTRBoPeG6bPoYCnLvTJLGAw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-developers+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-developers@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-developers.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-developers/CAFNZOJPciEjaiXO0oO-UTDCTnbgcSP3Us3D-gOdP4jvMZPGu_A%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-developers/CAFNZOJPciEjaiXO0oO-UTDCTnbgcSP3Us3D-gOdP4jvMZPGu_A%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
> ____________________
> Dott. Giuseppe De Marco
> CENTRO ICT DI ATENEO
> University of Calabria
> 87036 Rende (CS) - Italy
> Phone: +39 0984 496945
> e-mail: giuseppe.dema...@unical.it
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/CABms%2BYrNN%3DhU4fV%2BxfAdKPR0%3DXiqGiGS2PVLxQ%3DUG2cxOdZNGw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-developers/CABms%2BYrNN%3DhU4fV%2BxfAdKPR0%3DXiqGiGS2PVLxQ%3DUG2cxOdZNGw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/CAFNZOJMyc3fuVO7ubnwHmOjee9eNLexfa7-fUqorUeD-iOsSMA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-developers/CAFNZOJMyc3fuVO7ubnwHmOjee9eNLexfa7-fUqorUeD-iOsSMA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 
____________________
Dott. Giuseppe De Marco
CENTRO ICT DI ATENEO
University of Calabria
87036 Rende (CS) - Italy
Phone: +39 0984 496945
e-mail: giuseppe.dema...@unical.it

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CABms%2BYqKbEHqxESebG%3D92owYzk0FSa3%3DQbjbGfctY9U%2BSaebrg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to