Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36517

Class confusion

From Rodrick Brown <rodrick.brown@gmail.com>
Date 2013-01-09 15:13 -0500
Subject Class confusion
Newsgroups comp.lang.python
Message-ID <mailman.337.1357762451.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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')

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Class confusion Rodrick Brown <rodrick.brown@gmail.com> - 2013-01-09 15:13 -0500

csiph-web