Cleaner way to represent Rules (if-else) in Python -
i trying find design pattern (or maybe algorithm) me write these rules in cleaner way. suggestions?
def get_rules(user, value): if 500 <= value < 5000 , not user.address: return [request_address] if value >= 5000: if not user.address , not user.phone: return [request_address, request_phone] if user.address , not user.phone: return [request_phone] if not user.address , user.phone: return [request_address] # potentially ~20 more conditions here based on various attributes of user return [states.request_none]
note: not looking rules engine since don't want complicate code adding "business friendly" dsl in python. python simple language write these rules.
interesting read: http://martinfowler.com/bliki/rulesengine.html (but still trying stay away "framework" me).
you're checking lots of different combinations "if , not b else check not , b else check not , not b" strategy figure out combination of requests need send.
instead, check you're missing:
missing = [] if not user.phone: missing.append(request_phone) if not user.address: missing.append(request_address) return missing or [request_none]
Comments
Post a Comment