1
, 11
, 01101
, 11001
0
, 10
, 10100
, 01001
(
wird durch )
geschlossenBezahlung anfordern
Münzgeld eingeworfen werden)example_states.py
example_states.py
state
steamlit
-Skript bei jedem Aufruf neu ausgeführt wird, können wir die state variable so zwischen den Aufrufen speichernexample_states.py
import streamlit as st
import time
# Initialize state
if "state" not in st.session_state:
st.session_state["state"] = "state_start"
elif st.session_state["state"] == "state_start":
st.text("I'm in start state")
# A callback function triggers a rerun of the script
st.button("Go to state A!", type="primary", on_click= go_to_state_a)
st.button("Go to state B!", type="primary", on_click= go_to_state_b)
elif st.session_state["state"] == "state_a":
st.text("I'm in state A")
st.button("Go to exit state!", type="primary", on_click= go_to_state_exit)
elif st.session_state["state"] == "state_b":
st.text("I'm in state B")
time.sleep(3)
# With the rerun function we can rerun the script after a given time
go_to_state_exit()
st.rerun()
elif st.session_state["state"] == "state_exit":
st.text("I'm in exit state")
st.button("Restart!", type="primary", on_click= go_to_state_start)
state_b
wird nur ein Text angezeigt und nach 3 Sekunden wird der Zustand auf state_exit
gesetztgo_to_state_exit()
setzt den Zustand auf state_exit
st.rerun()
wird das Skript neu ausgeführtdef go_to_state_exit():
st.session_state["state"] = "state_exit"
...
elif st.session_state["state"] == "state_b":
st.text("I'm in state B")
time.sleep(3)
go_to_state_exit()
st.rerun()
streamlit
wird dabei automatisch das Skript neu ausgeführt)def go_to_state_exit():
st.session_state["state"] = "state_exit"
...
elif st.session_state["state"] == "state_a":
st.text("I'm in state A")
st.button("Go to exit state!", type="primary", on_click=go_to_state_exit)
def main_function(x, callback):
# Perform some operation
result = x * 2
# Call the callback function with the result
callback(result)
def callback_function_a(result):
print(f"The result is: {result}")
def callback_function_b(result):
print(f"THE RESULT IS: {result}!!!")
# Example usage
main_function(5, callback_function_a)
main_function(5, callback_function_b)
microcontroller_sm.c
#include <stdio.h>
#include "timing.h" //header that contains fictitious get_time_ms()
typedef enum State { ST_START, ST_STATE_A, ST_STATE_B, ST_STATE_EXIT } State;
State state = ST_START;
int start_wait_b;
void main(){
start_wait_b = get_time_ms();
while(1){
switch(state){
case ST_START:
printf("I'm in start state - Loading initial data\n");
state = ST_STATE_A;
break;
case ST_STATE_A:
printf("I'm in state A - Sending data somewhere\n");
state = ST_STATE_B;
start_wait_b = get_time_ms(); //Reset wait time for state B
break;
case ST_STATE_B:
printf("I'm in state B - Waiting without blocking\n");
if(get_time_ms() - start_wait_b > 30000){
state = ST_STATE_EXIT;
start_wait_b = get_time_ms();
}
break;
case ST_STATE_EXIT:
printf("I'm in exit state - exiting gracefully\n");
state = ST_START;
break;
}
}
}
streamlit
example_states.py
und verwenden Sie nur st.button
und st.text
cola_without_states.py
um ein realistischeres UI zu erstellencola_with_states_simple.py
cola_with_states.py
(als Beispiel für ein Behavioral-Design-Pattern)
state_pattern_document_naive.py
if
-Abfragen den Zustand des Dokuments abfragen und das Verhalten ändernpublish()
-Methode ändert das Verhalten des Dokuments in Abhängigkeit vom Zustand und dem Benutzer-Typclass Document:
def __init__(self):
self.state = "draft"
def publish(self, current_user):
if self.state == "draft":
if current_user.role == "user":
self.state = "moderation"
elif current_user.role == "admin":
self.state = "published"
elif self.state == "moderation":
if current_user.role == "admin":
self.state = "published"
elif self.state == "published":
pass # Do nothing.
state_pattern_document.py
from abc import ABC
class State(ABC):
@abstractmethod
def publish(self, document, current_user):
pass
class Draft(State):
def publish(self, document, current_user):
if current_user.role == "user":
document.state = Moderation()
elif current_user.role == "admin":
document.state = Published()
class Moderation(State):
def publish(self, document, current_user):
if current_user.role == "admin":
document.state = Published()
class Published(State):
def publish(self, document, current_user):
pass # Do nothing.
class Document:
def __init__(self):
self.state = Draft()
def publish(self, current_user):
#self == the current document!
self.state.publish(self, current_user)
```mermaid stateDiagram A : Auswahl anfordern B : Bezahlung anfordern C : Rückgeld herausgeben D : Ware ausgeben [*] -- > A : Systemstart A -- > B : Auswahl getätigt B -- > C : Abbrechen B -- > D : Summe erreicht C -- > A D -- > A