Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indentation error... lol
#1
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......
Reply
#2
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.
Reply
#3
Could you copy and paste the full traceback?
Reply
#4
there is clearly problem in lines 36-53
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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
Reply
#8
For functions/methods without a body, use "pass" to satisfy the interpreter and avoid parsing errors.
Reply
#9
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Indentation Error With If Else Statement JoeDainton123 3 1,830 Aug-02-2020, 08:29 PM
Last Post: deanhystad
  Getting error message for indentation Shafla 5 2,836 May-07-2019, 08:56 PM
Last Post: Yoriz
  Help please - indentation error bil007 1 2,048 Mar-24-2019, 11:41 AM
Last Post: Yoriz
  Very strange error of indentation samsonite 12 5,231 Mar-22-2019, 10:45 AM
Last Post: samsonite
  Indentation error in Python code ErnestTBass 5 3,574 Feb-28-2019, 04:26 PM
Last Post: samsonite
  Class - Error Message (Indentation) tkj80 3 4,494 Jan-20-2017, 07:48 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020