Skip to content

Commit

Permalink
Add pint integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaphe committed Nov 25, 2020
1 parent dc3a4cc commit 83763e9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
22 changes: 15 additions & 7 deletions LaTeXDatax/datax.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# datax.py
import sys

def printvariable(tag,variable,f):
"""
Prints variable named "tag" with value "variable" to an iostream "f",
Expand Down Expand Up @@ -31,10 +33,14 @@ def printvariable(tag,variable,f):
if isinstance(variable,str):
print('\\pgfkeyssetvalue{/datax/%s}{%s}'%(tag,variable),file=f)
return
else:
number = variable
unit = ''
form = '%.4g'
if 'pint' in sys.modules: # Is this entire block an illegal waste of resources? Maybe. Will it cause more overhead time than I have already spent seeking a better solution? No.
from pint import Quantity
if isinstance(variable, Quantity):
print('\\pgfkeyssetvalue{{/datax/{}}}{{{:Lx}}}'.format(tag,variable),file=f) # ignores format specification
return
number = variable
unit = ''
form = '%.4g'
else: # variable is tuple
if len(variable) == 3:
number = variable[0]
Expand All @@ -56,13 +62,15 @@ def printvariable(tag,variable,f):
formatstring = '\\pgfkeyssetvalue{/datax/%s}{\\SI{'+form+'}{%s}}'
print(formatstring%(tag,number,unit),file=f)

def printvariables(f,**variables):
def printvariables(filename,**variables):
"""
Do `printvariable` for each of the given keyword arguments.
NB! filename is not a string, but an iostream. It is named thusly to minimize the number of names unavailable to the user.
"""
print('% File auto-generated by LaTeXDatax.py. Will be overwritten.',file=f)
print('% File auto-generated by LaTeXDatax.py. Will be overwritten.',file=filename)
for tag,variable in variables.items():
printvariable(tag,variable,f)
printvariable(tag,variable,filename)

def datax(filename="data.tex",**variables):
"""
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a python interface for [the datax LaTeX package [ctan]](https://ctan.org

## Installation
```
pip install "LaTeXDatax"
pip install LaTeXDatax
```

## Usage
Expand All @@ -31,6 +31,15 @@ In the LaTeX document:
\end{document}
```

## Pint integration
```python
from pint import Quantity
from LaTeXDatax import datax
datax(filename = "data.tex", E = Quantity(24,"kg m / s^2"))
```

Works as you would hope, in large part because of how well [pint](https://pint.readthedocs.io/) implements siunitx strings. Hat off.

## Looking for contributors
I don't know python very well. This package works, but if you have ideas for how to improve it, or you spot some python faux pas, feel free to make a PR or get in touch!

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='LaTeXDatax',
version='1.1',
version='1.2',
author='David Gustavsson',
author_email='david.e.gustavsson@gmail.com',
description='Export individual data from a Python script to a LaTeX document',
Expand Down
3 changes: 3 additions & 0 deletions tests/test_LaTeXDatax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest, io

from LaTeXDatax import printvariables
from pint import Quantity

class TestDatax(unittest.TestCase):
def test_datax(self):
Expand All @@ -14,6 +15,7 @@ def test_datax(self):
c = (3.141592,"\\meter"),
d = (3.141592,"\\meter","%.2g"),
e = (3.141592,"%.2g"),
f = Quantity(3.141592,"\\meter"),
)
f.seek(0)
written = f.read()
Expand All @@ -24,6 +26,7 @@ def test_datax(self):
\\pgfkeyssetvalue{/datax/c}{\\SI{3.142}{\\meter}}
\\pgfkeyssetvalue{/datax/d}{\\SI{3.1}{\\meter}}
\\pgfkeyssetvalue{/datax/e}{\\num{3.1}}
\\pgfkeyssetvalue{/datax/f}{\\SI[]{3.141592}{\\meter}}
"""
self.assertEqual(written,target)

Expand Down

0 comments on commit 83763e9

Please sign in to comment.