Seite 1 von 1

Was will mir der Interpreter sagen ?

Verfasst: Donnerstag 13. Oktober 2005, 13:32
von Gunnar
Hi,

ich habe hier mal den code meiner Klasse angehängt nd wollte mal fragen warum der Interpreter die Kommandos :

newroute = {'NET':'224.0.0.0','MASK':'240.0.0.0','GATEWAY':'192.168.10.10'}
add = 'add'
route(newroute, add)

mit diesem Abschluss kommentiert :

<__main__.route instance at 0x00E94CB0>

Es wird alles ausgeführt, wie ich es will aber er sagt mir immer, an welche Speicherstelle er die Instanz hingeklebt hat. :roll:

Code: Alles auswählen

# -*- coding: cp1252 -*-
"""
Klasse zum  Modifizieren der Routingtabelle unter Windows
zuletzt geändert : 13.10.2005
"""

from os import popen

class route:

    def __init__(self, routingdict, action):
        """
        Benötigt werden ein Dictonary mit den Routinginformationen zum löschen oder erstellen einer Route
        und eine Variable mit dem Inhalt : add oder del
        13.10.2005
        """

        self.routingdict = routingdict

        if action == "add":
            self._route_add()
        elif action == "del":
            self._route_delete()
        else:
            raise IOError("attribute 'action' is not defined well")
        
        
            
    def _routingdata_test(self):
        """
        Überprüft das Dictonary auf folgende Mindestangaben :
        z.B. newroute = {'NET':'192.168.0.0','MASK':'255.255.0.0','GATEWAY':'10.1.1.1'}
        13.10.2005
        """
        a, b, c = "NET","MASK","GATEWAY"
        a1 = a in self.routingdict
        b1 = b in self.routingdict
        c1 = c in self.routingdict
        if a1 and b1 and c1 == True:
            return True
        else:
            return False


    def _route_add(self):
        """
        Testet das Eingansdictonary mit der Funktion "routingdata_test()" und legt eine neue Route in der Routingtabelle an
        mögliche Keys im Dictionary : NET, MASK, GATEWAY, METRIC, INTERFACE ( mind. jedoch NET, MASK, GATEWAY )
        13.10.2005
        """
        
        #Testen der Eingangsdaten auf Vollständigkeit und Übernahme der Werte
        test = self._routingdata_test()
        if test == True:
            NET = self.routingdict['NET']
            MASK = self.routingdict['MASK']
            GATEWAY = self.routingdict['GATEWAY']
            if "METRIC" in self.routingdict:
                METRIC = self.routingdict['METRIC']
            else:
                METRIC = ""
            if "INTERFACE" in self.routingdict:
                INTERFACE = self.routingdict['INTERFACE']
            else:
                INTERFACE = ""
        else:
            return False

        #String für das Kommando an's Betriebssystem zusammenbasteln
        command = 'route add ' + NET + ' mask ' + MASK + ' ' + GATEWAY +' metric ' + METRIC + ' if ' + INTERFACE
        
        #Anlegen der Route
        popen(command, 'w')

Vielen Dank für eure Tipps und bitte nicht weinen, wenn ihr den code seht. Ich bin immer noch am Üben und Verstehen, wenn es um OOP und Python geht :lol:

Gruß Gunnar

Re: Was will mir der Interpreter sagen ?

Verfasst: Donnerstag 13. Oktober 2005, 13:39
von Joghurt
Gunnar hat geschrieben:route(newroute, add)
Das erzeugt eine neue Instanz der route-Klasse. Und da du diese nirgendwo abspeichert, und im interaktiven Modus bist, gibt der Interpreter das Ergebnis dieses Befehls aus. Und das ist nunmal eine neue route-Instanz. In einem Skript ist das zu vergleichen mit

Code: Alles auswählen

print route(newroute,add)

Verfasst: Donnerstag 13. Oktober 2005, 13:51
von Gunnar
Danke soweit,

tritt das nur im interaktiven Modus auf ? Klar, die Instanz wird immer erstellt, aber diese Meldung des Interpreters ?
Des weiteren frage ich mich, ob ich das so glücklich gelöst habe...
Diese Klasse soll sich innerhalb des Programms exklusiv um die Routen kümmern und jeder andere Programmteil soll einfach ein dictonary und eine commandvariable dazu einkippen...sofern er etwas geändert haben möchte.

Damit will ich alle Aktionen auf die Routingtabelle einkapseln, damit später nicht x Klassen daran rumpfuschen.
Sinnvoll ?

Wie gesagt aber erstmal danke für die Erklärung :)


P.S.
Die Klasse ist auch noch nicht komplett fertig ;)