Re: [Tutor] Optparse question: if only certain values are acceptable

2009-05-09 Thread Alan Gauld


"Terry Carroll"  wrote 


The toy summary is that I want to have the following command format:

 prognam -f FORMAT

Where FORMAT, if specified, must be one of "X", "Y", or "Z".

In otherwords, if the user enters:

progname -f X

It runs, producing its output in format X.  Similar if "Y" or "Z" is
specified instead of "X".

But if the user specifies

progname -f A

I want it to spit up because A is not a recognized format.

I don't see anything in the docs that directly addresses this.  Is this 
something that I should use the callback parameter for?


Which part do you not understand or do you believe is not addresssed?
optparse can certainly allow you to specify the -f option and store the 
user entered value. 

Your code will have to act accordingly based on the value, optparse 
won't do that bit.


And for the invalid option value I think you can handle that too, 
in an else clause. 


Thats the easiest way to use it IMHO.

You can define a callback for your option but with only one 
option I dont see much point, you still need the value if/else 
inside the callback.


You can define the list of values too (via choice) but again 
catching anything other than X,Y,Z is just as easy using an 
else clause in your case.


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Optparse question: if only certain values are acceptable

2009-05-09 Thread Kent Johnson
On Fri, May 8, 2009 at 9:09 PM, Terry Carroll  wrote:

> It's exactly what I was looking for.  Thanks very much.
>
> Now I'm going to have to re-read the docs and see why I couldn't pick that
> up from them.

You have to read pretty far down, to the sections on Option attributes
and  Standard Option Types
http://docs.python.org/library/optparse.html#option-attributes
http://docs.python.org/library/optparse.html#standard-option-types

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


Re: [Tutor] Pythonic way to normalize vertical whitespace

2009-05-09 Thread spir
Le Fri, 08 May 2009 13:03:47 -0400,
pyt...@bdurham.com s'exprima ainsi:

[...]
> Approaches:
> 1. split text to list of lines that get stripped then:
> a. walk this list building a new list of lines that track and
> ignore extra blank lines
> -OR-
> b. re-join lines and replace '\n\n\n' wth' \n\n' until no more
> '\n\n\n' matches exist
> 2. use regular expressions to match and replace whitespace
> pattern of 3 or more adjacent \n's with surrounding whitespace
> 3. a 3rd party text processing library designed for efficiently
> cleaning up text
> Thanks!

You should try 1a and 1b if only to have written the code at least once ;-) The 
set of python string methods is very complete and practicle.
Still, in this case, 2 is rather straightforward and simple. If you cannot 
figure out the proper pattern, have a look at 
http://www.amk.ca/python/howto/regex/ and/or ask again the list. 

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code Dosent work.

2009-05-09 Thread David

Jacob Mansfield wrote:
not sure why i was using pygame, anyway the problem is with the saveing, 
press exit, the program will save to databox.txt, then start the program 
again giveing it the filename that it saved to, then look at the records.


Post the code you have now and I am sure someone can help point you in 
the right direction.

-david

--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] paramiko again

2009-05-09 Thread Matt Herzog
Hello again.

This code comes straight from the http://oreilly.com/catalog/9780596515829/ 
book.

The only actual code I changed was s/get/put on the second to last line. The 
author says I ought to be able to do this and have it Just Work. There are 
several things I don't understand. Would be nice if the books' author was on 
this list so I could ask him directly. Heh.

The code runs but does not upload any files. I would rather be specifying the 
local dir on the source machine rather than path on the destination. 

1. How can paramiko know whether the dir_path is on the local vs remote system?
2. I can't find sftp.put or sftp.get in ipython. Are they part of paramiko? If 
not, where do they come from?

Thanks for any insight.

--- begin --
#!/usr/bin/env python 
import paramiko 
import os 
hostname = '192.168.1.15' 
port = 22 
username = 'revjimjones' 
password = 'C0ol4id3' 
dir_path = '/home/revjimjones/logs' 
if __name__ == "__main__": 
t = paramiko.Transport((hostname, port)) 
t.connect(username=username, password=password) 
sftp = paramiko.SFTPClient.from_transport(t) 
files = sftp.listdir(dir_path) 
for f in files: 
print 'Uploading', f 
sftp.put(os.path.join(dir_path, f), f) 
t.close() 

 end ---


-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] paramiko again

2009-05-09 Thread David

Matt Herzog wrote:

Hello again.

This code comes straight from the http://oreilly.com/catalog/9780596515829/ 
book.

The only actual code I changed was s/get/put on the second to last line. The 
author says I ought to be able to do this and have it Just Work. There are 
several things I don't understand. Would be nice if the books' author was on 
this list so I could ask him directly. Heh.

The code runs but does not upload any files. I would rather be specifying the local dir on the source machine rather than path on the destination. 


1. How can paramiko know whether the dir_path is on the local vs remote system?
2. I can't find sftp.put or sftp.get in ipython. Are they part of paramiko? If 
not, where do they come from?

Thanks for any insight.

--- begin --
#!/usr/bin/env python 
import paramiko 
import os 
hostname = '192.168.1.15' 
port = 22 
username = 'revjimjones' 
password = 'C0ol4id3' 
dir_path = '/home/revjimjones/logs' 
if __name__ == "__main__": 
t = paramiko.Transport((hostname, port)) 
t.connect(username=username, password=password) 
sftp = paramiko.SFTPClient.from_transport(t) 
files = sftp.listdir(dir_path) 
for f in files: 
print 'Uploading', f 
sftp.put(os.path.join(dir_path, f), f) 
t.close() 


 end ---



Your going to have to read up on it. Download the source here;
http://github.com/robey/paramiko/downloads
Then look at the demos folder where you will see an example for sftp like;
# now, connect and use paramiko Transport to negotiate SSH2 across the 
connection

try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password, hostkey=hostkey)
sftp = paramiko.SFTPClient.from_transport(t)

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


[Tutor] Parsing Question

2009-05-09 Thread Michael Morrissey
Forgive me if I'm asking something stupid, but I'm not sure how to do this
(there are so many options in Python, I'm overwhelmed and confused). I have
a text file that looks like this:
1 the 126 name
2 of 127 very
3 to 128 through
4 and 129 just
5 a 130 form
6 in 131 much
7 is 132 great
etc...

Each has 2 numbers and 2 words on it. Each number is related to the word
that comes after it. So "1" and "the" are connected (kinda like a
dictionary), and "126" and "name" are related.

Using the above text file as an input, I'm trying to make an output that
lists all the words, one word per line, but none of the numbers. I was
hoping to get the words listed in order of their related numbers. So, the
output needs to look like:

the
of
to
 (skip to the 127th line)
very
through
just

I can open and loop over the text, I'm just not sure what commands I should
use to parse each line. Can anyone give me suggestions or examples?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Different Command Result Executing in Shell vs. Program

2009-05-09 Thread robert mcquirt

  
  
  
  
  
  
   
  
 
  
   Hi. I've not been working with Python very long and have run into a puzzling 
thing. I'm working on a program that needs to identify the filetype of files 
without extensions. Using the file command in the shell works fantastic, as in:
   
   
rob...@ubuntu:~$ file -b linuxlogotag
   JPEG image data, JFIF standard 1.01
   
   
I want to be able to perform the same task in a program for batch processing. 
After a bit of research, I found that I can execute the shell command from 
within a Python program using:
   
   
import os
   os.system('file -b /home/robert/linuxlogotag')
   
   
This also works fine. The problem is that when I try to replace the hard coded 
path/name with a string variable for looping, the results are not the same as 
the shell's. Here's the modified code I'm using for testing (the original code 
is from Bogdan's blog at http://bogdan.org.ua/).
   
   
import os, glob
   path = '/home/robert'
   for infile in glob.glob( os.path.join(path, '*.*') ):
 testCommand = "'file -b " + infile + "'"
 print testCommand,
 test = os.system(testCommand)
 print test
   
   
The code does indeed step through all the files in the directory and the 
testCommand string constructs properly. Yet, the output is not the same as 
shell results. All files, regardless of type, simply output the number 32512, 
with no description.

   
   I have searched forums, read the man file and perused several Python 
references and have not been able to find the answer. Why does the same command 
yield different results, depending on how it's run? Any education is greatly 
appreciated.

   
   Linux 2.6 (kubuntu 8.04)
   Python 2.5.2
   file command 4.21
  


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


Re: [Tutor] paramiko again

2009-05-09 Thread Alan Gauld

"Matt Herzog"  wrote

This code comes straight from the 
http://oreilly.com/catalog/9780596515829/ book.


The only actual code I changed was s/get/put on the second to last line.
The author says I ought to be able to do this and have it Just Work.



There are several things I don't understand.


What are they? You raise a few issues below but are they all?


The code runs but does not upload any files.


How do you know? Where are you looking for them?
What are you looking for?


I would rather be specifying the local dir on the source machine
rather than path on the destination.


Have you used normal ftp in its command line version?
The put command specifies the location on the  remote machine
where you want to store the files. This is normal ftp behaviour.


1. How can paramiko know whether the dir_path is on the
local vs remote system?


It always expects it to be on the remote machine I guess.


2. I can't find sftp.put or sftp.get in ipython.
Are they part of paramiko?


Yes. sftp is an object that you create with a call from paramiko.
The class exists in paramiko and you create an instance of it
called sftp.

   sftp = paramiko.SFTPClient.from_transport(t)


If not, where do they come from?


So get./put will be methods of whatever class
SFTPClient.from_transport() returns an instance of.


--- begin --
#!/usr/bin/env python
import paramiko
import os
hostname = '192.168.1.15'
port = 22
username = 'revjimjones'
password = 'C0ol4id3'
dir_path = '/home/revjimjones/logs'
if __name__ == "__main__":
   t = paramiko.Transport((hostname, port))
   t.connect(username=username, password=password)
   sftp = paramiko.SFTPClient.from_transport(t)
   files = sftp.listdir(dir_path)


This gets a list of the files from dir_path on the remote machine.


   for f in files:
   print 'Uploading', f
   sftp.put(os.path.join(dir_path, f), f)


So you are trying to upload files that currently exist on the
remote machine from your machine, that is unlikely to work.
It would only work if you happen to have files of exactly the
same name (updated versions maybe?). In that case you
will overwrite the remote server versions with your local copy.

I suspect you probably need to change the listdir() call to use
os.listdir(local_path) instead of stftp.listdir(). Where local_path
is wherever you store the files on the local mnachine that you
want to transfer.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] Parsing Question

2009-05-09 Thread Emile van Sebille

On 5/9/2009 4:23 PM Michael Morrissey said...
Forgive me if I'm asking something stupid, but I'm not sure how to do 
this (there are so many options in Python, I'm overwhelmed and 
confused). 


Look into the split method of string objects (which are what you should 
be reading from the file), then look into indexing the resulting list 
objects.  That should focus your attentions...


Emile




I have a text file that looks like this:

1 the 126 name 
2 of 127 very 
3 to 128 through 
4 and 129 just 
5 a 130 form 
6 in 131 much 
7 is 132 great 
etc...


Each has 2 numbers and 2 words on it. Each number is related to the word 
that comes after it. So "1" and "the" are connected (kinda like a 
dictionary), and "126" and "name" are related.


Using the above text file as an input, I'm trying to make an output that 
lists all the words, one word per line, but none of the numbers. I was 
hoping to get the words listed in order of their related numbers. So, 
the output needs to look like:


the
of
to
 (skip to the 127th line)
very
through
just

I can open and loop over the text, I'm just not sure what commands I 
should use to parse each line. Can anyone give me suggestions or examples?





___
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] Parsing Question

2009-05-09 Thread Alan Gauld


"Michael Morrissey"  wrote


a text file that looks like this:
1 the 126 name
2 of 127 very

Each has 2 numbers and 2 words on it. Each number is related to the word
that comes after it. So "1" and "the" are connected (kinda like a
dictionary), and "126" and "name" are related.

Using the above text file as an input, I'm trying to make an output that
lists all the words, one word per line, but none of the numbers. I was
hoping to get the words listed in order of their related numbers. So, the

I can open and loop over the text, I'm just not sure what commands I 
should

use to parse each line. Can anyone give me suggestions or examples?


For this a simple split() command should give you a list of 4 items.

I suggest you store each pair in a tuple (num, word) and put the
tuples in a list.

You can then sort the list and the tuples will be in numeric order
(element 0).

Then iterate over the list printing the word (element 1 of the tuple)

In pseudo code:

words = []
for line in open(fname):
 four = line.split()
 words.append( (four[0],four[1]) )
 words.append( (four[2],four[3]) )

for word in sorted(words):
   print word[1]

You should probably be able to do the first for loop as a list
comprehension,  but I can't think of how to get the split() call
embedded into it right now!

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] paramiko again

2009-05-09 Thread Matt Herzog
On Sun, May 10, 2009 at 12:53:49AM +0100, Alan Gauld wrote:
> Have you used normal ftp in its command line version?
> The put command specifies the location on the  remote machine
> where you want to store the files. This is normal ftp behaviour.

The server supports only sftp. Yeah, I could turn on ftp on some server but 
that would be more work. Heh.

> Yes. sftp is an object that you create with a call from paramiko.
> The class exists in paramiko and you create an instance of it
> called sftp.

Oh. I could call it anything, not that I'd want to. Thanks. I kept thinking I 
could find it using ipython.

> So you are trying to upload files that currently exist on the
> remote machine from your machine, that is unlikely to work.

Yes. That was not the plan though. Heh.

> I suspect you probably need to change the listdir() call to use
> os.listdir(local_path) instead of stftp.listdir(). Where local_path
> is wherever you store the files on the local mnachine that you
> want to transfer.

Yes, that is exactly what I wanted, os.listdir. But now I have another issue:
specifying the remote dir. When I login to the sftp server and type pwd:

sftp> pwd
Remote working directory: /

Is what I see. 

Also, I don't think I want "join" in the below, do I? Why do I want to "join" 
the two dirs? As far as I'm concerned, I just want to dump files into the only 
dir I can reach, which is "/" on the remote host's jail.

sftp.get(os.path.join(dir_path, f), f)

> 
> HTH,

It did help. Many thanks!
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/ 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] Different Command Result Executing in Shell vs. Program

2009-05-09 Thread Alan Gauld

"robert mcquirt"  wrote


import os
os.system('file -b /home/robert/linuxlogotag')
  
This also works fine. The problem is that when I try to replace 
the hard coded path/name with a string variable for looping, 
the results are not the same as the shell's. 



import os, glob
path = '/home/robert'
for infile in glob.glob( os.path.join(path, '*.*') ):
testCommand = "'file -b " + infile + "'"
print testCommand,
test = os.system(testCommand)


The problem is that the return code from os.system is the 
exit code of the command being executed - usually 0 for 
success or an error code. What you want is to actually 
capture the output of the command. To do that you need 
to look at the subprocess module which provides several 
examples  of what you want.
  
output is not the same as shell results. All files, regardless 
of type, simply output the number 32512, with no description.


That is the exit code of the file -b command. You would 
need to look at the file documentation to find out what 
it signifies!
  
PS. You can also find some simple examples using 
subprocess in the Using the OS topic of my tutorial.


HTH,

Alan G.

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


[Tutor] Triggering code on 1 minute intervale ..

2009-05-09 Thread Alex Feddor
.. What will be the best solution to trigger python code every minute as
soon as PC in on.

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


Re: [Tutor] Triggering code on 1 minute intervale ..

2009-05-09 Thread W W
On Sat, May 9, 2009 at 3:26 PM, Alex Feddor  wrote:

> .. What will be the best solution to trigger python code every minute as
> soon as PC in on.
>
an autorun script... and in your script use the time.sleep(60) I believe.

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


Re: [Tutor] Different Command Result Executing in Shell vs. Program

2009-05-09 Thread Dave Angel

robert mcquirt wrote:


 Hi. I've not been working with Python very long and have run into a puzzling 
thing. I'm working on a program that needs to identify the filetype of files 
without extensions. Using the file command in the shell works fantastic, as in:
   
   
rob...@ubuntu:~$ file -b linuxlogotag

   JPEG image data, JFIF standard 1.01
   
   
I want to be able to perform the same task in a program for batch processing. After a bit of research, I found that I can execute the shell command from within a Python program using:
   
   
import os

   os.system('file -b /home/robert/linuxlogotag')
   
   
This also works fine. The problem is that when I try to replace the hard coded path/name with a string variable for looping, the results are not the same as the shell's. Here's the modified code I'm using for testing (the original code is from Bogdan's blog at http://bogdan.org.ua/).
   
   
import os, glob

   path = '/home/robert'
   for infile in glob.glob( os.path.join(path, '*.*') ):
?testCommand = "'file -b " + infile + "'"
?print testCommand,
?test = os.system(testCommand)
?print test
   
   
The code does indeed step through all the files in the directory and the testCommand string constructs properly. Yet, the output is not the same as shell results. All files, regardless of type, simply output the number 32512, with no description.


   
   I have searched forums, read the man file and perused several Python references and have not been able to find the answer. Why does the same command yield different results, depending on how it's run? Any education is greatly appreciated.


   
   Linux 2.6 (kubuntu 8.04)

   Python 2.5.2
   file command 4.21
  
  
I think you have an extra set of quotes.  Remove the single quotes from 
the testCommand and see if that works any better.


  testCommand = "file -b " + infile


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


Re: [Tutor] Triggering code on 1 minute intervale ..

2009-05-09 Thread Dayo Adewunmi

Alex Feddor wrote:


.. What will be the best solution to trigger python code every minute 
as soon as PC in on. 


Cheers, Alex



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

A cron job?

Regards

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


[Tutor] Fwd: Sending Email - Outlook 2003 / Overcome Security Warning!

2009-05-09 Thread Alex Feddor
Hi

I would like to send email inside my Python code. On PC I have installed
Outlook 2003. On the web I found code bellow which is working however I need
every time manual clicked outlook security warning - alerting that outside
process would like to send email. Please advise how the security warning can
be tricked...
Here is working code sample:
from win32com.client import Dispatch

session = Dispatch("MAPI.session")

session.Logon('OUTLOOK')  # MAPI profile name

msg = session.Outbox.Messages.Add('Hellow', 'Test Message 400')

msg.Recipients.Add('ales',
'SMTP:alex.fed...@gmail.com
')

msg.Send()

session.Logoff()

On the web I found solution for VB.NET . There must be
similar solution also for Python.
http://www.add-in-express.com/docs/outlook-security-manager-automate.php


(vb.NET CODE)
Dim SecurityManager As New AddinExpress.Outlook.SecurityManager
SecurityManager.ConnectTo(outlookApp)

I tried running VB code inside Python however with no success as this is not
VBScript.
import win32com.client as win

vbscript = win.Dispatch("ScriptControl")

vbscript.language = "vbscript"

vbscript.addcode('Main\n\

*Sub Main*\n\

*OlSecurityManager.ConnectTo OutlookApp*\n\

*OlSecurityManager.DisableOOMWarnings = True*\n\

*End Sub*\n')

vbscript.eval("Main")

Looking forward to your advice ...

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