14. ツール:Pyreverse#

pylint と同時にインストールされる pyrevese コマンドを使うことで、Pythonのpackageのクラス図やモジュールの関係図(パッケージ図)を生成できます。 pyrevese は、既定の設定ではgraphviz/dotなどを使ってこれらのクラス図/パッケージ図を作成しますが、PlantUMLを用いてこれららの 図を作成することができます。 PlantUMLの形式で図を作成するには、コマンドオプションに -o puml を追加します。

@startuml classes_pythonsudoku
set namespaceSeparator none
class "About" as pythonsudoku.gui.About {
}
class "Board" as pythonsudoku.board.Board {
  boardsize : int
  cellsize : tuple
  filename : NoneType
  numbers : list
  clear()
  load(filename)
  load_board(board)
  load_numbers(numbers, xxx_todo_changeme2)
  save(filename)
}
class "Create" as pythonsudoku.gui.Create {
  gui
  callback(widget, handicap)
}
class "FileSelection" as pythonsudoku.gui.FileSelection {
  callback_cancel(widget, data)
  callback_ok(widget, data)
}
class "Gui" as pythonsudoku.gui.Gui {
  hbox
  menu
  progress
  redo_button
  sudoku
  undo_button
  view
  callback(widget, function)
  create(handicap)
  delete_event(widget, event, data)
  finished()
  solve()
  update(change_history)
}
class "Handicap" as pythonsudoku.gui.Handicap {
  cb
  gui
  spin
  callback(widget, command)
  delete_event(widget, event, data)
}
class "History" as pythonsudoku.history.History {
  history : list
  history_position : int
  numbers
  forget()
  is_first_position()
  is_last_position()
  redo()
  remember()
  undo()
}
class "Image" as pythonsudoku.image.Image {
  boardsize
  cellsize
  draw : ImageDraw
  filename
  format : object
  im : Image
  numbers
  square_height
  square_width
  create()
  draw_board()
  draw_numbers()
  in_greyscale(colour)
  save()
}
class "Menu" as pythonsudoku.gui.Menu {
}
class "MessageError" as pythonsudoku.gui.MessageError {
  callback(widget, id)
}
class "MessageInfo" as pythonsudoku.gui.MessageInfo {
  callback(widget, id)
}
class "OpenWindow" as pythonsudoku.gui.OpenWindow {
  callback_ok(widget, data)
}
class "Options" as pythonsudoku.config.Options {
  load_defaults()
  load_user_cfg()
}
class "PDF" as pythonsudoku.pdf.PDF {
  boardsize
  c : Canvas
  cellsize
  filename
  numbers
  page_size
  draw_board()
  draw_filename()
  draw_numbers()
  draw_title()
}
class "Printer" as pythonsudoku.printer.Printer {
}
class "Progress" as pythonsudoku.gui.Progress {
}
class "SaveImageWindow" as pythonsudoku.gui.SaveImageWindow {
  callback_ok(widget, data)
}
class "SavePDFWindow" as pythonsudoku.gui.SavePDFWindow {
  callback_ok(widget, data)
}
class "SaveWindow" as pythonsudoku.gui.SaveWindow {
  callback_ok(widget, data)
}
class "SelectValue" as pythonsudoku.gui.SelectValue {
  board
  i
  j
  callback(widget, data)
  delete_event(widget, event, data)
}
class "Sudoku" as pythonsudoku.sudoku.Sudoku {
  are_holes()
  create(handicap)
  finished()
  from_board(board)
  give_numbers(solved, how_many)
  possible_values(j, i)
  solvable()
  solve()
  to_board()
}
class "SudokuView" as pythonsudoku.gui.SudokuView {
  area
  board
  gc
  height
  pangolayout
  square_height : int
  square_width : int
  view
  width
  area_expose(area, event)
  draw_board()
  draw_numbers()
  press(widget, event)
  widget()
}
class "Value" as pythonsudoku.board.Value {
  integer()
  string()
}
class "XLF" as pythonsudoku.xls.XLF {
  wb : Workbook
  xlsfilename
  add_WhatIs()
  add_board(board)
  create_ws(board)
  draw_numbers(board, ws, hoffset)
  hide_columns(ws, cfrom, cto)
  lock_cell(cell)
  solve_and_save(board, ws)
  unlock_cell(cell)
}
pythonsudoku.gui.Gui --|> pythonsudoku.history.History
pythonsudoku.gui.OpenWindow --|> pythonsudoku.gui.FileSelection
pythonsudoku.gui.SaveImageWindow --|> pythonsudoku.gui.FileSelection
pythonsudoku.gui.SavePDFWindow --|> pythonsudoku.gui.FileSelection
pythonsudoku.gui.SaveWindow --|> pythonsudoku.gui.FileSelection
pythonsudoku.history.History --|> pythonsudoku.board.Board
pythonsudoku.gui.Menu --* pythonsudoku.gui.Gui : menu
pythonsudoku.gui.Progress --* pythonsudoku.gui.Gui : progress
pythonsudoku.gui.Progress --* pythonsudoku.gui.Gui : progress
@enduml

図 14.1 a class diagram created by pyreverse for pythonsudoku package#

@startuml packages_pythonsudoku
set namespaceSeparator none
package "pythonsudoku" as pythonsudoku {
}
package "pythonsudoku.board" as pythonsudoku.board {
}
package "pythonsudoku.check_modules" as pythonsudoku.check_modules {
}
package "pythonsudoku.config" as pythonsudoku.config {
}
package "pythonsudoku.gui" as pythonsudoku.gui {
}
package "pythonsudoku.history" as pythonsudoku.history {
}
package "pythonsudoku.image" as pythonsudoku.image {
}
package "pythonsudoku.info" as pythonsudoku.info {
}
package "pythonsudoku.pdf" as pythonsudoku.pdf {
}
package "pythonsudoku.printer" as pythonsudoku.printer {
}
package "pythonsudoku.sudoku" as pythonsudoku.sudoku {
}
package "pythonsudoku.text" as pythonsudoku.text {
}
package "pythonsudoku.xls" as pythonsudoku.xls {
}
pythonsudoku --> pythonsudoku.board
pythonsudoku --> pythonsudoku.sudoku
pythonsudoku.gui --> pythonsudoku.board
pythonsudoku.gui --> pythonsudoku.check_modules
pythonsudoku.gui --> pythonsudoku.config
pythonsudoku.gui --> pythonsudoku.history
pythonsudoku.gui --> pythonsudoku.image
pythonsudoku.gui --> pythonsudoku.info
pythonsudoku.gui --> pythonsudoku.pdf
pythonsudoku.gui --> pythonsudoku.printer
pythonsudoku.gui --> pythonsudoku.sudoku
pythonsudoku.history --> pythonsudoku.board
pythonsudoku.image --> pythonsudoku.board
pythonsudoku.image --> pythonsudoku.config
pythonsudoku.pdf --> pythonsudoku.board
pythonsudoku.pdf --> pythonsudoku.config
pythonsudoku.printer --> pythonsudoku.config
pythonsudoku.printer --> pythonsudoku.pdf
pythonsudoku.sudoku --> pythonsudoku.board
pythonsudoku.text --> pythonsudoku.board
pythonsudoku.text --> pythonsudoku.config
pythonsudoku.text --> pythonsudoku.sudoku
pythonsudoku.xls --> pythonsudoku.board
pythonsudoku.xls --> pythonsudoku.config
pythonsudoku.xls --> pythonsudoku.info
pythonsudoku.xls --> pythonsudoku.sudoku
@enduml

図 14.2 a package diagram created by pyreverse for pythonsudoku package#

14.1. pyreserveのインストール#

prreserveはPyLintと一緒にインストールされます。PyLint はPIPでインストールするのが簡単です。

python3 -m pip install pylint

14.2. pyreverse コマンドの使い方#

pyreverseコマンのヘルプメッセージ

pyreverse

Base class providing common behaviour for pyreverse commands.

usage: pyreverse [options]

Create UML diagrams for classes and modules in <packages>.

optional arguments:
-h, --help#

show this help message and exit

--filter-mode <mode>, -f <mode>#

filter attributes and functions according to <mode>. Correct modes are : 'PUB_ONLY' filter all non public attributes [DEFAULT], equivalent to PRIVATE+SPECIAL_A 'ALL' no filter 'SPECIAL' filter Python special functions except constructor 'OTHER' filter protected and private attributes (default: PUB_ONLY)

--class <class>, -c <class>#

create a class diagram with all classes related to <class>; this uses by default the options -ASmy (default: [])

--show-ancestors <ancestor>, -a <ancestor>#

show <ancestor> generations of ancestor classes not in <projects> (default: None)

--all-ancestors, -A#

show all ancestors off all classes in <projects> (default: None)

--show-associated <association_level>, -s <association_level>#

show <association_level> levels of associated classes not in <projects> (default: None)

--all-associated, -S#
show recursively all associated off all associated classes

(default: None)

--show-builtin, -b#

include builtin objects in representation of classes (default: False)

--module-names <y or n>, -m <y or n>#

include module name in representation of classes (default: None)

--only-classnames, -k#

don't show attributes and methods in the class boxes; this disables -f values (default: False)

--output <format>, -o <format>#

create a *.<format> output file if format is available. Available formats are: dot, vcg, puml, plantuml, mmd, html. Any other format will be tried to create by means of the 'dot' command line tool, which requires a graphviz installation. (default: dot)

--colorized#

Use colored output. Classes/modules of the same package get the same color. (default: False)

--max-color-depth <depth>#

Use separate colors up to package depth of <depth> (default: 2)

--ignore <file[,file...]>#

Files or directories to be skipped. They should be base names, not paths. (default: ('CVS',))

--project <project name>, -p <project name>#

set the project name. (default: )

--output-directory <output_directory>, -d <output_directory>#

set the output directory path. (default: )