How to use the pyquery.pyquery.PyQuery.Fn function in pyquery

To help you get started, we’ve selected a few pyquery examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dsc / pyquery / pyquery / pyquery.py View on Github external
class Fn(object):
        """Hook for defining custom function (like the jQuery.fn)

        >>> PyQuery.fn.listOuterHtml = lambda: this.map(lambda i, el: PyQuery(this).outerHtml())
        &gt;&gt;&gt; S = PyQuery('<ol>   <li>Coffee</li>   <li>Tea</li>   <li>Milk</li>   </ol>')
        &gt;&gt;&gt; S('li').listOuterHtml()
        ['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>']

        """
        def __setattr__(self, name, func):
            def fn(self, *args):
                func_globals(func)['this'] = self
                return func(*args)
            fn.__name__ = name
            setattr(PyQuery, name, fn)
    fn = Fn()

    #####################################################
    # Additional methods that are not in the jQuery API #
    #####################################################

    @property
    def base_url(self):
        """Return the url of current html document or None if not available.
        """
        if self._base_url is not None:
            return self._base_url
        if self._parent is not no_default:
            return self._parent.base_url

    def make_links_absolute(self, base_url=None):
        """Make all links absolute.
github gawel / pyquery / pyquery / pyquery.py View on Github external
&gt;&gt;&gt; fn = lambda: this.map(lambda i, el: PyQuery(this).outerHtml())
         &gt;&gt;&gt; PyQuery.fn.listOuterHtml = fn
         &gt;&gt;&gt; S = PyQuery(
         ...   '<ol>   <li>Coffee</li>   <li>Tea</li>   <li>Milk</li>   </ol>')
         &gt;&gt;&gt; S('li').listOuterHtml()
         ['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>']

        """
        def __setattr__(self, name, func):
            def fn(self, *args, **kwargs):
                func.__globals__['this'] = self
                return func(*args, **kwargs)
            fn.__name__ = name
            setattr(PyQuery, name, fn)
    fn = Fn()

    ########
    # AJAX #
    ########

    @with_camel_case_alias
    def serialize_array(self):
        """Serialize form elements as an array of dictionaries, whose structure
        mirrors that produced by the jQuery API. Notably, it does not handle
        the deprecated `keygen` form element.

            &gt;&gt;&gt; d = PyQuery('<form><input value="spam" name="order"></form>')
            &gt;&gt;&gt; d.serialize_array() == [{'name': 'order', 'value': 'spam'}]
            True
            &gt;&gt;&gt; d.serializeArray() == [{'name': 'order', 'value': 'spam'}]
            True