I am designing a class which produces information like cpu usage,disk usage,Mem usage,...... for 3 systems,lets say data centers which has many workstations and work stations has many PCs.So the CPU usage and the other parameters are required for all the 3 levels(Datacenters, Workstations, Pcs). Please suggest is the below class design correct or how it is to be designed
EDIT
class Datacenters:
def __init__(self,name,location,cpu,mem):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self):
return self.name,self.location ,self.cpu,self.mem
def getname(self):
return self.name
class WS(Datacenters):
def __init__(self,name,location,cpu,mem,obj):
#datacentername = Datacenters.__init__(self) #To which data center it is associated
obj.getname() #To which data center it is associated
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self,obj):
return self.name,self.location ,self.cpu,self.mem,obj.getname()
def getpcname(self):
return self.name
class Pcs(WS):
def __init__(self,name,location,cpu,mem,obj):
obj.getpcname() #To which WS it is associated
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
def getparam(self,obj):
return self.name,self.location ,self.cpu,self.mem,obj.getpcname()
a = Datacenters("dc1","Bl1",20,30)
print a.getparam()
b = WS("WS1","Bl1",20,30,a)
print b.getparam(a)
c = Pcs("PC1","Bl1",20,30,b)
print c.getparam(b)
-
-1: example is not even valid python code.Paulo Scardine– Paulo Scardine2010年12月19日 08:33:07 +00:00Commented Dec 19, 2010 at 8:33
-
2That's all bad code. Looks like you should read some basic introduction to Python before you try to use it.Chris Morgan– Chris Morgan2010年12月19日 08:40:28 +00:00Commented Dec 19, 2010 at 8:40
-
Guys i just gave an example come on..Rajeev– Rajeev2010年12月19日 08:42:10 +00:00Commented Dec 19, 2010 at 8:42
-
3Your example is meaningless. It's not valid code and doesn't even express what you're thinking, not even close.Falmarri– Falmarri2010年12月19日 08:43:58 +00:00Commented Dec 19, 2010 at 8:43
-
Please see the edit and let me know if it fineRajeev– Rajeev2010年12月19日 10:07:05 +00:00Commented Dec 19, 2010 at 10:07
4 Answers 4
Much of your code is pointless. I removed the lines that isn't needed:
class Datacenter:
def __init__(self,name,location,cpu,mem):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
class Workstation:
def __init__(self,name,location,cpu,mem,datacenter):
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
self.datacenter=datacenter
class Computer:
def __init__(self,name,location,cpu,mem,workstation):
# This line does nothing:
self.name=name
self.location=location
self.cpu=cpu
self.mem=mem
self.workstation=workstation
You don't need to write accessor methods in Python. Just access the attributes directly.
Also, the line obj.getname() did nothing.
And of course, as pointed out by others here, since when does a Datacenter have a CPU? And doesn't a Computer have the same location as a Workstation? Then it doesn't need the location. Probably your code should look like this:
class Datacenter:
def __init__(self,name,location):
self.name=name
self.location=location
class Workstation:
def __init__(self,name,location,datacenter):
self.name=name
self.location=location
self.datacenter=datacenter
class Computer:
def __init__(self,name,cpu,mem,workstation):
# This line does nothing:
self.name=name
self.cpu=cpu
self.mem=mem
self.workstation=workstation
def location(self):
return self.workstation.location
def datacenter(self):
return self.workstation.datacenter
3 Comments
Some suggestions:
- Use 4-spaces indentation
- Use properties instead of getters, provide single properties, not a single one for all parameters (because parameters might be added later or by subclassing). In order to use properties, derive the base class from
object(new style classes) - Use singular class names, "Datacenter" instead of "Datacenters", because a single instance doesn't represent multiple datacenters, right?
- Don't call parameters "obj"
- So, a datacenter has a CPU? And a workstation is a datacenter, and gets a datacenter as an argument? Whaaat?
These points were mostly about coding style. I can't really help you with your use case - hope you get the point that your class hierarchy is totally screwed?!
Comments
What does this even mean?
class WS(Datacentres):
name
location
cpu
mem
Your design sounds wrong anyway. If a Datacenter has workstations, why would workstation inheit from datacenter? A workstation isn't a datacenter.
2 Comments
class WS(Datacentres): is Python code to start declaration of a new class called WS that inherits from Datacentres. If it's meant to be interpreted another way, we need to be told how.You shouldn't use inheritance here in that way, since Pcs is not WS and WS is not Datacentres.
You can make a class UnitInfo which has all that fields (name, cpu...) and use the instances of that class in DataCentres, WS, Pcs, because there is "has" relationship here (and not "is").
Or, make a class called Unit (or Units, I don't understand why you use plural forms), and inherit from that class. Unit "has" "name, "cpu-usage" and so on. "Pc" is a Unit. Maybe Unit is not the appropriate name for that class though.