forked from UCLA-Plasma-Simulation-Group/JupyterPIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str2keywords.py
37 lines (32 loc) · 1.29 KB
/
str2keywords.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
import re
import numpy as np
# split id and keywords by whitespece or , or ;
id_key_pattern = re.compile(r'[\s,;]+')
# split keywords by commas except for those inside () and []
keyword_pattern = re.compile(r',(?!(?:[^([]*[(\[][^)\]]*[)\]])*[^()\[\]]*[)\]])')
# Here we assume the brackets are all balanced.
# Otherwise the keyword dictionary would cause runtime failure sooner or later
class str2keywords:
def __init__(self, string):
self.id, string = id_key_pattern.split(string + ' ', maxsplit=1)
# remove whitespaces
self.id = "".join(self.id.split())
string = "".join(string.split())
string = keyword_pattern.split(string)
if string[-1] == '':
string.pop(-1)
# store keywords as dictionary
self.keywords = dict(tuple(item.split('=')) for item in string)
for k, v in self.keywords.iteritems():
self.keywords[k] = eval(v)
def __eq__(self, other):
return self.id == other
# some tests
if __name__ == '__main__':
# first section of the string is the operation id, the rest is keywords
kw = str2keywords('fft norm="ortho" , axes=(0, 1 ), s=[3,3]')
a = np.mgrid[:3, :3][0]
if kw == 'fft':
# use ** to unpack the dictionary
a = np.fft.fft2(a, **kw.keywords)
print(a)