AttributeError:'function'对象没有属性'get'

时间:2013-12-16 11:17:19

标签: python python-3.x pythomnic3k

下午好,

  1. 我正在使用pythomnic3k python框架和分片solr,redis数据库服务器。

    以下是我的api请求:

    http://1.2.3.4:8090/api20/account2/addnewemail?Email=foo@yahoo.com&Token=[redacted]&AccountID=[redacted]

    错误:AttributeError: 'function' object has no attribute 'get'

  2. 出现此错误的函数:

    def count_maps(r):
        dt = r.get(Namespace.MAPPINGS + ':count')
    

    r是redis实例,Namespance.Mappings + ':count' = ytk:map:count

  3. solr schema xml文件包含以下分片实例:

    <?xml version="1.0" encoding="UTF-8"?>
        <mapping>
            <meta>
                <date>201-12-13 00:00:00</date>
                <active>true</active>
                <static url="0.0.0.0:8983/solr" />
            </meta>
            <map>
                <shard url="0.0.0.0:8983/solr" start="00000000" end="00009999" readonly="false">
                    <slave url="0.0.0.0:8983/solr" />
                </shard>
            </map>
        </mapping>
    
    1. Redis实例r位于redis_utils.py

      def test_redis_obj(r):
      try:
          r.ping()
          return True
      except ConnectionError: 
          return False
      
      def redis_obj(redis_def, timeout = None):
      if not redis_def:
          return None
              return redis.StrictRedis(
                  host = redis_def["ip"],
                  port = redis_def["port"],
                  db = redis_def["db"],
                  socket_timeout = timeout
              )
      
      def random_redis_obj(timeout = None):
          return redis_obj(random_redis_def(), timeout)
      
      def random_tested_redis_obj(attempts = 3, timeout = 1):
          for _ in range(attempts):
              r = random_redis_obj(timeout)
              if r and test_redis_obj(r):
                  return r
      raise YtkRedisError("active redis server not found (%d attempts done)" % attempts)
      
    2. 这是在solr_manager.py中调用count_maps(r)的函数:

      def get_unique_shards(r, the_map: int = -1):
          '''
          Returns array: [map][shard](0: url; 1: [slaves urls])
          with unique shards with their slaves in mappings
          '''
      num = count_maps(r)
      if the_map >= num:
          raise IndexError("There aren't map at index " + str(the_map))
      if the_map < 0:
          return _get_all_unique_shards(r, num)
      else:
          return _get_unique_shards(r, the_map)
      
      def count_maps(r):
          dt = r.get(Namespace.MAPPING + ':count')
          if dt is not None:
              return int(dt)
          return None
      
      • 我理解这个redis对象尝试计算solr实例,是不是?

      • 请帮助我解释为什么我的redis对象为None?我检查了我的日志“get attribute ..”错误并且无法调试它。

1 个答案:

答案 0 :(得分:0)

当我收到您的问题时,r是与redis的连接,如果是,您可以在py文件的开头定义,如:

import redis

r = redis.ConnectionPool(host='localhost', port=6379, db=0)

您可以使用r作为:

def count_maps():
    dt = r.get(Namespace.MAPPING + ':count')
    if dt is not None:
        return int(dt)
    return None
相关问题