Popular Python recipes tagged "meta:requires=decimal"http://code.activestate.com/recipes/langs/python/tags/meta:requires=decimal/2016-09-18T20:39:20-07:00ActiveState Code RecipesDBF reader and writer -- selective fields and nullreplace (Python)
2016-09-18T20:39:20-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/580696-dbf-reader-and-writer-selective-fields-and-nullrep/
<p style="color: grey">
Python
recipe 580696
by <a href="/recipes/users/4189558/">Tomas Nordin</a>
(<a href="/recipes/tags/database/">database</a>).
</p>
<p>This fork assumes a desire for limited selection of field names. With
huge files this might be necessary on some machines.</p>
<p>Also, assuming that the meaning of null in a dbf file means zero might
be a mistake, so the fork adds an argument nullreplace as way to
choose what to replace null with. Null is sometimes used to mean
missing value. This change is decoupled from the selective names
feature.</p>
Configurable JSON Extensions for Python (Python)
2016-05-22T19:00:54-07:00Michael Blan Palmerhttp://code.activestate.com/recipes/users/4194130/http://code.activestate.com/recipes/580664-configurable-json-extensions-for-python/
<p style="color: grey">
Python
recipe 580664
by <a href="/recipes/users/4194130/">Michael Blan Palmer</a>
(<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>The concept behind the package is to build a single class per type you want to add to json in Python. The new class will have a method for encoding to json and a method for decoding from json. The classes are then loaded into an encoder object and a decoder object that are hooked into the standard json loads
and dumps functions.</p>
10 lines for a beginner to try out the Spyder IDE for python programming (Python)
2015-09-18T15:04:09-07:00mai3ahttp://code.activestate.com/recipes/users/4192857/http://code.activestate.com/recipes/579100-10-lines-for-a-beginner-to-try-out-the-spyder-ide-/
<p style="color: grey">
Python
recipe 579100
by <a href="/recipes/users/4192857/">mai3a</a>
(<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/snippet/">snippet</a>, <a href="/recipes/tags/spyder/">spyder</a>, <a href="/recipes/tags/stuffthatworks/">stuffthatworks</a>).
</p>
<p>This python code generates no lint errors and just print out a few numbers.</p>
<p>Useful if you use the Spyder IDE for the first time, which provides NOTHING to get you started.</p>
Engineering notation. (Python)
2015-04-15T16:17:12-07:00Paul Sargenthttp://code.activestate.com/recipes/users/4192033/http://code.activestate.com/recipes/579046-engineering-notation/
<p style="color: grey">
Python
recipe 579046
by <a href="/recipes/users/4192033/">Paul Sargent</a>
(<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/ipython/">ipython</a>).
</p>
<p>eng(x) returns a string representing x using the "engineering notation"</p>
Pseudo-Random Experiment: Coin Toss (Python)
2014-03-31T18:51:15-07:00Fabian Mayerhttp://code.activestate.com/recipes/users/4189629/http://code.activestate.com/recipes/578859-pseudo-random-experiment-coin-toss/
<p style="color: grey">
Python
recipe 578859
by <a href="/recipes/users/4189629/">Fabian Mayer</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/toss/">toss</a>).
</p>
<p>This Python Script flips a coin a user defined number of times and returns the frequency and relative frequency for heads and tails.</p>
Continued fractions (Python)
2013-08-22T07:15:47-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/578647-continued-fractions/
<p style="color: grey">
Python
recipe 578647
by <a href="/recipes/users/4175415/">Glenn Hutchings</a>
(<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/fractions/">fractions</a>).
Revision 2.
</p>
<p>A class representing a <a href="https://en.wikipedia.org/wiki/Continued_fraction">continued fraction</a>.</p>
Complex Methods to Compute Pi in Python (Python)
2013-06-03T18:08:19-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578548-complex-methods-to-compute-pi-in-python/
<p style="color: grey">
Python
recipe 578548
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This script was written for an article I wrote about computing Pi with Python. This module uses the Decimal library. For more information <a href="http://thelivingpearl.com/2013/05/28/computing-pi-with-python/">How to Compute Pi in Python</a></p>
Archimedes Method for PI (arbitrary precision) (Python)
2013-03-02T21:56:42-08:00Bjorn Madsenhttp://code.activestate.com/recipes/users/4176352/http://code.activestate.com/recipes/578478-archimedes-method-for-pi-arbitrary-precision/
<p style="color: grey">
Python
recipe 578478
by <a href="/recipes/users/4176352/">Bjorn Madsen</a>
(<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/precision/">precision</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>The code below incorporates the newly revised python 3.3.0 decimal module to permit arbitrary precision (<a href="http://docs.python.org/3/library/decimal.html%29" rel="nofollow">http://docs.python.org/3/library/decimal.html)</a></p>
<p>The recipe was originally by FB35 from <a href="http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/" rel="nofollow">http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/</a> </p>
Rudimentary Database Engine (Python)
2013-09-02T01:51:53-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577825-rudimentary-database-engine/
<p style="color: grey">
Python
recipe 577825
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/experiment/">experiment</a>).
Revision 2.
</p>
<p>This module was written for self-academic purposes in an attempt to understand databases better. Just as in college where students are required to reinvent "wheels" like linked lists, trees, binary searches, et cetera, developing this program helped me understand some of the things that goes on behind the scenes in a database while also helping to learn what the desired output of each operation should be. The code in this module provides support for running a simple database engine that runs completely in memory and allows usage of various concepts available in a structured query language to get and set data that may be saved to file.</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>
Maclaurin's_series_ln(x)_decimal_module_version (Python)
2011-01-20T12:09:24-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577307-maclaurins_series_lnx_decimal_module_version/
<p style="color: grey">
Python
recipe 577307
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 5.
</p>
<p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p>
<p>The maclaurin's series approximation for ln(1+x) is used to approximate ln(x) by detucting the maclaurin's series for ln(1-x). ln(1+x) - ln(1-x) = ln(1+x/1-x)and by letting y = (1+x/1-x), y could take any positive value.</p>
<p>y = 1+x/1-x, therefore x = y-1/y+1 </p>
<p>-1 < x < 1</p>
Binary floating point summation accurate to full precision (Python)
2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/
<p style="color: grey">
Python
recipe 393090
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p>
nth-Root (Python)
2011-01-29T12:29:38-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/576857-nth-root/
<p style="color: grey">
Python
recipe 576857
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 5.
</p>
<p>I realised a lack of an nth root function within Python maths' modules, even though, you could fulfil the task by using the power function, and I wrote nth-Root program.
However, I used Newton’s method Xn+1 = Xn - f(Xn)/f’(Xn) , n = 1,2,3... and worked out myself the formula to solve the equation X^(b) - a for nth-root(a) as follows:
Xn+1 = 1/b((b-1)Xn + a/Xn^(b-1)). Though my program nth-Root uses a generator to generate each value approximation in the sequence from its predecessor, and it display the nth root value once two equal values are generated.
Nowadays Scientifics’ calculators display 9 digits to the right of a decimal point and my program nth-Root displays a 16 figures precision to the right of a decimal point.</p>
sin, cos, tan for Decimal (Python)
2007-07-05T19:49:49-07:00Alain Mellanhttp://code.activestate.com/recipes/users/4065697/http://code.activestate.com/recipes/523018-sin-cos-tan-for-decimal/
<p style="color: grey">
Python
recipe 523018
by <a href="/recipes/users/4065697/">Alain Mellan</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/generator/">generator</a>).
Revision 2.
</p>
<p>Implementation of sine, cosine, tangent functions for Decimal arithmetic, using Taylor series expansion. It uses simple numerator and denominator generators.</p>
<p>The nice part is, the code is independent from the Decimal library. Feed it a float, it works just the same as if you feed it a Decimal (apart from the precision :-)</p>
typeparser (Python)
2007-04-15T04:24:53-07:00Florian Leitnerhttp://code.activestate.com/recipes/users/4049249/http://code.activestate.com/recipes/511473-typeparser/
<p style="color: grey">
Python
recipe 511473
by <a href="/recipes/users/4049249/">Florian Leitner</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Python type-string parser. The code evolved from a post in python-list on 11/22/05 by Fredrik Lundh on a dictionary parser. It parses a type-string to their type objects for all basic types. Raises SyntaxError and SemanticError on failures.</p>
<p>Supported types:
* containers: defaultdict, deque, dict, list, tuple, set
* basic types: Decimal, bool, float, int, long, str
* None type</p>
<p>REQUIRES PYTHON >= 2.5</p>
DBF reader and writer (Python)
2009-04-29T07:28:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/362715-dbf-reader-and-writer/
<p style="color: grey">
Python
recipe 362715
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 7.
</p>
<p>Reader iterates over records in Dbase or Xbase files.
Writer creates dbf files from Python sequences.</p>
*Very* simple accountant's calculator (Python)
2004-09-02T10:54:01-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/303176-very-simple-accountants-calculator/
<p style="color: grey">
Python
recipe 303176
by <a href="/recipes/users/98030/">Brett Cannon</a>
.
</p>
<p>One of the signs that you love Python is when you start to use it as a simple calculator. The problem with that is beyond the usefulness of 'sum' the interactive interpreter is not optimal for any calculations beyond a few numbers. This mostly seems to stem from the numbers not being formatted in a nice fashion; <code>2345634+2894756-2345823</code> is not the easiest thing to read. That's where an accountant's calculator comes in handy; the tape presents numbers in a column view that is very uncluttered. And thanks to the decimal package a <em>very</em> simple one can be implemented quickly.</p>
<p>To use this recipe you input the number, an optional space, and then the operator (/, *, -, or +; everything you would find on the numeric keypad on your keyboard) and then press return. This will apply the number to the running total using the operator. To output the total just enter a blank line. To quit enter the letter 'q' and press return. This simple interface matches the output of a typical accountant's calculator, removing the need to have some other form of output.</p>