rdb_master_mongodb
whereのような文字列rdb_
が固定されてmaster
おり、データベース名でありmongodb
、mysql
、mongodb
、postgres
、mssql
またはのいずれかになりbdb
ます。に値を持つ辞書からこの文字列の値を取得する必要がありますmyDict[master][mongodb]
。これを取得するには、文字列を分割してrdb_master_mongodb
と の値を取得する必要がmaster
ありmongodb
ます。文字列が になることがあるため、分割は使用できませんrdb_master_test_mongodb
。したがってendswith
、正確なキーを取得するために使用する必要があります。ただし、endswith
リストでは機能しません。
タプルから一致するタプル値を取得する必要があります。今、私はこれを次のようにします:
import re
name = 'rdb_master_mongodb'
s = re.sub('rdb_', "", name)
VALID_DB = ('mysql', 'postgres', 'mongodb', 'mssql', 'bdb')
(a, b, c, d, e) = VALID_DB
if s.endswith(a):
db = a
if s.endswith(b):
db = b
if s.endswith(c):
db = c
if s.endswith(d):
db = d
if s.endswith(e):
db = e
db_name = re.sub('_'+db, "", s)
print db_name+" is "+db
これを行うより良い方法はありますか?