如何开始围绕另一个包装器编写一个Python包装器?
我知道这个问题已经被一两个人问过,但它与我的想法有些不同。请耐心听我说。
我正在尝试围绕现有的 couchdb-python 包装器编写一个包装器。这样做的原因是,如果我们决定将来切换到另一个 NoSQL 数据库,例如 MongoDB,我只需更改代码库的一部分。
问题 1:鉴于上述情况,这是建议的正确行动方案吗?
使用 couchdb-python 包装器访问 couchdb 的问题是,有时(并非总是)需要用户名/密码才能访问数据库。这就是代码中的样子:
server = Server() <---- When no username/password is required
server = Server('http://abc:123@localhost:5984') <---- When username/password is required
我如何定义初始 init 函数来满足这两种情况。我现在有这样的事情:
from couchdb import Server
class Couch(object):
""" CouchDB Wrapper """
COUCHDB_URI = {username_not_provided: 'http://localhost:5984',
username_provided: 'http://%s:%s@localhost:5984'}
def __init__(self, username=None, password=None):
if username and password:
self.url = COUCHDB_URI['username_provided'] % (username, password)
else:
self.url = COUCHDB_URI['username_not_provided']
self.server = Server(self.url)
我认为代码看起来很丑,有人可以为我指出上述正确的方向吗?
谢谢大家!我真的很感谢你的帮助。
I know this question has been asked by one or two people but it's somehow different from what I have in mind. Please bear with me.
I'm trying to write a wrapper around the existing couchdb-python wrapper. The reason for this is that in the event that we decide to switch to another NoSQL database in the future, for example MongoDB, I only have to change one part of my codebase.
Question 1: Is this the suggested right course of action given the above scenario?
The problem with accessing couchdb using the couchdb-python wrapper, sometimes (not all the time), a username/password is required to access the database. This is what it looks like in code:
server = Server() <---- When no username/password is required
server = Server('http://abc:123@localhost:5984') <---- When username/password is required
How would I define the inital init function to cater for both scenarios. I have somehting like this at the moment:
from couchdb import Server
class Couch(object):
""" CouchDB Wrapper """
COUCHDB_URI = {username_not_provided: 'http://localhost:5984',
username_provided: 'http://%s:%s@localhost:5984'}
def __init__(self, username=None, password=None):
if username and password:
self.url = COUCHDB_URI['username_provided'] % (username, password)
else:
self.url = COUCHDB_URI['username_not_provided']
self.server = Server(self.url)
I think the code looks ugly, can someone point me in the right direction for the above?
Thank you everyone! I really appreciate the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是基于拉斯曼对您的特定格式问题的回答,但我认为您可能想要以与此不同的方式进行处理。最好首先创建一个为任何 NoSQL 后端定义通用接口的基类。像这样:
数据库类将定义子类应该实现的所有接口方法。这样,您的更高级别的代码就可以假设一个通用的数据库对象,并且您只需要为不同的 NoSQL 后端定义新的模块。
编辑
你应该看看nonrel django:http://www. allbuttonspressed.com/projects/django-nonrel
他们正在为 django 完成同样的任务,使用 mongodb 作为他们的主要选择,但是您可以通过实现他们的一些基础来添加更多支持CouchDB 的类。这应该能让您很好地了解他们如何实现它,因为 django 是相同的想法,在可交换数据库后端上使用不可知的 ORM 层。
This is building off of larsmans answer to your specific formatting question, but I think you might want to go about it a different way than this. It might be better to first create a Base class that defines a common interface for any of your NoSQL backends. Something like this:
Database class would define all the interface methods that subclasses should implement. That way your higher level code can assume a common Database object, and you would just need to define new modules for different NoSQL backends.
Edit
You should take a look at nonrel django: http://www.allbuttonspressed.com/projects/django-nonrel
They are accomplishing this same task for django, using mongodb as their primary choice, but you would be able to add more support by implementing a few of their base classes for CouchDB. This should give you a good idea of how they accomplish it, since django is the same idea, using an agnostic ORM layer over swappable database backends.
您可以这样做:
尽管这里的缺点是
%s
在 URI 模板中不突出。恐怕不会有比这更漂亮的了。
You could do it this way:
although the downside here is that the
%s
doesn't stand out in the URI template.It's not going to get much prettier than this, I'm afraid.