[Tutor] Match on current line and next line. Possible?

2005-02-08 Thread Tom Tucker
Hello! How can I instruct Python to match on the current line and the
next line?


Assumptions;
- We are reading in one line at a time


BROKEN EXAMPLE (discussion)
##
file = open('/somefile','r').readlines()
for line in file:
match_one = re.search('^Python', line)
match_two = re.search('^\tBLAH', line)
if match_one and nextline == match_two: 
   do_something()


Maybe this just isn't possible, since we are working line by line. 

Any suggestions? 


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


[Tutor] Does Python have anything like Perls format output?

2005-04-23 Thread Tom Tucker
Good evening!  Does Python have a print function similar to Perl
format output (example below)?

Thanks

Tom

format STDOUT =
@< @ @<<< [EMAIL PROTECTED] [EMAIL PROTECTED]
$code, $date,$descript,$amt,   $balance
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Is it possible to load variable into a regex string?

2005-04-27 Thread Tom Tucker
Hello all! I am trying to pass a variable to my re.compile string (see
broken example below). Is something like this possible?  Thanks!

regexstring = 'H\sb'
textstring = 'BLAH blah'
match = re.compile((%s) % (regexstring))  # ?
if match.search(line):
  print "I found it!"


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


[Tutor] Python sort with a delimiter

2005-05-11 Thread Tom Tucker
Good morning!  Does Python have a sort function thats supports a delimiter?  
For example, I want to sort the below hostnames, based on the city.   

Thanks!
Tom


Hostnames to be sorted
-
sys01-xxx-austin-tx
sys02-xxx-austin-tx
sys01-yyy-austin-tx
sys01-xxx-newark-oh
sys01-yyy-newark-oh
sys01-zzz-newark-oh
sys02-zzz-newark-oh



Unwanted Sorted Output 
---
>>> hostnames =
['sys01-xxx-austin-tx','sys02-xxx-austin-tx','sys01-yyy-austin-tx','sys01-xxx-newark-oh','sys01-yyy-newark-oh','sys01-zzz-newark-oh','sys02-zzz-newark-oh']
>>> hostnames.sort()
>>> hostnames
['sys01-xxx-austin-tx', 'sys01-xxx-newark-oh', 'sys01-yyy-austin-tx',
'sys01-yyy-newark-oh', 'sys01-zzz-newark-oh', 'sys02-xxx-austin-tx',
'sys02-zzz-newark-oh']



Successful Unix Sort Command 
? Can Python do something like this ?

~ >sort -t '-' -k3 /tmp/hostnames 
sys01-xxx-austin-tx
sys01-yyy-austin-tx
sys02-xxx-austin-tx
sys01-xxx-newark-oh
sys01-yyy-newark-oh
sys01-zzz-newark-oh
sys02-zzz-newark-oh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python sort with a delimiter

2005-05-11 Thread Tom Tucker
Interesting!  Thank you! 


On 5/11/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Tom Tucker wrote:
> > Good morning!  Does Python have a sort function thats supports a delimiter?
> > For example, I want to sort the below hostnames, based on the city.
> 
> This is easy to do in Python but you have to break it up into a few steps - 
> Python's sort doesn't
> know how to find fields but you can break the text into fields yourself, then 
> sort on the desired
> field. Here is one way:
> 
> import operator
> 
> hostnames = '''sys01-xxx-austin-tx
> sys02-xxx-austin-tx
> sys01-xxx-newark-oh
> sys01-yyy-newark-oh
> sys01-yyy-austin-tx
> sys01-zzz-newark-oh
> sys02-zzz-newark-oh'''.split()
> 
> # Create a list of field lists by splitting on the first two '-'
> fieldData = [ line.split('-', 2) for line in hostnames ]
> 
> # The key parameter must be a function that returns the key
> # value from a list item.
> # operator.itemgetter creates a function that accesses the desired index
> fieldData.sort(key=operator.itemgetter(2))
> 
> # Put the fields back together and print
> for line in fieldData:
>  print '-'.join(line)
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pickling in plain English

2005-05-24 Thread Tom Tucker
I am having trouble understanding the c|Pickle modules.  What does 
serializing and de-serializing objects mean? I have read the below
urls and I "think" I understand the process, but I can't visualize the
beneifts.  Can someone kindly explain pickling in lamens terms?

Thanks,

Tom


http://effbot.org/librarybook/cpickle.htm
http://www.python.org/doc/2.3.5/lib/module-cPickle.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pickling in plain English

2005-05-24 Thread Tom Tucker
John,
Thanks that did help.  Like usual, I was making it harder than necessary.  

Tom,
I concur! Well put!

On 5/24/05, Tom Cloyd <[EMAIL PROTECTED]> wrote:
> I just want to take a moment to express my deep appreciation for this
> List. As one who is learning Python as amateur, in my spare moments, and
> already getting it to do good work for me, these clear, beautifully worked
> descriptions of basic aspects of Python are simply an ongoing delight for
> me - and singularly useful to my study of Python. To those of you who give
> your time, thought, and experience here to those of us who need it, thank
> you so very much.
> 
> Tom Cloyd
> 
> On Tue, 24 May 2005 16:32:50 -0700, <[EMAIL PROTECTED]> wrote:
> 
> > Quoting Tom Tucker <[EMAIL PROTECTED]>:
> >
> >> I am having trouble understanding the c|Pickle modules. What does
> >> serializing and de-serializing objects mean? I have read the below
> >> urls and I "think" I understand the process, but I can't visualize the
> >> beneifts. Can someone kindly explain pickling in lamens terms?
> >
> > It's actually astonishingly simple, once you get the hang of it.
> >
> > example:
> >
> >>>> arr = [1, 3, 5, 7]
> >>>> dct = {'foo':1, 'bar':2, 'baz':3}
> >>>> st = "re: your mail"
> >>>>
> >>>> import pickle
> >>>> f = file('dump', 'wb')
> >>>> pickle.dump((arr, dct, st), f)
> >>>> f.close()
> >>>> ^Z
> >
> > [new instance of python]
> >
> >>>> import pickle
> >>>> f = file('dump', 'r')
> >>>> res = pickle.load(f)
> >>>> res
> > ([1, 3, 5, 7], {'bar': 2, 'foo': 1, 'baz': 3}, 're: your mail')
> >
> > [and you can look at the file 'dump' on your HDD, if you want to]
> >
> > Basically, pickle allows you to write any python object to disk and the
> > load it
> > back again with very little effort.  This is important if you want your
> > program
> > to be able to save state between instances.
> >
> > You can pickle more complex objects; the only restriction is that
> > pickle.load
> > must be able to find the module where the classes are defined.
> >
> > [there are some other restrictions, but you can do a lot without
> > worrying about
> > them]
> >
> > Does this help?
> >
> 
> 
> 
> --
> 
> ==
> Tom Cloyd
> Bellingham, Washington, U.S.A: (360) 920-1226
> << BestMindHealth.com >>
> ==
> 
> Using Opera's revolutionary e-mail client (program):
> http://www.opera.com/mail/
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Covert numbers to hex fails

2005-05-24 Thread Tom Tucker
Good evening!  I am trying to pass a number variable and have it
converted to hex.  Any recommendations on how to achieve this?  Thank
you.

FAILS
--
>>> value = 1234567890
>>> hexoutput = hex('%d' % (value))
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: hex() argument can't be converted to hex
>>> 



WORKS
-
>>> hexoutput = hex(1234567890)
>>> print hexoutput
0x499602d2
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Covert numbers to hex fails

2005-05-24 Thread Tom Tucker
Thanks!I see the mistake.  

On 5/24/05, Tony Meyer <[EMAIL PROTECTED]> wrote:
> > FAILS
> > --
> > >>> value = 1234567890
> > >>> hexoutput = hex('%d' % (value))
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> > TypeError: hex() argument can't be converted to hex
> 
> Just don't convert the number to a string, e.g:
> 
> >>> value = 1234567890
> >>> hexoutput = hex(value)
> >>> hexoutput
> '0x499602d2'
> 
> =Tony.Meyer
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Covert numbers to hex fails

2005-05-25 Thread Tom Tucker
Alan,
Thanks!  Your response was helpful.  I was trying to pass a number
variable to the hex object.  The output associated with this would be
referenced in the hexoutput variable.

I guess this is as simple as
hexoutput = "%X"  % (value)

Thanks again for the help,

Tom

On 5/25/05, Alan G <[EMAIL PROTECTED]> wrote:
> 
> > Good evening!  I am trying to pass a number variable and have it
> > converted to hex.  Any recommendations on how to achieve this?
> 
> You appear to have answered your own question below.
> What exactly is the problem?
> 
> FAILS
> --
> >>> value = 1234567890
> >>> hexoutput = hex('%d' % (value))
> 
> WORKS
> -
> >>> hexoutput = hex(1234567890)
> 
> Are you trying to insert a hex representation of the number into a
> string for printing? If so the easiest way is using string format
> characters:
> 
> >>> print "In hex: %d = %X" % (42,42)
> In hex: 42 = 2A
> 
> HTH,
> 
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple lines with the command line

2007-04-16 Thread Tom Tucker

Have you tried the "email" module?

http://docs.python.org/lib/module-email.html



On 4/16/07, Kharbush, Alex [ITCSV] <[EMAIL PROTECTED]> wrote:



I want to use the UNIX Mail program with python

I need multiple entries with the os.system(cmd)line

I will need something like this

#output to unix

Mail [EMAIL PROTECTED] -f [EMAIL PROTECTED]
Subject : hello alex

#message part
how is it going

#the end of mail
.

When I write os.system("Mail [EMAIL PROTECTED]") my program will send email
to where i want it to go

MY PROBLEM is that i need to enter multiple lines of input into unix.
os.system() takes only one argument

thanks for the time

___
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] Multiple lines with the command line

2007-04-16 Thread Tom Tucker

Sorry, you mentioned you wanted to use the Unix mail program.

Is your information static? If yes, how bout this.
cmd = "mail -s 'my subject' [EMAIL PROTECTED] <[EMAIL PROTECTED]> <
/path/to/static_message"
os.system(cmd)

Similar approach could be to capture the users message body as command line
arguments, save them to a file, and then include them in the execution. Not
very sexy, but it will work.


example
##
~>/tmp/mail_wrapper.py  this is the body of my message
['this', 'is', 'the', 'body', 'of', 'my', 'message']


#!/usr/bin/env python
import sys
message = sys.argv[1:]

# save message variable to a file
# execute mail command < file




On 4/16/07, Tom Tucker < [EMAIL PROTECTED]> wrote:


Have you tried the "email" module?

http://docs.python.org/lib/module-email.html



On 4/16/07, Kharbush, Alex [ITCSV] <[EMAIL PROTECTED]> wrote:

>
> I want to use the UNIX Mail program with python
>
> I need multiple entries with the os.system(cmd)line
>
> I will need something like this
>
> #output to unix
>
> Mail [EMAIL PROTECTED] -f [EMAIL PROTECTED]
> Subject : hello alex
>
> #message part
> how is it going
>
> #the end of mail
> .
>
> When I write os.system("Mail [EMAIL PROTECTED]") my program will send
> email to where i want it to go
>
> MY PROBLEM is that i need to enter multiple lines of input into unix.
> os.system() takes only one argument
>
> thanks for the time
>
> ___
> 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] version and path

2007-05-14 Thread Tom Tucker
I would use the traditional Unix find command to find the various
versions installed.

find / -name "python*"



On 5/14/07, linda.s <[EMAIL PROTECTED]> wrote:
> how to check how many versions of Python i have in my mac machine?
> also, how to put the path to the version I desire?
> Thanks a lot!
> ___
> 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] Web

2007-05-14 Thread Tom Tucker
Jeff,
Can you clarify what you are looking to do.  You have a small program
that you want to distribute via Internet (allow users to view,
download, etc)? You want to use Python to create a website? Integrate
your program into a web page (modpython for example).



On 5/14/07, Jeff Molinari <[EMAIL PROTECTED]> wrote:
> How exacly can you use python to create a web site? I have a small program
> I'm working on that I would like to create available via internet but I dont
> know how to make python work through internet.
>
>  
> Be a better Globetrotter. Get better travel answers from someone who knows.
> Yahoo! Answers - Check it out.
> ___
> 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] version and path

2007-05-14 Thread Tom Tucker
If you let the command finish, you should have seen the following or
something equivalent below.  Running the find command I sent you
works, however you will see some noise (Permission Denied message).
These are common messages because your general user account is trying
to access restricted parts of the filesystem.  This is normal
behavior.

Here are some example files on my Mac system.

/usr/bin/python
/usr/bin/python2.3
/usr/bin/pythonw
/usr/bin/pythonw2.3
/usr/lib/python2.3

I'm assuming you are running a stock version of OS X and you have NOT
installed Python manually.  The typical Python path on my version OS X
(11.4) is /usr/bin/python. /usr/bin/python is a link to the actual
Python binary installed at /usr/bin/python2.3

For example...

>which python
/usr/bin/python

>ls -l /usr/bin/python
lrwxr-xr-x   1 root  wheel  9 Jan 17  2006 /usr/bin/python -> python2.3


Does this help?



On 5/14/07, linda. s <[EMAIL PROTECTED]> wrote:
> On 5/14/07, Tom Tucker <[EMAIL PROTECTED]> wrote:
> > I would use the traditional Unix find command to find the various
> > versions installed.
> >
> > find / -name "python*"
> >
> >
> >
> > On 5/14/07, linda.s <[EMAIL PROTECTED]> wrote:
> > > how to check how many versions of Python i have in my mac machine?
> > > also, how to put the path to the version I desire?
> > > Thanks a lot!
>
> That is what I got:
> find: /.Spotlight-V100: Permission denied
> find: /.Trashes: Permission denied
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2007-05-18 Thread Tom Tucker

Very cool! Where is Inky, Blinky, Pinky, and Clyde? ;-) Maybe dog catchers
would be better foes for Dusty.


On 5/18/07, Teresa Stanton <[EMAIL PROTECTED]> wrote:


Hi all:

Remember when I was having problems moving my .gif around my Tkinter maze?
Well, I was going about it all wrong (as some pointed out).  What REALLY
works is a graph.  I used a dictionary, the keys are main coordinates that
are a path that the .gif follows using a greedy algorithm that finds all
the
paths to a determined location.  The definition of the keys are all the
possible directions the .gif can go.  Not all paths are covered on
purpose.
In any case, here is the updated code, for those who asked me to post it.
I'm hoping to have it completed next week, without the gif of my dog (lol)
and a bad guy or two to follow the gif that will be used in the maze.

The next thing I have to do is bind an event to the arrow keys that will
update the current x and y position.. I haven't quite worked that out yet.
Right now the gif moves through the maze based on the path.  Now the event
(an keyboard arrow) should tell it to go to that direction at the next
turn.
The function that defines the move is called move_gif().  I could use some
ideas because I keep getting stuck on the details. How does the event
effect
the current course through the maze? Does it somehow update the current
call
to find_all_paths?
Or some sort of Helper function that interrupts the current path at a
vertex
giving it a new path?  That one seems most likely, but I'm still stuck on
how to stop at the vertex and move to the new location.  Any help would be
appreciated.

TYIA

T

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



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


[Tutor] Continue Matching after First Match

2007-05-18 Thread Tom Tucker

Please forgive the colors, just trying to help illustrate my question.

The below code snipet works as designed, however the regex matches once and
exits.  I want it to continue matching and printing until EOF.  Any
suggestions?

Why the cStringIO stuff?  The input data shown below is collected from
os.popen.  I was trying to find an easy way of matching my regex.  Matching
with a string seemed easier than looping through the ouput collected.  Hmm.
Come to think of it, I guess I could match on the first "^dn" catpure that
output and then keep looping until "^cn:" is seen. Then repeat.

Anyways, any suggestions to fix the below code?

Thanks for the help,



Code Snipet
###
multi_regex = re.compile(r'dn: uid=(\w+)..*cn: ((\w+ \w+)|(\w+ \w+\.
\w+))..*(?=dn:)', re.MULTILINE| re.DOTALL)
output = os.popen(command).readlines()
voutput = cStringIO.StringIO()
for line in output:
 voutput.write(line)
 contents = voutput.getvalue()  #<-- contents is 
match = re.search(multi_regex, contents)
if match:
   print match.group(1)
   print match.group(2)



Blue = is what the regex matches
Red = regex groups # match.group(#)
Green = next regex groups not being captured

INPUT Data
###
version: 1
dn: uid=jtucker,ou=people,dc=companyA,dc=com
host: hostA
host: hostB
host: hostC
description: other
gecos: John Tucker
gidNumber: 1
uidNumber: 1157
sn: Tucker
cn: John Tucker
uid: jtucker
objectClass: top
objectClass: account
objectClass: person
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetorgperson
objectClass: organizationalPerson
loginShell: /usr/bin/ksh
homeDirectory: /home/jtucker
dn: uid=ttucker,ou=people,dc=companyA,dc=com
loginShell: /usr/bin/zsh
host: hostZ
host: hostC
uid: ttucker
cn: Tom Tucker
sn: Tucker
objectClass: top
objectClass: account
objectClass: person
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetorgperson
objectClass: organizationalPerson
uidNumber: 108
gidNumber: 102
gecos: Tom Tucker
homeDirectory: /home/ttucker
description: system


Current OUTPUT
#
jtucker
John Tucker


Desired OUTPUT
####
jtucker
John Tucker
ttucker
Tom Tucker
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Continue Matching after First Match

2007-05-18 Thread Tom Tucker

Disregard! Looks like part of my problem is the regex string.

On 5/18/07, Tom Tucker <[EMAIL PROTECTED]> wrote:


Please forgive the colors, just trying to help illustrate my question.

The below code snipet works as designed, however the regex matches once
and exits.  I want it to continue matching and printing until EOF.  Any
suggestions?

Why the cStringIO stuff?  The input data shown below is collected from
os.popen.  I was trying to find an easy way of matching my regex.
Matching with a string seemed easier than looping through the ouput
collected.  Hmm.  Come to think of it, I guess I could match on the first
"^dn" catpure that output and then keep looping until "^cn:" is seen. Then
repeat.

Anyways, any suggestions to fix the below code?

Thanks for the help,



Code Snipet
###
multi_regex = re.compile(r'dn: uid=(\w+)..*cn: ((\w+ \w+)|(\w+ \w+\.
\w+))..*(?=dn:)', re.MULTILINE| re.DOTALL)
output = os.popen(command).readlines()
voutput = cStringIO.StringIO()
for line in output:
  voutput.write(line)
  contents = voutput.getvalue()  #<-- contents is 
match = re.search(multi_regex, contents)
if match:
print match.group(1)
print match.group(2)



Blue = is what the regex matches
Red = regex groups # match.group(#)
Green = next regex groups not being captured

INPUT Data
###
version: 1
dn: uid= jtucker,ou=people,dc=companyA,dc=com
host: hostA
host: hostB
host: hostC
description: other
gecos: John Tucker
gidNumber: 1
uidNumber: 1157
sn: Tucker
cn: John Tucker
uid: jtucker
objectClass: top
objectClass: account
objectClass: person
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetorgperson
objectClass: organizationalPerson
loginShell: /usr/bin/ksh
homeDirectory: /home/jtucker
dn: uid=ttucker ,ou=people,dc=companyA,dc=com
loginShell: /usr/bin/zsh
host: hostZ
host: hostC
uid: ttucker
cn: Tom Tucker
sn: Tucker
objectClass: top
objectClass: account
objectClass: person
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetorgperson
objectClass: organizationalPerson
uidNumber: 108
gidNumber: 102
gecos: Tom Tucker
homeDirectory: /home/ttucker
description: system


Current OUTPUT
#
jtucker
John Tucker


Desired OUTPUT
########
jtucker
John Tucker
ttucker
Tom Tucker



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


Re: [Tutor] Continue Matching after First Match

2007-05-21 Thread Tom Tucker

Alan & Martin,

Thanks for the feedback and suggestions.  Kodos is a great tool.  I use it
regularly to debug my regex mistakes.  It can also be a excellent learning
tool to those unfamiliar with regular expressions.   Thanks for the
Python-LDAP link and ldif example code.

On 5/20/07, Martin Walsh <[EMAIL PROTECTED]> wrote:


Hi Tom,

Tom Tucker wrote:
> Why the cStringIO stuff?  The input data shown below is collected from
> os.popen.  I was trying to find an easy way of matching my regex.

Ah, ldap...



Oh yes,


Matching with a string seemed easier than looping through the ouput
> collected.  Hmm.  Come to think of it, I guess I could match on the
> first "^dn" catpure that output and then keep looping until "^cn:" is
> seen. Then repeat.

Honestly, I'm not very good with regular expressions -- and try to avoid
them when possible. But in cases where they seem to be the best option,
I have formed a heavy dependence on regex debuggers like kodos.
http://kodos.sourceforge.net/




Kodos is an excellent regex tool.  I use it regularly to verify regex
strings.


Anyways, any suggestions to fix the below code?


Have you had a look at the python-ldap package?

http://python-ldap.sourceforge.net/




Thanks.  I  checked the Python module index page and

You could probably access ldap directly with python, if that's an

option. Or, you could roll your own ldif parser (but make sure your data
contains a newline between each dn, or the parser will choke with a
'ValueError: Two lines starting with dn: in one record.'):

import ldif
from cStringIO import StringIO

class MyLDIF(ldif.LDIFParser):
def __init__(self, inputfile):
ldif.LDIFParser.__init__(self, inputfile)
self.users = []

def handle(self, dn, entry):
self.users.append((entry['uid'], entry['cn']))

raw = """\

"""

if __name__ == '__main__':
io = StringIO(raw)
lp = MyLDIF(io)
lp.parse()
for user in lp.users:
uid = user[0][0]
cn = user[1][0]
print uid
print cn

... or ...

You could also use ldif.LDIFRecordList directly without creating a
custom parser class which would return a list of (dn, entry) tuples. The
module author warns that 'It can be a memory hog!', and I can imagine
this is true if you are working with a particularly large ldap directory.

io = StringIO(raw)
directory = ldif.LDIFRecordList(io)
directory.parse()
for dn, entry in directory.all_records:
print entry['uid'][0]
print entry['cn'][0]

___
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] string replacement

2007-05-21 Thread Tom Tucker

One way is...

CODE

#!/usr/bin/python

handle = file("test.txt",'a')
number = 1
for num in range(1,5):
   handle.write("TestingSome%s\n" % (number))
   handle.write("something%s-test\n" % (number))
   number += 1
handle.close()

OUTPUT

TestingSome1
something1-test
TestingSome2
something2-test
TestingSome3
something3-test
TestingSome4
something4-test



On 5/21/07, Andreas Kostyrka <[EMAIL PROTECTED]> wrote:


-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Well, there are a number of ways to achieve this, but the classical one
would be:

"""abc%(i)s
def%(i)s
""" % dict(i=1)

Andreas

Chandrashekar wrote:
> Hi,
>
> I am trying to do something like this in python. Can you please help?
>
> handle= open('test.txt','a')
>
> handle.write('''
>
> Testsomething$i
>
> something$i-test
>
> '''
> )
>
> when i write into the file, i would like to have the output like this.
>
> Testsomething1
> something1-test
> Testsomething2
> something2-test
> Testsomething3
> something3-test
> ...
>
> I can perform the same using a loop. But how do i append i (1,2,..n)
> while i am writing into the file.(I mean inside the handle.write)
>
> Thanks,
> Chandru
>
> 
> Pinpoint customers
> <
http://us.rd.yahoo.com/evt=48250/*http://searchmarketing.yahoo.com/arp/sponsoredsearch_v9.php?o=US2226&cmp=Yahoo&ctv=AprNI&s=Y&s2=EM&b=50
>who
> are looking for what you sell.
>
>
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGUVPtHJdudm4KnO0RAgZwAJ9XOamfWZHd1CXYGuLIlOP11p7PWwCgxPD7
UILsdZNbknZz4zq71EuvxSs=
=0y02
-END PGP SIGNATURE-
___
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] leave tutorial

2007-05-30 Thread Tom Tucker

http://mail.python.org/mailman/listinfo/tutor


On 5/30/07, Kriti Satija <[EMAIL PROTECTED]> wrote:


i want to leave the tutorial


  Looking for people who are YOUR TYPE? Find them at
in.groups.yahoo.com
___
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] how can I compare a local directory or file with a remote one

2007-06-20 Thread Tom Tucker

What are we comparing? Size of files, number of files in a directory, md5sum
of the files, directory size, etc?  What about rsync?  You can use rsync to
compare directories between a source and destiantion system and just report
differences.  For example, comparing /etc directores between two RHEL
4.5systems.



On 6/20/07, Richard Querin <[EMAIL PROTECTED]> wrote:


I'm interested in writing a quick script that would run a diff-type
command that would compare a local directory to a remote one to
identify the changes in the files within that directory.

I was initially thinking that I would maybe use the linux diff command
in conjunction with the wget command (or something similar) to create
a local copy but that involves downloading files. Is there any way in
python to do a similar thing but without having to download a copy of
the remote files/directories?

Any ideas?
___
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] Regular Expression help

2007-06-27 Thread Tom Tucker

I think I have a solution.

File

(0012,0042) Clinical Trial Subject Reading ID LO 1
(0012,0050) Clinical Trial Time Point ID LO 1
(0012,0051) Clinical Trial Time Point Description ST 1
(0012,0060) Clinical Trial Coordinating Center Name LO 1
(0018,0010) Contrast/Bolus Agent LO 1
(0018,0012) Contrast/Bolus Agent Sequence SQ 1
(0018,0014) Contrast/Bolus Administration Route Sequence SQ 1
(0018,0015) Body Part Examined CS 1


Script
#
#!/usr/bin/python

import re

#matchstr regex flow
# (\(\d+,\d+\)) # (0018,0014)
# \s   # [space]
# (..*)# Contrast/Bolus Administration Route Sequence
# \s   # space
# ([a-z]{2}) # SQ - two letters and no more
# \s  # [space]
# (\d)# 1 - single digit
# re.I)   # case insensitive

matchstr = re.compile(r"(\(\d+,\d+\))\s(..*)\s([a-z]{2})\s(\d)",re.I)
myfile = open('/tmp/file','r')

for line in myfile.readlines():
   regex_match = matchstr.match(line)
   if regex_match:
   print regex_match.group(1) + ";" + regex_match.group(2) +
";" + regex_match.group(3) + ";" + regex_match.group(4)


Output
#
(0012,0042);Clinical Trial Subject Reading ID;LO;1
(0012,0050);Clinical Trial Time Point ID;LO;1
(0012,0051);Clinical Trial Time Point Description;ST;1
(0012,0060);Clinical Trial Coordinating Center Name;LO;1
(0018,0010);Contrast/Bolus Agent;LO;1
(0018,0012);Contrast/Bolus Agent Sequence;SQ;1
(0018,0014);Contrast/Bolus Administration Route Sequence;SQ;1
(0018,0015);Body Part Examined;CS;1


On 6/27/07, Gardner, Dean <[EMAIL PROTECTED]> wrote:


 Hi

I have a text file that I would like to split up so that I can use it in
Excel to filter a certain field. However as it is a flat text file I need to
do some processing on it so that Excel can correctly import it.

File Example:
tag descVR  VM
(0012,0042) Clinical Trial Subject Reading ID LO 1
(0012,0050) Clinical Trial Time Point ID LO 1
(0012,0051) Clinical Trial Time Point Description ST 1
(0012,0060) Clinical Trial Coordinating Center Name LO 1
(0018,0010) Contrast/Bolus Agent LO 1
(0018,0012) Contrast/Bolus Agent Sequence SQ 1
(0018,0014) Contrast/Bolus Administration Route Sequence SQ 1
(0018,0015) Body Part Examined CS 1

What I essentially want is to use python to process this file to give me

(0012,0042); Clinical Trial Subject Reading ID; LO; 1
(0012,0050); Clinical Trial Time Point ID; LO; 1
(0012,0051); Clinical Trial Time Point Description; ST; 1
(0012,0060); Clinical Trial Coordinating Center Name; LO; 1
(0018,0010); Contrast/Bolus Agent; LO; 1
(0018,0012); Contrast/Bolus Agent Sequence; SQ ;1
(0018,0014); Contrast/Bolus Administration Route Sequence; SQ; 1
(0018,0015); Body Part Examined; CS; 1

so that I can import to excel using a delimiter.

This file is extremely long and all I essentially want to do is to break
it into it 'fields'

Now I suspect that regular expressions are the way to go but I have only
basic experience of using these and I have no idea what I should be doing.

Can anyone help.

Thanks

DISCLAIMER:
Unless indicated otherwise, the information contained in this message is
privileged and confidential, and is intended only for the use of the
addressee(s) named above and others who have been specifically authorized to
receive it. If you are not the intended recipient, you are hereby notified
that any dissemination, distribution or copying of this message and/or
attachments is strictly prohibited. The company accepts no liability for any
damage caused by any virus transmitted by this email. Furthermore, the
company does not warrant a proper and complete transmission of this
information, nor does it accept liability for any delays. If you have
received this message in error, please contact the sender and delete the
message. Thank you.

___
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] how do I input " as part of a string?

2007-07-13 Thread Tom Tucker

Try escaping the quotes.


f = open("data.ini","w")
f.write("my \"car\" has been a great one for me")
f.close()


~ >cat data.ini
my "car" has been a great one for me

On 7/13/07, elis aeris <[EMAIL PROTECTED]> wrote:


also, why does the interpreter restart when I run this?



f = open("data.ini","w")

f.write("yeahppe")

f.close()
___
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] automate daily tasks

2007-09-11 Thread Tom Tucker
Are you looking for a method to automate the execution of your Python
program? Is that the question?
If yes, have you tried using cron or at jobs (man cron or man at).

Tom

On 9/11/07, chinni <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> I am working on MAC OS x my project is updating all ready installed
> products.I have to automate this in python.so,can any one will give some
> suggestions and some examples how to automate.some basic things of my
> project is ... i think u all know that there will be a plist file for each
> product in mac.i have to degrade the version of plist automatically
> through my script and it has to edit the plist file and degrade it to least
> version and as to check for updates by itself.like this there are 15
> products under this now each time i can't go and degrade the version and
> save it and run updates manually..so,that can any one plz..tell me how to
> start from first step...
>
> --
> Best Regards,
> M.Srikanth Kumar,
>
>
> ___
> 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] Tutor Digest, Vol 43, Issue 36

2007-09-12 Thread Tom Tucker
Comments below.

On 9/12/07, chinni <[EMAIL PROTECTED]> wrote:
>
> Hi,
> Thanx for Responding for the Query.
> The actual Output what i want is my python script has to change the
> version number(degrade) in plist file.and automatically change the time to
> 25 hrs more from present time cause my product will check for updates for
> every 25hrs.
> But u all told that cronjob and at inbuilt features of UNIX but every time
> ihave to manually go and change the version.i can't so that's why i want
> to write a script for this
>
> 1.open the app
>
   2.degrade the version automatically to least version
What is the manual process for this task?  More info is need here.  Is plist
a text file?

3.change the time to 25hrs more

Are we changing the system time 25 hours ahead, changing a time variable in
the script, or flag/timestamp in the plist file?


4.check for updates(1.force Update and silent update)

This is achievable with a system call.


5.the whole procedure is for admin and non-admin users

We change the unix file permission to restrict which user and groups are
allowed access to the file.


can i implement this by using both python and shell scripting.if possible
> give the reference book for quick learn.

>From what we know so far, yes.  Please share what you have coded so far.


Message: 2
> > Date: Tue, 11 Sep 2007 22:24:37 +0530
> > From: chinni <[EMAIL PROTECTED]>
> > Subject: [Tutor] automate daily tasks
> > To: tutor@python.org
> > Message-ID:
> > <[EMAIL PROTECTED]>
> > Content-Type: text/plain; charset="iso-8859-1"
> >
> > Hi all,
> >
> > I am working on MAC OS x my project is updating all ready installed
> > products.I have to automate this in python.so,can any one will give some
> > suggestions and some examples how to automate.some basic things of my
> > project is ... i think u all know that there will be a plist file for
> > each
> > product in mac.i have to degrade the version of plist automatically
> > through
> > my script and it has to edit the plist file and degrade it to least
> > version
> > and as to check for updates by itself.like this there are 15 products
> > under
> > this now each time i can't go and degrade the version and save it and
> > run
> > updates manually..so,that can any one plz..tell me how to start from
> > first
> > step...
> >
> > --
> > Best Regards,
> > M.Srikanth Kumar,
> > -- next part ------
> > An HTML attachment was scrubbed...
> > URL:
> > http://mail.python.org/pipermail/tutor/attachments/20070911/75c73c5e/attachment-0001.htm
> >
> > --
> >
> > Message: 3
> > Date: Tue, 11 Sep 2007 13:03:54 -0400
> > From: "Tom Tucker" < [EMAIL PROTECTED]>
> > Subject: Re: [Tutor] automate daily tasks
> > To: [EMAIL PROTECTED]
> > Cc: tutor@python.org
> > Message-ID:
> > <[EMAIL PROTECTED]>
> > Content-Type: text/plain; charset="iso-8859-1"
> >
> > Are you looking for a method to automate the execution of your Python
> > program? Is that the question?
> > If yes, have you tried using cron or at jobs (man cron or man at).
> >
> > Tom
> >
> > On 9/11/07, chinni < [EMAIL PROTECTED]> wrote:
> > >
> > > Hi all,
> > >
> > > I am working on MAC OS x my project is updating all ready installed
> > > products.I have to automate this in python.so,can any one will give
> > some
> > > suggestions and some examples how to automate.some basic things of my
> > > project is ... i think u all know that there will be a plist file for
> > each
> > > product in mac.i have to degrade the version of plist automatically
> > > through my script and it has to edit the plist file and degrade it to
> > least
> > > version and as to check for updates by itself.like this there are 15
> > > products under this now each time i can't go and degrade the version
> > and
> > > save it and run updates manually..so,that can any one plz..tell me how
> > to
> > > start from first step...
> > >
> > > --
> > > Best Regards,
> > > M.Srikanth Kumar,
> > >
> > >
> > > ___
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> > >
> > -- next part --
> > An HTML attachment was scrubbed...
> > URL: 
> > http://mail.python.org/

Re: [Tutor] how to add arguments to a command line program

2007-09-14 Thread Tom Tucker
See optparse  or
sys.argv.



On 9/14/07, shawn bright <[EMAIL PROTECTED]> wrote:
>
> lo there all,
>
> i want to write a program that will be called from another program.
> I need to pass it one variable.
> i suppose i could also do this with a module, and create a new instance of
> whatever i want to pass it to,
> but all the same, how would i go about this. like if i had a program that
> i wanted to pass a number into.
>
> i am sure i do it with sys args, but don't know how.
>
> like
>
> python run_my_script.py somevar
>
> does this make sense ?
>
> The other way to do this is create a class i suppose that could be called.
> Should probably do it that way, but still, i would like to know.
>
> thanks.
>
> shawn
>
> ___
> 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] how to match regular expression from right to left

2007-09-16 Thread Tom Tucker
Yep, looks like it.

>>> line = 'us::blah blah2 1002 blah3'
>>> import re
>>> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
>>> if TAG_pattern.search(line):
... print 'we have a match'
...
we have a match
>>>

>>> line2 ='us::blah blah2 1001 blah3'
>>> if TAG_pattern.search(line2):
... print 'we have a match'
... else:
... print 'no match found'
...
no match found



On 9/16/07, 王超 <[EMAIL PROTECTED]> wrote:
> hi,
>
> I want to match the regular expression from right to left, such as:
>
> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
> TAG_pattern.search(line)
>
> Does the search method support this?
>
> Thanks,
> Daniel
>
> ___
> 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] data type conversion for print statement

2007-09-25 Thread Tom Tucker
Excerpt from an email Danny Yoo sent to me and the list in 2005.  I had the
same question. ;-)


Hi Tom,

The 'print' statement is hardcoded to add a space between elements.
print is meant to make output easy, at the cost of control.


If we need more fine-grained control over output, we may want to take a
look at the sys.stdout object; it's a file object that corresponds to the
output we send to the user.

###
>>> import sys
>>> sys.stdout
', mode 'w' at 0x2a060>
###

As a file object, we can use the methods that files provide:

   http://www.python.org/doc/lib/bltin-file-objects.html

But the one we'll probably want is 'write()': the write() method of a file
object lets us send content out, without any adulteration:

##
>>> import sys
>>> for i in range(5):
... sys.stdout.write('hello' + str(i))
...
hello0hello1hello2hello3hello4
##


We might have to be a little bit more careful with write(), because unlike
the print statement, the write() method isn't magical, and won't
automatically try to coerse objects to strings.  The code above shows that
we str() each number, and for good reason.  If we try without it, we'll
get a TypeError:

##
>>> sys.stdout.write(42)
Traceback (most recent call last):
 File "", line 1, in ?
TypeError: argument 1 must be string or read-only character buffer, not
int
##

So it just means we'll have to be more aware about the type of a value
when we use write().


For some more information, see:

   http://www.python.org/doc/tut/node9.html#SECTION00920
   http://www.python.org/doc/tut/node9.html#SECTION00910


Please feel free to ask more questions.  Good luck!




On 9/25/07, Tim <[EMAIL PROTECTED]> wrote:
>
> Hello,
> I have a print statement where I use concatenation of variables with "+"
> to
> avoid extra whitespaces. The variables are mixed (float/int).
>
> How can I convert them all to strings to have a clean print statement?
>
> example
> print str(var1)+"and this "+str(var2)+"needs to check "+str(var3)
>
> how can I convert var1, var2, var3 all at once?
>
> This would avoid errors because of mixed data types.
>
> BTW, why does a statment like
> print var1, var2
> automatically add spaces between the variables?
>
> Thanks in advance for your help.
>
> Timmie
>
> ___
> 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] killing bash, sshd... dangerous?

2007-10-28 Thread Tom Tucker
Instead of reading in an outputfile (.ps), try reading from command
output.  The correct terminology escapes me (for file in
os.popen(cmd).readlines():).

Are you looking for a auto logout method? For example if no activity
after X minutes kill the shell.  Bash and a few other shells have this
functionality already.





On 10/28/07, John <[EMAIL PROTECTED]> wrote:
> Hello, I've written a little script with the intention of killing all of my
> bash, sshd sessions... is this dangerous? How could I make it work so that
> it didn't kill the session it was run from (as it is I suppose I have to run
> it with nohup):
>
>
>
> #!/usr/bin/env python
> import os
>
> cmd="ps -u myuser | grep 'bash' > .ps"
> os.system(cmd)
>
> pid=[]
> fid=open('.ps','r')
> for line in fid:
> pid.append(line.split(' ')[0])
>
> for i in pid:
> cmd="""kill -9 %s""" % (i)
> os.system(cmd)
>
>
> cmd="ps -u myuser | grep 'sshd' > .ps"
> os.system(cmd)
>
> pid=[]
> fid=open('.ps','r')
> for line in fid:
> pid.append(line.split(' ')[0])
>
> for i in pid:
> cmd="""kill -9 %s""" % (i)
> os.system(cmd)
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Where did those spaces come from?

2005-09-12 Thread Tom Tucker
Tutor,

Good evening!    The goal is to parse a simple file and grab column one.  
Then print each value horizontally separated by a comma. 
Why is Python adding a space padding between each value?   Please see below. 
Thanks ahead of time.




INPUT_FILE # unwanted lines removed

    [EMAIL PROTECTED]    blah    blah
    [EMAIL PROTECTED]    blah    blah
    [EMAIL PROTECTED]    blah    blah
    [EMAIL PROTECTED]    blah    blah


OUTPUT DESIRED
##
,,,


SCRIPT
#
import re

input_file = open('/tmp/file','r')
number_match = re.compile('^\d+\s+\w+\@')
for line in input_file.readlines():
    if number_match.match(line):
    line = re.split('\s+', line)
    print line[0],
    print ",",


OUTPUT GENERATED

 ,  ,   ,  ,    




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


[Tutor] stfftime question

2006-07-04 Thread Tom Tucker
Below is an example of me converting a datetime to milliseconds on a
Mac running Pythong 2.3.5.  The same working code on a Solaris system
with Python 2.3.2 fails.  Any thoughts? What arguments am I missing?



>From my Mac
#
Python 2.3.5 (#1, Oct  5 2005, 11:07:27)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> dtstr = datetime.datetime(1973,9,4,04,3,25,453)
>>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond)
>>> print output
115977805.453


>From Work (Solaris)

Python 2.3.2 (#1, Nov 17 2003, 22:32:28)
[GCC 2.95.3 20010315 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> dtstr = datetime.datetime(1973,9,4,04,3,25,453)
>>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond)
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: not enough arguments for format string
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] stfftime question

2006-07-04 Thread Tom Tucker
I found a temporary solution.  The goal in the end was to compare two
dates/times and  retrieve the millisecond delta between the two.

Work around
#
import datetime
import time
t1 = datetime.datetime(1973,9,4,04,3,25,453)
t2 = datetime.datetime(1973,9,4,04,3,25,553)
t1tuple = time.mktime(t1.timetuple())+(t1.microsecond/1000.)
t2tuple = time.mktime(t2.timetuple())+(t2.microsecond/1000.)
delta  = (t2tuple - t1tuple) * 1000
print delta



On 7/4/06, Tom Tucker <[EMAIL PROTECTED]> wrote:
> Below is an example of me converting a datetime to milliseconds on a
> Mac running Pythong 2.3.5.  The same working code on a Solaris system
> with Python 2.3.2 fails.  Any thoughts? What arguments am I missing?
>
>
>
> From my Mac
> #
> Python 2.3.5 (#1, Oct  5 2005, 11:07:27)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import datetime
> >>> dtstr = datetime.datetime(1973,9,4,04,3,25,453)
> >>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond)
> >>> print output
> 115977805.453
>
>
> From Work (Solaris)
> 
> Python 2.3.2 (#1, Nov 17 2003, 22:32:28)
> [GCC 2.95.3 20010315 (release)] on sunos5
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import datetime
> >>> dtstr = datetime.datetime(1973,9,4,04,3,25,453)
> >>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond)
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: not enough arguments for format string
> >>>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Matching on multiple log lines

2006-10-28 Thread Tom Tucker
I would appreciate any recommendations or assistance on how to read
and parse this log.  The log is constantly growing and has the
potential of being large.  For this reason, we are reading in line by
line.  How do I match on multiple lines, while reading a log line by
line.   Hopefully the logic example and sample log will make more
sense.
Thank you,


Logic:
--
1) Search for this string 'Writing Message to'
2) Search ahead for this string 'TRANSPORT_STREAM_ID'
3) Followed by a blank line
4) If 1,2, and 3:  increase a counter or something (counter +=1)
4) Repeat process


Example log

2006/10/24 20:46:05.853 Writing Message to 192.168.1.1:3211
Protocol:  2
Message Type:  PROVISION
Parameters:
Parameter type:  CHANNEL_IDParameter value:   0001
Parameter type:  SCG_IDParameter value:   0001
Parameter type:  TRANSPORT_STREAM_IDParameter value:   0160
Parameter type:  PROGRAM_NUMBERParameter value:   0001
Parameter type:  GROUPParameter value:   0009 0002
2006/10/24 20:46:05.957 Receiving message from 192.168.1.1:3211
2006/10/24 20:47:05.011 Writing Message to 192.168.1.2:3211
Protocol:  2
Message Type:  PROVISION
Parameters:
Parameter type:  CHANNEL_IDParameter value:   0001
Parameter type:  SCG_IDParameter value:   0001
Parameter type:  TRANSPORT_STREAM_IDParameter value:   0160

2006/10/24 20:47:05.057 Blah Blah..more logging
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] rsync python script

2008-03-21 Thread Tom Tucker
The success or failure could be confirmed by checking the exit status and/or
confirming host77 has the same /var/lib file structure as the source host.
The later is the most accurate, however a bit more complicated to confirm.
As for determining the reason for failure, possible causes off the top of my
head might be name resolution, network failure, either system being offline,
crond problems on originating system, /var/lib filesystem full on host77,
etc.  I would continue to brainstorm other possible causes and think of ways
to check for it in your script. For example, perform a name resolution check
on host77. If resolvable, proceed to ping check method.   Good luck.


On Fri, Mar 21, 2008 at 3:08 AM, Kaushal Shriyan <[EMAIL PROTECTED]>
wrote:

> Hi
>
> [EMAIL PROTECTED]
> 0 18 * * * rsync -av /var/lib/mysql [EMAIL PROTECTED]:/var/lib/
>
> If i put this two lines in crontab it will run correctly,My requirement
> was to create a python script, this python script should indicate success or
> failures and the reason for failure
>
> Any ideas
>
> Thanks and Regards
>
> Kaushal
> ___
> 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] int to string

2008-03-21 Thread Tom Tucker
>>> y = 3
>>> type(y)

>>> x = str(y)
>>> type(x)



On Fri, Mar 21, 2008 at 8:05 PM, elis aeris <[EMAIL PROTECTED]> wrote:

> how do I convert int to string?
>
> ___
> 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] append string

2008-03-21 Thread Tom Tucker
Strings are immutable, you can't append to them.

How about this

>>> mystring = 'Elis'

>>> mystring.append('Aeris')
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'str' object has no attribute 'append'


>>> mystring + ' Aeris'
'Elis Aeris'
>>> x = mystring + ' Aeris'
>>> print x
Elis Aeris



On Fri, Mar 21, 2008 at 8:04 PM, elis aeris <[EMAIL PROTECTED]> wrote:

> how do I append to the end of strings?
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Library for Disk Usage (UNIX)

2008-03-26 Thread Tom Tucker
Hello all. I'm looking for a builtin Python library capable of providing
similar output to what the unix df command provides.  Obviously, I'm trying
to avoid a system call if possible.  I'm looking for the following fields at
a mimimum, total size, used, and /path. Suggestions?  I was looking at
os.stat(/path)[WXYZ}, and os.path.getsize, but they are lacking.

Thanks for the help,

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


Re: [Tutor] Supported platform

2008-05-21 Thread Tom Tucker
I would check the docs on the www.python.org website.

>From the main page

"Python runs on Windows, Linux/Unix, Mac OS X, OS/2, Amiga, Palm Handhelds,
and Nokia mobile phones. Python has also been ported to the Java and .NET
virtual machines."



On Wed, May 21, 2008 at 2:11 PM, Amit Kumar <[EMAIL PROTECTED]>
wrote:

> Can I get list of supported platform by python?
>
> can I install python on AS/400 and HP-Unix?
>
> Regards
> Amit
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Init Script (RHEL 5 Linux)

2011-03-07 Thread Tom Tucker
Hello. I am trying implement a *one time* execution of an init script.  Any
recommendation on how to fork or detach this
script from the normal init 3 execution process so that it doesn't hold up
other activities?

Thanks

Here is the high level logic.


   1. Execute script
  1. perform these activities
   2. Check network connectivity
  1. If connectivity: perform these activities
 1. Delete init script os.remove(sys.argv[0])
  2. If no connectivity:
 - Sleep 15 seconds
 - Check connectivity again
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mac IDE

2011-09-29 Thread Tom Tucker
Another IDE to consider that supports the MAC OS is PyCharm from JetBrains.

On Thu, Sep 29, 2011 at 7:50 AM, Wayne Werner wrote:

> On Thu, Sep 29, 2011 at 5:50 AM, Walter Prins  wrote:
>
>> On 29 September 2011 10:42, Robert Johansson <
>> robert.johans...@math.umu.se> wrote:
>>
>>> Hi,
>>>
>>> ** **
>>>
>>> I know that there is a lot to read about different IDEs on the net but I
>>> have tried a couple and I’m still not pleased. My demands are not that high,
>>> when I’m under Windows I’m happy with IDLE (an interactive shell and debug)
>>> but the problem is with Mac (Python >= 2.7 and OS 10.7). IDLE had serious
>>> problems and TextWrangler had no interactive shell. There’s a lot of other
>>> stuff to try and I would be grateful if someone could spare me some time on
>>> this. 
>>>
>>>
>>>
>> Well, if you're prepared to spend a bit of money, I've heard very good
>> things about Wingware, which is also available on Mac  (Note, not a user
>> myself currently, but has seen it before and been favourably impressed,
>> enough to suggest it here despite not currently actively using it myself.)
>> Link: http://wingware.com/
>
>
> I'll second that. If you're really into IDEs, Wingware is a great one -
> they also have a student/open source license that may be right up your
> alley.
>
> My personal favorite?
>
> Two terminal windows - one with Vim, editing my Python scripts, and another
> with an interactive interpreter. Since you can map keys in Vim, I have 
> mapped to save and run current file. If you're in the habit of editing
> multiple files you could set it up to map  to ask which file you want to
> set as your main .py file. And since you mentioned debug, I usually just use
> pdb if I need debugging. You could easily map a key such as  to insert a
> new line and type 'pdb.set_trace()'. Vim has a fairly steep learning curve,
> but if you spend 30 minutes with the vimtutor you'll be fine. With newer
> versions of Vim you can also write plugins for them in Python.
>
> Of course these capabilities (and many many more) are available with Emacs.
>
> I personally recommend that you learn one (or both) of these editors. They
> will highly improve the speed at which you are able to edit your code.
>
> HTH,
> Wayne
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python telnet

2011-10-22 Thread Tom Tucker
Take a look at pyexpect. I have used this mod to ssh into juniper firewalls,
excute a command, save results and exit. Instead of exiting you can drop
down to interactive mode.
On Oct 22, 2011 3:40 PM, "Rayon"  wrote:

> ** **
>
> ** **
>
> ** **
>
> *From:* Rayon [mailto:ra...@gtt.co.gy]
> *Sent:* 21 October 2011 18:53
> *To:* 'tutor@python.org'
> *Subject:* python telnet
>
> ** **
>
> Can I connect to a telnet session and return data without disconnecting the
> data session.  
>
> ** **
>
> Regards Rayon
>
> ** **
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Iterate Suggestion

2012-04-14 Thread Tom Tucker
Hello all.  Any suggestions how I could easily iterate over a list and
print the output 3 across (when possible)?  One method I was considering
was removing the recently printed item from the list, checking list length,
etc.  Based on the remaining length of the list I would then print X
across. Yah? Is their and easier approach I might be overlooking?


For example...

mylist = ['serverA', 'serverB', 'serverC', 'serverD',' serverE', 'serverF',
'serverG']


Desired Output

serverA  serverB  serverC
serverD  serverE  serverF
serverG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate Suggestion

2012-04-14 Thread Tom Tucker
All,
Thanks for the help.

On Sat, Apr 14, 2012 at 1:33 PM, Bod Soutar  wrote:

>
>
> On 14 April 2012 18:29, Bod Soutar  wrote:
>
>>
>>
>> On 14 April 2012 16:27, Tom Tucker  wrote:
>>
>>>
>>> Hello all.  Any suggestions how I could easily iterate over a list and
>>> print the output 3 across (when possible)?  One method I was considering
>>> was removing the recently printed item from the list, checking list length,
>>> etc.  Based on the remaining length of the list I would then print X
>>> across. Yah? Is their and easier approach I might be overlooking?
>>>
>>>
>>> For example...
>>>
>>> mylist = ['serverA', 'serverB', 'serverC', 'serverD',' serverE',
>>> 'serverF', 'serverG']
>>>
>>>
>>> Desired Output
>>> 
>>> serverA  serverB  serverC
>>> serverD  serverE  serverF
>>> serverG
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>> How about something like this
>>
>>
>> mylist =  ['serverA', 'serverB', 'serverC', 'serverD','serverE',
>> 'serverF', 'serverG']
>> tempstr = ""
>> count = 0
>>
>> for item in mylist:
>> count += 1
>> if count == 3:
>> tempstr += (i + "\n")
>> count = 0
>> else:
>> tempstr += (i + " ")
>>
>> print tempstr
>>
>>
>> The above code prepares an empty string variable, and a count variable.
>> On each iteration, it checks to see if the count is equal to 3. If it is,
>> then it appends the current list element to the string and also adds a
>> newline character("\n"), then the count is reset to 0. If the count isn't
>> 3, it simply appends the current list element along with a space.
>>
>> Hope this helps,
>> Bodsda
>>
>>
>> erm, I screwed up my variable names. where you see 'i', this should be
> item (or the other way around).
> My bad.
>
> -- Bodsda
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ssh connection

2012-12-17 Thread Tom Tucker
One option is PyExpect

http://www.noah.org/python/pexpect/



On Mon, Dec 17, 2012 at 8:40 AM, Ufuk Eskici  wrote:

> Hello All,
>
> Can we make an SSH connection with Pyhton 3.3 ?
>
> I want to connecto to my router, send commands and receive outputs.
>
> Thanks.
> Ufuk
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] For Loop Question

2012-12-23 Thread Tom Tucker
Python Gurus,
I got a question for ya.  Below I have three instance variables (self.A,
self.B, etc).  How can I use the below
for loop for A, B, C to also call those instance variables?

Example
###

.
.
 self.A  = 1
 self.B= 2
 self.C= 3

myDict = {'A': 1, 'B': 2, 'C': 1}
for x in [A, B, C]:

  if myDict[x] == (self.%s % (x)): # Invalid if statement.. ;-)
print "Yep, we have a match!"
  else:
print "Sorry..No match!"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor