15

I have this class in my parser.py file

class HostInfo(object):
def __init__(self, host_id):
    self.osclass = []
    self.osmatch = []
    self.osfingerprint = []
    self.portused = []
    self.ports = []
    self.extraports = []
    self.tcpsequence = {}
    self.hostnames = []
    self.tcptssequence = {}
    self.ipidsequence = {}
    self.trace = {'port': '', 'proto': '', 'hop': []}
    self.status = {}
    self.address = []
    self.hostscript = []

    # Umit extension
    self.id = host_id
    self.comment = ''

    # XXX this structure it not being used yet.
    self.nmap_host = {
            'status': {'state': '', 'reason': ''},
            'smurf': {'responses': ''},
            'times': {'to': '', 'srtt': '', 'rttvar': ''},
            'hostscript': [],
            'distance': {'value': ''},
            'trace': {'port': '', 'proto': '', 'hop': []},
            'address': [],
            'hostnames': [],
            'ports': [],
            'uptime': {'seconds': '', 'lastboot': ''},
            'tcpsequence': {'index': '', 'values': '', 'class': ''},
            'tcptssequence': {'values': '', 'class': ''},
            'ipidsequence': {'values': '', 'class': ''},
            'os': {}
            }

after that it defined a function which trying to find an host id from a xml file

def get_id(self):
    try:
        return self._id
    except AttributeError:
        raise Exception("Id is not set yet.")

def set_id(self, host_id):
    try:
        self._id = int(host_id)
    except (TypeError, ValueError):
        raise Exception("Invalid id! It must represent an integer, "
                "received %r" % host_id)

Now i want to use call this get_id function from an another file.I tried so many time but it shows an error i.e. module can't be import

3
  • 1
    Does parser.py compile? Your indentation - as posted (please fix) - is incorrect, which would keep it from importing. Commented Jul 15, 2011 at 14:03
  • /have you imported from that file? Suppose get_id is in a file called A.py. In your main file, you should do from A import get_id Commented Jul 15, 2011 at 14:04
  • 2
    Don't use getter and setters in python code. It's considered bad practice. Use property() instead. Commented Jul 15, 2011 at 14:08

1 Answer 1

10
from parser import HostInfo

obj = HostInfo(<whatever host_id you need here>)
obj.get_id

this is the way, how are you actually doing it?

Sign up to request clarification or add additional context in comments.

2 Comments

Yeh, but now i am confused about that how to paas the argument to obj.get_id function?
@sharda PAL: if get_id is a method of your HostInfo class, then self = the instance of that class. So, in my example, self is "obj". I have a question for you: what's that object parameter? in that code you're not using it anywhere, so i guess the best definition for your class is '' class HostInfo: ''. You don't need to pass self to get_id, it's implicit.

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.