view MoinMoin/converter/_args.py @ 2940:a3299ab946b6 reyha_verma/fixes-482-1426427038125

close unwanted branch rey...
author RogerHaase <haaserd@gmail.com>
date Tue, 28 Apr 2015 12:52:42 -0700
parents 791bdedb0c20
children
line wrap: on
line source
# Copyright: 2009 MoinMoin:BastianBlank
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - Arguments wrapper
"""


class Arguments(object):
    """
    Represent an argument list that may contain positional or keyword args.
    """
    __slots__ = 'positional', 'keyword'

    def __init__(self, positional=None, keyword=None):
        self.positional = positional and positional[:] or []
        self.keyword = keyword and keyword.copy() or {}

    def __contains__(self, key):
        """
        Check for positional argument or keyword key presence.
        """
        return key in self.positional or key in self.keyword

    def __getitem__(self, key):
        """
        Access positional arguments by index or keyword args by key name.
        """
        if isinstance(key, (int, slice)):
            return self.positional[key]
        return self.keyword[key]

    def __len__(self):
        """
        Total count of positional + keyword args.
        """
        return len(self.positional) + len(self.keyword)

    def __repr__(self):
        return '<{0}({1!r}, {2!r})>'.format(self.__class__.__name__, self.positional, self.keyword)

    def items(self):
        """
        Return an iterator over all (key, value) pairs.
        Positional arguments are assumed to have a None key.
        """
        for value in self.positional:
            yield None, value
        for item in self.keyword.iteritems():
            yield item

    def keys(self):
        """
        Return an iterator over all keys from the keyword arguments.
        """
        for key in self.keyword.iterkeys():
            yield key

    def values(self):
        """
        Return an iterator over all values.
        """
        for value in self.positional:
            yield value
        for value in self.keyword.itervalues():
            yield value