Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Identifying string success flag
Hi Guys,

I have a string of text seperated by a pipe symbol like:

# a list of flags to look for on success ...
submit_success = "TEXT_1|TEXT_2|TEXT_3|etc"

# if a flag is found do our routines ...
if any(s in self.driver.page_source for s in submit_success.split('|')):
    # run this code once a submit_success flag is found
Once one of those TEXT strings is found in the html we mark it as a success.

What i'm having trouble with is, how can i find out which of the "submit_success" flags was found?

Any help would be appreciated.

If you can be sure that submit_success only contains relevant flags, and nothing you're not interested in, you can just use the split string. Otherwise, you could use a set:

valid_flags = set('a|e|i|o|u'.split('|'))
current_flags = 'c|r|a|i|g'
matched_flags = valid_flags.intersection(current_flags.split('|')) # will be set(['a', 'i'])
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

built in any function only returns a boolean. As is i dont believe there is a way within the builtin to return the matches. Thus you would have to build your own any function that returns the matches.
(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)
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)
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
Thank you very much guys! solved the issue using your help. Python 3.8 does look good :)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Practicing using a "flag": please point in right direction magsloo 5 356 May-10-2019, 04:58 AM
Last Post: perfringo
  How to make an activation flag? Krszt 4 528 Oct-11-2018, 09:39 PM
Last Post: nilamo
  identifying a dictionary with an attribute? Skaperen 7 586 Oct-04-2018, 05:48 AM
Last Post: Skaperen
  Identifying only specific words in a string GilbyScarChest 2 482 Aug-08-2018, 03:22 AM
Last Post: GilbyScarChest
  Why does pyinstaller register success with py to exe, but? Pleiades 1 1,151 May-09-2018, 11:02 AM
Last Post: Pleiades
  No success with PyInstaller j.crater 2 1,785 Mar-10-2018, 10:29 PM
Last Post: j.crater
  Identifying the value of all adjacent elements in an array JoeB 2 2,436 Nov-23-2017, 05:10 PM
Last Post: JoeB
  Identifying object types microphone_head 5 1,440 Oct-01-2017, 02:04 PM
Last Post: buran
  FTP not downloading files but showing success python_lover 2 1,643 Jan-25-2017, 02:31 PM
Last Post: python_lover

Forum Jump:

Users browsing this thread: 1 Guest(s)