The following code has begun to smell, but I have not yet decided with what to replace it, other than, obviously, a database.
I made a very unsatisfactory workaround for my attempt to make relationships among tuple members as I assigned them.
How can I modify a piece of code like this that contains some hostnames or a few secrets, and hierarchical relationships among them, to better follow good coding practices for Python 3.5+?
#!/usr/bin/env python3.5
'''
This file contains basic data about hosts for clustered ReDiS, as deployed.
Example Usage:
>>> import socket
>>> import redislist
>>> import redislist.b01red
>>> for redisHost in b01red:
... print (redisHost.hostname)
... print (socket.getaddrinfo(redisHost.hostname, redisHost.port))
...
'''
from collections import namedtuple
RedisContainer = namedtuple('RedisContainer', 'hostname addr port slaveof')
b01red = ['', '', '', ''] # These values are placeholders to prevent
# premature assignment errors.
b01red = [RedisContainer(hostname='b01rd00009',addr='10.253.128.1',port='7381', slaveof='master'), \
# Slaves are b01rd00000, b01grd00009, b01grd00000.
RedisContainer(hostname='b01rd00000',addr='10.253.128.2',port='7381', slaveof=b01red[0] ),
# 10.253.128.2 (b01rd00000) slaveof 10.253.128.1 (b01rd00009).
RedisContainer(hostname='b01rd00001',addr='10.253.128.3',port='7381', slaveof='master'), \
# Slaves are b01rd00002, b01grd00001, b01grd00002.
RedisContainer(hostname='b01rd00002',addr='10.253.128.4',port='7381', slaveof=b01red[2] )]
# 10.253.128.4 (b01rd00002) slaveof 10.253.128.3 (b01rd00001).
# Reassign to account for new values for relative variables.
b01red[0] = RedisContainer(hostname='b01rd00009',addr='10.253.128.1',port='7381', slaveof='master')
# Slaves are b01rd00000, b01grd00009, b01grd00000.
b01red[1] = RedisContainer(hostname='b01rd00000',addr='10.253.128.2',port='7381', slaveof=b01red[0])
# 10.253.128.2 (b01rd00000) slaveof 10.253.128.1 (b01rd00009).
b01red[2] = RedisContainer(hostname='b01rd00001',addr='10.253.128.3',port='7381', slaveof='master')
# Slaves are b01rd00002, b01grd00001, b01grd00002.
b01red[3] = RedisContainer(hostname='b01rd00002',addr='10.253.128.4',port='7381', slaveof=b01red[2])
# 10.253.128.4 (b01rd00002) slaveof 10.253.128.3 (b01rd00001).
b01gred = ['', '', '', '', '', '', ''] # These values are placeholders to prevent
# premature assignment errors.
b01gred = [RedisContainer(hostname='b01grd00009',addr='10.150.55.1',port='7381', slaveof=b01red[0]), \
# 10.150.55.1 (b01grd00009) slaveof 10.253.128.1 (b01rd00009).
RedisContainer(hostname='b01grd00000',addr='10.150.55.2',port='7381', slaveof=b01red[0]), \
# 10.150.55.2 (b01grd00000) slaveof 10.253.128.1 (b01rd00009).
RedisContainer(hostname='b01grd00001',addr='10.150.55.3',port='7381', slaveof=b01red[2]), \
# 10.150.55.3 (b01grd00001) slaveof 10.253.128.3 (b01rd00001).
RedisContainer(hostname='b01grd00002',addr='10.150.55.4',port='7381', slaveof=b01red[2])]
# 10.150.55.4 (b01grd00002) slaveof 10.253.128.3 (b01rd00001).
b01gred[0] = RedisContainer(hostname='b01grd00009',addr='10.150.55.1',port='7381', slaveof=b01red[0])
# 10.150.55.1 (b01grd00009) slaveof 10.253.128.1 (b01rd00009).
b01gred[1] = RedisContainer(hostname='b01grd00000',addr='10.150.55.2',port='7381', slaveof=b01red[0])
# 10.150.55.2 (b01grd00000) slaveof 10.253.128.1 (b01rd00009).
b01gred[2] = RedisContainer(hostname='b01grd00001',addr='10.150.55.3',port='7381', slaveof=b01red[2])
# 10.150.55.3 (b01grd00001) slaveof 10.253.128.3 (b01rd00001).
b01gred[3] = RedisContainer(hostname='b01grd00002',addr='10.150.55.4',port='7381', slaveof=b01red[2])
# 10.150.55.4 (b01grd00002) slaveof 10.253.128.3 (b01rd00001).
b03red = ['', '', '', '', '', '', '']
b03red = [RedisContainer(hostname='b03rd00009',addr='10.253.128.1',port='7381', slaveof=''), \
# Slaves are b03rd00010, gb03rd00009, gb03rd00010
RedisContainer(hostname='b03rd00010',addr='10.253.128.2',port='7381', slaveof=''), \
# 10.253.128.2 (b03rd00010) slaveof 10.253.128.1 (b03rd00009).
RedisContainer(hostname='b03rd00011',addr='10.253.128.51',port='7381', slaveof=''), \
# Slaves are b03rd00012, gb03rd00011, gb03rd00012
RedisContainer(hostname='b03rd00012',addr='10.253.128.84',port='7381', slaveof=''), \
# 10.253.128.84 (b03rd00012) slaveof 10.253.128.51 (b03rd00011).
RedisContainer(hostname='b03rd00013',addr='10.253.128.17',port='7381', slaveof=''), \
# Slaves are b03rd00014, gb03rd00013, gb03rd00014
RedisContainer(hostname='b03rd00014',addr='10.253.128.222',port='7381', slaveof=b03red[4])]
b03red[0] = RedisContainer(hostname='b03rd00009',addr='10.253.128.1',port='7381', slaveof='master')
b03red[1] = RedisContainer(hostname='b03rd00010',addr='10.253.128.2',port='7381', slaveof=b03red[0])
b03red[2] = RedisContainer(hostname='b03rd00011',addr='10.253.128.51',port='7381', slaveof='master')
b03red[3] = RedisContainer(hostname='b03rd00012',addr='10.253.128.84',port='7381', slaveof=b03red[2])
b03red[4] = RedisContainer(hostname='b03rd00013',addr='10.253.128.17',port='7381', slaveof='master')
b03red[5] = RedisContainer(hostname='b03rd00014',addr='10.253.128.222',port='7381', slaveof=b03red[4])
gb03red = ['','','','','','','']
gb03red = [RedisContainer(hostname='gb03rd00009',addr='10.150.55.1',port='7381', slaveof=b03red[0]), \
# 10.150.55.1 (gb03rd00009) slaveof 10.253.128.1 (b03rd00009).
RedisContainer(hostname='gb03rd00010',addr='10.150.55.2',port='7381', slaveof=b03red[0]), \
# 10.150.55.2 (gb03rd00009) slaveof 10.253.128.1 (b03rd00009).
RedisContainer(hostname='gb03rd00011',addr='10.150.55.3',port='7381', slaveof=b03red[2]), \
# 10.150.55.3 (gb03rd00011) slaveof 10.253.128.51 (b03rd00011).
RedisContainer(hostname='gb03rd00012',addr='10.150.55.4',port='7381', slaveof=b03red[2]), \
# 10.150.55.4 (gb03rd00012) slaveof 10.253.128.51 (b03rd00011).
RedisContainer(hostname='gb03rd00013',addr='10.150.55.5',port='7381', slaveof=b03red[4]), \
# 10.150.55.5 (gb03rd00013) slaveof 10.253.128.17 (b03rd00013)
RedisContainer(hostname='gb03rd00014',addr='10.150.55.6',port='7381', slaveof=b03red[4]) ]
# 10.150.55.6 (gb03rd00014) slaveof 10.253.128.17 (b03rd00013)
# Reassign to account for new values.
gb03red[0] = RedisContainer(hostname='gb03rd00009',addr='10.150.55.1',port='7381', slaveof=b03red[0])
gb03red[1] = RedisContainer(hostname='gb03rd00010',addr='10.150.55.2',port='7381', slaveof=b03red[0])
gb03red[2] = RedisContainer(hostname='gb03rd00011',addr='10.150.55.3',port='7381', slaveof=b03red[2])
gb03red[3] = RedisContainer(hostname='gb03rd00012',addr='10.150.55.4',port='7381', slaveof=b03red[2])
gb03red[4] = RedisContainer(hostname='gb03rd00013',addr='10.150.55.5',port='7381', slaveof=b03red[4])
gb03red[5] = RedisContainer(hostname='gb03rd00014',addr='10.150.55.6',port='7381', slaveof=b03red[4])
clusters = [b01red, b01gred, b03red, gb03red]
def list_hosts():
for a_cluster in clusters:
for redisHost in a_cluster:
if type(redisHost.slaveof) == RedisContainer:
print (redisHost.hostname, redisHost.addr, "Redis Master IP Address = ", redisHost.slaveof.addr)
else:
print (redisHost.hostname, redisHost.addr, "Redis Master IP Address = ", redisHost.slaveof)
The following shows some of the output when this runs: \
$ python3.5 Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44) Type "help", "copyright", "credits" or "license" for more information. >>> import redislist >>> redislist.list_hosts() b01rd00009 10.253.128.1 Redis Master IP Address = master b01rd00000 10.253.128.2 Redis Master IP Address = 10.253.128.1 b01rd00001 10.253.128.3 Redis Master IP Address = master b01rd00002 10.253.128.4 Redis Master IP Address = 10.253.128.3 b01grd00009 10.150.55.1 Redis Master IP Address = 10.253.128.1 b01grd00000 10.150.55.2 Redis Master IP Address = 10.253.128.1 b01grd00001 10.150.55.3 Redis Master IP Address = 10.253.128.3 b01grd00002 10.150.55.4 Redis Master IP Address = 10.253.128.3 b03rd00009 10.253.128.1 Redis Master IP Address = master b03rd00010 10.253.128.2 Redis Master IP Address = 10.253.128.1 b03rd00011 10.253.128.51 Redis Master IP Address = master b03rd00012 10.253.128.84 Redis Master IP Address = 10.253.128.51 b03rd00013 10.253.128.17 Redis Master IP Address = master b03rd00014 10.253.128.222 Redis Master IP Address = 10.253.128.17 gb03rd00009 10.150.55.1 Redis Master IP Address = 10.253.128.1 gb03rd00010 10.150.55.2 Redis Master IP Address = 10.253.128.1 gb03rd00011 10.150.55.3 Redis Master IP Address = 10.253.128.51 gb03rd00012 10.150.55.4 Redis Master IP Address = 10.253.128.51 gb03rd00013 10.150.55.5 Redis Master IP Address = 10.253.128.17 gb03rd00014 10.150.55.6 Redis Master IP Address = 10.253.128.17
I don't have much experience with named tuples, here, and I'm especially interested in better ways to take advantage of them. Also, let me know if this is annoyingly long, and I will reduce its size.
Currently, the best alternative to my oddly-named tuples seems to be the "Ordered Dictionary", described in this page.
Summary:
If I really must use a .py to "store" some data (like hostnames or secrets) where some parts depend on other parts, as exemplified in the above code, what best practices for coding in Python 3.5 will improve this code?