#245: Fire up your Python time machine (and test some code)
Watch the live stream:
About the show
Sponsored by us:
Special guest: Juan Pedro Araque Espinosa (Youtube Chanel: Commit that Line)
Michael #1: State of the community (via Jet Brains)
- This report presents the combined results of the fifth annual Developer Ecosystem Survey conducted by JetBrains
- Not just Python, but all of us
- Python is more popular than Java in terms of overall usage, while Java is more popular than Python as a main language.
- The 5 fastest growing languages are Python, TypeScript, Kotlin, SQL, and Go.
- A majority of the respondents (71%) develop for web backend.
- Does fall into the trap of “Hi, I’m a CSS developer, nice to meet you” though
- Women are more likely than men to be involved in data analysis, machine learning, and UX/UI design or research.
- Women are less likely than men to be involved in infrastructure development and DevOps, system administration, or Deployment.
Brian #2: Cornell - record & replay mock server
- Suggested by Yael Mintz (and it’s her project)
- Introduction blog post
- “Cornell makes it dead simple, via its record and replay features to perform end-to-end testing in a fast and isolated testing environment.
- When your application integrates with multiple web-based services, end-to-end testing is crucial before deploying to production. Mocking is often a tedious task. It becomes even more tiresome when working with multiple APIs from multiple vendors.
- vcrpy is an awesome library that records and replays HTTP interactions for unit tests. Its output is saved to reusable "cassette" files.
- By wrapping vcrpy with Flask, Cornell provides a lightweight record and replay server that can be easily used during distributed system testing and simulate all HTTP traffic needed for your tests.”
Juanpe #3: Factory boy (with Pydantic by chance)
Factory_boyallows creating factories to generate objects that could be used as text fixtures
- Briefly mentioned in the past in episode 193
- A factory takes a base object and allows to very easily and naturally define default values for each field of the object.
- One can have many factories for the same object that could be used define different types of fixtures of the same object
- It works with ORM objects (Django, Mongo, SQLAlchemy…)
- If you have a project that uses Pydantic to define your objects, factory boy also supports Pydantic although it is not documented and does it by a side effect
- Internally factory boy generates a parameters dictionary that that is unpacked when constructing the model at hands. This works perfectly with pydantic and can be used to generate pydantic objects on the fly with the full power of factory boy
Michael #4: pyinstrument
- Call stack profiler for Python. Shows you why your code is slow!
- Instead of writing
python script.py, type
- Your script will run as normal, and at the end (or when you press
^C), Pyinstrument will output a colored summary showing where most of the time was spent.
- Async support! Pyinstrument now detects when an async task hits an await, and tracks time spent outside of the async context under this await.
- Pyinstrument also has a Python API. Just surround your code with Pyinstrument
- Nice middleware examples for Flask & Django
- RC2 planned for 2021-09-06
- official release is planned for 2021-10-04
- It is strongly encourage maintainers of third-party Python projects to prepare their projects for 3.10 compatibility during this phase
- Reminder of major changes:
- PEP 623 -- Deprecate and prepare for the removal of the wstr member in PyUnicodeObject.
- PEP 604 -- Allow writing union types as X | Y
- PEP 612 -- Parameter Specification Variables
- PEP 626 -- Precise line numbers for debugging and other tools.
- PEP 618 -- Add Optional Length-Checking To zip.
- bpo-12782: Parenthesized context managers are now officially allowed.
- PEP 632 -- Deprecate distutils module.
- PEP 613 -- Explicit Type Aliases
- PEP 634 -- Structural Pattern Matching: Specification
- PEP 635 -- Structural Pattern Matching: Motivation and Rationale
- PEP 636 -- Structural Pattern Matching: Tutorial
- PEP 644 -- Require OpenSSL 1.1.1 or newer
- PEP 624 -- Remove Py_UNICODE encoder APIs
- PEP 597 -- Add optional EncodingWarning
Juanpe #6: time-machine
- Time-machine mock datetime and time related calls globally noticeably faster than other well known tools like freezgun.
- The mocking is achieved by replacing the c-level calls by whatever value we want which means the library does not need to mock individual imports.
- Mocking datetime cannot be done with patch.object and needs to be patched everywhere it is used which can turn mocking everything into a tedious (and/or slow) process.
- Datetime methods (now, today, utcnow…) can be mocked by setting a frozen time or by letting the time tick since the mock call is made.
- It provides a simple context manager to use it as well as pytest fixture that makes using it very simple
from datetime import datetime import time_machine @time_machine.travel("2021-01-01 21:00") def test_in_the_past(): assert datetime.now() == datetime(2021, 1, 1, 21, 0) --------------------------------- # The time_machine fixture can also be used with pytest def test_in_the_past(time_machine): time_machine.move_to(datetime(2021, 1, 1, 21, 0)) assert datetime.now() == datetime(2021, 1, 1, 21, 0)
- Credit-card stealing malware found in official Python repository and Software downloaded 30,000 times from PyPI ransacked developers’ machines (via Joe Riedly)
- Flavors of TDD - Test & Code episode 162
- Working on tox and CI chapter of 2nd edition of pytest book, hoping that to be released within the next week.