A Google of "Perl to Python" will yield some of the programs that will do this for you. If the translation is incomplete, post the offending code, with an explanation of what it should do, and someone will help. Note that this code appears to call other functions, so just translating this portion will probably not work.
# Very Loose translation, beware of the globals() and NEVER explicitly translate Perl to Python. See why below.
# Assumes that legs, the var referenced by legs, reference, ref, fields, fxall, merge_swap_legs, new_fk_ref, and logg exist
if legs in globals():
legs = globals()[legs]
legs = locals()[legs]
if reference < len(legs): # if reference in legs if legs is a dictionary (if is a hash)
logg("2nd leg of swapes: %s - merging"%ref)
swap = merge_swap_legs(legs[ref], fields)
ref = new_fk_ref(fxall,ref,swap)
legs[ref] = fields
logg("1st leg of swap: %s"%ref)
legs is a string
The variable referenced by legs is a list/dictionary
reference is an integer/string (if ^ is list/dictionary)
ref is of same type as reference
fields can be any object
fxall is arbitrary
Everything else are functions
This however is a horrible coding style, post the complete code and I'll see if it can be easily built in python.