Python Forum
Identifying string success flag
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Identifying string success flag
#4
(Aug-13-2019, 05:07 PM)graham23s Wrote: What i'm having trouble with is, how can i find out which of the "submit_success" flags was found?
If you want to keep track of an intermediate value, using any() might not be the best, since the assumption is that it's a side-effect free operation. It doesn't have to be side-effect free, you just need to put a little effort into getting it to work.

>>> class ValueFinder:
...   def __init__(self, callback):
...     self.callback = callback
...     self.match = None
...     self.match_found = False
...   def __call__(self, test):
...     if self.callback(test):
...       self.match = test
...       self.match_found = True
...     return self.match_found
...
>>> submit_success = "TEXT_1|TEXT_2|TEXT_3|etc".split("|")
>>> page_source = '''one fish, TEXT_2 fish, red fish, blue fish'''
>>> finder = ValueFinder(lambda x: x in page_source)
>>> if any(finder(s) for s in submit_success):
...   print(finder.match)
...
TEXT_2
Or, instead of using any(), you could roll your own:
>>> submit_success = "TEXT_1|TEXT_2|TEXT_3|etc".split("|")
>>> page_source = '''one fish, TEXT_2 fish, red fish, blue fish'''
>>> def first(test, seq):
...   for item in seq:
...     if test(item):
...       return item
...   return None
...
>>> match = first(lambda s: s in page_source, submit_success)
>>> if match:
...   print(match)
...
TEXT_2
Once python 3.8 is out, you could also use the walrus operator to do this:
submit_success = "TEXT_1|TEXT_2|TEXT_3|etc"

if any((match := s) in self.driver.page_source for s in submit_success.split('|')):
    # "match" is the TEXT_ value that matched
Reply


Messages In This Thread
Identifying string success flag - by graham23s - Aug-13-2019, 05:07 PM
RE: Identifying string success flag - by ichabod801 - Aug-13-2019, 05:23 PM
RE: Identifying string success flag - by metulburr - Aug-13-2019, 05:24 PM
RE: Identifying string success flag - by nilamo - Aug-14-2019, 09:04 PM
RE: Identifying string success flag - by graham23s - Aug-14-2019, 09:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Identifying if the program I have is python and then decompiling jpnyc 7 2,518 Jun-02-2022, 10:16 PM
Last Post: jpnyc
  Identifying keywords in text drchips 6 126,502 Mar-29-2022, 12:32 PM
Last Post: snippsat
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,291 Mar-16-2022, 08:54 PM
Last Post: cubangt
  trying to put a a filter on identifying a straight CompleteNewb 1 1,725 Dec-01-2021, 11:11 PM
Last Post: CompleteNewb
  how to check for thread kill flag nanok66 1 2,264 May-09-2020, 10:06 PM
Last Post: nanok66
  Check for a special characters in a column and flag it ayomayam 0 2,123 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  Using a flag error blackjesus24 1 1,693 Jan-30-2020, 09:42 AM
Last Post: buran
  Practicing using a "flag": please point in right direction magsloo 5 3,283 May-10-2019, 04:58 AM
Last Post: perfringo
  How to make an activation flag? Krszt 4 3,489 Oct-11-2018, 09:39 PM
Last Post: nilamo
  identifying a dictionary with an attribute? Skaperen 7 3,975 Oct-04-2018, 05:48 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020