<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tristan M・ﾄリスタン</title>
    <description>The latest articles on DEV Community by Tristan M・ﾄリスタン (@tristan_m).</description>
    <link>https://dev.to/tristan_m</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F954349%2F463be02f-0b56-4a22-912a-6402b77ddce8.jpeg</url>
      <title>DEV Community: Tristan M・ﾄリスタン</title>
      <link>https://dev.to/tristan_m</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tristan_m"/>
    <language>en</language>
    <item>
      <title>Extensible Software</title>
      <dc:creator>Tristan M・ﾄリスタン</dc:creator>
      <pubDate>Mon, 10 Jul 2023 12:53:32 +0000</pubDate>
      <link>https://dev.to/tristan_m/extensible-software-377g</link>
      <guid>https://dev.to/tristan_m/extensible-software-377g</guid>
      <description>&lt;p&gt;Good morning/afternoon/evening to yourself reader!&lt;/p&gt;

&lt;h3&gt;
  
  
  Preamble
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/Tristan-Muggridge/Extensible-Application-Demo/blob/main/README.md"&gt;Github Repo Link for further reading&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'd like to mention that this is my first post, and while it mightn't be very good, it wasn't written by an AI - so I think that has to count for something!&lt;br&gt;
&lt;em&gt;Although, that is exactly what an AI would say to garner your good favour...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I've been using Anki (mobile/desktop application to digitise flashcards and utilise SRS (Spaced-Repetition-Systems) to enhance the users ability to memorise aforementioned flashcards.&lt;/p&gt;

&lt;p&gt;I really quite enjoy Anki, and owe my Japanese proficiency to itself after I ditched Memrise, but this is a blog for another day.&lt;/p&gt;

&lt;p&gt;A feature of Anki, and some other applications that you may have used before, is the ability to download extensions that other users of the Anki community have created and made available out of the goodness of their hearts - and made available for all to download and enrich their Anki experience.&lt;/p&gt;

&lt;p&gt;This got me thinking... How would someone make code like this?&lt;/p&gt;

&lt;h3&gt;
  
  
  Objective
&lt;/h3&gt;

&lt;p&gt;I wanted to design something that would minimally fit the following requirements.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run (this one is important)&lt;/li&gt;
&lt;li&gt;Allow for the loading of zero to many files downloaded into a straightforward directory (think Skyrim's &lt;em&gt;Mods&lt;/em&gt; directory).&lt;/li&gt;
&lt;li&gt;Allow these extensions to interact with, and piggy back off main, and enrich the user experience.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Problems
&lt;/h3&gt;

&lt;p&gt;I was not particularly concerned regarding step 1, writing software has been something that I've been doing for at least a little while (3 years).&lt;/p&gt;

&lt;p&gt;For step 2, I'm accustomed to performing IO operations on files strewn about here and there across the filesystem, but these were always JSON/YAML configs, ENV Configurations, poorly formatted CSV files, et cetera. I'd never found myself in a position where I &lt;em&gt;wanted&lt;/em&gt; much less &lt;em&gt;thought it'd be a good idea to&lt;/em&gt; start running random scripts!&lt;/p&gt;

&lt;p&gt;I was familiar with JavaScript's feature of &lt;em&gt;eval&lt;/em&gt; which would look a little something like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pRt9W3Ms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quzgfye4kuzlip5j8a4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pRt9W3Ms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quzgfye4kuzlip5j8a4m.png" alt="Code example of using eval function inside of TypeScript" width="800" height="462"&gt;&lt;/a&gt;&lt;br&gt;
src: &lt;a href="https://ray.so/#width=920&amp;amp;code=dHlwZSBVc2VyUHJlZmVyZW5jZXMgPSB7CiAgICBiZXZlcmFnZTogImNvZmZlZSIgfCAiYmxhY2sgY29mZmVlIiB8ICJibGFjayBjb2ZmZWUgd2l0aCBtaWxrIjsKICAgIHBldDogImNhdCIgfCAiYnVybWVzZSBjYXQiIHwgIm1vZ2d5IGNhdCI7CiAgICBibG9nOiAibWVkaXVtIiB8ICJkZXYudG8iIHwgImhhc2hub2RlIjsKICAgIHZpZGVvR2FtZTogIlBlcnNvbmEgNSBSb3lhbGUiIHwgIlNoaW4gTWVnYW1pIFRlbnNlaSAo5paw5aWz56We6Lui55SfKSIgfCAiUGVyc29uYSA0IEdvbGRlbiI7Cn0KCmNvbnN0IHVzZXJQcmVmczogVXNlclByZWZlcmVuY2VzID0gewogICAgYmV2ZXJhZ2U6ICJjb2ZmZWUiLAogICAgcGV0OiAiY2F0IiwKICAgIGJsb2c6ICJkZXYudG8iLAogICAgdmlkZW9HYW1lOiAiUGVyc29uYSA1IFJveWFsZSIKfQoKY29uc3QgbXlDb2RlID0gJyBmb3IgKGNvbnN0IFtrZXksIHZhbHVlXSBvZiBPYmplY3QuZW50cmllcyh1c2VyUHJlZnMpKSBjb25zb2xlLmRlYnVnKGAke2tleX06YCwgdmFsdWUpICcKCmV2YWwobXlDb2RlKQ&amp;amp;theme=falcon&amp;amp;padding=16"&gt;link to eval function example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I apologise for getting carried away with the example...&lt;/p&gt;

&lt;p&gt;But this was different! This wasn't a line or 50, this was a whole file... surely there has to be a better way. There is!&lt;/p&gt;

&lt;p&gt;Step 3 was also something that took at least a little bit of consideration. I needed to make sure that the extensions wouldn't come and unload themselves as soon as they finish executing, it's for times like these I'm grateful for my short stint at Unity game development!&lt;/p&gt;

&lt;h3&gt;
  
  
  Solutions
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;This took the longest, but that's because everything else kept breaking!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I achieved this by creating a function which simply looked for the &lt;em&gt;extensions&lt;/em&gt; directory inside of the code's context, once inside we ~stole the jewels~ find all the .py files (could make this anything since it's plaintext files, maybe even .muggz?) and used a native library called importlib to swoop up the extensions code as if it was part of the program all along as an import!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NUPSdAWk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb1gghz6tx979xhi6hwc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NUPSdAWk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb1gghz6tx979xhi6hwc.png" alt="Python code example of module import function" width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;src: &lt;a href="https://ray.so/#width=920&amp;amp;code=ZGVmIGxvYWRfZXh0ZW5zaW9ucyhzdGF0ZTogU3RhdGVNYW5hZ2VyKSAtPiBOb25lOgogICAgcHJpbnQoIi0tIExvYWRpbmcgRXh0ZW5zaW9ucyAtLSIpCgogICAgaW1wb3J0IG9zCiAgICBpbXBvcnQgaW1wb3J0bGliCiAgICAKICAgIGZvciBmaWxlbmFtZSBpbiBvcy5saXN0ZGlyKGV4dGVuc2lvbnNfcGF0aCk6CiAgICAgICAgaWYgZmlsZW5hbWUuZW5kc3dpdGgoIi5weSIpOgogICAgICAgICAgICBwcmludChmIi0tIExvYWRpbmcge2ZpbGVuYW1lfSAtLSIpCiAgICAgICAgICAgIG1vZHVsZSA9IGltcG9ydGxpYi5pbXBvcnRfbW9kdWxlKCJleHRlbnNpb25zLiIgKyBmaWxlbmFtZVs6LTNdKQogICAgICAgICAgICBtb2R1bGUuZXh0LnJ1bihzdGF0ZSk&amp;amp;theme=raindrop&amp;amp;padding=16&amp;amp;language=python&amp;amp;darkMode=true"&gt;link to above image&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I provide all of these extensions with access to the central application state which we'll be discussing next!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To solve the dilemma of extensions doing their thing and going to buy milk when they ran out of lines to run - I opted to implement an Observer architectural pattern for the extensions to &lt;em&gt;subscribe&lt;/em&gt; to pieces of state in the application, and register events that would fire in the event that a piece of state was changed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This isn't a particularly advanced concept as it comes up quite intuitively when learning about front-end development, after all, an onclick event is merely the same concept.&lt;/p&gt;

&lt;p&gt;In the interest of not having my code absolutely roasted to oblivion (though, you're welcome to do that also because I'm not infallible) this meant the creation of a few classes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;state_manager.py: holds a key:value dictionary of pieces of state. This is the middleman that all extensions and main use as a source of truth. Coordinates access to pieces of state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;emitter.py: an Observable (which would also be a valid name), this allows for the un/subscription of functions to entities. Also holds the logic for triggering all functions subscribed via an &lt;em&gt;emit&lt;/em&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;state.py: inherits from emitter and is responsible for acting as an entry point for managing subscriptions, setter, and getter methods.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code chunks for these classes are a little chunkier than those previous, and so I'm going to elect to not provide screenshots - if you're curious please don't hesitate to chuck out the git repo linked above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Results
&lt;/h3&gt;

&lt;p&gt;It worked! For the sake of testing and MVP main looks like the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invoke StateManager to create a new store&lt;/li&gt;
&lt;li&gt;Try to import a state.yaml file from parent directory to populate state&lt;/li&gt;
&lt;li&gt;Create a state of &lt;strong&gt;name&lt;/strong&gt; and set it's value to &lt;em&gt;John&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Load extensions&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  print_name.py
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;subscribe to 'name' in the state_manager (&lt;em&gt;this will create state if not present, see print_age.py&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;every time name is updated, print that the name was updated, and increment the name_changed state
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;models.extension&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Extension&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;models.state_manager&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StateManager&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;StateManager&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""
        This extension prints the name whenever it is changed.
        この拡張機能は、名前が変更されるたびに名前を表示します。
    """&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"print_name extension: age was changed to"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name_changes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name_changes"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name_changes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"print_name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;print_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  print_age.py
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Very structurally similar to print_name.py with one key difference.&lt;/li&gt;
&lt;li&gt;Subscribe to 'age' state which does not exist &lt;strong&gt;YET&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;This still subscribed to a state against key of 'age' however, the value is set to null, with a single subscriber (print_age.py)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;models.extension&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Extension&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;models.state_manager&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StateManager&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;StateManager&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""
        This extension prints the age whenever it is changed.
        この拡張機能は、年齢が変更されるたびに年齢を表示します。
    """&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"print_age extension: value was set to"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"age_changes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"age_changes"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"age_changes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Extension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"print_age"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;print_age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change age to 30 (this invokes print_age.py's subscribed function)&lt;/li&gt;
&lt;li&gt;Change name to Jane (invoking print_name's function)&lt;/li&gt;
&lt;li&gt;print state_manager&lt;/li&gt;
&lt;li&gt;save to yaml&lt;/li&gt;
&lt;li&gt;end&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This concludes my first blog post and short adventure into extensible software! It was a lot of fun, and a short respite from my current fullstack personal project that I'm looking forward to talking about when the MVP is finished!&lt;/p&gt;

&lt;p&gt;Until then I hope you have a fantastic rest of your day/night!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>python</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
