Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
zolfa-nd2reader
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lorenzo ZOLFANELLI
zolfa-nd2reader
Commits
7c779b3a
Commit
7c779b3a
authored
8 years ago
by
Ruben Verweij
Browse files
Options
Downloads
Patches
Plain Diff
Small refactor of common functions
parent
cb2c06f9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
nd2reader/common.py
+39
-31
39 additions, 31 deletions
nd2reader/common.py
with
39 additions
and
31 deletions
nd2reader/common.py
+
39
−
31
View file @
7c779b3a
...
@@ -115,28 +115,15 @@ def parse_date(text_info):
...
@@ -115,28 +115,15 @@ def parse_date(text_info):
for
line
in
text_info
.
values
():
for
line
in
text_info
.
values
():
line
=
line
.
decode
(
"
utf8
"
)
line
=
line
.
decode
(
"
utf8
"
)
# ND2s seem to randomly switch between 12- and 24-hour representations.
# ND2s seem to randomly switch between 12- and 24-hour representations.
absolute_start_24
=
_parse_date_24h
(
line
)
absolute_start_12
=
_parse_date_12h
(
line
)
if
not
absolute_start_12
and
not
absolute_start_24
:
continue
return
absolute_start_12
if
absolute_start_12
else
absolute_start_24
return
None
def
_parse_date_12h
(
line
):
try
:
try
:
absolute_start_12
=
datetime
.
strptime
(
line
,
"
%m/%d/%Y %I:%M:%S %p
"
)
absolute_start
=
datetime
.
strptime
(
line
,
"
%m/%d/%Y %H:%M:%S
"
)
return
absolute_start_12
except
(
TypeError
,
ValueError
):
except
(
TypeError
,
ValueError
):
return
None
def
_parse_date_24h
(
line
):
try
:
try
:
absolute_start_24
=
datetime
.
strptime
(
line
,
"
%m/%d/%Y %H:%M:%S
"
)
absolute_start
=
datetime
.
strptime
(
line
,
"
%m/%d/%Y %I:%M:%S %p
"
)
return
absolute_start_24
except
(
TypeError
,
ValueError
):
except
(
TypeError
,
ValueError
):
return
None
absolute_start
=
None
return
absolute_start
def
_parse_metadata_item
(
data
,
cursor_position
):
def
_parse_metadata_item
(
data
,
cursor_position
):
...
@@ -176,19 +163,38 @@ def read_metadata(data, count):
...
@@ -176,19 +163,38 @@ def read_metadata(data, count):
"""
"""
if
data
is
None
:
if
data
is
None
:
return
None
return
None
data
=
six
.
BytesIO
(
data
)
data
=
six
.
BytesIO
(
data
)
metadata
=
{}
metadata
=
{}
for
_
in
range
(
count
):
for
_
in
range
(
count
):
cursor_position
=
data
.
tell
()
cursor_position
=
data
.
tell
()
header
=
data
.
read
(
2
)
header
=
data
.
read
(
2
)
if
not
header
:
if
not
header
:
# We've reached the end of some hierarchy of data
# We've reached the end of some hierarchy of data
break
break
if
six
.
PY3
:
if
six
.
PY3
:
header
=
header
.
decode
(
"
utf8
"
)
header
=
header
.
decode
(
"
utf8
"
)
data_type
,
name_length
=
map
(
ord
,
header
)
data_type
,
name_length
=
map
(
ord
,
header
)
name
=
data
.
read
(
name_length
*
2
).
decode
(
"
utf16
"
)[:
-
1
].
encode
(
"
utf8
"
)
name
=
data
.
read
(
name_length
*
2
).
decode
(
"
utf16
"
)[:
-
1
].
encode
(
"
utf8
"
)
value
=
_get_value
(
data
,
data_type
,
cursor_position
)
value
=
_get_value
(
data
,
data_type
,
cursor_position
)
metadata
=
_add_to_metadata
(
metadata
,
name
,
value
)
return
metadata
def
_add_to_metadata
(
metadata
,
name
,
value
):
"""
Add the name value pair to the metadata dict
:param metadata:
:param name:
:param value:
:return:
"""
if
name
not
in
metadata
.
keys
():
if
name
not
in
metadata
.
keys
():
metadata
[
name
]
=
value
metadata
[
name
]
=
value
else
:
else
:
...
@@ -196,7 +202,9 @@ def read_metadata(data, count):
...
@@ -196,7 +202,9 @@ def read_metadata(data, count):
# We have encountered this key exactly once before. Since we're seeing it again, we know we
# We have encountered this key exactly once before. Since we're seeing it again, we know we
# need to convert it to a list before proceeding.
# need to convert it to a list before proceeding.
metadata
[
name
]
=
[
metadata
[
name
]]
metadata
[
name
]
=
[
metadata
[
name
]]
# We've encountered this key before so we're guaranteed to be dealing with a list. Thus we append
# We've encountered this key before so we're guaranteed to be dealing with a list. Thus we append
# the value to the already-existing list.
# the value to the already-existing list.
metadata
[
name
].
append
(
value
)
metadata
[
name
].
append
(
value
)
return
metadata
return
metadata
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment