I need a flexible way to mask the material, so I decided to share ...
def mask(string, start=0, end=0, chunk=4, char="*"): if isinstance(string, str): string_len = len(string) if string_len < 3: return "input len = {}, what the point?".format(string_len) if start <= 0 or end <= 0: start = string_len // chunk end = start if string_len <= 4: mask_len = string_len // 2 start = 1 if mask_len - 1 == 0 else mask_len - 1 end = start else: mask_len = string_len - abs(start) - abs(end) return string[:abs(start)] + (char * mask_len) + string[-end:] if __name__ == "__main__": s = "abcdefghijklmnopqrstuvwxyz01234567890a0b0c0d0e0f1a1b1c1d1e1f2a2b" l = [i for i in s] message = "ip: {}\nop: {}\n" section = "-"*79 print("\nSTRINGS") print(section) print(message.format(s[:4], mask(s[:4]))) print(message.format(s[:3], mask(s[:3]))) print(message.format(s[:2], mask(s[:2]))) print(message.format(s, mask(s))) print(message.format(s, mask(s,start=2,end=4,char="#"))) print(message.format(s, mask(s,start=3,end=3,char="x")))
source share