1 module matplotlibd.core.translate;
2 
3 alias immutable bool PyBool;
4 alias immutable (void*) PyNone;
5 
6 PyBool False = false;
7 PyBool True = true;
8 PyNone None = null;
9 
10 
11 string d2py(T)(T v) {
12     import std.format: format;
13     static if (is(typeof(v) : PyNone))
14         return "None";
15 
16     else static if (is(typeof(v) : bool))
17         return v ? "True" : "False";        
18 
19     else static if (is(typeof(v) : string))
20         return format("\"%s\"", v);
21 
22     else
23         return format("%s", v);
24 }
25 
26 unittest {
27     import std.range: iota;
28     assert(d2py(None) == "None");
29     assert(d2py(null) == "None");
30     assert(d2py(True) == "True");
31     assert(d2py(true) == "True");
32     assert(d2py(False) == "False");
33     assert(d2py(false) == "False");
34     assert(d2py("Hello!") == "\"Hello!\"");
35     assert(d2py(5.iota) == "[0, 1, 2, 3, 4]");
36 }
37 
38 
39 string parseArgs(Args)(Args args) {
40     static if (is(typeof(args.keys) : string[])) {
41         string parsed;
42         foreach(key; args.byKey)
43             parsed ~= key ~ "=" ~  d2py(args[key]) ~ ",";
44     }
45     else
46         string parsed =  d2py(args) ~ ",";
47     return parsed;
48 }
49 
50 unittest {
51     import std.range: iota;
52     assert(parseArgs(5) == "5,");
53     assert(parseArgs(5.iota) == "[0, 1, 2, 3, 4],");
54     assert(parseArgs(["test": 5]) == "test=5,");
55     assert(parseArgs(["test": "test"]) == "test=\"test\",");
56     assert(parseArgs(["test": 5.iota]) == "test=[0, 1, 2, 3, 4],");
57     assert(parseArgs(["test": false]) == "test=False,");
58     assert(parseArgs(["test": False]) == "test=False,");
59 }