473,765 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Troubleshooting : re.finditer() creates object even when no match found

Hello,
I really like the finditer() method of the re module. I'm having
difficulty at the moment, however, because finditer() still creates a
callable-iterator oject, even when no match is found. This is
undesirable in cases where I would like to circumvent execution of code
meant to parse out data from my finditer() object.

I know that if I place a finditer() object in an iterative for loop,
the loop will not execute, but is there some way I can test to see if
the object contains no matches in the first place? I thought about
using .next() but I don't want to lose the ability to process the first
(sometimes only) match in the finditer() object.
Thanks in advance,
Chris

Jul 18 '05 #1
8 2882
Chris Lasher wrote:
I know that if I place a finditer() object in an iterative for loop,
the loop will not execute, but is there some way I can test to see if
the object contains no matches in the first place?


Basically, you want to peek into an interable. See my recipes:

http://aspn.activestate.com/ASPN/Coo.../Recipe/304373

The short answer is that you can do something like:

try:
first, iterable = peek(iterable)
except StopIteration:
# do whatever you do if there are no matches
else:
# do whatever you do if there are matches

and you won't lose the first element of the iterable.

Steve
Jul 18 '05 #2
Thanks Steve,
That's odd that there's no built-in method to do this. It seems like
it would be a common task. Is there any way to request a feature like
this from the RE module keepers, whomever they may be?
In the meantime, may I use your code, with accredation to you?
Thanks,
Chris

Jul 18 '05 #3
Chris Lasher wrote:
Is there any way to request a feature like
this from the RE module keepers, whomever they may be?
The most direct way would be to go to Python at sourceforge[1] and make
a feature request to add peek to itertools. (This is probably the most
reasonable location for it.) Requests accompanied by patches are much
more likely to be accepted. =) Of course, your feature, if accepted,
wouldn't be applied until Python 2.5, which is probably about a year and
a half off.

I actually thought about doing this myself, but I've got a few other
things on my stack, and since itertools is (I believe) written in C,
writing the patch would be a little more work...
In the meantime, may I use your code, with accredation to you?


Help yourself. That's what the Cookbook's for. =)

[1] http://sourceforge.net/projects/python/
Jul 18 '05 #4
Chris Lasher wrote:
That's odd that there's no built-in method to do this. It seems like
it would be a common task.


if you do this a lot, maybe you shouldn't use finditer? iterators are
designed to give you the next item (if any) when you're ready to deal
with it... if that's not what you want, you can use findall, search loops,
scanner objects, etc. or you can implement a standard "iterate ahead"
loop.

what's your use case?

</F>

Jul 18 '05 #5
Chris Lasher wrote:
Hello,
I really like the finditer() method of the re module. I'm having
difficulty at the moment, however, because finditer() still creates a
callable-iterator oject, even when no match is found. This is
undesirable in cases where I would like to circumvent execution of code
meant to parse out data from my finditer() object.


Take a look at itertools.tee

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #6
Nick Coghlan wrote:
Chris Lasher wrote:
Hello,
I really like the finditer() method of the re module. I'm having
difficulty at the moment, however, because finditer() still creates a
callable-iterator oject, even when no match is found. This is
undesirable in cases where I would like to circumvent execution of code
meant to parse out data from my finditer() object.

Take a look at itertools.tee


Bleh - I hit send instead of delete. Tee probably doesn't do what you want.
Steve's cookbook recipe is likely a better option.

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #7
Nick Coghlan wrote:
Nick Coghlan wrote:
Chris Lasher wrote:
Hello,
I really like the finditer() method of the re module. I'm having
difficulty at the moment, however, because finditer() still creates a
callable-iterator oject, even when no match is found. This is
undesirable in cases where I would like to circumvent execution of code
meant to parse out data from my finditer() object.


Take a look at itertools.tee


Bleh - I hit send instead of delete. Tee probably doesn't do what you
want. Steve's cookbook recipe is likely a better option.


Actually, there's an equally valid solution with tee too -- check Peter
Otten's comments at the bottom of the recipe.

Steve
Jul 18 '05 #8
Steven Bethard wrote:
first, iterable = peek(iterable)


I really like this as a general solution to a problem that bothers me
occasionally. IMHO it's much better than having UndoFiles or similar
things lying about for every use case.

Thanks!
--
Michael Hoffman
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9999
by: Clint Herron | last post by:
Howdy! I posted this question on CSharpCorner.com, but then realized I should probably post it on a more active newsgroup. This will be my only cross-post. I'm creating a game engine, and using CodeDOM for my scripting needs (I realize I could use yacc or something else, but I wanted to try using CodeDOM -- this is more of an exercise for me to learn this stuff). Well, I compile my in-game scripts just fine. See appendix A for my code
3
2739
by: Ben | last post by:
Hi I have just created a new ASP .net web project and when i run the project I receieve this error: System.Reflection.AmbiguousMatchException: Ambiguous match found. Any help would be much appreciated Thanks
3
1672
by: Alan Silver | last post by:
Hello, I have some pages that are protected by forms authentication, and am adding code to the global.asax so that if someone tries to load (say) /order83.aspx, if they are logged in, it will rewrite the url to /order.aspx?orderid=83, and if they aren't logged in, it will redirect them to the default page. The reason for this is that I don't want people seeing the login page if they aren't site admin folk. If I just let the normal...
0
3368
by: gui.besse | last post by:
It seems that we can't bind a collection of instance of different type. Let's have an example: // My POCO public interface ITest { string Name { get;set;} } public class A : ITest {
3
3740
by: Alexandre | last post by:
Hi! I receive this error in my webapp: Ambiguous match found. At line: Line 1: <%@ page language="C#" masterpagefile="~/memberscontents/master_interna.master" autoeventwireup="true" inherits="members_contents_user_profile, App_Web_user_profile.aspx.73f80179" title="Untitled Page" %>
4
6285
by: rn5a | last post by:
Consider the following code: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) Dim dInfo As DirectoryInfo dInfo = New DirectoryInfo(Server.MapPath("/Folder1")) dgFD.DataSource = dInfo.GetFiles("*.*") dgFD.DataBind() End Sub
1
7112
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
2
2284
by: aparna | last post by:
Hi, I have one project in dotnet 1.1and when I am trying to compile that project I am getting "Ambiguous match found" error in code behind file(.aspx.cs) but there is no such file in that project,how I can view that file and how to remove this error. Thanks Aparna
4
2445
by: =?Utf-8?B?U3dhcHB5?= | last post by:
hi, Im running this code public Boolean IsLayoutOpen(String strLayoutName) { Layout Layout_Obj = null; try { Layouts layouts = instrumentation.Layouts; //This call is through COM object
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8830
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.