Python multiple search based on enabled or not
<p>Everyone, hello!</p>

<p>I'm currently trying to find several strings of text in telnet, from the Telnetlib (<a href="https://docs.python.org/2/library/telnetlib.html" rel="nofollow">https://docs.python.org/2/library/telnetlib.html</a>) in Python 2.7.</p>

<p>So far, the snippet I'm using that works great is:</p>

<pre><code> while True:
    r = tn.read_some()
    if any(x in r for x in [string1, string2]):
        action
</code></pre>

<p>The issue I'm facing is that I have several more strings (roughly 3 or so in total). Depending if they are enabled I would like to include them into the if function.</p>

<p>The enabling/disabling is set in ConfigParser in a config.ini file named string1_enable = yes/no</p>

<p>The longest route (and the only one I can think of right now is)</p>

<pre><code>if string1_enable == "yes" and string2_enable == "no" and string3_enable "no":
    s1 = (x in r for x in [string1])
if string1_enable == "no" and string2_enable == "yes" and string3_enable "no":
    s2 = (x in r for x in [string2])
</code></pre>

<p>This would of course be a disaster to look at, impossibly long to go through and I wouldn't even know how to properly implement it....
Thus, any help would be really appreciated. Thanks!</p>

<p>You can build a list based on the config:</p>

<pre><code>strings = []
if string1_enable == "yes":
    strings.append(string1)
if string2_enable == "yes":
    strings.append(string2)
if string3_enable == "yes":
    strings.append(string3)

while True:
   r = tn.read_some()
   if any(x in r for x in strings):
       action
</code></pre>

python if-statement