-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodeswitch.py
53 lines (40 loc) · 1.48 KB
/
codeswitch.py
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
import redis
from collections import OrderedDict
from site_credentials import *
REDIS = redis.Redis(host=redis_server, port=redis_port, password=redis_key)
def get_all_items(wd_prop, descending_order=True):
identifier_list = hgetall('wikidata_to_' + wd_prop)
identifier_list = [x + '|' + y for x, y in identifier_list.items()]
identifier_list.sort(reverse=descending_order)
return OrderedDict({x.split('|')[0]: x.split('|')[1] for x in identifier_list})
def hgetall(keyname):
raw = REDIS.hgetall(keyname)
return {x.decode('utf-8'): y.decode('utf-8') for x, y in raw.items()}
def hget(keyname, itemname):
if type(itemname) is str:
raw = REDIS.hget(keyname, itemname)
if raw is None:
return None
else:
return raw.decode('utf-8')
else:
raw = REDIS.hmget(keyname, itemname)
ret = []
for result in raw:
if result is None:
ret.append(None)
else:
ret.append(result.decode('utf-8'))
return ret
def doi_to_wikidata(doi):
return hget('P356_to_wikidata', doi)
def pmid_to_wikidata(pmid):
return hget('P698_to_wikidata', pmid)
def pmcid_to_wikidata(pmcid):
return hget('P932_to_wikidata', pmcid)
def wikidata_to_doi(wikidata):
return hget('wikidata_to_P356', wikidata)
def wikidata_to_pmid(wikidata):
return hget('wikidata_to_P698', wikidata)
def wikidata_to_pmcid(wikidata):
return hget('wikidata_to_P932', wikidata)