{"id":7237,"date":"2022-08-24T06:44:51","date_gmt":"2022-08-24T06:44:51","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=7237"},"modified":"2022-08-24T13:02:55","modified_gmt":"2022-08-24T13:02:55","slug":"maintaining-pynapl-part-2-apl-arrays-python-objects-and-json","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2022\/08\/maintaining-pynapl-part-2-apl-arrays-python-objects-and-json\/","title":{"rendered":"Maintaining Py&#8217;n&#8217;APL Part 2: APL Arrays, Python Objects, and JSON"},"content":{"rendered":"<p>As part of the bigger, overarching refactoring goal of making Py&#8217;n&#8217;APL great again, I refactored some of the code that deals with sending data from Python to APL and receiving data from APL into Python. In this blog post, I will describe \u2013 to the best of my abilities \u2013 how that part of the code works, how it differs from what was in place, and why those changes were made.<\/p>\n<p><em>The starting point for this blog post is the commit <a href=\"https:\/\/github.com\/Dyalog\/pynapl\/commit\/b7d4749c7cf991f35019b8c7623901a8a4f7ba61\" target=\"_blank\">b7d4749<\/a>.<\/em><\/p>\n<p>This blog post is mostly concerned with the files <code>Array.py<\/code>, <code>ConversionInterface.py<\/code>, and <code>ObjectWrapper.py<\/code> (these were the original file names before I tore them apart and moved things around). It does not make much sense to list where all the things went, but you can <a href=\"https:\/\/github.com\/Dyalog\/pynapl\/compare\/b7d4749...40523b9\" target=\"_blank\">use GitHub&#8217;s compare feature to compare the starting commit for this blog post with the &#8220;final&#8221; commit for this blog post<\/a>.<\/p>\n<h2>State of Affairs<\/h2>\n<p>If you are going to refactor a working piece of code, the first thing you need to do is to make sure that you know what the code is doing! This will help to ensure that your refactoring does not break the functionality of the code. With that in mind, I started working my way through the code.<\/p>\n<p>I started by looking at the file <code>ConversionInterface.py<\/code> and the two classes <code>Sendable<\/code> and <code>Receivable<\/code> that were defined in there. By reading the comments, I understood that these two classes were defining the &#8220;conversion interface&#8221;. In this context, the word &#8220;interface&#8221; has approximately the Java meaning of interface: it defines a set of methods that the classes that inherit from these base classes have to implement. For the class <code>Sendable<\/code>, there are two methods <code>toJSONDict<\/code> and <code>toJSONString<\/code>; and for the class <code>Receivable<\/code>, there is one method <code>to_python<\/code>.<\/p>\n<p>Even though I had <em>just<\/em> started, I already had a couple of questions:<\/p>\n<ol>\n<li>Do the names <code>Sendable<\/code> and <code>Receivable<\/code> mean that these objects will be sent to\/received from APL or from Python respectively?<\/li>\n<li>Why is there a comment next to the definition of <code>Sendable<\/code> that says that classes that implement a method <code>from_python<\/code> will inherit from <code>Sendable<\/code>? Is that a comment that became a lie as the code evolved? If not, why isn&#8217;t there a stub for that method in the class itself?<\/li>\n<\/ol>\n<p>The more I pondered on these questions, the more I started to think that the &#8220;conversion interface&#8221; isn&#8217;t necessarily about the sending to\/receiving from APL, but rather the conversion of built-in Python types to helper classes like <code>APLArray<\/code> or <code>APLNamespace<\/code> (from the file <code>Array.py<\/code>) and back. So, it might be that <code>Sendable<\/code> and <code>Receivable<\/code> are supposed to be base classes for these helper classes, telling us which ones can be converted to\/from built-in Python types. I needed to solve this conundrum before I could prepare these two base classes and use Python mechanisms to enforce these &#8220;interfaces&#8221;.<\/p>\n<h2>What the Interface Really Means<\/h2>\n<p>After playing around with the code a bit more, I felt more confident that <code>Sendable<\/code> should be inherited by classes that represent things that can be sent to APL and <code>Receivable<\/code> represents things that can be received from APL. However, it must be noted that Py&#8217;n&#8217;APL doesn&#8217;t send Python built-in types directly to APL. Whenever we want to send something to APL, Py&#8217;n&#8217;APL first converts it to the suitable intermediate (Python) class. For example, lists and tuples are converted to <code>APLArray<\/code>, and dictionaries are converted to <code>APLNamespace<\/code>.<\/p>\n<p>If an <code>APLArray<\/code> instance is supposed to be <em>sendable<\/em> to APL, we must first be able to build it <em>from the corresponding Python built-in types<\/em>, and that is why almost all <code>Sendable<\/code> subclasses also implement a method <code>from_python<\/code>. Looking at it from the other end of the connection, <code>Receivable<\/code> instances come from APL and Py&#8217;n&#8217;APL starts by taking the JSON and converting it into the appropriate <code>APLArray<\/code> instances, <code>APLNamespace<\/code> instances, etc. Only then can we convert those intermediate representations <em>to Python<\/em>, and that is why all <code>Receivable<\/code> subclasses come with a method <code>to_python<\/code>. In addition, those <code>Receivable<\/code> instances come from APL as JSON, so we need to be able to instantiate them from JSON. That is why <code>Receivable<\/code> subclasses also implement a method <code>fromJSONString<\/code>, although that is not defined in the <code>Receivable<\/code> interface.<\/p>\n<p>So, we have established that APL needs to know how to make sense of Python&#8217;s objects and Python needs to know how to make sense of APL&#8217;s arrays. (In Python, everything is an object, and in APL, everything is an array. In less precise \u2013 but maybe clearer \u2013 words, Python needs to be able to handle whatever APL passes to it, and APL needs to be able to handle whatever Python passes to it.) To implement this, we need to determine how Python objects map to APL arrays and how APL arrays map to Python objects. This is not trivial, otherwise I wouldn&#8217;t be writing about it! Here are two simple examples showing why this is not trivial:<\/p>\n<ul>\n<li>Python does not have native support for arrays of arbitrary rank.<\/li>\n<li>APL does not have a key-value mapping type like Python&#8217;s <code>dict<\/code>.\n<\/ul>\n<p>To solve the issues around Python and APL not having exactly the same type of data, we create lossless intermediate representations in both host languages. For example, Python needs to have an intermediate representation for APL arrays so that we can preserve rank information in Python. When possible, intermediate representations should know how to convert into the closest value in the host language. For example, the Python intermediate representation of a high-rank APL array should know how to convert itself into a Python list.<\/p>\n<p>I began by looking at the handling of APL arrays and namespaces. These are the conversions that need to be in place:<\/p>\n<ul>\n<li>APL arrays \u2190\u2192 Python lists<\/li>\n<li>APL arrays \u2190 arbitrary Python iterables<\/li>\n<li>APL namespaces \u2190\u2192 Python dictionaries<\/li>\n<\/ul>\n<p>When sending data from the Python side, it first needs to be converted into an instance of the appropriate <code>APLProxy<\/code> subclass. For example, a dictionary will be converted into an instance of <code>APLNamespace<\/code>. That object is converted to JSON, which is then sent to APL. APL receives the JSON and looks for a special field <code>__extended_json_type__<\/code>, which identifies the type of object. In this example, that is <code>\"APLNamespace\"<\/code>. APL then uses that information to decode the JSON data into the appropriate thing (a namespace in this example).<\/p>\n<p>When sending data from the APL side, a similar thing happens. First, the object is converted into a namespace that <code>\u2395JSON<\/code> knows how to handle. For example, an array becomes a namespace with attributes <code>shape<\/code> (the shape of the original array) and <code>data<\/code> (the ravel of the original array); the namespace is tagged with an attribute <code>__extended_json_type__<\/code>, which is a simple character vector informing Python what the object is. That namespace gets converted to JSON with <code>\u2395JSON<\/code>, and the JSON is sent to Python. Python receives the JSON and decodes it into a Python dictionary. Python then uses <code>__extended_json_type__<\/code> to determine the actual object that the dictionary represents (an array, in our example) and uses the information available to build an instance of the appropriate <code>APLProxy<\/code> subclass (<code>APLArray<\/code> in this example).<\/p>\n<p>Github commit <a href=\"https:\/\/github.com\/Dyalog\/pynapl\/commit\/40523b9024dae352476065379fffa1adb1f3bc00\" target=\"_blank\">40523b9<\/a> shows one initial implementation of the APL code that takes APL arrays and namespaces and converts them into namespaces that <code>\u2395JSON<\/code> can handle and that Python knows how to interpret. This commit also shows the APL code for the reverse operation. For now, this APL code lives in the file <code>Proxies.apln<\/code> and the respective Python code lives in the file <code>proxies.py<\/code>. Everything is ready for me to hook this into the Py&#8217;n&#8217;APL machinery so that Py&#8217;n&#8217;APL uses this mechanism to pass data around&#8230;but that&#8217;s for another blog post!<\/p>\n<h2>Summary of Changes<\/h2>\n<p>GitHub&#8217;s compare feature shows <a href=\"https:\/\/github.com\/Dyalog\/pynapl\/compare\/b7d4749...40523b9\" target=\"_blank\">all the changes I made<\/a> since the commit that was the starting point for this post. The most notable changes are:<\/p>\n<ul>\n<li>Moving the contents of <code>ConversionInterface.py<\/code> and <code>ObjectWrapper.py<\/code> into <code>Array.py<\/code>.<\/li>\n<li>Adding the file <code>proxies.py<\/code> that will have the Python code to deal with the JSON and conversions, which will end up replacing most of the code I mentioned in the previous bullet point.<\/li>\n<li>Adding the file <code>Proxies.apln<\/code> that will have the APL code to deal with the JSON and conversions, which will end up replacing a chunk of code that currently lives in <code>Py.dyalog<\/code>, which is a huge file with almost all of the Py&#8217;n&#8217;APL APL code.<\/li>\n<\/ul>\n<p>Blog posts in this series:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.dyalog.com\/blog\/2022\/05\/maintaining-pynapl-part-1-the-beginning\/\" target=\"_blank\">Part 1: The Beginning<\/a><\/li>\n<li><a href=\"https:\/\/www.dyalog.com\/blog\/2022\/08\/maintaining-pyna\u2026objects-and-json\/\" target=\"_blank\">Part 2: APL Arrays, Python Objects, and JSON<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As part of the bigger, overarching refactoring goal of making Py&#8217;n&#8217;APL great again, I refactored some of the code that deals with sending data from Python to APL and receiving data from APL into Python. In this blog post, I &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2022\/08\/maintaining-pynapl-part-2-apl-arrays-python-objects-and-json\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-7237","post","type-post","status-publish","format-standard","hentry","category-tools"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=7237"}],"version-history":[{"count":33,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7237\/revisions"}],"predecessor-version":[{"id":7385,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7237\/revisions\/7385"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=7237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=7237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=7237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}