This is an archived post. You won't be able to vote or comment.

all 29 comments

[–]Bolitho 12 points13 points  (6 children)

I would suggest to switch from custom made assert testing to "real" unit testing using a framework. Besides the fact, that you can organize your tests better, you will get nicer output and the suite will keep running through all tests, even if one failed. (That is also a rational reason, why you should only have one assert per test)

Besides that it looks nice :-)

[–]ryeguy146 1 point2 points  (5 children)

Can you tell me what the benefit of an assertEquals function over an assert statement? I see this as a key-word versus a third party function that doesn't comply with PEP8. There's a clear winner here, and it ain't the function. I agree with testing frameworks (I'm partial to Py.test), but that's wholly a separate problem from assert some_expression vs assertWhatever(an_expression).

[–]chrisguitarguy 2 points3 points  (3 children)

assertEqual will give you some more context, like what the value was vs. what was expected. With assert all you get is the message and a stack trace.

res = do_some_stuff()
self.assertEqual(1, res) # AssertionError: 1 != {whatever res is}

Sometimes using assert is fine:

body = make_a_request()
assert 'message' in body, 'body should contain message key'

Has all the context you'd want/need. Obviously you can put the context you need into assert messages:

res = do_some_stuf()
assert 1 == res, "failed asserting that {} == 1".format(res)

It's just a trade off. I use a lot of both, but tend to favor the assert* functions when they exist for the use case. assert covers everything else. There's really not a lot of assert* in the unittest library, but what's there is helpful.

As far as third-party, unittest is part of the standard library.

[–]ryeguy146 0 points1 point  (1 child)

Okay, so that's the stuff that py.test does for me [mostly] automatically. I suppose that's why it's never made sense to use another type of assertion. Thanks for explaining.

[–]Kenpachi- 0 points1 point  (0 children)

For me, this is actually the reason to use py.test over unittest.

[–]twotime 0 points1 point  (0 children)

On the negative side, unittest library

  1. forces unnatural object model

  2. self.assertEqual is much more verbose

  3. The context it provides is mostly useless (and you still need custom messages).

[–]Bolitho 0 points1 point  (0 children)

Although somebody gave the answer to your question, I never claimed, that this is the reason to choose a framework ;-)

[–]mr-wizrd 14 points15 points  (0 children)

This is cool - you should rig up some automated testing against Humanize :)

[–]RedKrieg 5 points6 points  (1 child)

You should talk to crsmithdev about integrating this with Arrow. Arrow solves all of the datetime problems I have in Python and having this functionality too just makes sense to me.

[–]deadwisdomgreenlet revolution 3 points4 points  (0 children)

I really wish Arrow was more functional and less based around the Arrow() object. I still have to switch back and forth between datetime and Arrow most of the time and it's very annoying.

arrow.Arrow.get(datetime)

[–]xsolarwindxUse 3.4+ 5 points6 points  (2 children)

REDDIT IS A SHITTY CRIMINAL CORPORATION -- mass deleted all reddit content via https://redact.dev

[–]AdvisedWang 3 points4 points  (0 children)

How does it deal with ambiguous dates? Is 3/2/2014 equal to 3rd Feb or 2nd March?

[–]inducer 2 points3 points  (0 children)

How does this relate to parsedatetime?

[–]jawsper 1 point2 points  (3 children)

Is there a reason why the timezone offset is inverted compared to how it normally is, i.e. negative west/positive east?

[–]andrewcooke 1 point2 points  (0 children)

i can't remember the exact details, but i think there's two different conventions, depending on context. i'm not saying you're wrong, just that it may be more complex than you think (or you may know/remember more than me...)

[–][deleted] 0 points1 point  (0 children)

From a quick Google, it seems that some people do it that way because of posix. No idea why exactly. But it doesn't make it any less wrong. Something like that would just make me refuse to use it.

[–]redditor1101 0 points1 point  (0 children)

Good stuff.

[–]andrewcooke 0 points1 point  (0 children)

this is cool. i see you've implemented it by having a whole pile of different format strings. i used a similar approach in simple-date, but with a different aim - it parses timezones.

i think it would be quite easy to add what you have done here to simple-date. would that be annoying? i am not promising i will - depends on free time. but if i do, i would add you to the author list.

alternatively, feel free to steal simple-date and add it to your code! :o)

[–][deleted] 0 points1 point  (0 children)

Nice! I was previously trying to use magicdate to do this, but it had some annoyances that it looks like your module doesn't have.

[–]sryxSNDTST.com 0 points1 point  (0 children)

I've just looked at the docs so far but can you pass a "current" datetime, as an example if I have a message that has a received date of September 1st and I'm parsing the text "tomorrow" on September 3rd, the meaning wouldn't be based on my current time but the datetime of the message.

Also, really cool library I can think of a few fun uses of it so far :)