当前位置: 首页>>代码示例>>Python>>正文


Python URLObject.with_fragment方法代码示例

本文整理汇总了Python中urlobject.URLObject.with_fragment方法的典型用法代码示例。如果您正苦于以下问题:Python URLObject.with_fragment方法的具体用法?Python URLObject.with_fragment怎么用?Python URLObject.with_fragment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urlobject.URLObject的用法示例。


在下文中一共展示了URLObject.with_fragment方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: URLObjectRelativeTest

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_fragment [as 别名]
class URLObjectRelativeTest(unittest.TestCase):

    def setUp(self):
        self.url = URLObject("https://github.com/zacharyvoase/urlobject?spam=eggs#foo")

    def test_relative_with_scheme_returns_the_given_URL(self):
        assert self.url.relative('http://example.com/abc') == 'http://example.com/abc'

    def test_relative_with_netloc_returns_the_given_URL_but_preserves_scheme(self):
        assert self.url.relative('//example.com/abc') == 'https://example.com/abc'

    def test_relative_with_path_replaces_path_and_removes_query_string_and_fragment(self):
        assert self.url.relative('another-project') == 'https://github.com/zacharyvoase/another-project'
        assert self.url.relative('.') == 'https://github.com/zacharyvoase/'
        assert self.url.relative('/dvxhouse/intessa') == 'https://github.com/dvxhouse/intessa'
        assert self.url.relative('/dvxhouse/intessa') == 'https://github.com/dvxhouse/intessa'

    def test_relative_with_empty_string_removes_fragment_but_preserves_query(self):
        # The empty string is treated as a path meaning 'the current location'.
        assert self.url.relative('') == self.url.without_fragment()

    def test_relative_with_query_string_removes_fragment(self):
        assert self.url.relative('?name=value') == self.url.without_fragment().with_query('name=value')

    def test_relative_with_fragment_removes_nothing(self):
        assert self.url.relative('#foobar') == self.url.with_fragment('foobar')

    def test_compound_relative_urls(self):
        assert self.url.relative('//example.com/a/b') == 'https://example.com/a/b'
        assert self.url.relative('//example.com/a/b#bar') == 'https://example.com/a/b#bar'
        assert self.url.relative('//example.com/a/b?c=d#bar') == 'https://example.com/a/b?c=d#bar'
        assert self.url.relative('/a/b?c=d#bar') == 'https://github.com/a/b?c=d#bar'
        assert self.url.relative('?c=d#bar') == 'https://github.com/zacharyvoase/urlobject?c=d#bar'
        assert self.url.relative('#bar') == 'https://github.com/zacharyvoase/urlobject?spam=eggs#bar'
开发者ID:vmalloc,项目名称:urlobject,代码行数:36,代码来源:urlobject_test.py

示例2: URLObjectModificationTest

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_fragment [as 别名]

#.........这里部分代码省略.........
        assert (self.url.with_port(24) ==
                'https://github.com:24/zacharyvoase/urlobject?spam=eggs#foo')

    def test_with_port_replaces_port_number(self):
        url = URLObject('https://github.com:59/')
        assert url.with_port(67) == 'https://github.com:67/'

    def test_without_port_removes_port_number(self):
        url = URLObject('https://github.com:59/')
        assert url.without_port() == 'https://github.com/'

    def test_with_path_replaces_path(self):
        assert (self.url.with_path('/dvxhouse/intessa') ==
                'https://github.com/dvxhouse/intessa?spam=eggs#foo')

    def test_root_goes_to_root_path(self):
        assert self.url.root == 'https://github.com/?spam=eggs#foo'

    def test_parent_jumps_up_one_level(self):
        url = URLObject('https://github.com/zacharyvoase/urlobject')
        assert url.parent == 'https://github.com/zacharyvoase/'
        assert url.parent.parent == 'https://github.com/'

    def test_add_path_segment_adds_a_path_segment(self):
        url = URLObject('https://github.com/zacharyvoase/urlobject')
        assert (url.add_path_segment('tree') ==
                'https://github.com/zacharyvoase/urlobject/tree')
        assert (url.add_path_segment('tree/master') ==
                'https://github.com/zacharyvoase/urlobject/tree%2Fmaster')

    def test_add_path_adds_a_partial_path(self):
        url = URLObject('https://github.com/zacharyvoase/urlobject')
        assert (url.add_path('tree') ==
                'https://github.com/zacharyvoase/urlobject/tree')
        assert (url.add_path('tree/master') ==
                'https://github.com/zacharyvoase/urlobject/tree/master')

    def test_is_leaf(self):
        assert URLObject('https://github.com/zacharyvoase/urlobject').is_leaf
        assert not URLObject('https://github.com/zacharyvoase/').is_leaf

    def test_with_query_replaces_query(self):
        assert (self.url.with_query('spam-ham-eggs') ==
                'https://github.com/zacharyvoase/urlobject?spam-ham-eggs#foo')

    def test_without_query_removes_query(self):
        assert (self.url.without_query() ==
                'https://github.com/zacharyvoase/urlobject#foo')

    def test_add_query_param_adds_one_query_parameter(self):
        assert (self.url.add_query_param('spam', 'ham') ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham#foo')

    def test_add_query_params_adds_multiple_query_parameters(self):
        assert (self.url.add_query_params([('spam', 'ham'), ('foo', 'bar')]) ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham&foo=bar#foo')

    def test_add_query_params_with_multiple_values_adds_the_same_query_parameter_multiple_times(self):
        assert (self.url.add_query_params({'foo': ['bar', 'baz']}) ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs&foo=bar&foo=baz#foo')

    def test_set_query_param_adds_or_replaces_one_query_parameter(self):
        assert (self.url.set_query_param('spam', 'ham') ==
                'https://github.com/zacharyvoase/urlobject?spam=ham#foo')

    def test_set_query_params_adds_or_replaces_multiple_query_parameters(self):
        assert (self.url.set_query_params({'foo': 'bar'}, spam='ham') ==
                'https://github.com/zacharyvoase/urlobject?foo=bar&spam=ham#foo')

    def test_set_query_params_with_multiple_values_adds_or_replaces_the_same_parameter_multiple_times(self):
        assert (self.url.set_query_params({'spam': ['bar', 'baz']}) ==
                'https://github.com/zacharyvoase/urlobject?spam=bar&spam=baz#foo')
        assert (self.url.set_query_params({'foo': ['bar', 'baz']}) ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs&foo=bar&foo=baz#foo')
        # Ensure it removes all appearances of an existing name before adding
        # the new ones.
        url = URLObject('https://github.com/zacharyvoase/urlobject?foo=bar&foo=baz#foo')
        assert (url.set_query_params({'foo': ['spam', 'ham']}) ==
                'https://github.com/zacharyvoase/urlobject?foo=spam&foo=ham#foo')

    def test_del_query_param_removes_one_query_parameter(self):
        assert (self.url.del_query_param('spam') ==
                'https://github.com/zacharyvoase/urlobject#foo')

    def test_del_query_params_removes_multiple_query_parameters(self):
        url = URLObject('https://github.com/zacharyvoase/urlobject?foo=bar&baz=spam#foo')
        assert (url.del_query_params(['foo', 'baz']) ==
                'https://github.com/zacharyvoase/urlobject#foo')

    def test_with_fragment_replaces_fragment(self):
        assert (self.url.with_fragment('part') ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs#part')

    def test_with_fragment_encodes_fragment_correctly(self):
        assert (self.url.with_fragment('foo bar#baz') ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs#foo%20bar%23baz')

    def test_without_fragment_removes_fragment(self):
        assert (self.url.without_fragment() ==
                'https://github.com/zacharyvoase/urlobject?spam=eggs')
开发者ID:vmalloc,项目名称:urlobject,代码行数:104,代码来源:urlobject_test.py

示例3: render

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_fragment [as 别名]
    def render(self, context):

        kwargs = MultiValueDict()
        for key in self.kwargs:
            key = smart_str(key, 'ascii')
            values = [value.resolve(context) for value in self.kwargs.getlist(key)]
            kwargs.setlist(key, values)

        if 'base' in kwargs:
            url = URLObject.parse(kwargs['base'])
        else:
            url = URLObject(scheme='http')

        if 'secure' in kwargs:
            if convert_to_boolean(kwargs['secure']):
                url = url.with_scheme('https')
            else:
                url = url.with_scheme('http')

        if 'query' in kwargs:
            query = kwargs['query']
            if isinstance(query, basestring):
                query = render_template_from_string_without_autoescape(query, context)
            url = url.with_query(query)

        if 'add_query' in kwargs:
            for query_to_add in kwargs.getlist('add_query'):
                if isinstance(query_to_add, basestring):
                    query_to_add = render_template_from_string_without_autoescape(query_to_add, context)
                    query_to_add = dict(decode_query(query_to_add))
                for key, value in query_to_add.items():
                    url = url.add_query_param(key, value)

        if 'scheme' in kwargs:
            url = url.with_scheme(kwargs['scheme'])

        if 'host' in kwargs:
            url = url.with_host(kwargs['host'])

        if 'path' in kwargs:
            url = url.with_path(kwargs['path'])

        if 'add_path' in kwargs:
            for path_to_add in kwargs.getlist('add_path'):
                url = url.add_path_component(path_to_add)

        if 'fragment' in kwargs:
            url = url.with_fragment(kwargs['fragment'])

        if 'port' in kwargs:
            url = url.with_port(kwargs['port'])

        # sensible default
        if not url.host:
            url = url.with_scheme('')

        # Convert the URLObject to its unicode representation
        url = unicode(url)

        # Handle escaping. By default, use the value of
        # context.autoescape. This can be overridden by
        # passing an "autoescape" keyword to the tag.
        if 'autoescape' in kwargs:
            autoescape = convert_to_boolean(kwargs['autoescape'])
        else:
            autoescape = context.autoescape

        if autoescape:
            url = escape(url)

        if self.asvar:
            context[self.asvar] = url
            return ''

        return url
开发者ID:selwin,项目名称:django-spurl,代码行数:77,代码来源:spurl.py

示例4: SpurlURLBuilder

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_fragment [as 别名]

#.........这里部分代码省略.........
    def handle_scheme_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_scheme(url.scheme)

    def handle_host(self, value):
        host = self.prepare_value(value)
        self.url = self.url.with_hostname(host)

    def handle_host_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_hostname(url.hostname)

    def handle_path(self, value):
        path = self.prepare_value(value)
        self.url = self.url.with_path(path)

    def handle_path_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_path(url.path)

    def handle_add_path(self, value):
        path_to_add = self.prepare_value(value)
        self.url = self.url.add_path(path_to_add)

    def handle_add_path_from(self, value):
        url = URLObject(value)
        path_to_add = url.path
        if path_to_add.startswith('/'):
            path_to_add = path_to_add[1:]
        self.url = self.url.add_path(path_to_add)

    def handle_fragment(self, value):
        fragment = self.prepare_value(value)
        self.url = self.url.with_fragment(fragment)

    def handle_fragment_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_fragment(url.fragment)

    def handle_port(self, value):
        self.url = self.url.with_port(int(value))

    def handle_port_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_port(url.port)

    def handle_autoescape(self, value):
        self.autoescape = convert_to_boolean(value)

    def set_sensible_defaults(self):
        if self.url.hostname and not self.url.scheme:
            self.url = self.url.with_scheme('http')

    def prepare_value(self, value):
        """Prepare a value by unescaping embedded template tags
        and rendering through Django's template system"""
        if isinstance(value, six.string_types):
            value = self.render_template(self.unescape_tags(value))
        return value

    def unescape_tags(self, template_string):
        """Spurl allows the use of templatetags inside templatetags, if
        the inner templatetags are escaped - {\% and %\}"""
        return template_string.replace('{\%', '{%').replace('%\}', '%}')

    def compile_string(self, template_string, origin):
开发者ID:albertkoch,项目名称:django-spurl,代码行数:70,代码来源:spurl.py

示例5: SpurlURLBuilder

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_fragment [as 别名]

#.........这里部分代码省略.........
    def handle_scheme_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_scheme(url.scheme)

    def handle_host(self, value):
        host = self.prepare_value(value)
        self.url = self.url.with_hostname(host)

    def handle_host_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_hostname(url.hostname)

    def handle_path(self, value):
        path = self.prepare_value(value)
        self.url = self.url.with_path(path)

    def handle_path_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_path(url.path)

    def handle_add_path(self, value):
        path_to_add = self.prepare_value(value)
        self.url = self.url.add_path(path_to_add)

    def handle_add_path_from(self, value):
        url = URLObject(value)
        path_to_add = url.path
        if path_to_add.startswith('/'):
            path_to_add = path_to_add[1:]
        self.url = self.url.add_path(path_to_add)

    def handle_fragment(self, value):
        fragment = self.prepare_value(value)
        self.url = self.url.with_fragment(fragment)

    def handle_fragment_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_fragment(url.fragment)

    def handle_port(self, value):
        self.url = self.url.with_port(int(value))

    def handle_port_from(self, value):
        url = URLObject(value)
        self.url = self.url.with_port(url.port)

    def handle_autoescape(self, value):
        self.autoescape = convert_to_boolean(value)

    def set_sensible_defaults(self):
        if self.url.hostname and not self.url.scheme:
            self.url = self.url.with_scheme('http')

    def prepare_value(self, value):
        """Prepare a value by unescaping embedded template tags
        and rendering through Django's template system"""
        if isinstance(value, basestring):
            value = self.unescape_tags(value)
            value = self.render_template(value)
        return value

    def unescape_tags(self, template_string):
        """Spurl allows the use of templatetags inside templatetags, if
        the inner templatetags are escaped - {\% and %\}"""
        return template_string.replace('{\%', '{%').replace('%\}', '%}')
开发者ID:hzlf,项目名称:openbroadcast.org,代码行数:69,代码来源:spurl.py


注:本文中的urlobject.URLObject.with_fragment方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。