Skip to main content

Python DSL for writing PlantUML sequence diagram

Project description

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 example

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.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distributions

napkin-0.5.2-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

napkin-0.5.2-py2-none-any.whl (24.6 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: napkin-0.5.2.tar.gz
  • Upload date:
  • Size: 12.1 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/2.7.15

File hashes

Hashes for napkin-0.5.2.tar.gz
Algorithm Hash digest
SHA256 feca3b5a6557b3bc1eed7107feb5b4ab5e51fe3410aa4e1f701b21d972f27521
MD5 829e5e1fcccf8a78e8ab5e9140aa865c
BLAKE2b-256 516c11b96a5959ffe391639061723f76ed062bd5e7e56880663dce4f14789503

See more details on using hashes here.

File details

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

File metadata

  • Download URL: napkin-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 24.6 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/2.7.15

File hashes

Hashes for napkin-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ce46a4e164e0102aa0346d17780271c0556f9be43f1d2ecb1ea1a8983bcd4351
MD5 833bd5cf02e85e9ac5c5b31b433b59a2
BLAKE2b-256 7919d4347bd0af6d26b9f7fb5350c616e3f3162ceb8ac29bd92914325d0866bb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: napkin-0.5.2-py2-none-any.whl
  • Upload date:
  • Size: 24.6 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/2.7.15

File hashes

Hashes for napkin-0.5.2-py2-none-any.whl
Algorithm Hash digest
SHA256 37d61d2479fa644b397d2a80c492d1223a710479b19039313d5149f15540edc2
MD5 885216d3034ad9164f88f06af2af29de
BLAKE2b-256 fa8b6f018775c4fb9e5995405585eda469eee5e59dde7a066392d622a227d26b

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