Live data from Hacker News

FreezeGun: easy mocking of Python datetimes

stevepulec.com

1–10 of 18 posts

Re: FreezeGun: easy mocking of Python datetimes

#3
Rubyists desiring similar functionality should check out either of the following libs. As far as I can tell they differ only in their pun selection.

https://github.com/travisjeffery/timecop

https://github.com/bebanjo/delorean

In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.

Re: FreezeGun: easy mocking of Python datetimes

#4

Rubyists desiring similar functionality should check out either of the following libs. As far as I can tell they differ only in their pun selection. https://github.com/travisjeffery/timecop https://github.com/bebanjo/delorean In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.

>In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.

Hmm, perhaps I should check Ruby out after all...

Re: FreezeGun: easy mocking of Python datetimes

#6

Cool, but unfortunately it breaks if the code your testing does from datetime import datetime :( https://gist.github.com/4261936

Hey Timothy, thanks for the response. You are correct. I'll add a warning to the library and work on a solution. Unfortunately, I think it will need to involve ctypes.

Re: FreezeGun: easy mocking of Python datetimes

#7

Rubyists desiring similar functionality should check out either of the following libs. As far as I can tell they differ only in their pun selection. https://github.com/travisjeffery/timecop https://github.com/bebanjo/delorean In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.

>In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite. Hmm, perhaps I should check Ruby out after all...

It's great. We got unicorns and Series As for everyone!

Re: FreezeGun: easy mocking of Python datetimes

#8
post #6

Cool, but unfortunately it breaks if the code your testing does from datetime import datetime :( https://gist.github.com/4261936

Hey Timothy, thanks for the response. You are correct. I'll add a warning to the library and work on a solution. Unfortunately, I think it will need to involve ctypes.

Have you tried patching the now() method onto datetime.datetime instead of replacing the whole class? If that doesn't work, you could replace datetime first:

    import freezegun; freezegun.monkey_patch()
    from datetime import datetime
After that, freeze_time would just set a flag on your datetime class.

Re: FreezeGun: easy mocking of Python datetimes

#9
post #8
post #6

Earlier quoted context omitted.

Hey Timothy, thanks for the response. You are correct. I'll add a warning to the library and work on a solution. Unfortunately, I think it will need to involve ctypes.

Have you tried patching the now() method onto datetime.datetime instead of replacing the whole class? If that doesn't work, you could replace datetime first: import freezegun; freezegun.monkey_patch() from datetime import datetime After that, freeze_time would just set a flag on your datetime class.

Sadly, it's not possible. datetime is a C class, so it is immutable.

Re: FreezeGun: easy mocking of Python datetimes

#10
post #9
post #8

Earlier quoted context omitted.

Have you tried patching the now() method onto datetime.datetime instead of replacing the whole class? If that doesn't work, you could replace datetime first: import freezegun; freezegun.monkey_patch() from datetime import datetime After that, freeze_time would just set a flag on your datetime class.

Sadly, it's not possible. datetime is a C class, so it is immutable.

Correct. I've gone with his latter solution for now and added a warning about import order. I'm not very happy with this solution through and will be spending some time with ctypes in the next few days to come up with something better.
Post reply on HN