Skip to content

Commit

Permalink
refactor flatten function. move to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
manu-chroma committed Jun 11, 2017
1 parent 3bba34e commit 6a582c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
26 changes: 0 additions & 26 deletions schema_salad/flatten.py

This file was deleted.

3 changes: 1 addition & 2 deletions schema_salad/ref_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from StringIO import StringIO

from . import validate
from schema_salad.utils import aslist
from .flatten import flatten
from schema_salad.utils import aslist, flatten
from .sourceline import SourceLine, add_lc_filename, relname

import requests
Expand Down
3 changes: 1 addition & 2 deletions schema_salad/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import avro
import copy
from schema_salad.utils import add_dictlist, aslist
from schema_salad.utils import add_dictlist, aslist, flatten
import sys
import pprint
from pkg_resources import resource_stream
Expand All @@ -15,7 +15,6 @@
from avro.schema import Names, SchemaParseException
from . import ref_resolver
from .ref_resolver import Loader, DocumentType
from .flatten import flatten
import logging
from . import jsonld_context
from .sourceline import SourceLine, strip_dup_lineno, add_lc_filename, bullets, relname
Expand Down
23 changes: 23 additions & 0 deletions schema_salad/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@ def aslist(l): # type: (Any) -> List
return l
else:
return [l]

# http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html

def flatten(l, ltypes=(list, tuple)):
# type: (Any, Any) -> Any
if l is None:
return []
if not isinstance(l, ltypes):
return [l]

ltype = type(l)
l = list(l)
i = 0
while i < len(l):
while isinstance(l[i], ltypes):
if not l[i]:
l.pop(i)
i -= 1
break
else:
l[i:i + 1] = l[i]
i += 1
return ltype(l)

0 comments on commit 6a582c3

Please sign in to comment.