How to optimize the code to save data at GAE datastore? I have the following code (for Country and City classes key_name is numeric id with addition of 'i' at the beginning):

def add_country(country, country_name):
  if country and country_namef and country_name != '':
    return Country.get_or_insert('i'+str(country), country_name=country_name)
  else:
    return None  

def add_city(city, city_name, country):
  if country and city and city_name and city_name != '':
    return City.get_or_insert('i'+str(city), city_name=city_name, parent=country)
  else:
    return None


Is it correct code or can it be optimized somehow?
def add_country(country, country_name):
  if country and country_namef and country_name != '':


I assume that country_namef is a typo. If so, there is no point in checking country_name != '' because empty strings are already considered False

    return Country.get_or_insert('i'+str(country), country_name=country_name)
  else:
    return None  

def add_city(city, city_name, country):
  if country and city and city_name and city_name != '':


As before, empty strings are false, so you are checking that twice

    return City.get_or_insert('i'+str(city), city_name=city_name, parent=country)
  else:
    return None