Skip to main content

Python DSL for writing PlantUML sequence diagram

Project description

Build Status

Napkin

Napkin is a tool to "write" sequence diagrams effectively as Python code.

Motivation

The sequence diagrams are useful tool to capture the behavioural aspect of the design. PlantUML is a great tool to draw nice sequence diagrams with simple human readable plain text.

However, the syntax of PlantUML is hard to use when there are nested calls, where lifeline with multiple activation/deactivations are involved. Unfortunately, this situation is quite common in sequence diagram for S/W.

For example, consider the following common sequence diagram, which is from Figure 4.2, UML Distilled 3E: Figure 4.2, UML Distilled 3E

The PlainUML script for the diagram will be as follows:

@startuml
participant User
participant Order
participant OrderLine
participant Product
participant Customer

User -> Order : calculatePrice()
activate Order
Order -> OrderLine : calculatePrice()
activate OrderLine
OrderLine -> Product : getPrice(quantity:number)
OrderLine -> Customer : getDiscountedValue(Order)
activate Customer
Customer -> Order : getBaseValue()
activate Order
Customer <-- Order: value
deactivate Order
OrderLine <-- Customer: discountedValue
deactivate Customer
deactivate OrderLine
deactivate Order
@enduml

It is quite hard to follow especially as there are multiple level of nested actviation/deactivation.

What if we express the same thing as the following Python code ?

@napkin.seq_diagram()
def distributed_control(c):
    user = c.object('User')
    order = c.object('Order')
    orderLine = c.object('OrderLine')
    product = c.object('Product')
    customer = c.object('Customer')

    with user:
        with order.calculatePrice():
            with orderLine.calculatePrice():
                product.getPrice('quantity:number')
                with customer.getDiscountedValue(order):
                    order.getBaseValue().ret('value')
                    c.ret('discountedValue')

distributed_control is normal function accepting a context object, c to access APIs. The function defines objects and the control starts with user object, which then calls orderLine.calculatePrice(). Basically, the sequence diagram is expressed as "almost" normal python code.

There are several advantages in using Python instead of using other special syntax language:

  • Easy to write/maintain scripts for the correct diagrams
  • Many common mistakes are detected as normal Python error. For example, method call to an undefined object will be just normal Python error.(This can be even checked by IDE without running scripts).
  • Any Python editor can become sequence diagram editor
  • There can be many interesting usages by taking advantage of Python as general language. For example, we can build a library for patterns.

Installation

Install and update using pip

pip install -U napkin

Hello world

Write a simple script called hello.py as follows:

import napkin

@napkin.seq_diagram()
def hello_world(c):
    user = c.object('user')
    world = c.object('world')
    with user:
        world.hello()

Then, the following command will generate hello_world.uml:

$ napkin hello.py

More examples

See more examples

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

napkin-0.5.3.tar.gz (12.8 kB view details)

Uploaded Source

Built Distributions

napkin-0.5.3-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

napkin-0.5.3-py2-none-any.whl (15.9 kB view details)

Uploaded Python 2

File details

Details for the file napkin-0.5.3.tar.gz.

File metadata

  • Download URL: napkin-0.5.3.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for napkin-0.5.3.tar.gz
Algorithm Hash digest
SHA256 050b7d87ad6a002832c85e9039794b10f2d19eb05dc80f35986d0ce9281fe2e8
MD5 73589bee7817965f61e4a4ac15a4e798
BLAKE2b-256 3f5074e1e632fcca21beb71736bd35c89e0a4d5ca03960994b8761dfad583cec

See more details on using hashes here.

File details

Details for the file napkin-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: napkin-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for napkin-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8dfdbb92cfbde5890b0656c2f3f398874bfaa72b60d37210ab79605dcde33693
MD5 0242d19a87016ca887cf2a679482c8d8
BLAKE2b-256 8b81a543a65513ce861aee13d72cd92dafbb70a4dc3e1712c32fce72cef493a3

See more details on using hashes here.

File details

Details for the file napkin-0.5.3-py2-none-any.whl.

File metadata

  • Download URL: napkin-0.5.3-py2-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.5

File hashes

Hashes for napkin-0.5.3-py2-none-any.whl
Algorithm Hash digest
SHA256 c254ae924a2153067c2988980edfb900eafb481f10e941227a10313782f974b6
MD5 5f26be8e400e9f9570930572b7dd740d
BLAKE2b-256 878ec7dfb629626695eb5b78295f991985ffd4f1550e3be7e78568ab3a1b095e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page