我正在尝试学习Docker的细节,我对保存图像的前景感到困惑.
我运行了基本的Ubuntu映像,安装了Anaconda Python和其他一些东西......那么现在保存进度的最佳方法是什么?保存,提交,导出?
这些似乎都与VirtualBox的工作原理相同,VirtualBox为您的虚拟机提供了一个明显的保存状态文件.
我有一个由 N 个 3x3 数组(矩阵集合,尽管数据类型是 np.ndarray)组成的数组,我有一个由 N 个 3x1 数组(向量集合)组成的数组。我想要做的是将每个矩阵乘以每个向量,所以我希望得到 N 个 3x1 数组。
简单的例子:
A = np.ones((6,3,3))
B = np.ones((6,3,1))
np.dot(A,B) # This gives me a 6x3x6x1 array, which is not what I want
np.array(map(np.dot,A,B)) # This gives me exactly what I want, but I don't want to have to rely on map
Run Code Online (Sandbox Code Playgroud)
我已经厌倦了各种重塑、探索einsum等,但无法让它按照我想要的方式工作。我如何让它与 numpy 广播一起使用?这个操作最终需要执行数千次,我不希望map或列出理解操作来减慢速度。
我试图按照Continuum Analytics博客上的一个例子来对Python,Cython,Numba进行基准测试,以获得使用for循环计算的总和.不幸的是,我发现Cython比Python慢!
这是我的Python函数定义:
def python_sum(y):
N = len(y)
x = y[0]
for i in xrange(1,N):
x += y[i]
return x
Run Code Online (Sandbox Code Playgroud)
现在我的Cython功能:
def cython_sum(int[:] y):
cdef int N = y.shape[0]
cdef int x = y[0]
cdef int i
for i in xrange(1,N):
x += y[i]
return x
Run Code Online (Sandbox Code Playgroud)
现在我有一个脚本可以提取两个函数和基准:
import timeit
import numpy as np
import cython_sum
import python_sum
b = np.ones(10000)
timer = timeit.Timer(stmt='python_sum.python_sum(b)', setup='from __main__ import python_sum, b')
print "Python Sum (ms): %g" % (timer.timeit(1)*1000)
timer = timeit.Timer(stmt='cython_sum.cython_sum(b)', …Run Code Online (Sandbox Code Playgroud) 我有一种情况,我想在 Python 中创建两个单独的记录器对象,每个对象都有自己独立的处理程序。我所说的“单独”是指我希望能够独立地将日志语句传递给每个对象,而不会污染其他日志。
import logging
from my_other_logger import init_other_logger
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stdout)])
other_logger = init_other_logger(__name__)
logger.info('Hello World') # Don't want to see this in the other logger
other_logger.info('Goodbye World') # Don't want to see this in the first logger
Run Code Online (Sandbox Code Playgroud)
import logging
import os, sys
def init_other_logger(namespace):
logger = logging.getLogger(namespace)
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(LOG_FILE_PATH)
logger.addHandler(fh)
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
fh.setFormatter(formatter)
#logger.propagate = False
return logger
Run Code Online (Sandbox Code Playgroud)
我能够确定在这里有用的唯一配置是属性logger.propagate。按原样运行上面的代码将所有日志语句传输到日志流和日志文件。当我没有logger.propagate = False任何内容通过管道传输到日志流时,两个日志对象再次将其输出通过管道传输到日志文件。
如何创建一个仅将日志发送到一个处理程序的日志对象,以及另一个将日志发送到另一个处理程序的日志对象?
Just recently upgraded to Mac OSX Catalina, and it seems to have broken all my Python venv's.
source venv/bin/activatepython or python3, it actually runs the system Python interpreters, instead of the expected Python interpreter in my venv.With my venv activated...
>>> which python
/usr/bin/python
>>> which python3
/usr/local/bin/python3
Run Code Online (Sandbox Code Playgroud)
I expect that to point to /Users/<username>/<path-to-venv>/venv/bin/python3 …
alko发布了一个非凡的答案,用于在此线程中以数字方式计算多元函数的偏导数.
我现在有一个关于增强此函数以接受输入值数组的后续问题.我有一些代码,我循环遍历一大堆n维点,计算每个变量的偏导数,这在计算上非常昂贵.
np.vectorize使用partial_derivative包装函数矢量化很容易,但它会导致包装器出现问题:
from scipy.misc import derivative
import numpy as np
def foo(x, y):
return(x**2 + y**3)
def partial_derivative(func, var=0, point=[]):
args = point[:]
def wraps(x):
args[var] = x
return func(*args)
return derivative(wraps, point[var], dx=1e-6)
vfoo = np.vectorize(foo)
>>>foo(3,1)
>>>10
>>>vfoo([3,3], [1,1])
>>>array([10,10])
>>>partial_derivative(foo,0,[3,1])
>>>6.0
>>>partial_derivative(vfoo,0,[[3,3], [1,1]])
>>>TypeError: can only concatenate list (not "float") to list
Run Code Online (Sandbox Code Playgroud)
理想情况下,最后一行应该返回[6.0, 6.0].在这种情况下,提供给矢量化函数的两个数组vfoo基本上是成对压缩的,因此([3,3], [1,1])转换为两个点,[3,1]并且[3,1].当它传递给函数时,这似乎会受到损害wraps.它最终传递给函数的重点derivative是[3,3].此外,显然已经 …
我正在尝试使用 CloudFormation 模板来定义 CodeBuild 和 CodePipeline,以自动部署托管在 S3 存储桶中的静态网站。为了在信用到期时给予信用,我主要遵循https://dzone.com/articles/continuous-delivery-to-s3-via-codepipeline-and-cod 中的模板。
我无法解决的问题是,在我为 Hugo 版本添加环境变量后,我想用它来创建静态站点文件,我从 AWS 控制台收到一条错误消息:“模板验证错误:模板格式错误:模板的资源块中存在未解决的资源依赖关系 [HUGO_VERSION]。”
为什么它不接受我在 environment_variables 下定义的 HUGO_VERSION 环境变量?这是格式的 0.1 版,因此与当前的 0.2 版略有不同,但我已阅读以下链接:https : //docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref .html#build-spec-ref-syntax
那件事真的让我困惑的是,如果我用$ {} HUGO_VERSION删除线,模板被接受就好了-然后建立节目HUGO_VERSION = 0.49(因为printenv命令的)后,CloudWatch的日志的检查!是什么赋予了?
最初,模板看起来像这样。
---
AWSTemplateFormatVersion: '2010-09-09'
Description: Pipeline using CodePipeline and CodeBuild for continuous delivery of a single-page application to S3
Parameters:
SiteBucketName:
Type: String
Description: Name of bucket to create to host the website
GitHubUser:
Type: String
Description: GitHub User
Default: "stelligent"
GitHubRepo:
Type: …Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-cloudformation hugo aws-codepipeline aws-codebuild
我正在尝试测试一个类并模拟其方法之一,但我似乎无法用我的模拟行为替换其中一个实例方法的行为。我的代码是这样组织的:
--src
----module
------__init__.py
------my_class.py
--tst
----__init__.py
----test_my_class.py
my_class.py包含以下内容
class MyClass:
def __init__(self):
pass
def do_something(self):
return 'Real Output'
Run Code Online (Sandbox Code Playgroud)
我的测试文件test_my_class.py包含以下内容。
from unittest.mock import patch
from src.module.my_class import MyClass
def test_my_class():
my_class = MyClass()
assert my_class.do_something() == 'Real Output'
@patch('src.module.my_class.MyClass')
def test_mock_my_class(mock_my_class):
mock_my_class.return_value.do_something.return_value = 'Mocked Output'
my_class = MyClass()
assert my_class.do_something() == 'Mocked Output'
Run Code Online (Sandbox Code Playgroud)
第一个测试工作得很好(到目前为止没有涉及任何模拟)。然而,第二个测试给了我以下断言错误。我希望该do_something()方法被模拟并返回“模拟输出”,并且断言语句的计算结果为 true。我这里哪里出错了?
AssertionError: assert <bound method MyClass.do_something of <src.module.my_class.MyClass object at 0x1057133c8>> == 'Mocked Output'
E + where <bound method MyClass.do_something of …
我有一个 Dockerfile,我试图在其中安装并使用 asdf 来管理 Python 包版本。下面显示了我的 Dockerfile 的片段。
SHELL ["/bin/bash", "-c"]
RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
RUN chmod +x ~/.asdf/asdf.sh ~/.asdf/completions/asdf.bash
RUN echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
RUN echo ". $HOME/.asdf/completions/asdf.bash" >> ~/.bashrc
ENV PATH="$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH"
ENV PATH="$HOME/.asdf:$PATH"
RUN echo -e '\nsource $HOME/.asdf/asdf.sh' >> ~/.bashrc
RUN source ~/.bashrc
RUN bash -c 'echo -e which asdf'
RUN asdf plugin-add python
Run Code Online (Sandbox Code Playgroud)
最后一行是有问题的行。当我尝试构建此 Docker 映像时,我得到以下信息。
=> ERROR [17/19] RUN asdf plugin-add python 0.3s
------
> [17/19] RUN asdf plugin-add python:
#21 0.292 …Run Code Online (Sandbox Code Playgroud) 我试图理解matplotlib,但我不能为我的生活摆脱这个灰色边框周围的情节.
我的代码非常简单.我一直在浏览matplotlib文档,但令人沮丧的是我找不到任何关于如何更改背景颜色的内容.有人可以帮忙吗?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10)
y = np.linspace(0,10)
plt.plot(x,y)
plt.show()
Run Code Online (Sandbox Code Playgroud)

谢谢!
我在计算从根到二叉树中指定节点的路径时遇到了麻烦(这特别是关于此问题的Python解决方案)。
这是一个例子。给定下面的二叉树,如果我指定值为4的节点,我想返回[1、2、4]。如果我指定值为5的节点,我想返回[1、2、5]。
1
/ \
2 3
/ \
4 5
Run Code Online (Sandbox Code Playgroud)
这是我尝试的解决方案。
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def path(root, k, l=[]):
if not root:
return []
if root.val == k:
return l
# Pre-order traversal: Visit root, then left, then right.
l.append(root.val)
path(root.left, k, l)
path(root.right, k, l)
return l
Run Code Online (Sandbox Code Playgroud)
现在如果我运行这个
>>> a = TreeNode(1)
>>> b = TreeNode(2)
>>> c = TreeNode(3)
>>> d = TreeNode(4)
>>> e = TreeNode(5) …Run Code Online (Sandbox Code Playgroud) python ×9
docker ×2
numpy ×2
python-3.x ×2
anaconda ×1
arrays ×1
asdf ×1
bash ×1
binary-tree ×1
cython ×1
derivative ×1
hugo ×1
logging ×1
macos ×1
matplotlib ×1
matrix ×1
mocking ×1
python-2.7 ×1
python-2.x ×1
scipy ×1
unit-testing ×1
vector ×1
virtualenv ×1