[Tutor] Remove a blank row of a Table

2015-02-27 Thread shouqiang1989
Hello, everybody,  I came accross a question a few days 
ago. I wrote a Python script to copy something to a table in a word 
file. For some reaons, there are some blank rows. And I want to remove 
the blank rows. I tried many ways, but all didn't work. Below is the 
code. The code in blue is to remove the  blank row.


import codecs
import os
import textwrap
import win32com,string,shutil
from win32com.client import Dispatch,constants
def ReportPackage(reportName, reportApi, package=None):
desfile='C:\\Daten\\TestCASE5\\Model_hill.doc'
w = win32com.client.Dispatch('Word.Application')
w.Visible = 1
os.path.join(reportApi.GetReportDir(), reportName)
fd = w.Documents.Open(desfile)  #open the model fileinfo = 
reportApi.GetInfo()
if package is None:
package = reportApi.GetMainPackage()
result = info.GetResult()
execTime = info.GetExecutionTime()
pkgName =package.GetName()
else:
result = package.GetResult()
execTime = package.GetTime()
i=1
s=0
f=0
e=0

form = fd.Tables[6]
form.Rows.Add()   


form.Rows.Last.Cells[2].Range.InsertAfter("%(pkgName)s"  % {'pkgName': 
pkgName})
form.Rows.Last.Cells[3].Range.InsertAfter("%(executionTime)s" 
%{'executionTime': execTime})

form.Rows.Add()

lines = form.Rows.Last()
lines.Cells[0].Range.InsertAfter("Num")
lines.Cells[1].Range.InsertAfter("Time")
lines.Cells[2].Range.InsertAfter("Comment")
lines.Cells[3].Range.InsertAfter("Result")

testCase = 
package.GetTestCase(excludeSubPackages=reportApi.IsSet("hideSubPackages", 
"True"))

for reportItem in testCase.IterTestSteps():
try:


 if reportItem.PResult == "NONE" : #constants.RESULT_NONE:
 strResult = '' 
 else :
 strResult = reportItem.PResult

 
 lNam = textwrap.wrap(reportItem.PName)
 maxLines = len(lNam)
 

 fd.Tables[6].Rows.Add()

 if strResult == 'SUCCESS':
 
fd.Tables[6].Rows.Last.Cells[3].Shading.BackgroundPatternColorIndex=4   
  
 s=s+1
 elif strResult == 'FAILED':
 
fd.Tables[6].Rows.Last.Cells[3].Shading.BackgroundPatternColor=250
 f=f+1
 elif strResult == 'ERROR' :
 
fd.Tables[6].Rows.Last.Cells[3].Shading.BackgroundPatternColor=180
 e=e+1


 if strResult == 'SUCCESS' or strResult == 'FAILED' or strResult == 
'ERROR' :   
 fd.Tables[6].Rows.Last.Cells[3].Range.InsertAfter(strResult)
 fd.Tables[6].Rows.Last.Cells[2].Range.InsertAfter(lNam[0])
 fd.Tables[6].Rows.Last.Cells[1].Range.InsertAfter(("%.3f" % 
reportItem.PTimestamp))
 fd.Tables[6].Rows.Last.Cells[0].Range.InsertAfter(i)
 i=i+1
 else :
 pass


 for idx in xrange (1,maxLines) :
 if fd.Tables[6].Rows[idx].Cells[1] == None :
 fd.Tables[6].Rows[idx].Remove()
 else :
 pass
except :
break

s=str(s)
f=str(f)
e=str(e)
fd.Tables[6].Rows.Add()
fd.Tables[6].Rows.Last.Cells[2].Range.InsertAfter("s="+s+' ')
fd.Tables[6].Rows.Last.Cells[2].Range.InsertAfter("f="+f+' ')
fd.Tables[6].Rows.Last.Cells[2].Range.InsertAfter("e="+e+' ')
#insert the results in table 7



fd.Save.im_self
fd.Close()




def _ReportProjectElement():
return()
   


def ReportProject():
return()


Thanks in advance.




Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python in MAC OS 10.6

2015-02-27 Thread Alan Gauld

On 27/02/15 02:16, Barbara Heliodora G. Rodrigues wrote:

Dear tutor,
I'd like to ask for help with an issue I have with python. My MAC is with OS 
10-6.8,


I'm not a MacOs expert but I know MacOS uses Python in some of its tools 
so do not mess with the standard version, you might break something.



I can't install any new software that uses python,


What the OS uses as Python should not necessarily impact what you use as 
Python. Usually its just a case of changing a link or creating an alias 
or environment variable.


At the worst case you can edit the shebang line of your scripts.


I tried to use some plot (matplotlib) routines that were working fine before,


How are you running these scripts? Do you just double click an icon or 
do you use a commandline? If the latter what do you type?

If the former somebody with a modern Mac will need to advise.


Is there a way to force macports to install a 64bits version of python?


Probably, but I don't know macports.


I downloaded and installed the 3.4 and 3.1 myself, but still the

> computer only sees the version macports installed.

Have you tried calling python3 instead of just python?
That should find your Python 3.X version.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Remove a blank row of a Table

2015-02-27 Thread Alan Gauld

On 27/02/15 03:19, shouqiang1...@sina.com wrote:

Hello, everybody,  I came accross a question a few days
ago. I wrote a Python script to copy something to a table in a word
file. For some reaons, there are some blank rows. And I want to remove
the blank rows. I tried many ways, but all didn't work. Below is the
code. The code in blue is to remove the  blank row.


First thing is that there is no blue on a plain text email, so we can't 
see which lines. You need  to add a comment to alert us.



import codecs
import os
import textwrap
import win32com,string,shutil
from win32com.client import Dispatch,constants


Most of your code seems to be based on calls to the win32com
library and the Word object model functions. Most of us on this
list don't work with that regularly (if at all) so you
probably will get a better response on the PyWin32 support
forum.

There is a Win32 mailing list too, on Gmane its

gmane.comp.python.windows



def ReportPackage(reportName, reportApi, package=None):
 desfile='C:\\Daten\\TestCASE5\\Model_hill.doc'
As a general style point its usually better to make filenames a 
parameter of the function. Otherwise the function is of limited

reuse potential as it can only ever work on one file.

Its not clear what reportApi is but you seem to call lots of functions 
on it. Is it a Win32 thing or another python module?



 w = win32com.client.Dispatch('Word.Application')
 w.Visible = 1
 os.path.join(reportApi.GetReportDir(), reportName)


You call os.path.join here but do not store the result.
It does nothing.


 fd = w.Documents.Open(desfile)  #open the model fileinfo = 
reportApi.GetInfo()


I assume the info assignment is supposed to be inline?


 if package is None:
 package = reportApi.GetMainPackage()
 result = info.GetResult()
 execTime = info.GetExecutionTime()
 pkgName =package.GetName()
 else:
 result = package.GetResult()
 execTime = package.GetTime()


No idea what that did but it seems irrelevant to most of what follows.


 i=1
 s=0
 f=0
 e=0


No idea what these are supposed to be for. Its much better
to use meaningful names. The cost of typing a few extra letters
is a small price to pay for the extra readability.



 form = fd.Tables[6]


I'm guessing this is a hard coded dependency on the structure
of your document which we can't see. I'll just assume that
its correct.


 form.Rows.Add()
 form.Rows.Last.Cells[2].Range.InsertAfter("%(pkgName)s"  % {'pkgName': 
pkgName})
 form.Rows.Last.Cells[3].Range.InsertAfter("%(executionTime)s" 
%{'executionTime': execTime})

 form.Rows.Add()
 lines = form.Rows.Last()
 lines.Cells[0].Range.InsertAfter("Num")
 lines.Cells[1].Range.InsertAfter("Time")
 lines.Cells[2].Range.InsertAfter("Comment")
 lines.Cells[3].Range.InsertAfter("Result")


This is all Word object model stuff so I'll just assume its correct.


 testCase = package.GetTestCase(excludeSubPackages=reportApi.IsSet("hideSubPackages", 
"True"))


We don;t know what package does nor what reportApi does so
again I'll just assume you know what you are doing here.

And I don't know what testCase might be either, nor what the reportItem 
objects are. So I'll stop here.


Can you see the pattern? So much of what you are doing relies on 
external libraries that are not part of Python that it's almost 
impossible for us to comment on your code. You really need to find 
somebody familiar with the Word API rather than a Python expert.

You may be lucky and find one here, but a dedicated forum is
a much better bet.

We can comment on some finer points of your Python style but
that's probably not going to solve your real issue.


 except :
 break


But this one I can't let rest.

By doing this you are hiding all attempts by Python to alert you to 
problems. If anything goes wrong in the code above Python throws an 
exception which you just throw away. Python may be telling you the 
solution to your problem but you are ignoring it.


Always catch specific exceptions, but only if you know how to
handle them. Otherwise let Python help you by telling you what's
gone wrong.



def _ReportProjectElement():
 return()


The () after return is misleading, lose them.
It makes it look like you are calling a function
called return, which you aren't.

Its usually better when writing stubs to return a
hard coded example of valid output. If the function
doesn't return anything its more obvious if you
use pass rather than return. (IMHO at least :-)

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 132, Issue 57

2015-02-27 Thread Rishi Ganesh V
how to pass the argument in the command line
i have the input file as csv and output file as txt
both input file and output file i am passing the value to an argument

like
inputfile=sys.argv[1] , output file=sys.argv[2]

convert(inputfile,outputfile)


how to pass the value in argument

On Fri, Feb 27, 2015 at 4:30 PM,  wrote:

>  [image: Boxbe]  This message is eligible
> for Automatic Cleanup! (tutor-requ...@python.org) Add cleanup rule
> 
> | More info
> 
>
> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>1. Re: Gaussian process regression (Steven D'Aprano)
>2. Help with python in MAC OS 10.6 (Barbara Heliodora G. Rodrigues)
>3. Remove a blank row of a Table (shouqiang1...@sina.com)
>4. Re: Help with python in MAC OS 10.6 (Alan Gauld)
>
>
> --
>
> Message: 1
> Date: Fri, 27 Feb 2015 12:57:00 +1100
> From: Steven D'Aprano 
> To: tutor@python.org
> Subject: Re: [Tutor] Gaussian process regression
> Message-ID: <20150227015659.gy7...@ando.pearwood.info>
> Content-Type: text/plain; charset=us-ascii
>
> On Fri, Feb 27, 2015 at 03:04:25AM +0900, Huijae wrote:
>
> > Hi, I am trying to use Gaussian process regression for Near Infrared
> > spectra. I have reference data(spectra), concentrations of reference
> > data and sample data, and I am trying to predict concentrations of
> > sample data. Here is my code. from sklearn.gaussian_process import
> > GaussianProcess gp = GaussianProcess() gp.fit(reference,
> > concentration) concentration_pred = gp.predict(sample) The results
> > always gave me the same concentration even though I used different
> > sample data. When I used some parts of reference data as sample data,
> > it predicted concentration well. But whenever I use different data
> > than reference data, it always gave me the same concentration. Can I
> > get some help with this problem? What am I doing wrong? I would
> > appreciate any help. Thanks, Jay
>
> Your mail program has mangled your code and mixed up the layout. I
> suggest turning off all formatting, "Rich Text" or HTML mail, since that
> usually destroys the layout of code.
>
> I'm going to try to guess your code's layout:
>
> from sklearn.gaussian_process import GaussianProcess
> gp = GaussianProcess()
> gp.fit(reference, concentration)
> concentration_pred = gp.predict(sample)
>
>
> This is not enough for us to give any meaningful advice. What is
> "sklearn"? What are "reference", "concentration", "sample"? We'd need
> to see actual data, and know what GaussianProcess does, to help.
>
>
> > The results always gave me the same concentration
> > even though I used different sample data.
>
> *Exactly* the same? To 17 decimal places? Or just close to the same?
>
> Perhaps the different samples give the same result because they are
> samples from the same population.
>
>
> > When I used some parts of
> > reference data as sample data, it predicted concentration well. But
> > whenever I use different data than reference data, it always gave me
> > the same concentration.
>
> You may need to ask people who know more about GaussianProcess and how
> it works.
>
>
>
> --
> Steve
>
>
> --
>
> Message: 2
> Date: Fri, 27 Feb 2015 02:16:56 + (UTC)
> From: "Barbara Heliodora G. Rodrigues" 
> To: "tutor@python.org" 
> Subject: [Tutor] Help with python in MAC OS 10.6
> Message-ID:
> <199673520.1431247.1425003416015.javamail.ya...@mail.yahoo.com>
> Content-Type: text/plain; charset=UTF-8
>
> Dear tutor,
> I'd like to ask for help with an issue I have with python. My MAC is with
> OS 10-6.8, darwin kernel, 64 bits. For some reason I updated macports and
> it automatically updated python to 2.7 with a 32 bits library, and it is
> giving me lots of trouble. I can't install any new software that uses
> python, and recently I tried to use some p

Re: [Tutor] Tutor Digest, Vol 132, Issue 57

2015-02-27 Thread Rishi Ganesh V
Dear tutor,

how to pass the argument in the command line
i have the input file as csv and output file as txt
both input file and output file i am passing the value to an argument in
the command line

in the script it is like
def convert(inputfile, outputfile):
  reader = csv.reader(open(inputfile, "rb"))
  file = open(outputfile, 'w+')
..



inputfile=sys.argv[1] , output file=sys.argv[2]

convert(inputfile,outputfile)


1.how to pass the value to an argument in the command line

2.if i compiled i am getting an following error
Value error: too many values to unpack

So how  to avoid these error...


On Fri, Feb 27, 2015 at 5:51 PM, Rishi Ganesh V 
wrote:

> how to pass the argument in the command line
> i have the input file as csv and output file as txt
> both input file and output file i am passing the value to an argument
>
> like
> inputfile=sys.argv[1] , output file=sys.argv[2]
>
> convert(inputfile,outputfile)
>
>
> how to pass the value in argument
>
> On Fri, Feb 27, 2015 at 4:30 PM,  wrote:
>
>>  [image: Boxbe]  This message is
>> eligible for Automatic Cleanup! (tutor-requ...@python.org) Add cleanup
>> rule
>> 
>> | More info
>> 
>>
>> Send Tutor mailing list submissions to
>> tutor@python.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>> https://mail.python.org/mailman/listinfo/tutor
>> or, via email, send a message with subject or body 'help' to
>> tutor-requ...@python.org
>>
>> You can reach the person managing the list at
>> tutor-ow...@python.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of Tutor digest..."
>>
>>
>> Today's Topics:
>>
>>1. Re: Gaussian process regression (Steven D'Aprano)
>>2. Help with python in MAC OS 10.6 (Barbara Heliodora G. Rodrigues)
>>3. Remove a blank row of a Table (shouqiang1...@sina.com)
>>4. Re: Help with python in MAC OS 10.6 (Alan Gauld)
>>
>>
>> --
>>
>> Message: 1
>> Date: Fri, 27 Feb 2015 12:57:00 +1100
>> From: Steven D'Aprano 
>> To: tutor@python.org
>> Subject: Re: [Tutor] Gaussian process regression
>> Message-ID: <20150227015659.gy7...@ando.pearwood.info>
>> Content-Type: text/plain; charset=us-ascii
>>
>> On Fri, Feb 27, 2015 at 03:04:25AM +0900, Huijae wrote:
>>
>> > Hi, I am trying to use Gaussian process regression for Near Infrared
>> > spectra. I have reference data(spectra), concentrations of reference
>> > data and sample data, and I am trying to predict concentrations of
>> > sample data. Here is my code. from sklearn.gaussian_process import
>> > GaussianProcess gp = GaussianProcess() gp.fit(reference,
>> > concentration) concentration_pred = gp.predict(sample) The results
>> > always gave me the same concentration even though I used different
>> > sample data. When I used some parts of reference data as sample data,
>> > it predicted concentration well. But whenever I use different data
>> > than reference data, it always gave me the same concentration. Can I
>> > get some help with this problem? What am I doing wrong? I would
>> > appreciate any help. Thanks, Jay
>>
>> Your mail program has mangled your code and mixed up the layout. I
>> suggest turning off all formatting, "Rich Text" or HTML mail, since that
>> usually destroys the layout of code.
>>
>> I'm going to try to guess your code's layout:
>>
>> from sklearn.gaussian_process import GaussianProcess
>> gp = GaussianProcess()
>> gp.fit(reference, concentration)
>> concentration_pred = gp.predict(sample)
>>
>>
>> This is not enough for us to give any meaningful advice. What is
>> "sklearn"? What are "reference", "concentration", "sample"? We'd need
>> to see actual data, and know what GaussianProcess does, to help.
>>
>>
>> > The results always gave me the same concentration
>> > even though I used different sample data.
>>
>> *Exactly* the same? To 17 decimal places? Or just close to the same?
>>
>> Perhaps the different samples give the same result because they are
>> samples from the same population.
>>
>>
>> > When I used some parts of
>> > reference data as sample data, it predicted concentration well. But
>> > whenever I use different data than reference data, it always gave me
>> > the same concentration.
>>
>> You may need to ask peop

[Tutor] CLI args [was:Re: Tutor Digest, Vol 132, Issue 57]

2015-02-27 Thread Alan Gauld

On 27/02/15 12:31, Rishi Ganesh V wrote:


in the script it is like
def convert(inputfile, outputfile):
   reader = csv.reader(open(inputfile, "rb"))
   file = open(outputfile, 'w+')
..



inputfile=sys.argv[1] , output file=sys.argv[2]

convert(inputfile,outputfile)


The assignments need to be on separate lines not comma separated

Then, assuming your script is called myscript.py you call it like

$ python myscript.py myinputfile.csv myoutputfile.txt

You can find more in the "talking to the User" topic of my
tutorial (see below)

Please do not send the entire digest, some people pay by the byte.

Also change the subject line to something descriptive so it can be found 
in the archives.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use python to parse the subject line of emails, listen for and react to commands

2015-02-27 Thread Danny Yoo
On Thu, Feb 26, 2015 at 4:18 PM, Alan Gauld  wrote:
> On 26/02/15 20:51, Willie Sims wrote:
>> I know py can parse emails,  but I cant figure out how to limit to only
>> the subject and only if its in that certain format
>> and only if its coming from a recognized email address
>
> OK, Show us what you've tried. That makes it easier for us to see what is
> tripping you up. Otherwise we are just guessing.
>
> All 3 things you ask for can be done but we need a starting point and the
> best one is your existing, but faulty, code.


Also, if possible, include the sample inputs to the program, and the
desired output.  Pretend that your program worked perfectly, and show
input and imaginary-perfect output.

You can feel free to sanitize the input so that it's not revealing
anything personally sensitive, of course!  Just have something that
has enough of the shape of the original input that we can tell what
you want to extract.

This will allow folks to actually execute your program on the set of
data that you are using, and compare what you're getting vs what you
want.  To compare the difference, the delta, between what we've got
and what we want, is a good technique that folks can use to
troubleshoot.

Generally, it's be great if you can provide a short, self-contained,
correct example.  (http://sscce.org/)  We're not asking this to be
pedantic, but to sharpen the problem statement.  By making the
question self-contained and standalone, you'll make it much easier for
folks to *replicate* what you're seeing.  Without this, it becomes
harder for us to know what problem you're trying to solve, or what
difficulties you're encountering.


Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Variable data to CSV

2015-02-27 Thread Thomas Toker
Hey all so I'm new to python(like 2 days ago new) I need to find the device 
address, usb port, and number of times the error occurs Heres what I have: 
import re import csv
import reimport csvf = open("file.txt", "r") #location of log file
searchlines = f.readlines()                   #create list from 
filef.close()for element in searchlines:     usbPres = 
re.search('(USB)',element)  #pattern to find usb lines     devAddr = 
re.search('(address)\s\d+',element)  #parsing pattern for device address     
port = re.search('(port)\s\d',element)  #parsing pattern for port     if 
usbPres:         This is where I get lost because I want to assign the correct 
port to device address and then count number of time it failed before a new 
device is inserted into that port. then write it to a CSV

 SAMPLE TEXT ***
[11883.112089] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11883.224080] usb 1-7: new high speed USB device using ehci_hcd and address 42
[11883.328151] hub 1-0:1.0: unable to enumerate USB device on port 7
[11904.472097] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11907.440096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11910.408093] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11913.376095] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11913.616090] usb 1-7: new high speed USB device using ehci_hcd and address 47
[11913.716121] hub 1-0:1.0: unable to enumerate USB device on port 7
[11927.340096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11930.308096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11933.276124] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11936.244118] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11939.212116] hub 1-0:1.0: Cannot enable port 7. Maybe the USB cable is 
bad?Any help is much appreciated.Tom


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use python to parse the subject line of emails, listen for and react to commands

2015-02-27 Thread Willie Sims
I have started working with 2.7 simply because I don't know when the error
is caused by me or because of version issues.
This is how far I have gotten, 
I can access and pull emails that only come from a selected email address
(OWNER)
I have it parsed down to print the subject only
And I have it saving the string to a file output.txt
And I have it deleting the emails so they don't reread, 
Now I want to eval the output.txt and act on the keyword that is written,,
each key word will be a python script name. So if I sent in the subject line
"status"
It saves the word as status.py in output.txt, then at the bottom I tried
eval output.txt but get the error
 
Traceback (most recent call last):
  File "C:\Python27\New folder\start.py", line 82, in 
start_command = eval(open("output.txt").read())
  File "", line 1, in 
NameError: name 'test' is not defined


import sys
import imaplib
import getpass
import email
import email.header
import datetime

EMAIL_ACCOUNT = "myem...@gmail.com"
EMAIL_FOLDER = "INBOX"
OWNER = "willie14...@outlook.com"
COMMAND_FILE = open("output.txt","w")

def process_mailbox(M):
rv, data = M.search(None, "From", (OWNER))
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
msg = email.message_from_string(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = unicode(decode[0])
print 'Message %s: %s' % (num, subject)
COMMAND_FILE.write('%s' % (subject))
COMMAND_FILE.close()
#print 'Raw Date:', msg['Date']
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass())
except imaplib.IMAP4.error:
print "LOGIN FAILED!!! "
sys.exit(1)
print rv, data
rv, mailboxes = M.list()
if rv == 'OK':
print "Mailboxes:"
print mailboxes
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "Processing mailbox...\n"
process_mailbox(M)
M.select('INBOX')  # select all trash
M.store("1:*", '+FLAGS', '\\Deleted')  #Flag all Trash as Deleted
M.expunge()
M.close()
else:
print "ERROR: Unable to open mailbox ", rv

M.logout()
start_command = eval(open("output.txt").read())


Traceback (most recent call last):
  File "C:\Python27\New folder\start.py", line 82, in 
start_command = eval(open("output.txt").read())
  File "", line 1, in 
NameError: name 'test' is not defined



-Original Message-
From: Tutor [mailto:tutor-bounces+willie14228=outlook@python.org] On
Behalf Of Alan Gauld
Sent: Thursday, February 26, 2015 6:18 PM
To: tutor@python.org
Subject: Re: [Tutor] Use python to parse the subject line of emails, listen
for and react to commands

On 26/02/15 20:51, Willie Sims wrote:
> I know py can parse emails,  but I cant figure out how to limit to 
> only
 > the subject and only if its in that certain format
> and only if its coming from a recognized email address

OK, Show us what you've tried. That makes it easier for us to see what is
tripping you up. Otherwise we are just guessing.

All 3 things you ask for can be done but we need a starting point and the
best one is your existing, but faulty, code.

> and since I'm using 3.4 it seems all the other kind of examples don't 
> work

A lot of old examples on the web will be for Python v32 and there are
significant differences to v3.

However, Python v3 has not lost any important functionality so whatever
worked in v2 can be made to work in v3. Again we just need a clue as to what
you are trying to do. (show us your code - and any error messages and tell
us your OS too)

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Variable data to CSV

2015-02-27 Thread Alan Gauld

On 27/02/15 20:00, Thomas Toker wrote:

Hey all so I'm new to python(like 2 days ago new)


Welcome.
However it will help if:
1) You always post in plain text. As you see below HTML code gets zapped 
in email.




I need to find the device address, usb port, and number of times the error 
occurs


OK, Assume we don;t know anything about USB
Explain how that relates tom your sample data below.
Which columns do you want to extract/process?

Your data is not consistent in structure so using csv is
probably more trouble than its worth.
Look at using split() based on both spaces and colons.



import re import csv
import reimport csvf = open("file.txt", "r") #location of log file
searchlines = f.readlines()   #create list from 
filef.close()for element in searchlines: usbPres = 
re.search('(USB)',element)  #pattern to find usb lines devAddr = 
re.search('(address)\s\d+',element)  #parsing pattern for device address 
port = re.search('(port)\s\d',element)  #parsing pattern for port if 
usbPres: This is where I get lost because I want to assign the correct 
port to device address and then count number of time it failed before a new 
device is inserted into that port. then write it to a CSV


But the code is too messed up to read easily so please repost
in plain text.




 SAMPLE TEXT ***
[11883.112089] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11883.224080] usb 1-7: new high speed USB device using ehci_hcd and address 42
[11883.328151] hub 1-0:1.0: unable to enumerate USB device on port 7
[11904.472097] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11907.440096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11910.408093] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
[11913.376095] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with python in MAC OS 10.6

2015-02-27 Thread Cameron Simpson

On 27Feb2015 02:16, Barbara Heliodora G. Rodrigues 
 wrote:

I'd like to ask for help with an issue I have with python. My MAC is with OS 
10-6.8, darwin kernel, 64 bits. For some reason I updated macports and it 
automatically updated python to 2.7 with a 32 bits library, and it is giving me 
lots of trouble. I can't install any new software that uses python, and 
recently I tried to use some plot (matplotlib) routines that were working fine 
before, simply didn't work, the window with the plot didn't open.Is there a way 
to force macports to install a 64bits version of python?


Since I use MacPorts and Alan doesn't I'll chip in...

First up, you're talking about the MacPorts pythn (in /opt/local) not the 
system Python (in /usr).


This page:

 
http://stackoverflow.com/questions/2111283/how-to-build-64-bit-python-on-os-x-10-6-only-64-bit-no-universal-nonsense

seems to offer useful advice on getting MacPorts to install your preferred 
variant on Python.


Next, re this:

I downloaded and installed the 3.4 and 3.1 myself, but still the computer only 
sees the version macports installed.


You need to clarify what this statement means, probably with examples of how 
you are invoking your python programs, and the output of "which python".


Your Mac should (now) have multiple Pythons installed:

 /usr/bin/python
   Supplied with MacOSX, the system Python. Probably 64-bit python. As the 
   cited web page above says, run:


 file /usr/bin/python

   to see what architecture it is. You can do that for any of the other 
   executables too to check how they were built.


 /opt/local/bin/python
   The MacPorts python (python 2 by default). It should like you need to 
   reinstall this with the right "variants" settings to get it to be 64-bit.


 Your personally built Pythons.

Regarding the last, please outline what options you used to build these. It is 
very important to build/install with a suitable --prefix Configure option to 
put them in the right place: not a specific place, but a place _not_ 
conflicting with the OSX or MacPorts pythons, for example 
/usr/local/python-2.7.7 (adjust to suit).


It is normal to add some symlinks in /usr/local/bin for "python", "python2" etc 
pointing at your preferred executables.


Next, _how_ are you installing extra packages like matplotlib etc?

I would advocate using MacPorts to install these if available; that leaves the 
arrangements in the hands of MacPorts and keeps things simple for you.


Otherwise, I would next suggest learning to use virtualenv to make a specific 
python "environment" for you work. This is easier than it sounds. The 
virtualenv command prepares a direct of your choice (eg $HOME/venv-2.7.7, to 
invent a name) which contains "python" and "pip" executables in the "bin" 
subdirectory which are crafted to automatically install and use packages 
entirely within the virtualenv directory, avoiding conflicts with other systems 
like OSX or MacPorts. This is great for experimentation of maintaining special 
setups of your own.


You can base the virtualenv of any of the installed Pythons (OSX, MacPorts, 
whatever), to get the installed libraries for free.


Please respond with further information and we can proceed from there.

Cheers,
Cameron Simpson 

To have no errors
Would be life without meaning
No struggle, no joy
- Haiku Error Messages 
http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use python to parse the subject line of emails, listen for and react to commands

2015-02-27 Thread Alan Gauld

On 27/02/15 19:43, Willie Sims wrote:

I have started working with 2.7 simply because I don't know when the error
is caused by me or because of version issues.
This is how far I have gotten,
I can access and pull emails that only come from a selected email address
(OWNER)
I have it parsed down to print the subject only
And I have it saving the string to a file output.txt
And I have it deleting the emails so they don't reread,
Now I want to eval the output.txt and act on the keyword that is written,,


That's nearly always a bad idea. eval is a big security risk, especially 
if applied to external input. And as you've discovered,

it makes debugging into a bit of a nightmare.


eval output.txt but get the error

Traceback (most recent call last):
   File "C:\Python27\New folder\start.py", line 82, in 
 start_command = eval(open("output.txt").read())
   File "", line 1, in 
NameError: name 'test' is not defined



That's probably because a line in output.txt contains the
word test which is not known to python. Without seeing
output.txt it's impossible to be more specific.


import sys
import imaplib
import getpass
import email
import email.header
import datetime

EMAIL_ACCOUNT = "myem...@gmail.com"
EMAIL_FOLDER = "INBOX"
OWNER = "willie14...@outlook.com"
COMMAND_FILE = open("output.txt","w")

def process_mailbox(M):
 rv, data = M.search(None, "From", (OWNER))
 if rv != 'OK':
 print "No messages found!"
 return
 for num in data[0].split():
 rv, data = M.fetch(num, '(RFC822)')
 if rv != 'OK':
 print "ERROR getting message", num
 return
 msg = email.message_from_string(data[0][1])
 decode = email.header.decode_header(msg['Subject'])[0]
 subject = unicode(decode[0])
 print 'Message %s: %s' % (num, subject)
COMMAND_FILE.write('%s' % (subject))
 COMMAND_FILE.close()
 #print 'Raw Date:', msg['Date']


I assume this is where the function is supposed to end?

Note that you are throwing away all the data you find. It is lost when 
the function exits. Are the print statements and file output sufficient?
Also note you have the file closure inside the loop. That means you only 
get the first item in the file before you close it I'm not clear what 
the loop is doing so it might be OK.



M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
 rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass())
except imaplib.IMAP4.error:
 print "LOGIN FAILED!!! "
 sys.exit(1)
print rv, data
rv, mailboxes = M.list()
if rv == 'OK':
 print "Mailboxes:"
 print mailboxes
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
 print "Processing mailbox...\n"
 process_mailbox(M)
 M.select('INBOX')  # select all trash
 M.store("1:*", '+FLAGS', '\\Deleted')  #Flag all Trash as Deleted
 M.expunge()
 M.close()
else:
 print "ERROR: Unable to open mailbox ", rv

M.logout()
start_command = eval(open("output.txt").read())


Traceback (most recent call last):
   File "C:\Python27\New folder\start.py", line 82, in 
 start_command = eval(open("output.txt").read())
   File "", line 1, in 
NameError: name 'test' is not defined


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use python to parse the subject line of emails, listen for and react to commands

2015-02-27 Thread Danny Yoo
> That's nearly always a bad idea. eval is a big security risk, especially if
> applied to external input. And as you've discovered,
> it makes debugging into a bit of a nightmare.

Yes, I concur with Alan.  eval() is definitely the wrong tool here.
It's **extraordinarily** dangerous in the context of consuming
arbitrary email input.

Did anyone teach you to use eval()?


An alternative approach to what you're considering is to define a
mapping from command name to functionality.  A quick-and-dirty
approach uses a dictionary, and may have enough power for what you're
trying to do.   Here's an example to demonstrate: let's say that we'd
like a calculator that takes a sequence of operations, like:

 zero
 inc
 inc
 double

and performs them in turn to zero out memory, increment, increment,
and double.  (getting us four).

Here's a brief sketch of what this might look like:


def zero(n):
return 0

def double(n):
return n * 2

def inc(n):
return n + 1

cmd_mapping = {
'zero': zero,
'double': double,
'inc': inc
}

## Run the calculator:
memory = 0
for cmd_string in 'inc inc inc double double inc'.split():
cmd = cmd_mapping[cmd_string]
memory = cmd(memory)
print memory



Here, we let a string dictate what's being computed, but in a
controlled fashion.  The "magic" here is the line:

cmd = cmd_mapping[cmd_string]

which gets a function, which we can just call later on.  Since
cmd_mapping is a dictionary whose contents we control, we know that
the only command being looked up has to be in the cmd_mapping.

Compare vs. an eval-based approach.  Unlike an approach that uses
eval(), if the command string contains bad values here, nothing too
bad will happen: at worst, we'll see a reliable dictionary lookup
error.  You'll even get a good line number in the stack trace!  If we
were to use eval, we would not.  In the worst case, we might
accidentally let an adversary dictate what our computer is going to
do, against our wishes.


For more discussion why eval() is almost certainly not the tool you
want to use, see:

http://code.activestate.com/lists/python-tutor/8/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use python to parse the subject line of emails, listen for and react to commands

2015-02-27 Thread Willie Sims
Sorry, I should clarify something No one has taught me anything, I have been 
trying to learn all this on my own. To be honest much of the code I have is 
taken from examples and modified for what I was trying to do. Talk about 
learning the Hard Way!!! 

I have actually made everything work
My test.py is simply a print ("it works")
The way I made it work was to execfile the subject +.py, Again this is a small 
embedded device (BeagleBone Black) that will be using a mobile hotspot for 
internet, the hotspot has a private ip (Verizon) so I cannot access the server 
from outside the network. As such and because I am looking for ways to minimize 
data usage I want to be able interact with the device through email 
commands.. 
The code is not pretty and I have no doubt that there are thousands of ways for 
it to be hacked or messed up since there is little to no error checking in it. 
I don’t know enough to do so yet sorry I am doing the best I can and have only 
started learning python a week ago. 
I still have to add in a more complex command system that will allow me to 
change a settings page. So for example if I sent an email to the devices 
registered email address with the command 
NEW_OWNER(emailaddr...@email.com),(PASSWORD) it would change the Owners email 
to the one listed in the subject and only accept emails from that email address 
if the password matches. 
My plan is to have an email template that the script will reply back with that 
will have the commands listed in html so when the command is clicked from the 
email it will automatically open the email app and add in the keyword in the 
subject line to help eliminate typo errors.. 

Each Keyword is an .py script that this script will start it does not yet have 
a way of doing more complex changes like what I listed above and while I wish I 
could say that I wrote all this script from scratch the truth is I butchered 
other scripts from examples and made it work. But It is helping me learn (ALONG 
WITH GIVING ME A LOT OF GRAY HAIRS) that being said I love how python can be 
SIMPLE or as complex as you need it!

Sorry for what may seem like dumb questions I really do want to thank all of 
you for your help! 
 
#!/usr/bin/env python

import sys
import imaplib
import getpass
import email
import email.header


EMAIL_ACCOUNT = "myem...@gmail.com" # the email account that py will check
EMAIL_FOLDER = "INBOX"
OWNER = "willie14...@outlook.com"
COMMAND_FILE = open("output.txt","w")


def process_mailbox(M):

rv, data = M.search(None, "From",(OWNER))
if rv != 'OK':
print "No messages found!"
return

for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return

msg = email.message_from_string(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = unicode(decode[0])
print '%s' % (subject)
COMMAND_FILE.write('%s' % (subject)+'.py')
COMMAND_FILE.close()
EX = '%s' %(subject)+'.py' #save the subject as an name.py
execfile (EX) # exe name as a py 

M = imaplib.IMAP4_SSL('imap.gmail.com')

try:
rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass())
except imaplib.IMAP4.error:
print "LOGIN FAILED!!! "
sys.exit(1)

print rv, data

rv, mailboxes = M.list()
#if rv == 'OK':
#print "Mailboxes:"
#print mailboxes

rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "Processing mailbox...\n"
process_mailbox(M)
M.select('INBOX')  
M.store("1:*", '+FLAGS', '\\Deleted')  #Flag all Trash as Deleted
M.expunge()
M.close()
else:
print "ERROR: Unable to open mailbox ", rv

M.logout()


-Original Message-
From: Danny Yoo [mailto:d...@hashcollision.org] 
Sent: Friday, February 27, 2015 7:27 PM
To: willie14...@outlook.com
Cc: Python Tutor Mailing List
Subject: Re: [Tutor] Use python to parse the subject line of emails, listen for 
and react to commands

> That's nearly always a bad idea. eval is a big security risk, 
> especially if applied to external input. And as you've discovered, it 
> makes debugging into a bit of a nightmare.

Yes, I concur with Alan.  eval() is definitely the wrong tool here.
It's **extraordinarily** dangerous in the context of consuming arbitrary email 
input.

Did anyone teach you to use eval()?


An alternative approach to what you're considering is to define a mapping from 
command name to functionality.  A quick-and-dirty approach uses a dictionary, 
and may have enough power for what you're
trying to do.   Here's an example to demonstrate: let's say that we'd
like a calculator that takes a sequence of operations, like:

 zero
 inc
 inc
 double

and performs them in turn to zero out memory, increment, increment, and double. 
 (getting us four).

Here's a brief sketch of what this might look like:


def zero(n):
retu