-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue223 #260
Open
paul-bradbeer
wants to merge
2
commits into
OpenDataScotland:main
Choose a base branch
from
paul-bradbeer:issue223
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Issue223 #260
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"python.formatting.provider": "black" | ||
"python.formatting.provider": "black", | ||
"python.testing.pytestArgs": [ | ||
"tests" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true, | ||
"python.defaultInterpreterPath": "F:\\Program Files\\python3.9\\python" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
try: | ||
from processor import Processor | ||
except: | ||
from .processor import Processor | ||
|
||
|
||
class csvProcessor(Processor): | ||
def __init__(self, type): | ||
super().__init__(type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
try: | ||
from processor import Processor | ||
except: | ||
from .processor import Processor | ||
|
||
|
||
class jsonProcessor(Processor): | ||
def __init__(self, type): | ||
super().__init__(type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import json, dataclasses | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class jsonRow: | ||
Title: str = "" | ||
Owner: str = "" | ||
PageURL: str = "" | ||
AssetURL: str = "" | ||
FileName: str = "" | ||
DateCreated: str = "" | ||
DateUpdated: str = "" | ||
FileSize: str = "" | ||
FileSizeUnit: str = "" | ||
FileType: str = "" | ||
NumRecords: str = "" | ||
OriginalTags: str = "" | ||
ManualTags: str = "" | ||
License: str = "" | ||
Description: str = "" | ||
|
||
def __init__(self): | ||
Title = "" | ||
Owner = "" | ||
PageURL = "" | ||
AssetURL = "" | ||
FileName = "" | ||
DateCreated = "" | ||
DateUpdated = "" | ||
FileSize = "" | ||
FileSizeUnit = "" | ||
FileType = "" | ||
NumRecords = "" | ||
OriginalTags = "" | ||
ManualTags = "" | ||
License = "" | ||
Description = "" | ||
|
||
class EnhancedJSONEncoder(json.JSONEncoder): | ||
def default(self, o): | ||
if dataclasses.is_dataclass(o): | ||
return dataclasses.asdict(o) | ||
return super().default(o) | ||
|
||
def toJSON(self): | ||
return json.dumps(self, cls=self.EnhancedJSONEncoder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,8 +49,8 @@ def main(): | |
} | ||
) | ||
source_scotgov["Source"] = "sparql" | ||
#print("DateUpdated " + source_scotgov["DateUpdated"]) | ||
#print("DateCreated " + source_scotgov["DateCreated"]) | ||
# print("DateUpdated " + source_scotgov["DateUpdated"]) | ||
# print("DateCreated " + source_scotgov["DateCreated"]) | ||
try: | ||
source_scotgov["DateUpdated"] = pd.to_datetime( | ||
source_scotgov["DateUpdated"], utc=True | ||
|
@@ -110,7 +110,7 @@ def main(): | |
source_usmart = pd.concat( | ||
[ | ||
source_usmart, | ||
pd.read_csv( | ||
pd.read_json( | ||
folder + r"/" + filename, | ||
parse_dates=["DateCreated", "DateUpdated"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line caused an error for me. I think it needs to be |
||
), | ||
|
@@ -165,19 +165,43 @@ def main(): | |
# From Scottish Parliament | ||
print("\tMerging Scottish Parliament...") | ||
path = "data/bespoke_ScottishParliament/Scottish Parliament.json" | ||
scottish_parliament_scraped = pd.read_json(path, convert_dates=["dateCreated", "dateUpdated"]) | ||
scottish_parliament_scraped = pd.read_json( | ||
path, convert_dates=["dateCreated", "dateUpdated"] | ||
) | ||
|
||
for index, row in scottish_parliament_scraped.iterrows(): | ||
for index, row in scottish_parliament_scraped.iterrows(): | ||
resources = pd.json_normalize(row["resources"]) | ||
for resource_index, resource_row in resources.iterrows(): | ||
# TEMP FIX: Need to do this mapping until we modify the merged_output.json schema to support nesting resources inside each dataset entry | ||
source_scraped = pd.concat( | ||
[source_scraped, pd.DataFrame.from_records([{"Title": row["title"], "Owner": row["owner"], "PageURL": row["pageURL"], "AssetURL": resource_row["assetUrl"], "DateCreated": row["dateCreated"], "DateUpdated": row["dateUpdated"], "FileSize": resource_row["fileSize"], "FileType": resource_row["fileType"], "NumRecords": resource_row["numRecords"], "OriginalTags": row["tags"], "ManualTags" : row["tags"], "License": row["licence"], "Description": row["description"], "FileName": resource_row["fileName"]}])] | ||
[ | ||
source_scraped, | ||
pd.DataFrame.from_records( | ||
[ | ||
{ | ||
"Title": row["title"], | ||
"Owner": row["owner"], | ||
"PageURL": row["pageURL"], | ||
"AssetURL": resource_row["assetUrl"], | ||
"DateCreated": row["dateCreated"], | ||
"DateUpdated": row["dateUpdated"], | ||
"FileSize": resource_row["fileSize"], | ||
"FileType": resource_row["fileType"], | ||
"NumRecords": resource_row["numRecords"], | ||
"OriginalTags": row["tags"], | ||
"ManualTags": row["tags"], | ||
"License": row["licence"], | ||
"Description": row["description"], | ||
"FileName": resource_row["fileName"], | ||
} | ||
] | ||
), | ||
] | ||
) | ||
|
||
source_scraped["Source"] = "Web Scraped" | ||
# endregion | ||
|
||
### Combine all data into single table | ||
print("Concatenating all") | ||
data = pd.concat( | ||
|
@@ -191,7 +215,7 @@ def main(): | |
] | ||
) | ||
data = data.reset_index(drop=True) | ||
|
||
print(f"Output untidy {dt.now()}") | ||
### Saves copy of data without cleaning - for analysis purposes | ||
data.to_json("data/merged_output_untidy.json", orient="records", date_format="iso") | ||
|
@@ -395,7 +419,7 @@ def tidy_licence(licence_name): | |
"Other (Public Domain)": "Public Domain", | ||
"Public Domain": "Public Domain", | ||
"Public Sector End User Licence (Scotland)": "Public Sector End User Licence (Scotland)", | ||
"Scottish Parliament Copyright Policy": "Scottish Parliament Copyright Policy" | ||
"Scottish Parliament Copyright Policy": "Scottish Parliament Copyright Policy", | ||
} | ||
|
||
for key in known_licences.keys(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
from ..jsonRow import jsonRow | ||
|
||
|
||
def test_json_conversion(): | ||
# Test 1 - every attribute populated | ||
|
||
example_row1 = jsonRow() | ||
|
||
example_row1.Title = "Example title" | ||
example_row1.Owner = "Example owner" | ||
example_row1.PageURL = "Example original dataset link" | ||
example_row1.AssetURL = "Example resource link" | ||
example_row1.FileName = "Example filename" | ||
example_row1.DateCreated = "05/Nov/2023" | ||
example_row1.DateUpdated = "05/Dec/2023" | ||
example_row1.FileSize = "Example size" | ||
example_row1.FileSizeUnit = "Example size unit" | ||
example_row1.FileType = "Example file type" | ||
example_row1.NumRecords = "Example num records" | ||
example_row1.OriginalTags = "Example tags" | ||
example_row1.ManualTags = "Example manual tags" | ||
example_row1.License = "Example licence" | ||
example_row1.Description = "Example description" | ||
|
||
outputJson1 = example_row1.toJSON() | ||
|
||
expectedJson1 = '{"Title": "Example title", "Owner": "Example owner", "PageURL": "Example original dataset link", "AssetURL": "Example resource link", "FileName": "Example filename", "DateCreated": "05/Nov/2023", "DateUpdated": "05/Dec/2023", "FileSize": "Example size", "FileSizeUnit": "Example size unit", "FileType": "Example file type", "NumRecords": "Example num records", "OriginalTags": "Example tags", "ManualTags": "Example manual tags", "License": "Example licence", "Description": "Example description"}' | ||
|
||
assert outputJson1 == expectedJson1 | ||
|
||
# Test 2 - some attributes left as defaults (blank) | ||
|
||
example_row2 = jsonRow() | ||
|
||
example_row2.Title = "Example title" | ||
example_row2.Owner = "Example owner" | ||
example_row2.PageURL = "Example original dataset link" | ||
example_row2.AssetURL = "Example resource link" | ||
example_row2.FileType = "Example file type" | ||
example_row2.OriginalTags = "Example tags" | ||
example_row2.ManualTags = "Example manual tags" | ||
example_row2.License = "Example licence" | ||
example_row2.Description = "Example description" | ||
|
||
outputJson2 = example_row2.toJSON() | ||
|
||
expectedJson2 = '{"Title": "Example title", "Owner": "Example owner", "PageURL": "Example original dataset link", "AssetURL": "Example resource link", "FileName": "", "DateCreated": "", "DateUpdated": "", "FileSize": "", "FileSizeUnit": "", "FileType": "Example file type", "NumRecords": "", "OriginalTags": "Example tags", "ManualTags": "Example manual tags", "License": "Example licence", "Description": "Example description"}' | ||
|
||
assert outputJson2 == expectedJson2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would remove this as it's dependent on the machine config of whoever's running the scripts