我正在为Google Cloud API编写客户端库,这些库具有相当常见的异步帮助程序重载模式:
目前我们正在使用异步方法,但是:
(await foo.Bar().ConfigureAwait(false)).TransformToBaz()括号很烦人.使用两个语句可以提高可读性,但这意味着我们不能使用表达式身体方法.ConfigureAwait(false)- 这在某种程度上可以通过工具解决,但它仍然有点气味Task<TResult>.ContinueWith听起来是个好主意,但我读过Stephen Cleary的博客文章推荐反对它,原因看似合理.我们正在考虑Task<T>为此添加扩展方法:
潜在的延伸方法
public static async Task<TResult> Convert<TSource, TResult>(
this Task<TSource> task, Func<TSource, TResult> projection)
{
var result = await task.ConfigureAwait(false);
return projection(result);
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以非常简单地从同步方法中调用它,例如
public async Task<Bar> BarAsync()
{
var fooRequest = BuildFooRequest();
return FooAsync(fooRequest).Convert(foo => new Bar(foo));
}
Run Code Online (Sandbox Code Playgroud)
甚至:
public Task<Bar> BarAsync() =>
FooAsync(BuildFooRequest()).Convert(foo => new Bar(foo));
Run Code Online (Sandbox Code Playgroud)
它看起来如此简单和有用,我有点惊讶没有已经可用的东西.
作为我使用它来使表达式方法工作的一个例子,在Google.Cloud.Translation.V2代码中我有两种方法来翻译纯文本:一个接受一个字符串,一个接受多个字符串.单字符串版本的三个选项是(在参数方面有所简化):
常规异步方法
public async Task<TranslationResult> TranslateTextAsync( …Run Code Online (Sandbox Code Playgroud) 我正在编写代码来将(可能)非常大的整数值存储到chars由指针引用的数组中.我的代码看起来像这样:
cdef class Variable:
cdef unsigned int Length
cdef char * Array
def __cinit__(self, var, length):
self.Length = length
self.Array = <char *>malloc(self.Length * sizeof(char)) # Error
for i in range(self.Length):
self.Array[i] = <char>(var >> (8 * i))
def __dealloc__(self):
self.Array = NULL
Run Code Online (Sandbox Code Playgroud)
当我尝试编译代码时,我在注释行中收到错误"存储临时Python引用的不安全C派生".我的问题是:我在C中存储的临时Python参考和存储,以及如何修复它?
我有一个SQL连接服务,我正在尝试构建.我基本上这样做:
var data = await GetDataFromFlatFilesAsync(dir).ConfigureAwait(false);
using (var conn = new SqlConnection(MyConnectionString))
{
try
{
conn.Open();
var rw = new SqlReaderWriter(conn);
await DoStuffWithSqlAsync(rw, data).ConfigureAwait(false);
}
catch (Exception ex)
{
// Do exceptional stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
DoStuffWithSqlAsync 像这样运作:
private async Task DoStuffWithSqlAsync(SqlReaderWriter rw, IEnumerable<Thing> data)
{
await Task.WhenAll(data.Select(rw.RunQueryAsync)).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)
并RunQueryAsync按此操作:
public async Task RunQueryAsync<T>(T content)
{
// _conn is assigned to in the constructor with conn
try
{
var dataQuery = _conn.CreateCommand();
dataQuery.CommandText = TranslateContentToQuery(content);
await dataQuery.ExecuteNonQueryAsync().ConfigureAwait(false);
} …Run Code Online (Sandbox Code Playgroud) 我为 Powershell 安装了 Posh-Git 模块,最近我还安装了 Anaconda 并执行了conda init. 显然,这profile.ps1通过添加以下代码来修改文件:
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "{User}\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
Run Code Online (Sandbox Code Playgroud)
这确实让我看到了我正在使用的 conda 环境,但它也隐藏了我正在使用的 Git 分支。我该如何修改它以便我可以看到两者?
我试图在 S3 中设置一个事件通知,该通知会在任何对象创建事件上向 Snowflake 生成的 SQS 发送消息。这相当简单,但是当我尝试测试配置时,我注意到没有发生任何事件。经过仔细检查,我注意到这一点:
单击该错误时,我收到一条消息,指出 ARN 的 S3 资源格式无效。我从 Snowflake 收到此 ARN,因此我确信它是有效的。那么,为什么会发生此错误以及如何解决它?
我正在尝试在 Golang 中实现一些缓存函数,但我希望它们对字符串和实现该接口的其他对象都有效Stringer。我正在尝试使用 Golang 泛型,这是我到目前为止所做的:
import (
"fmt"
)
type String interface {
~string | fmt.Stringer
}
Run Code Online (Sandbox Code Playgroud)
但是,这会产生错误cannot use fmt.Stringer in union (fmt.Stringer contains methods)。有没有办法在不依赖反射或类型装箱/拆箱的情况下做到这一点?
我有一个具有以下结构的 protobuf 存储库:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 protos\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 common\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 buf.yaml\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 time.proto\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 internal\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 buf.yaml\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 user\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 user.proto\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 buf.gen.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 buf.work.yaml\nRun Code Online (Sandbox Code Playgroud)\n`time.proto 的存根是:
\nsyntax = "proto3";\npackage common;\n\noption go_package = "github.com/my-org/protobuf-gen-go/gopb"; // golang\n\n// Message definition here\nRun Code Online (Sandbox Code Playgroud)\n并user.proto引用该文件。这是它的存根:
syntax = "proto3";\npackage internal.dynamodb;\n\nimport "common/time.proto";\n\noption go_package = "github.com/my-org/protobuf-internal/user"; // golang\n\n// Message definition here\nRun Code Online (Sandbox Code Playgroud)\n这两个buf.yaml文件看起来都是这样的:
version: v1\nbreaking:\n use:\n - FILE\nlint:\n use:\n - DEFAULT\nRun Code Online (Sandbox Code Playgroud)\n看起来buf.work.yaml像这样:
version: v1\ndirectories:\n - protos/common\n …Run Code Online (Sandbox Code Playgroud) 我有一个Python脚本,它应该运行大量其他脚本,每个脚本都位于脚本工作目录的子目录中.这些其他脚本中的每一个都应该连接到游戏客户端并为该游戏运行AI.为了实现这一点,我必须在两个独立的线程上运行每个脚本(每个线程一个).我遇到的问题是有时脚本的输出没有被捕获.我的运行代码如下所示:
def run(command, name, count):
chdir(name)
output = check_output(" ".join(command), stderr = STDOUT, shell = True).split('\r')
chdir('..')
with open("results_" + str(count) + ".txt", "w") as f:
for line in output:
f.write(line)
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它确实设法捕获更长的流,但短的流不被注意.如何更改代码以解决此问题?
更新:我不认为这是一个缓冲问题,因为check_output("ls ..", shell = True).split('\n')[:-1]返回预期的结果,该命令应该比我试图运行的脚本花费更少的时间.
更新2:我发现输出正在削减更长的运行.事实证明,由于某种原因,我运行的所有进程都会错过输出结束.这也解释了为什么较短的运行根本不产生任何输出.
假设我有一个A类:
class A:
def __init__(self, x, y):
self.x = x
self.y = y
def sum(self):
return self.x + self.y
Run Code Online (Sandbox Code Playgroud)
我定义了一个名为的工厂方法factory:
def factory(x, y):
class B: pass
b = B()
setattr(b, 'x', x)
setattr(b, 'y', y)
B.__name__ = 'A'
return b
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做print(type(A(1, 2))),print(type(factory(1, 2)))他们会证明这些是不同的类型.如果我尝试做,factory(1, 2).sum()我会得到一个例外.但是,type(A).__name__并且type(factory(1, 2)).__name__是相同的,如果我这样做,A.sum(factory(1, 2))我会得到3,好像我用它来调用它A.所以,我的问题是:
如果factory(1, 2).sum()不在sumB上定义或继承,我需要做什么才能完成工作?
python ×3
c# ×2
go ×2
amazon-s3 ×1
amazon-sqs ×1
async-await ×1
buf ×1
c ×1
conda ×1
cython ×1
duck-typing ×1
extjs ×1
generics ×1
git ×1
javascript ×1
pointers ×1
powershell ×1
scripting ×1
snowpipe ×1
sql ×1
task ×1