Python寻求转移和寻求结束

时间:2017-05-11 19:28:24

标签: python

嗨,大家好,我是python的初学者, 问题是这个......

所以当我再次打开文件时,我尝试在文件末尾添加换行符。 这是一段代码:

from rest_framework_swagger.views import renderers


class WITSchemaGenerator(SchemaGenerator):
    ''' Custom Schema generator to support parameters for list and detail route '''

    def is_list_view(self, path, method, view):  # pylint: disable=locally-disabled, no-self-use
        """
        Return True if the given path/method appears to represent a list view.
        """
        if hasattr(view, 'action'):
            # Viewsets have an explicitly defined action, which we can inspect.
            return view.action.startswith('list')

        if method.lower() != 'get':
            return False
        path_components = path.strip('/').split('/')
        if path_components and '{' in path_components[-1]:
            return False
        return True

    def get_pagination_fields(self, path, method, view):
        if not self.is_list_view(path, method, view):
            return []

        if not getattr(view, 'pagination_class', None):
            return []

        paginator = view.pagination_class()
        return paginator.get_schema_fields(view)

    def get_filter_fields(self, path, method, view):
        if not self.is_list_view(path, method, view):
            return []

        if not getattr(view, 'filter_backends', None):
            return []

        fields = []
        for filter_backend in view.filter_backends:
            fields += filter_backend().get_schema_fields(view)
        return fields

    def get_custom_fields(self, path, method, view):  # pylint: disable=locally-disabled, no-self-use, unused-argument
        ''' Add custom parameters to schema fields '''
        if not getattr(view, 'get_schema_fields', None):
            return []

        return view.get_schema_fields(view)

    def get_link(self, path, method, view):
        """
        Return a `coreapi.Link` instance for the given endpoint.
        """
        fields = self.get_path_fields(path, method, view)
        fields += self.get_serializer_fields(path, method, view)
        fields += self.get_pagination_fields(path, method, view)
        fields += self.get_filter_fields(path, method, view)
        fields += self.get_custom_fields(path, method, view)

        if fields and any([field.location in ('form', 'body') for field in fields]):
            encoding = self.get_encoding(path, method, view)
        else:
            encoding = None

        description = self.get_description(path, method, view)

        if self.url and path.startswith('/'):
            path = path[1:]

        return coreapi.Link(
            url=urlparse.urljoin(self.url, path),
            action=method.lower(),
            encoding=encoding,
            fields=fields,
            description=description
        )


def get_swagger_view(title=None, url=None, patterns=None, urlconf=None):
    """
    Returns schema view which renders Swagger/OpenAPI.
    """

    class WITSwaggerSchemaView(APIView):
        ''' Custom WIT Swagger Schema View '''
        _ignore_model_permissions = True
        exclude_from_schema = True
        permission_classes = [AllowAny]
        renderer_classes = [
            CoreJSONRenderer,
            renderers.OpenAPIRenderer,
            renderers.SwaggerUIRenderer
        ]

        def get(self, request):  # pylint: disable=locally-disabled, no-self-use
            ''' get a shema to return '''
            generator = WITSchemaGenerator(
                title=title,
                url=url,
                patterns=patterns,
                urlconf=urlconf
            )
            schema = generator.get_schema(request=request)

            if not schema:
                raise exceptions.ValidationError(
                    'The schema generator did not return a schema Document'
                )

            return Response(schema)

    return WITSwaggerSchemaView.as_view()

但它没有用。

感谢他们的帮助。 祝你有愉快的一天。

1 个答案:

答案 0 :(得分:0)

您想要以二进制文件打开文件" b"或" rb"模式。然后,您可以使用seek移动到给定位置。使用上下文管理器也是一个好主意,因此您不必担心在完成后关闭文件。例如 -

with open("file.txt", "rb") as f:
    f.seek(2)
    # do some other stuff with f