user623990
user623990

Reputation:

Python indentation confusion, using 4 space tabs

I've got this little script:

filters = []
pipes = []

check_environment()
config()
fix_syslog()
make_fifo()

def check_environment():
    # check python
    # check for syslog
    # check for mknod
    # check for root privileges

def config():
    accepted_filters = ['auth', 'authpriv', 'daemon', 'cron', 'ftp', 'lpr', \
    'kern', 'mail', 'news', 'syslog', 'user', 'uucp', 'local0', 'local1'    \
    'local2', 'local3', 'local4', 'local5', 'local6', 'local7']

    accepted_priorities = ['Emergency', 'Alert', 'Critical', 'Error',       \
    'Warning', 'Notice', 'Info', 'Debug']


    print "Entered configuration mode. Type 'help' for more information"
    loop = true

    while loop:
        command = input(">> ")

        # handle the "done" command
        if command == "done":
            accept = input("Are you sure you are done configuring? (yes/no) ")
            if accept == "yes":
                loop = false

        # handle the "help" command
        elif command == "help":
            print "help Displays this help message"
            print "new  Adds a filter to the temporary list"
            print "show List all current temporary filters"
            print "del  Remove a filter from the temporary list"
            print "done Commits to the filters and continues with install"

        # handles the "show" command
        elif command == "show":
            for x in filters:
                for y in pipes:
                    print filters.index(x), x, y

        # handles the "new" command
        elif command == "new":
            new_filter = input("Enter desired facility/priority (eg. kern.Error): ")

            separator = new_filter.index('.')
            if separator == -1:
                print "You've entered an invalid facility/priority. Please try again."
                return
            facility = new_filter[:separator]
            priority = new_filter[separator:]

            if facility in accepted_filters and priority in accepted_priorities:
                filters.append(new_filter)
            else:
                print "You've entered an invalid facility/priority. Please try again."
                return

            new_pipe = input("Enter desired target pipe (kernel_error_pipe): ")

            if new_pipe[0] != "|":
                new_pipe = "|" + new_pipe

            pipes.append(new_pipe)

        # handles the "del" command
        elif command == "del":
            print "Run 'show' to see which filters are available to delete."
            which_filter = input("Insert the number of the filter to delete: ")
            filters.remove(filters.index(which_filter))
            pipes.remove(pipes.index(which_filter))

        # all other cases
        else:
            print "Invalid command. Type 'help' to see a list of available commands"

def fix_syslog():
  # check over variables
  # backup to specified folder
  # write own file with comments

def make_fifo():
  # create pipe folder
  # create pipes

This might be bad code but I've just started debugging and it is my first contact with Python. I'm getting this error:

File "./install.py", line 31
    def config():
      ^
IndentationError: expected an indented block

Everything seems to be indented properly, and I've set kate to make tabs equal to 4 spaces. Why is it throwing an error? And are there any tips to avoid these in the future?

Upvotes: 0

Views: 521

Answers (3)

utdemir
utdemir

Reputation: 27216

In def check_environment():, you should specify some code, if you don't want it to do anything for now, use pass.

def check_environment():
    # check python
    # check for syslog
    # check for mknod
    # check for root privileges
    pass

def config():
    accepted_filters = ['auth', 'auth ....

And also, when creating a list, you don't need to use backslashes, because of the commas, just do:

accepted_filters = ['auth', 'authpriv', 'daemon', 'cron', 'ftp', 'lpr', 
    'kern', 'mail', 'news', 'syslog', 'user', 'uucp', 'local0', 'local1',    
    'local2', 'local3', 'local4', 'local5', 'local6', 'local7']

accepted_priorities = ['Emergency', 'Alert', 'Critical', 'Error',
    'Warning', 'Notice', 'Info', 'Debug']

It is not an error, but it is a bad practice.

Also, you forgot a comma after 'local1'(I assume you don't want to get "local1local2").

Upvotes: 3

Michael Markert
Michael Markert

Reputation: 4026

There's a block missing:

def check_environment():

make it

def check_environment():
    pass

And you will run into further errors because you call check_environment() (and config()) before it's defined.

Upvotes: 2

orlp
orlp

Reputation: 117681

An indented block of code is expected after a function definition:

def check_environment():
    # check python
    # check for syslog
    # check for mknod
    # check for root privileges

def config():
    # .. code

There are only comments and no code after the def check_environment():. This explains the error. If you want an empty function use this:

def check_environment():
    pass

Upvotes: 8

Related Questions