pattern search

D

Diez B. Roggisch

Fabian said:
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
 
?

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

Fabian said:
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.
 
F

Fabian Braennstroem

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
 
P

Paul McGuire

Fabian said:
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
 
P

Paul McGuire

Hi to all,

Wojciech Mu?a schrieb am 03/27/2007 03:34 PM:



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
 
F

Fabian Braennstroem

Hi to all,

Wojciech Mu?a schrieb am 03/27/2007 03:34 PM:
Use module glob.

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

Greetings!
Fabian
 
D

Diez B. Roggisch

Paul said:
Fabian said:
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
 
G

Gabriel Genellina

Paul said:
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)
 
F

Fabian Braennstroem

Hi,

Gabriel Genellina schrieb am 03/27/2007 10:09 PM:
Paul said:
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
 
F

Fabian Braennstroem

Hi Paul,

Paul McGuire schrieb am 03/27/2007 07:19 PM:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top