Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!feeder1.cambriumusenet.nl!feed.tweaknews.nl!195.238.0.231.MISMATCH!news.skynet.be!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'api.': 0.04; 'sys': 0.05; 'attributes': 0.07; 'python': 0.09; 'def': 0.10; 'host)': 0.16; 're-run': 0.16; 'self.client': 0.16; 'skip:? 40': 0.16; 'xmlrpclib': 0.16; 'to:name:python-list@python.org': 0.20; 'import': 0.21; 'wrote': 0.26; 'extend': 0.26; 'i.e.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.27; 'this?': 0.28; 'environment': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'error': 0.30; 'code': 0.31; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'test': 0.36; 'does': 0.37; 'received:209': 0.37; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'satellite': 0.65; 'url:api': 0.84; 'subject:Class': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=N0anvRIyZLfpQFg7n4BtqpNApLlGCKMW7kjdnp/hC6I=; b=zehPDaH0dFX7UoLh5LYItQ7x3/2p0OiWLgcMQzRbls8jqqnzXHku+rJGCiKqsZuSv7 M+LLl6z7cemYTPGI7cP7TCeuXdt9TB+SjHtvubbgR8c2JAwGg7CP4UQsAa1GBDpXlyoB 4e2t+o6G+XmI+tsRh/bajKNueEu1WkX+F29bSZ8OmuwXosCXkt+AqMZI31HNc0RQRrTj p3E+EUzQ73kviXQUacKPqzuZbEDYv+RHpFbPYteI+ozri6Nfpmr7dYgQ4livGv+9GBbY 9KoY+1r2UnW5V5RKagZqG421r8DAHeT7+gmyzL3K1w4qM54YLOGpMOnDDLuwtTcC7YeW ABHg== MIME-Version: 1.0 From: Rodrick Brown Date: Wed, 9 Jan 2013 15:13:30 -0500 Subject: Class confusion To: "python-list@python.org" Content-Type: multipart/alternative; boundary=e89a8ff1cae0f9b46004d2e0b3ed X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 128 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357762451 news.xs4all.nl 6937 [2001:888:2000:d::a6]:57038 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36517 --e89a8ff1cae0f9b46004d2e0b3ed Content-Type: text/plain; charset=ISO-8859-1 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') --e89a8ff1cae0f9b46004d2e0b3ed Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
How can I make a class that has methods with attributes an= d other functions?=A0
=A0
I see a lot of code

<= /div>

I'm reading the documentation to Redhat&= #39;s Satellite software which has a XMLRPC interface and wrote the followi= ng code to test the api.=A0

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?=A0

i.e.=A0

sc =3D SatelliteCon= nect()
sc.get_systemlist().get_systemid() ?=A0
or=A0
sc.get_systemlist().get_running_kernel()=A0
=

How does one chain methods and attributes like this wi= th classes?=A0

import xmlrpclib
import os
import sys

class SatelliteConnec= t(object):
SATELLITE_URL =3D "http://nebula.nydc.fxcorp.prv/rpc/api"
SATELLITE_LOGIN = =3D os.environ['USER']
SATELLITE_PASS =3D os.environ.get(= 9;SATELLITE_PASS',None)

def __init__(self):
self.client =3D xmlrpclib.Server(sel= f.SATELLITE_URL, verbose=3D0)
self._check_env('SATELLITE_PAS= S')
self.key =3D self.client.auth.login(self.SATELLITE_LOGIN= , self.SATELLITE_PASS)

def _check_env(self, env_var):
if not os.environ.get(= 9;SATELLITE_PASS'):
print("{} error please set envi= ronment varible {} and re-run script".format(sys.argv[0], env_var)) sys.exit(-1)
    def get_r=
unningkernel(self, sysid):
        self.get_systemid('somehost')
        kernel =3D self.client.system.getRunningKernel(self.key, sysid)
        if kernel:=A0
            retu=
rn kernel
        else:
            return None

def ge= t_systemlist(self):
systemlist =3D self.client.system.listSystem= s(self.key)
return([ system.get('name') for system in sy= stemlist ])

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

def get_systemid(s= elf, host):
systemlist =3D self.client.system.getId(self.key, ho= st)
for system in systemlist:
return system.get(&= #39;id')


--e89a8ff1cae0f9b46004d2e0b3ed--