Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pattern search

Reply
Thread Tools

pattern search

 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-27-2007
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
 
Reply With Quote
 
 
 
 
=?ISO-8859-2?Q?Wojciech_Mu=B3a?=
Guest
Posts: n/a
 
      03-27-2007
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.
 
Reply With Quote
 
 
 
 
Fabian Braennstroem
Guest
Posts: n/a
 
      03-27-2007
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

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      03-27-2007
On Mar 27, 10:18 am, "Diez B. Roggisch" <d...@nospam.web.de> 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

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      03-27-2007
On Mar 27, 3:13 pm, Fabian Braennstroem <f.braennstr...@gmx.de> 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

 
Reply With Quote
 
Fabian Braennstroem
Guest
Posts: n/a
 
      03-27-2007
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

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-27-2007
Paul McGuire schrieb:
> On Mar 27, 10:18 am, "Diez B. Roggisch" <d...@nospam.web.de> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-27-2007
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" <d...@nospam.web.de> 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

 
Reply With Quote
 
Fabian Braennstroem
Guest
Posts: n/a
 
      03-28-2007
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" <d...@nospam.web.de> 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

 
Reply With Quote
 
Fabian Braennstroem
Guest
Posts: n/a
 
      03-29-2007
Hi Paul,

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
| SEO , Search Engine Optimizer, SEARCH OPtiMIzAtIoN with SeaRch OPtiMizer optimizer.seo@gmail.com Digital Photography 0 04-22-2007 04:20 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM
boolean endsWith(String s, Pattern pattern) lepikhin@gmail.com Java 17 11-16-2005 10:31 AM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57