Re: [Tutor] Redux: optparse
On Sat, Dec 27, 2008 at 2:55 PM, Matt Herzog wrote: > What I can't figure out now is how to pass a compiled regex to an optparse > option. I'm confused ias to "option" versus "arg" when using the optparse > module. In fact there seems to be no way to define what the arg should be; > only options. Is the arg always implied? I have read several pages on > optparse and am none the wiser. You want to *retrieve* the regex from the option, not pass it to the option. See below. "args" is the positional arguments - any parameters that aren't prefixed with a switch like "-x". They don't need any configuration. > How do I fix the rx = re.compile('-x') line below so that the string I pass > on the command line gets passed into the re.compile? > > #!/usr/bin/python > import fileinput, sys, string, optparse, re > > #def main(): > optparser = optparse.OptionParser() > optparser.add_option("-x", "--regx", help="regular expression") > # take the first argument out of sys.argv and assign it to searchterm > #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] > (options, args) = optparser.parse_args() > > rx = re.compile('-x') Try rx = re.compile(options.regx) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Redux: optparse
> Do you want to use optparse, or get the command line arguments yourself? > It seems the pattern string will be the first arg, will it? Again I am confused. I assumed that optparse was the best way to pass in arguments (such as filenames) from the command line. Like so: ./script.py -x regex *.html > > rx = re.compile('-x') > > Then, if the note above is right, you have: > > pat_string = sys.argv[1] > rx = re.compile(pat_string) # pattern object > > Is that what you wish to do? Probablement. :) The below code does what I want: --- #!/usr/bin/python import fileinput, sys, string # Take the first argument out of sys.argv and assign it to searchterm. searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] for line in fileinput.input(): num_matches = line.count(searchterm) if num_matches: # A nonzero count means there was a match. print "found '%s' %d times in %s on line %d." % (searchterm, num_matches, fileinput.filename(), fileinput.filelineno()) --- I wanted to use optparse instead of sys.argv. Perhaps I should study fileinput instead. I don't understand that either. > > for line in fileinput.input(): > > num_matches = string.count(line, rx) > Above you are counting the number of *regex pattern* may be inside a > string. Not the number of (sub)strings matching the pattern, which is > probably what you intend.[ Also, you are using a deprecated function > "count" of the string module. Use line.count(substring) instead. Anyway, > this is probably not what you want.] > To count the number of pattern matches, you must use the > "matching" (lol) regex method, namely findall: > > result_list = rx.findall(line) > > > if num_matches: > > print "found '%s' %d times in %s on line %d." % (rx, num_matches, > > fileinput.filename(), fileinput.filelineno()) > > Do you want to print only the last match? If not, you need to record > matches found by the search loop into a list. > > > #if __name__ == '__main__': > > #main() > > > ___ > 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] Redux: optparse
Le samedi 27 décembre 2008 à 14:55 -0500, Matt Herzog a écrit : > On Wed, Dec 24, 2008 at 01:12:55AM -, Alan Gauld wrote: > > > > "Kent Johnson" wrote > > > > >> for filename in os.listdir(directory): > > >> result = re.match(s, filename) > > >> print result > > > > > >You never open and read the files. You are searching for the pattern > > >in the filename, not in the contents of the file. > > > > Also note that match() only searches starting at the start of the > > string. > > > > Thus match will find foo at > > > > foobar > > > > but not in > > > > sofoo > > > > You usually need to use search() to find the pattern anywhere > > within the string. > > > > Also look at the thread earlier this week on using listdir() and > > the fileinput module. > > Hello again and thanks for the encouragement. > > I have been working on this problem again today and switched to the fileinput > method. What I can't figure out now is how to pass a compiled regex to an > optparse option. I'm confused ias to "option" versus "arg" when using the > optparse module. In fact there seems to be no way to define what the arg > should be; only options. Is the arg always implied? I have read several pages > on optparse and am none the wiser. > > How do I fix the rx = re.compile('-x') line below so that the string I pass > on the command line gets passed into the re.compile? > > #!/usr/bin/python > import fileinput, sys, string, optparse, re > > #def main(): > optparser = optparse.OptionParser() > optparser.add_option("-x", "--regx", help="regular expression") > # take the first argument out of sys.argv and assign it to searchterm > #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] > (options, args) = optparser.parse_args() Do you want to use optparse, or get the command line arguments yourself? It seems the pattern string will be the first arg, will it? > rx = re.compile('-x') Then, if the note above is right, you have: pat_string = sys.argv[1] rx = re.compile(pat_string) # pattern object Is that what you wish to do? > for line in fileinput.input(): > num_matches = string.count(line, rx) Above you are counting the number of *regex pattern* may be inside a string. Not the number of (sub)strings matching the pattern, which is probably what you intend.[ Also, you are using a deprecated function "count" of the string module. Use line.count(substring) instead. Anyway, this is probably not what you want.] To count the number of pattern matches, you must use the "matching" (lol) regex method, namely findall: result_list = rx.findall(line) > if num_matches: > print "found '%s' %d times in %s on line %d." % (rx, num_matches, > fileinput.filename(), fileinput.filelineno()) Do you want to print only the last match? If not, you need to record matches found by the search loop into a list. > #if __name__ == '__main__': > #main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor