Python Forum

Full Version: Indentation error... lol
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Getting an indentation error which is usually indicative of a syntax error ELSEWHERE in the code (a big weakness of Python).

Code:

import csv
import os
import sys
import PyGUITools as pg 
import sqlite3
rootpath = os.path.dirname(__file__)


class DBObject():  #args here are for inheritance
    
    def __init__(self,path):
        self.path = path

        self.conn = sqlite3.connect(self.path)
        self.c = self.conn.cursor()
        self.filename = os.path.basename(self.path)
        self.dbname = os.path.splitext(self.filename)[0]

    def CreateTable(self,tablename):

        sql = "CREATE TABLE [IF NOT EXISTS] [" + self.dbname + "]." + tablename + " (ID data_type PRIMARY KEY"
        #self.ExecuteSQL(sql)

    def ExecuteSQL(self,sql):
        c = self.c
        c.execute(sql)
        conn = self.conn
        conn.commit()
        
    def TableCount(self):
        sql =  "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name != 'android_metadata' AND name != 'sqlite_sequence';"
        #self.ExecuteSQL(sql)

    class TableDefs():

        def __init__(self,tablename):
            
        def Wipe(self):

        def Copy(self):

        def AddField(self):

        class Fields():

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

            def Count(self):

            def Add(name,type="text"):

            def Edit(self):

mypath = os.path.join(rootpath,"Test1.db")
db = DBObject(mypath)
db.CreateTable("MyTable")
P.S - if you comment out this block....

    def ExecuteSQL(self,sql):
        c = self.c
        c.execute(sql)
        conn = self.conn
        conn.commit()
It suddenly runs :) lol but python said it was an "indentation" error......
Can you post full error traceback message (can use error tags)? It usually tells on which line the error appeared, which makes it very easy to spot the mistake in majority of cases.
Could you copy and paste the full traceback?
there is clearly problem in lines 36-53
Hi all,
thanks for replies.


Here is output with all code un-commented:
[Running] python -u "\\Mac\Home\Desktop\PythonProj\CSVParsing.py"
'\\mac\Home\Desktop\PythonProj'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
  File "\\Mac\Home\Desktop\PythonProj\CSVParsing.py", line 38
    def Wipe(self):
    ^
IndentationError: expected an indented block

[Done] exited with code=1 in 0.087 seconds
(Apr-01-2020, 06:30 PM)buran Wrote: [ -> ]there is clearly problem in lines 36-53

Unless it is illegal to nest classes in Python i'm not sure how its so obvious.
All the code is properly indented and i see no syntactical issues anywhere.
(Apr-02-2020, 05:45 AM)ironfelix717 Wrote: [ -> ]Unless it is illegal to nest classes in Python i'm not sure how its so obvious.

lines 36-24:
        def __init__(self,tablename):
             
        def Wipe(self):
 
        def Copy(self):
 
        def AddField(self):
and

lines 49-53:
def Count(self):
 
def Add(name,type="text"):
 
def Edit(self):
all of these are at the same level of indentation. That is why it is so obvious...You have like 7 functions without body. After each of these lines it expects indented block of code.

And also even if not illegal you sholud not abuse nesting . A class nested 3 levels deep... All I have to say is Flat is better than nested.
Zen of python
(Apr-02-2020, 06:09 AM)buran Wrote: [ -> ]
(Apr-02-2020, 05:45 AM)ironfelix717 Wrote: [ -> ]Unless it is illegal to nest classes in Python i'm not sure how its so obvious.

lines 36-24:
        def __init__(self,tablename):
             
        def Wipe(self):
 
        def Copy(self):
 
        def AddField(self):
and

lines 49-53:
def Count(self):
 
def Add(name,type="text"):
 
def Edit(self):
all of these are at the same level of indentation. That is why it is so obvious...You have like 7 functions without body. After each of these lines it expects indented block of code.

And also even if not illegal you sholud not abuse nesting . A class nested 3 levels deep... All I have to say is Flat is better than nested.
Zen of python


They are at the same level of indentation for obvious reason: they are all methods of the same class object. I'm laying out the structure of my object. I'm not writing the methods yet. So, i can't have an empty function in python? That's an awesome feature.

Disagreed about flat vs nested. Perhaps that is the "pythonic" way, to me, life makes more sense when there's a hierarchy. That's evident not just in programming.

Cheers
For functions/methods without a body, use "pass" to satisfy the interpreter and avoid parsing errors.
(Apr-02-2020, 02:19 PM)ironfelix717 Wrote: [ -> ]So, i can't have an empty function in python?
def Count(self):
    pass
@stullis beat me at this