小编Toc*_*Toc的帖子

通用字典的不区分大小写的访问

我有一个使用托管dll的应用程序.其中一个dll返回一个通用字典:

Dictionary<string, int> MyDictionary;  
Run Code Online (Sandbox Code Playgroud)

字典包含大小写的键.

另一方面,我得到一个潜在的密钥列表(字符串),但我不能保证这种情况.我试图使用键获取字典中的值.但是,由于案件不匹配,以下情况当然会失败:

bool Success = MyDictionary.TryGetValue( MyIndex, out TheValue );  
Run Code Online (Sandbox Code Playgroud)

我希望TryGetValue会有一个像MSDN文档中提到的忽略大小写标志,但它似乎对通用词典无效.

有没有办法让字典的值忽略关键案例?有没有比使用正确的StringComparer.OrdinalIgnoreCase参数创建字典的新副本更好的解决方法?

c# generics dictionary

225
推荐指数
4
解决办法
9万
查看次数

固定装置的pytest夹具

我目前正在为一个中等大小的库(约300个文件)编写测试.该库中的许多类共享相同的测试方案,使用pytest编码:

文件test_for_class_a.py:

import pytest

@pytest.fixture()
def setup_resource_1():
    ...

@pytest.fixture()
def setup_resource_2():
    ...

@pytest.fixture()
def setup_class_a(setup_resource_1, setup_resource_2):
    ...

def test_1_for_class_a(setup_class_a):
    ...

def test_2_for_class_a(setup_class_a):
    ...
Run Code Online (Sandbox Code Playgroud)

class_b,class_c等存在类似的文件...唯一的区别是setup_resource_1和setup_resource_2的内容.

现在我想重新使用在test_for_class_a.py,test_for_class_b.py和test_for_class_c.py中定义的fixtures setup_class_a,setup_class_b,setup_class_c来对它们进行测试.

在文件test_all_class.py中,这可以工作,但每次测试仅限于一个夹具:

from test_for_class_a import *

@pytest.mark.usefixtures('setup_class_a')      # Fixture was defined in test_for_class_a.py
def test_some_things_on_class_a(request)
    ...
Run Code Online (Sandbox Code Playgroud)

但我正在寻找一种更通用的方法:

from test_for_class_a import *
from test_for_class_b import *   # I can make sure I have no collision here 
from test_for_class_c import *   # I can make sure I have no collision here 

==> @generate_test_for_fixture('setup_class_a', 'setup_class_b', 'setup_class_c') 
def test_some_things_on_all_classes(request) …
Run Code Online (Sandbox Code Playgroud)

python pytest

6
推荐指数
1
解决办法
3797
查看次数

如何使用conda“冻结”GLIBC版本?

我经常在 Linux 上使用 conda 环境。现在我需要为带有 GLIBC 2.5 的 Linux 创建一个环境。我可以使用 GLIBC 2.5 获得 miniconda 的官方版本,但(据我所知)没有任何东西可以保证我将安装的软件包不需要高于 2.5 的 GLIBC 版本。有什么方法可以限制或知道 GLIBC 包的要求是什么?

python glibc conda

5
推荐指数
0
解决办法
457
查看次数

在嵌套类中使用super

我对python 2.7.12中的以下错误感到困惑.假设我们在类中有一个类定义,类似于:

class C(object):
    def __init__(self):
        print "class C"


class D(object):
    def __init__(self):
        print "class D"


class A(D):

    class B(C):
        def __init__(self):
            # Strangely here B is "not defined", why?
            super(B, self).__init__()
            print "class B"

    def __init__(self):
        super(D, self).__init__()
        print "class A"

    def do_something(self):
        b_class = self.B()
        print "b_class within A : {}".format(b_class)


a_class = A()
a_class.do_something()
Run Code Online (Sandbox Code Playgroud)

但是如果我们在类B的范围之外提取类的定义A,一切都很好.

在嵌套类中调用时,我们是否需要使用"super"?我无法理解为什么它的使用在嵌套类的内部或外部会有所不同.有什么指针吗?

python inheritance

2
推荐指数
1
解决办法
398
查看次数

标签 统计

python ×3

c# ×1

conda ×1

dictionary ×1

generics ×1

glibc ×1

inheritance ×1

pytest ×1