Skip to main content

Powerful yet easy command line calculator.

Project description

Logo

cli-calc

test codecov Python Version

pypi github release pypi downloads

DEV

Powerful yet easy command line calculator.

Introduction

Using plain python at the command line already provides a pretty decent calculator:

$ python
Python 3.8.5 (default, Aug  2 2020, 16:00:15)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**10
1024
>>> _ / 10
102.4
>>> import math
>>> math.pi
3.141592653589793
>>> math.sin(math.pi / 2)
1.0
>>> exit()

[ Read more about Python as a Calculator. ]

But not all is straight forward. For example, if you want to calculate sin(pi/2):

  • First you need to import math,
  • and then use it with math.sin( math.pi / 2 ).

But there must be a better way! And there is...

cli-calc

$ cli-calc
hex, int, float,
0x0, 0, 0.0, : 2**10
2**10
0x400, 1024, 1024.0, : _ / 10
1024.0/10
0x66, 102, 102.4, : pi
pi
0x3, 3, 3.141592653589793, : sin(pi / 2)
sin(pi/2)
0x1, 1, 1.0, : q
$

Batteries included:

  • cli-calc adds the standard math library to the mix, as well as cmath and others.
  • Provides convenience functions for formatted output, like fractions and IEEE 754.
  • Supports line input from files and Unix pipes.
  • And some more... :-)

More examples

$ cli-calc
hex, int, float,
0x0, 0, 0.0, : cos(pi/2)
cos(pi/2)
0x0, 0, 6.123233995736766e-17, : 0xFF ^ 0b10
0xFF^0b10
0xfd, 253, 253.0, : 2**8-1
2**8-1
0xff, 255, 255.0, : log(e)
log(e)
0x1, 1, 1.0, : comb(49,6)
comb(49,6)
0xd56048, 13983816, 13983816.0, : factorial(42)
factorial(42)
0x3c1581d491b28f523c23abdf35b689c908000000000, 1405006117752879898543142606244511569936384000000000, 1.40500611775288e+51, : random()
random()
0x0, 0, 0.24958817003921918, : cmath.phase(complex(-1.0, 0.0))
cmath.phase(complex(-1.0,0.0))
0x3, 3, 3.141592653589793, : q
$

Included Math Libraries

Ready to use out of the box:

Installation

pip install cli-calc

Configuration

For convenience, add a shell script in ~/bin:

cd ~/bin
vi cli-calc

with the following lines:

path=~/path/to/your/cli-calc/cli_calc
cd $path
./main.py

Make it executable:

chmod a+x cli-calc

then use it anywhere. :-)

echo "7+8" | cli-calc
cat foo.bar | cli-calc
cli-calc

Help

cli-calc
h
Input:
    "q" for quit, "h" for help

    "_" for last float value
    "_int_" for last int value

    "sin(pi/2)" for sinus, ...

    "pi", "tau" and "e" for pi, tau and Euler

    "+f" to add display for fraction, "-f" to suppress display for fraction
        Other letters are:
        he(x), (o)ctal, (b)inary, (i)nteger,
        (f)raction, (t)ruth, i(e)ee, ieee_bi(n), f(r)om_ieee
        "float" is always visible

    See https://docs.python.org/3/library/math.html, use without "math."
        https://www.w3schools.com/python/python_operators.asp

    Try "cos(pi/2)", XOR: "0xFF ^ 0b10", "2**8-1", "factorial(42)",
        "help(math)"

Warning

  • Use of eval is evil. However precautions are taken.

Pull Request

PR's are Welcome!

License

MIT

Credits

This project was generated with wemake-python-package. Current template version is: d06993f12e3ffad79652a2aec86189dee92d94dd. See what is updated since then.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cli-calc-0.2.25.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

cli_calc-0.2.25-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file cli-calc-0.2.25.tar.gz.

File metadata

  • Download URL: cli-calc-0.2.25.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.5 Darwin/18.6.0

File hashes

Hashes for cli-calc-0.2.25.tar.gz
Algorithm Hash digest
SHA256 442eb025107f68edd3b05a41296f64fed37050dd3afcadf1aa941d3b7328a229
MD5 a9d7aac5535c2ae16165298ec40e8666
BLAKE2b-256 c3eec50ef7339758d6a38c44ef45208040ab3c512ec3b50acd05d46e235bc9dd

See more details on using hashes here.

File details

Details for the file cli_calc-0.2.25-py3-none-any.whl.

File metadata

  • Download URL: cli_calc-0.2.25-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.8.5 Darwin/18.6.0

File hashes

Hashes for cli_calc-0.2.25-py3-none-any.whl
Algorithm Hash digest
SHA256 e10f03f1556ffa953702544aa1a4b4c2870244d12aacb1686d328bf6fc51e5ce
MD5 60f45f5bff1c860105f6d7ddf72e7979
BLAKE2b-256 db48626202be8e7cf6b6140c750e5842df9bb87011362efe2394e1930ea35ce2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page