惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园_首页
H
Help Net Security
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
GbyAI
GbyAI
Scott Helme
Scott Helme
D
Docker
Hacker News: Ask HN
Hacker News: Ask HN
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
G
GRAHAM CLULEY
C
Cisco Blogs
The Hacker News
The Hacker News
F
Full Disclosure
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
G
Google Developers Blog
量子位
K
Kaspersky official blog
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
P
Palo Alto Networks Blog
H
Heimdal Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Schneier on Security
Schneier on Security
The Register - Security
The Register - Security
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
N
News and Events Feed by Topic
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
V
V2EX
爱范儿
爱范儿

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Bitly | bit.ly/3MI4Iz0 Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Bitly | bit.ly/31lKCYA Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW The Easiest Way to Debug Kubernetes Workloads Bitly | bit.ly/2PHVudx Cloud Native CI/CD with Tekton - Building Custom Tasks Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Deep Dive into Docker Internals - Union Filesystem Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Bitly | bit.ly/3ifZxYr Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Bitly | bit.ly/3beQPpy Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Ultimate Setup for Your Next Golang Project Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
Dictionary Dispatch Pattern in Python
Martin · 2023-02-02 · via Martin Heinz's Blog

Have you ever written a long chain of if/else statements or a huge match/case block, with all statements just matching against a list of values, and wondered how could you make it more concise and readable?

If so, then dictionary dispatch pattern might be a tool for you. With dictionary dispatch we can replace any block of conditionals with a simple lookup into Python's dict- here's how it works...

Using Lambda Functions

The whole idea of dictionary dispatch is that we run different functions based on the value of a variable, instead of using conditional statement for each of the values.

Without dictionary dispatch, we would have to use either if/else statements or match/case block like so:


x, y = 5, 3
operation = "add"

if operation == "add":
    print(x + y)
elif operation == "mul":
    print(x * y)

# ---------------

match operation:
    case "add":
        print(x + y)
    case "mul":
        print(x * y)

While this works fine for just a few ifs or cases, it can become verbose and unreadable with growing number of options.

Instead, we can do the following:


functions = {
    "add": lambda x, y: x + y,
    "mul": lambda x, y: x * y
}

print(functions["add"](5, 3))
# 8
print(functions["mul"](5, 3))
# 15

The simplest way to implement dictionary dispatch is using lambda functions. In this example we assign each lambda function to a key in a dictionary. We can then call the function by looking up the key names and optionally passing in parameters.

Using lambdas is suitable when your operations can be expressed with single line of code, however, in general using proper Python function is the way to go...

Using Proper Functions

lambda functions are nice for simple cases, but chances are that you will want to dispatch on functions that require more than one line of code:


def add(x, y):
    return x + y

def mul(x, y):
    return x * y

functions = {
    "add": add,
    "mul": mul,
}

print(functions["add"](5, 3))
# 8
print(functions["mul"](5, 3))
# 15

Only difference when using proper functions is that they have to be defined outside of dictionary, because Python doesn't allow for inline function definitions. While this may seem annoying and less readable, it - in my opinion - forces you to write cleaner and more testable code.

Default Result

In case you want to use this pattern to emulate match/case statements, then you should consider using default value for when dictionary key is not present:


from collections import defaultdict

cases = defaultdict(lambda *args: lambda *a: "Invalid option", {
    "add": add,
    "mul": mul,
})

print(cases["add"](5, 3))
# 8
print(cases["_"](5, 3))
# Invalid option

This snippet leverages defaultdict, who's first argument specifies the "default factory", which is a function that will be called when key is not found. You will notice that we used 2 lambda functions here - first is there to catch any number of arguments passed to it, and the second is there because we need to return a callable.

Passing Parameters

We've already seen in all the previous examples that passing arguments to the functions in the dictionary is very straightforward. However, what if you wanted to manipulate the arguments before passing them to a function?


def handle_event(e):
    print(f"Handling event in 'handler_event' with {e}")
    return e

def handle_other_event(e):
    print(f"Handling event in 'handle_other_event' with {e}")
    return e

# With lambda:
functions = {
    "event1": lambda arg: handle_event(arg["some-key"]),
    "event2": lambda arg: handle_other_event(arg["some-other-key"]),
}

event = {
    "some-key": "value",
    "some-other-key": "different value",
}

print(functions["event1"](event))
# Handling event in 'handler_event' with value
# value
print(functions["event2"](event))
# Handling event in 'handle_other_event' with different value
# different value

First option is to use lambda function, which allows us to - for example - lookup a specific key in the payload as shown above.

Another option is to use partial to "freeze" the arguments, that however requires you to have the argument/payload before defining the dictionary:


event = {
    "some-key": "value",
    "some-other-key": "different value",
}

functions = {
    "event1": partial(handle_event, event["some-key"]),
    "event2": partial(handle_other_event, event["some-other-key"]),
}

print(functions["event1"]())
# Handling event in 'handler_event' with value
# value
print(functions["event2"]())
# Handling event in 'handle_other_event' with different value
# different value

Real World

So far, we experimented only with hello-world-like code examples. There are many real world use cases for dictionary dispatch, so let's take a look at some of them:


# parse_args.py
import argparse

functions = {
    "add": add,
    "mul": mul,
}

parser = argparse.ArgumentParser()

parser.add_argument(
    "operation",
    choices=["add", "mul"],
    help="operation to perform (add, mul)",
)
parser.add_argument(
    "x",
    type=int,
    help="first number",
)
parser.add_argument(
    "y",
    type=int,
    help="second number",
)

args = parser.parse_args()
answer = functions.get(args.operation,)(args.x, args.y)

print(answer)

First one being parsing of CLI arguments. Here we use builtin argparse module to create a simple CLI application. The code here consists mostly of defining the dictionary and setting up 3 possible arguments to the CLI.

When this code is invoked from CLI we will get the following:


python parse_args.py
# usage: parse_args.py [-h] {add,mul} x y
# parse_args.py: error: the following arguments are required: operation, x, y

python parse_args.py add 1 2
# 8

python parse_args.py mul 5 3
# 15

If operation (add or mul) and 2 numeric arguments are specified, then the arguments get unpacked into args variable. These arguments along with the args.operation are then used when invoking the function from dictionary, result of which is then assigned to the answer variable.

Another practical example of using dictionary dispatch is reacting to many different incoming events - for example - from a webhook, such as pull request events from GitHub:


event = {
  "action": "opened",
  "pull_request": {
    "url": "https://octocoders.github.io/api/v3/repos/Codertocat/Hello-World/pulls/2",
    "id": 2,
    "state": "open",
    "locked": False,
    "title": "Update the README with new information.",
    "user": {
      "login": "Codertocat",
      "id": 4
    },
    "body": "This is a pretty simple change that we need to pull into master.",
    "sender": {
      "login": "Codertocat",
      "id": 4
    }
  }
}

GitHub pull request event can specify many different actions, e.g. assigned, edited, labeled, etc. Here we will implement dictionary dispatch for the 4 most common ones:


def opened(e):
    print(f"Processing with action 'opened': {e}")
    ...

def reopened(e):
    print(f"Processing with action 'reopened': {e}")
    ...

def closed(e):
    print(f"Processing with action 'closed': {e}")
    ...

def synchronize(e):
    print(f"Processing with action 'synchronize': {e}")
    ...

actions = {
    "opened": opened,
    "reopened": reopened,
    "closed": closed,
    "synchronize": synchronize,
}

actions[event["action"]](event)
# Processing with action 'opened': {'action': 'opened', 'pull_request': {...}, "body": "...", ... }

We define an individual function for each action type, so that we can handle each case separately. In this example we directly pass the whole payload to all of the functions, we could however, manipulate the event payload before passing it, as we've seen in earlier example.

Visitor Pattern

Finally, while simple dictionary is usually enough, if you require a more robust solution you could use Visitor Pattern instead:


class Visitor:
    def visit(self, action, payload):
        method_name = f"visit_{action}"
        m = getattr(self, method_name, None)
        if m is None:
            m = self.default_visit
        return m(payload)

    def default_visit(self, action):
        print("Default action...")


class GithubEvaluator(Visitor):

    def visit_opened(self, payload):
        print(f"Processing with action 'opened': {payload}")

    def visit_reopened(self, payload):
        print(f"Processing with action 'reopened': {payload}")


e = GithubEvaluator()
e.visit("opened", event)
# Processing with action 'opened': {'action': 'opened', 'pull_request': {...}, "body": "...", ... }

This pattern is implemented by first creating a Visitor parent class which has visit function. This function automatically invokes a function with name matching pattern visit_<ACTION>. These individual functions are then implemented by the child class - where each of them is essentially acts as one of the "keys" in "dictionary". To then use this pattern/class we simply invoke visit method and let the class decide which function to invoke.

Closing Thoughts

Avoiding conditionals is a sure way to keep things simple, that however doesn't mean that we should try to shoehorn dictionary dispatch into every piece of code that requires conditional block.

With that said, there are good use cases for this pattern, such as very long chains of conditional statements. You might also want to use it if - for whatever reason - you're stuck using version of Python that doesn't support match/case.

Additionally, the lookup dictionary can be dynamically changed, for example by adding keys or changing the values (functions), which is something that cannot be achieved with normal conditional statements.

Finally, even if you don't want to use dictionary (table) dispatch, it's good be familiar with it, because at some point you will most likely run into code that uses it. 😉