本文整理汇总了Python中API.has_key方法的典型用法代码示例。如果您正苦于以下问题:Python API.has_key方法的具体用法?Python API.has_key怎么用?Python API.has_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类API
的用法示例。
在下文中一共展示了API.has_key方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import API [as 别名]
# 或者: from API import has_key [as 别名]
class TStat:
def __init__(self, address, cacheExpiry=5, api=None, logger=None, logLevel=None):
self.address = address
self.setCacheExpiry(cacheExpiry)
self.cache = {}
if logger is None:
if logLevel is None:
logLevel = logging.WARNING
logging.basicConfig(level=logLevel)
self.logger = logging.getLogger('TStat')
else:
self.logger = logger
if api is None:
self.api = API()
self.api = getAPI(self.getModel())
time.sleep(2)
else:
self.api = api
def setCacheExpiry(self, newExpiry):
self.cacheExpiry = datetime.timedelta(seconds=newExpiry)
def _getConn(self):
"""Used internally to get a connection to the tstat."""
return httplib.HTTPConnection(self.address)
def _post(self, key, value):
"""Used internally to modify tstat settings (e.g. cloud mode)."""
l = self.logger
# Check for valid request
if not self.api.has_key(key):
l.error("%s does not exist in API" % key)
return False
# Retrieve the mapping from api key to thermostat URL
entry = self.api[key]
l.debug("Got API entry: %s" % entry)
try:
if len(entry.setters) < 1:
raise TypeError
except TypeError:
l.error("%s cannot be set (maybe readonly?)" % key)
return False
# Check for valid values
if entry.valueMap is not None:
inverse = dict((v,k) for k, v in entry.valueMap.iteritems())
if not inverse.has_key(value) and not entry.valueMap.has_key(value):
l.warning("Value '%s' may not be a valid value for '%s'" % (value, key))
elif inverse.has_key(value):
value = inverse[value]
for setter in entry.setters:
location = setter[0]
jsonKey = setter[1]
if entry.usesJson:
params = dumps({jsonKey: value})
else:
params = urllib.urlencode({jsonKey: value})
l.debug("Will send params: %s" % params)
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
response = None
count = 0
while response is None and count < 5:
try:
conn = self._getConn()
conn.request("POST", location, params, headers)
response = conn.getresponse()
except socket.error:
response = None
if response is None:
time.sleep(count*random.randint(0, 3))
count = count + 1
if response.status != 200:
l.error("Error %s while trying to set '%s' with '%s'" % (response.status, location, params))
continue
data = response.read()
response.close()
success = False
for s in self.api.successStrings:
if data.startswith(s):
success = True
break
if not success:
l.error("Error trying to set '%s' with '%s': %s" % (location, params, data))
l.debug("Response: %s" % data)
return True
def _get(self, key, raw=False):
"""Used internally to retrieve data from the tstat and process it with JSON if necessary."""
l = self.logger
l.debug("Requested: %s" % key)
#.........这里部分代码省略.........