Python: NameError: global name 'searcher' is not defined

Viewed 19

I'm pretty new to python and I'm trying to make a program for a class which should be able to run in the command line with argv attributes in order to find a certain target integer in a file of multiple integers. I am having issues with how my searcher class is being defined, was wondering where I've made a mistake, code is as follows:

import sys
import string
import math
import time

print('cmd entry:', sys.argv)



class searcher(object):
    size = ""

    def __init__(self, size):
        pass

    def get(self, f, target):
        if sys.agrv != null:
            f = open(sys.argv[1], 'r')
            target = sys.argv[2]
            print('target:', target)
        else:
            print('no file or target')
        pass

    def linearsearch(self, f, target, tic, toc, elapsed):
        tic = time.perf_counter()
        for line in f:
            if target in line:
                tok = time.perf_counter()
                elapsed = toc - tic
                return line,elapsed
        pass

    def binarysearch(self, f, target, tic, toc, elapsed, low, high):
        low = 0
        high = len(f) - 1
        mid = 0
    
        while low <= high:
    
            mid = (high + low) // 2
    
            # If t is greater, ignore left half
            if arr[mid] < target:
                low = mid + 1
    
            # If t is smaller, ignore right half
            elif arr[mid] > target:
                high = mid - 1
    
            # means t is present at mid
            else:
                return mid
    
        # If we reach here, then the element was not present
        return -1
        pass

    def output(self, line, elapsed):
        print('line:', line)
        print('elapsed:', elapsed)
        pass

    def main(args):
      s = searcher(1000)
      f = open(sys.argv[1], 'r')
      target = sys.argv[2]
      s.get(f, target)
      s.linearsearch(f, target, tic, toc, elapsed)
      s.binarysearch(f, target, tic, toc, elapsed, low, high)
      s.output(line, elapsed)

    if __name__ == '__main__':
        main(sys.argv)
0 Answers
Related