473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RE: Continually check object status

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, status):
self.status = status
self.state = 'run'

def start(self):
self.athread = threading.Threa d(target=self.p rint_status)
self.athread.st art()

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

def print_status(se lf):
while self.state == 'run':
print self.status
time.sleep(1)

def stop(self):
self.state = 'stop'
self.athread.jo in()

#main loop
c = Creature('happy ')
c.start()
time.sleep(3) #wait some time
c.change_status ('managing')
time.sleep(3) #wait some more time
c.change_status ('bye')
time.sleep(1)
c.stop()
concept would be similar with GUI as well

-----Original Message-----
From: py************* *************** *************** *******@python. org
[mailto:py****** *************** *************** **************@ python.org]
On Behalf Of fu*********@gma il.com
Sent: Saturday, August 02, 2008 1:54 PM
To: py*********@pyt hon.org
Subject: 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.
--
http://mail.python.org/mailman/listinfo/python-list
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 2 '08 #1
0 1316

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

Similar topics

15
2128
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> <body> <script language="javascript"> <!-- var temp = new Array(2) var status = new Array();
2
1133
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 Microsoft Visual C# .NET 69462-335-0000007-18707 Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3
1
30019
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, see if the IP is reachable(if yes I will assume the pc is operational), update the database with the result and generate a web page displaying the results. I am using port 135 because I have an all windows network and this port should be open...
1
18187
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 won't be able to know and I try to print a document I will get an error. Is there anyway that I can check the status for the printer that the user select is ready or not?
0
2133
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? The code below runs but it results in "Your account was not created. Please try again." It's obviously not picking up username and comparing it. code.vb ' MembershipProvider.CreateUser
4
4184
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 check to see if the status is in stopped or running mode. But that doesn't tell me if it is actually running. I need to know this so that if it happens I can programmatically start the same service on another machine.
0
1555
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. Now my question is just to get a little clarification. On the login page I create and object of type Auth
8
2144
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 on the PC? If the page exists, the program should load and display it. The program would run on a couple of thousand PCs and the idea is to have a very basic, one-to-many instant message.
5
1309
by: futileissue | last post by:
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):
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.