Inconsistent use of space and tabs?

Posted on
Wed Jul 13, 2022 3:30 am
Bildhauer offline
Posts: 160
Joined: Sep 30, 2019
Location: 22113 Oststeinbek (near Hamburg), Germany

Inconsistent use of space and tabs?

Hello,

I'm getting the error message m. a. compiling this script:

Code: Select all
import datetime as dt
from datetime import date
today = dt.datetime.now()
yesterday = today - dt.timedelta(days=1)
now = dt.datetime.now()
BeginnLetzteStunde = now.hour-1
emailBody=""
if BeginnLetzteStunde == -1 :
   BeginnLetzteStunde =23
if BeginnLetzteStunde == 1 :
   emailBody = u"Datum\tUhrzeit\tVerbrauch [Wh]\n"
for dev in indigo.devices.iter("props.SupportsEnergyMeter") :
     if dev.enabled and dev.name == "Verbrauch Grenzweg stündlich" :
      import datetime as dt
from datetime import date
today = dt.datetime.now()
yesterday = today - dt.timedelta(days=1)
now = dt.datetime.now()
BeginnLetzteStunde = now.hour-1
emailBody=""
if BeginnLetzteStunde == -1 :
   BeginnLetzteStunde =23
if BeginnLetzteStunde == 1 :
   emailBody = u"Datum\tUhrzeit\tVerbrauch [Wh]\n"
for dev in indigo.devices.iter("props.SupportsEnergyMeter") :
     if dev.enabled and dev.name == "Verbrauch Grenzweg stündlich" :
      emailBody = emailBody + u"{}\t{}\t{}\n".format(today.strftime("%d.%m.%Y"), today.strftime("%H:%M"), round(dev.energyAccumTotal,2))
      

Str_heute=str(yesterday.strftime("%Y%m%d"))
import os.path
save_path = '/Users/smerckens/Desktop/Z-Wave Exporte/'
filename = Str_heute+'.Export des stündlichen Verbrauchs'
completeName = save_path+filename   
file1 = open(completeName , "a", encoding='utf-8')
file1.write(emailBody)
file1.close()
      

Str_heute=str(yesterday.strftime("%Y%m%d"))
import os.path
save_path = '/Users/smerckens/Desktop/Z-Wave Exporte/'
filename = Str_heute+'.Export des stündlichen Verbrauchs'
completeName = save_path+filename   
file1 = open(completeName , "a", encoding='utf-8')
file1.write(emailBody)
file1.close()


What do I need to of to get of it in line 14 ("emailBody = emailBody + u"{}\t{}\t{}\n".format(today.strftime("%d.%m.%Y"), today.strftime("%H:%M"), round(dev.energyAccumTotal,2))"?

Thanks in advance for your help!

Best regards

Bildhauer

Posted on
Wed Jul 13, 2022 4:58 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Inconsistent use of space and tabs?

The error you posted is telling you what the problem is -- an inconsistent use of spaces and tabs. In looking at the script you posted, I can see that some lines are indented with three spaces, some with five and some with six (well, 1 -- but I'll get to that in a sec). Also, you suggest the problem is with line 14, but the line you posted below the code is line 27 which is a bit confusing. You should make each indention the same 4 spaces. A second indention should be 8 spaces, and so on. Some of your indentions are 5 spaces followed by a 1 space indention.

Second, you don't need to import a library more than once in your script (and for a short script like this, there is no reason to do conditional imports). The convention is to import them at the top. I've removed the extras.

Third, it looks like you've repeated the same block of code twice at the end (that might be a copy/paste issue).

I did this quickly but I think it's right (so there might be an error).

Code: Select all
import datetime as dt
from datetime import date
import os.path

today = dt.datetime.now()
yesterday = today - dt.timedelta(days=1)
now = dt.datetime.now()
BeginnLetzteStunde = now.hour-1
emailBody=""
if BeginnLetzteStunde == -1 :
    BeginnLetzteStunde =23
if BeginnLetzteStunde == 1 :
    emailBody = u"Datum\tUhrzeit\tVerbrauch [Wh]\n"
# for dev in indigo.devices.iter("props.SupportsEnergyMeter") :
#     if dev.enabled and dev.name == "Verbrauch Grenzweg stündlich" :
#         import datetime as dt
# from datetime import date
today = dt.datetime.now()
yesterday = today - dt.timedelta(days=1)
now = dt.datetime.now()
BeginnLetzteStunde = now.hour-1
emailBody=""
if BeginnLetzteStunde == -1 :
    BeginnLetzteStunde =23
if BeginnLetzteStunde == 1 :
    emailBody = u"Datum\tUhrzeit\tVerbrauch [Wh]\n"
for dev in indigo.devices.iter("props.SupportsEnergyMeter") :
    if dev.enabled and dev.name == "Verbrauch Grenzweg stündlich" :
        emailBody = emailBody + u"{}\t{}\t{}\n".format(today.strftime("%d.%m.%Y"), today.strftime("%H:%M"), round(dev.energyAccumTotal,2))
     

Str_heute=str(yesterday.strftime("%Y%m%d"))
# import os.path
save_path = '/Users/smerckens/Desktop/Z-Wave Exporte/'
filename = Str_heute+'.Export des stündlichen Verbrauchs'
completeName = save_path+filename   
file1 = open(completeName , "a", encoding='utf-8')
file1.write(emailBody)
file1.close()
     

# Str_heute=str(yesterday.strftime("%Y%m%d"))
# import os.path
# save_path = '/Users/smerckens/Desktop/Z-Wave Exporte/'
# filename = Str_heute+'.Export des stündlichen Verbrauchs'
# completeName = save_path+filename   
# file1 = open(completeName , "a", encoding='utf-8')
# file1.write(emailBody)
# file1.close()

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Jul 13, 2022 5:07 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Inconsistent use of space and tabs?

There's repetition in the first block of code, too. I think this should be close:

Code: Select all
import datetime as dt
from datetime import date
import os.path

today = dt.datetime.now()
yesterday = today - dt.timedelta(days=1)
BeginnLetzteStunde = today.hour-1
emailBody=""
if BeginnLetzteStunde == -1 :
    BeginnLetzteStunde =23
if BeginnLetzteStunde == 1 :
    emailBody = u"Datum\tUhrzeit\tVerbrauch [Wh]\n"
for dev in indigo.devices.iter("props.SupportsEnergyMeter") :
    if dev.enabled and dev.name == "Verbrauch Grenzweg stündlich" :
        emailBody = emailBody + u"{}\t{}\t{}\n".format(today.strftime("%d.%m.%Y"), today.strftime("%H:%M"), round(dev.energyAccumTotal,2))
     
Str_heute=str(yesterday.strftime("%Y%m%d"))
save_path = '/Users/smerckens/Desktop/Z-Wave Exporte/'
filename = Str_heute+'.Export des stündlichen Verbrauchs'
completeName = save_path+filename   
file1 = open(completeName , "a", encoding='utf-8')
file1.write(emailBody)
file1.close()

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Jul 13, 2022 6:58 am
Bildhauer offline
Posts: 160
Joined: Sep 30, 2019
Location: 22113 Oststeinbek (near Hamburg), Germany

Re: Inconsistent use of space and tabs?

Thx. I will check …

Posted on
Thu Jul 14, 2022 11:15 am
Bildhauer offline
Posts: 160
Joined: Sep 30, 2019
Location: 22113 Oststeinbek (near Hamburg), Germany

Re: Inconsistent use of space and tabs?

Thx, it's doing its job!

Bildhauer

Posted on
Thu Jul 14, 2022 11:45 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Inconsistent use of space and tabs?

Good deal. Glad to hear that's working for you. A couple more notes to think about that are absolutely not necessary for the script to run, but I would recommend anyway:

1. The current convention for working with file objects is to use "with open". The "with" operation will close the file for you. I've updated the attached to reflect that.

2. If you're using Python 3, there is no longer a need to identify Unicode strings like u"string"; in Python 3, all strings are Unicode strings. I didn't change those.

3. You can add strings together using "+=" rather than emailBody = emailBody + "string"

Code: Select all
import datetime as dt
from datetime import date
import os.path

today = dt.datetime.now()
yesterday = today - dt.timedelta(days=1)
BeginnLetzteStunde = today.hour-1
emailBody = ""

if BeginnLetzteStunde == -1:
    BeginnLetzteStunde = 23
if BeginnLetzteStunde == 1:
    emailBody = u"Datum\tUhrzeit\tVerbrauch [Wh]\n"
for dev in indigo.devices.iter("props.SupportsEnergyMeter"):
    if dev.enabled and dev.name == "Verbrauch Grenzweg stündlich":
        emailBody += u"{}\t{}\t{}\n".format(today.strftime("%d.%m.%Y"), today.strftime("%H:%M"), round(dev.energyAccumTotal,2))
     
Str_heute = str(yesterday.strftime("%Y%m%d"))
save_path = '/Users/smerckens/Desktop/Z-Wave Exporte/'
filename = Str_heute + '.Export des stündlichen Verbrauchs'
completeName = save_path + filename
with open(completeName , "a", encoding='utf-8'):
    file1.write(emailBody)
Last edited by DaveL17 on Wed Aug 03, 2022 3:38 pm, edited 1 time in total.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Jul 14, 2022 3:00 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Inconsistent use of space and tabs?

While you're at it, you can delete these lines as they're not doing anything:

Code: Select all
from datetime import date
import os.path

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jul 17, 2022 10:39 am
Dual offline
Posts: 255
Joined: Feb 05, 2019

Re: Inconsistent use of space and tabs?

I enjoy reading the responses from you two. It’s like a mini tutorial. Thanks!


Sent from my iPhone using Tapatalk

Posted on
Wed Aug 03, 2022 1:09 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Inconsistent use of space and tabs?

Dave, for completeness you’ve a typo in your post above.

“wtih” not “with”.


Sent from my iPad using Tapatalk Pro

Posted on
Wed Aug 03, 2022 3:39 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Inconsistent use of space and tabs?

Pedantry! I love it. Fixed.

Thanks for the heads up.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest