Skip to content

Instantly share code, notes, and snippets.

@dyjjones
Created August 9, 2016 02:09
Show Gist options
  • Save dyjjones/10da5f7529c1daf67e89e0bcaf138de3 to your computer and use it in GitHub Desktop.
Save dyjjones/10da5f7529c1daf67e89e0bcaf138de3 to your computer and use it in GitHub Desktop.
server number tracker
#server numbers
from collections import defaultdict
def next_server_number(numbers):
for i,n in enumerate(sorted(numbers), 1):
if i != n:
return i
return len(numbers) + 1
def split_letter_digit(s):
alpha = ''
for i,c in enumerate(s):
if c.isalpha():
alpha += c
else:
return alpha, int(s[i:])
class Tracker:
def __init__(self):
self.host_names = defaultdict(list)
def allocate(self, s):
for i, element in enumerate(self.host_names[s], 1):
if not element:
self.host_names[s][i] = True
return s + str(i)
self.host_names[s].append(True)
return '{}{}'.format(s, len(self.host_names[s]))
def deallocate(self, s):
host_type, number = split_letter_digit(s)
if host_type in self.host_names:
val = self.host_names[host_type][number-1]
if val:
self.host_names[host_type][number-1] = False
else:
raise ValueError('Server name not allocated.')
else:
raise ValueError('No Such Host Type.')
tracker = Tracker()
print(tracker.allocate('taco'))
print(tracker.allocate('taco'))
tracker.deallocate('taco1')
tracker.deallocate('taco2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment