3

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)
4
  • self only exists inside the methods which take it as a parameter. You're trying to use it elsewhere. Commented Apr 23, 2015 at 14:44
  • so how can I access the class variables of this instance? Commented Apr 23, 2015 at 14:47
  • You don't access class variables of the instance. You access class variables of the class. There are "class variables" and "instance variables". The extension name is a class variable and is accessed with Frep.extension. Commented Apr 23, 2015 at 14:50
  • BTW, i forgot to execute get_params() here Commented Apr 23, 2015 at 15:20

3 Answers 3

4

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.

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

Comments

3

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()

Comments

2

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.

2 Comments

You want Frep.extension, not Frep.extension(). It's not a method.
You can initialize attributes using the syntax used by the OP. However such variables are class attributes, which behave quite differently from instance variables A really good explanation of how they work can be found here toptal.com/python/….

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.