Refactored to use lambda expressions

This commit is contained in:
Anon 2022-10-28 20:25:31 -07:00
parent 408d766d6d
commit 42ee67a107

View File

@ -21,9 +21,6 @@
# yandereBot, or manipulate hash files in some way. Typically instantiating a yandereBot object is unnecessary for this. # yandereBot, or manipulate hash files in some way. Typically instantiating a yandereBot object is unnecessary for this.
import os import os
import datetime
import shutil
import contextlib
# Requires properly formatted MD5 checksum lines. # Requires properly formatted MD5 checksum lines.
@ -75,33 +72,34 @@ class HashObject:
# Checking for the binary character when comparing hash string is usually overkill since the CoreUtils package reads # Checking for the binary character when comparing hash string is usually overkill since the CoreUtils package reads
# files in binary mode regardless of whether the -b switch is passed. # files in binary mode regardless of whether the -b switch is passed.
# It is included here since it may be useful for some other applications besides the bot. # It is included here since it may be useful for some other applications besides the bot.
def is_matching_atter_binary(v_list, v_hash, v_atter): def is_matching_atter_binary(v_list, v_hash, callback):
atter_bin = "get_binary_char"
return next( return next(
(x for x in v_list if (x for x in v_list if
getattr(v_hash, v_atter)() == getattr(x, v_atter)() and callback(v_hash) == callback(x) and
getattr(v_hash, atter_bin)() == getattr(x, atter_bin)() v_hash.get_binary_char() == x.get_binary_char()
), None) ), None)
def is_matching_atter(v_list, v_hash, v_atter): def is_matching_atter(v_list, v_hash, callback):
return next((x for x in v_list if getattr(v_hash, v_atter)() == getattr(x, v_atter)()), None) return next(
(x for x in v_list if
callback(v_hash) == callback(x)
), None)
def get_matching(v_list, v_hash, callback, match_bin=False):
def get_matching(v_list, v_hash, atter, match_bin=False): return is_matching_atter(v_list, v_hash, callback) if not match_bin else is_matching_atter_binary(v_list, v_hash, callback)
return is_matching_atter(v_list, v_hash, atter) if not match_bin else is_matching_atter_binary(v_list, v_hash, atter)
def get_matching_hash_in_list(v_list, v_hash, match_bin=False): def get_matching_hash_in_list(v_list, v_hash, match_bin=False):
return get_matching(v_list, v_hash, "get_hash_string", match_bin) return get_matching(v_list, v_hash, lambda x: x.get_hash_string(), match_bin)
def get_matching_path_in_list(v_list, v_hash, match_bin=False): def get_matching_path_in_list(v_list, v_hash, match_bin=False):
return get_matching(v_list, v_hash, "get_hash_path", match_bin) return get_matching(v_list, v_hash, lambda x: x.get_hash_path(), match_bin)
def get_matching_full_string_in_list(v_list, v_hash, match_bin=False): def get_matching_full_string_in_list(v_list, v_hash, match_bin=False):
return get_matching(v_list, v_hash, "get_full_string", match_bin) return get_matching(v_list, v_hash, lambda x: x.get_full_string(), match_bin)
# ---------------------------- HASH LIST FUNCTIONS -------------------------------------------- # ---------------------------- HASH LIST FUNCTIONS --------------------------------------------