Commit 30779db6 authored by Georgios D. Tsoukalas's avatar Georgios D. Tsoukalas
Browse files

integrate Args and argmap, change argmap magic pos

parent 414a78a3
......@@ -35,7 +35,7 @@
from random import random, choice, randint
from math import log
from inspect import isclass
from .utils.argmap import argmap_decode
from .utils.argmap import argmap_decode, argmap_check, argmap_dict_to_list
from collections import OrderedDict
......@@ -538,49 +538,43 @@ class ListOf(Canonical):
class Args(Canonical):
def _parse(self, arglist):
formalslen = len(
arglen = len(arglist)
if arglen != formalslen:
raise Exception('param inconsistent')
def _check(self, item):
if argmap_check(item):
if hasattr(item, 'keys') and callable(item.keys):
arglist = argmap_dict_to_list(item)[:-1]
arglist = [(None, item[i]) for i in xrange(0, len(item)-1)]
arglist = OrderedDict(item).items()
except (TypeError, ValueError), e:
m = "%s: %s is not dict-able" % (self, shorts(item))
raise CanonifyException(m)
parsed = OrderedDict()
keys =
kw =
arglen = len(arglist)
if arglen != len(keys):
m = "inconsistent number of parameters: %s != %s" % (
arglen, len(keys))
raise CanonifyException(m)
canonified = OrderedDict()
position = 0
for k, v in arglist:
if k:
parsed[k] =[k].parse(v)
canonified[k] = kw[k].check(v)
# find the right position
for i in range(position, arglen):
key = keys[i]
if not key in parsed.keys():
if not key in canonified.keys():
position = i + 1
else: # exhausted
raise Exception("shouldn't happen")
parsed[key] =[key].parse(v)
return parsed
def _check(self, item):
item = OrderedDict(item)
except TypeError, e:
m = "%s: %s is not dict-able" % (self, shorts(item))
raise CanonifyException(m)
canonified = OrderedDict()
for n, c in
t = item[n] if n in item else None
canonified[n] = c(t)
except KeyError:
m = ("%s: Argument '%s' not found in '%s'"
% (self, shorts(n), shorts(item)))
raise CanonifyException(m)
canonified[key] = kw[key].check(v)
return canonified
......@@ -203,7 +203,6 @@ def argmap_decode_atom(inputf):
def argmap_decode_args(inputf):
append = args.append
s = inputf(1)
key = None
......@@ -215,6 +214,7 @@ def argmap_decode_args(inputf):
if s == ']':
if key is not None:
append((None, key))
return args, None
if s == '=':
......@@ -243,7 +243,7 @@ def argmap_check(obj):
if hasattr(obj, 'keys'):
# this could cover both cases
return ARGMAP_MAGIC in obj
return hasattr(obj, '__len__') and len(obj) and obj[0] == ARGMAP_MAGIC
return hasattr(obj, '__len__') and len(obj) and obj[-1] == ARGMAP_MAGIC
def argmap_unzip_dict(argmap):
if not hasattr(argmap, 'keys'):
......@@ -258,7 +258,7 @@ def argmap_unzip_dict(argmap):
return args, kw
def argmap_unzip_list(argmap):
if not argmap or argmap[0] != ARGMAP_MAGIC:
if not argmap or argmap[-1] != ARGMAP_MAGIC:
m = "argmap unzip list: magic not found"
raise ValueError(m)
......@@ -287,7 +287,7 @@ def argmap_unzip(argmap):
raise ValueError(m)
def argmap_zip_list(args, kw):
return [ARGMAP_MAGIC] + [(None, a) for a in args] + kw.items()
return [(None, a) for a in args] + kw.items() + [ARGMAP_MAGIC]
def argmap_zip_dict(args, kw):
argmap = OrderedDict()
......@@ -298,3 +298,13 @@ def argmap_zip_dict(args, kw):
argmap_zip = argmap_zip_list
def argmap_list_to_dict(argmap):
args, kw = argmap_unzip_list(argmap)
kw[None] = args
return kw
def argmap_dict_to_list(argmap):
args, kw = argmap_unzip_dict(argmap)
return args + kw.items() + [ARGMAP_MAGIC]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment