nodes: 0, module; 1, function_definition; 2, function_name:countries; 3, parameters; 4, block; 5, identifier:db; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, function_definition; 10, if_statement; 11, expression_statement; 12, with_statement; 13, return_statement; 14, identifier:code; 15, None; 16, identifier:term; 17, None; 18, comment:"""List the countries available in the registry.
  
      The function will return the list of countries. When either 'code' or
      'term' parameters are set, it will only return the information about
      those countries that match them.
  
      Take into account that 'code' is a country identifier composed by two
      letters (i.e ES or US). A 'InvalidValueError' exception will be raised when
      this identifier is not valid. If this value is valid, 'term' parameter
      will be ignored.
  
      When the given values do not match with any country from the registry
      a 'NotFounError' exception will be raised.
  
      :param db: database manager
      :param code: country identifier composed by two letters
      :param term: term to match with countries names
  
      :returns: a list of countries sorted by their country id
  
      :raises InvalidValueError: raised when 'code' is not a string composed by
          two letters
      :raises NotFoundError: raised when the given 'code' or 'term' is not
          found for any country from the registry
      """; 19, function_name:_is_code_valid; 20, parameters; 21, block; 22, boolean_operator; 23, block; 24, assignment; 25, with_clause; 26, block; 27, identifier:cs; 28, identifier:code; 29, return_statement; 30, comparison_operator:code is not None; 31, not_operator; 32, raise_statement; 33, identifier:cs; 34, list; 35, with_item; 36, expression_statement; 37, if_statement; 38, comment:# Detach objects from the session; 39, expression_statement; 40, boolean_operator; 41, identifier:code; 42, None; 43, call; 44, call; 45, as_pattern; 46, assignment; 47, boolean_operator; 48, block; 49, else_clause; 50, call; 51, boolean_operator; 52, line_continuation:\; 53, call; 54, identifier:_is_code_valid; 55, argument_list; 56, identifier:InvalidValueError; 57, argument_list; 58, call; 59, as_pattern_target; 60, identifier:query; 61, call; 62, identifier:code; 63, identifier:term; 64, if_statement; 65, expression_statement; 66, if_statement; 67, block; 68, attribute; 69, argument_list; 70, comparison_operator:type(code) == str; 71, line_continuation:\; 72, comparison_operator:len(code) == 2; 73, attribute; 74, argument_list; 75, identifier:code; 76, binary_operator:'country code must be a 2 length alpha string - %s given'
                                  % str(code); 77, attribute; 78, argument_list; 79, identifier:session; 80, attribute; 81, argument_list; 82, identifier:code; 83, block; 84, elif_clause; 85, assignment; 86, not_operator; 87, block; 88, expression_statement; 89, identifier:session; 90, identifier:expunge_all; 91, call; 92, identifier:str; 93, call; 94, integer:2; 95, identifier:code; 96, identifier:isalpha; 97, string; 98, call; 99, identifier:db; 100, identifier:connect; 101, identifier:session; 102, identifier:query; 103, identifier:Country; 104, expression_statement; 105, identifier:term; 106, block; 107, identifier:cs; 108, call; 109, identifier:cs; 110, expression_statement; 111, raise_statement; 112, assignment; 113, identifier:type; 114, argument_list; 115, identifier:len; 116, argument_list; 117, string_content:country code must be a 2 length alpha string - %s given; 118, identifier:str; 119, argument_list; 120, assignment; 121, expression_statement; 122, attribute; 123, argument_list; 124, assignment; 125, call; 126, identifier:cs; 127, call; 128, identifier:code; 129, identifier:code; 130, identifier:code; 131, identifier:query; 132, call; 133, assignment; 134, call; 135, identifier:all; 136, identifier:e; 137, conditional_expression:code if code else term; 138, identifier:NotFoundError; 139, argument_list; 140, attribute; 141, argument_list; 142, attribute; 143, argument_list; 144, identifier:query; 145, call; 146, attribute; 147, argument_list; 148, identifier:code; 149, identifier:code; 150, identifier:term; 151, keyword_argument; 152, call; 153, identifier:all; 154, identifier:query; 155, identifier:filter; 156, comparison_operator:Country.code == code.upper(); 157, attribute; 158, argument_list; 159, identifier:query; 160, identifier:order_by; 161, attribute; 162, identifier:entity; 163, identifier:e; 164, attribute; 165, argument_list; 166, attribute; 167, call; 168, identifier:query; 169, identifier:filter; 170, call; 171, identifier:Country; 172, identifier:code; 173, call; 174, line_continuation:\; 175, identifier:order_by; 176, attribute; 177, identifier:Country; 178, identifier:code; 179, attribute; 180, argument_list; 181, attribute; 182, argument_list; 183, attribute; 184, argument_list; 185, identifier:Country; 186, identifier:code; 187, identifier:code; 188, identifier:upper; 189, attribute; 190, identifier:like; 191, binary_operator:'%' + term + '%'; 192, identifier:session; 193, identifier:query; 194, identifier:Country; 195, identifier:Country; 196, identifier:name; 197, binary_operator:'%' + term; 198, string; 199, string; 200, identifier:term; 201, string_content:%; 202, string_content:%
code: def countries(db, code=None, term=None):
      """List the countries available in the registry.
  
      The function will return the list of countries. When either 'code' or
      'term' parameters are set, it will only return the information about
      those countries that match them.
  
      Take into account that 'code' is a country identifier composed by two
      letters (i.e ES or US). A 'InvalidValueError' exception will be raised when
      this identifier is not valid. If this value is valid, 'term' parameter
      will be ignored.
  
      When the given values do not match with any country from the registry
      a 'NotFounError' exception will be raised.
  
      :param db: database manager
      :param code: country identifier composed by two letters
      :param term: term to match with countries names
  
      :returns: a list of countries sorted by their country id
  
      :raises InvalidValueError: raised when 'code' is not a string composed by
          two letters
      :raises NotFoundError: raised when the given 'code' or 'term' is not
          found for any country from the registry
      """
  
      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()
  
          # Detach objects from the session
          session.expunge_all()
  
      return cs