Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a python class function from another file

Tags:

python

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

like image 244
Amit Pal Avatar asked Jul 15 '11 14:07

Amit Pal


People also ask

Can you call a function from a different Python file?

To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.

How do I import a function from one file to another in Python?

To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.

How do you call a function in Python?

To use functions in Python, you write the function name (or the variable that points to the function object) followed by parentheses (to call the function). If that function accepts arguments (as most functions do), then you'll pass the arguments inside the parentheses as you call the function.


1 Answers

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?

like image 81
Samuele Mattiuzzo Avatar answered Oct 24 '22 00:10

Samuele Mattiuzzo