如何开始围绕另一个包装器编写一个Python包装器?

发布于 2024-12-25 11:17:08 字数 1089 浏览 3 评论 0原文

我知道这个问题已经被一两个人问过,但它与我的想法有些不同。请耐心听我说。

我正在尝试围绕现有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2025-01-01 11:17:08

这是基于拉斯曼对您的特定格式问题的回答,但我认为您可能想要以与此不同的方式进行处理。最好首先创建一个为任何 NoSQL 后端定义通用接口的基类。像这样:

class Database(object):

    def __init__(self, **kwargs):
        self.connect(**kwargs)

    def connect(self, **kwargs):
        pass


class Couch(Database):

    def __init__(**kwargs):
        super(Couch, self).__init__(**kwargs)

    def connect(self, username=None, password=None):
        if username and password:
            # connect this way
            pass
        else:
            # connect that way
            pass

数据库类将定义子类应该实现的所有接口方法。这样,您的更高级别的代码就可以假设一个通用的数据库对象,并且您只需要为不同的 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:

class Database(object):

    def __init__(self, **kwargs):
        self.connect(**kwargs)

    def connect(self, **kwargs):
        pass


class Couch(Database):

    def __init__(**kwargs):
        super(Couch, self).__init__(**kwargs)

    def connect(self, username=None, password=None):
        if username and password:
            # connect this way
            pass
        else:
            # connect that way
            pass

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.

简单 2025-01-01 11:17:08

您可以这样做:

class Couch(object):
    URI_TEMPLATE = "http://%slocalhost:5984"

    def __init__(self, username=None, password=None):
        if username and password:
            user_part = "%s:%s@" % (username, password)
        else:
            user_part = ""
        self.url = URI_TEMPLATE % user_part

尽管这里的缺点是 %s 在 URI 模板中不突出。

恐怕不会有比这更漂亮的了。

You could do it this way:

class Couch(object):
    URI_TEMPLATE = "http://%slocalhost:5984"

    def __init__(self, username=None, password=None):
        if username and password:
            user_part = "%s:%s@" % (username, password)
        else:
            user_part = ""
        self.url = URI_TEMPLATE % user_part

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文