[Tutor] returning the entire line when regex matches

2009-05-03 Thread Nick Burgess
How do I make this code print lines NOT containing the string 'Domains'?


import re
for line in log:
if re.search (r'Domains:', line):
print line


This does not work...

if re.search != (r'Domains:', line):
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] returning the entire line when regex matches

2009-05-03 Thread Steve Willoughby
Nick Burgess wrote:
> How do I make this code print lines NOT containing the string 'Domains'?
> 
> 
> import re
> for line in log:
> if re.search (r'Domains:', line):
> print line
> 
> 
> This does not work...
> 
> if re.search != (r'Domains:', line):

re.search (r'Domains:', line)

is a function call which (simplifying slightly here) returns a true
value if  matches the regex "Domains:".  It might make it more
clear if you leave out the extra space there (because I think you're
getting confused thinking re.search and (...) are two separate
expressions since you put != between them):

re.search(r'Domains:', line)

If you want to print lines which do NOT match, try this:

if not re.search(r'Domains:', line):
   print line


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


Re: [Tutor] returning the entire line when regex matches

2009-05-03 Thread Alan Gauld


"Nick Burgess"  wrote 


How do I make this code print lines NOT containing the string 'Domains'?



Don't use regex, use in:

for line in log:
if "Domains" in line:
print line


This does not work...

if re.search != (r'Domains:', line):


Because you are assigning a tuple containing a string and a line 
of text to a name which is currently bound to a function object 
(re.search) which is not a sensible thing to do since you lose access 
to the search function. If you really do need to use regex you probably 
want:


if not re.search(r'Domains', line):
  print line

But if it is a simple string you are searching for regex is overkill 
and probably slower than using in.


--
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] returning the entire line when regex matches

2009-05-03 Thread Steve Willoughby
Alan Gauld wrote:
>> if re.search != (r'Domains:', line):
> 
> Because you are assigning a tuple containing a string and a line of text
> to a name which is currently bound to a function object (re.search)

Actually, he's COMPARING the two, not assigning.  But comparing a
function object and a tuple is still not going to do anything sensible.

> But if it is a simple string you are searching for regex is overkill and
> probably slower than using in.

This is good advice.  I was assuming that the question was about regex
and would be grown into a more complex pattern match in the final
application.  If all you really want to do is see if a constant text
string like "Domain:" is in a string, regex will work but is overkill.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] returning the entire line when regex matches

2009-05-03 Thread Alan Gauld


"Steve Willoughby"  wrote


if re.search != (r'Domains:', line):


Because you are assigning a tuple containing a string and a line of text
to a name which is currently bound to a function object (re.search)


Actually, he's COMPARING the two, not assigning.  But comparing a
function object and a tuple is still not going to do anything sensible.


So he is, I didn't notice the ! in there.
I really must start wearing my glasses when using the PC!!! :-(

Alan G.


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


Re: [Tutor] returning the entire line when regex matches

2009-05-03 Thread Alan Gauld


"Alan Gauld"  wrote


How do I make this code print lines NOT containing the string 'Domains'?



Don't use regex, use in:

for line in log:
if "Domains" in line:
print line


Should, of course,  be

  if "Domains" not in line:
  print line

Alan G.


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


[Tutor] Code Dosent work.

2009-05-03 Thread Jacob Mansfield
hi everyone, I'm a bit new here but i was wondering if someone could check
some of my code, it's not doing quite what it's meant to.
thanks

Databox_2_0.py:

import pygame, sys, os
pygame.init()
def load(filename):
if filename != '':
e = 1
dec = "placeholder"
fic = open(filename, "r")
while dec != '':
num = str(e)
print "found " + num + " enteries"
dec = fic.readline(e)
databox[e] = dec
dec = fic.readline((e+1))
databox2[e] = dec
e = e+1
fic.close()
else:
return 0
def search():
print "\n"
x = 1
items = len(databox)
ins = items+1
while ins > x :
dac = databox[x]
dac2 = databox2[x]
x = x + 1
print dac + " " + dac2
print "\n\n"

def add():
dat = raw_input("First name.\n")
dat2 = raw_input("\nSecond name.\n")
items = len(databox)
ins = items+1
databox[ins] = dat
databox2[ins] = dat2
print "Done.\n\n"
def exitprog():
fic = open('databox.txt','w')
print "saveing\n"
x = 1
items = len(databox)
ins = items+1
while ins > x :
dac = databox[x]
dac2 = databox2[x]
x = x + 1
fic.write(dac)
fic.write(dac2)
fic.close()
print "goodbye"
pygame.time.delay(900)
exit()
databox = dict()
databox2 = dict()
go = raw_input("filename, blank for none.\n")
load(go)
while True:
print "Welcome to databox V2.0."
print " 1. Searth the database."
print " 2. Add a record."
print " 3. Exit."
inme = raw_input("Please make a selection.\n")
if inme == "1":
search()
elif inme == "2":
add()
elif inme == "3":
exitprog()
else:
print "input not recignised."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code Dosent work.

2009-05-03 Thread David

Jacob Mansfield wrote:
hi everyone, I'm a bit new here but i was wondering if someone could 
check some of my code, it's not doing quite what it's meant to.

thanks

Works without pygame;
filename, blank for none.

Welcome to databox V2.0.
 1. Searth the database.
 2. Add a record.
 3. Exit.
Please make a selection.
2
First name.
David

Second name.
William
Done.


Welcome to databox V2.0.
 1. Searth the database.
 2. Add a record.
 3. Exit.
Please make a selection.
1


David William

Why were you using pygame?


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


[Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-03 Thread Dan Liang
Hi tutors,

I am working on a file and need to replace each occurrence of a certain
label (part of speech tag in this case) by a number of sub-labels. The file
has the following format:

word1  \tTag1
word2  \tTag2
word3  \tTag3

Now the tags are complex and I wanted to split them in a tab-delimited
fashion to have this:

word1   \t   Tag1Part1   \t   Tag2Part2   \t   Tag3Part3

I searched online for some solution and found the code below which uses a
dictionary to store the tags that I want to replace in keys and the sub-tags
as values. The problem with this is that it sometimes replaces tags that are
not surrounded by spaces, which I do not like to happen. Also, I wanted each
new sub-tag to be followed by a tab, so that the new items that I end up
having in my file are tab-delimited. For this, I put tabs between the items
of each key in the dictionary. I started thinking that this will not be the
best solution of the problem and perhaps a script that uses regular
expressions would be better. Since I am new to Python, I thought I should
ask you for your thoughts for a best solution. The items I want to replace
are about 150 and I did not know how to iterate over them with regular
expressions. Below is my previous code:


#!usr/bin/python

import re, sys
f = file(sys.argv[1])
readed= f.read()

def replace_words(text, word_dic):
for k, v in word_dic.iteritems():
text = text.replace(k, v)
return text

# the dictionary has target_word:replacement_word pairs

word_dic = {
'abbrev': 'abbrevnullnull',
'adj': 'adjnullnull',
'adv': 'advnullnull',
'case_def_acc': 'case_defaccnull',
'case_def_gen': 'case_defgennull',
'case_def_nom': 'case_defnomnull',
'case_indef_acc': 'case_indefaccnull',
'verb_part': 'verb_partnullnull'}


# call the function and get the changed text

myString = replace_words(readed, word_dic)


fout = open(sys.argv[2], "w")
fout.write(myString)
fout.close()

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


Re: [Tutor] Code Dosent work.

2009-05-03 Thread Lie Ryan

David wrote:

Jacob Mansfield wrote:
hi everyone, I'm a bit new here but i was wondering if someone could 
check some of my code, it's not doing quite what it's meant to.

thanks


Why were you using pygame?



The only line that uses pygame is pygame.time.delay(900). It is an 
overkill to import pygame just to put delay. Especially when it is 
possible to do the same thing with standard lib.


import time
time.sleep(0.9)

Now on to what you should know:
1) pythons' file.readline(x) reads until the end of line returning at 
most x bytes (it could return less than x bytes if it sees a newline). 
The x argument is not linenumber.
2) The problem starts from your "save" function. Look at your data file, 
it does not have line ending.


change the while block inside exitprog() function with:

while ins > x :
dac = databox[x]
dac2 = databox2[x]
x = x + 1
fic.write(dac + ' ' + dac2 + '\n')

also, change this while block in the load():

while dec != '':
num = str(e)
print "found " + num + " enteries"
dec = fic.readline(e)
databox[e] = dec
dec = fic.readline((e+1))
databox2[e] = dec
e = e+1

with:

for e, line in enumerate(fic):
databox[e+1], databox2[e+1] = line.split()
print "found" + str(e) + "entries"

Also, these minor things bothers me:
1) For databox and databox2, it is unnecessary to use dict. A list() or 
set() should serve you better (and is simpler than dict).
2) You have databox and databox2, which is a parallel container for each 
other. Keeping them in sync is going to be a mess. Rather you should use 
a single databox which contains a 2-tuple.
3) time.sleep() can replace pygame.time.delay(), however while 
pygame.time.delay() is in milisecond, time.sleep is in seconds so use 
0.9 instead of 900.
4) In python, index number usually starts with 0, the ugly [e+1] code in 
the for loop I gave above is caused by the use of 1-based indexing in 
the rest of the code.

5) Use for loop, it's easier.

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


Re: [Tutor] Code Dosent work.

2009-05-03 Thread Stefan Behnel
Jacob Mansfield wrote:
> hi everyone, I'm a bit new here but i was wondering if someone could check
> some of my code, it's not doing quite what it's meant to.

As a general remark: it's a good idea to add a note on what your code is
supposed to do, and in what way it behaves unexpectedly, i.e. what is the
expected and the observed behaviour. Posting error messages is helpful, too.

Stefan

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