小编Løi*_*ten的帖子

模拟和猴子修补有什么区别?

我使用python,我对测试有点新意.我经常看到测试用本地方法替换外部依赖项,如下所示:

import some_module

def get_file_data():
  return "here is the pretend file data"

some_module.get_file_data = get_file_data

# proceed to test
Run Code Online (Sandbox Code Playgroud)

我在这个问题中看到这被称为"猴子修补" .我还看到"模拟"这个词与"钱修补"或者似乎非常相似的场景一起使用了很多.

这两个概念有什么区别吗?

python testing unit-testing monkeypatching mocking

9
推荐指数
1
解决办法
3003
查看次数

如何做一个象征性的泰勒扩展未知函数$ f(x)$使用sympy

圣人中,对未知函数f(x)进行泰勒展开是相当容易的,

x = var('x')
h = var('h')
f = function('f',x)
g1 = taylor(f,x,h,2)
Run Code Online (Sandbox Code Playgroud)

如何在同情中做到这一点?


更新

asmeurer指出,这是一个很快就可以通过拉动请求http://github.com/sympy/sympy/pull/1888获得的功能.我用pip安装了分支,

pip install -e git+git@github.com:renatocoutinho/sympy.git@897b#egg=sympy --upgrade
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试计算f(x)的系列时,

x, h = symbols("x,h")
f = Function("f")
series(f,x,x+h)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误,

TypeError:必须使用f实例作为第一个参数调用未绑定方法series()(改为使用Symbol实例)

python sympy

8
推荐指数
2
解决办法
6138
查看次数

pytest 固定装置和线程同步

我正在尝试使用 pytest-xdist 以使我的测试并行运行,问题是每个线程都将转到与所有测试共享的夹具并根据线程数执行它。

它给我带来了一个问题,因为该夹具的作用是为我的测试创建数据,一旦它已经创建,我就会得到和错误,因为它已经创建(通过 REST)。

conftest.py:

lock = threading.Lock()

@pytest.fixture(scope=session)
def init_data(request):

    lock.acquire()
    try:
        data = []
        if checking_data_not_created():
            data.append(some_rest_command_creating_data_1())
            data.append(some_rest_command_creating_data_2())
    finally:
        lock.release()

    yield data

    lock.acquire()
    try:
        remove_all_data()
    finally:
        lock.release()
Run Code Online (Sandbox Code Playgroud)

测试类.py:

class classTests():

    def first_test(init_data):
        test body and validation....

     def second_test(init_data):
        test body and validation....
Run Code Online (Sandbox Code Playgroud)

我正在使用命令:pytest -v -n2

假设第一个线程应该运行 first_test() 并且第二个线程应该运行 second_test() 其中一个总是会失败,因为第一个已经在夹具部分创建了数据,另一个线程将出现异常并且他应该运行的所有测试都将失败。

如您所见,我尝试使用锁来同步线程,但它也不起作用。

知道如何克服这个问题吗?

谢谢。

python multithreading pytest xdist

7
推荐指数
1
解决办法
3593
查看次数

可在 networkx 中使用的属性列表

我正在使用 networkx,但无法在任何地方找到边或节点的可用属性列表。我对已经分配的属性不感兴趣,但我对创建或编辑节点或边时可以设置/更改的内容不感兴趣。

有人可以指出我记录的地方吗?

谢谢!

python networkx

6
推荐指数
2
解决办法
4271
查看次数

PyCharm在用作解释器的docker容器中覆盖PYTHONPATH

我有一个包含各种位的docker镜像,包括Spark.这是我的Dockerfile:

FROM docker-dev.artifactory.company.com/centos:7.3.1611

# set proxy
ENV http_proxy http://proxyaddr.co.uk:8080
ENV HTTPS_PROXY http://proxyaddr.co.uk:8080
ENV https_proxy http://proxyaddr.co.uk:8080

RUN yum install -y epel-release
RUN yum install -y gcc
RUN yum install -y krb5-devel
RUN yum install -y python-devel
RUN yum install -y krb5-workstation
RUN yum install -y python-setuptools
RUN yum install -y python-pip
RUN yum install -y xmlstarlet
RUN yum install -y wget java-1.8.0-openjdk
RUN pip install kerberos
RUN pip install numpy
RUN pip install pandas
RUN pip install coverage
RUN pip install …
Run Code Online (Sandbox Code Playgroud)

python pycharm docker

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

我怎样才能在GPU上运行theano

如果我使用python 3.5运行以下代码

import numpy as np
import time
import theano
A = np.random.rand(1000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices('XY')
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print ("NP time: %f[s], theano time: %f[s] **(times should be close when run
on CPU!)**" %(np_end-np_start, t_end-t_start))
print ("Result difference: %f" % (np.abs(AB-tAB).max(), ))
Run Code Online (Sandbox Code Playgroud)

我得到了输出

NP time: 0.161123[s], theano time: 0.167119[s] (times should be close when
run on CPU!)
Result …
Run Code Online (Sandbox Code Playgroud)

python cuda gpu theano

5
推荐指数
3
解决办法
2万
查看次数

在pytest中,如何确保在类级别之前调用模块级别的fixture

我的 pytest 很旧(版本 2.8.3)。

pytestmark = pytest.mark.usefixtures("module_level")

@pytest.fixture(scope='module')
def module_level(request):
    print("module start")
    def fin():
        print("module end")
    request.addfinalizer(fin)

@pytest.fixture(scope='class')
def class_level(request):
    print("class start")
    def fin():
        print("class end")
    request.addfinalizer(fin)

@pytest.mark.usefixtures("class_level")
class TestMyClass:  
    def test_func(self):
        pass
Run Code Online (Sandbox Code Playgroud)

但是,我得到的订单是:

class start
module start
class end
module end
Run Code Online (Sandbox Code Playgroud)

这不是我想要的。那么编写模块级设置/清理装置的正确方法是什么(并确保它在一切之前进行设置并在一切之后进行清理)?

python fixture pytest

5
推荐指数
1
解决办法
2371
查看次数

我应该直接返回数据集还是应该使用 one_shot 迭代器?

我正在使用 Dataset API 构建数据管道,但是当我训练多个 GPU 并返回dataset.make_one_shot_iterator().get_next()我的输入函数时,我得到

ValueError: dataset_fn() must return a tf.data.Dataset when using a tf.distribute.Strategy
Run Code Online (Sandbox Code Playgroud)

我可以按照错误消息直接返回数据集,但我不明白iterator().get_next()它在单 GPU 和多 GPU 上训练的目的和工作原理。

...

    dataset = dataset.repeat(num_epochs)
    dataset = dataset.batch(batch_size = batch_size)
    dataset = dataset.cache()

    dataset = dataset.prefetch(buffer_size=None)

    return dataset.make_one_shot_iterator().get_next()

return _input_fn
Run Code Online (Sandbox Code Playgroud)

python iterator pipeline tensorflow tensorflow-datasets

5
推荐指数
1
解决办法
715
查看次数

AttributeError: 'function' 对象没有属性 'func_name' 和 python 3

我下载了以下代码:

from __future__ import print_function
from time import sleep

def callback_a(i, result):
    print("Items processed: {}. Running result: {}.".format(i, result))

def square(i):
    return i * i

def processor(process, times, report_interval, callback):
    print("Entered processor(): times = {}, report_interval = {}, callback = {}".format(
    times, report_interval, callback.func_name))
    # Can also use callback.__name__ instead of callback.func_name in line above.
    result = 0
    print("Processing data ...")
    for i in range(1, times + 1):
        result += process(i)
        sleep(1)
        if i % report_interval == 0:
            # This …
Run Code Online (Sandbox Code Playgroud)

python function callback python-2.x python-3.x

5
推荐指数
1
解决办法
6291
查看次数

clang:错误:没有这样的文件或目录:'/ Users /waterskiingmithrill/Development/iPhone/Event/Event_Prefix.pch'

我弄乱了我的iPhone项目.我跑svn update了几个月前摆弄了存储库.自那以后发生了许多变化,从旧的角度恢复几乎是不可能的.

当我尝试构建项目时,我得到:

ProcessPCH /Users/waterskiingmithrill/Library/Developer/Xcode/DerivedData/Event-cqyicuekijryjpbuuucigvdrxrdc/Build/Intermediates/PrecompiledHeaders/Event_Prefix-brpgiiwwokyynuheloumlvxlmurs/Event_Prefix.pch.pth Event_Prefix.pch normal i386 objective-c com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/waterskiingmithrill/Development/iPhone/Event
setenv LANG en_US.US-ASCII
setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c-header -arch i386 -fmessage-length=0 -std=c99 -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wreturn-type -Wno-implicit-atomic-properties -Wformat -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-sign-compare -Wno-shorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wno-undeclared-selector -Wno-deprecated-implementations -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk -fexceptions -fasm-blocks -Wprotocol -Wdeprecated-declarations -mmacosx-version-min=10.6 -g -fvisibility=hidden -Wno-conversion -Wno-sign-conversion -fobjc-abi-version=2 -fobjc-legacy-dispatch "-DIBOutlet=__attribute__((iboutlet))" "-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection(ClassName)))" "-DIBAction=void)__attribute__((ibaction)" -D__IPHONE_OS_VERSION_MIN_REQUIRED=40000 -iquote "/Users/waterskiingmithrill/Library/Developer/Xcode/DerivedData/Event-cqyicuekijryjpbuuucigvdrxrdc/Build/Intermediates/Event.build/Debug-iphonesimulator/HatFair.build/Hat Fair-generated-files.hmap" "-I/Users/waterskiingmithrill/Library/Developer/Xcode/DerivedData/Event-cqyicuekijryjpbuuucigvdrxrdc/Build/Intermediates/Event.build/Debug-iphonesimulator/HatFair.build/Hat Fair-own-target-headers.hmap" "-I/Users/waterskiingmithrill/Library/Developer/Xcode/DerivedData/Event-cqyicuekijryjpbuuucigvdrxrdc/Build/Intermediates/Event.build/Debug-iphonesimulator/HatFair.build/Hat Fair-all-target-headers.hmap" -iquote …
Run Code Online (Sandbox Code Playgroud)

iphone xcode objective-c clang ios

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