小编fil*_*yoo的帖子

Python Networkx:在multiDiGraph中查找给定路径的所有边

在我的多向图中,我想找到 2 个节点之间可能的所有(简单)路径。我设法获得了所有路径,但无法区分源节点到达目标节点所需的边(假设它是一个多向图)。

例如,我有 A->B->C,其中 (A,B) 和 (B,C) 之间有多个平行边。如果我说 A->B 有 5 条平行边,B->C 有 2 条平行边,则 all_simple_path(graph, source='A', target='C') 将总共返回 7 条路径,全部为课程 A->B->C

使用 get_edge_data() 时,它返回每个节点之间的所有平行边。但我想要的是能够列出路径中指定节点采用的所有组合边。

谢谢 !

python graph edges networkx

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

Django REST 框架:HTML 渲染通用 APIView

我是 Django REST 框架的新手。我尝试做的是在 HTML 中呈现 Generic APIView (RetrieveUpdateDestroyAPIView),类似于在 Browsable API 中自动呈现 ViewSet 的方式。

按照官方文档,我在 myApp/ views.py中有:

class AnnounceViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows announces to be viewed or edited.
    """
    queryset = Announce.objects.all()
    serializer_class = AnnounceSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,
                          IsOwnerOrReadOnly,)


    def perform_create(self, serializer): # without this, the POST request of the announce doesnt work
        serializer.save(owner=self.request.user)


class AnnounceList(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'myApp/announces_list.html'
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def get(self, request):
        queryset = Announce.objects.all()
        return Response({'announces': queryset})

class …
Run Code Online (Sandbox Code Playgroud)

html python django rest django-rest-framework

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

React Redux:使用Enzyme / Jest测试mapStateToProps和mapDispatchToProps

所以,我想测试mapStateToProps,并mapDispatchToProps与酶/玩笑。

我有一个像这样的DrawerAvatar组件:

DrawerAvatar.js

const mapStateToProps = state => ({
  isAuthenticated: state.authReducer.isAuthenticated
});

export default compose(
  connect(mapStateToProps, null)
)(DrawerAvatar);
Run Code Online (Sandbox Code Playgroud)

DrawerAvatar.test.js

import configureMockStore from 'redux-mock-store';
import connectedDrawerAvatar, { DrawerAvatar } from './DrawerAvatar';

const mockStore = configureMockStore();

it('mapStateToProps should return the right value', () => {
  const initialState = {
    someState: 123
  };
  const store = mockStore(initialState);
  const wrapper = shallow(<connectedDrawerAvatar store={store} />);
  expect(wrapper.props().someState).toBe(123);
});
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用,因为会wrapper.props().someState返回undefined...因此,我不知道如何使用连接的组件来测试mapStatesToProps和redux-mock-store。

我也不知道怎么测试mapDispatchToProps ..!我已经尝试了此博客中提供的方法,但是它不起作用。

非常感谢你 !

编辑:这有效,但是我不确定它是否真的测试 …

reactjs jestjs redux enzyme react-redux

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

Django:获取特定用户的所有对象

尝试显示用户的所有Announce对象时出现问题.我的问题是:考虑我以id = 1的用户身份登录.当我去/ users/1 /它显示我的所有帖子.但问题是当我想通过转到/ users/2 /来显示id = 2的其他用户的所有帖子时,它仍然显示我的所有Announce对象,而不是id = 2的用户.

models.py

class Announce(models.Model):
    owner        = models.ForeignKey('auth.User', related_name='announces')
    created_date = models.DateTimeField(auto_now_add=True)
    body         = models.TextField(max_length=1000)
Run Code Online (Sandbox Code Playgroud)

views.py

class UserAnnouncesList(ListView):
    model = Announce
    template_name = 'myApp/user_announces_list.html'
    context_object_name = 'all_announces_by_user'

    def get_queryset(self):
        return Announce.objects.filter(owner=self.request.user)
Run Code Online (Sandbox Code Playgroud)

urls.py

urlpatterns = [
url(r'users/(?P<pk>[0-9]+)/$', views.UserAnnouncesList.as_view(), name='user_announces_list'),]
Run Code Online (Sandbox Code Playgroud)

user_announces_list.html

{% extends "account/base.html" %}

{% block content %}
{% for announce in all_announces_by_user %}
<h1>{{announce.user.username}}</h1>
    <p>{{announce.body}}</p>

{% endfor %}
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)

我是否必须使用某种类似的:Announce.objects.get(pk = ???)?

我感谢您的帮助!

python django listview django-queryset django-related-manager

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