473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(self, new_status):
self.status = new_status

def print_status(self):
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.
Aug 2 '08 #1
5 1294
fu*********@gmail.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(self, new_status):
self.status = new_status

def print_status(self):
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
Aug 2 '08 #2
fu*********@gmail.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(self, new_status):
self.status = new_status

def print_status(self):
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
Aug 2 '08 #3
On Aug 2, 12:58*pm, Gary Herron <gher...@islandtraining.comwrote:
futileis...@gmail.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(self, new_status):
* * * * self.status = new_status
* * def print_status(self):
* * * * 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.
Aug 2 '08 #4
On Aug 2, 1:05*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
futileis...@gmail.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(self, new_status):
* * * * self.status = new_status
* * def print_status(self):
* * * * 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.
Aug 2 '08 #5
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
Aug 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: tabonni | last post by:
I want to check each button groups and save the checked value into a 2 dimensional array. But, it doesn't work. Could anyone tell me what's wrong with my code. My code is as follow: <html>...
2
by: FredC | last post by:
OS Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1...
1
by: gallaczmit | last post by:
Will this code give me a true view of a computer's status? All I am looking for is to see if the computer is reachable or not. My end goal is to get a list of IP addresses from a MS SQL Server,...
1
by: Vanessa | last post by:
Hi, I'm trying to select a printer in the system printers collection. I'm able to do this. However, if the printer is a network printer and happens that this printer is not on or not ready, I...
0
by: Fossie | last post by:
Hi, I need to check that someone signing up is listed in an xml file. I am using a customer membership provider for Access and trying to integrate the xml check into that. Am I on the right track?...
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
0
by: kevin bailey | last post by:
I have a framework working where I have multiple pages each checking the authentication status. Unauthorised users are redirected to a login page - otherwise the requested page is shown. ...
8
by: Alan | last post by:
Hello, I need to write a systemtray program to check every minute if a certain intranet page exists. Would "webBrowser.Navigate" and catching exceptions to it be a good choice in terms of load...
0
by: Edwin.Madari | last post by:
updated creature running in its own thread will get you started. try it foryourself, change sleep times per your need. import os, sys, threading, time class Creature: def __init__(self,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.