Skip to content

Commit

Permalink
arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Jun 2, 2019
1 parent defb770 commit 0a3c3ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions scripts/build_and_upload_package.sh
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
scripts/clean_package.sh
#scripts/clean_package.sh
#python2 setup.py bdist
#python2 setup.py bdist_wheel
#python3 setup.py bdist
python3 setup.py bdist_wheel
twine upload dist/*
scripts/clean_package.sh
#scripts/clean_package.sh
30 changes: 29 additions & 1 deletion warg/arguments.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
from collections import namedtuple
from pathlib import Path, PosixPath
from warnings import warn

Expand Down Expand Up @@ -170,7 +171,34 @@ def check_for_duplicates_in_args(**kwargs) -> None:
def namedtuple_args(n_tuple):
@wrapt.decorator(adapter=n_tuple)
def wrapper(wrapped, instance, args, kwargs):
n = n_tuple(*args, **kwargs)
if isinstance(args[0], n_tuple):
n = args[0]
else:
n = n_tuple(*args, **kwargs)
return wrapped(n)

return wrapper


if __name__ == "__main__":

c = namedtuple("C", ("a", "b"))

@namedtuple_args(c)
def add(v):
return v.a + v.b

def add2(a, b):
return a + b

h = add(2, 2)
print(h)

j = add(c(1, 4))
print(j)

wq = add2(2, 4)
print(wq)

wc = add2(*c(4, 3))
print(wc)
16 changes: 14 additions & 2 deletions warg/named_ordered_dictionary.py
Expand Up @@ -23,7 +23,7 @@

class IllegalAttributeKey(Exception):
def __init__(self, key, type):
msg = f'Overwritting of attribute "{key}" on type "{type}" is not allowed'
msg = f'Overwriting of attribute "{key}" on type "{type}" is not allowed'
Exception.__init__(self, msg)


Expand Down Expand Up @@ -203,6 +203,12 @@ def __setitem__(self, key, value):
def keys(self):
return self.__dict__.keys()

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

def values(self):
return self.__dict__.values()

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

Expand Down Expand Up @@ -278,6 +284,12 @@ def __truediv__(self, other):
def __floordiv__(self, other):
return self.__truediv__(other)

def __setstate__(self, state):
self.__dict__ = state

def __getstate__(self):
return self.__dict__


NOD = NamedOrderedDictionary

Expand Down Expand Up @@ -352,4 +364,4 @@ def __floordiv__(self, other):

a = NamedOrderedDictionary(4, 2)
b = a + a
assert a.as_list() == [8, 4]
assert b.as_list() == [8, 4], b

0 comments on commit 0a3c3ca

Please sign in to comment.