# <a name="transitions-module"></a> transitions
[![Version](https://img.shields.io/badge/version-v0.9.3-orange.svg)](https://github.com/pytransitions/transitions)
[![Build Status](https://github.com/pytransitions/transitions/actions/workflows/pytest.yml/badge.svg)](https://github.com/pytransitions/transitions/actions?query=workflow%3Apytest)
[![Coverage Status](https://coveralls.io/repos/github/pytransitions/transitions/badge.svg?branch=master)](https://coveralls.io/github/pytransitions/transitions?branch=master)
[![PyPi](https://img.shields.io/pypi/v/transitions.svg)](https://pypi.org/project/transitions)
[![Copr](https://img.shields.io/badge/dynamic/json?color=blue&label=copr&query=builds.latest.source_package.version&url=https%3A%2F%2Fcopr.fedorainfracloud.org%2Fapi_3%2Fpackage%3Fownername%3Daleneum%26projectname%3Dpython-transitions%26packagename%3Dpython-transitions%26with_latest_build%3DTrue)](https://copr.fedorainfracloud.org/coprs/aleneum/python-transitions/)
[![GitHub commits](https://img.shields.io/github/commits-since/pytransitions/transitions/0.9.2.svg)](https://github.com/pytransitions/transitions/compare/0.9.2...master)
[![License](https://img.shields.io/github/license/pytransitions/transitions.svg)](LICENSE)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/pytransitions/transitions/master?filepath=examples%2FPlayground.ipynb)
<!-- [![Pylint](https://img.shields.io/badge/pylint-9.71%2F10-green.svg)](https://github.com/pytransitions/transitions) -->
<!--[![Name](Image)](Link)-->
A lightweight, object-oriented state machine implementation in Python with many extensions. Compatible with Python 2.7+ and 3.0+.
## Installation
pip install transitions
... or clone the repo from GitHub and then:
python setup.py install
## Table of Contents
- [Quickstart](#quickstart)
- [Non-Quickstart](#the-non-quickstart)
- [Some key concepts](#some-key-concepts)
- [Basic initialization](#basic-initialization)
- [States](#states)
- [Callbacks](#state-callbacks)
- [Checking state](#checking-state)
- [Enumerations](#enum-state)
- [Transitions](#transitions)
- [Triggering a transition](#triggers)
- [Automatic transitions](#automatic-transitions-for-all-states)
- [Transitioning from multiple states](#transitioning-from-multiple-states)
- [Reflexive transitions from multiple states](#reflexive-from-multiple-states)
- [Internal transitions](#internal-transitions)
- [Ordered transitions](#ordered-transitions)
- [Queued transitions](#queued-transitions)
- [Conditional transitions](#conditional-transitions)
- [Check transitions](#check-transitions)
- [Callbacks](#transition-callbacks)
- [Callable resolution](#resolution)
- [Callback execution order](#execution-order)
- [Passing data](#passing-data)
- [Alternative initialization patterns](#alternative-initialization-patterns)
- [Logging](#logging)
- [(Re-)Storing machine instances](#restoring)
- [Typing support](#typing-support)
- [Extensions](#extensions)
- [Hierarchical State Machine](#hsm)
- [Diagrams](#diagrams)
- [Threading](#threading)
- [Async](#async)
- [State features](#state-features)
- [Django](#django-support)
- [Bug reports etc.](#bug-reports)
## Quickstart
They say [a good example is worth](https://www.google.com/webhp?ie=UTF-8#q=%22a+good+example+is+worth%22&start=20) 100 pages of API documentation, a million directives, or a thousand words.
Well, "they" probably lie... but here's an example anyway:
```python
from transitions import Machine
import random
class NarcolepticSuperhero(object):
# Define some states. Most of the time, narcoleptic superheroes are just like
# everyone else. Except for...
states = ['asleep', 'hanging out', 'hungry', 'sweaty', 'saving the world']
def __init__(self, name):
# No anonymous superheroes on my watch! Every narcoleptic superhero gets
# a name. Any name at all. SleepyMan. SlumberGirl. You get the idea.
self.name = name
# What have we accomplished today?
self.kittens_rescued = 0
# Initialize the state machine
self.machine = Machine(model=self, states=NarcolepticSuperhero.states, initial='asleep')
# Add some transitions. We could also define these using a static list of
# dictionaries, as we did with states above, and then pass the list to
# the Machine initializer as the transitions= argument.
# At some point, every superhero must rise and shine.
self.machine.add_transition(trigger='wake_up', source='asleep', dest='hanging out')
# Superheroes need to keep in shape.
self.machine.add_transition('work_out', 'hanging out', 'hungry')
# Those calories won't replenish themselves!
self.machine.add_transition('eat', 'hungry', 'hanging out')
# Superheroes are always on call. ALWAYS. But they're not always
# dressed in work-appropriate clothing.
self.machine.add_transition('distress_call', '*', 'saving the world',
before='change_into_super_secret_costume')
# When they get off work, they're all sweaty and disgusting. But before
# they do anything else, they have to meticulously log their latest
# escapades. Because the legal department says so.
self.machine.add_transition('complete_mission', 'saving the world', 'sweaty',
after='update_journal')
# Sweat is a disorder that can be remedied with water.
# Unless you've had a particularly long day, in which case... bed time!
self.machine.add_transition('clean_up', 'sweaty', 'asleep', conditions=['is_exhausted'])
self.machine.add_transition('clean_up', 'sweaty', 'hanging out')
# Our NarcolepticSuperhero can fall asleep at pretty much any time.
self.machine.add_transition('nap', '*', 'asleep')
def update_journal(self):
""" Dear Diary, today I saved Mr. Whiskers. Again. """
self.kittens_rescued += 1
@property
def is_exhausted(self):
""" Basically a coin toss. """
return random.random() < 0.5
def change_into_super_secret_costume(self):
print("Beauty, eh?")
```
There, now you've baked a state machine into `NarcolepticSuperhero`. Let's take him/her/it out for a spin...
```python
>>> batman = NarcolepticSuperhero("Batman")
>>> batman.state
'asleep'
>>> batman.wake_up()
>>> batman.state
'hanging out'
>>> batman.nap()
>>> batman.state
'asleep'
>>> batman.clean_up()
MachineError: "Can't trigger event clean_up from state asleep!"
>>> batman.wake_up()
>>> batman.work_out()
>>> batman.state
'hungry'
# Batman still hasn't done anything useful...
>>> batman.kittens_rescued
0
# We now take you live to the scene of a horrific kitten entreement...
>>> batman.distress_call()
'Beauty, eh?'
>>> batman.state
'saving the world'
# Back to the crib.
>>> batman.complete_mission()
>>> batman.state
'sweaty'
>>> batman.clean_up()
>>> batman.state
'asleep' # Too tired to shower!
# Another productive day, Alfred.
>>> batman.kittens_rescued
1
```
While we cannot read the mind of the actual batman, we surely can visualize the current state of our `NarcolepticSuperhero`.
![batman diagram](https://user-images.githubusercontent.com/205986/104932302-c2f24580-59a7-11eb-8963-5dce738b9305.png)
Have a look at the [Diagrams](#diagrams) extensions if you want to know how.
## The non-quickstart
A state machine is a _model_ of behavior composed of a finite number of _states_ and _transitions_ between those states. Within each state and transition some _action_ can be performed. A state machine needs to start at some _initial state_. When using `transitions`, a state machine may consist of multiple objects where some (_machines_) contain definitions for the manipulation of other (_models_). Below, we will look at some core concepts and how to work with them
没有合适的资源?快使用搜索试试~ 我知道了~
transitions-Python轻量级、面向对象的有限状态机实现
共82个文件
py:39个
pyi:16个
txt:6个
1 下载量 62 浏览量
2024-10-06
20:38:09
上传
评论
收藏 1.12MB ZIP 举报
温馨提示
transitions-Python轻量级、面向对象的有限状态机实现
资源推荐
资源详情
资源评论
收起资源包目录
transitions-Python轻量级、面向对象的有限状态机实现.zip (82个子文件)
transitions-master
Changelog.md 32KB
noxfile.py 937B
setup.py 2KB
.coveragerc 49B
.github
ISSUE_TEMPLATE
support-request.md 1KB
feature_request.md 615B
bug_report.md 629B
workflows
pytest.yml 2KB
pytest.ini 125B
LICENSE 1KB
tests
utils.py 3KB
__init__.py 0B
test_imports.py 642B
test_codestyle.py 2KB
test_factory.py 2KB
test_mermaid.py 10KB
test_core.py 51KB
test_async.py 29KB
test_reuse.py 22KB
test_add_remove.py 3KB
test_parallel.py 11KB
test_markup.py 8KB
test_enum.py 14KB
test_pygraphviz.py 4KB
test_states.py 8KB
test_threading.py 11KB
test_graphviz.py 22KB
test_nesting.py 41KB
test_experimental.py 8KB
examples
Frequently asked questions.ipynb 39KB
Playground.ipynb 9KB
Graph MIxin Demo.ipynb 741KB
Graph MIxin Demo Nested.ipynb 583KB
mypy.ini 378B
requirements.txt 4B
MANIFEST.in 347B
.gitignore 221B
setup.cfg 134B
binder
apt.txt 9B
postBuild 35B
requirements.txt 13B
requirements_mypy.txt 24B
appveyor.yml 1KB
.pylintrc 15KB
README.md 97KB
transitions
__init__.py 554B
version.py 174B
experimental
utils.py 5KB
__init__.py 0B
utils.pyi 2KB
version.pyi 17B
core.pyi 11KB
__init__.pyi 264B
py.typed 1B
core.py 60KB
extensions
diagrams_pygraphviz.py 10KB
asyncio.py 33KB
__init__.py 836B
locking.py 8KB
diagrams_base.pyi 2KB
diagrams_graphviz.py 12KB
diagrams_graphviz.pyi 3KB
states.py 10KB
markup.pyi 4KB
nesting.py 60KB
__init__.pyi 689B
asyncio.pyi 9KB
diagrams.py 12KB
states.pyi 1KB
locking.pyi 3KB
factory.pyi 2KB
diagrams_mermaid.pyi 2KB
nesting.pyi 10KB
factory.py 4KB
diagrams_mermaid.py 11KB
diagrams.pyi 4KB
markup.py 12KB
diagrams_pygraphviz.pyi 2KB
diagrams_base.py 6KB
conftest.py 615B
requirements_test.txt 67B
requirements_diagrams.txt 20B
共 82 条
- 1
资源评论
Unity打怪升级
- 粉丝: 1w+
- 资源: 208
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功