Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ML
lib
Binary Storage
Commits
77a45bf7
Commit
77a45bf7
authored
Apr 06, 2020
by
Sasha Z
Browse files
Implemented iterator.
parent
2298474d
Changes
2
Hide whitespace changes
Inline
Side-by-side
py/read_storage.py
View file @
77a45bf7
...
...
@@ -67,6 +67,16 @@ class BinaryStorage:
def
read
(
self
):
return
read_record
(
self
.
file
)
def
__iter__
(
self
):
self
.
file
.
seek
(
HEADER_SZ
)
return
self
def
__next__
(
self
):
record
=
read_record
(
self
.
file
)
if
'EOF'
in
record
:
raise
StopIteration
return
record
def
readAt
(
self
,
key
):
offset
=
self
.
rec_table
[
key
]
...
...
py/test_read.py
View file @
77a45bf7
...
...
@@ -18,31 +18,33 @@ epilog_text = '''
# will read and print out records with indices n1 and n2 from <filename>
'''
parser
=
argparse
.
ArgumentParser
(
epilog
=
epilog_text
,
formatter_class
=
RawTextHelpFormatter
)
parser
.
add_argument
(
'--index'
,
'-i'
,
action
=
'append'
,
default
=
[],
type
=
int
,
parser
.
add_argument
(
'--index'
,
'-i'
,
action
=
'append'
,
default
=
[],
type
=
int
,
help
=
'indices of records to read'
)
parser
.
add_argument
(
'file'
,
help
=
'file to read'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
file
=
BinaryStorage
(
args
.
file
)
print
(
args
.
file
,
'has'
,
len
(
file
),
'records'
)
print
(
args
.
file
,
'has'
,
len
(
file
),
'records'
)
def
show
(
record
):
if
'value'
in
record
:
value
=
record
[
'value'
]
# if isinstance(value,(str,list,tuple)):
if
hasattr
(
value
,
'__len__'
)
and
len
(
value
)
>
4
:
record
[
'value'
]
=
[
value
[
0
],
value
[
1
],
'...'
,
value
[
len
(
value
)
-
1
]]
if
hasattr
(
value
,
'__len__'
)
and
len
(
value
)
>
4
:
record
[
'value'
]
=
[
value
[
0
],
value
[
1
],
'...'
,
value
[
len
(
value
)
-
1
]]
return
record
if
(
not
args
.
index
):
while
True
:
record
=
file
.
read
()
if
'EOF'
in
record
:
break
print
(
show
(
record
))
def
show2
(
record
):
return
str
(
record
[
'name'
])
+
' '
+
str
(
record
[
'timestamp'
])
if
not
args
.
index
:
for
record
in
file
:
print
(
show2
(
record
))
else
:
for
i
in
args
.
index
:
print
(
i
,
":"
,
show
(
file
[
i
]))
print
(
i
,
":"
,
show
2
(
file
[
i
]))
file
.
close
()
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