Skip to content

Commit

Permalink
Fix Training sort (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
samhjn authored and twd2 committed Mar 11, 2018
1 parent 635d3f9 commit e9244b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ python:
- 3.6
install:
- pip install -r requirements.txt
- node scripts/fix_abroad_shrinkwrap.js
- npm config set registry https://registry.npm.taobao.org
- npm install
script:
- npm run build:production
Expand Down
5 changes: 3 additions & 2 deletions vj4/handler/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from vj4.handler import base
from vj4.util import json
from vj4.util import pagination
from vj4.util import misc


def _parse_dag_json(dag):
Expand All @@ -29,8 +30,8 @@ def _parse_dag_json(dag):
raise error.ValidationError('dag')
new_node = {'_id': int(node['_id']),
'title': str(node.get('title', '')),
'require_nids': list(set(map(int, node['require_nids']))),
'pids': list(set(map(document.convert_doc_id, node['pids'])))}
'require_nids': misc.dedupe(map(int, node['require_nids'])),
'pids': misc.dedupe(map(document.convert_doc_id, node['pids']))}
new_dag.append(new_node)
except ValueError:
raise error.ValidationError('dag') from None
Expand Down
16 changes: 16 additions & 0 deletions vj4/test/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest

from vj4.util import misc


class Test(unittest.TestCase):
def test_dedupe(self):
self.assertListEqual(misc.dedupe([2,1,1,3,2,3]),[2,1,3])
self.assertListEqual(misc.dedupe([]),[])
self.assertListEqual(misc.dedupe(map(int,['2','1','1','3','2','3'])),[2,1,3])
self.assertListEqual(misc.dedupe(['b','a','b','c','b']),['b','a','c'])
self.assertListEqual(misc.dedupe([0]),[0])


if __name__ == '__main__':
unittest.main()
11 changes: 11 additions & 0 deletions vj4/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,14 @@ def format_seconds(seconds):
def base64_encode(str):
encoded = base64.b64encode(str.encode())
return encoded.decode()


def dedupe(list):
result = []
result_set = set()
for i in list:
if i in result_set:
continue
result.append(i)
result_set.add(i)
return result

0 comments on commit e9244b5

Please sign in to comment.