Skip to content

Commit fdbc753

Browse files
author
Kazuki Suzuki Przyborowski
committed
Update pyarchivefile.py
1 parent 62cba4a commit fdbc753

1 file changed

Lines changed: 54 additions & 8 deletions

File tree

pyarchivefile.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -732,23 +732,69 @@ def GetTotalSize(file_list):
732732
return total_size
733733

734734

735-
def create_alias_function_alt(prefix, base_name, suffix, target_function):
735+
def create_alias_function_alt(prefix, base_name, suffix, target_function, positional_overrides=None):
736+
"""
737+
Creates a new function in the global namespace that wraps 'target_function',
738+
allowing optional overrides of specific positional arguments via 'positional_overrides'.
739+
740+
:param prefix: String prefix for the new function's name
741+
:param base_name: Base string to use in the new function's name
742+
:param suffix: String suffix for the new function's name
743+
:param target_function: The function to be wrapped/aliased
744+
:param positional_overrides: Optional dict {index: new_value} for overriding specific positional arguments
745+
"""
736746
# Define a new function that wraps the target function
737747
def alias_function(*args, **kwargs):
738-
return target_function(*args, **kwargs)
748+
# Convert args to a list so we can modify specific positions
749+
args_list = list(args)
750+
751+
# If there are positional overrides, apply them
752+
if positional_overrides:
753+
for index, value in positional_overrides.items():
754+
# Only apply if the index is within the bounds of the original arguments
755+
if 0 <= index < len(args_list):
756+
args_list[index] = value
757+
758+
# Call the target function with possibly modified arguments
759+
return target_function(*args_list, **kwargs)
760+
739761
# Create the function name by combining the prefix, base name, and the suffix
740762
function_name = "{}{}{}".format(prefix, base_name, suffix)
763+
741764
# Add the new function to the global namespace
742765
globals()[function_name] = alias_function
743766

744767

745-
def create_alias_function(prefix, base_name, suffix, target_function):
746-
# Create the function name by combining the prefix, base name, and the suffix
747-
# Use the format method for string formatting, compatible with Python 2 and 3
768+
def create_alias_function(prefix, base_name, suffix, target_function, positional_overrides=None):
769+
"""
770+
Creates a new function in the global namespace that wraps 'target_function',
771+
allowing optional overrides of specific positional arguments via 'positional_overrides'.
772+
773+
:param prefix: String prefix for the new function's name
774+
:param base_name: Base string to use in the new function's name
775+
:param suffix: String suffix for the new function's name
776+
:param target_function: The function to be wrapped/aliased
777+
:param positional_overrides: Optional dict {index: new_value} for overriding specific positional arguments
778+
"""
779+
# Define a new function that wraps the target function
780+
def alias_function(*args, **kwargs):
781+
# Convert args to a list so we can modify specific positions
782+
args_list = list(args)
783+
784+
# If there are positional overrides, apply them
785+
if positional_overrides:
786+
for index, value in positional_overrides.items():
787+
if 0 <= index < len(args_list):
788+
args_list[index] = value
789+
790+
# Call the target function with possibly modified arguments
791+
return target_function(*args_list, **kwargs)
792+
793+
# Create the function name by combining the prefix, base_name, and suffix
748794
function_name = "{}{}{}".format(prefix, base_name, suffix)
749-
# Add the new function (alias of the target_function) to the global namespace
750-
# This line is compatible as-is with both Python 2 and 3
751-
globals()[function_name] = target_function
795+
796+
# Add the new function to the global namespace
797+
globals()[function_name] = alias_function
752798

753799

754800
class ZlibFile:

0 commit comments

Comments
 (0)