Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SE211_CSV
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
ktc53
SE211_CSV
Commits
dd835b1d
Commit
dd835b1d
authored
Jan 30, 2020
by
PlanetCookieX
Browse files
Options
Downloads
Patches
Plain Diff
Added documentation comments to csv_f
parent
a2ae7b0b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Csv_F.py
+64
-1
64 additions, 1 deletion
Csv_F.py
__pycache__/Csv_F.cpython-34.pyc
+0
-0
0 additions, 0 deletions
__pycache__/Csv_F.cpython-34.pyc
with
64 additions
and
1 deletion
Csv_F.py
+
64
−
1
View file @
dd835b1d
# This file serves as the data structure for a user's file to interact with
class
Csv_F
:
# Initialize variables
def
__init__
(
self
,
csv_data
,
path
,
row_headers
,
col_headers
,
r_delim
=
'
\n
'
,
c_delim
=
'
,
'
,
updated
=
False
):
self
.
__csv_data
=
csv_data
self
.
path
=
path
...
...
@@ -9,61 +10,92 @@ class Csv_F:
self
.
c_delim
=
c_delim
self
.
updated
=
updated
# Return object contents as a string
def
__str__
(
self
):
return
str
(
self
.
__csv_data
)
+
'
\n
'
+
self
.
path
+
'
\n
'
+
str
(
self
.
__row_headers
)
+
'
\n
'
+
str
(
self
.
__col_headers
)
# Takes in an id that will be used for access,
# a boolean that represents if the id refers to a header,
# and another optional boolean that represents if the id refers to a header id.
# Returns an array-usable index
def
get_index
(
self
,
id
,
is_header
,
is_row
=
False
):
# If the user requests a header id and there are no headers
if
is_header
and
not
(
self
.
__row_headers
if
is_row
else
self
.
__col_headers
):
raise
Exception
(
'
No
'
+
(
'
row
'
if
is_row
else
'
col
'
)
+
'
headers detected
'
)
from
None
else
:
try
:
# Retreives the index of id from headers or returns an index outright
index
=
((
self
.
__row_headers
[
1
:]
if
is_row
else
self
.
__col_headers
[
1
:]).
index
(
id
)
if
is_header
else
id
)
# Checks if index is inbounds and the right type
if
is_row
:
self
.
__csv_data
[
index
]
else
:
self
.
__csv_data
[
0
][
index
]
index
=
int
(
index
)
except
(
ValueError
,
IndexError
):
raise
Exception
(
'
Id does not exist in
'
+
(
'
row
'
if
is_row
else
'
col
'
)
+
'
headers or is out of bounds. id:
'
+
str
(
id
))
from
None
return
index
# Returns width of csv data
def
get_width
(
self
):
return
len
(
self
.
__csv_data
[
0
])
# Returns height of csv data
def
get_height
(
self
):
return
len
(
self
.
__csv_data
)
# Returns csv data
def
get_data
(
self
):
return
self
.
__csv_data
# Takes an id of the requested row,
# and a boolean that represents if the id is for a header.
# Returns a full row based on the id
def
get_row
(
self
,
id
,
is_header
=
False
):
index
=
self
.
get_index
(
id
,
is_header
,
is_row
=
True
)
return
self
.
__csv_data
[
index
]
# Takes an id of the requested col,
# and a boolean that represents if the id is for a header.
# Returns a full col based on the id
def
get_col
(
self
,
id
,
is_header
=
False
):
index
=
self
.
get_index
(
id
,
is_header
)
return
[
self
.
__csv_data
[
i
][
index
]
for
i
in
range
(
len
(
self
.
__csv_data
))
]
# Takes two ids, one for the requested row and one for the col,
# and two booleans that represents if the row id is for a header, and if the col id is for a header.
# Returns a single value based on the id
def
get_val
(
self
,
r_id
,
c_id
,
r_is_header
=
False
,
c_is_header
=
False
):
r_index
=
self
.
get_index
(
r_id
,
r_is_header
,
is_row
=
True
)
c_index
=
self
.
get_index
(
c_id
,
c_is_header
)
return
self
.
__csv_data
[
r_index
][
c_index
]
# Takes an id representing the desired row,
# a list of new values for the row,
# a boolean that represents if the id refers to a header,
# and another boolean that prepresents if the new row should be force assigned.
# Returns a boolean representing success
def
set_row
(
self
,
id
,
new_row
,
is_header
=
False
,
force
=
False
):
#Initial variable setup
index
=
self
.
get_index
(
id
,
is_header
,
is_row
=
True
)
data_len
=
self
.
get_width
()
new_data_len
=
len
(
new_row
)
# If new values over or under flow and are not forced
if
new_data_len
!=
data_len
and
not
force
:
raise
Exception
(
'
New data length does not match target. Set force flag if required.
'
)
from
None
return
False
else
:
if
new_data_len
>=
data_len
:
# Go through new row until we hit the end of old
for
col
,
data
in
enumerate
(
new_row
):
if
col
==
data_len
:
break
else
:
self
.
__csv_data
[
index
][
col
]
=
str
(
data
)
else
:
# Go through the old data and replace with new values until none are left. Then continue with default value.
for
col
in
range
(
data_len
):
if
col
>
new_data_len
-
1
:
self
.
__csv_data
[
index
][
col
]
=
'
0
'
...
...
@@ -74,22 +106,31 @@ class Csv_F:
return
True
# Takes an id representing the desired col,
# a list of new values for the col,
# a boolean that represents if the id refers to a header,
# and another boolean that prepresents if the new col should be force assigned.
# Returns a boolean representing success
def
set_col
(
self
,
id
,
new_col
,
is_header
=
False
,
force
=
False
):
#Initial variable setup
index
=
self
.
get_index
(
id
,
is_header
)
data_len
=
self
.
get_height
()
new_data_len
=
len
(
new_col
)
# If new values over or under flow and are not forced
if
new_data_len
!=
data_len
and
not
force
:
raise
Exception
(
'
New data length does not match target. Set force flag if required.
'
)
from
None
return
False
else
:
if
new_data_len
>=
data_len
:
# Go through new col until we hit the end of old
for
row
,
data
in
enumerate
(
new_col
):
if
row
==
data_len
:
break
else
:
self
.
__csv_data
[
row
][
index
]
=
str
(
data
)
else
:
# Go through the old data and replace with new values until none are left. Then continue with default value.
for
row
in
range
(
data_len
):
if
row
>
new_data_len
-
1
:
self
.
__csv_data
[
row
][
index
]
=
'
0
'
...
...
@@ -99,8 +140,14 @@ class Csv_F:
self
.
updated
=
True
return
True
# Takes two ids, one representing the desired row, and one for the col
# a new val,
# a two booleans, one that represents if the row id refers to a header, and one for the col id.
# Returns a boolean representing success
def
set_val
(
self
,
new_val
,
r_id
,
c_id
,
r_is_header
=
False
,
c_is_header
=
False
):
# Checks for atomic types
if
type
(
new_val
)
in
[
int
,
float
,
str
,
bool
]:
# Gets indexes and sets value
r_index
=
self
.
get_index
(
r_id
,
r_is_header
,
is_row
=
True
)
c_index
=
self
.
get_index
(
c_id
,
c_is_header
)
self
.
__csv_data
[
r_index
][
c_index
]
=
str
(
new_val
)
...
...
@@ -110,27 +157,36 @@ class Csv_F:
raise
Exception
(
'
The value entered must be a singlton.
'
+
str
(
type
(
new_val
))
+
'
Not accepted.
'
)
from
None
return
False
# Returns the row headers
def
get_row_headers
(
self
):
return
self
.
__row_headers
# Returns the col headers
def
get_col_headers
(
self
):
return
self
.
__col_headers
# Takes a list of new headers
# and a boolean representing if the assignment should be forced.
# Returns a boolean representing success
def
set_row_headers
(
self
,
new_headers
,
force
=
False
):
#Initial variable setup
header_len
=
len
(
self
.
__row_headers
)
new_header_len
=
len
(
new_headers
)
# If new values over or under flow and are not forced
if
new_header_len
!=
header_len
and
not
force
:
raise
Exception
(
'
New header length does not match target. Set force flag if required.
'
)
from
None
return
False
else
:
if
new_header_len
>=
header_len
:
# Go through new headers until we hit the end of old
for
i
,
header
in
enumerate
(
new_headers
):
if
i
==
header_len
:
break
else
:
self
.
__row_headers
[
i
]
=
header
else
:
# Go through the old headers and replace with new values until none are left. Then continue with default value.
for
i
in
range
(
header_len
):
if
i
>
new_header_len
-
1
:
self
.
__row_headers
[
i
]
=
'
0
'
...
...
@@ -140,21 +196,28 @@ class Csv_F:
self
.
updated
=
True
return
True
# Takes a list of new headers
# and a boolean representing if the assignment should be forced.
# Returns a boolean representing success
def
set_col_headers
(
self
,
new_headers
,
force
=
False
):
#Initial variable setup
header_len
=
len
(
self
.
__col_headers
)
new_header_len
=
len
(
new_headers
)
# If new values over or under flow and are not forced
if
new_header_len
!=
header_len
and
not
force
:
raise
Exception
(
'
New header length does not match target. Set force flag if required.
'
)
from
None
return
False
else
:
if
new_header_len
>=
header_len
:
# Go through new headers until we hit the end of old
for
i
,
header
in
enumerate
(
new_headers
):
if
i
==
header_len
:
break
else
:
self
.
__col_headers
[
i
]
=
header
else
:
# Go through the old headers and replace with new values until none are left. Then continue with default value.
for
i
in
range
(
header_len
):
if
i
>
new_header_len
-
1
:
self
.
__col_headers
[
i
]
=
'
0
'
...
...
...
...
This diff is collapsed.
Click to expand it.
__pycache__/Csv_F.cpython-34.pyc
+
0
−
0
View file @
dd835b1d
No preview for this file type
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
sign in
to comment