Skip to content

Commit c5eadb5

Browse files
committed
Add read/write methods for archiving GitHub datasets
1 parent 6ee9374 commit c5eadb5

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

minerutils/github.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import datetime
1010
import sys
11+
import bigjson
1112
from minerutils.auth import MinerWithAuthentication
1213

1314
class GitHub(MinerWithAuthentication):
@@ -51,6 +52,8 @@ def _get(self, url, params={}, headers={}):
5152
return resp
5253

5354
def _getNextURL(self, resp):
55+
if (resp is None):
56+
return None
5457
if (not 'Link' in resp.headers):
5558
return None
5659
linksText = resp.headers['Link']
@@ -80,3 +83,21 @@ def repoExists(self, user, repo):
8083
return False
8184
else:
8285
return True
86+
87+
def writeData(self, path, data):
88+
try:
89+
with open(path, 'w', encoding='utf8') as f:
90+
json.dump(data, f, ensure_ascii=False)
91+
except IOError:
92+
print("File not accessible")
93+
94+
def readData(self, path):
95+
try:
96+
wrapper = None
97+
with open(path, 'rb') as f:
98+
wrapper = bigjson.load(f, 'utf8').to_python()
99+
return wrapper
100+
except FileNotFoundError:
101+
print("File not found")
102+
except IOError:
103+
print("File not accessible")

tests/test_github.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import os
23
from minerutils import GitHub
34

45
class GitHubTest(unittest.TestCase):
@@ -8,6 +9,8 @@ def setUp(self):
89

910
def tearDown(self):
1011
self.g = None
12+
if os.path.exists('sample.json'):
13+
os.remove('sample.json')
1114

1215
def test_repo_exists(self):
1316
self.assertTrue(self.g.repoExists("EPICLab", "EPICLab.github.io"))
@@ -44,5 +47,11 @@ def test_url_params(self):
4447
users = self.g.get('/search/users?q=EPICLab')
4548
self.assertTrue(len(users) > 0)
4649

50+
def test_data_io(self):
51+
data = [{"1": "one"}, {"2": "two"}, {"3": "three"}]
52+
self.g.writeData('sample.json', data)
53+
output = self.g.readData('sample.json')
54+
self.assertListEqual(data, output)
55+
4756
if __name__ == '__main__':
4857
unittest.main()

0 commit comments

Comments
 (0)