Skip to content

Commit dbffeb2

Browse files
author
Tim Vaillancourt
committed
safer/cleaner to use isinstance() to detect what class something is
1 parent 60a2f6d commit dbffeb2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

MongoBackup/Replset.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def __init__(self, db, user, password, authdb, max_lag_secs):
2121

2222
# Get a DB connection
2323
try:
24-
if self.db.__class__.__name__ == "DB":
24+
if isinstance(self.db, DB):
2525
self.connection = self.db.connection()
2626
else:
27-
raise Exception, "'db' field is an instance of %s, not 'DB'!" % self.db.__class__.__name__, None
27+
raise Exception, "'db' field is not an instance of class: 'DB'!", None
2828
except Exception, e:
2929
logging.fatal("Could not get DB connection! Error: %s" % e)
3030
raise e
@@ -147,17 +147,17 @@ def __init__(self, sharding, db, user, password, authdb, max_lag_secs):
147147
self.replset_conns = {}
148148

149149
# Check Sharding class:
150-
if not self.sharding.__class__.__name__ == "Sharding":
151-
raise Exception, "'sharding' field is an instance of %s, not 'Sharding'!" % self.sharding.__class__.__name__, None
150+
if not isinstance(self.sharding, Sharding):
151+
raise Exception, "'sharding' field is not an instance of class: 'Sharding'!", None
152152

153153
# Get a DB connection
154154
try:
155-
if self.db.__class__.__name__ == "DB":
155+
if isinstance(self.db, DB):
156156
self.connection = self.db.connection()
157157
if not self.connection.is_mongos:
158158
raise Exception, 'MongoDB connection is not to a mongos!', None
159159
else:
160-
raise Exception, "'db' field is an instance of %s, not 'DB'!" % self.db.__class__.__name__, None
160+
raise Exception, "'db' field is not an instance of class: 'DB'!", None
161161
except Exception, e:
162162
logging.fatal("Could not get DB connection! Error: %s" % e)
163163
raise e

MongoBackup/Sharding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from time import sleep
44

5+
from Common import DB
6+
57

68
class Sharding:
79
def __init__(self, db, user=None, password=None, authdb='admin', balancer_wait_secs=300, balancer_sleep=5):
@@ -16,12 +18,12 @@ def __init__(self, db, user=None, password=None, authdb='admin', balancer_wait_s
1618

1719
# Get a DB connection
1820
try:
19-
if self.db.__class__.__name__ == "DB":
21+
if isinstance(self.db, DB):
2022
self.connection = self.db.connection()
2123
if not self.connection.is_mongos:
2224
raise Exception, 'MongoDB connection is not to a mongos!', None
2325
else:
24-
raise Exception, "'db' field is an instance of %s, not 'DB'!" % self.db.__class__.__name__, None
26+
raise Exception, "'db' field is not an instance of class: 'DB'!", None
2527
except Exception, e:
2628
logging.fatal("Could not get DB connection! Error: %s" % e)
2729
raise e

0 commit comments

Comments
 (0)