pattern search

Discussion in 'Python' started by Diez B. Roggisch, Mar 27, 2007.

  1. Fabian Braennstroem wrote:

    > Hi,
    >
    > I wrote a small gtk file manager, which works pretty well. Until
    > now, I am able to select different file (treeview entries) just by
    > extension (done with 'endswith'). See the little part below:
    >
    > self.pathlist1=[ ]
    > self.patternlist=[ ]
    > while iter:
    > # print iter
    > value = model.get_value(iter, 1)
    > # if value is what I'm looking for:
    > if value.endswith("."+ pattern):
    > selection.select_iter(iter)
    > selection.select_path(n)
    > self.pathlist1.append(n)
    > self.patternlist.append(value)
    > iter = model.iter_next(iter)
    > # print value
    > n=n+1
    >
    > Now, I would like to improve it by searching for different 'real'
    > patterns just like using 'ls' in bash. E.g. the entry
    > 'car*.pdf' should select all pdf files with a beginning 'car'.
    > Does anyone have an idea, how to do it?


    Use regular expressions. They are part of the module "re". And if you use
    them, ditch your code above, and make it just search for a pattern all the
    time. Because the above is just the case of

    *.ext



    Diez
     
    Diez B. Roggisch, Mar 27, 2007
    #1
    1. Advertising

  2. Fabian Braennstroem wrote:
    > Now, I would like to improve it by searching for different 'real'
    > patterns just like using 'ls' in bash. E.g. the entry
    > 'car*.pdf' should select all pdf files with a beginning 'car'.
    > Does anyone have an idea, how to do it?


    Use module glob.
     
    =?ISO-8859-2?Q?Wojciech_Mu=B3a?=, Mar 27, 2007
    #2
    1. Advertising

  3. Hi,

    I wrote a small gtk file manager, which works pretty well. Until
    now, I am able to select different file (treeview entries) just by
    extension (done with 'endswith'). See the little part below:

    self.pathlist1=[ ]
    self.patternlist=[ ]
    while iter:
    # print iter
    value = model.get_value(iter, 1)
    # if value is what I'm looking for:
    if value.endswith("."+ pattern):
    selection.select_iter(iter)
    selection.select_path(n)
    self.pathlist1.append(n)
    self.patternlist.append(value)
    iter = model.iter_next(iter)
    # print value
    n=n+1

    Now, I would like to improve it by searching for different 'real'
    patterns just like using 'ls' in bash. E.g. the entry
    'car*.pdf' should select all pdf files with a beginning 'car'.
    Does anyone have an idea, how to do it?

    Greetings!
    Fabian
     
    Fabian Braennstroem, Mar 27, 2007
    #3
  4. Diez B. Roggisch

    Paul McGuire Guest

    On Mar 27, 10:18 am, "Diez B. Roggisch" <> wrote:
    > Fabian Braennstroem wrote:
    > > Hi,

    >
    > > I wrote a small gtk file manager, which works pretty well. Until
    > > now, I am able to select different file (treeview entries) just by
    > > extension (done with 'endswith'). See the little part below:

    >
    > > self.pathlist1=[ ]
    > > self.patternlist=[ ]
    > > while iter:
    > > # print iter
    > > value = model.get_value(iter, 1)
    > > # if value is what I'm looking for:
    > > if value.endswith("."+ pattern):
    > > selection.select_iter(iter)
    > > selection.select_path(n)
    > > self.pathlist1.append(n)
    > > self.patternlist.append(value)
    > > iter = model.iter_next(iter)
    > > # print value
    > > n=n+1

    >
    > > Now, I would like to improve it by searching for different 'real'
    > > patterns just like using 'ls' in bash. E.g. the entry
    > > 'car*.pdf' should select all pdf files with a beginning 'car'.
    > > Does anyone have an idea, how to do it?

    >
    > Use regular expressions. They are part of the module "re". And if you use
    > them, ditch your code above, and make it just search for a pattern all the
    > time. Because the above is just the case of
    >
    > *.ext
    >
    > Diez- Hide quoted text -
    >
    > - Show quoted text -


    The glob module is a more direct tool based on the OP's example. The
    example he gives works directly with glob. To use re, you'd have to
    convert to something like "car.*\.pdf", yes?

    (Of course, re offers much more power than simple globbing. Not clear
    how much more the OP was looking for.)

    -- Paul
     
    Paul McGuire, Mar 27, 2007
    #4
  5. Diez B. Roggisch

    Paul McGuire Guest

    On Mar 27, 3:13 pm, Fabian Braennstroem <> wrote:
    > Hi to all,
    >
    > Wojciech Mu?a schrieb am 03/27/2007 03:34 PM:
    >
    > > Fabian Braennstroem wrote:
    > >> Now, I would like to improve it by searching for different 'real'
    > >> patterns just like using 'ls' in bash. E.g. the entry
    > >> 'car*.pdf' should select all pdf files with a beginning 'car'.
    > >> Does anyone have an idea, how to do it?

    >
    > > Use module glob.

    >
    > Thanks for your help! glob works pretty good, except that I just
    > deleted all my lastet pdf files :-(
    >
    > Greetings!
    > Fabian


    Then I shudder to think what might have happened if you had used
    re's! :)

    -- Paul
     
    Paul McGuire, Mar 27, 2007
    #5
  6. Hi to all,

    Wojciech Mu?a schrieb am 03/27/2007 03:34 PM:
    > Fabian Braennstroem wrote:
    >> Now, I would like to improve it by searching for different 'real'
    >> patterns just like using 'ls' in bash. E.g. the entry
    >> 'car*.pdf' should select all pdf files with a beginning 'car'.
    >> Does anyone have an idea, how to do it?

    >
    > Use module glob.


    Thanks for your help! glob works pretty good, except that I just
    deleted all my lastet pdf files :-(

    Greetings!
    Fabian
     
    Fabian Braennstroem, Mar 27, 2007
    #6
  7. Paul McGuire schrieb:
    > On Mar 27, 10:18 am, "Diez B. Roggisch" <> wrote:
    >> Fabian Braennstroem wrote:
    >>> Hi,
    >>> I wrote a small gtk file manager, which works pretty well. Until
    >>> now, I am able to select different file (treeview entries) just by
    >>> extension (done with 'endswith'). See the little part below:
    >>> self.pathlist1=[ ]
    >>> self.patternlist=[ ]
    >>> while iter:
    >>> # print iter
    >>> value = model.get_value(iter, 1)
    >>> # if value is what I'm looking for:
    >>> if value.endswith("."+ pattern):
    >>> selection.select_iter(iter)
    >>> selection.select_path(n)
    >>> self.pathlist1.append(n)
    >>> self.patternlist.append(value)
    >>> iter = model.iter_next(iter)
    >>> # print value
    >>> n=n+1
    >>> Now, I would like to improve it by searching for different 'real'
    >>> patterns just like using 'ls' in bash. E.g. the entry
    >>> 'car*.pdf' should select all pdf files with a beginning 'car'.
    >>> Does anyone have an idea, how to do it?

    >> Use regular expressions. They are part of the module "re". And if you use
    >> them, ditch your code above, and make it just search for a pattern all the
    >> time. Because the above is just the case of
    >>
    >> *.ext
    >>
    >> Diez- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > The glob module is a more direct tool based on the OP's example. The
    > example he gives works directly with glob. To use re, you'd have to
    > convert to something like "car.*\.pdf", yes?
    >
    > (Of course, re offers much more power than simple globbing. Not clear
    > how much more the OP was looking for.)


    I'm aware of the glob-module. But it only works on files. I was under
    the impression that he already has a list of files he wants to filter
    instead of getting it fresh from the filesystem.

    Diez
     
    Diez B. Roggisch, Mar 27, 2007
    #7
  8. En Tue, 27 Mar 2007 18:42:15 -0300, Diez B. Roggisch <>
    escribió:

    > Paul McGuire schrieb:
    >> On Mar 27, 10:18 am, "Diez B. Roggisch" <> wrote:
    >>> Fabian Braennstroem wrote:
    >>>> while iter:
    >>>> value = model.get_value(iter, 1)
    >>>> if value.endswith("."+ pattern): [...]
    >>>>
    >>>> Now, I would like to improve it by searching for different 'real'
    >>>> patterns just like using 'ls' in bash. E.g. the entry
    >>>> 'car*.pdf' should select all pdf files with a beginning 'car'.
    >>>> Does anyone have an idea, how to do it?


    >>> Use regular expressions. They are part of the module "re". And if you
    >>> use them, ditch your code above, and make it just search for a pattern
    >>> all the time. Because the above is just the case of
    >>> *.ext


    >> The glob module is a more direct tool based on the OP's example. The
    >> example he gives works directly with glob. To use re, you'd have to
    >> convert to something like "car.*\.pdf", yes?


    > I'm aware of the glob-module. But it only works on files. I was under
    > the impression that he already has a list of files he wants to filter
    > instead of getting it fresh from the filesystem.


    In that case the best way would be to use the fnmatch module - it already
    knows how to translate from car*.pdf into the right regexp. (The glob
    module is like a combo os.listdir+fnmatch.filter)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 27, 2007
    #8
  9. Hi,

    Gabriel Genellina schrieb am 03/27/2007 10:09 PM:
    > En Tue, 27 Mar 2007 18:42:15 -0300, Diez B. Roggisch <>
    > escribió:
    >
    >> Paul McGuire schrieb:
    >>> On Mar 27, 10:18 am, "Diez B. Roggisch" <> wrote:
    >>>> Fabian Braennstroem wrote:
    >>>>> while iter:
    >>>>> value = model.get_value(iter, 1)
    >>>>> if value.endswith("."+ pattern): [...]
    >>>>>
    >>>>> Now, I would like to improve it by searching for different 'real'
    >>>>> patterns just like using 'ls' in bash. E.g. the entry
    >>>>> 'car*.pdf' should select all pdf files with a beginning 'car'.
    >>>>> Does anyone have an idea, how to do it?

    >
    >>>> Use regular expressions. They are part of the module "re". And if you
    >>>> use them, ditch your code above, and make it just search for a pattern
    >>>> all the time. Because the above is just the case of
    >>>> *.ext

    >
    >>> The glob module is a more direct tool based on the OP's example. The
    >>> example he gives works directly with glob. To use re, you'd have to
    >>> convert to something like "car.*\.pdf", yes?

    >
    >> I'm aware of the glob-module. But it only works on files. I was under
    >> the impression that he already has a list of files he wants to filter
    >> instead of getting it fresh from the filesystem.

    >
    > In that case the best way would be to use the fnmatch module - it already
    > knows how to translate from car*.pdf into the right regexp. (The glob
    > module is like a combo os.listdir+fnmatch.filter)


    I have a already a list, but I 'glob' looked so easy ... maybe it is
    faster to use fnmatch. When I have time I try it out...

    Thanks!
    Fabian
     
    Fabian Braennstroem, Mar 28, 2007
    #9
  10. Hi Paul,

    Paul McGuire schrieb am 03/27/2007 07:19 PM:
    > On Mar 27, 3:13 pm, Fabian Braennstroem <> wrote:
    >> Hi to all,
    >>
    >> Wojciech Mu?a schrieb am 03/27/2007 03:34 PM:
    >>
    >>> Fabian Braennstroem wrote:
    >>>> Now, I would like to improve it by searching for different 'real'
    >>>> patterns just like using 'ls' in bash. E.g. the entry
    >>>> 'car*.pdf' should select all pdf files with a beginning 'car'.
    >>>> Does anyone have an idea, how to do it?
    >>> Use module glob.

    >> Thanks for your help! glob works pretty good, except that I just
    >> deleted all my lastet pdf files :-(
    >>
    >> Greetings!
    >> Fabian

    >
    > Then I shudder to think what might have happened if you had used
    > re's! :)


    A different feature it had was to copy the whole home-partition
    (about 19G) into one of its own directories ... the strange thing:
    it just needed seconds to do that and I did not have the permission
    to all files and directories! It was pretty strange! Hopefully it
    was no security bug in python...

    Greetings!
    Fabian
     
    Fabian Braennstroem, Mar 29, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,877
    Chris Uppal
    Nov 16, 2005
  2. sunny
    Replies:
    1
    Views:
    468
    Salt_Peter
    Dec 7, 2006
  3. Abby Lee
    Replies:
    5
    Views:
    437
    Abby Lee
    Aug 2, 2004
  4. Pallav singh
    Replies:
    0
    Views:
    366
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    0
    Views:
    405
    Pallav singh
    Jan 22, 2012
Loading...

Share This Page