除了可能通过其他方式解决的潜在名称冲突 - 从您需要的模块中仅导入部件是否有任何好处:
import SomeModule (x, y, z)
Run Code Online (Sandbox Code Playgroud)
...只是导入所有这些,这更简洁,更容易维护:
import SomeModule
Run Code Online (Sandbox Code Playgroud)
例如,它会使二进制文件更小吗?
我的函数需要以字符串或二进制数据(例如,从文件读取)的形式接受输入。如果是字符串,我想将其转换为原始数据(bytes或bytearray)。
在Python 3中,我可以做到data = bytes(data, 'utf8')。但是,这在Python 2中失败了,因为它只接受一个参数。反之亦然,它data = bytes(data)适用于Python 2,但不适用于Python 3,因为它抱怨需要编码才能工作。
为了便于讨论,我们假设所有输入(如果是字符串形式)都是UTF-8编码的。是否有比下面的怪诞更好的方法来实现我要寻找的东西:
try:
data = bytes(data, 'utf8')
except:
data = bytes(data)
Run Code Online (Sandbox Code Playgroud)
nb data.encode()在Py3中工作,但是在字符串包含非ASCII字节的情况下在Py2中失败。
我试图根据配置字符串选择摘要算法(来自rust-crypto).在Python或JavaScript中,比方说,我可能会使用反射来实现:
getattr(Digest, myAlgorithm)
Run Code Online (Sandbox Code Playgroud)
...但是从我能够使用谷歌开始,这不是像Rust这样的语言的最佳实践(而且我没有找到关于如何完成的细节).我最初的想法是使用模式匹配:
let mut digest = match myAlgorithm {
"sha256" => Sha256::new(),
...
};
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用,因为虽然匹配的所有分支实现相同的特征,但它们最终是不同的类型.此外,假设有一种解决方法,在代码中手动枚举所有这些选项是很麻烦的.
在Rust中这样做的正确方法是什么?
我正在尝试编写 Terraform 来创建一个 Azure 存储帐户,然后在其中创建一堆存储容器。一个重要的细节是存储帐户具有限制对特定地址空间的访问的网络规则。这导致容器创建失败。
我设法通过使用 来解决这个问题azurerm_storage_account_network_rules,具体取决于容器,这样就不会阻止它们的创建。像这样的东西:
resource "azurerm_storage_account" "this" {
name = local.storage_name
resource_group_name = azurerm_resource_group.this.name
location = var.location
account_tier = "Standard"
account_kind = "StorageV2"
is_hns_enabled = true
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "data" {
for_each = toset(var.storage_containers)
name = each.value
storage_account_name = azurerm_storage_account.this.name
container_access_type = "private"
}
# FIXME This order prevents destruction of infrastructure :(
resource "azurerm_storage_account_network_rules" "this" {
storage_account_id = azurerm_storage_account.this.id
default_action = "Deny"
bypass = ["AzureServices"]
virtual_network_subnet_ids = [
# Some address …Run Code Online (Sandbox Code Playgroud) 我的理解是,Haskell中模块的虚线语法表示磁盘上的逻辑结构.所以,如果我们有这样的结构:
Main.hs
Foo/
Bar.hs -- exports "Bar"
Quux.hs -- exports "Quux"
Run Code Online (Sandbox Code Playgroud)
...然后在我们的Main.hs,我们可以做:
import Foo.Bar
import Foo.Quux
Run Code Online (Sandbox Code Playgroud)
(我假设我们只能在文件系统的叶节点上有模块.例如,在上面,我们也无法拥有Foo模块.)
在这个例子中,我们正在遍历树.如果我们想上去怎么办?
lib/
SomeModule.hs
XYZ.hs
src/
Main.hs
Run Code Online (Sandbox Code Playgroud)
那就是,Main.hs我们如何进口SomeModule或XYZ?
也许这不会是常见现象Main,但是模块间的依赖关系呢?他们可以合法地需要引用"堂兄"节点.
我正在尝试设置一个Ubuntu Docker容器,它在端口9000上运行Node.js HTTP应用程序.为了模仿生产环境的设置,我还希望将Apache作为一个简单的反向代理服务器运行在容器中,转发给它来自端口80的应用程序(我向大坏世界揭露).
我已经能够很好地设置Node.js应用程序容器了,我可以在我的内部安装和设置Apache Dockerfile; 但我对设置反向代理完全不熟悉,所以当Apache确实启动时,它不会代理.
我Dockerfile看起来像:
# DOCKER-VERSION 1.3.0
FROM ubuntu:12.04
# Install and set up Apache as a reverse proxy
RUN apt-get -y install apache2 libapache2-mod-proxy-html
COPY apache2.conf /etc/apache2/app.conf
RUN cat /etc/apache2/app.conf >> /etc/apache2/apache2.conf
RUN service apache2 start
# Install and set up Node.js and bundle app
# ...This works...
EXPOSE 80
CMD ["./start-app.sh"]
Run Code Online (Sandbox Code Playgroud)
...... apache2.conf我追加的/etc/apache2/apache2.conf是:
ServerName localhost
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so
<Proxy *> …Run Code Online (Sandbox Code Playgroud) 我正在玩 Python 的新(ish)asyncio东西,试图将其事件循环与传统线程结合起来。我编写了一个在自己的线程中运行事件循环的类,以隔离它,然后提供一个(同步)方法,该方法在该循环上运行协程并返回结果。(我意识到这使它成为一个有点毫无意义的例子,因为它必须序列化所有内容,但这只是一个概念验证)。
import asyncio
import aiohttp
from threading import Thread
class Fetcher(object):
def __init__(self):
self._loop = asyncio.new_event_loop()
# FIXME Do I need this? It works either way...
#asyncio.set_event_loop(self._loop)
self._session = aiohttp.ClientSession(loop=self._loop)
self._thread = Thread(target=self._loop.run_forever)
self._thread.start()
def __enter__(self):
return self
def __exit__(self, *e):
self._session.close()
self._loop.call_soon_threadsafe(self._loop.stop)
self._thread.join()
self._loop.close()
def __call__(self, url:str) -> str:
# FIXME Can I not get a future from some method of the loop?
future = asyncio.run_coroutine_threadsafe(self._get_response(url), self._loop)
return future.result()
async def _get_response(self, url:str) -> str: …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个两级旭日/圆环图(用于打印),其中第二级是第一级的详细视图。我已阅读并理解本教程,但我是 R 和 ggplot2 新手,在生成第二个级别时遇到困难。在前面提到的文章中,根级别只有一个元素(有点多余),而我的根有很多元素;其中,二级至少有1个,最多10个要素。
假设我的数据有三列:name、type和value; 其中name和type分别定义根元素和第二层元素。每个都name恰好有一个type,它是跨 s 的 sall的总和(其中,至少有一个,并且跨s 的集合可能相交或互斥)。例如:valuetypenametype
name type value
----- ------- ------
foo all 444
foo type1 123
foo type2 321
bar all 111
bar type3 111
baz all 999
baz type1 456
baz type3 543
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法创建根级别堆栈(在转换为极坐标之前):
data.all <- data[data$type == "all",]
ggplot(data.all, aes(x=1, y=data.all$value, fill=data.all$name)) + geom_bar(stat="identity")
Run Code Online (Sandbox Code Playgroud)
我对第二级堆栈的需要是使type值在值内对齐name,与它们的值成比例: …
这里有许多类似的(ish)问题,关于如何在 Python 中修补类的超类以进行测试。我从他们那里收集了一些想法,但我仍然没有到达我需要的地方。
想象一下,我有两个基类:
class Foo(object):
def something(self, a):
return a + 1
class Bar(object):
def mixin(self):
print("Hello!")
Run Code Online (Sandbox Code Playgroud)
现在我定义了我想要测试的类:
class Quux(Foo, Bar):
def something(self, a):
self.mixin()
return super().something(a) + 2
Run Code Online (Sandbox Code Playgroud)
假设我想测试mixin已调用的,我想替换模拟的返回值Foo.something,但重要的是(并且必然)我不想更改Quux.something. 假设修补超类“有效”,我试过unittest.mock.patch:
with patch("__main__.Foo", spec=True) as mock_foo:
with patch("__main__.Bar", spec=True) as mock_bar:
mock_foo.something.return_value = 123
q = Quux()
assert q.something(0) == 125
mock_bar.mixin.assert_called_once()
Run Code Online (Sandbox Code Playgroud)
这不起作用:超类的定义something和mixin在Quux实例化时不会被模拟,这并不奇怪,因为类的继承是在补丁之前定义的。
我mixin至少可以通过明确设置来解决这个问题:
# This works to mock the mixin method
q …Run Code Online (Sandbox Code Playgroud) typing.Callable有两个“参数”:参数类型和返回类型。对于任意参数,参数类型应该是...,或者是显式类型列表(例如,[str, str, int])。
有没有一种方法可以表示Callable具有完全相同(尽管是任意的)泛型签名的 s?
例如,假设我想要一个接受函数并返回具有相同签名的函数的函数,如果我预先知道函数签名,我可以这样做:
def fn_combinator(*fn:Callable[[Some, Argument, Types], ReturnType]) -> Callable[[Some, Argument, Types], ReturnType]:
...
Run Code Online (Sandbox Code Playgroud)
但是,我不预先知道参数类型,并且我希望我的组合器具有适当的通用性。我曾希望这会起作用:
ArgT = TypeVar("ArgT")
RetT = TypeVar("RetT")
FunT = Callable[ArgT, RetT]
def fn_combinator(*fn:FunT) -> FunT:
...
Run Code Online (Sandbox Code Playgroud)
然而,解析器(至少在 Python 3.7 中)不喜欢ArgT放在第一个位置。Callable[..., RetT]我能做的就是最好的吗?
python ×3
haskell ×2
module ×2
apache ×1
azure ×1
binary ×1
docker ×1
donut-chart ×1
generics ×1
ggplot2 ×1
hierarchy ×1
mocking ×1
mod-proxy ×1
networking ×1
node.js ×1
patch ×1
pie-chart ×1
python-3.x ×1
r ×1
reflection ×1
rust ×1
string ×1
superclass ×1
terraform ×1
unit-testing ×1