小编Sar*_*rit的帖子

定义typing.Dict和dict之间的区别?

我正在练习使用Python 3.5中的类型提示.我的一位同事使用typing.Dict:

import typing


def change_bandwidths(new_bandwidths: typing.Dict,
                      user_id: int,
                      user_name: str) -> bool:
    print(new_bandwidths, user_id, user_name)
    return False


def my_change_bandwidths(new_bandwidths: dict,
                         user_id: int,
                         user_name: str) ->bool:
    print(new_bandwidths, user_id, user_name)
    return True


def main():
    my_id, my_name = 23, "Tiras"
    simple_dict = {"Hello": "Moon"}
    change_bandwidths(simple_dict, my_id, my_name)
    new_dict = {"new": "energy source"}
    my_change_bandwidths(new_dict, my_id, my_name)

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

它们都工作得很好,似乎没有区别.

我已经阅读了typing模块文档.

之间typing.Dictdict哪一个,我应该在程序中使用?

python dictionary type-hinting

58
推荐指数
4
解决办法
2万
查看次数

string_agg没有函数匹配给定的名称

我有关系数据库,并希望使用,string_agg()因为它似乎适合我的需要.

我想要 :

product_id | quiz_id
-----------+----------
         1 | 1,6
         2 | 2,7
         3 | 3,8
         4 | 4
Run Code Online (Sandbox Code Playgroud)

这是我的数据库.

    select quiz_id , product_id, lastmodified from dugong.quiz;
 quiz_id | product_id |         lastmodified          
---------+------------+-------------------------------
       1 |          1 | 2015-11-11 14:46:55.619162+07
       2 |          2 | 2015-11-11 14:46:55.619162+07
       3 |          3 | 2015-11-11 14:46:55.619162+07
       4 |          4 | 2015-11-11 14:46:55.619162+07
       5 |          5 | 2015-11-11 14:46:55.619162+07
       6 |          1 | 2015-11-11 14:46:55.619162+07
       7 |          2 | 2015-11-11 14:46:55.619162+07
       8 |          3 …
Run Code Online (Sandbox Code Playgroud)

postgresql

20
推荐指数
1
解决办法
2万
查看次数

如何删除(卸载)特定资源上的舵图

我已经安装了redis. 我的默认名字是plinking-narwhal. 现在我想用我指定的名称安装一个服务。但首先我想删除现有的。我曾尝试删除它们但没有成功。

$ kubectl get all
NAME                                               READY     STATUS    RESTARTS   AGE
pod/plinking-narwhal-redis-master-0                1/1       Running   0          12m
pod/plinking-narwhal-redis-slave-9b645b597-2vh82   1/1       Running   7          12m

NAME                                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/kubernetes                      ClusterIP   10.96.0.1        <none>        443/TCP    15m
service/plinking-narwhal-redis-master   ClusterIP   10.109.186.189   <none>        6379/TCP   12m
service/plinking-narwhal-redis-slave    ClusterIP   10.99.122.12     <none>        6379/TCP   12m

NAME                                           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/plinking-narwhal-redis-slave   1         1         1            1           12m

NAME                                                     DESIRED   CURRENT   READY     AGE
replicaset.apps/plinking-narwhal-redis-slave-9b645b597   1         1         1         12m

NAME                                             DESIRED   CURRENT   AGE
statefulset.apps/plinking-narwhal-redis-master   1         1         12m …
Run Code Online (Sandbox Code Playgroud)

redis kubernetes kubernetes-helm

16
推荐指数
2
解决办法
4万
查看次数

Django测试VS pytest


我是新来的Django unittestpytest.但是,我开始觉得pytest测试用例更紧凑,更清晰.

这是我的测试用例:

class OrderEndpointTest(TestCase):
    def setUp(self):
        user = User.objects.create_superuser(username='admin', password='password', email='pencil@gmail.com')
        mommy.make(CarData, _quantity=1)
        mommy.make(UserProfile, _quantity=1, user=user)

    def test_get_order(self):
        mommy.make(Shop, _quantity=1)
        mommy.make(Staff, _quantity=1, shop=Shop.objects.first())
        mommy.make(Order, _quantity=1, car_info={"color": "Black"}, customer={"name": "Lord Elcolie"},
                   staff=Staff.objects.first(), shop=Shop.objects.first())

        factory = APIRequestFactory()
        user = User.objects.get(username='admin')
        view = OrderViewSet.as_view({'get': 'list'})

        request = factory.get('/api/orders/')
        force_authenticate(request, user=user)
        response = view(request)
        assert 200 == response.status_code
        assert 1 == len(response.data.get('results'))
Run Code Online (Sandbox Code Playgroud)

这是pytest版本

def test_get_order(car_data, admin_user, orders):
    factory = APIRequestFactory()
    user = User.objects.get(username='admin')
    view = OrderViewSet.as_view({'get': …
Run Code Online (Sandbox Code Playgroud)

python django pytest django-unittest pytest-django

12
推荐指数
1
解决办法
3691
查看次数

Runner 不健康,将被禁用

我用它托管我的存储库gitlab.com并安装runnerDigitalOcean. 它一直运行到今天泰国时间 16March2019 14:24。

# gitlab-runner status
Runtime platform                                    arch=amd64 os=linux pid=16937 revision=4745a6f3 version=11.8.0
gitlab-runner: Service is running!
# gitlab-runner unregister --all-runners
Runtime platform                                    arch=amd64 os=linux pid=16299 revision=4745a6f3 version=11.8.0
Running in system-mode.

WARNING: Unregistering all runners
ERROR: Unregistering runner from GitLab forbidden   runner=2bcd7af4
ERROR: Failed to unregister runner HerrRunner
# gitlab-runner list
Runtime platform                                    arch=amd64 os=linux pid=16346 revision=4745a6f3 version=11.8.0
Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml
HerrRunner                                          Executor=shell Token=2bcd7af455f866ede7991992a68780 URL=https://gitlab.com/
# gitlab-runner --debug run
Runtime platform                                    arch=amd64 …
Run Code Online (Sandbox Code Playgroud)

linux gitlab gitlab-ci gitlab-ci-runner

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

软件包“docker.io”没有安装候选者

当我尝试通过输入以下命令在 debian 6 中安装 docker.io 时: apt-get install docker.io 结果是:

E:软件包“docker.io”没有安装候选者

解决办法是什么?谢谢

linux debian apt

8
推荐指数
2
解决办法
2万
查看次数

如何在容器内创建postgres扩展?

我必须安装hstore到我的docker postgres
这是我在shell中的普通命令来执行我的需要

psql -d template1 -c 'create extension hstore';
Run Code Online (Sandbox Code Playgroud)

如果我删除该行我的容器,它可以工作,但我自己执行hstore安装,我必须告诉我的项目中的每个人都这样做,这不是一个好的做法

这是我的 yml file

  postgres:
    build:
      context: .
      dockerfile: dockerfiles/devdb.dockerfile
    environment:
      POSTGRES_USER: uih
      POSTGRES_PASSWORD: uIhbod!
      POSTGRES_DB: uih_portal
    ports:
        - "5433:5432"
Run Code Online (Sandbox Code Playgroud)

这是我的docker文件 devdb.dockerfile

FROM postgres:9.5

RUN mkdir -p /var/lib/postgresql-static/data
ENV PGDATA /var/lib/postgresql-static/data

# psql -d template1 -c 'create extension hstore;'
CMD ["psql", "-d", "template1", "-c", "'create extension hstore;'"]

RUN echo "hstore extension installed"
Run Code Online (Sandbox Code Playgroud)

构建后我无法运行它

$ docker-compose up postgres
Recreating uihportal_postgres_1
Attaching to uihportal_postgres_1
postgres_1       | psql: could …
Run Code Online (Sandbox Code Playgroud)

postgresql docker

8
推荐指数
1
解决办法
6682
查看次数

如何捕获`botocore.errorfactory.UserNotFoundException`?

我正在使用AWS Cognito制作OAuth服务器。我现在正在创建异常处理程序以防使用不存在,但requests打算获得一个

ipdb> pk
'David'
ipdb> res = self.cognito_client.admin_get_user(
            UserPoolId=settings.AWS_USER_POOL_ID,
            Username=pk
        )
*** botocore.errorfactory.UserNotFoundException: An error occurred (UserNotFoundException) when calling the AdminGetUser operation: User does not exist.
Traceback (most recent call last):
  File "/Users/sarit/.pyenv/versions/futuready-titan/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/sarit/.pyenv/versions/futuready-titan/lib/python3.8/site-packages/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
Run Code Online (Sandbox Code Playgroud)
boto3==1.12.15            # via -r el.in
botocore==1.15.15         # via boto3, s3transfer
django==3.0.3
python3.8.1
Run Code Online (Sandbox Code Playgroud)

我已经检查过botocore 源代码 UserNotFoundException

问题:
我怎么能具体说catch这个exception

python botocore boto3

8
推荐指数
3
解决办法
2767
查看次数

如何遵循提示:改用可调用对象,例如,使用`dict` 而不是`{}`?

如何遵循警告?

models.py

from django.contrib.postgres.fields import JSONField
from django.db import models
from django_extensions.db.models import TimeStampedModel


class UnderwritingValidator(TimeStampedModel):
    plan = models.PositiveIntegerField(null=True, blank=True, unique=True)
    logic = JSONField(default=dict(
        accept_list=[],
        reject_list=[]
    ))
Run Code Online (Sandbox Code Playgroud)

然后 makemigrations

WARNINGS:
uw_validators.UnderwritingValidator.logic: (postgres.E003) JSONField default should be a callable instead of an instance so that it's not shared between all field instances.
    HINT: Use a callable instead, e.g., use `dict` instead of `{}`.
Migrations for 'uw_validators':
  uw_validators/migrations/0002_auto_20191011_0321.py
    - Remove field accept_list from underwritingvalidator
    - Remove field reject_list from underwritingvalidator
    - Add …
Run Code Online (Sandbox Code Playgroud)

python django postgresql

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

Django 3 ASGI 和 Django 2 + Channels 有什么不同?

django-channels是我将在新的一年里学习的第一个材料清单。但是 Django 3 也有 ASGI 功能,没有任何文档。那么我怀疑django-channels用例 VS Django 3 ASGI之间有什么不同?

python django websocket django-channels asgi

7
推荐指数
0
解决办法
960
查看次数