2

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)
6
  • -1: example is not even valid python code. Commented Dec 19, 2010 at 8:33
  • 2
    That's all bad code. Looks like you should read some basic introduction to Python before you try to use it. Commented Dec 19, 2010 at 8:40
  • Guys i just gave an example come on.. Commented Dec 19, 2010 at 8:42
  • 3
    Your example is meaningless. It's not valid code and doesn't even express what you're thinking, not even close. Commented Dec 19, 2010 at 8:43
  • Please see the edit and let me know if it fine Commented Dec 19, 2010 at 10:07

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

original class names doesn't make sense, they shouldn't be plural
@Paweł Prażak: This is true as well, I'll update. WS is also a crappy name. :)
Thanks this was elpful +1 for the explaination
3

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

2

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.

http://en.wikipedia.org/wiki/Has-a

2 Comments

But my point is that WS needs to have a reference to which Datacenter it belongs
@Rajeev Then you need to either explicitly describe in English what you're thinking, or else provide valid Python code that shows what you're thinking. Based on what you've provided we can't tell what your design is. The line 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.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.