the message to send back to the client.""" return_value = exists, value = handle_get(key) if not exists: return return_value elif not isinstance(list_value, list): return (False, 'ERROR: Key [{}] contains non-list value ([{}])'.format( key, value)) else: DATA[key].append(value) return (True, 'Key [{}] had value [{}] appended'.format(key, value)) def handle_delete(key): """Return a tuple containing True if the key could be deleted and the message to send back to the client.""" if key not in DATA: return ( False, 'ERROR: Key [{}] not found and could not be deleted.'.format(key)) else: del DATA[key] def handle_stats(): """Return a tuple containing True and the contents of the STATS dict.""" return (True, str(STATS)) 有两点须要留意: 多重赋值 (multiple assignment) 和代码重用. 有些函数仅仅是为了加倍有逻辑性而对已有函数的简单包装罢了, 比如 handle_get 和 handle_getlist . 因为我们有时仅仅是须要一个已有函数的返回值,而其他时刻却须要检查该函数到底返回了什么内容, 这时刻就会应用 多重赋值 。
来看一下 handle_append . 如不雅我们测验测验调用 handle_get 然则 key 并不存在时, 那么我们简单地返回 handle_get 所返回的内容。 此外, 我们还欲望可以或许将 handle_get 返回的 tuple 作为一个零丁的返回值进行引用。 那么当 key 不存在的时刻, 我们就可以简单地应用 return return_value .
推荐阅读
交通银行信息技术管理部副总经理张漫丽:交通银行“大数据+人工智能”应用研究
大年夜数据隐含着巨大年夜的社会、经济、科研价值,已引起了各行各业的高度看重。如不雅能经由过程人工智能技巧有效地组织和应用大年夜数据,将对社会经济和科学研究成长产生巨大年夜的推>>>详细阅读
本文标题:用Python写一个NoSQL数据库
地址:http://www.17bianji.com/lsqh/35303.html
1/2 1