def get_common_checksums_sparse(common_dict, message, min_length, max_length, sparse_value, character_list, max_length_only): if len(message) > max_length: return False #sparse_characters_to_use = sorted(sample(character_list,sparse_value)) sparse_characters_to_use = sample(character_list,sparse_value) #print sparse_characters_to_use for n in sparse_characters_to_use: new_message = message + str(unichr(n)) try: if len(new_message) > 1: formatted_message = str(Command(new_message[0],new_message[1:])) else: formatted_message = str(Command(new_message)) except CommandException as e: continue checksum_char = formatted_message[-1] #common_dict.setdefault(checksum_char,[]).append(formatted_message) if len(message) >= min_length: if max_length_only: if len(message) == max_length: common_dict.setdefault(checksum_char,0) common_dict[checksum_char] += 1 else: common_dict.setdefault(checksum_char,0) common_dict[checksum_char] += 1 if not get_common_checksums_sparse(common_dict, new_message, min_length, max_length, sparse_value, character_list, max_length_only): break return True
def test_command_formatted_string(): command_string = "testcommand" value_string = "testvalue" proper_beginning = command_string + "|" + value_string command = Command(command_string, [value_string]) assert command.get_formatted_string().startswith(proper_beginning) assert command.get_formatted_string()[-2] == "*" assert command.get_formatted_string()[-1] == Command.get_checksum( proper_beginning)
def test_command_fullstring_with_values(): command_string = "testcommand" value_string = "testvalue" full_string = command_string + "|" + value_string checksum = Command.get_checksum(full_string) full_string += "*{}".format(checksum) command = Command.create_command(full_string) assert command.get_command() == command_string assert command.get_values() == [value_string]
def generate_random_strings_with_checksum(repeats): for n in range(0,repeats): commandString = "" for n in range(randint(1,11)): commandString += str(unichr(randint(33,126))) try: newcommand = Command(commandString) except CommandException as e: print("{} --> {}".format(str(e),commandString)) else: print("------") print(newcommand) print(ord(str(newcommand)[-1])) print("------")
def get_common_checksums(common_dict, message, min_length, max_length, character_list, max_length_only): if len(message) > max_length: return False for n in range(33,127): new_message = message + str(unichr(n)) try: formatted_message = str(Command(new_message)) except CommandException as e: continue checksum_char = formatted_message[-1] #common_dict.setdefault(checksum_char,[]).append(formatted_message) if len(message) >= min_length: if max_length_only: if len(message) == max_length: common_dict.setdefault(checksum_char,0) common_dict[checksum_char] += 1 else: common_dict.setdefault(checksum_char,0) common_dict[checksum_char] += 1 if not get_common_checksums(common_dict, new_message, min_length, max_length, character_list, max_length_only): break return True
def test_command_list_value(): command_string = "testcommand" value_string = "testvalue" command = Command(command_string, [value_string]) assert command.get_command() == command_string assert command.get_values() == [value_string]
def test_command_only_command_given(): command_string = "testcommand" command = Command(command_string) assert command.get_command() == command_string
def test_command_fullstring_invalid_characters_error(): with raises(CommandException): bad_command = "bad|command*mydude" bad_string = "{}*{}".format(bad_command, Command.get_checksum(bad_command)) command = Command.create_command(bad_string)
def test_command_fullstring_wrong_checksum_error(): with raises(CommandException): command = Command.create_command("a*a")
def test_command_fullstring_empty_command_error(): with raises(CommandException): command = Command.create_command("")
def test_command_fullstring_with_only_command(): command_string = "testcommand" checksum = Command.get_checksum(command_string) full_string = "{}*{}".format(command_string, checksum) command = Command.create_command(full_string) assert command.get_command() == command_string
max_val = float(max(dict1.values())) for key,value in sorted(dict1.iteritems()): print("{}: {:.2f} --> {}".format(key,value/max_val,value)) def checksum_distribution(): old_dict = {} min_length = 1 max_length = 6 # depth of tree (where each node + leaf is a possible string) max_length_only=False min_char = 97#33 max_char = 122#126 sparse_value = 2 # branching factor (don't exceed max_char-min_char); affects time exponentially repetitions = 10000 # repeats; affects time linearly for n in range(repetitions): new_dict = get_common_checksums_sparse_wrapper(min_length, max_length,sparse_value,min_char,max_char,max_length_only) combine_dicts(old_dict,new_dict) print_dict_normalized(old_dict) print("-----") if __name__=="__main__": #print Command("gb") print(Command("ready")) #checksum_distribution() #other_dict = get_common_checksums_wrapper(3) #print_dict(other_dict)