Jump to content

How to handle state ..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hei,

I have the following situation in which I cannot come up with a good way to define an object's state. This is an application designed to test another application, a website. The classes involved are Central, helper1, helper2 and so on. There are about 10 helper-classes, which specialize in different part of the website.
The Central-instance (call it cen) has the instances of all the helper classes. In the software, tests run cen and it runs the helper-instances. For the most part, cen takes care of its own status (of course). The status is essentially where on the website the browser (and thus the entire software) is.
But,
there are situations where the helpers have to set the status.
I thought I don't want to merely define an attribute and getter and setter for it. That would create more coupling. I guess I don't mind if the cen knows the helpers, nothing wrong with that but the other way around..
At first I thought, of course, enum would solve it. Python doesn't do those. There are various ways to implement them but I was thinking: is there a more simpler way? Perhaps another way which isn't anything like enums ?

Help is appreciated.
Thanks.

Edited by denarced, 12 November 2010 - 10:52 AM.
brainfart


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Can you please add some details to your question and some code demonstrating your problem? Now your question is too general and sounds like "good way to define an object's state". What's wrong with this method:
instance = YourClass()
instance.state = YourClass.STATE_ONE


#3
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
I actually solved this, sort of.
I noticed that it wasn't necessary to give other instances the possibility to change Central-instance's state. After that the problem was pretty much gone.

I created a dictionary which contains all the states which are allowed.
I created a method with which the state is set. It checks the argument against the before mentioned dictionary.
class Central(object):
    def __init__(self):
        self._allowed_states = {"stopped", "started"}
        
    def set_state(self, state):
        if state in self._allowed_states:
            self._state = state
        else:
            raise Exception("Illegal state.")


#4
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

Vladimir said:

instance = YourClass()
instance.state = YourClass.STATE_ONE

Internally, I think you're suggestion would work.
Why not, basic stuff.
But when used from outside, this'd be a suicide.
Not that I'm saying that that's what you were suggesting..

#5
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
I do not insist that my method is the most correct, but it's actually one of the best :D You can check full list and very good arguments for this method: What's the best way to implement an 'enum' in Python? - Stack Overflow

You code can be improved like this:
class Central(object):
    STATE_STARTED = 'started'
    STATE_STOPPED = 'stopped'

    def start(self):
        # some code
        self.state = Central.STATE_STARTED

    def stop(self):
        # some code
        self.state = Central.STATE_STOPPED


#6
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

Vladimir said:

You code can be improved like this:
class Central(object):

    STATE_STARTED = 'started'

    STATE_STOPPED = 'stopped'


    def start(self):

        # some code

        self.state = Central.STATE_STARTED


    def stop(self):

        # some code

        self.state = Central.STATE_STOPPED

Now this one looks solid.
I like it 'cause it
  • defines the states as class attributes
  • state is set with methods that have no arguments
I certainly was considering something like this myself.
(Can't remember why I didn't go with something like this :confused:)

I'm somewhat new to python but everytime I do things the same way I did them in C, I get suspicious :). I keep thinking that Python probably has a much better way and I'm being an idiot. And a lot of the time it has been true, Python is pretty fantastic in its freedom.