小编pun*_*lly的帖子

如何在降价单元格ipython/jupyter笔记本中改变颜色?

我只是想在一个单元格中格式化一个特定的字符串.我将该单元格的格式更改为"Markdown",但我不确定如何更改单个单词的文本颜色.

我不想改变整个笔记本的外观(通过CSS文件).

markdown ipython ipython-notebook jupyter-notebook

84
推荐指数
5
解决办法
10万
查看次数

使用API​​RequestFactory测试基于令牌的身份验证的正确方法是什么?

对我的端点的查询工作正常(只要我传递一个有效的令牌),它返回我的响应数据的json表示.

服务api中的代码调用我的端点,在头中传递一个auth令牌:

headers = {'content-type': 'application/json',
           'Authorization': 'Token {}'.format(myToken)}
url = 'http://localhost:8000/my_endpoint/'
r = session.get(url=url, params=params, headers=headers)
Run Code Online (Sandbox Code Playgroud)

在views.py中,我有一个方法装饰器,它在视图上包装dispatch方法(viewsets.ReadOnlyModelViewSet):

def login_required(f):
    def check_login_and_call(request, *args, **kwargs):
        authentication = request.META.get('HTTP_AUTHORIZATION', b'')
        if isinstance(authentication, str):
            authentication = authentication.encode(HTTP_HEADER_ENCODING)
        key = authentication.split()
        if not key or len(key) != 2:
            raise PermissionDenied('Authentication failed.')
        user, token = authenticate_credentials(key[1])
        return f(request, *args, **kwargs)
    return check_login_and_call
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用令牌编写测试来验证请求:

from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory
from rest_framework.test import APITestCase
from rest_framework.test import force_authenticate

class EndpointViewTest(APITestCase):
    def setUp(self):
        self.factory = …
Run Code Online (Sandbox Code Playgroud)

python authentication django integration-testing django-rest-framework

10
推荐指数
3
解决办法
7745
查看次数

用于 POST 请求的 Django rest 框架自定义过滤器

在filters.py 中,我CustomFilter定义了一个类型为ComboSortFilter和的值IntegerListFilter

在views.py 中,我定义了一个ViewSet,filter_class = CustomFilter它对GET请求非常有用,在url 中使用查询字符串参数,使用自定义过滤进行处理。

我还需要支持POST请求,因为有些过滤条件太长,无法放入 URL。

所以我post在我的 ViewSet 中添加了一个方法,我从中提取参数,request.DATA然后过滤,序列化和分页它们......

在views.py中我的ViewSet的post方法中:

queryset = MyModel.objects.filter(**filter_args)
page = self.paginate_queryset(queryset)
serializer = self.get_pagination_serializer(page)
return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)

对于简单的过滤,上述工作正常。但是,我在CustomFilter过滤字段中定义的过滤使用ComboSortFilterIntegerListFilter有点复杂:

在过滤器.py中:

class IntegerListFilter(django_filters.Filter):
    def filter(self, qs, value):
        if value not in (None, ''):
            integers = [int(v) for v in value.split(',')]
            return qs.filter(**{'{0}__{1}'.format(self.name, self.lookup_type): integers})
        return qs

class ComboSortFilter(django_filters.Filter):
    def __init__(self, threshold, lookup_type, order='ASC'): …
Run Code Online (Sandbox Code Playgroud)

python django post filter django-rest-framework

6
推荐指数
1
解决办法
3814
查看次数

枚举是否创建了其参数的副本?

如果我在迭代一个非常大的图形集群列表时使用枚举,我想确保我不会在内存中不必要地创建此列表的任何副本.

我一直在努力确认它不会创建任何副本,但我想肯定.

for i, cluster in enumerate(c):
    # code that does stuff with i and cluster
Run Code Online (Sandbox Code Playgroud)

python enumerate

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