1
1

More than 1 year has passed since last update.

StarUMLを使ってPythonのオブジェクトを生成する

Posted at

オブジェクト指向のソフトウェアモデラーでPythonのオブジェクトコードを生成する

  • UML(Unified Modeling Language/統一モデリング言語)はソフトウェア工学の分野で使われる
  • オブジェクト指向の分析・設計・モデリングに用いられる

環境

  • python : 3.11.0
  • StarUML : v5.1.0

UMLツール「 Star UML 」をインストールする

スクリーンショット 2023-01-24 22.15.40.png

オブジェクトを作成

スクリーンショット 2023-01-24 22.09.44.png

オブジェクトは以下の構造になっている

  • class(クラス名)
  • Attribute(属性)
  • Method(関数)

Extension Toolを使ってPythonコードをジェネレートする

generate.png

生成されたオブジェクト

Server.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

class Server:
    def __init__(self):
        self.id = None
        self.name = None
        self.port = None
        self.ip_addr = None

    def login(self, ):
        pass

    def ssh(self, ):
        pass

    def ping(self, ):
        pass

    def instance(self, ):
        pass

ポイント

  • 属性が変数として宣言されている
  • 動作がメソッドとして宣言されている

メソッドの修正とクラスのインスタンス化

Server.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ping3 import ping


class Server:
    def __init__(self):
        self.id = None
        self.name = None
        self.port = None
        self.ip_addr = None

    def login(self, ):
        pass

    def ssh(self, ):
        pass

    def ping(self, targetIp):
        ping_rep = ping(targetIp, timeout=0.5, unit='ms', ttl=64)
        if ping_rep is None or ping_rep is False:
            print(f'Host: {targetIp:15} is NOT reachable')
        else:
            print(f'Host: {targetIp:15} is reachable    :{ping_rep:.2f}(ms)')

    def instance(self, name):
        print(name)


if __name__ == ('__main__'):
    svr = Server()
    svr.name = "Edge Server"
    svr.instance(svr.name)

    svr.ping("172.22.0.1")

if_name__ = 'main' を使って実行してみる

$ python ./Server.py

Edge Server
Host: 172.22.0.1      is reachable    :6.14(ms)

オブジェクトの関係を視覚的に確認できて便利!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1