Skip to content

Instantly share code, notes, and snippets.

@sanfx
Last active May 10, 2016 09:59
Show Gist options
  • Save sanfx/7037550 to your computer and use it in GitHub Desktop.
Save sanfx/7037550 to your computer and use it in GitHub Desktop.
mock argparse's Namespace behaviour.
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
# a better way
import collections
MockNamespace = collections.namedtuple("MockNamespace", ["fltr"])
MockNamespace('some/path')
# will resultMockNamespace(fltr='some/path')
@endrebak
Copy link

Dang, namedtuple is what I was looking for. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment