pattern search

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabian Braennstroem

    #1

    pattern search

    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.patternlis t=[ ]
    while iter:
    # print iter
    value = model.get_value (iter, 1)
    # if value is what I'm looking for:
    if value.endswith( "."+ pattern):
    selection.selec t_iter(iter)
    selection.selec t_path(n)
    self.pathlist1. append(n)
    self.patternlis t.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

  • Diez B. Roggisch

    #2
    Re: pattern search

    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.patternlis t=[ ]
    while iter:
    # print iter
    value = model.get_value (iter, 1)
    # if value is what I'm looking for:
    if value.endswith( "."+ pattern):
    selection.selec t_iter(iter)
    selection.selec t_path(n)
    self.pathlist1. append(n)
    self.patternlis t.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

    Comment

    • =?ISO-8859-2?Q?Wojciech_Mu=B3a?=

      #3
      Re: pattern search

      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.

      Comment

      • Paul McGuire

        #4
        Re: pattern search

        On Mar 27, 10:18 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
        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.patternlis t=[ ]
        while iter:
        # print iter
        value = model.get_value (iter, 1)
        # if value is what I'm looking for:
        if value.endswith( "."+ pattern):
        selection.selec t_iter(iter)
        selection.selec t_path(n)
        self.pathlist1. append(n)
        self.patternlis t.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

        Comment

        • Fabian Braennstroem

          #5
          Re: pattern search

          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

          Comment

          • Paul McGuire

            #6
            Re: pattern search

            On Mar 27, 3:13 pm, Fabian Braennstroem <f.braennstr... @gmx.dewrote:
            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

            Comment

            • Diez B. Roggisch

              #7
              Re: pattern search

              Paul McGuire schrieb:
              On Mar 27, 10:18 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
              >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.patternlis t=[ ]
              >> while iter:
              >># print iter
              >> value = model.get_value (iter, 1)
              >># if value is what I'm looking for:
              >> if value.endswith( "."+ pattern):
              >> selection.selec t_iter(iter)
              >> selection.selec t_path(n)
              >> self.pathlist1. append(n)
              >> self.patternlis t.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

              Comment

              • Gabriel Genellina

                #8
                Re: pattern search

                En Tue, 27 Mar 2007 18:42:15 -0300, Diez B. Roggisch <deets@nospam.w eb.de>
                escribió:
                Paul McGuire schrieb:
                >On Mar 27, 10:18 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
                >>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+fnma tch.filter)

                --
                Gabriel Genellina

                Comment

                • Fabian Braennstroem

                  #9
                  Re: pattern search

                  Hi,

                  Gabriel Genellina schrieb am 03/27/2007 10:09 PM:
                  En Tue, 27 Mar 2007 18:42:15 -0300, Diez B. Roggisch <deets@nospam.w eb.de>
                  escribió:
                  >
                  >Paul McGuire schrieb:
                  >>On Mar 27, 10:18 am, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
                  >>>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+fnma tch.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

                  Comment

                  • Fabian Braennstroem

                    #10
                    Re: pattern search

                    Hi Paul,

                    Paul McGuire schrieb am 03/27/2007 07:19 PM:
                    On Mar 27, 3:13 pm, Fabian Braennstroem <f.braennstr... @gmx.dewrote:
                    >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

                    Comment

                    Working...