-1

I have a script which prints variables (set by user) perfectly.

os.system('clear')

print "Motion Detection Started"
print "------------------------"
print "Pixel Threshold (How much)   = " + str(threshold)
print "Sensitivity (changed Pixels) = " + str(sensitivity)
print "File Path for Image Save     = " + filepath
print "---------- Motion Capture File Activity --------------" 

I now wish to email this code to myself to confirm when running. I have included in the script email using email.mimieText and multipart. But the output no longer shows the relative variables just the code.

    body =  """ Motion Detection Started \n Pixel Threshold (How much)   = " + str(threshold) \n    Sensitivity (changed Pixels) = " + str(sensitivity) \n File Path for Image Save     = " + filepath """

Im sure it is the """ wrapper but unclear what i should use instead?

Kousik
  • 21,485
  • 7
  • 36
  • 59
bighead85
  • 71
  • 1
  • 4
  • 10

4 Answers4

2

in python """ quotes mean to take everything between them literally.

The easiest solution here would be to define a string myString="", then at every print statement, instead of printing you can append to your string with myString=myString+"whatever I want to append\n"

Cruncher
  • 7,641
  • 1
  • 31
  • 65
  • actually, `myString += '...\n'` looks better to my eyes, but easiest would be to replace `print ` with `myString += '\n' + ` – Aprillion Aug 30 '13 at 14:24
  • @deathApril like to stay away from +=. If I do myString = myString + "string" it really reminds me that strings are immutable, and I haven't changed it in place. – Cruncher Aug 30 '13 at 14:47
  • it's ok if it reminds you, it would not help remind me since `i = i + 2` or `i["blah"] = i["blah"] + 2` does not mean something is immutable.. and i'm not sure what is the reminder of immutability good for, it doesn't have any more consequences than a literal string assignment `s = "bbb"` would have.. – Aprillion Sep 08 '13 at 09:24
  • @deathApril if the String is passed into a function, then `myString+='sjiojfaw'` might compel me to think that the original string passed in has changed, which is not the case. I have just reassigned a value to the reference on the local stack. – Cruncher Sep 09 '13 at 13:32
1

It should be:

body =  " Motion Detection Started \n Pixel Threshold (How much)   = " + str(threshold) + \
        "\n Sensitivity (changed Pixels) = " + str(sensitivity) + \
        "\n File Path for Image Save     = " + filepath
JoshG79
  • 1,685
  • 11
  • 14
1

When you do the following, you're telling it everything there is part of the string (notice how the code highlights):

body =  """ Motion Detection Started \n Pixel Threshold (How much)   = " + str(threshold) \n    Sensitivity (changed Pixels) = " + str(sensitivity) \n File Path for Image Save     = " + filepath """

You need to actually add the variables to the string, like when you concatenated them in your print statement.

body =  "Motion Detection Started \n Pixel Threshold (How much)   = " + str(threshold) + " \n    Sensitivity (changed Pixels) = " + str(sensitivity) + "\n File Path for Image Save     = " + filepath

You can also do string formatting:

body = "Motion Detection Started\nPixel Threshold (How much) = {}\nSensitivity (changed Pixels) = {}\nFile Path for Image Save = {}".format(threshold, sensitivity, filepath)
thegrinner
  • 11,546
  • 5
  • 41
  • 64
0

In case you'd like the emails code to be a bit more reusable and robust, Template strings might help you. E.g., save the email text as a template in separate file, template.txt:

Motion Detection Started
------------------------------------------------------
Pixel Threshold (How much)   =  $threshold
Sensitivity (changed Pixels) =  $sensitivity
File Path for Image Save     =  $filepath
---------- Motion Capture File Activity --------------

and in your code, create a class for sending emails together with 1 or more instances of the class (you can have more than 1 template):

import string

class DebugEmail():    
    def __init__(self, templateFileName="default_template.txt"):
        with open(templateFileName) as f:
            self.template = string.Template(f.read())

    def send(self, data):
        body = self.template.safe_substitute(data)
        print(body) # replace by sending email

debugEmail1 = DebugEmail("template.txt")

# and test it like this:

threshold = 1
sensitivity = 1
debugEmail1.send(locals())

sensitivity = 200
filepath = "file"
debugEmail1.send(locals())
Aprillion
  • 21,510
  • 5
  • 55
  • 89