我刚刚看到了雷蒙德·赫廷格的谈话如何使Pythonic更简洁,并意识到我应该将他的许多想法付诸实践,特别是将API包装在一个使一切变得更简单和易于使用的类中。下面是我包装PyMongo所做的工作:
from pymongo import MongoClient
class MongoDB(object):
"""Provides a RAII wrapper for PyMongo db connections.
Available collection functions limited to those in
attributes_to_pass. Number of simultaneous connection
users also tracked. """
attributes_to_pass = ["update", "insert", "count"]
client = None
num_users = 0
def __init__(self, db, collection):
MongoDB.client = MongoDB.client or MongoClient()
self.collection = MongoDB.client[db][collection]
MongoDB.num_users += 1
def __enter__(self, *args):
return self
def __exit__(self, type, value, traceback):
MongoDB.num_users -= 1
if MongoDB.num_users is 0:
self.client.close()
def __getattr__(self, attr):
if attr in MongoDB.attributes_to_pass:
return getattr(self.collection, attr)
else:
return getattr(self, attr)
def main():
with MongoDB(db = "db1", collection = "c1") as m:
print(m.count())
m.update({"jello":5} , {"hello":"you"}, upsert = True)
print(m.count())
m.insert({"joe":6})
with MongoDB(db ='db1', collection = 'c2') as j:
j.insert({"joe":6})
print(j.count())
if __name__ == "__main__":
main()我非常感谢所有关于如何使这件事变得更好的建议。
发布于 2015-10-08 11:23:32
太好了,你提供了一个文档字符串!但是,您犯了一个小格式错误,在简短的单行摘要和其余的docstring之间应该有一个空行。在样式指南中推荐它部分用于可读性,部分用于脚本解析器。
class MongoDB(object):
"""Provides a RAII wrapper for PyMongo db connections.
Available collection functions limited to those in
attributes_to_pass. Number of simultaneous connection
users also tracked. """看起来attributes_to_pass实际上是一个常数。如果是这样的话,UPPER_SNAKE_CASE就应该清楚地知道它是这样的,特别是当它与经常变化的属性(如client和num_users )混合时。它也应该是一个元组,因为元组是不可变的,所以除非在源代码中更新,否则不会改变。
ATTRIBUTES_TO_PASS = ("update", "insert", "count")此外,当您调用这些常量时,您将引用类名。虽然这是可能的,但我个人认为它的可读性较低。我还以为你是在召唤一个你刚导入的类呢。您可以只传递self而不是类名。
def __getattr__(self, attr):
if attr in self.ATTRIBUTES_TO_PASS:
return getattr(self.collection, attr)
else:
return getattr(self, attr)https://codereview.stackexchange.com/questions/106868
复制相似问题