how do I make a class return as Namespace(filter='b', list="['hi','by']")

class NameSpace(object):
    """Mock for argparse's NameSpace
    """
    def __init__(self, lst=None, flter=None):
        super(NameSpace, self).__init__()
        self._filter = flter
        self._lst = lst

Is it possible what I am thinking, i guess we have to do something using special method call ?

Recommended Answers

All 7 Replies

i mean object that returns of the type <class 'argparse.Namespace'>

but dont have to use argparse.. just return Namespace(filter='b', list="['hi','by']") as output with filter and list passed at the time of object creation ...

define a __repr__() method.

Why don't you simply call Namespace's constructor ?

import argparse
obj = argparse.Namespace(filter='b', list="['hi','by']")

what if argparse is not there ?

this is what gives me exactly what i want, the tip was enough.

class NameSpace(object):
    """Mock for argparse's NameSpace
    """
    def __init__(self, lst=None, flter=None):
        super(NameSpace, self).__init__()
        self.filter = flter
        self.lst = lst

    def __repr__(self):
        return 'NameSpace(filter=self.filter, lst = self.lst)'

args = NameSpace(lst='["Hi","By"]', flter='B')
print args, type(args), args.lst, args.filter

However I wanted to know if this is equivalent to say mocking a argparse.Namespace behaviour too?

In python 2.7, the Namespace class is defined with this code

class _AttributeHolder(object):
    """Abstract base class that provides __repr__.

    The __repr__ method returns a string in the format::
        ClassName(attr=name, attr=name, ...)
    The attributes are determined either by a class-level attribute,
    '_kwarg_names', or by inspecting the instance __dict__.
    """

    def __repr__(self):
        type_name = type(self).__name__
        arg_strings = []
        for arg in self._get_args():
            arg_strings.append(repr(arg))
        for name, value in self._get_kwargs():
            arg_strings.append('%s=%r' % (name, value))
        return '%s(%s)' % (type_name, ', '.join(arg_strings))

    def _get_kwargs(self):
        return sorted(self.__dict__.items())

    def _get_args(self):
        return []

class Namespace(_AttributeHolder):
    """Simple object for storing attributes.

    Implements equality by attribute names and values, and provides a simple
    string representation.
    """

    def __init__(self, **kwargs):
        for name in kwargs:
            setattr(self, name, kwargs[name])

    __hash__ = None

    def __eq__(self, other):
        return vars(self) == vars(other)

    def __ne__(self, other):
        return not (self == other)

    def __contains__(self, key):
        return key in self.__dict__

Use this code in your program to mock Namespace

hmm, I looked at it in the argparse module. However is it incorrect to mock the way I did ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.