小编Phi*_*tin的帖子

获取与网址匹配的Flask视图功能

我有一些网址路径,想检查他们是否指向我的Flask应用中的网址规则.如何使用Flask检查?

from flask import Flask, json, request, Response

app = Flask('simple_app')

@app.route('/foo/<bar_id>', methods=['GET'])
def foo_bar_id(bar_id):
    if request.method == 'GET':
        return Response(json.dumps({'foo': bar_id}), status=200)

@app.route('/bar', methods=['GET'])
def bar():
    if request.method == 'GET':
        return Response(json.dumps(['bar']), status=200)
Run Code Online (Sandbox Code Playgroud)
test_route_a = '/foo/1'  # return foo_bar_id function
test_route_b = '/bar'  # return bar function
Run Code Online (Sandbox Code Playgroud)

python werkzeug flask

10
推荐指数
1
解决办法
4100
查看次数

基于值的线程锁

如果之前有人问过这个问题,请原谅我。我环顾四周,但我觉得我没有合适的词汇来通过搜索网络找到它。

我在 python 中有一个多线程应用程序。我希望能够锁定某个代码块,但只能锁定具有特定条件的其他线程。让我举个例子:有三个线程,thread_athread_bthread_c。每个线程都可以foo随时运行该函数。我不希望任何两个bar彼此相等的线程能够同时访问Code block ALPHA。但是,我不想阻止bar值不同的线程。在这种情况下,假设首先thread_a有一个bar == "cat"和命中线(3)。在thread_ahits line之前(5),让我们说thread_b, with bar == "cat"hits line (3)。我愿意thread_b等待。但如果thread_c出现,与bar == "dog",我希望它能够继续前进。

(1) def foo(bar):
(2)    
(3)     lock(bar)
(4)     # Code block ALPHA (two threads with equivalent bar should not be in here)
(5)     unlock(bar)
Run Code Online (Sandbox Code Playgroud)

另请注意, 的可能值bar是完全不可预测的,但发生碰撞的可能性非常高。

感谢您的任何帮助。我正在查看的库是python …

python multithreading python-multithreading

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

XCTest Assert Expectation没有实现

当使用XCTest和XCTestExpectation编写某个异步测试时,我想声明某个块没有被执行.以下代码成功断言已执行块,如果不是,则测试失败.

#import <XCTest/XCTest.h>
#import "Example.h"

@interface Example_Test : XCTestCase

@property (nonatomic) Example *example;

@end

@implementation Example_Test
- (void)setUp {
    [super setUp];
}

- (void)tearDown {
     [super tearDown];
}

- (void)testExampleWithCompletion {
    self.example = [[Example alloc] init];
    XCTestExpectation *expectation = [self expectationWithDescription:@"expection needs to be fulfilled"];
    [self.example exampleWithCompletion:^{
        [expectation fulfill]
    }];
    [self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
        if (error) {
            NSLog(@"Timeout Error: %@", error);
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

似乎没有一种明显的方法可以反过来执行此操作; 如果块在超时后没有执行则测试成功,如果在超时之前执行则失败.除此之外,我想声明该块在以后满足不同条件时执行.

有没有一种直接的方法来使用XCTestExpectation执行此操作,还是我必须创建一个解决方法?

xcode ios xctest xctestexpectation

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

python:在numpy中将两个一维矩阵相乘

a = np.asarray([1,2,3])

b = np.asarray([2,3,4,5])

a.shape

(3,)

b.shape

(4,)
Run Code Online (Sandbox Code Playgroud)

我想要一个 3 x 4 矩阵,它是 a 和 b 的乘积

1
2    *    2 3 4 5
3
Run Code Online (Sandbox Code Playgroud)

np.dot(a, b.transpose())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: objects are not aligned
Run Code Online (Sandbox Code Playgroud)

当数组是二维时,点积仅相当于矩阵乘法,因此 np.dot 不起作用。

python numpy matrix

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