Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
asccol
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
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
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Owen Graham
asccol
Commits
e10c4cd5
Verified
Commit
e10c4cd5
authored
2 years ago
by
Owen Graham
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+4
-0
4 additions, 0 deletions
.gitignore
asccol.py
+61
-0
61 additions, 0 deletions
asccol.py
with
65 additions
and
0 deletions
.gitignore
0 → 100644
+
4
−
0
View file @
e10c4cd5
*.py[cod]
__pycache__/
*.so
*~
This diff is collapsed.
Click to expand it.
asccol.py
0 → 100644
+
61
−
0
View file @
e10c4cd5
"""
Parse AMRC
'
s ASCII columnar data.
"""
from
collections
import
namedtuple
Column
=
namedtuple
(
'
Column
'
,
(
'
name
'
,
'
type
'
,
'
width
'
))
class
DataSpec
:
"""
Details about how to parse an aligned data file.
Arguments:
cols (Columns): columns
'
name, type, and width
leading (int): number of leading lines to ignore
"""
def
__init__
(
self
,
cols
,
leading
=
0
):
self
.
cols
=
cols
self
.
leading
=
leading
self
.
namedtuple
=
namedtuple
(
'
row
'
,
(
col
.
name
for
col
in
cols
))
class
Columns
(
tuple
):
"""
Details about how to parse aligned data.
"""
def
__new__
(
cls
,
*
data
):
columns
=
map
(
Column
.
_make
,
data
)
return
tuple
.
__new__
(
cls
,
columns
)
def
__repr__
(
self
):
contents
=
'
,
'
.
join
(
map
(
repr
,
self
))
return
f
'
{
type
(
self
).
__name__
}
(
{
contents
}
)
'
def
parse_data
(
stream
,
dataspec
):
"""
Read lines from `stream` and yield `namedtuple` data.
"""
for
line_num
,
line
in
enumerate
(
stream
,
start
=
1
):
if
line_num
<=
dataspec
.
leading
:
continue
row
=
parse_line
(
line
,
dataspec
,
line_num
=
line_num
)
yield
dataspec
.
namedtuple
.
_make
(
row
)
def
parse_line
(
line
,
dataspec
,
line_num
=
None
):
"""
Yield data from the line.
"""
start
=
end
=
0
for
col_num
,
col
in
enumerate
(
dataspec
.
cols
,
start
=
1
):
end
+=
col
.
width
cell
=
line
[
start
:
end
].
strip
()
if
cell
:
try
:
yield
col
.
type
(
cell
)
except
ValueError
as
err
:
where
=
f
'
data column
{
col_num
}
'
if
line_num
is
not
None
:
where
=
f
'
line
{
line_num
}
,
{
where
}
'
message
=
f
'
Unexpected data format at
{
where
}
:
{
cell
!r}
'
raise
ValueError
(
message
)
from
err
else
:
yield
None
start
=
end
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