<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Matt's Blog - python</title><link href="https://blog.wakayos.com/" rel="alternate"/><link href="https://blog.wakayos.com/feeds/python.atom.xml" rel="self"/><id>https://blog.wakayos.com/</id><updated>2023-03-19T00:00:00-04:00</updated><subtitle>Developer, Devops Guy in a Govtech World</subtitle><entry><title>Creating a useful and funny python serializer</title><link href="https://blog.wakayos.com/python-markpickle-2023.html" rel="alternate"/><published>2023-03-19T00:00:00-04:00</published><updated>2023-03-19T00:00:00-04:00</updated><author><name>Matthew Martin</name></author><id>tag:blog.wakayos.com,2023-03-19:/python-markpickle-2023.html</id><summary type="html">&lt;p&gt;I wrote a library that serializes and deserializes markdown to and from python, but more in the style of pickle&lt;/p&gt;</summary><content type="html">&lt;p&gt;and less in the style of a document object model.&lt;/p&gt;
&lt;h1&gt;Hello markpickle&lt;/h1&gt;
&lt;p&gt;I got the name from ChatGPT.&lt;/p&gt;
&lt;h2&gt;Origin Story&lt;/h2&gt;
&lt;p&gt;I wanted an API to support as many mime types as possible. Some mime types are for the people, such as PDF, Excel/CSV.
Some time types are for the machines, like json, bson.&lt;/p&gt;
&lt;p&gt;I thought, what about HTML and text? HTML has a problem with XSS risks. Text has a problem of not having much of a
standard story for "styled" plaintext. Except for Markdown. Can I use markdown as a mime type for what starts out as a
JSON object?&lt;/p&gt;
&lt;h2&gt;Challenges&lt;/h2&gt;
&lt;p&gt;The app is two functions, loads and dumps and they take or return an &lt;code&gt;Any&lt;/code&gt; type and take or return a string. The &lt;code&gt;Any&lt;/code&gt;
is a problem, because mypy doesn't know how to deal with arbitrarily complex types. It seems to get confused with
&lt;code&gt;Union&lt;/code&gt;s of &lt;code&gt;Union&lt;/code&gt;s of &lt;code&gt;Union&lt;/code&gt;s. It gets confused with generics, e.g. lists that can work with any contents.&lt;/p&gt;
&lt;h2&gt;Impedance Mismatches&lt;/h2&gt;
&lt;p&gt;Markdown has some analogies:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;header groupings + paragraphs make dicts, or even nested dicts&lt;/li&gt;
&lt;li&gt;Lists or lists of lists&lt;/li&gt;
&lt;li&gt;tables are dicts, but you can only put scalars in the values. This makes python objects of a certain level of nesting
  unrepresentable.&lt;/li&gt;
&lt;li&gt;strings and non-string types can be represented, but you can only infer a number should be integer. This blocks round
  tripping.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Some things are impossible&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;empty dictionaries, empty lists exist in python but have no representation in markdown. An empty list is just
  zero-length whitespace where the list is not. This blocks round tripping.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Python has no use for a lot of markdown formatting, such as bold, or code block or quote. Those things would just be
strings and they'd continue to hold markdown in the string.&lt;/p&gt;</content><category term="python"/><category term="python"/></entry></feed>