iNoob
iNoob

Reputation: 1395

Docopt and Classes

Ive gotten quite comfortable over the last year coding in python on an off, but I have stayed away from Classes (as in structuring my code in them) because I have not understood them.

I am now trying to get my head around what I need to change in my coding practices to take advantage of using Classes in all their glory.

I have been trying to use an example script I wrote and pipe that to a Class based version. Safe to say I am sucking bad and cant get my simple script to work. Im sure there are a myriad of this Im most likely doing incorrectly. I would really appreciate someone pointing them out to me.

I dont mind finger points and belly laughs too ^_^

Coder After (not working)

"""
Description:

This script is used to walk a directory and print out each filename and  directory including the full path.

Author: Name

Usage:
  DirLister.py (-d <directory>)
  DirLister.py -h | --help
  DirLister.py --version

Options:
  -d <directory>     The top level directory you want to list files and directories from.
  -h --help          Show this screen.
  --version          Show version.
  """

import os
from docopt import docopt

class walking:
    def __init__(self, directory):
        self.directory = arguments['-d']

    def walk(self, directory):
        for root, dirs, files in os.walk(self.directory):
            for filename in files:
            print os.path.join(root, filename)

if __name__ == '__main__':

    arguments = docopt(__doc__, version= '1.0.0')
    print arguments
    if arguments['-d'] is None:
        print __doc__
        exit(0)
    else:
        walking.walk(directory)

Original Non-Class Based Code (working)

"""
Description:

This script is used to walk a directory and print out each filename and     directory including the full path.

Author: Name

Usage:
  DirLister.py (-d <directory>)
  DirLister.py -h | --help
  DirLister.py --version

Options:
  -d <directory>     The top level directory you want to list files and directories from.
  -h --help          Show this screen.
  --version          Show version.
  """

import os
from docopt import docopt

arguments = docopt(__doc__, version= '1.0.0')

def walk(dir):

   for root, dirs, files in os.walk(dir):
        for filename in files:
            print os.path.join(root, filename)

if __name__ == '__main__':

    if arguments['-d'] is None:
        print __doc__
        exit(0)
    else:
        walk(arguments['-d'])

Upvotes: 0

Views: 119

Answers (1)

Francis Colas
Francis Colas

Reputation: 3647

You've forgotten to post the error you get (since you say it's not working).

But indeed there are several issues. First, I'd call the class Walking.

Then in your __init__ function, you try to access arguments which is neither a global variable nor an argument; you wanted to write:

def __init__(self, directory):
    self.directory = directory

But you also need to create an instance of your class in you main:

walking = Walking(arguments['-d'])

That assumes that the name of the class is Walking instead of walking. I advise you to look at PEP8 for the naming conventions.

The general idea is that the class is the type of an object, but not the object itself*, so the class Walking: block is basically defining a new kind of objects. And then you can create objects that are instances of this class. It's the same when you create a list: mylist = list() (but there are also other ways for lists like mylist = [1, 2]).

*It happens that most things in Python are objects, including classes, but they have obviously other methods and they have another base class.

Upvotes: 1

Related Questions