code: def countries(db, code=None, term=None):
      def _is_code_valid(code):
          return type(code) == str \
              and len(code) == 2 \
              and code.isalpha()
      if code is not None and not _is_code_valid(code):
          raise InvalidValueError('country code must be a 2 length alpha string - %s given'
                                  % str(code))
      cs = []
      with db.connect() as session:
          query = session.query(Country)
          if code or term:
              if code:
                  query = query.filter(Country.code == code.upper())
              elif term:
                  query = query.filter(Country.name.like('%' + term + '%'))
              cs = query.order_by(Country.code).all()
              if not cs:
                  e = code if code else term
                  raise NotFoundError(entity=e)
          else:
              cs = session.query(Country).\
                  order_by(Country.code).all()
          session.expunge_all()
      return cs