Verified Commit bcdb276e authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

2to3 idioms

parent fa8231c7
......@@ -450,7 +450,7 @@ class _Printer(object):
self.__setup()
prompt = 'Hit Return for more, or q (and Return) to quit: '
lineno = 0
while 1:
while True:
try:
for i in range(lineno, lineno + self.MAXLINES):
print(self.__lines[i])
......
......@@ -224,7 +224,7 @@ class Collection(object):
except KeyError:
continue # Ignore unknown fields
# Dereference ListFields
if (type(field) == ListField and
if (isinstance(field, ListField) and
isinstance(field.field, EmbeddedDocumentField)):
edoc = field.field.document_type
try:
......
......@@ -36,7 +36,7 @@ def update_if_changed(obj, values):
for k, v in values.items():
vv = getattr(obj, k)
if v != vv:
if type(v) != int or not hasattr(vv, "id") or v != vv.id:
if not isinstance(v, int) or not hasattr(vv, "id") or v != vv.id:
setattr(obj, k, v)
changes += [(k, v)]
if changes:
......
......@@ -17,7 +17,7 @@ class SQLExtractor(BaseExtractor):
"""
Returns tuple of SQL Query, list of bind parameters
"""
if type(self.SQL) == list:
if isinstance(self.SQL, list):
for sql in self.SQL:
yield sql, []
else:
......
......@@ -34,7 +34,7 @@ class CapabilityRule(object):
def from_json(cls, data):
if "oids" not in data:
raise ValueError("oids is required")
if type(data["oids"]) != list:
if not isinstance(data["oids"], list):
raise ValueError("oids must be list")
return CapabilityRule(
oids=[load_rule(d) for d in data["oids"]]
......
......@@ -40,7 +40,7 @@ class MatcherRule(object):
def from_json(cls, data):
if "$match" not in data:
raise ValueError("Matcher is required")
if type(data["$match"]) != list:
if not isinstance(data["$match"], list):
raise ValueError("$match must be list")
return MatcherRule(
oids=[(d.get("$match"), load_rule(d)) for d in data["$match"]],
......
......@@ -98,7 +98,7 @@ class MIB(Document):
enum_map = s.get("enum_map", {})
for k in syntax:
sk = syntax[k]
if type(sk) != dict:
if not isinstance(sk, dict):
continue
if "nodetype" in sk and sk["nodetype"] == "namednumber":
enum_map[sk["number"]] = k
......
......@@ -81,7 +81,7 @@ class GeocoderCache(Document):
@classmethod
def clean_query(cls, query):
if type(query) == str:
if isinstance(query, str):
query = unicode(query, "utf-8")
query = query.upper().encode("utf-8")
query = cls.rx_slash.sub("/", query)
......
......@@ -296,7 +296,7 @@ class TableColumn(ReportNode):
:return:
"""
d = self.format_data(s)
if type(d) != SafeString:
if not isinstance(d, SafeString):
d = self.quote(d)
return "<td%s>%s</td>" % (self.html_td_attrs(), d)
......@@ -567,7 +567,7 @@ class TableSection(ReportSection):
if self.data:
n = 1
for row in self.data:
if type(row) == SectionRow:
if isinstance(row, SectionRow):
# Display section row
if (current_section and self.has_total and
current_section.subtotal):
......@@ -624,14 +624,14 @@ class TableSection(ReportSection):
if self.enumerate:
n = 1
for row in self.data:
if type(row) == SectionRow:
if isinstance(row, SectionRow):
writer.writerow([row.name])
continue
writer.writerow([n] + list(row))
n += 1
else:
for row in self.data:
if type(row) == SectionRow:
if isinstance(row, SectionRow):
writer.writerow([row.name])
continue
writer.writerow(row)
......@@ -658,7 +658,7 @@ class TableSection(ReportSection):
if self.enumerate:
n = 1
for row in self.data:
if type(row) == SectionRow:
if isinstance(row, SectionRow):
# writer.writerow([row.name])
prefix = [mrf, row.name]
if date:
......@@ -668,7 +668,7 @@ class TableSection(ReportSection):
n += 1
else:
for row in self.data:
if type(row) == SectionRow:
if isinstance(row, SectionRow):
# writer.writerow([row.name])
prefix = [mrf, row.name]
if date:
......
......@@ -17,7 +17,7 @@ def json_escape(s):
"""
Escape JSON predefined sequences
"""
if type(s) == bool:
if isinstance(s, bool):
return "true" if s else "false"
if s is None:
return ""
......
......@@ -344,7 +344,7 @@ class ManagedObjectSelector(models.Model):
if type(s) in (int, long, str, unicode):
s = [s]
if type(s) != list:
if not isinstance(s, list):
raise ValueError("list required")
objects = set()
for so in s:
......
......@@ -24,8 +24,7 @@ class Script(BaseScript):
def execute(self):
v = self.cli("brctl showmacs br0", cached=True)
macs = self.rx_mac.findall(v)
macs.sort()
macs = sorted(self.rx_mac.findall(v))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -23,8 +23,7 @@ class Script(BaseScript):
re.MULTILINE)
def execute(self):
macs = self.rx_mac.findall(self.cli("get system manuf info"))
macs.sort()
macs = sorted(self.rx_mac.findall(self.cli("get system manuf info")))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -203,7 +203,7 @@ class MetricScriptBase(BaseScriptMetaclass):
raise ValueError(
"Failed to parse file '%s': %s" % (path, e)
)
if type(data) != dict:
if not isinstance(data, dict):
raise ValueError(
"Error in file '%s': Must be defined as object" % path
)
......
......@@ -35,8 +35,7 @@ class Script(BaseScript):
pass
# Fallback to CLI
macs = self.rx_mac.findall(self.cli("show version", cached=True))
macs.sort()
macs = sorted(self.rx_mac.findall(self.cli("show version", cached=True)))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -21,8 +21,7 @@ class Script(BaseScript):
def execute(self):
v = self.cli("show system information", cached=True)
macs = self.rx_mac.findall(v)
macs.sort()
macs = sorted(self.rx_mac.findall(v))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -33,8 +33,7 @@ class Script(BaseScript):
}]
else:
cmd = self.cli("show interface")
macs = self.rx_mac2.findall(cmd)
macs.sort()
macs = sorted(self.rx_mac2.findall(cmd))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -20,8 +20,7 @@ class Script(BaseScript):
rx_mac = re.compile(r"MAC address\s+: ((?:[0-9A-F]{2}[:\-]){5}[0-9A-F]{2})")
def execute_cli(self):
macs = self.rx_mac.findall(self.cli("show mac"))
macs.sort()
macs = sorted(self.rx_mac.findall(self.cli("show mac")))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -22,8 +22,7 @@ class Script(BaseScript):
def execute(self):
v = self.cli("interface show", cached=True)
macs = self.rx_line.findall(v)
macs.sort()
macs = sorted(self.rx_line.findall(v))
return [{
"first_chassis_mac": f,
"last_chassis_mac": t
......
......@@ -514,7 +514,7 @@ class DiscoveryCheck(object):
for k, v in six.iteritems(values):
vv = getattr(obj, k)
if v != vv:
if type(v) != int or not hasattr(vv, "id") or v != vv.id:
if not isinstance(v, int) or not hasattr(vv, "id") or v != vv.id:
if k in ignore_empty and (v is None or v == ""):
continue
setattr(obj, k, v)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment