Skip to content

Commit

Permalink
python: Resolve pep8 comparison errors.
Browse files Browse the repository at this point in the history
Resolve pep8 errors:

  E711 comparison to None should be 'if cond is None:'

The reason comparing against None with "is None" is preferred over
"== None" is because a class can define its own equality operator and
produce bizarre unexpected behavior.  Using "is None" has a very
explicit meaning that can not be overridden.

  E721 do not compare types, use 'isinstance()'

This one is actually a mistake by the tool in most cases.
'from ovs.db import types' looks just like types from the Python stdlib.
In those cases, use the full ovs.db.types name.  Fix one case where it
actually was types from the stdlib.

Signed-off-by: Russell Bryant <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
russellb committed Jan 5, 2016
1 parent bdca6c4 commit 3c05711
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ endif
if HAVE_FLAKE8
ALL_LOCAL += flake8-check
flake8-check: $(FLAKE8_PYFILES)
$(AM_V_GEN) if flake8 $^ --ignore=E111,E112,E113,E123,E126,E127,E128,E129,E131,E201,E203,E222,E225,E226,E231,E241,E251,E261,E262,E265,E271,E501,E502,E703,E711,E713,E721,W601 ${FLAKE8_FLAGS}; then touch $@; else exit 1; fi
$(AM_V_GEN) if flake8 $^ --ignore=E111,E112,E113,E123,E126,E127,E128,E129,E131,E201,E203,E222,E225,E226,E231,E241,E251,E261,E262,E265,E271,E501,E502,E703,E713,W601 ${FLAKE8_FLAGS}; then touch $@; else exit 1; fi
endif

include $(srcdir)/manpages.mk
Expand Down
4 changes: 2 additions & 2 deletions debian/ovs-monitor-ipsec
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ path certificate "%s";
if host in self.psk_hosts:
raise error.Error("host %s already defined for psk" % host)

if vals["certificate"] == None:
if vals["certificate"] is None:
raise error.Error("'certificate' not defined for %s" % host)
elif vals["private_key"] == None:
elif vals["private_key"] is None:
# Assume the private key is stored in the same PEM file as
# the certificate. We make a copy of "vals" so that we don't
# modify the original "vals", which would cause the script
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/db/idl.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def run(self):
if (msg.type == ovs.jsonrpc.Message.T_NOTIFY
and msg.method == "update"
and len(msg.params) == 2
and msg.params[0] == None):
and msg.params[0] is None):
# Database contents changed.
self.__parse_update(msg.params[1])
elif (msg.type == ovs.jsonrpc.Message.T_REPLY
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/db/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def is_identifier(s):


def json_type_to_string(type_):
if type_ == None:
if type_ is None:
return "null"
elif type_ == bool:
return "boolean"
Expand Down
10 changes: 6 additions & 4 deletions python/ovs/db/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from ovs.db import error
import ovs.db.parser
from ovs.db import types
import ovs.db.types


def _check_id(name, json):
Expand Down Expand Up @@ -101,7 +101,8 @@ def copy(self):
return DbSchema.from_json(self.to_json())

def __follow_ref_table(self, column, base, base_name):
if not base or base.type != types.UuidType or not base.ref_table_name:
if (not base or base.type != ovs.db.types.UuidType
or not base.ref_table_name):
return

base.ref_table = self.tables.get(base.ref_table_name)
Expand Down Expand Up @@ -181,7 +182,7 @@ def from_json(json, name):
indexes_json = parser.get_optional("indexes", [list], [])
parser.finish()

if max_rows == None:
if max_rows is None:
max_rows = sys.maxint
elif max_rows <= 0:
raise error.Error("maxRows must be at least 1", json)
Expand Down Expand Up @@ -257,7 +258,8 @@ def from_json(json, name):
parser = ovs.db.parser.Parser(json, "schema for column %s" % name)
mutable = parser.get_optional("mutable", [bool], True)
ephemeral = parser.get_optional("ephemeral", [bool], False)
type_ = types.Type.from_json(parser.get("type", [dict, str, unicode]))
type_ = ovs.db.types.Type.from_json(parser.get("type",
[dict, str, unicode]))
parser.finish()

return ColumnSchema(name, mutable, not ephemeral, type_)
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def finish(self):
elif self.parse_state != Parser.__parse_end:
self.__error("unexpected end of input")

if self.error == None:
if self.error is None:
assert len(self.stack) == 1
return self.stack.pop()
else:
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def run(self):
request.id = "echo"
self.rpc.send(request)
else:
assert action == None
assert action is None

def wait(self, poller):
if self.rpc is not None:
Expand Down
31 changes: 16 additions & 15 deletions tests/test-ovsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ovs.db.idl
import ovs.db.schema
from ovs.db import data
from ovs.db import types
import ovs.db.types
import ovs.ovsuuid
import ovs.poller
import ovs.util
Expand All @@ -37,8 +37,8 @@ def unbox_json(json):


def do_default_atoms():
for type_ in types.ATOMIC_TYPES:
if type_ == types.VoidType:
for type_ in ovs.db.types.ATOMIC_TYPES:
if type_ == ovs.db.types.VoidType:
continue

sys.stdout.write("%s: " % type_.to_string())
Expand All @@ -54,15 +54,16 @@ def do_default_atoms():
def do_default_data():
any_errors = False
for n_min in 0, 1:
for key in types.ATOMIC_TYPES:
if key == types.VoidType:
for key in ovs.db.types.ATOMIC_TYPES:
if key == ovs.db.types.VoidType:
continue
for value in types.ATOMIC_TYPES:
if value == types.VoidType:
for value in ovs.db.types.ATOMIC_TYPES:
if value == ovs.db.types.VoidType:
valueBase = None
else:
valueBase = types.BaseType(value)
type_ = types.Type(types.BaseType(key), valueBase, n_min, 1)
valueBase = ovs.db.types.BaseType(value)
type_ = ovs.db.types.Type(ovs.db.types.BaseType(key),
valueBase, n_min, 1)
assert type_.is_valid()

sys.stdout.write("key %s, value %s, n_min %d: "
Expand All @@ -80,25 +81,25 @@ def do_default_data():

def do_parse_atomic_type(type_string):
type_json = unbox_json(ovs.json.from_string(type_string))
atomic_type = types.AtomicType.from_json(type_json)
atomic_type = ovs.db.types.AtomicType.from_json(type_json)
print ovs.json.to_string(atomic_type.to_json(), sort_keys=True)


def do_parse_base_type(type_string):
type_json = unbox_json(ovs.json.from_string(type_string))
base_type = types.BaseType.from_json(type_json)
base_type = ovs.db.types.BaseType.from_json(type_json)
print ovs.json.to_string(base_type.to_json(), sort_keys=True)


def do_parse_type(type_string):
type_json = unbox_json(ovs.json.from_string(type_string))
type_ = types.Type.from_json(type_json)
type_ = ovs.db.types.Type.from_json(type_json)
print ovs.json.to_string(type_.to_json(), sort_keys=True)


def do_parse_atoms(type_string, *atom_strings):
type_json = unbox_json(ovs.json.from_string(type_string))
base = types.BaseType.from_json(type_json)
base = ovs.db.types.BaseType.from_json(type_json)
for atom_string in atom_strings:
atom_json = unbox_json(ovs.json.from_string(atom_string))
try:
Expand All @@ -110,7 +111,7 @@ def do_parse_atoms(type_string, *atom_strings):

def do_parse_data(type_string, *data_strings):
type_json = unbox_json(ovs.json.from_string(type_string))
type_ = types.Type.from_json(type_json)
type_ = ovs.db.types.Type.from_json(type_json)
for datum_string in data_strings:
datum_json = unbox_json(ovs.json.from_string(datum_string))
datum = data.Datum.from_json(type_, datum_json)
Expand All @@ -119,7 +120,7 @@ def do_parse_data(type_string, *data_strings):

def do_sort_atoms(type_string, atom_strings):
type_json = unbox_json(ovs.json.from_string(type_string))
base = types.BaseType.from_json(type_json)
base = ovs.db.types.BaseType.from_json(type_json)
atoms = [data.Atom.from_json(base, atom_json)
for atom_json in unbox_json(ovs.json.from_string(atom_strings))]
print ovs.json.to_string([data.Atom.to_json(atom)
Expand Down
4 changes: 2 additions & 2 deletions tests/test-reconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def diff_stats(old, new, delta):
print(" %sconnected" % negate)

if (old.last_connected != new.last_connected or
(new.msec_since_connect != None and
(new.msec_since_connect is not None and
old.msec_since_connect != new.msec_since_connect - delta) or
(old.total_connected_duration != new.total_connected_duration - delta
and not (old.total_connected_duration == 0 and
Expand All @@ -139,7 +139,7 @@ def diff_stats(old, new, delta):
% (new.msec_since_connect, new.total_connected_duration))

if (old.last_disconnected != new.last_disconnected or
(new.msec_since_disconnect != None and
(new.msec_since_disconnect is not None and
old.msec_since_disconnect != new.msec_since_disconnect - delta)):
print(" disconnected at %d ms (%d ms ago)"
% (new.last_disconnected, new.msec_since_disconnect))
Expand Down
4 changes: 2 additions & 2 deletions vtep/ovs-vtep
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bfd_ref = {}
def call_prog(prog, args_list):
cmd = [prog, "-vconsole:off"] + args_list
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
if len(output) == 0 or output[0] == None:
if len(output) == 0 or output[0] is None:
output = ""
else:
output = output[0].strip()
Expand Down Expand Up @@ -100,7 +100,7 @@ class Logical_Switch(object):
column = vtep_ctl("--columns=tunnel_key find logical_switch "
"name=%s" % self.name)
tunnel_key = column.partition(":")[2].strip()
if (tunnel_key and type(eval(tunnel_key)) == types.IntType):
if tunnel_key and isinstance(eval(tunnel_key), types.IntType):
self.tunnel_key = tunnel_key
vlog.info("using tunnel key %s in %s"
% (self.tunnel_key, self.name))
Expand Down

0 comments on commit 3c05711

Please sign in to comment.