What are some code structuring/programming techniques to avoid this:
if url.netloc == "www.youtube.com" or "youtu.be" or "soundcloud.com or //
"instagram.com" or "vine.co" or ETC ETC
do XYZ
What are some code structuring/programming techniques to avoid this:
if url.netloc == "www.youtube.com" or "youtu.be" or "soundcloud.com or //
"instagram.com" or "vine.co" or ETC ETC
do XYZ
Following line
url.netloc == "www.youtube.com" or "youtu.be" or "soundcloud.com" or "instagram.com"
is like following:
(url.netloc == "www.youtube.com") or ("youtu.be") or ("soundcloud.com") or ("instagram.com")
and it always yield True
because non-empty string is treated as truth value if used as predicates.
Use in
instead as follow:
if url.netloc in ("www.youtube.com", "youtu.be", "soundcloud.com", "instagram.com", ..):
Alternatively use can use or
, but it requires you to repeat or url.netloc ==
multiple times.
The simplest, I guess, would be:
xyz_targets = ('www.youtube.com', 'youtu.be', ...)
if url.netloc in xyz_targets:
doXYZ()
Or even:
actions = {'www.youtube.com': doXYZ,
'youtu.be': doXYZ,
...
}
actions.get(url.netloc, doNothing)()
Or any variant on a similar idea which parses a config file for building xyz_targets
or actions