royskatt
royskatt

Reputation: 1210

Python - Confused by class and instance variable

I tried to write a small wrapper script for the unix find-command. Something I messed up with the parameter passing. Could you give me a hint regarding my mistake?

The error message is

Traceback (most recent call last):
  File "frep.py", line 43, in <module>
    inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)
NameError: name 'self' is not defined

This is the code:

import os
import subprocess
import string
import sys


class Frep(object):

    extension = ""
    search_string =""
    commands = []
    rel_numbers = ""


    def get_params(self):
        Frep.no_of_params = len(sys.argv)
        if Frep.no_of_params == 4:
            Frep.extension = str(sys.argv[1])
            Frep.search_string = str(sys.argv[2])
            Frep.rel_numbers = str(sys.argv[3])
        else:
            print "Usage:   frep [FILE_EXTENSION] [SEARCH_STRING] [RELEASE_NUMBERS]"
            print "Example: frep sql my_search_string [5-6]"
            print " "
            sys.exit()


    def prep_shell_commands(self, ext, ss, reln):
        print ext
        tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '["+reln+"]*'", shell=True), '\n')
        #tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '[6-7]*'", shell=True), '\n')
        for d in tmp_folderlist:
            commands.append("find " + d + " -type f -name '*" + ext +"' -exec grep -il '" + ss +"' {} \;")
        print commands


    def exec_commands(self, c_list):
        for c in c_list:
            os.system(c)


inst = Frep()

inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)

exec_commands(self.commands)

Upvotes: 3

Views: 192

Answers (3)

PM 2Ring
PM 2Ring

Reputation: 55499

Here's a re-write of your code. This version's not perfect, but it shows how to define a class & how to use a class instance in the way that it's usually done in Python.

FWIW, it's probably better to validate input data before passing it into your class, but I guess it's ok here.

I haven't tested this code, since I don't have the required directories and files, but hopefully it doesn't contain any horrible errors. :)

#!/usr/bin/env python

import os
import subprocess
import sys

class Frep(object):
    def __init__(self, args):
        if len(args) != 3:
            self.usage()

        self.extension = args[0]
        self.search_string = args[1]
        self.rel_numbers = args[2]
        self.commands = []

    def usage(self):
        print "Usage:   frep FILE_EXTENSION SEARCH_STRING RELEASE_NUMBERS"
        print "Example: frep sql my_search_string [5-6]"
        print " "
        sys.exit()

    def prep_shell_commands(self):
        print self.extension
        cmd = "find /data/grep_dir -maxdepth 1 -type d -name '[" + self.rel_numbers + "]*'"
        tmp_folderlist = subprocess.check_output(cmd, shell=True).split('\n')

        for d in tmp_folderlist:
            cmd = "find " + d + " -type f -name '*" + self.extension + "' -exec grep -il '" + self.search_string + "' {} \;"
            self.commands.append(cmd)
        print self.commands

    def exec_commands(self):
        for cmd in self.commands:
            os.system(cmd)


def main():
    inst = Frep(sys.argv[1:])
    inst.prep_shell_commands()
    inst.exec_commands()


if __name__ == '__main__':
    main()

Upvotes: 3

amitchone
amitchone

Reputation: 1638

In the instance of Frep, where you list the following:

extension = ""
search_string =""
commands = []
rel_numbers = ""

To be able to properly access these variables you'd need to do the following:

class Frep(object):
    def__init__(self):
        self.extension = ""
        self.search_string = ""
        self.commands = []
        self.rel_numbers = ""

Then, when you refer to those variables within your class, you can use self.variablename.

To refer to them outside of the class, you would use Frep.extension.

Upvotes: 2

Nick Bailey
Nick Bailey

Reputation: 3162

You invoke an instance member by using the name of the instance of the object

inst.prep_shell_commands(inst.extension, inst.search_string, isnt.rel_numbers)

That being said, if your method is always going to be invoking instance variables, then you should rewrite your prep_shell_commands method so that it takes no parameters.

Upvotes: 4

Related Questions