Live data from Hacker News

Solving Algorithmic Problems in Python with Pytest (2019)

adamj.eu

1–10 of 45 posts

Re: Solving Algorithmic Problems in Python with Pytest (2019)

#5
post #3

I see the point of pytest and the greatness of it. I like pytest but I like minimalism more. I dont find it a good showcase here: couldn't you have kept only the asserts and just run the script?

In that case, only the first fail will be reported and exit. Which is fair enough in some cases, but may be confusing for long sequences of tests.

(There’s the option to get that behaviour in pytest as well with a parameter. Also, pytest may run the tests in parallel, which may speed up the results)

Re: Solving Algorithmic Problems in Python with Pytest (2019)

#6
post #4

Test driven development is just such a good habit to get into. You trade just a little bit of ramp-up speed at the beginning of an implementation for a massive reduction in cognitive overhead.

I'd love to see how test driven development looks like for ML systems.

You write the test for your non-existent model, then write the model and try to train the model to 'pass' the test? Do you also train it on the test case or on other data only?

Re: Solving Algorithmic Problems in Python with Pytest (2019)

#9
How would you write tests for the following problem with random output?

Write a function biasedcoin(n,p) that takes the number of coin flips, n, and the probability of heads, p. It flips a biased coin n times, and returns the ratio of number of heads/number of tails. p is guaranteed to have two significant numbers eg. p = 0.60 or p = 0.74. You should use the random.randrange function to generate random numbers.

Example implementation

def biasedcoin(n,p):

    import randrange from random

    heads = tails = 0

    for i in range(n):

        if randrange(100) 

Re: Solving Algorithmic Problems in Python with Pytest (2019)

#10
post #6
post #4

Test driven development is just such a good habit to get into. You trade just a little bit of ramp-up speed at the beginning of an implementation for a massive reduction in cognitive overhead.

I'd love to see how test driven development looks like for ML systems. You write the test for your non-existent model, then write the model and try to train the model to 'pass' the test? Do you also train it on the test case or on other data only?

It's doable once you're out of pure experimentation and into the development phase at which test driven development can help.

Test that this ETL function expects a DataFrame with a given schema and returns one with a different (but also known) schema, even with all these edge cases in the filters and group-bys.

Test that the "train_classifier" method/function rejects negative penalisation parameters, returns an object of type X (a trained sklearn object say, or dictionary of weights that can be deserialised), fails loudly if you don't have enough samples from category Y etc.

Test that the predict method returns a probability as a float, a predicted class as an int, a DataFrame with metadata and headers, etc etc.

Post reply on HN