Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Python Automation Cookbook
  • Table Of Contents Toc
Python Automation Cookbook

Python Automation Cookbook - Third Edition

By : Jaime Buelta
close
close
Python Automation Cookbook

Python Automation Cookbook

By: Jaime Buelta

Overview of this book

Automating repetitive tasks and integrating systems efficiently becomes increasingly complex as workflows scale. This book helps you solve that problem with practical Python recipes that guide you from foundational automation to advanced, AI-powered workflows. You start by building a strong base in Python automation, exploring tested solutions for file handling, web scraping, APIs, testing, and system operations, and learning how to design reliable automation workflows. The cookbook approach enables you to quickly apply solutions to real problems while building a deeper understanding through hands-on practice. This third edition expands the scope of automation by introducing AI-powered capabilities. You learn how to call AI models within your scripts, use and implement the Model Context Protocol (MCP) for system integration, and design intelligent agents that automate decision-making processes. New chapters provide real-world examples of AI agents in business automation, helping you move beyond scripts to adaptive systems. This book combines practical knowledge with modern techniques to ensure you stay current with evolving automation practices. By the end of this book, you will be able to design, build, and extend Python automation workflows, including AI-driven solutions, to handle complex real-world tasks with confidence.
Table of Contents (20 chapters)
close
close
18
Other Books You May Enjoy
19
Index

Using a third-party tool—parse

While manually parsing data works very well for small strings, as seen in the previous recipe, it can be very laborious to tweak the exact formula to work with a variety of inputs. What if the input has an extra dash sometimes? Or if it has a variable length header depending on the size of one of the fields?

A more advanced option is to use regular expressions, as we'll see in the next recipe. But there's a great module in Python called parse (https://github.com/r1chardj0n3s/parse), which allows us to reverse format strings. It is a fantastic tool that's powerful, easy to use, and greatly improves the readability of code.

Getting ready

Add the parse module to the requirements.txt file in our virtual environment and reinstall the dependencies, as shown in the Creating a virtual environment recipe.

The requirements.txt file should look like this:

pendulum==3.2.0
requests== 2.32.5
parse==1.20.2

Then, reinstall the modules in the virtual environment:

$ uv pip install -r requirements.txt
Resolved 13 packages in 349ms
Prepared 1 package in 45ms
Installed 1 package in 4ms
 + parse==1.20.2

How to do it…

  1. Import the parse function:
    >>> from parse import parse
  2. Define the log to parse, in the same format as in the Extracting data from structured strings recipe:
    >>> LOG = '[2025-10-06T12:58:00.714611] - SALE - PRODUCT: 1345 - PRICE: $09.99'
  3. Analyze it and describe it as you would do when trying to print it, like this:
    >>> FORMAT = '[{date}] - SALE - PRODUCT: {product} - PRICE: ${price}'
  4. Run parse and check the results:
    >>> result = parse(FORMAT, LOG)
    >>> result
    <Result () {'date': '2025-10-06T12:58:00.714611', 'product': '1345', 'price': '09.99'}>
    >>> result['date']
    '2025-10-06T12:58:00.714611'
    >>> result['product']
    '1345'
    >>> result['price']
    '09.99'
  5. Note the results are all strings. Define the types to be parsed:
    >>> FORMAT = '[{date:ti}] - SALE - PRODUCT: {product:d} - PRICE: ${price:05.2f}'
  6. Parse once again, noticing that the types now match the defined ones:
    >>> result = parse(FORMAT, LOG)
    >>> result['date']
    datetime.datetime(2025, 10, 6, 12, 58, 0, 714611)
    >>> result['product']
    1345
    >>> result['price']
    9.99

Define a custom type for the price to avoid issues with the float type:

>>> from decimal import Decimal
>>> def price(string):
...   return Decimal(string)
...
>>> FORMAT = '[{date:ti}] - SALE - PRODUCT: {product:d} - PRICE: ${price:price}'
>>> parse(FORMAT, LOG, {'price': price})
<Result () {'date': datetime.datetime(2025, 10, 6, 12, 58, 0, 714611), 'product': 1345, 'price': Decimal('9.99')}>

Notice that now the price field is defined as Decimal.

How it works…

The parse module allows us to define a format, as a string, that reverses the format method when parsing values. A lot of the concepts that we discussed when creating strings apply here—put values in brackets, define the type after a colon, and so on.

By default, as seen initially, the values are parsed as strings. This is a good starting point when analyzing text. The values can be parsed into more useful native types, as shown next. Please note that while most of the parsing types are the same as the ones in the Python Format Specification mini-language, some others are available, such as ti for timestamps in ISO format.

Though we are using timestamp in this book in a more liberal way as a replacement for "Date and time," in the strictest sense, it should only be used for numeric formats, such as Unix timestamp or epoch, defined as the number of seconds since a particular time.

The usage of a timestamp that includes other formats is common anyway as it's a clear and understandable concept, but be sure to agree on formats when sharing information with others.

If native types are not enough, our own parsing can be defined, as demonstrated in the last step. Note that the definition of the price function gets a string and returns the proper format, which in this case a Decimal type.

All the issues about floats and price information described in the There's more section of the Extracting data from structured strings recipe apply here as well.

There's more…

The timestamp can also be translated into a pendulum object for consistency. Also, pendulum objects carry over time zone information. Adding the same structure as in the previous recipe gives the following object, which is capable of parsing logs:

import parse
from decimal import Decimal
import pendulum
class PriceLog(object):
  def __init__(self, timestamp, product_id, price):
    self.timestamp = timestamp
    self.product_id = product_id
    self.price = price
  def __repr__(self):
    return '<PriceLog ({}, {}, {})>'.format(self.timestamp,
                                            self.product_id,
                                            self.price)
  @classmethod
  def parse(cls, text_log):
    '''
    Parse from a text log with the format
    [<Timestamp>] - SALE - PRODUCT: <product id> - PRICE: $<price>     to a PriceLog object
   '''
    def price(string):
      return Decimal(string)
    def isodate(string):
      return pendulum.parse(string)

    FORMAT = ('[{timestamp:isodate}] - SALE - PRODUCT: {product:d} - '
              'PRICE: ${price:price}')
    formats = {'price': price, 'isodate': isodate}
    result = parse.parse(FORMAT, text_log, formats)
    return cls(timestamp=result['timestamp'],
               product_id=result['product'],
               price=result['price'])

So, parsing it returns similar results:

>>> log = '[2025-10-06T14:58:59.051545] - SALE - PRODUCT: 827 - PRICE: $22.25'
>>> PriceLog.parse(log)
<PriceLog (2025-10-06 14:58:59.051545+00:00, 827, 22.25)>

This code is contained in the GitHub file, https://github.com/PacktPublishing/Python-Automation-Cookbook-Third-Edition/blob/main/ch01/price_log.py

All supported parse types can be found in the documentation at https://github.com/r1chardj0n3s/parse#format-specification.

Typically, GenAI tries to use regexes, which we will see in the next two recipes, to parse values. That works in some cases, but it can overcomplicate things. We will see some of the problems of regexes later. Having an easier way to parse regular values is very valuable and should not be underestimated.

See also

  • The Extracting data from structured strings recipe, earlier in this chapter, to learn how to use simple processes to get information from text.
  • The next recipe, Introducing regular expressions, to learn how to detect and extract patterns from text.
  • The Going deeper into regular expressions recipe, later in this chapter, to further your knowledge of regular expressions.
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python Automation Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon