Skip to content

Commit

Permalink
Handle views in util functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Mar 6, 2018
1 parent 6e2fecb commit 185e279
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 1 addition & 3 deletions law/job/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,7 @@ def encode_bool(cls, value):
@classmethod
def encode_list(cls, value):
encoded = base64.b64encode(six.b(" ".join(str(v) for v in value) or "-"))
if six.PY3:
return encoded.decode("utf-8")
return encoded
return encoded.decode("utf-8") if six.PY3 else encoded

def pack(self):
return [
Expand Down
13 changes: 9 additions & 4 deletions law/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


class NoValue(object):

def __bool__(self):
return False

Expand Down Expand Up @@ -307,6 +308,10 @@ def times_two(i):
objects of the respective types are traversed or mapped. The can be booleans or integer values
that define the depth of that setting in the struct.
"""
# interpret generators and views as lists
if isinstance(struct, (types.GeneratorType, ValuesView)):
struct = list(struct)

types = tuple()
if map_dict:
types = types + (dict,)
Expand Down Expand Up @@ -367,12 +372,12 @@ def times_two(i):


def mask_struct(mask, struct, replace=_no_value):
# interpret genrators as lists
if isinstance(struct, types.GeneratorType):
return mask_struct(mask, list(struct), replace=replace)
# interpret generators and views as lists
if isinstance(struct, (types.GeneratorType, ValuesView)):
struct = list(struct)

# when mask is a bool, or struct is not a dict or sequence, apply the mask immediately
elif isinstance(mask, bool) or not isinstance(struct, (list, tuple, dict)):
if isinstance(mask, bool) or not isinstance(struct, (list, tuple, dict)):
return struct if mask else replace

# check list and tuple types
Expand Down

0 comments on commit 185e279

Please sign in to comment.