Let us say I have a python method to get last synced changelist in p4. (The question itself is not related to p4 but only a basic python question)
def get_last_synced_changelist(p4port, client_name, p4 = None):
with P4Connection(p4port) as p4:
last_change_list = p4.run_changes("-m1", "@%s" % client_name)
if last_change_list:
return last_change_list[0]["change"]
else:
return None
The caller of this method can either supply p4port which is a String or they could supply the p4 object in itself. My requirement is that if a "p4" object is supplied I want to run the method body without the with context, i.e, I dont want the enter or exit method called on the p4. This is because the responsibility of closing/entering p4 object lies now with the caller. If p4 object is not supplied the with syntax needs to be constructed in this program using the p4port string.
Can someone tell me what is the best way of structuring this method? I want to change the body as less as possible. Basically something like
p4 or with P4Connection(p4port) as p4:
p4.run_changes(...)
But I am not sure what the best syntax is.