def get_territory_from_address(address):
 if (not address):
  return
 if isinstance(address, basestring):
  address = frappe.get_doc(u'Address', address)
 territory = None
 for fieldname in (u'city', u'state', u'country'):
  territory = frappe.db.get_value(u'Territory', address.get(fieldname))
  if territory:
   break
 return territory

def get_territory_from_address ( address ) : if ( not address ) : return if isinstance ( address , basestring ) : address = frappe . get_doc ( u'Address' , address ) territory = None for fieldname in ( u'city' , u'state' , u'country' ) : territory = frappe . db . get_value ( u'Territory' , address . get ( fieldname ) ) if territory : break return territory
tries to match city .