def get_address_territory(address_name):
 territory = None
 if address_name:
  address_fields = frappe.db.get_value(u'Address', address_name, [u'city', u'state', u'country'])
  for value in address_fields:
   territory = frappe.db.get_value(u'Territory', value)
   if territory:
    break
 return territory

def get_address_territory ( address_name ) : territory = None if address_name : address_fields = frappe . db . get_value ( u'Address' , address_name , [ u'city' , u'state' , u'country' ] ) for value in address_fields : territory = frappe . db . get_value ( u'Territory' , value ) if territory : break return territory
tries to match city .