如何将python dict转换为DictRow对象

Pra*_*wda 4 unit-testing psycopg2 pytest python-3.x

嗨,我正在使用 pytest 编写单元测试。但我无法模拟几个 db 函数。我们使用 psycopg2 进行数据库连接和执行。从 psycopg2 返回的查询响应属于 DictRow 类型,可以通过键或索引访问。前任:

response = ['prajwal', '23', 'engineer'] #Response of a query "select name, age , job from users"

>>>response[0]
   'prajwal'
    
>>>response['name']
   'prajwal'
Run Code Online (Sandbox Code Playgroud)

我想知道有什么方法可以将 dict/list 转换为上述类型。

Axe*_*319 5

查看 的来源psycopg2,创建 aDictRow需要传入一个DictCursor对象。然而,它唯一使用的DictCursor似乎是indexanddescription属性。

# found in lib\site-packages\psycopg2.extras.py
class DictRow(list):
    """A row object that allow by-column-name access to data."""

    __slots__ = ('_index',)

    def __init__(self, cursor):
        self._index = cursor.index
        self[:] = [None] * len(cursor.description)
Run Code Online (Sandbox Code Playgroud)

索引看起来像一个dict将键映射到索引的索引。例如response['name'] = 0 ,描述看起来像您dict要转换的内容。

如果你觉得自己很笨拙,你可以利用鸭子打字,假装你只是在cursor满足要求时传入 a 。

唯一的警告是在我们实例化 之后DictRow,我们需要填充它。我们的假cursor黑客会处理剩下的。

from psycopg2.extras import DictRow

class DictRowHack:
    def __init__(self, my_dict):
        # we need to set these 2 attributes so that
        # it auto populates our indexes
        self.index = {key: i for i, key in enumerate(my_dict)}
        self.description = my_dict

def dictrow_from_dict(my_dict):
    # this is just a little helper function
    # so you don't always need to go through
    # the steps to recreate a DictRow
    fake_cursor = DictRowHack(my_dict)

    my_dictrow = DictRow(fake_cursor)

    for k, v in my_dict.items():
        my_dictrow[k] = v
    
    return my_dictrow

response = {'name': 'prajwal', 'age': '23', 'job': 'engineer'}

my_dictrow = dictrow_from_dict(response)

print(my_dictrow[1])
print(my_dictrow['name'])
print(type(my_dictrow))
Run Code Online (Sandbox Code Playgroud)