小编Woo*_*193的帖子

在数组中查找封闭空间

所以,我生成了一个空格数组,它们的属性是它们可以是红色或黑色。但是,我想防止红色被黑色包围。我有一些例子来说明我的意思:

0 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
1 0 1 0 0 0 0 1
0 1 0 0 1 1 1 0
0 0 0 0 1 0 1 0
1 1 1 0 1 1 1 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)

如果红色为 0,黑色为 1,则此示例包含四个外壳,我在生成数组时希望避免所有外壳。我的输入是数组的大小和我可以生成的 1 的数量。

我该怎么做呢?

python arrays algorithm

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

Python的check_output方法有时不返回输出

我有一个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:我发现输出正在削减更长的运行.事实证明,由于某种原因,我运行的所有进程都会错过输出结束.这也解释了为什么较短的运行根本不产生任何输出.

python scripting multiprocessing

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

Git 提交未经验证,但应该验证

我的提交目前显示为“未验证”,但应将其设置为“已验证”。

我已按照本指南创建我的 GPG 密钥,当我这样做时,gpg --list-secret-keys --keyid-format LONG我得到以下信息:

sec   rsa4096/SOME_KEY 2019-10-24 [SC]
      SOME_OTHER_LONGER_KEY
uid                 [ultimate] Ryan Wood <myemail@address.com>
ssb   rsa4096/SOME_OTHER_KEY 2019-10-24 [E]
Run Code Online (Sandbox Code Playgroud)

我按照此处的gpg --armor --export SOME_KEY说明生成了公钥并将其放在 GitHub 上。此外,我按照本指南进行设置,并在上次提交时提示输入密码,我输入正确。我还验证了我提供给 GPG 的电子邮件和我在 GitHub 上列出的电子邮件是相同的。最后,我根据这个问题中提供的答案在 git 中设置我的签名密钥。但是,我的承诺未经验证。git config --global commit.gpgsign truegit config --global user.signingkey SOME_KEY

我还需要在这里做任何其他事情,或者该过程是否需要一定的时间才能显示提交已验证?

git github

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

Python GRPC - 无法选择子通道

我正在尝试在 Python 中设置 GRPC 客户端来访问特定服务器。服务器设置为需要通过访问令牌进行身份验证。因此,我的实现如下所示:

def create_connection(target, access_token):
    credentials = composite_channel_credentials(
        ssl_channel_credentials(),
        access_token_call_credentials(access_token))

    target = target if target else DEFAULT_ENDPOINT
    return secure_channel(target = target, credentials = credentials)

conn = create_connection(svc = "myservice", session = Session(client_id = id, client_secret = secret)
stub = FakeStub(conn)
stub.CreateObject(CreateObjectRequest())
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当我尝试使用此连接时,出现以下错误:

File "<stdin>", line 1, in <module>
File "\anaconda3\envs\test\lib\site-packages\grpc\_interceptor.py", line 216, in __call__
    response, ignored_call = self._with_call(request,
File "\anaconda3\envs\test\lib\site-packages\grpc\_interceptor.py", line 257, in _with_call
    return call.result(), call
File "anaconda3\envs\test\lib\site-packages\grpc\_channel.py", line 343, in result
    raise self
File "\anaconda3\envs\test\lib\site-packages\grpc\_interceptor.py", …
Run Code Online (Sandbox Code Playgroud)

python grpc envoyproxy

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

修改Go GRPC服务器流拦截器上的元数据

我一直在尝试在服务器流拦截器上设置元数据,以便实际的 RPC 函数可以在下游读取它们:

func UserIDInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    ss.SendHeader(metadata.New(map[string]string{"X-User-Id": "real_user_id"}))
    return handler(srv, ss)
}

func (server *Server) GetObjects(req *iam.GetObjectsRequest, client iam.Service_GetObjectsServer) error {
    ctx := client.Context()
    userID, ok := HeaderFromMetadata(ctx, "X-User-Id")

    log.Printf("User ID: %s, Ok: %t\n", userID, ok)
    return nil
}

func HeaderFromMetadata(ctx context.Context, headers ...string) (string, bool) {
    meta, ok := metadata.FromIncomingContext(ctx)
    if !ok {
        return "", false
    }

    for _, header := range headers {
        if value := meta.Get(header); len(value) > 0 …
Run Code Online (Sandbox Code Playgroud)

go grpc-go

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

Python中Redis LPOP参数数量错误

我有一个简单的 Redis 命令,它执行以下操作:

redis_conn.lpop(queue_name, batch_size)
Run Code Online (Sandbox Code Playgroud)

根据Redis 文档及其Python SDK 文档,这应该是一个有效的请求。然而,我收到以下错误:

redis.exceptions.ResponseError:“lpop”命令的参数数量错误

也许我很迟钝,犯了一个菜鸟错误,因为现在是凌晨 2:00,但是,这应该可行。那么为什么不呢?

python redis

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

Go 中打印类型参数名称

如果我在 Golang 中编写一个泛型函数,我可以像这样打印该函数的任一参数的类型,这提供了对类型参数的一些了解:

func foo[T any](a T, b T) string {
    return fmt.Sprintf("%v and %v are of type %T", a, b, a)
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我修改函数以取切片:

func foo[T any](args ...T) string {
    return fmt.Sprintf("%+v are of type %T", args, args)
}
Run Code Online (Sandbox Code Playgroud)

这不会打印我想要的内容,因为调用foo[int]将意味着args具有一种类型[]int,而我想打印int。我可以修改它来打印,args[0]但这意味着我还必须检查 whereargs为空的情况,在这种情况下,我没有办法获取 的类型args。我还可以使用反射来获取类型,但由于它是一个通用函数,我也应该在编译时知道这一点。

有没有一种方法可以T在编译时获取名称,而无需打印任何参数的类型或通过反射?

generics go

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

protoc:Go 包的包名不一致

我正在使用 protoc 创建一些 DTO。定义采用以下结构:

/protobuf
|-- common.proto
|-- /api
    |-- /service
        |-- csvdownload.proto
Run Code Online (Sandbox Code Playgroud)

我的csvdownload.proto看起来像这样:

syntax = "proto3";
package protobuf.api.service;

import "common.proto";

option go_package = ".;service"; // golang

message CsvExportRequest {
    Common.Currency exportCurrency = 2;
    Common.Decimal rounding = 3;
}
Run Code Online (Sandbox Code Playgroud)

和存根common.proto看起来像这样:

syntax = "proto3";
package protobuf;

option go_package = ".;gopb"; // golang
Run Code Online (Sandbox Code Playgroud)

我试图csvdownload.proto通过从/protobuf目录中运行以下命令来编译:

protoc --go_out=gopb --go_opt=paths=source_relative .\api\service\csvdownload.proto
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

protoc-gen-go:转到包“。” 具有不一致的名称 gopb (common.proto) 和 service (api/service/csvdownload.proto)

我认为这意味着无法生成代码,因为common.protocsvdownload.proto声明了不同的包,但我不确定这是否会有所作为,并且根据我对 Protobuf 工作原理的理解,它不应该妨碍我编译csvdownload.proto …

go protocol-buffers

3
推荐指数
1
解决办法
2497
查看次数

API 异常过滤器在 ASP.NET MVC 中的异常上返回 200 响应代码

我为我的 API 编写了一个异常过滤器来处理 API 中抛出的异常:

using System;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class ApiExceptionFilter : ExceptionFilterAttribute {

    public override void OnException(ExceptionContext context) {

        // First, generate the status code based on the exception type
        HttpStatusCode status = context.Exception switch {
            UnauthorizedAccessException => HttpStatusCode.Unauthorized,
            ArgumentException => HttpStatusCode.BadRequest,
            MissingHeaderException => HttpStatusCode.BadRequest,
            NotFoundException => HttpStatusCode.NotFound,
            _ => HttpStatusCode.InternalServerError,
        };

        // Next, inject the status code and exception message into
        // a new error response and set the result …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc exception

3
推荐指数
1
解决办法
2487
查看次数

在 Python 中的 F 字符串中引用字符串值

我试图引用我发送到 Python 中的 f 字符串的值之一:

f'This is the value I want quoted: \'{value}\''
Run Code Online (Sandbox Code Playgroud)

这可行,但我想知道是否有一个格式化选项可以为我执行此操作,类似于%qGo 中的工作方式。基本上,我正在寻找这样的东西:

f'This is the value I want quoted: {value:q}'
>>> This is the value I want quoted: 'value'
Run Code Online (Sandbox Code Playgroud)

我也可以使用双引号。这可能吗?

python string-formatting

3
推荐指数
1
解决办法
200
查看次数