Git Product home page Git Product logo

printi's Introduction

๐Ÿšง This Python package is a WIP.

Still TODO:

โ˜ Publish to PyPi/conda-forge

โ˜ Make it work for older versions than 3.10

โ˜ Test with macOS/Linux

โ˜ Test with Jupyter

Printi

Printi watches what you print, and if it sees a number that can be displayed in a more insightful way, it will tell you.

For example, the last line in the code snippet below is printi telling you that -2.4674011002723395 is very very close to -ฯ€ยฒ/4:

>>> import cmath
>>> import printi
>>> printi.watch()
>>> cmath.log(1j) ** 2
(-2.4674011002723395+0j)
๐Ÿ’ก -2.4674011002723395 โ‰ˆ -ฯ€ยฒ/4

API

printi()

The printi function is a drop-in replacement for print. It has the same API as the builtin print function.

To use printi as a function, you'll need to use from printi import printi (for all other methods it is enough to use import printi).

printi.watch()

Rather than use printi() directly, you can tell it to watch stdout. You can add this to your startup script referenced by PYTHONSTARTUP.

Example Python startup file using Printi:

# This file is referenced in PYTHONSTARTUP
import sys
from pathlib import Path

sys.path.append(str(Path.home() / 'path/to/printi/src'))
from printi import printi

printi.watch()

Now when you start any interactive session, Printi will be watching.

printi.unwatch()

Printi will stop watching.

printi.update_config()

Updates the configuration options for Printi. All possible keys, and their defaults are:

printi.update_config(
  min_denominator=3,
  max_denominator=100,
  tol=1e-9,
  symbol='๐Ÿ’ก',
  specials={
      math.pi: 'ฯ€',
      math.tau: 'ฯ„',
      math.e: 'e',
  },
)
  • min_denominator is the minimum denominator.

  • max_denominator is the max. If this is set too high, you'll get false positives

  • tol is the tolerance. This is passed to math.isclose as rel_tol

  • symbol. Change this if you don't like the light bulb. Why don't you like the light bulb?

  • specials is a dict of float/string pairs that will be added to the existing dict. To remove a key, pass None as the value. Example below:

    >>> printi.update_config(specials={1.2345678: 'ฮป'})
    >>> 'How do you like 1.2345678?'
    'How do you like 1.2345678?'
    ๐Ÿ’ก 1.2345678 โ‰ˆ ฮป

Limitations

Printi will only find 'representations' in the form a ยฑ b * c ** d where

  • a is an integer
  • b is a fraction (respecting min/max denominator)
  • c is either one of the 'special' values, or a digit
  • d is a selection of positive and negative integers and fractions

Check out the tests in /tests/test_printi.py for lots of examples.

Development

  • Run pipenv run test to run the tests
  • Run py -m build to build

printi's People

Contributors

davidgilbertson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.