Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92178

Re: Find in ipython3

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Find in ipython3
Organization Decebal Computing
References <87y4k2hyvf.fsf@Equus.decebal.nl> <mailman.177.1433448836.13271.python-list@python.org> <87bnguhbec.fsf@Equus.decebal.nl>
Date 2015-06-06 11:57 +0200
Message-ID <874mmlqhul.fsf@Equus.decebal.nl> (permalink)

Show all headers | View raw


On Friday  5 Jun 2015 09:17 CEST, Cecil Westerhof wrote:

> I was already thinking along those lines. I made it:
> def find(directory, to_match):
> to_match = to_match.lower()
> results = []
> for dirpath, dirnames, filenames in os.walk(expanduser(directory)):
> for filename in filenames:
> if(fnmatch(filename.lower(), to_match)):
> results.append(os.path.join(dirpath, filename))
> return results

I have a slightly better variant:
    def find(directory, to_match, ignore_case = False):
        to_match =  to_match + r'$'
        if ignore_case:
            p = re.compile(to_match, re.IGNORECASE)
        else:
            p = re.compile(to_match)
        results = []
        for dirpath, dirnames, filenames in os.walk(expanduser(directory)):
            for filename in filenames:
                if p.match(filename):
                    results.append(os.path.join(dirpath, filename))
        return results

Default it works now case sensitive. But I now use regular expression.
That is a lot more efficient. The old version took 4.4 seconds and
this version takes 2.4 seconds. But the ‘!find’ version takes about
half a second. Why is this version so much less efficient?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Find in ipython3 Cecil Westerhof <Cecil@decebal.nl> - 2015-06-02 18:13 +0200
  Re: Find in ipython3 Cameron Simpson <cs@zip.com.au> - 2015-06-04 12:54 +1000
    Re: Find in ipython3 Cecil Westerhof <Cecil@decebal.nl> - 2015-06-04 07:09 +0200
      Re: Find in ipython3 Cameron Simpson <cs@zip.com.au> - 2015-06-04 15:43 +1000
      Re: Find in ipython3 Grant Edwards <invalid@invalid.invalid> - 2015-06-04 14:27 +0000
        Re: Find in ipython3 Cecil Westerhof <Cecil@decebal.nl> - 2015-06-04 17:12 +0200
          Re: Find in ipython3 Michael Torrie <torriem@gmail.com> - 2015-06-04 13:11 -0600
  Re: Find in ipython3 Michael Torrie <torriem@gmail.com> - 2015-06-04 13:09 -0600
  Re: Find in ipython3 Tim Chase <python.list@tim.thechases.com> - 2015-06-04 14:17 -0500
  Re: Find in ipython3 random832@fastmail.us - 2015-06-04 16:13 -0400
    Re: Find in ipython3 Cecil Westerhof <Cecil@decebal.nl> - 2015-06-05 09:17 +0200
      Re: Find in ipython3 Cecil Westerhof <Cecil@decebal.nl> - 2015-06-06 11:57 +0200
        Re: Find in ipython3 Laura Creighton <lac@openend.se> - 2015-06-06 13:07 +0200
          Re: Find in ipython3 Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 08:20 +0200
            Re: Find in ipython3 Cameron Simpson <cs@zip.com.au> - 2015-06-07 17:38 +1000
            Re: Find in ipython3 Laura Creighton <lac@openend.se> - 2015-06-07 11:33 +0200
              Re: Find in ipython3 Steven D'Aprano <steve@pearwood.info> - 2015-06-07 23:16 +1000
            Re: Find in ipython3 Peter Otten <__peter__@web.de> - 2015-06-07 12:27 +0200
            Re: Find in ipython3 Laura Creighton <lac@openend.se> - 2015-06-07 15:01 +0200
            Re: Find in ipython3 Chris Angelico <rosuav@gmail.com> - 2015-06-07 22:13 +1000

csiph-web