[Tutor] python grep implementation
I am trying to implement grep functionality in python. import os, sys, getopt import multiprocessing as mp import re import itertools def get_files(path, pattern): for (dirpath, dirnames, filenames) in os.walk(path): for filename in filenames: if filename.endswith(pattern): yield os.path.join(dirpath, filename) def worker_search_fn(arg): fname, regex = arg with open(fname, 'rt') as f: match = regex.search(f.read()) if match: print(fname + " " +":"+ match.group()) return def main(argv): path, f_pattern, s_pattern = '', '', '' try: opts, args = getopt.getopt(argv,"hi:p:f:s:S:",["ifile=","file_pattern=","string_pattern=","string_flags="]) except getopt.GetoptError: print 'test.py -i -p -f -S ' print 'example usage python a.py -i . -s \'.*_i2c_register.*\' -f .c,.h,.cpp -S "S"' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -i -p , -f ' sys.exit() elif opt in ("-i", "--ifile"): path = arg elif opt in ("-f", "--file_pattern"): f_pattern = arg.split(",") elif opt in ("-s", "--string_pattern"): s_pattern = arg elif opt in ("-S", "--string_flags"): s_pattern_flags = arg regex = re.compile(s_pattern, getattr(re, s_pattern_flags)) files = get_files(path, tuple(f_pattern)) mp.Pool().map(worker_search_fn, itertools.izip(files, itertools.repeat(regex))) if __name__ == "__main__": main(sys.argv[1:]) I want to see if I can further speedup in any way possible and also some code review. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help
On Wednesday, October 12, 2016, Alan Gauld via Tutor wrote: > On 12/10/16 09:03, niraj pandey wrote: > > > Can you pls guide how to print this screen (Attached here) content in > > printer ? > > As we already pointed out this is a text list so attachments > are usually stripped off... > > However, printing from Tkinter is not easy. The simplest way is usually > to create an HTML file and use the OS to print that via a browser (many > browsers have a print command line option). It is possible to convert > your screen into a graphics file and print that, but the results can be > a bit unpredictable. > > Alan G. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] enforcing specific types in Python 3.5?
I have implemented the equivalent of "insert if unique" in Python & SQLAlchemy to help with data normalization. However to help minimize the number of preliminary SELECT statements needed, it helps to check types through calls to isinstance() before getting to the salient code. Unfortunately, the code begins to be cluttered with type-checking minutiae. While researching this problem, I have found potential solutions like the following: http://stackoverflow.com/questions/9305751/force-python-class-member-variable-to-be-specific-type ...but given that Python 3.5 has syntactically understands gradual typing, I have wondered whether addition of this feature offers anything over use of property() as described above. Toy examples have not revealed anything useful on this front: $ cat example.py #!/usr/bin/env python class Foobar(): def __init__(self, value): self.value = value def f(s: str) -> int: print(s) return 3.14 def main(): i = f('foobar') print(type(i)) print('i = "{}"'.format(i)) i = f(Foobar(3)) print(type(i)) print('i = "{}"'.format(i)) if __name__ == '__main__': main() $ python example.py foobar i = "3.14" <__main__.Foobar object at 0x85b8aaac> i = "3.14" $ I understand that gradual typing may be useful with static analysis, but I don't see that any type enforcement occurs by default at runtime. Am I missing something here? Is there a better solution for type enforcement? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor