{"action":"created","issue":{"url":"https://api.github.com/repos/CoolProp/CoolProp/issues/296","labels_url":"https://api.github.com/repos/CoolProp/CoolProp/issues/296/labels{/name}","comments_url":"https://api.github.com/repos/CoolProp/CoolProp/issues/296/comments","events_url":"https://api.github.com/repos/CoolProp/CoolProp/issues/296/events","html_url":"https://github.com/CoolProp/CoolProp/issues/296","id":51199704,"number":296,"title":"Update examples for v5","user":{"login":"ibell","id":1859947,"avatar_url":"https://avatars.githubusercontent.com/u/1859947?v=3","gravatar_id":"","url":"https://api.github.com/users/ibell","html_url":"https://github.com/ibell","followers_url":"https://api.github.com/users/ibell/followers","following_url":"https://api.github.com/users/ibell/following{/other_user}","gists_url":"https://api.github.com/users/ibell/gists{/gist_id}","starred_url":"https://api.github.com/users/ibell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ibell/subscriptions","organizations_url":"https://api.github.com/users/ibell/orgs","repos_url":"https://api.github.com/users/ibell/repos","events_url":"https://api.github.com/users/ibell/events{/privacy}","received_events_url":"https://api.github.com/users/ibell/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/CoolProp/CoolProp/milestones/1","labels_url":"https://api.github.com/repos/CoolProp/CoolProp/milestones/1/labels","id":763975,"number":1,"title":"v 5.1","description":"","creator":{"login":"ibell","id":1859947,"avatar_url":"https://avatars.githubusercontent.com/u/1859947?v=3","gravatar_id":"","url":"https://api.github.com/users/ibell","html_url":"https://github.com/ibell","followers_url":"https://api.github.com/users/ibell/followers","following_url":"https://api.github.com/users/ibell/following{/other_user}","gists_url":"https://api.github.com/users/ibell/gists{/gist_id}","starred_url":"https://api.github.com/users/ibell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ibell/subscriptions","organizations_url":"https://api.github.com/users/ibell/orgs","repos_url":"https://api.github.com/users/ibell/repos","events_url":"https://api.github.com/users/ibell/events{/privacy}","received_events_url":"https://api.github.com/users/ibell/received_events","type":"User","site_admin":false},"open_issues":7,"closed_issues":5,"state":"open","created_at":"2014-08-24T21:35:38Z","updated_at":"2015-02-10T04:16:56Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2014-12-06T23:02:07Z","updated_at":"2015-02-10T06:55:08Z","closed_at":null,"body":"We have some rudimentary examples for v4, and it would be good to come up with more complete examples for all the procedural languages that we can test from the command line.\r\n\r\n@Arik100 has volunteered to help.\r\n\r\nFirst step is to get the detailed example working in C++, then port it to SWIG based wrappers."},"comment":{"url":"https://api.github.com/repos/CoolProp/CoolProp/issues/comments/73653096","html_url":"https://github.com/CoolProp/CoolProp/issues/296#issuecomment-73653096","issue_url":"https://api.github.com/repos/CoolProp/CoolProp/issues/296","id":73653096,"user":{"login":"ibell","id":1859947,"avatar_url":"https://avatars.githubusercontent.com/u/1859947?v=3","gravatar_id":"","url":"https://api.github.com/users/ibell","html_url":"https://github.com/ibell","followers_url":"https://api.github.com/users/ibell/followers","following_url":"https://api.github.com/users/ibell/following{/other_user}","gists_url":"https://api.github.com/users/ibell/gists{/gist_id}","starred_url":"https://api.github.com/users/ibell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ibell/subscriptions","organizations_url":"https://api.github.com/users/ibell/orgs","repos_url":"https://api.github.com/users/ibell/repos","events_url":"https://api.github.com/users/ibell/events{/privacy}","received_events_url":"https://api.github.com/users/ibell/received_events","type":"User","site_admin":false},"created_at":"2015-02-10T06:55:08Z","updated_at":"2015-02-10T06:55:08Z","body":"This is almost there:\r\n``` python\r\nimport json\r\n\r\nheader_template = \"\"\"\r\n[\r\n    \r\n]\r\n\"\"\"\r\n\r\n# Each entry in the list is a chunk (probably a line) in the to-be-generated code\r\nbody_template = \"\"\"\r\n[\r\n    \"\",\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'**************** INFORMATION ***************'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'CoolProp version:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"get_global_param_string\",\r\n                \"arguments\": [\r\n                    \"'version'\"\r\n                ]\r\n            }\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'CoolProp gitrevision:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"get_global_param_string\",\r\n                \"arguments\": [\r\n                    \"'gitrevision'\"\r\n                ]\r\n            }\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'CoolProp Fluids:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"get_global_param_string\",\r\n                \"arguments\": [\r\n                    \"'FluidsList'\"\r\n                ]\r\n            }\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    \"\",\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'*********** HIGH LEVEL INTERFACE *****************'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Critical temperature of water:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"Props1SI\",\r\n                \"arguments\": [\r\n                    \"'Water'\",\r\n                    \"'Tcrit'\"\r\n                ]\r\n            },\r\n            \"'K'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Boiling temperature of water at 101325 Pa:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"PropsSI\",\r\n                \"arguments\": [\r\n                    \"'T'\", \"'P'\", \"101325\", \"'Q'\", \"0\", \"'Water'\"\r\n                ]\r\n            },\r\n            \"'K'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Phase of water at 101325 Pa and 300 K:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"PhaseSI\",\r\n                \"arguments\": [\r\n                    \"'P'\", \"101325\", \"'Q'\", \"0\", \"'Water'\"\r\n                ]\r\n            }\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'c_p of water at 101325 Pa and 300 K:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"PropsSI\",\r\n                \"arguments\": [\r\n                    \"'C'\", \"'P'\", \"101325\", \"'T'\", \"300\", \"'Water'\"\r\n                ]\r\n            },\r\n            \"'J/kg/K'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'c_p of water (using derivatives) at 101325 Pa and 300 K:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"PropsSI\",\r\n                \"arguments\": [\r\n                    \"'d(H)/d(T)|P'\", \"'P'\", \"101325\", \"'T'\", \"300\", \"'Water'\"\r\n                ]\r\n            },\r\n            \"'J/kg/K'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    \"\",\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [ \"'*********** HUMID AIR PROPERTIES *****************'\" ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Humidity ratio of 50% rel. hum. air at 300 K, 101325 Pa:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"HAPropsSI\",\r\n                \"arguments\": [\r\n                    \"'W'\", \"'T'\", \"300\", \"'P'\", \"101325\", \"'R'\", \"0.5\"\r\n                ]\r\n            },\r\n            \"'kg_w/kg_da'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Relative humidity from last calculation:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"HAPropsSI\",\r\n                \"arguments\": [\r\n                    \"'R'\", \"'T'\", \"300\", \"'P'\", \"101325\", \"'W'\", \r\n                    {\r\n                        \"type\": \"function\",\r\n                        \"function\": \"HAPropsSI\",\r\n                        \"arguments\": [\r\n                            \"'W'\", \"'T'\", \"300\", \"'P'\", \"101325\", \"'R'\", \"0.5\" \r\n                        ]\r\n                    }\r\n                ]\r\n            },\r\n            \"'(fractional)'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    \"\",\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [ \"'*********** INCOMPRESSIBLE FLUID AND BRINES *****************'\" ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Density of 50% (mass) ethylene glycol/water at 300 K, 101325 Pa:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"PropsSI\",\r\n                \"arguments\": [\r\n                    \"'D'\", \"'T'\", \"300\", \"'P'\", \"101325\", \"'INCOMP::MEG-50%'\"\r\n                ]\r\n            },\r\n            \"'kg/m^3'\"\r\n        ],\r\n        \"EOL\":true\r\n    },\r\n    {\r\n        \"type\": \"print\",\r\n        \"arguments\": [\r\n            \"'Viscosity of Therminol D12 at 350 K, 101325 Pa:'\",\r\n            {\r\n                \"type\": \"function\",\r\n                \"function\": \"PropsSI\",\r\n                \"arguments\": [\r\n                    \"'V'\", \"'T'\", \"350\", \"'P'\", \"101325\", \"'INCOMP::TD12'\"\r\n                ]\r\n            },\r\n            \"'Pa-s'\"\r\n        ],\r\n        \"EOL\":true\r\n    }\r\n]\r\n\"\"\"\r\n\r\nclass BaseParser(object):\r\n    \r\n    def footer(self):\r\n        return ''\r\n        \r\n    def parse(self):\r\n        j = json.loads(body_template)\r\n        lines = []\r\n        lines.append(self.header())\r\n        for line in j:\r\n            if isinstance(line, basestring):\r\n                lines.append(line)\r\n            elif isinstance(line, dict):\r\n                lines.append(self.dict2string(line))\r\n            else:\r\n                raise ValueError()\r\n        lines.append(self.footer())\r\n                \r\n        return '\\n'.join(lines)\r\n    \r\n    def write(self, fname, code):\r\n        with open(fname, 'w') as fp:\r\n            fp.write(code)\r\n        \r\n    def map_function(self, name):\r\n        if name in self.function_name_mapping:\r\n            return self.function_name_mapping[name]\r\n        else:\r\n            return name\r\n        \r\nclass Python(BaseParser):\r\n    \r\n    function_name_mapping = dict(Props1SI = 'CoolProp.PropsSI')\r\n        \r\n    def parse_arguments(self, arguments):\r\n        out = []\r\n        for arg in arguments:\r\n            if isinstance(arg, basestring) and arg[0] == \"'\" and arg[-1] == \"'\":\r\n                out.append('\\\"' + arg[1:-1] + '\\\"')\r\n            elif isinstance(arg, dict):\r\n                out.append(self.dict2string(arg))\r\n            else:\r\n                out.append(arg)\r\n        return out\r\n        \r\n    def dict2string(self, d):\r\n        if d['type'] == 'comment':\r\n            return '# ' + d['comment']\r\n        elif d['type'] == 'function':\r\n            return d['function'] + '(' + ', '.join(self.parse_arguments(d['arguments'])) + ')'\r\n        elif d['type'] == 'print':\r\n            return 'print(' + ', '.join(self.parse_arguments(d['arguments'])) + ')'\r\n        elif d['type'] == 'assignment':\r\n            return d['assignment']\r\n        else:\r\n            return '??????????????????????????????'\r\n    \r\n    def header(self):\r\n        return '\\n'.join(['from __future__ import print_function',\r\n                          'from CoolProp.CoolProp import PhaseSI, PropsSI, PropsSI, get_global_param_string',\r\n                          'from CoolProp.HumidAirProp import HAPropsSI',\r\n                          'from math import sin'])\r\n        \r\nclass Java(BaseParser):\r\n    \r\n    function_name_mapping = dict(Props1SI = 'CoolProp.Props1SI',\r\n                                 PropsSI = 'CoolProp.PropsSI',\r\n                                 PhaseSI = 'CoolProp.PhaseSI',\r\n                                 HAPropsSI = 'CoolProp.HAPropsSI',\r\n                                 get_global_param_string = 'CoolProp.get_global_param_string')\r\n        \r\n    def parse_arguments(self, arguments):\r\n        out = []\r\n        for arg in arguments:\r\n            if isinstance(arg, basestring) and arg[0] == \"'\" and arg[-1] == \"'\":\r\n                out.append('\\\"' + arg[1:-1] + '\\\"')\r\n            elif isinstance(arg, dict):\r\n                out.append(self.dict2string(arg))\r\n            else:\r\n                out.append(arg)\r\n        return out\r\n        \r\n    def dict2string(self, d):\r\n        if d['type'] == 'comment':\r\n            l = '        # ' + d['comment']\r\n        elif d['type'] == 'print':\r\n            l = '        System.out.println(' + ' + \" \" + '.join(self.parse_arguments(d['arguments'])) + ')'\r\n        elif d['type'] == 'function':\r\n            l = self.map_function(d['function']) + '(' + ', '.join(self.parse_arguments(d['arguments'])) + ')'\r\n        elif d['type'] == 'assignment':\r\n            l = d['assignment']\r\n        else:\r\n            raise ValueError\r\n            \r\n        if 'EOL' in d and d['EOL']:\r\n            l += ';'\r\n            \r\n        return l\r\n    \r\n    def header(self):\r\n        return 'public class Example {\\n    static {\\n        System.loadLibrary(\"CoolProp\");\\n    }\\n\\n    public static void main(String argv[]){'\r\n        \r\n    def footer(self):\r\n        return '    }\\n}'\r\n        \r\nclass Csharp(BaseParser):\r\n    \r\n    function_name_mapping = dict(Props1SI = 'CoolProp.Props1SI',\r\n                                 PropsSI = 'CoolProp.PropsSI',\r\n                                 PhaseSI = 'CoolProp.PhaseSI',\r\n                                 HAPropsSI = 'CoolProp.HAPropsSI',\r\n                                 get_global_param_string = 'CoolProp.get_global_param_string')\r\n        \r\n    def parse_arguments(self, arguments):\r\n        out = []\r\n        for arg in arguments:\r\n            if isinstance(arg, basestring) and arg[0] == \"'\" and arg[-1] == \"'\":\r\n                out.append('\\\"' + arg[1:-1] + '\\\"')\r\n            elif isinstance(arg, dict):\r\n                out.append(self.dict2string(arg))\r\n            else:\r\n                out.append(arg)\r\n        return out\r\n        \r\n    def dict2string(self, d):\r\n        if d['type'] == 'comment':\r\n            l = '            # ' + d['comment']\r\n        elif d['type'] == 'print':\r\n            l = '            Console.Write(' + ' + \" \" + '.join(self.parse_arguments(d['arguments'])) + ' + \"\\\\n\")'\r\n        elif d['type'] == 'function':\r\n            l = self.map_function(d['function']) + '(' + ', '.join(self.parse_arguments(d['arguments'])) + ')'\r\n        elif d['type'] == 'assignment':\r\n            l = d['assignment']\r\n        else:\r\n            raise ValueError\r\n            \r\n        if 'EOL' in d and d['EOL']:\r\n            l += ';'\r\n            \r\n        return l\r\n    \r\n    def header(self):\r\n        return 'using System;\\nusing System.Collections.Generic;\\nusing System.Text;\\n\\nnamespace ConsoleApplication1\\n{\\n    class Program\\n    {\\n        static void Main(string[] args)\\n        {'\r\n        \r\n    def footer(self):\r\n        return '        }\\n    }\\n}'\r\n        \r\nif __name__=='__main__':\r\n    \r\n    P = Python()\r\n    code = P.parse()\r\n    P.write('code.py', code)\r\n    J = Java()\r\n    code = J.parse()\r\n    J.write('platform-independent/Example.java', code)\r\n    C = Csharp()\r\n    code = C.parse()\r\n    C.write('cs-platform-independent/Example.cs', code)\r\n    \r\n    # C++\r\n    # MATLAB\r\n    # Octave\r\n```"}}