-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
38 lines (30 loc) · 1.09 KB
/
common.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
import icat
class IcatClient(object):
"""
Simple wrapper around python-icat that checks to see if session
is valid before returning the client. If it has expired, a new
session in initiated.
"""
def __init__(self, config):
self.config = config
def getInstance(self):
try:
self.icatclient.refresh()
except:
url = self.config.get('main', 'ICAT_URL') + "/ICATService/ICAT?wsdl"
self.icatclient = icat.client.Client(url)
self.icatclient.login('db', {
'username' : self.config.get('main', 'ICAT_USER'),
'password' : self.config.get('main', 'ICAT_PASSWD').decode("utf8")
})
return self.icatclient
def chunks(l, n):
"""
Split a list of integers into a list of comma separated strings which
are grouped into chunks of size n.
chunks([1,2,3,4,5,6,7,8,9], 3) -> ["1,2,3", "4,5,6", "7,8,9"]
Parameters:
l - a list of integers
n - the chunk size
"""
return [",".join(str(j) for j in l[i:i + n]) for i in range(0, len(l), n)]