-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclear_dataset.py
More file actions
executable file
·77 lines (64 loc) · 2.96 KB
/
clear_dataset.py
File metadata and controls
executable file
·77 lines (64 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals, division
from shareabouts_tool import ShareaboutsTool
from argparse import ArgumentParser
import json
spinner_frames = '\|/―'
step = 0
def place_done_callback(place, place_response):
import sys
global step
if place_response.status_code != 204:
print('Error deleting place %s: %s (%s)' % (place, place_response.status_code, place_response.text))
return
else:
print('\r%s - Deleted %s ' % (step, spinner_frames[step % 4]), end='')
sys.stdout.flush()
step += 1
def place_matches_filters(place, included_attributes=[], restricted_attributes=[], eq_attribute_values={}, ne_attribute_values={}):
for attr in included_attributes:
if attr not in place:
return False
for attr in restricted_attributes:
if attr in place:
return False
for attr, val in eq_attribute_values.items():
if place.get(attr, None) != val:
return False
for attr, val in ne_attribute_values.items():
if place.get(attr, None) == val:
return False
return True
def main(config, delete=True, included_attributes=[], restricted_attributes=[], eq_attribute_values={}, ne_attribute_values={}):
tool = ShareaboutsTool(config['host'])
all_places = [
place for place in
tool.get_places(config['owner'], config['dataset'])
#...put a condition here to filter the places, if desired...
if place_matches_filters(
place, included_attributes, restricted_attributes,
eq_attribute_values, ne_attribute_values)
]
print('Deleting the %s places...' % (len(all_places),))
if delete:
tool.delete_places(
config['owner'], config['dataset'], config['key'],
all_places, place_done_callback)
print('\nDone!')
if __name__ == '__main__':
parser = ArgumentParser(description='Remove all places from a dataset.')
parser.add_argument('configuration', type=str, help='The configuration file name')
parser.add_argument('--test', '--no-delete', dest='delete', action='store_false', help='Actually delete the places?')
parser.add_argument('--has-attr', nargs='*', help='Delete items that have the attribute(s)')
parser.add_argument('--wo-attr', nargs='*', help='Delete items that do not have the attribute(s)')
parser.add_argument('--attr-is', nargs='*', help='Delete items that have the specific attribute values')
parser.add_argument('--attr-not', nargs='*', help='Delete items that do not have the specific attribute values')
args = parser.parse_args()
config = json.load(open(args.configuration))
main(
config, delete=args.delete,
included_attributes=args.has_attr, restricted_attributes=args.wo_attr,
eq_attribute_values=dict([item.split('=') for item in args.attr_is]),
ne_attribute_values=dict([item.split('=') for item in args.attr_not]),
)