Continually check object status

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • futileissue@gmail.com

    Continually check object status

    Beginner, so please bare with me. I'm not sure what to call what it
    is I'm looking for.

    If I have an object class, let's call it "Creature":

    class Creature:
    def __init__(self, status):
    self.status = "happy"

    def change_status(s elf, new_status):
    self.status = new_status

    def print_status(se lf):
    print self.status

    I would like to be able to print out the Creature's status every 20
    seconds. Let's say I use a script like this:

    import time
    while True:
    time.sleep(20)
    Creature.print_ status()

    But, while cycling through printing the status, I would like to be
    able to update Creature.status to something new.

    I might be approaching this from the wrong direction entirely. Thanks
    for your input.
  • Gary Herron

    #2
    Re: Continually check object status

    futileissue@gma il.com wrote:
    Beginner, so please bare with me. I'm not sure what to call what it
    is I'm looking for.
    >
    If I have an object class, let's call it "Creature":
    >
    class Creature:
    def __init__(self, status):
    self.status = "happy"
    >
    def change_status(s elf, new_status):
    self.status = new_status
    >
    def print_status(se lf):
    print self.status
    >
    I would like to be able to print out the Creature's status every 20
    seconds. Let's say I use a script like this:
    >
    import time
    while True:
    time.sleep(20)
    Creature.print_ status()
    >
    But, while cycling through printing the status, I would like to be
    able to update Creature.status to something new.
    >
    To answer your question, we need to know from where you would derive the
    directions to change the status. For instance:
    * time based (random or periodically scheduled)
    * user mouse/keyboard action
    * some state external to the program (file content, socket data, phase
    of the moon, price of tea in China, ...)

    Each of those possibilities would require a substantially different
    approach.

    Gary Herron


    I might be approaching this from the wrong direction entirely. Thanks
    for your input.
    --

    >

    Comment

    • Diez B. Roggisch

      #3
      Re: Continually check object status

      futileissue@gma il.com schrieb:
      Beginner, so please bare with me. I'm not sure what to call what it
      is I'm looking for.
      >
      If I have an object class, let's call it "Creature":
      >
      class Creature:
      def __init__(self, status):
      self.status = "happy"
      >
      def change_status(s elf, new_status):
      self.status = new_status
      >
      def print_status(se lf):
      print self.status
      >
      I would like to be able to print out the Creature's status every 20
      seconds. Let's say I use a script like this:
      >
      import time
      while True:
      time.sleep(20)
      Creature.print_ status()
      >
      But, while cycling through printing the status, I would like to be
      able to update Creature.status to something new.
      >
      I might be approaching this from the wrong direction entirely. Thanks
      for your input.
      The "simple", yet possibly dangerous answer is: you need
      multi-threading. Multi-threading is a technique that allows several
      (quasi)-parallel paths of execution whilst sharing memory and objects
      inside that memory. The module in python to achieve this is called
      "threading" .

      However, concurrent programming is a very advanced topic, ridded with
      pitfalls for even experienced developers.

      There are other ways to solve the problem, commonly known as event-loops
      and timers. These are usually part of frameworks for e.g GUI-creation an
      such, but you can also roll your own if you like.

      So, the better answer might be a question: what do you ultimately want
      to achieve? Given the name of your class, Creature, I assume you are
      writing on some game or such. Depending on how you plan to do that, you
      might have a framwork providing you with the needed tools/library calls
      or whatever.

      Diez

      Comment

      • futileissue@gmail.com

        #4
        Re: Continually check object status

        On Aug 2, 12:58 pm, Gary Herron <gher...@island training.comwro te:
        futileis...@gma il.com wrote:
        Beginner, so please bare with me.  I'm not sure what to call what it
        is I'm looking for.
        >
        If I have an object class, let's call it "Creature":
        >
        class Creature:
            def __init__(self, status):
                self.status = "happy"
        >
            def change_status(s elf, new_status):
                self.status = new_status
        >
            def print_status(se lf):
                print self.status
        >
        I would like to be able to print out the Creature's status every 20
        seconds.  Let's say I use a script like this:
        >
        import time
        while True:
            time.sleep(20)
            Creature.print_ status()
        >
        But, while cycling through printing the status, I would like to be
        able to update Creature.status to something new.
        >
        To answer your question, we need to know from where you would derive the
        directions to change the status.  For instance:
          * time based (random or periodically scheduled)
          * user mouse/keyboard action
          * some state external to the program (file content, socket data, phase
        of the moon, price of tea in China, ...)
        >
        Each of those possibilities would require a substantially different
        approach.
        >
        Gary Herron
        >
        I might be approaching this from the wrong direction entirely.  Thanks
        for your input.
        --
        http://mail.python.org/mailman/listinfo/python-list
        >
        >
        I was thinking about it taking directions from a GTK event handler,
        such as a user selecting a button.

        Comment

        • futileissue@gmail.com

          #5
          Re: Continually check object status

          On Aug 2, 1:05 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
          futileis...@gma il.com schrieb:
          >
          >
          >
          Beginner, so please bare with me.  I'm not sure what to call what it
          is I'm looking for.
          >
          If I have an object class, let's call it "Creature":
          >
          class Creature:
              def __init__(self, status):
                  self.status = "happy"
          >
              def change_status(s elf, new_status):
                  self.status = new_status
          >
              def print_status(se lf):
                  print self.status
          >
          I would like to be able to print out the Creature's status every 20
          seconds.  Let's say I use a script like this:
          >
          import time
          while True:
              time.sleep(20)
              Creature.print_ status()
          >
          But, while cycling through printing the status, I would like to be
          able to update Creature.status to something new.
          >
          I might be approaching this from the wrong direction entirely.  Thanks
          for your input.
          >
          The "simple", yet possibly dangerous answer is: you need
          multi-threading. Multi-threading is a technique that allows several
          (quasi)-parallel paths of execution whilst sharing memory and objects
          inside that memory. The module in python to achieve this is called
          "threading" .
          >
          However, concurrent programming is a very advanced topic, ridded with
          pitfalls for even experienced developers.
          >
          There are other ways to solve the problem, commonly known as event-loops
          and timers. These are usually part of frameworks for e.g GUI-creation an
          such, but you can also roll your own if you like.
          >
          So, the better answer might be a question: what do you ultimately want
          to achieve? Given the name of your class, Creature, I assume you are
          writing on some game or such. Depending on how you plan to do that, you
          might have a framwork providing you with the needed tools/library calls
          or whatever.
          >
          Diez
          I was afraid that someone was going to mention threading. I have read
          about it before but not been able to do much with it.

          My ultimate goal is to create some sort of tamagotchi style virtual
          pet to interact with. Over time it gets hungry or bored, but the
          process can be fixed by a user "feeding" or "playing with" it. I
          wanted to take this opportunity to teach myself some PyGTK coding as
          well, but I thought that maybe I could build the creature object and
          looping in such a way that it would be possible to add a GUI to it
          later.

          Comment

          • Diez B. Roggisch

            #6
            Re: Continually check object status

            I was afraid that someone was going to mention threading. I have read
            about it before but not been able to do much with it.
            >
            My ultimate goal is to create some sort of tamagotchi style virtual
            pet to interact with. Over time it gets hungry or bored, but the
            process can be fixed by a user "feeding" or "playing with" it. I
            wanted to take this opportunity to teach myself some PyGTK coding as
            well, but I thought that maybe I could build the creature object and
            looping in such a way that it would be possible to add a GUI to it
            later.
            No, that's not possible. But when you use GTK, there are timer-functions
            that let you register a timer event which will then invoke a callback in
            which you can check whatever status you like.

            Diez

            Comment

            Working...