Skip to content

Commit 6a582c3

Browse files
committed
refactor flatten function. move to utils.py
1 parent 3bba34e commit 6a582c3

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

schema_salad/flatten.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

schema_salad/ref_resolver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
from StringIO import StringIO
1313

1414
from . import validate
15-
from schema_salad.utils import aslist
16-
from .flatten import flatten
15+
from schema_salad.utils import aslist, flatten
1716
from .sourceline import SourceLine, add_lc_filename, relname
1817

1918
import requests

schema_salad/schema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import avro
22
import copy
3-
from schema_salad.utils import add_dictlist, aslist
3+
from schema_salad.utils import add_dictlist, aslist, flatten
44
import sys
55
import pprint
66
from pkg_resources import resource_stream
@@ -15,7 +15,6 @@
1515
from avro.schema import Names, SchemaParseException
1616
from . import ref_resolver
1717
from .ref_resolver import Loader, DocumentType
18-
from .flatten import flatten
1918
import logging
2019
from . import jsonld_context
2120
from .sourceline import SourceLine, strip_dup_lineno, add_lc_filename, bullets, relname

schema_salad/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,26 @@ def aslist(l): # type: (Any) -> List
1616
return l
1717
else:
1818
return [l]
19+
20+
# http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
21+
22+
def flatten(l, ltypes=(list, tuple)):
23+
# type: (Any, Any) -> Any
24+
if l is None:
25+
return []
26+
if not isinstance(l, ltypes):
27+
return [l]
28+
29+
ltype = type(l)
30+
l = list(l)
31+
i = 0
32+
while i < len(l):
33+
while isinstance(l[i], ltypes):
34+
if not l[i]:
35+
l.pop(i)
36+
i -= 1
37+
break
38+
else:
39+
l[i:i + 1] = l[i]
40+
i += 1
41+
return ltype(l)

0 commit comments

Comments
 (0)