Last active
August 23, 2020 14:39
-
-
Save progala/5b47397ffeccbb686ae750613c4f2379 to your computer and use it in GitHub Desktop.
Solution for fun little challenge from Nick Russo https://twitter.com/nickrusso42518/status/1296852620959776777
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| from pathlib import Path | |
| def compute_dscp_class(dscp): | |
| quot, rmnd = divmod(dscp, 8) | |
| if rmnd == 0: | |
| dscpcl = "cs{}".format(quot) | |
| elif (quot, rmnd) == (5, 6): | |
| dscpcl = "ef" | |
| else: | |
| dscpcl = "af{}{}".format(quot, rmnd // 2) | |
| return dscpcl | |
| def gen_conversion_table(): | |
| tos_string_tbl = { | |
| 0: "Routine", | |
| 1: "Priority", | |
| 2: "Immediate", | |
| 3: "Flash", | |
| 4: "FlashOverride", | |
| 5: "Critical", | |
| 6: "Internetworkcontrol", | |
| 7: "Networkcontrol", | |
| } | |
| dscp_dec = [0, *list(range(8, 41, 2)), 46, 48, 56] | |
| conv_tbl = [] | |
| for dscp in dscp_dec: | |
| tos_dec = dscp << 2 | |
| dscpcl = compute_dscp_class(dscp) | |
| conv_tbl.append( | |
| { | |
| "DSCP Class": dscpcl, | |
| "DSCP (bin)": "{0:06b}".format(dscp), | |
| "DSCP (hex)": "{0:#04x}".format(dscp), | |
| "DSCP (dec)": "{}".format(dscp), | |
| "ToS (dec)": "{}".format(tos_dec), | |
| "ToS (hex)": "{0:#02x}".format(tos_dec), | |
| "ToS (bin)": "{0:08b}".format(tos_dec), | |
| "ToS Prec. (bin)": "{:08b}".format(tos_dec)[0:3], | |
| "ToS Prec. (dec)": "{}".format(dscp >> 3 & 1), | |
| "ToS Delay Flag": "{}".format(dscp >> 2 & 1), | |
| "ToS Throghput Flag": "{}".format(dscp >> 1 & 1), | |
| "ToS Reliability Flag": "0", | |
| "TOS String Format": "{}".format(tos_string_tbl[dscp // 8]), | |
| } | |
| ) | |
| return conv_tbl | |
| def main(): | |
| conversion_table = gen_conversion_table() | |
| with Path("dscp_tos_conv_table.csv").open(mode="w", newline="") as fout: | |
| dict_writer = csv.DictWriter(f=fout, fieldnames=conversion_table[0].keys()) | |
| dict_writer.writeheader() | |
| dict_writer.writerows(conversion_table) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment