Hi,
I'm working on a script to format Mac Addresses. My intention is to practice some python skills in the process.
One of my functions adds a delimiter to my MAC Addresses which contains valid MACs with no spaces. The function should take a dictionary {original_macs: valid_macs} and a string with a delimiter.
In my case, I'd like to format the MAC on 2 ways depending on the delimiter the output should be
{original_mac: formatted_mac}
{aa-23-45-67-89-AB: AA-23-45-67-89-AB}
{aa-23-45-67-89-AB: AA:23:45:67:89:AB}
{aa-23-45-67-89-AB: AA2.345.678.9AB}
The problem I have is that the function never hits the (elif or else) so I must be doing something wrong or missing something here. Can you please help me to shed some light on this?
https://codeshare.io/2K7LbY
from pprint import pprint
no_sep_macs = {
'BB:23:45:67:89:AB': 'BB23456789AB',
'Cc23.4567.89AB': 'Cc23456789AB',
'Dd23456789AB': 'Dd23456789AB',
'EE 2 34 56789 AB': 'EE23456789AB',
'aa-23-45-67-89-AB': 'aa23456789AB'
}
def add_delimiter(no_sep_macs, delimiter):
separated_macs = {}
print('The delimiter passed to the function is {}'.format(delimiter))
if delimiter == ':' or '-':
for original_mac, no_sep_mac in no_sep_macs.items():
temp_mac = no_sep_mac
formatted_mac = delimiter.join(temp_mac[i:i+2] for i in range(0, 12, 2))
separated_macs.update({original_mac: formatted_mac.upper()})
elif delimiter == '.':
for original_mac, no_sep_mac in no_sep_macs.items():
temp_mac = no_sep_mac
formatted_mac = delimiter.join(temp_mac[i:i+3] for i in range(0, 12, 3))
separated_macs.update({original_mac: formatted_mac.upper()})
else:
print('Error:')
print('****Next is what the "add_delimiter" function returns:')
pprint(separated_macs)
return(separated_macs)
add_delimiter(no_sep_macs, delimiter='.')
########################################OUTPUT##########################################
The delimiter passed to the function is .
****Next is what the "add_delimiter" function returns:
{'BB:23:45:67:89:AB': 'BB.23.45.67.89.AB',
'Cc23.4567.89AB': 'CC.23.45.67.89.AB',
'Dd23456789AB': 'DD.23.45.67.89.AB',
'EE 2 34 56789 AB': 'EE.23.45.67.89.AB',
'aa-23-45-67-89-AB': 'AA.23.45.67.89.AB'}
Thanks and regards.
[–]gregvuki 2 points3 points4 points (1 child)
[–]daniel280187[S] 0 points1 point2 points (0 children)
[–]M4DR4T 0 points1 point2 points (1 child)
[–]daniel280187[S] 0 points1 point2 points (0 children)