Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35391

Re: Python, email temperature

Date 2012-12-22 13:12 -0800
From Gary Herron <gary.herron@islandtraining.com>
Subject Re: Python, email temperature
References <543ec063-14c1-4ca6-a911-ae6f58c22a8c@googlegroups.com> <mailman.1208.1356209086.29569.python-list@python.org> <4d1cf59e-7fb0-4b93-bfe8-db484edb5a45@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1214.1356211303.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 12/22/2012 12:54 PM, KarlE wrote:
> On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
>> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <ran...@gmail.com> wrote:
>>
>> Hi!
>>
>>
>>
>> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
>>
>>
>>
>>
>> The two programs work very well on their own, but this doesnt work.
>>
>>
>>
>> this works: server.sendmail(fromaddr, toaddrs, msg)
>>
>> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
>>
>>
>>
>> despite the command "print cputemp" working in the same program.
>>
>>
>>
>> When i run the program i get the error:
>>
>>
>>
>> Traceback (most recent call last):
>>
>>    File "sendcpu.py", line 36, in <module>
>>
>>      msg = cpu_temperature
>>
>> NameError: name 'cpu_temperature' is not defined
>>
>>
>>
>> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
>>
>>
>>
>> You should copy and paste the code here including the context around the error.  You say print cputemp works, but cpu_temperature is not defined.  They are spelled differently.  Start there
>>
>>
>>
>>
>> Thanx!
>>
>>
>>
>> //Alexander
>>
>>
>>
>> --
>>
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>>
>> -- 
>> Joel Goldstick
> Hi!
>
> I made a typing error, and couldnt edit the post :( this is the code:
>
>
> #!/usr/bin/env python
> from __future__ import division
> from subprocess import PIPE, Popen
> import psutil
> import smtplib
>
> def get_cpu_temperature():
>      process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
>      output, _error = process.communicate()
>      return float(output[output.index('=') + 1:output.rindex("'")])
>
>
> def main():
>      cpu_temperature = get_cpu_temperature()
>      cpu_usage = psutil.cpu_percent()
>
>      ram = psutil.phymem_usage()
>      ram_total = ram.total / 2**20       # MiB.
>      ram_used = ram.used / 2**20
>      ram_free = ram.free / 2**20
>      ram_percent_used = ram.percent
>
>      disk = psutil.disk_usage('/')
>      disk_total = disk.total / 2**30     # GiB.
>      disk_used = disk.used / 2**30
>      disk_free = disk.free / 2**30
>      disk_percent_used = disk.percent
>      #
>      # Print top five processes in terms of virtual memory usage.
>      #
>      print 'CPU temperature is: ',  cpu_temperature
>
> fromaddr = 'myemailadress'
> toaddrs  = 'myemailadress'
> #msg = 'There was a terrible error that occured and I wanted you to know!'
> msg = cpu_temperature
>
> # Credentials (if needed)
> username = 'myusername'
> password = 'mypassword'
>
> # The actual mail send
> server = smtplib.SMTP('smtp.gmail.com:587')
> server.starttls()
> server.login(username,password)
> server.sendmail(fromaddr, toaddrs, cpu_temperature)
> server.quit()
>
>
>
>
> if __name__ == '__main__':
>      main()
>
> running it gives the following error:
>
> pi@raspberrypi /home/python $ python sendcpu.py
> Traceback (most recent call last):
>    File "sendcpu.py", line 36, in <module>
>      msg = cpu_temperature
> NameError: name 'cpu_temperature' is not defined
> pi@raspberrypi /home/python $
>
>
> isnt cpu_temperature defined?
>
>

First:  Learn about Python SCOPES.  You are defining variables inside 
(as local variables) the procedure main, but they are lost as soon as 
main returns.  If you want values computed inside main but available 
outside main, you should return them.

Second:  Some confusion over what order things are executed in.  The 
code in main is run when you call main -- ans that's at the very end of 
the file.   The lines before the call to main expect to use the value 
cpu_temperature when you have not yet called main to compute the value 
(and which doesn't even return the value as noted above).

The confusion is partly caused by having some of your code inside main 
and some of it outside main and expecting the two parts to 
communicate.    I'd suggest putting everything up through the 
server.quit() into procedure main.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python, email temperature Alexander Ranstam <ranstam@gmail.com> - 2012-12-22 12:36 -0800
  Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 12:38 -0800
  Re: Python, email temperature Joel Goldstick <joel.goldstick@gmail.com> - 2012-12-22 15:44 -0500
    Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 12:54 -0800
      Re: Python, email temperature Gary Herron <gary.herron@islandtraining.com> - 2012-12-22 13:12 -0800
    Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 12:54 -0800
      Re: Python, email temperature No One <atsidi@gmail.com> - 2012-12-22 21:53 +0000
  Re: Python, email temperature Gary Herron <gary.herron@islandtraining.com> - 2012-12-22 12:45 -0800
  Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-22 14:50 -0800
    Re: Python, email temperature Chris Angelico <rosuav@gmail.com> - 2012-12-23 10:15 +1100
  Re: Python, email temperature KarlE <ranstam@gmail.com> - 2012-12-23 05:46 -0800
    Re: Python, email temperature Mitya Sirenef <msirenef@lightbird.net> - 2012-12-23 12:23 -0500
    Re: Python, email temperature Dave Angel <d@davea.name> - 2012-12-23 15:34 -0500
    Re: Python, email temperature Terry Reedy <tjreedy@udel.edu> - 2012-12-23 19:31 -0500

csiph-web