diff --git a/_posts/2019-07-15-MindMap.md b/_posts/2019-07-15-MindMap.md new file mode 100644 index 0000000..841cd98 --- /dev/null +++ b/_posts/2019-07-15-MindMap.md @@ -0,0 +1,128 @@ +--- +layout: post +title: "Mind map generation with Python" +description: "Step 1" +date: 2019-07-15 18:38:00 +categories: projects frc +--- + +While working on an assignment with [Coggle](https://coggle.it) today, I noticed an interesting option in the save menu. *Download as .mm file*. Having rarely worked with mind maps before, and only doing it online, it never occured to me that someone would have a file format for it. So I took a look. + +## What is a .mm file? +It turns out, a `.mm` file is just some XML describing the mind map. Here is a simple mind map: + +![Simple Mind Map](/assets/images/mindmap-simple.png) + +And again as a `.mm` file: + +```xml + + + + + + + + + + + + + + + + + + +``` + +Neat, right? + +## What can we do with it? +I have not done much research about this because I wanted to work all of this out on my own. But I know one thing as a fact: working with XML sucks (especially in Python). I decided that this would be much better if I could load `.mm` files as JSON. This would allow easy manipulation and some cool projects. + +## My script +Like everything I do, I made a script to play with these files. + +It's pretty simple. First, It loads a `.mm` file, then parses it into a `list` of `xml.etree.ElementTree.Element`. + +```python +raw_mm = "" + +with open(args.file, "r") as fp: + raw_mm = fp.read() + fp.close() + +xml = ET.fromstring(raw_mm) +``` + +The parsed `list` is then passed into a recursive function that constructs a `dict` + +```python +def xmlToDict(xml): + output = [] + for elem in list(xml): + + if "TEXT" not in elem.attrib: + continue + + name = elem.attrib['TEXT'] + json_element = {"name": name} + + try: + json_element["children"] = xmlToDict(elem) + except: + continue + + # Detect node type + if json_element["children"]: + json_element["type"] = "branch" + else: + json_element["type"] = "leaf" + del json_element["children"] + + output.append(json_element) + + return output +``` + +Finally, the `dict` is written to a file with `json.dump` + +```python +json.dump(mind_map, open(args.file + ".json", "w")) +``` + +The whole script (with comments) can be found on my [GitHub account](https://gist.github.com/Ewpratten/0d8f7c7371380c9ca8adcfc6502ccf84#file-parser-py). + +## The output +Running the `.mm` file from above through the script gives: + +```json +[ + { + "name":"Master Node", + "children":[ + { + "name":"Child branch", + "children":[ + { + "name":"Children 1", + "type":"leaf" + }, + { + "name":"Children 2", + "type":"leaf" + } + ], + "type":"branch" + } + ], + "type":"branch" + } +] +``` + +## The next step +This script just translates a `.mm` file to JSON. Nothing else. Next, I want to convert this to a library, and add a JSON to `.mm` function as well. This leads into my ultimate goal for this project. + +I want a script that I can drop in the root of any project to build a [Gource](https://gource.io/)-style visualization of the folder structure. This will give me a way to make cool visualizations for lessons on the robotics team. \ No newline at end of file diff --git a/_site/all_posts.html b/_site/all_posts.html index bc791df..bd61e67 100644 --- a/_site/all_posts.html +++ b/_site/all_posts.html @@ -96,6 +96,13 @@
    + +
  • Mind map generation with Python

  • + + + + +
  • Taking a look back at GMAD

  • diff --git a/_site/assets/images/mindmap-simple.png b/_site/assets/images/mindmap-simple.png new file mode 100644 index 0000000..44565fe Binary files /dev/null and b/_site/assets/images/mindmap-simple.png differ diff --git a/_site/blog/2019/07/15/mindmap.html b/_site/blog/2019/07/15/mindmap.html new file mode 100644 index 0000000..bbb1557 --- /dev/null +++ b/_site/blog/2019/07/15/mindmap.html @@ -0,0 +1,264 @@ + + + + + Evan Pratten + + + + + + + + + + + + + + + +
    + + + + + + + + + + + +
    + + +
    +
    + +

    While working on an assignment with Coggle today, I noticed an interesting option in the save menu. Download as .mm file. Having rarely worked with mind maps before, and only doing it online, it never occured to me that someone would have a file format for it. So I took a look.

    + +

    What is a .mm file?

    +

    It turns out, a .mm file is just some XML describing the mind map. Here is a simple mind map:

    + +

    Simple Mind Map

    + +

    And again as a .mm file:

    + +
    <map version="0.9.0">
    +    <node TEXT="Master Node" FOLDED="false" POSITION="right" ID="5d2d02b1a315dd0879f48c1c" X_COGGLE_POSX="0" X_COGGLE_POSY="0">
    +        <edge COLOR="#b4b4b4"/>
    +        <font NAME="Helvetica" SIZE="17"/>
    +        <node TEXT="Child branch" FOLDED="false" POSITION="right" ID="f72704969525d2a0333dd635">
    +            <edge COLOR="#7aa3e5"/>
    +            <font NAME="Helvetica" SIZE="15"/>
    +            <node TEXT="Children 1" FOLDED="false" POSITION="right" ID="c83826af506cae6e55761d5c">
    +                <edge COLOR="#7ea7e5"/>
    +                <font NAME="Helvetica" SIZE="13"/>
    +            </node>
    +            <node TEXT="Children 2" FOLDED="false" POSITION="right" ID="47723a4d0fb766863f70d204">
    +                <edge COLOR="#82aae7"/>
    +                <font NAME="Helvetica" SIZE="13"/>
    +            </node>
    +        </node>
    +    </node>
    +</map>
    +
    + +

    Neat, right?

    + +

    What can we do with it?

    +

    I have not done much research about this because I wanted to work all of this out on my own. But I know one thing as a fact: working with XML sucks (especially in Python). I decided that this would be much better if I could load .mm files as JSON. This would allow easy manipulation and some cool projects.

    + +

    My script

    +

    Like everything I do, I made a script to play with these files.

    + +

    It’s pretty simple. First, It loads a .mm file, then parses it into a list of xml.etree.ElementTree.Element.

    + +
    raw_mm = ""
    +
    +with open(args.file, "r") as fp:
    +    raw_mm = fp.read()
    +    fp.close()
    +
    +xml = ET.fromstring(raw_mm)
    +
    + +

    The parsed list is then passed into a recursive function that constructs a dict

    + +
    def xmlToDict(xml):
    +    output = []
    +    for elem in list(xml):
    +
    +        if "TEXT" not in elem.attrib:
    +            continue
    +        
    +        name = elem.attrib['TEXT']
    +        json_element = {"name": name}
    +
    +        try:            
    +            json_element["children"] = xmlToDict(elem)
    +        except:
    +            continue
    +        
    +        # Detect node type
    +        if json_element["children"]:
    +            json_element["type"] = "branch"
    +        else:
    +            json_element["type"] = "leaf"
    +            del json_element["children"]
    +        
    +        output.append(json_element)
    +    
    +    return output
    +
    + +

    Finally, the dict is written to a file with json.dump

    + +
    json.dump(mind_map, open(args.file + ".json", "w"))
    +
    + +

    The whole script (with comments) can be found on my GitHub account.

    + +

    The output

    +

    Running the .mm file from above through the script gives:

    + +
    [
    +    {
    +        "name":"Master Node",
    +        "children":[
    +            {
    +                "name":"Child branch",
    +                "children":[
    +                    {
    +                        "name":"Children 1",
    +                        "type":"leaf"
    +                    },
    +                    {
    +                        "name":"Children 2",
    +                        "type":"leaf"
    +                    }
    +                ],
    +                "type":"branch"
    +            }
    +        ],
    +        "type":"branch"
    +    }
    +]
    +
    + +

    The next step

    +

    This script just translates a .mm file to JSON. Nothing else. Next, I want to convert this to a library, and add a JSON to .mm function as well. This leads into my ultimate goal for this project.

    + +

    I want a script that I can drop in the root of any project to build a Gource-style visualization of the folder structure. This will give me a way to make cool visualizations for lessons on the robotics team.

    +

    +
    +
    + +
    + + + + +
    + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/_site/feed.xml b/_site/feed.xml index 7052881..cab9c7b 100644 --- a/_site/feed.xml +++ b/_site/feed.xml @@ -1,4 +1,118 @@ -Jekyll2019-07-15T11:11:19-04:00http://0.0.0.0:4000/feed.xmlEvan PrattenComputer wizard, student, <a href="https://github.com/frc5024">@frc5024</a> programming team lead, and radio enthusiast.Taking a look back at GMAD2019-07-13T10:43:00-04:002019-07-13T10:43:00-04:00http://0.0.0.0:4000/blog/2019/07/13/Lookback-GMAD<p>One day, back in June of 2018, I was both looking for a new project to work on, and trying to decide which Linux distro to install on one of my computers. From this, a little project was born. <a href="/gmad">Give Me a Distro</a> (or, GMAD, as I like to call it) is a little website that chooses a random distribution of Linux and shows a description of what you are about to get yourself into, and a download link for the latest ISO.</p> +Jekyll2019-07-15T19:01:28-04:00http://0.0.0.0:4000/feed.xmlEvan PrattenComputer wizard, student, <a href="https://github.com/frc5024">@frc5024</a> programming team lead, and radio enthusiast.Mind map generation with Python2019-07-15T14:38:00-04:002019-07-15T14:38:00-04:00http://0.0.0.0:4000/blog/2019/07/15/MindMap<p>While working on an assignment with <a href="https://coggle.it">Coggle</a> today, I noticed an interesting option in the save menu. <em>Download as .mm file</em>. Having rarely worked with mind maps before, and only doing it online, it never occured to me that someone would have a file format for it. So I took a look.</p> + +<h2 id="what-is-a-mm-file">What is a .mm file?</h2> +<p>It turns out, a <code class="highlighter-rouge">.mm</code> file is just some XML describing the mind map. Here is a simple mind map:</p> + +<p><img src="/assets/images/mindmap-simple.png" alt="Simple Mind Map" /></p> + +<p>And again as a <code class="highlighter-rouge">.mm</code> file:</p> + +<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;map</span> <span class="na">version=</span><span class="s">"0.9.0"</span><span class="nt">&gt;</span> + <span class="nt">&lt;node</span> <span class="na">TEXT=</span><span class="s">"Master Node"</span> <span class="na">FOLDED=</span><span class="s">"false"</span> <span class="na">POSITION=</span><span class="s">"right"</span> <span class="na">ID=</span><span class="s">"5d2d02b1a315dd0879f48c1c"</span> <span class="na">X_COGGLE_POSX=</span><span class="s">"0"</span> <span class="na">X_COGGLE_POSY=</span><span class="s">"0"</span><span class="nt">&gt;</span> + <span class="nt">&lt;edge</span> <span class="na">COLOR=</span><span class="s">"#b4b4b4"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;font</span> <span class="na">NAME=</span><span class="s">"Helvetica"</span> <span class="na">SIZE=</span><span class="s">"17"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;node</span> <span class="na">TEXT=</span><span class="s">"Child branch"</span> <span class="na">FOLDED=</span><span class="s">"false"</span> <span class="na">POSITION=</span><span class="s">"right"</span> <span class="na">ID=</span><span class="s">"f72704969525d2a0333dd635"</span><span class="nt">&gt;</span> + <span class="nt">&lt;edge</span> <span class="na">COLOR=</span><span class="s">"#7aa3e5"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;font</span> <span class="na">NAME=</span><span class="s">"Helvetica"</span> <span class="na">SIZE=</span><span class="s">"15"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;node</span> <span class="na">TEXT=</span><span class="s">"Children 1"</span> <span class="na">FOLDED=</span><span class="s">"false"</span> <span class="na">POSITION=</span><span class="s">"right"</span> <span class="na">ID=</span><span class="s">"c83826af506cae6e55761d5c"</span><span class="nt">&gt;</span> + <span class="nt">&lt;edge</span> <span class="na">COLOR=</span><span class="s">"#7ea7e5"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;font</span> <span class="na">NAME=</span><span class="s">"Helvetica"</span> <span class="na">SIZE=</span><span class="s">"13"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;/node&gt;</span> + <span class="nt">&lt;node</span> <span class="na">TEXT=</span><span class="s">"Children 2"</span> <span class="na">FOLDED=</span><span class="s">"false"</span> <span class="na">POSITION=</span><span class="s">"right"</span> <span class="na">ID=</span><span class="s">"47723a4d0fb766863f70d204"</span><span class="nt">&gt;</span> + <span class="nt">&lt;edge</span> <span class="na">COLOR=</span><span class="s">"#82aae7"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;font</span> <span class="na">NAME=</span><span class="s">"Helvetica"</span> <span class="na">SIZE=</span><span class="s">"13"</span><span class="nt">/&gt;</span> + <span class="nt">&lt;/node&gt;</span> + <span class="nt">&lt;/node&gt;</span> + <span class="nt">&lt;/node&gt;</span> +<span class="nt">&lt;/map&gt;</span> +</code></pre></div></div> + +<p>Neat, right?</p> + +<h2 id="what-can-we-do-with-it">What can we do with it?</h2> +<p>I have not done much research about this because I wanted to work all of this out on my own. But I know one thing as a fact: working with XML sucks (especially in Python). I decided that this would be much better if I could load <code class="highlighter-rouge">.mm</code> files as JSON. This would allow easy manipulation and some cool projects.</p> + +<h2 id="my-script">My script</h2> +<p>Like everything I do, I made a script to play with these files.</p> + +<p>It’s pretty simple. First, It loads a <code class="highlighter-rouge">.mm</code> file, then parses it into a <code class="highlighter-rouge">list</code> of <code class="highlighter-rouge">xml.etree.ElementTree.Element</code>.</p> + +<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">raw_mm</span> <span class="o">=</span> <span class="s">""</span> + +<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="nb">file</span><span class="p">,</span> <span class="s">"r"</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span> + <span class="n">raw_mm</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> + <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> + +<span class="n">xml</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">raw_mm</span><span class="p">)</span> +</code></pre></div></div> + +<p>The parsed <code class="highlighter-rouge">list</code> is then passed into a recursive function that constructs a <code class="highlighter-rouge">dict</code></p> + +<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">xmlToDict</span><span class="p">(</span><span class="n">xml</span><span class="p">):</span> + <span class="n">output</span> <span class="o">=</span> <span class="p">[]</span> + <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">xml</span><span class="p">):</span> + + <span class="k">if</span> <span class="s">"TEXT"</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">elem</span><span class="o">.</span><span class="n">attrib</span><span class="p">:</span> + <span class="k">continue</span> + + <span class="n">name</span> <span class="o">=</span> <span class="n">elem</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s">'TEXT'</span><span class="p">]</span> + <span class="n">json_element</span> <span class="o">=</span> <span class="p">{</span><span class="s">"name"</span><span class="p">:</span> <span class="n">name</span><span class="p">}</span> + + <span class="k">try</span><span class="p">:</span> + <span class="n">json_element</span><span class="p">[</span><span class="s">"children"</span><span class="p">]</span> <span class="o">=</span> <span class="n">xmlToDict</span><span class="p">(</span><span class="n">elem</span><span class="p">)</span> + <span class="k">except</span><span class="p">:</span> + <span class="k">continue</span> + + <span class="c1"># Detect node type +</span> <span class="k">if</span> <span class="n">json_element</span><span class="p">[</span><span class="s">"children"</span><span class="p">]:</span> + <span class="n">json_element</span><span class="p">[</span><span class="s">"type"</span><span class="p">]</span> <span class="o">=</span> <span class="s">"branch"</span> + <span class="k">else</span><span class="p">:</span> + <span class="n">json_element</span><span class="p">[</span><span class="s">"type"</span><span class="p">]</span> <span class="o">=</span> <span class="s">"leaf"</span> + <span class="k">del</span> <span class="n">json_element</span><span class="p">[</span><span class="s">"children"</span><span class="p">]</span> + + <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">json_element</span><span class="p">)</span> + + <span class="k">return</span> <span class="n">output</span> +</code></pre></div></div> + +<p>Finally, the <code class="highlighter-rouge">dict</code> is written to a file with <code class="highlighter-rouge">json.dump</code></p> + +<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">mind_map</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="nb">file</span> <span class="o">+</span> <span class="s">".json"</span><span class="p">,</span> <span class="s">"w"</span><span class="p">))</span> +</code></pre></div></div> + +<p>The whole script (with comments) can be found on my <a href="https://gist.github.com/Ewpratten/0d8f7c7371380c9ca8adcfc6502ccf84#file-parser-py">GitHub account</a>.</p> + +<h2 id="the-output">The output</h2> +<p>Running the <code class="highlighter-rouge">.mm</code> file from above through the script gives:</p> + +<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">[</span><span class="w"> + </span><span class="p">{</span><span class="w"> + </span><span class="s2">"name"</span><span class="p">:</span><span class="s2">"Master Node"</span><span class="p">,</span><span class="w"> + </span><span class="s2">"children"</span><span class="p">:[</span><span class="w"> + </span><span class="p">{</span><span class="w"> + </span><span class="s2">"name"</span><span class="p">:</span><span class="s2">"Child branch"</span><span class="p">,</span><span class="w"> + </span><span class="s2">"children"</span><span class="p">:[</span><span class="w"> + </span><span class="p">{</span><span class="w"> + </span><span class="s2">"name"</span><span class="p">:</span><span class="s2">"Children 1"</span><span class="p">,</span><span class="w"> + </span><span class="s2">"type"</span><span class="p">:</span><span class="s2">"leaf"</span><span class="w"> + </span><span class="p">},</span><span class="w"> + </span><span class="p">{</span><span class="w"> + </span><span class="s2">"name"</span><span class="p">:</span><span class="s2">"Children 2"</span><span class="p">,</span><span class="w"> + </span><span class="s2">"type"</span><span class="p">:</span><span class="s2">"leaf"</span><span class="w"> + </span><span class="p">}</span><span class="w"> + </span><span class="p">],</span><span class="w"> + </span><span class="s2">"type"</span><span class="p">:</span><span class="s2">"branch"</span><span class="w"> + </span><span class="p">}</span><span class="w"> + </span><span class="p">],</span><span class="w"> + </span><span class="s2">"type"</span><span class="p">:</span><span class="s2">"branch"</span><span class="w"> + </span><span class="p">}</span><span class="w"> +</span><span class="p">]</span><span class="w"> +</span></code></pre></div></div> + +<h2 id="the-next-step">The next step</h2> +<p>This script just translates a <code class="highlighter-rouge">.mm</code> file to JSON. Nothing else. Next, I want to convert this to a library, and add a JSON to <code class="highlighter-rouge">.mm</code> function as well. This leads into my ultimate goal for this project.</p> + +<p>I want a script that I can drop in the root of any project to build a <a href="https://gource.io/">Gource</a>-style visualization of the folder structure. This will give me a way to make cool visualizations for lessons on the robotics team.</p>While working on an assignment with Coggle today, I noticed an interesting option in the save menu. Download as .mm file. Having rarely worked with mind maps before, and only doing it online, it never occured to me that someone would have a file format for it. So I took a look.Taking a look back at GMAD2019-07-13T10:43:00-04:002019-07-13T10:43:00-04:00http://0.0.0.0:4000/blog/2019/07/13/Lookback-GMAD<p>One day, back in June of 2018, I was both looking for a new project to work on, and trying to decide which Linux distro to install on one of my computers. From this, a little project was born. <a href="/gmad">Give Me a Distro</a> (or, GMAD, as I like to call it) is a little website that chooses a random distribution of Linux and shows a description of what you are about to get yourself into, and a download link for the latest ISO.</p> <h2 id="backend-tech">Backend tech</h2> <p>This is one of the simplest projects I have ever made. All the backend does is:</p> @@ -481,15 +595,4 @@ __<span class="o">()</span> <span class="o"&g <li>Have mentors do “guest presentations”</li> <li>Dedicate a day to robot driving lessons</li> <li>Use a custom library with wrappers and tools built by me to provide easy interfaces for new programmers</li> -</ul>Over the past two years (2018 / 2019), I have been a member of my school’s FRC team, Raider Robotics. Specifically, a programmer.I made a new song!2019-06-17T06:20:00-04:002019-06-17T06:20:00-04:00http://0.0.0.0:4000/blog/2019/06/17/AMM2M1-release<p>I am currently taking a class in school called <a href="https://www.facebook.com/studio225beal/">Music and computers (AMM2M)</a>, where as part of the class, whe get together into bands, and produce a song. After taking a break from music production for over a year, we have released our song for the class (we do two songs, but the second is not finished yet).</p> - -<h2 id="my-contribution">My contribution</h2> -<p>My main contribution to the project was a mix of live drumming, and most of the song’s MIDI work. The song is far from perfect, but pretty good for the time we had to produce it.</p> - -<h2 id="just-give-me-the-song">Just give me the song!</h2> -<p>Ok. Ok. Here is the song:</p> - -<audio controls=""> -<source src="/assets/audio/SpamPhoneCalls.mp3" type="audio/mpeg" /> -Your browser does not support audio players -</audio>I am currently taking a class in school called Music and computers (AMM2M), where as part of the class, whe get together into bands, and produce a song. After taking a break from music production for over a year, we have released our song for the class (we do two songs, but the second is not finished yet). \ No newline at end of file +</ul>Over the past two years (2018 / 2019), I have been a member of my school’s FRC team, Raider Robotics. Specifically, a programmer. \ No newline at end of file diff --git a/_site/index.html b/_site/index.html index 4278a28..e1f67b5 100644 --- a/_site/index.html +++ b/_site/index.html @@ -127,12 +127,12 @@ diff --git a/assets/images/mindmap-simple.png b/assets/images/mindmap-simple.png new file mode 100644 index 0000000..44565fe Binary files /dev/null and b/assets/images/mindmap-simple.png differ