Posts

Showing posts from 2013

Context aware unit conversion in Pint

Today I am releasing version 0.4 of Pint, a Python units library. Pint is Python package to define, operate and manipulate physical quantities: the product of a numerical value and a unit of measurement. It allows arithmetic operations between them and conversions from and to different units. It provides a comprehensive and extensive list of physical units, prefixes and constants defined in a standalone text file. The registry can parse prefixed and pluralized forms of units resulting in a much shorter and maintainable unit definition list. It also provides great NumPy integration, with implicit unit conversion and an emphasis on correctness. What's new Pint 0.4 introduces the concept of Context. A Context enables to convert between unrelated dimensions based on pre-established rules. For example, in spectroscopy you might require to convert between wavelength and frequency. As expected, Pint will raise an error if you try to do this:     >>> import ...

Running Python code in a LaTeX document

Image
I am currently working on a LaTeX document in which the content is still in a very fluid phase and  the figures change often. I usually have a Python script that prepares the figures and saves them as PDFs. To iterate faster, I wanted to insert Python code directly into LaTeX, executing the code when the document is compiled. Googling around I found that I was not the first one to want this. There is this LaTeX package with this github repo . The package provides a new environment ( python ) within which you can write arbitrary Python code. The code is executed when the document is compiled and the output is inserted into the document. I try it and it worked like a charm. It was even printing the traceback in red when there was an error in the script. You can do things like this: \documentclass[a4paper]{book} \usepackage[pdftex]{graphicx} \usepackage{python} \begin{document} A simple example: \begin{python} print(2+3) \end{python} \vspace{1cm} Generating and displaying...

Make your functions units-aware with Pint 0.3

Image
Pint is Python package to define, operate and manipulate physical quantities : the product of a numerical value and a unit of measurement. It allows arithmetic operations between them and conversions from and to different units. >>> from pint import UnitRegistry >>> ureg = UnitRegistry() >>> q1 = 24. * ureg.meter >>> q2 = 10. * ureg.centimeters >>> q1 + q2 <Quantity(24.1, 'meter')> It provides a comprehensive and extensive list of physical units, prefixes and constants defined in a standalone text file. The registry can parse prefixed and pluralized forms of units resulting in a much shorter and maintainable unit definition list. It also provides great NumPy integration, with implicit unit conversion and an emphasis on correctness. >>> import numpy as np >>> np.cos([0, 45] * ureg.degrees) <Quantity([ 1. 0.70710678], 'dimensionless')> >>> np.cos([0, 45] * ureg.meter) Traceb...