Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
068b1bc3
Commit
068b1bc3
authored
Mar 27, 2014
by
Hilton Bristow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More generic todict
parent
72d5609a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
12 deletions
+21
-12
parse_tree.py
modules/matlab/generator/parse_tree.py
+21
-12
No files found.
modules/matlab/generator/parse_tree.py
View file @
068b1bc3
import
collections
from
textwrap
import
fill
from
filters
import
*
...
...
@@ -333,23 +334,31 @@ def constants(tree):
for
gen
in
constants
(
val
):
yield
gen
def
isstring
(
s
):
"""
Check if variable is string representable (regular/unicode/raw)
in a Python 2 and 3 compatible manner
"""
try
:
return
isinstance
(
s
,
basestring
)
except
NameError
:
return
isinstance
(
s
,
str
)
def
todict
(
obj
,
classkey
=
None
):
"""
Convert the ParseTree to a dictionary, stripping all objects of their
methods and converting class names to strings
"""
if
isstring
(
obj
):
return
obj
if
isinstance
(
obj
,
dict
):
for
k
in
obj
.
keys
():
obj
[
k
]
=
todict
(
obj
[
k
],
classkey
)
obj
.
update
((
key
,
todict
(
val
,
classkey
))
for
key
,
val
in
obj
.
items
())
return
obj
elif
isinstance
(
obj
,
list
):
return
[
todict
(
v
,
classkey
)
for
v
in
obj
]
elif
hasattr
(
obj
,
"__dict__"
):
data
=
dict
([(
key
,
todict
(
value
,
classkey
))
for
key
,
value
in
obj
.
__dict__
.
items
()
if
not
callable
(
value
)
and
not
key
.
startswith
(
'_'
)])
if
classkey
is
not
None
and
hasattr
(
obj
,
"__class__"
):
if
isinstance
(
obj
,
collections
.
Iterable
):
return
[
todict
(
val
,
classkey
)
for
val
in
obj
]
if
hasattr
(
obj
,
'__dict__'
):
attrs
=
dict
((
key
,
todict
(
val
,
classkey
))
for
key
,
val
in
vars
(
obj
)
.
items
())
if
classkey
is
not
None
and
hasattr
(
obj
,
'__class__'
):
data
[
classkey
]
=
obj
.
__class__
.
__name__
return
data
else
:
return
obj
return
attrs
return
obj
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment