python - Is it possible to reduce the amount of arguments this on the boilerplate function? -


i'm trying create automagic django model instantiator. function take couple of arguments, instantiate few django models, link foreign keys together, save lot , return main model me. fine , in end, function takes 4 normal arguments , 2 tuples (filled contenttype name , array of arguments different function call).

this leads following function def:

def create_rule(profile, lifestyles, bool, title, input, output):     rule = rule.objects.create(         user_profile=profile,          lifestyles=lifestyles,         bool=bool,         title=title,     )      models = contenttype.objects.filter(app_label="rules")     input_cls = models.get(model=input[0]).model_class()     input_cls.objects.create(*input[1], rule=rule)      output_cls = models.get(model=output[0]).model_class()     output_cls.objects.create(*output[1], rule=rule)      return rule 

an average function call might little this:

create_rule(profile, '1,3,6,7', 1, "switch off when 5:00",      ('eventruleinput', [5, 'start']),      ('propruleoutput', [35, 'switch', 0])) 

is there nice way reduce amount of arguments i'm using (not guess)? there better way of structuring function call? doing wrong way?

note: mockup implementation, im kinda wondering wether want approach problem this.

sometimes, function's gotta take lot of arguments. there nothing wrong this, per se. here tips on making little cleaner:

don't name bool, "bool":

def create_rule(profile, lifestyles, is_something, title, input, output):     rule = rule.objects.create(     ....     bool=is_something, ) 

unpack tuples clarity:

input_val, input_command = input input_cls = models.get(model=input_val).model_class() input_cls.objects.create(*input_command, rule=rule) output_val, output_command, output_third_arg = output .... 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -