Let's take some time and learn about testing in #Python today.

To kick things off, you'll learn about the `doctest` module!

๐Ÿงต๐Ÿ๐Ÿ‘‡
What is `doctest`? #Python's `doctest` modules looks for pieces of text in docstrings that look like REPL sessions.

Next, it executes them and verifies that the output in the docstring matches when the code is actually run

Check out the docs: docs.python.org/3/library/doctโ€ฆ
Here is an example of a #Python function with a docstring that could be used by the `doctest` module to test that function:
You call the `doctest` module with #Python by using the `-m` flag with the Python executable

If you call `doctest` without any flags, it will only show output if there is an error in the test

If you pass `-v` to `doctest`, then you run it in verbose mode
Here are a couple of runs of that code above. The file was named `dtest.py` for this example.

You run it once without `-v` and then again with `-v`. Check out the output to see what happens
You can also run `doctest` within the module instead of calling the #Python executable directly.

To do that, `import doctest` and then run `testmod()`:
All of the examples in this thread come from my article on `doctest` over on @mousevspython here:

blog.pythonlibrary.org/2014/03/17/pytโ€ฆ
I hope you enjoyed learning about basic testing in #Python with `doctest`

Follow me for more great Python content!

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

8 Nov
Did you know you can watermark images with #Python?

Find out how using the #Pillow package in this mini-thread!

๐Ÿงต๐Ÿ๐Ÿ‘‡
If you want to add text, such as a URL, to an image, you can do that pretty easily with #Python and #pillow

The following code snippet is only 21 lines!
Here are the before and after photos of the Yequina lighthouse showing how to add a watermark with #Python
Read 5 tweets
8 Nov
Have you ever needed to kill โ˜ ๏ธ a subprocess in #Python?

Let's talk a little about that a little!

๐Ÿงต๐Ÿ๐Ÿ‘‡
I had a use-case where I would spin up a process using `subprocess` that I wanted to kill if it ran too long

One way to do that is to use #Python's `threading` module, which has a handy `Timer` class

In this example, the `Timer` class is set to die after 5 seconds
#Python's `subprocess` module has a `run()` function that includes a `timeout` parameter (starting in 3.5) that you can use as well.

Here's an example of how that would work:
Read 5 tweets
7 Nov
#Python 3 has had asynchronous capabilities since 3.4. So let's talk about that today!

๐Ÿงต๐Ÿ๐Ÿ‘‡
Your first stop when learning about async in #Python is the official documentation.

There you will learn about the following:

๐Ÿ The `asyncio` module
๐Ÿ The `async` and `await` keywords

docs.python.org/3/library/asynโ€ฆ
The #Python documentation has a nice "Hello World" example for async.

In this example, you see how to create an asynchronous function, sleep and print out some strings:
Read 10 tweets
7 Oct
I use a lot of #Python resource. Here are some of my favorite!

Yes, it's another thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
Mouse vs Python, which is my own blog, is a resource I use myself because I write it for my future self as well as for you.

I put what I am currently working on or neat code snippets on there that I think I will use

blog.pythonlibrary.org
If I need to know about a specific module in #Python, I still check the Python Module of the Week website by @doughellmann

pymotw.com/3/
Read 11 tweets
7 Oct
Concurrency is a hard topic. So today we're going to talk about multiprocessing in #Python

Join for me another fun mini-thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
The Pros of using `multiprocessing` in #Python (part 1 of 2)

๐Ÿ Processes use separate memory space
๐Ÿ Code can be more straightforward compared to threads
๐Ÿ Uses multiple CPUs / cores
๐Ÿ Avoids the Global Interpreter Lock (GIL)
The Pros of using `multiprocessing` in #Python (part 2 of 2)

๐Ÿ Child processes can be killed (unlike threads)
๐Ÿ The multiprocessing module has an interface similar to `threading.Thread`
๐Ÿ Good for CPU-bound processing (encryption, binary search, matrix multiplication)
Read 9 tweets
7 Oct
Do you know how to make a #Python class method act like an attribute?

You can do that by turning the method into a `property`

Learn how in this thread!

๐Ÿงต๐Ÿ‘‡๐Ÿ
The first step is to look at a regular #Python class.

The following `Person` class has a method called `full_name()`. To get the full name of the `Person`, you must call `full_name()`

Wouldn't it be nice if you could just use dot notation instead of calling it?
In #Python, you can turn the method into a `property` using the `@property` decorator

Once you do that, you can then use dot notation to access the `full_name()` method as if it were an instance attribute!
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(