Python Pattern Matching Examples: ETL and Dataclasses
In I walked you through the theory of Structural Pattern Matching, so now it’s time to apply that knowledge and build something practical.
Let’s say you need to process data from one system (a JSON-based REST API) into another (a CSV file for use in Excel). A common task. Extracting, Transforming, and Loading (ETL) data is one of the things Python does especially well, and with pattern matching you can simplify and organize your business logic in such a way that it remains maintainable and understandable.
Let’s get some test data. For this you’ll need the requests library.
>>> resp = requests.get('https://demo.inspiredpython.com/invoices/')
>>> assert resp.ok
>>> data = resp.json()
>>> data[0]
{'recipient': {'company': 'Trommler',
'address': 'Annette-Döring-Allee 5\n01231 Grafenau',
'country_code': 'DE'},
'invoice_id': 15134,
'currency': 'JPY',
'amount': 945.57,
'sku': 'PROPANE-ACCESSORIES'}
Read More ->
from Planet Python
via read more
No comments:
Post a Comment