<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/assets/rss.xsl" type="text/xsl"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
	<title>Sacha Chua - category - babel</title>
	<atom:link href="https://sachachua.com/blog/category/babel/feed/index.xml" rel="self" type="application/rss+xml" />
	<atom:link href="https://sachachua.com/blog/category/babel" rel="alternate" type="text/html" />
	<link>https://sachachua.com/blog/category/babel/feed/index.xml</link>
	<description>Emacs, sketches, and life</description>
	<lastBuildDate>Fri, 08 Nov 2024 01:48:10 GMT</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>daily</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>11ty</generator>
  <item>
		<title>Python, Org Mode, and writing Org tables to CSVs so that I can read them back</title>
		<link>https://sachachua.com/blog/2019/10/python-org-mode-and-writing-org-tables-to-csvs-so-that-i-can-read-them-back/</link>
		<dc:creator><![CDATA[Sacha Chua]]></dc:creator>
		<pubDate>Sun, 27 Oct 2019 16:08:00 GMT</pubDate>
    <category>emacs</category>
<category>org</category>
<category>writing</category>
<category>babel</category>
<category>python</category>
		<guid isPermaLink="false">https://sachachua.com/blog/?p=29480</guid>
		<description><![CDATA[<p>I&#8217;ve been getting deeper into Python so that I can model our personal finances. I really like using the <code>pandas</code> library to manipulate data. All those years I spent trying to juggle increasing complex spreadsheets… Working with Python code in Org Babel blocks is just so much more fun. I like being able to keep my assumptions in tables without having to fuss around with naming cells for easy-to-read formulas, slice and summarize parts of my data frames, organize my notes in outlines and add commentary, and define more complicated functions that I don&#8217;t have to squeeze into a single line.</p>
<p>I haven&#8217;t quite been able to tempt W- into the world of Org Babel Python blocks. Still, I don&#8217;t want to give up the awesomeness of having pretty tables that I can easily edit and use. So I have a bunch of named tables (using <code>#+NAME:</code>), and some code that exports my tables to CSVs:</p>
<pre class="example"><code>#+NAME: tables</code>
<code>| Table         | Key                 |</code>
<code>|&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-+&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-|</code>
<code>| assets_w      | Description         |</code>
<code>| assets_s      | Description         |</code>
<code>| tax_rates     |                     |</code>
<code>| disposition   | Asset               |</code>
<code>| probate_rates | Asset               |</code>
<code>| basic         | Client information  |</code>
<code>| base_expenses | Category            |</code>
<code>| general       | General assumptions |</code>
<code></code>
<code>#+begin_src emacs-lisp :results silent :var tables=tables :tangle no</code>
<code>  (defun my-tbl-export (row)</code>
<code>    "Search for table named `NAME` and export."</code>
<code>    (interactive "s")</code>
<code>    (save-excursion</code>
<code>      (goto-char (point-min))</code>
<code>      (let ((case-fold-search t))</code>
<code>        (when (search-forward-regexp (concat "#\\+NAME: +" (car row)) nil t)</code>
<code>          (next-line)</code>
<code>          (org-table-export (format "%s.csv" (car row)) "orgtbl-to-csv")))))</code>
<code>  (mapc 'my-tbl-export tables)</code>
<code>#+end_src</code>
</pre>
<p>and some code that imports them back in, and formats tables nicely if I&#8217;m displaying them in Org. The <code>in_org</code> block doesn&#8217;t get tangled into index.py, so I don&#8217;t clutter command-line use with Org table markup.</p>
<pre class="example"><code>#+begin_src python :results silent :tangle no</code>
<code>  in_org=1</code>
<code>#+end_src</code>
<code></code>
<code>#+begin_src python :results silent :exports code</code>
<code>  import pandas as pd</code>
<code>  import numpy as np</code>
<code>  import orgbabelhelper as ob</code>
<code>  def out(df, **kwargs):</code>
<code>    if 'in_org' in globals():</code>
<code>      print(ob.dataframe_to_orgtable(df, **kwargs))</code>
<code>    else:</code>
<code>      print(df)</code>
<code>    return df</code>
<code>#+end_src</code>
<code></code>
<code>#+begin_src python :results silent :var tables=tables :colnames yes</code>
<code>  for row in tables:</code>
<code>    table = row[0]</code>
<code>    index = row[1] </code>
<code>    if row[1] == '':</code>
<code>      index = None</code>
<code>    globals()[table] = pd.read_csv(table + '.csv', index_col=index).apply(pd.to_numeric, errors='ignore')</code>
<code>    # print(globals()[table])</code>
<code>#+end_src</code>
</pre>
<p>Then I can use <code>C-c C-v C-b</code> (<code>org-babel-execute-buffer</code>) to update everything if I change the table in my Org file, and I can use <code>C-c C-v C-t</code> (<code>org-babel-tangle</code>) to create an index.py that W- can read through or run without needing Org.</p>
]]></description>
		</item>
	</channel>
</rss>