[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Can I set up a Finite-State Machine using Python? I'm looking
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 22
Thread images: 3
File: iii.gif (4 KB, 374x172) Image search: [Google]
iii.gif
4 KB, 374x172
Can I set up a Finite-State Machine using Python?
I'm looking to input a string and have the program throw a boolean whereas True means the string is contained in the FSM.
>>
>>55072898
>Can I set up a Finite-State Machine using Python?
You have to implement it, but yes.
>>
>>55072898
FSMs are bad because they mutate state

The best program is one completely free of side-effects.

t. functional programmer
>>
>>55072930
That's a strawman of how functional programmers advocate state and immutable logic.
>>
>>55072930
Doesn't python have unchangeable states?
>>55072921
Ok, how? I know how to tell if a string is accepted IRL, i have no clue how to tell a program that.
>>
>>55072930
What if someone were to write a function that generates a new state-vector from a current state combined with an input? Would that tickle your fancy?

FSMs are one of the nicest, easy to reason about ways to implement a lot of stuff imho
>>
File: Python in a single image.jpg (372 KB, 1280x1920) Image search: [Google]
Python in a single image.jpg
372 KB, 1280x1920
Bumpan because I need to understand this.
>>
>>55072898
I don't really see the point of making your own FSM for string inputs when you already have implemented regexes in the language.
>>
>>55073093
Me too. Regex is the perfect solution for searching a substring within a string.
>>
>>55073093
I don't either. It's mandatory to take the test in 2 weeks though.
Even so, I didn't know I have regexes already implemented on Python.
>>
>>55072898

Super easy..

I don't know python, but here's a Ruby solution.
Python way should be pretty similar..

(For the "switch"-statement you would probably use if-else-statements in python..)

def number_in_FSM? (a)

# the state
state = "e0"

# ignore empty strings
return "fuck you!" if a.empty?

# get the elements as array
a = a.split

# check each element
a.each do |i|
# which state are we in ?
case state
when "e0"
if a[i] == 1
state = "e1"
elsif a[i] == 2
state = "e2"
else
return "not in the FSM, sorry.."
end

when "e1"
# some logic here
when "e2"
# some logic here
end
end

end



The principle is not difficult is it?

Of course you could also write a "state" class with the decision logic inside. This would be more robust, but a little bit verbose if the FSM is small. It depends on what you want with it..


By the way:
Google "state machine python", there are a lot of solutions out there..
>>
>>55073231

OK, I found this solution in the internet, it's a FSM for a traffic lights.
>http://stackoverflow.com/questions/2101961/python-state-machine-design#answer-2103398


It's pretty neat!

Damn, I have to learn me some Python someday..

# trafficLight.pystate

# define state machine
statemachine TrafficLight:
Red -> Green
Green -> Yellow
Yellow -> Red

# define some class level constants
Red.carsCanGo = False
Yellow.carsCanGo = True
Green.carsCanGo = True

Red.delay = wait(20)
Yellow.delay = wait(3)
Green.delay = wait(15)


And here's how to use it:

import statemachine
import trafficLight

tl = trafficLight.Red()
for i in range(6):
print tl, "GO" if tl.carsCanGo else "STOP"
tl.delay()
tl = tl.next_state()
>>
>>55073231
In python:
def numFSM(a):
state='e0'
if a == '':
print('nigger')
else :
a = a.split()
for i in a:
if state == 'e0':
if i == 1:
state = 'e1'
elif i == 2:
state = 'e2'
else:
print('not in the FSM you nigger')
elif state == 'e1':
pass
elif state == 'e2':
pass
>>
>>55073492

Thanks, short and beautiful.
>>
>>55073492
>>55073575
>short and beautiful.
That code is the exact opposite of beautiful.
>>
>>55072930
Remind me again why we allow Haskell fags to shit all over this board with their uneducated opinions?
>>
>>55073587
I don't think he was serious m8
>>
>>55073587

>That code is the exact opposite of beautiful.

It's not that bad.
I look at it and can immediately understand it.

If you put some comments in it it would pass as a "quick and dirty" solution..

Of course it would be better to have some kind of Enumeration of the states, but this depends on the size and purpose..
>>
File: Screenshot_2016-06-14-15-03-57.png (366 KB, 1080x1920) Image search: [Google]
Screenshot_2016-06-14-15-03-57.png
366 KB, 1080x1920
>>55072898
What is wrong with that picture?
>>
>>55073492
def numFSM(a):
state='e2'
if a == '':
print('[x] Error')
else :
a = a.split()
case0 = {i: 'e'+str(i) for i in range(0, len(a))}
#case1 = {#...}
#case2 = {#...}
while a :
for x in range(0, len(a)):
if state == 'e0':
state = case0[x]
print(state)
elif state == 'e1':
#state = case1[x]
print('case1')
elif state == 'e2':
#state = case2[x]
print('case2')

>>
>>55072898
You might want to split it up into checking for transitions and actually acting according to the transitions:

// some pseudo code

loop:

// check transitions
transition = notrans

if x: transition = xtrans

else if y: transition = ytrans

// change state

if transition = xtrans: doChangeStateX()

else if transition = ytrans: doChangeStateY()

else: doChangeStateDefault()
>>
>>55072930
You are retarded.
Thread replies: 22
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.