Saturday, September 3, 2022

Pattern Matching: Python vs. Scala

Target audience: Beginner
Estimated reading time: 3'   

Ever found yourself frustrated by those pesky chains of if and elif statements? Python's latest versions (3.10 and above) offer a remedy.
This article describes the structured pattern matching [ref 1] and how it relates to its definition and implementation in Scala.


Table of contents

Notes
  • Environments:  Python 3.10, Scala 2.13.2
  • To enhance the readability of the algorithm implementations, we have omitted non-essential code elements like error checking, comments, exceptions, validation of class and method arguments, scoping qualifiers, and import statements.

Overview

As a data engineer using Scala for data pre-processing and Python deep learning frameworks, I have always been interested in comparing the features of these two languages.

Python already supports a limited form of this through sequence unpacking assignments. There are two approaches to match a value or patterns in older version of Python (similar to switch/case idiom available in most programming languages):
  • Write a sequence of if/elif/else condition - action pairs.
  • Create a dictionary with key as condition and value as action.
Neither of these options are flexible and easy to maintain. It was a matter of time for Python to join quite a few programming languages in adopting structural pattern matching, in version 3.10 [ref 2].

This new feature is very similar to the pattern matching found in the Scala programming language. Would it be interesting to compare the semantic and extensibility of pattern matching in Python and Scala?

Pattern matching in Scala

Typed pattern matching has been part of the Scala programming language for the last 10 years [ref 3]. The purpose is to check a value against one or several values or patterns. This feature is more powerful than the switch statement found in most of programming language as it  can deconstructs a value or class instance into its constituent parts. 

In the following example the type of a status instance (inherited from trait Status) is matched against all possible types (Failure, Success and Unknown).

sealed trait Status

case class Failure(error: String) extends Status
case object Success extends Status
case object Unknown extends Status
  

def processStatus(status: Status): String = status match {
    case Failure(errorMsg) => errorMsg
    case Success => "Succeeded"
    case Unknown => "Undefined status"
}

Note that the set of types derived from Status is sealed (or restricted). Therefore the function processStatus does not need to handle undefined types (already checked by the compiler).


Python value-type pattern

This is the simplest construct for pattern matching. A value along with its type is checked against a give set of value.

from typing import Any
from enum import Enum

class EnumValue(Enum):
    SUCCESS = 1
    FAILURE = -1
    UNKNOWN = 0


def variable_matching(value: Any):
    match value:
        case 2.0:
            print(f'Input {value} is match as a float')
        case "3.5":
            print(f'Input {value} is match as a string')
        case EnumValue.SUCCESS:
            print(f'Success')
        case _:
            print(f'Failed to match {value}')


if __name__ == '__main__':
    variable_matching(3.5)              # Failed to match 3.5
    variable_matching("3.5")            # 3.5 is matched as a string
    variable_matching(EnumValue.FAILURE)   # Failed to match EnumValue.FAILURE


In the previous code snippet, the argument of the function variable_matching is checked against a set of values AND their types. The attempt to match the input against 3.5 failed because the type is incorrect.

The following truth table illustrates the basic matching algorithm:

Matched type

Matched value

Outcome

No

No

Failed

Yes

No

Failed

No

Yes

Failed

Yes

Yes

Succeed


What about more complex types?

Python mappings pattern

The previous section dealt with matching single value and types. What about more complex structures such as dictionaries.
The following code snippet illustrates the mechanism to match a pattern of key-value pairs.

from typing import Dict, Optional
#  Dict keys: 'name', 'status', 'role', 'bonus'


def mappings_matching(json_dict: Dict) -> Optional[Dict]:
    match json_dict:
       case {'name': 'Joan'}:
          json_dict['status'] = 'vacation'
          return json_dict
       case {'role': 'engineer', 'status': 'promoted'}:
          json_dict['bonus'] = True
          return json_dict
       case _:
          print(f'ERROR: {str(json_dict)} not supported')
          return None



if __name__ == '__main__':
  json_object = {
   'name': 'Joan', 'status': 'full-time', 'role': 'marketing director', 'bonus': False
  }
  print(mappings_matching(json_object))
  # {'name': 'Joan', 'status': 'vacation', 'role': 'marketing director', 'bonus': False}

  json_object = {
   'name': 'Frank', 'status': 'promoted', 'role': 'engineer', 'bonus': False
  }
  print(mappings_matching(json_object))
  # {'name': 'Frank', 'status': 'promoted', 'role': 'engineer', 'bonus': True}

  json_object = {
   'name': 'Frank', 'status': 'promoted', 'role': 'account manager', 'bonus': False
  }
  print(mappings_matching(json_object))
  # ERROR: {'name': 'Frank', 'status': 'promoted', 'role': 'account manager', 'bonus': False} not supported



The function mappings_matching attempts to match a single value Frank for key name then match values for two values, engineer and promoted for the respective keys, role and status.

Python class pattern

The previous section dealt with matching against built-in types. Let's look at custom types or classes that applies an operation such as adding, multiplying of two values.
First we defined a data class, Operator which is fully defined by two parameters:
  • name of the operator with type string
  • arguments, args, of the operator with a type tuple.
In order to succeed, the operation should be 
  • supported (name as key)
  • has exactly two arguments
These two condition defined the context of the pattern matching. The method Operator.__call__ generates the string representation of the operation op (args) (i.e,  + (4, 5)).
The method attempts to match 1) the name of the operator and 2) the number of arguments (which is expected to be 2 for addition and multiplication).

from typing import Any, AnyStr, Tuple
from dataclasses import dataclass


@dataclass
class Operator:
    name: AnyStr
    args: Tuple

    def __call__(self) -> AnyStr:
        match (self.name, len(self.args)):       # Minimum condition for matching
            case ('multiply', 2):
                value = self.args[0]*self.args[1]
                return f'{self.args[0]} x {self.args[1]} = {value}'
            case ('add', 2):
                value = self.args[0] + self.args[1]
                return f'{self.args[0]} + {self.args[1]} = {value}'
            case _:
                return "Undefined operation"

    def __str__(self):
        return f'{self.name}: {str(self.args)}'


if __name__ == '__main__':
operator = Operator(
"add", (3.5, 6.2))
print(operator) #
add: (3.5, 6.2)


Now let's match object of any type to perform the operation. The process follows two steps
  1. Match the type of input to Operator
  2. Match the attributes of the operator by invoking the method Operator.__call__ described above.

def object_matching(obj: Any) -> AnyStr:
    match obj:                                     # First match: Is an operator?
        case Operator('multiply', _):      # Second match: Are operator attributes valid?
            return obj()
        case Operator(_, _):
            return obj()
        case _:
           return f'Type not find {str(obj)}'


if __name__ == '__main__':
    operator = Operator("add", (3.5, 6.2))
    print(object_matching(operator))               # 3.5 + 6.2 = 9.7
    operator = Operator("multiply", (3, 2))
    print(object_matching(operator))               # 3 x 2 = 6
    operator = Operator("multiply", (1, 3, 9))
    print(object_matching(operator)) # Undefined operation
operator = Operator("divided", (3, 3)) print(object_matching(operator)) vvvv# Undefined operation print(object_matching(3.4)) b. # Type not find 3.4



This post illustrates some of the applications of the structural pattern matching feature introduced in version 3.10. There are many more patterns that worth exploring [4].

Thank you for reading this article. For more information ...

References




---------------------------
Patrick Nicolas has over 25 years of experience in software and data engineering, architecture design and end-to-end deployment and support with extensive knowledge in machine learning. 
He has been director of data engineering at Aideo Technologies since 2017 and he is the author of "Scala for Machine Learning" Packt Publishing ISBN 978-1-78712-238-3