Class confusion

2013-01-09 Thread Rodrick Brown
How can I make a class that has methods with attributes and other
functions?

I see a lot of code


I'm reading the documentation to Redhat's Satellite software which has a
XMLRPC interface and wrote the following code to test the api.

I would like to extend this code to support methods with methods? I see
this done a lot in python code but I'm not sure how to accomplish something
like this?

i.e.

sc = SatelliteConnect()
sc.get_systemlist().get_systemid() ?
or
sc.get_systemlist().get_running_kernel()

How does one chain methods and attributes like this with classes?

import xmlrpclib
import os
import sys

class SatelliteConnect(object):

SATELLITE_URL = http://nebula.nydc.fxcorp.prv/rpc/api;
SATELLITE_LOGIN = os.environ['USER']
SATELLITE_PASS = os.environ.get('SATELLITE_PASS',None)

def __init__(self):
self.client = xmlrpclib.Server(self.SATELLITE_URL, verbose=0)
self._check_env('SATELLITE_PASS')
self.key = self.client.auth.login(self.SATELLITE_LOGIN,
self.SATELLITE_PASS)

def _check_env(self, env_var):
if not os.environ.get('SATELLITE_PASS'):
print({} error please set environment varible {} and
re-run script.format(sys.argv[0], env_var))

sys.exit(-1)

def get_runningkernel(self, sysid):
self.get_systemid('somehost')
kernel = self.client.system.getRunningKernel(self.key, sysid)
if kernel:

return kernel
else:
return None


def get_systemlist(self):
systemlist = self.client.system.listSystems(self.key)
return([ system.get('name') for system in systemlist ])

self.client.auth.logout(self.key)

def get_systemid(self, host):
systemlist = self.client.system.getId(self.key, host)
for system in systemlist:
return system.get('id')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class confusion

2013-01-09 Thread MRAB

On 2013-01-09 20:13, Rodrick Brown wrote:

How can I make a class that has methods with attributes and other
functions?
I see a lot of code


I'm reading the documentation to Redhat's Satellite software which has a
XMLRPC interface and wrote the following code to test the api.

I would like to extend this code to support methods with methods? I see
this done a lot in python code but I'm not sure how to accomplish
something like this?

i.e.

sc = SatelliteConnect()
sc.get_systemlist().get_systemid() ?
or
sc.get_systemlist().get_running_kernel()

How does one chain methods and attributes like this with classes?


[snip]
This:

sc.get_systemlist().get_systemid()

simply means that the method get_systemlist returns an instance of
some class (let's call it SystemList) which has a method
get_systemid.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Class confusion

2013-01-09 Thread Matt Jones
# Something like...

class SystemList(object):
   def get_systemid(self):
  return System Id: bleh

   def get_running_kernel(self):
  return Kernel: bleh


class SatelliteConnect(object):
   def get_systemlist(self):
  return SystemList()


# Now the code you wrote would work, only return those literals thought,
you'd want to do something meaningful inside of SystemList's methods.

*Matt Jones*


On Wed, Jan 9, 2013 at 3:28 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-01-09 20:13, Rodrick Brown wrote:

 How can I make a class that has methods with attributes and other
 functions?
 I see a lot of code


 I'm reading the documentation to Redhat's Satellite software which has a
 XMLRPC interface and wrote the following code to test the api.

 I would like to extend this code to support methods with methods? I see
 this done a lot in python code but I'm not sure how to accomplish
 something like this?

 i.e.

 sc = SatelliteConnect()
 sc.get_systemlist().get_**systemid() ?
 or
 sc.get_systemlist().get_**running_kernel()

 How does one chain methods and attributes like this with classes?

  [snip]
 This:

 sc.get_systemlist().get_**systemid()

 simply means that the method get_systemlist returns an instance of
 some class (let's call it SystemList) which has a method
 get_systemid.

 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class confusion

2013-01-09 Thread Rodrick Brown
On Wed, Jan 9, 2013 at 4:34 PM, Matt Jones matt.walker.jo...@gmail.comwrote:

 # Something like...

 class SystemList(object):
def get_systemid(self):
   return System Id: bleh

def get_running_kernel(self):
   return Kernel: bleh


 class SatelliteConnect(object):
def get_systemlist(self):
   return SystemList()


 # Now the code you wrote would work, only return those literals thought,
 you'd want to do something meaningful inside of SystemList's methods.


Thanks for the tip Matt, I had no idea it was so simple. :-)


 *Matt Jones*


 On Wed, Jan 9, 2013 at 3:28 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-01-09 20:13, Rodrick Brown wrote:

 How can I make a class that has methods with attributes and other
 functions?
 I see a lot of code


 I'm reading the documentation to Redhat's Satellite software which has a
 XMLRPC interface and wrote the following code to test the api.

 I would like to extend this code to support methods with methods? I see
 this done a lot in python code but I'm not sure how to accomplish
 something like this?

 i.e.

 sc = SatelliteConnect()
 sc.get_systemlist().get_**systemid() ?
 or
 sc.get_systemlist().get_**running_kernel()

 How does one chain methods and attributes like this with classes?

  [snip]
 This:

 sc.get_systemlist().get_**systemid()

 simply means that the method get_systemlist returns an instance of
 some class (let's call it SystemList) which has a method
 get_systemid.

 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list



 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class confusion

2013-01-09 Thread Rodrick Brown
Can anyone care to advise on the following? Based on the responses does
this look sufficient?

#!/opt/local/bin/python

class SystemList(object):
sysmap = { '1039' : 'nebula',
   '1040' : 'mercury'}

def __init__(self, sysid):
self.sysid = sysid

def get_sysname(self):
return self.sysmap[self.sysid]

class System(object):
def __init__(self):
pass

def get_hostname(self,sysid):
return  SystemList(sysid)

if __name__ == '__main__':
sc = System()

for sysid in ('1039','1040'):
print(sc.get_hostname(sysid).get_sysname())



On Wed, Jan 9, 2013 at 5:18 PM, Rodrick Brown rodrick.br...@gmail.comwrote:

 On Wed, Jan 9, 2013 at 4:34 PM, Matt Jones matt.walker.jo...@gmail.comwrote:

 # Something like...

 class SystemList(object):
def get_systemid(self):
   return System Id: bleh

def get_running_kernel(self):
   return Kernel: bleh


 class SatelliteConnect(object):
def get_systemlist(self):
   return SystemList()


 # Now the code you wrote would work, only return those literals thought,
 you'd want to do something meaningful inside of SystemList's methods.


 Thanks for the tip Matt, I had no idea it was so simple. :-)


  *Matt Jones*


 On Wed, Jan 9, 2013 at 3:28 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-01-09 20:13, Rodrick Brown wrote:

 How can I make a class that has methods with attributes and other
 functions?
 I see a lot of code


 I'm reading the documentation to Redhat's Satellite software which has a
 XMLRPC interface and wrote the following code to test the api.

 I would like to extend this code to support methods with methods? I see
 this done a lot in python code but I'm not sure how to accomplish
 something like this?

 i.e.

 sc = SatelliteConnect()
 sc.get_systemlist().get_**systemid() ?
 or
 sc.get_systemlist().get_**running_kernel()

 How does one chain methods and attributes like this with classes?

  [snip]
 This:

 sc.get_systemlist().get_**systemid()

 simply means that the method get_systemlist returns an instance of
 some class (let's call it SystemList) which has a method
 get_systemid.

 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list



 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class confusion

2013-01-09 Thread Matt Jones
Does this look sufficient for what?  You haven't actually told us what it
is you're trying to accomplish.  I gave you the how, you must supply the
why.

*Matt Jones*


On Wed, Jan 9, 2013 at 6:43 PM, Rodrick Brown rodrick.br...@gmail.comwrote:

 Can anyone care to advise on the following? Based on the responses does
 this look sufficient?

 #!/opt/local/bin/python

 class SystemList(object):
 sysmap = { '1039' : 'nebula',
'1040' : 'mercury'}

 def __init__(self, sysid):
 self.sysid = sysid

 def get_sysname(self):
 return self.sysmap[self.sysid]

 class System(object):
 def __init__(self):
 pass

 def get_hostname(self,sysid):
 return  SystemList(sysid)

 if __name__ == '__main__':
 sc = System()

 for sysid in ('1039','1040'):
 print(sc.get_hostname(sysid).get_sysname())



 On Wed, Jan 9, 2013 at 5:18 PM, Rodrick Brown rodrick.br...@gmail.comwrote:

 On Wed, Jan 9, 2013 at 4:34 PM, Matt Jones 
 matt.walker.jo...@gmail.comwrote:

 # Something like...

 class SystemList(object):
def get_systemid(self):
   return System Id: bleh

def get_running_kernel(self):
   return Kernel: bleh


 class SatelliteConnect(object):
def get_systemlist(self):
   return SystemList()


 # Now the code you wrote would work, only return those literals thought,
 you'd want to do something meaningful inside of SystemList's methods.


 Thanks for the tip Matt, I had no idea it was so simple. :-)


  *Matt Jones*


 On Wed, Jan 9, 2013 at 3:28 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-01-09 20:13, Rodrick Brown wrote:

 How can I make a class that has methods with attributes and other
 functions?
 I see a lot of code


 I'm reading the documentation to Redhat's Satellite software which has
 a
 XMLRPC interface and wrote the following code to test the api.

 I would like to extend this code to support methods with methods? I see
 this done a lot in python code but I'm not sure how to accomplish
 something like this?

 i.e.

 sc = SatelliteConnect()
 sc.get_systemlist().get_**systemid() ?
 or
 sc.get_systemlist().get_**running_kernel()

 How does one chain methods and attributes like this with classes?

  [snip]
 This:

 sc.get_systemlist().get_**systemid()

 simply means that the method get_systemlist returns an instance of
 some class (let's call it SystemList) which has a method
 get_systemid.

 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list



 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
http://mail.python.org/mailman/listinfo/python-list