[Tutor] [tutor] Pil image related question -- resending message without attachments

2008-01-05 Thread Varsha Purohit
Hello All,
  I am actually working on a project which deals with image processing.
I just used pil function to resize the image into the dimensions i want. i
was able to resize the image but the clarity of the new image is not good.
Can anyone suggest which other functions i should use to improve the
clarity. I am attaching the code and sending the images in the attachment.
Can anyone suggest me how to do it ?

import Image

imageFile = "srk.jpg" #original small image

im1 = Image.open(imageFile)

width = 680
height = 420

im2 = im1.resize((width,height), Image.NEAREST)
im3 = im1.resize ((width,height), Image.BICUBIC)

#images get saved in c drive with jpg extensions
ext = ".jpg"
im2.save("C:/nearest" + ext)
im3.save("C:/Bicubic" + ext)

print "files are saved"



-- 
Varsha Purohit,
Graduate Student
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program review

2008-01-05 Thread Jeff Younker
The procesar function is complicated.   It tries to do several things
that that are better done elsewhere.  It needs to be broken up into
several functions and methods.   This also clarifies and isolates the
exception handling.

> def procesar(mensaje):
>try :
The enclosing try block isn't needed.  More on this later.
>
>try :
>fIncl = open(mensaje.direcciones)
>except Exception, e :
>logging.error('Error!!! No pude abrir "%s" : %s',
>mensaje.direcciones,
>e.strerror)
>raise

This is one function.  It has the argument filename.
>
>try :
>fExcl = open(mensaje.excluidas)
>except Exception, e :
>logging.error('No pude abrir "%s" : %s',
>mensaje.excluidas,
>e.strerror)
>fIncl.close()
>raise

This is the same function with a different argument.

>
>except : pass
>else :

The outer exception block here does nothing.   If an exception
is raised then the following code won't be executed anyway.

>mails = enumerate(
>set(addr.strip() for addr in fIncl)
>- set(excl.strip() for excl in fExcl))
>fIncl.close()
>fExcl.close()

This is a method that gets the addresses from the files specified in
mensaje.  It opens the files.  It reads the contents.  It closes the  
files.
It should probably go in mensaje.   You should use try-finally blocks
to close the open files.  (or with blocks in python 2.5)

>   miCorreo = Correo(mensaje)
>miCorreo.connect()
>empiezo = time.clock()
>for nro, addr in mails :
>if nro%mensaje.cuantos == 0 and nro > 0 :
>miCorreo.disconnect()
>time.sleep(mensaje.intervalo - (time.clock() -  
> empiezo))
>if not miCorreo.connect() :
>logging.info('Terminando')
>return
>empiezo = time.clock()
>miCorreo.enviar(addr)
>miCorreo.disconnect()

This is the real meat.  Everything before it can be reduced to one line.

-jeff
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create popup image

2008-01-05 Thread Alan Gauld

"Alvin Tan" <[EMAIL PROTECTED]> wrote 

> I am doing a project, something like friend finder. 

Sorry, I don't now it...

> for example , mobile 1 request the location of 
> mobile 2, then a map will actually display on 
> mobile 1 to show the location of mobile 2. 
> But how can i have the popup image?

What platform, OS etc?

Are you using a GIS package forlocations?

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program review

2008-01-05 Thread Jeff Johnson
I would like to "butt" in here and mention that this is some of the most 
useful information I have seen!  I am a programmer of 25 years that is 
new to Python.  This type of back and forth dialog on actual production 
code is extremely useful to learning the language.  I have done this 
with Ed Leafe with Dabo and it has helped a lot.

Keep it up!

Jeff

Jeff Johnson
[EMAIL PROTECTED]
SanDC, Inc.
623-582-0323
Fax 623-869-0675

Ricardo Aráoz wrote:
> Kent Johnson wrote:
>> Ricardo Aráoz wrote:
>>> Ok, here is a corrected new version, I hope.
>>>
>>> Kent, the fact that Mensaje.__init__ reads the message configuration is
>>> by design (it's a feature, not a bug ;c) ).
>> You misunderstood my suggestion. I would add a method to Mensaje that 
>> creates the email.MIMEMultipart.MIMEMultipart() currently done by 
>> Correo.__init__().
>>
>> Almost every line of Correo.__init__() is reading data from attributes 
>> of mensaje. This is a code smell called Feature Envy and is a clear sign 
>> that the code is in the wrong place.
>>
>> In Mensaje you could have
>>  def createMessage(self):
>>  messg = email.MIMEMultipart.MIMEMultipart()
>>  messg['From'] = self.De
>>  messg['To'] = self.Para
>>  messg['Subject'] = self.Encabezado
>>  messg['Reply-To'] = self.ResponderA
>>  messg.preamble = 'This is a multi-part message in MIME format'
>>  messg.attach(email.MIMEText.MIMEText(self.html, 'html'))
>>
>> Then in Correo.__init__() you would have
>>  self.messg = mensaje.createMessage()
>>
>> It doesn't change the functionality but it is a better design.
>>
> 
> I see, yes it is better, I'll correct it. Thanks
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program review

2008-01-05 Thread Ricardo Aráoz
Kent Johnson wrote:
> Ricardo Aráoz wrote:
>> Ok, here is a corrected new version, I hope.
>>
>> Kent, the fact that Mensaje.__init__ reads the message configuration is
>> by design (it's a feature, not a bug ;c) ).
> 
> You misunderstood my suggestion. I would add a method to Mensaje that 
> creates the email.MIMEMultipart.MIMEMultipart() currently done by 
> Correo.__init__().
> 
> Almost every line of Correo.__init__() is reading data from attributes 
> of mensaje. This is a code smell called Feature Envy and is a clear sign 
> that the code is in the wrong place.
> 
> In Mensaje you could have
>  def createMessage(self):
>  messg = email.MIMEMultipart.MIMEMultipart()
>  messg['From'] = self.De
>  messg['To'] = self.Para
>  messg['Subject'] = self.Encabezado
>  messg['Reply-To'] = self.ResponderA
>  messg.preamble = 'This is a multi-part message in MIME format'
>  messg.attach(email.MIMEText.MIMEText(self.html, 'html'))
> 
> Then in Correo.__init__() you would have
>  self.messg = mensaje.createMessage()
> 
> It doesn't change the functionality but it is a better design.
> 

I see, yes it is better, I'll correct it. Thanks

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program review

2008-01-05 Thread Kent Johnson
Ricardo Aráoz wrote:
> Ok, here is a corrected new version, I hope.
> 
> Kent, the fact that Mensaje.__init__ reads the message configuration is
> by design (it's a feature, not a bug ;c) ).

You misunderstood my suggestion. I would add a method to Mensaje that 
creates the email.MIMEMultipart.MIMEMultipart() currently done by 
Correo.__init__().

Almost every line of Correo.__init__() is reading data from attributes 
of mensaje. This is a code smell called Feature Envy and is a clear sign 
that the code is in the wrong place.

In Mensaje you could have
 def createMessage(self):
 messg = email.MIMEMultipart.MIMEMultipart()
 messg['From'] = self.De
 messg['To'] = self.Para
 messg['Subject'] = self.Encabezado
 messg['Reply-To'] = self.ResponderA
 messg.preamble = 'This is a multi-part message in MIME format'
 messg.attach(email.MIMEText.MIMEText(self.html, 'html'))

Then in Correo.__init__() you would have
 self.messg = mensaje.createMessage()

It doesn't change the functionality but it is a better design.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Create popup image

2008-01-05 Thread Alvin Tan

hi all,
 
 
I am doing a project, something like friend finder. 
 
for example , mobile 1 request the location of mobile 2, then a map will 
actually display on mobile 1 to show the location of mobile 2. But how can i 
have the popup image?
 
 
Alvin
_
Get your free suite of Windows Live services today!
http://www.get.live.com/wl/all___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program review

2008-01-05 Thread Ricardo Aráoz
Ok, here is a corrected new version, I hope.

Kent, the fact that Mensaje.__init__ reads the message configuration is
by design (it's a feature, not a bug ;c) ). The class is meant to be
instantiated for using only once and with only one configuration and one
message so including the configuration in the __init__ method kind of
stresses that fact. What's more, I've been thinking of reading both
files in the __init__ method so that the object is created ready to do
it's intended job. Anyway, I guess it's a matter of personal taste in
this case.

Here's the code :

#!/usr/bin/env python

import time
import smtplib
import email
import ConfigParser
import logging

class Mensaje(object) :
def __init__(self) :
cfg = ConfigParser.ConfigParser()
try :
cfg.readfp(open('config.cfg'))
except Exception, e :
logging.error('No pude leer "config.cfg" : %s', e.strerror)

self.direcciones = cfg.get('Archivos', 'Direcciones')
self.excluidas = cfg.get('Archivos', 'Excluir')
self.cuantos = cfg.getint('Correo', 'MailsPorVez')
self.intervalo = cfg.getint('Correo', 'IntervaloEnSegundos')

try :
htmlFile = open(cfg.get('Archivos', 'Mensaje'))
self.html = htmlFile.read()
htmlFile.close()
except Exception, e :
logging.error('No pude leer "%s" : %s',
cfg.get('Archivos', 'Mensaje'),
e.strerror)

self.De = cfg.get('Encabezados', 'De')
self.Para = ''
self.Encabezado = cfg.get('Encabezados', 'Encabezado')
self.ResponderA = cfg.get('Encabezados', 'ResponderA')
self.Servidor = cfg.get('Correo', 'Servidor')
self.Usuario = cfg.get('Correo', 'Usuario')
self.Contra = cfg.get('Correo', 'Contrasenia')

class Correo(object) :
def __init__(self, mensaje) :
self.messg = email.MIMEMultipart.MIMEMultipart()
self.messg['From'] = mensaje.De
self.messg['To'] = mensaje.Para
self.messg['Subject'] = mensaje.Encabezado
self.messg['Reply-To'] = mensaje.ResponderA
self.messg.preamble = 'This is a multi-part message in MIME format'
self.messg.attach(email.MIMEText.MIMEText(mensaje.html, 'html'))

self.Servidor = mensaje.Servidor
self.Conexion = smtplib.SMTP()
self.Usuario = mensaje.Usuario
self.Contra = mensaje.Contra

def connect(self) :
try :
self.Conexion.connect(self.Servidor)
self.Conexion.set_debuglevel(False)
self.Conexion.ehlo()
self.Conexion.starttls()
self.Conexion.ehlo()
self.Conexion.login(self.Usuario, self.Contra)
return True
except :
logging.error('No me pude conectar al Servidor')
return False

def disconnect(self) :
self.Conexion.close()

def enviar(self, addr) :
self.messg.replace_header('To', addr)
try :
self.Conexion.sendmail(self.messg['From'],
self.messg['To'],
self.messg.as_string())
logging.info('Enviado a : %s', self.messg['To'])
except SMTPRecipientsRefused :
logging.error('El destinatario fue rechazado por el servidor')
except SMTPHeloError :
logging.error('El servidor no respondio apropiadamente')
except SMTPSenderRefused :
logging.error('El From: fue rechazado por el servidor')
except SMTPDataError :
logging.error('El servidor respondio con un error desconocido')

def procesar(mensaje):
try :
try :
fIncl = open(mensaje.direcciones)
except Exception, e :
logging.error('Error!!! No pude abrir "%s" : %s',
mensaje.direcciones,
e.strerror)
raise
try :
fExcl = open(mensaje.excluidas)
except Exception, e :
logging.error('No pude abrir "%s" : %s',
mensaje.excluidas,
e.strerror)
fIncl.close()
raise
except : pass
else :
mails = enumerate(
set(addr.strip() for addr in fIncl)
- set(excl.strip() for excl in fExcl))
fIncl.close()
fExcl.close()
miCorreo = Correo(mensaje)
miCorreo.connect()
empiezo = time.clock()
for nro, addr in mails :
if nro%mensaje.cuantos == 0 and nro > 0 :
miCorreo.disconnect()
time.sleep(mensaje.intervalo - (time.clock() - empiezo))
if not miCorreo.connect() :
logging.info('Terminando')
return
empiezo = time.clock()
miCorreo.enviar(addr)
miCorreo.disconnect()


if __name__ == '

Re: [Tutor] classes and the deepcopy function

2008-01-05 Thread bob gailer
Michael wrote:
> Hi Michael
>
> Thanks for the quick reply, I think I get it. So becuase I did not 
> declare them withing the init method using self they are shared by every 
> object that is created, even completely brand new ones?
>
> Is it normal practice to declare your variables in a class? I notice 
> that you don't have to, you can create them as you go, but i thought 
> declaring and initialising them in the class would make it easier to 
> understand and see what the class is for and should contain.
And now for the rest of the story:

class Foo:
  a = 0
  b = []

f = Foo()
f.a = 3
f.b.append(1)

g = Foo()
g.a = 4
g.b.append(2)

print f.a, g.a # 3 4
print f.b, g.b # [1, 2] [1, 2]

[snip]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program review

2008-01-05 Thread Ricardo Aráoz
Thanks everyone for your feedback. I'll correct the code and re-post it.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] classes and the deepcopy function

2008-01-05 Thread Alan Gauld

"Michael" <[EMAIL PROTECTED]> wrote

> Is it normal practice to declare your variables in a class? I notice
> that you don't have to, you can create them as you go, but i thought
> declaring and initialising them in the class would make it easier to
> understand and see what the class is for and should contain.

Its normal to declare them in the definition. Its just that instance
variables go in the init method whereas class variables go in the
class scope and are, as you say, shatred by all instances.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor