Popular recipes tagged "units"http://code.activestate.com/recipes/tags/units/2014-01-03T01:29:28-08:00ActiveState Code RecipesTemperature Conversation Application in Python (Python)
2014-01-03T01:29:28-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578804-temperature-conversation-application-in-python/
<p style="color: grey">
Python
recipe 578804
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/temperature/">temperature</a>, <a href="/recipes/tags/units/">units</a>).
</p>
<p>This is a basic program in python that can cover between units of temperature. This was written for an article on <a href="http://thelivingpearl.com/2014/01/02/temperature-conversation-application-in-python/">How to use modules in Python</a>. </p>
Unit Conversions Using Decimal (Python)
2011-04-13T06:55:05-07:00Dave Baileyhttp://code.activestate.com/recipes/users/4168479/http://code.activestate.com/recipes/577652-unit-conversions-using-decimal/
<p style="color: grey">
Python
recipe 577652
by <a href="/recipes/users/4168479/">Dave Bailey</a>
(<a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/units/">units</a>).
</p>
<p>A table driven routine generates a unit conversion module and a doctest file. The generated module uses the Decimal class to create unit conversion classes which uses class properties to perform the conversion. conversion.py supplies the following classes:
Distance
Area
Volumn
Time
Velocity
Acceleration
Mass
Force
Weight
Pressure
Frequency
Power
Temperature</p>
<p>New classes and additional units can easily be created by adding to the table.</p>
Numerical type with units (dimensions.py) (Python)
2010-07-24T17:24:13-07:00David Klaffenbachhttp://code.activestate.com/recipes/users/2007923/http://code.activestate.com/recipes/577333-numerical-type-with-units-dimensionspy/
<p style="color: grey">
Python
recipe 577333
by <a href="/recipes/users/2007923/">David Klaffenbach</a>
(<a href="/recipes/tags/dimensions/">dimensions</a>, <a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/units/">units</a>).
</p>
<p>I implemented dimensions.py perhaps eight years ago as an exercise and have used it occasionally ever since.</p>
<p>It allows doing math with dimensioned values in order to automate unit conversions (you can add m/s to mile/hour) and dimensional checking (you can't add m/s to mile/lightyear). It specifically does not convert 212F to 100C but rather will convert 9F to 5C (valid when converting temperature differences).</p>
<p>It is similar to unums (<a href="http://home.scarlet.be/be052320/Unum.html" rel="nofollow">http://home.scarlet.be/be052320/Unum.html</a>) but with a significant difference:</p>
<p>I used a different syntax Q(25,'m/s') as opposed to 100*m/s (I recall not wanting to have all the base SI units directly in the namespace). I'm not entirely sure which approach is really better.</p>
<p>I also had a specific need to have fractional exponents on units, allowing the following:</p>
<pre class="prettyprint"><code>>>> km=Q(10,'N*m/W^(1/2)')
>>> km
Q(10.0, 'kg**0.5*m/s**0.5')
</code></pre>
<p>Looking back I see a few design decisions I might do differently today, but I'll share it anyway.</p>
<p>Some examples are in the source below the line with if __name__ == "__main__":</p>
<p>Note that I've put two files into the code block below, dimensions.py and dimensions.data, so please cut them apart if you want to try it.</p>
Metric Prefix Symbol and Decimal Quiz (Python)
2009-10-11T18:39:58-07:00Daniel Sahahttp://code.activestate.com/recipes/users/4171644/http://code.activestate.com/recipes/576928-metric-prefix-symbol-and-decimal-quiz/
<p style="color: grey">
Python
recipe 576928
by <a href="/recipes/users/4171644/">Daniel Saha</a>
(<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/metric/">metric</a>, <a href="/recipes/tags/prefix/">prefix</a>, <a href="/recipes/tags/symbol/">symbol</a>, <a href="/recipes/tags/units/">units</a>).
Revision 3.
</p>
<p>Quiz on the metric prefixs, symbols, and decimals associated with each other.</p>