Don't use count() for substring checking

Use the idiomatic 'X in Y'
This commit is contained in:
Cole Robinson 2018-02-13 19:04:08 -05:00
parent ab884b620f
commit 8917305a6e
8 changed files with 23 additions and 23 deletions

View File

@ -156,13 +156,13 @@ class Command(object):
exc = "" exc = ""
try: try:
if app.count("virt-install"): if "virt-install" in app:
ret = virtinstall.main(conn=conn) ret = virtinstall.main(conn=conn)
elif app.count("virt-clone"): elif "virt-clone" in app:
ret = virtclone.main(conn=conn) ret = virtclone.main(conn=conn)
elif app.count("virt-convert"): elif "virt-convert" in app:
ret = virtconvert.main(conn=conn) ret = virtconvert.main(conn=conn)
elif app.count("virt-xml"): elif "virt-xml" in app:
ret = virtxml.main(conn=conn) ret = virtxml.main(conn=conn)
except SystemExit as sys_e: except SystemExit as sys_e:
ret = sys_e.code ret = sys_e.code
@ -316,13 +316,13 @@ class App(object):
if iscompare and auto_printarg: if iscompare and auto_printarg:
if self.appname == "virt-install": if self.appname == "virt-install":
if (not cli.count("--print-xml") and if ("--print-xml" not in cli and
not cli.count("--print-step") and "--print-step" not in cli and
not cli.count("--quiet")): "--quiet" not in cli):
args += " --print-step all" args += " --print-step all"
elif self.appname == "virt-clone": elif self.appname == "virt-clone":
if not cli.count("--print-xml"): if "--print-xml" not in cli:
args += " --print-xml" args += " --print-xml"
return args return args

View File

@ -118,7 +118,7 @@ def _storeForDistro(fetcher, guest):
try: try:
return urlfetcher.getDistroStore(guest, fetcher) return urlfetcher.getDistroStore(guest, fetcher)
except Exception as e: except Exception as e:
if str(e).count("502"): if "502" in str(e):
logging.debug("Caught proxy error: %s", str(e)) logging.debug("Caught proxy error: %s", str(e))
time.sleep(.5) time.sleep(.5)
continue continue

View File

@ -976,7 +976,7 @@ def parse_optstr_tuples(optstr):
if not opt: if not opt:
continue continue
if opt.count("="): if "=" in opt:
cliname, val = opt.split("=", 1) cliname, val = opt.split("=", 1)
else: else:
cliname = opt cliname = opt

View File

@ -493,7 +493,7 @@ class Cloner(object):
# If the suffix is greater than 7 characters, assume it isn't # If the suffix is greater than 7 characters, assume it isn't
# a file extension and is part of the disk name, at which point # a file extension and is part of the disk name, at which point
# just stick '-clone' on the end. # just stick '-clone' on the end.
if origpath.count(".") and len(origpath.rsplit(".", 1)[1]) <= 7: if "." in origpath and len(origpath.rsplit(".", 1)[1]) <= 7:
path, suffix = origpath.rsplit(".", 1) path, suffix = origpath.rsplit(".", 1)
suffix = "." + suffix suffix = "." + suffix

View File

@ -59,12 +59,12 @@ class VirtualDeviceAddress(XMLBuilder):
if addrstr is None: if addrstr is None:
return return
if addrstr.count(":") in [1, 2] and addrstr.count("."): if addrstr.count(":") in [1, 2] and "." in addrstr:
self.type = self.ADDRESS_TYPE_PCI self.type = self.ADDRESS_TYPE_PCI
addrstr, self.function = addrstr.split(".", 1) addrstr, self.function = addrstr.split(".", 1)
addrstr, self.slot = addrstr.rsplit(":", 1) addrstr, self.slot = addrstr.rsplit(":", 1)
self.domain = "0" self.domain = "0"
if addrstr.count(":"): if ":" in addrstr:
self.domain, self.bus = addrstr.split(":", 1) self.domain, self.bus = addrstr.split(":", 1)
elif addrstr == "spapr-vio": elif addrstr == "spapr-vio":
self.type = self.ADDRESS_TYPE_SPAPR_VIO self.type = self.ADDRESS_TYPE_SPAPR_VIO

View File

@ -78,9 +78,9 @@ def _sysconfig_keyboard(f):
re.search("KEYTABLE", s) is not None or re.search("KEYTABLE", s) is not None or
(re.search("KEYBOARD", s) is not None and (re.search("KEYBOARD", s) is not None and
re.search("KEYBOARDTYPE", s) is None)): re.search("KEYBOARDTYPE", s) is None)):
if s.count('"'): if '"' in s:
delim = '"' delim = '"'
elif s.count('='): elif '=' in s:
delim = '=' delim = '='
else: else:
continue continue

View File

@ -345,11 +345,11 @@ def _AddressStringToHostdev(conn, addrstr):
try: try:
# Determine addrstr type # Determine addrstr type
if addrstr.count(":") in [1, 2] and addrstr.count("."): if addrstr.count(":") in [1, 2] and "." in addrstr:
addrstr, func = addrstr.split(".", 1) addrstr, func = addrstr.split(".", 1)
addrstr, slot = addrstr.rsplit(":", 1) addrstr, slot = addrstr.rsplit(":", 1)
domain = "0" domain = "0"
if addrstr.count(":"): if ":" in addrstr:
domain, bus = addrstr.split(":", 1) domain, bus = addrstr.split(":", 1)
else: else:
bus = addrstr bus = addrstr
@ -360,14 +360,14 @@ def _AddressStringToHostdev(conn, addrstr):
hostdev.slot = "0x%.2X" % int(slot, 16) hostdev.slot = "0x%.2X" % int(slot, 16)
hostdev.bus = "0x%.2X" % int(bus, 16) hostdev.bus = "0x%.2X" % int(bus, 16)
elif addrstr.count(":"): elif ":" in addrstr:
vendor, product = addrstr.split(":") vendor, product = addrstr.split(":")
hostdev.type = "usb" hostdev.type = "usb"
hostdev.vendor = "0x%.4X" % int(vendor, 16) hostdev.vendor = "0x%.4X" % int(vendor, 16)
hostdev.product = "0x%.4X" % int(product, 16) hostdev.product = "0x%.4X" % int(product, 16)
elif addrstr.count("."): elif "." in addrstr:
bus, device = addrstr.split(".", 1) bus, device = addrstr.split(".", 1)
hostdev.type = "usb" hostdev.type = "usb"

View File

@ -93,7 +93,7 @@ def _sanitize_libxml_xml(xml):
# Strip starting <?...> line # Strip starting <?...> line
if xml.startswith("<?"): if xml.startswith("<?"):
ignore, xml = xml.split("\n", 1) ignore, xml = xml.split("\n", 1)
if not xml.endswith("\n") and xml.count("\n"): if not xml.endswith("\n") and "\n" in xml:
xml += "\n" xml += "\n"
return xml return xml
@ -116,7 +116,7 @@ def _add_pretty_child(parentnode, newnode):
whitespace and nicely format the result. whitespace and nicely format the result.
""" """
def node_is_text(n): def node_is_text(n):
return bool(n and n.type == "text" and not n.content.count("<")) return bool(n and n.type == "text" and "<" not in n.content)
def prevSibling(node): def prevSibling(node):
parent = node.get_parent() parent = node.get_parent()
@ -266,7 +266,7 @@ def _remove_xpath_node(ctx, xpath, dofree=True):
is_orig = (curxpath == xpath) is_orig = (curxpath == xpath)
node = _get_xpath_node(ctx, curxpath) node = _get_xpath_node(ctx, curxpath)
if curxpath.count("/"): if "/" in curxpath:
nextxpath, ignore = curxpath.rsplit("/", 1) nextxpath, ignore = curxpath.rsplit("/", 1)
else: else:
nextxpath = None nextxpath = None
@ -289,7 +289,7 @@ def _remove_xpath_node(ctx, xpath, dofree=True):
# Look for preceding whitespace and remove it # Look for preceding whitespace and remove it
white = node.get_prev() white = node.get_prev()
if white and white.type == "text" and not white.content.count("<"): if white and white.type == "text" and "<" not in white.content:
white.unlinkNode() white.unlinkNode()
white.freeNode() white.freeNode()