Skip to content

Commit

Permalink
unit prefixes - set default to not active
Browse files Browse the repository at this point in the history
so stuff like `Sum(log(x)**n,(n,1,12))` would work.
  • Loading branch information
idanpa committed Feb 16, 2024
1 parent 39d6675 commit 6385356
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ pip install git+https://github.com/idanpa/calcpy
* Tuples are matrices `((1,2),(3,4))**2`
* All variables and functions are restored between sessions (delete using `del`)
* Datetime calculations `d"yesterday at 9 am" - d"1990-1-30 9:20"` (using [dateparser](https://github.com/scrapinghub/dateparser))
* Unit prefixes `G`, `M`, `k`, `m`, `u`, `n`, `p`, `KB`, `MB`, `GB`, `TB` (so `4MB-32KB` or `4G/3.2n` are valid)
* Sizes `KB`, `MB`, `GB`, `TB` (e.g. `4MB-32KB`)
* Unit prefixes `G`, `M`, `k`, `m`, `u`, `n`, `p` (`4G/3.2n`, enable by `calcpy.units_prefixes=True`)
* Implicit lambda `f(a,b):=a**2+b**2`
* Latex input `plot($\frac{1,x}$)` (latex output with `latex(1/x)`)
* Copy to clipboard `copy(Out[12])`
Expand Down
16 changes: 8 additions & 8 deletions calcpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CalcPy(IPython.core.magic.Magics):
previewer = traitlets.Bool(True, config=True, help="enable previewer")
bitwidth = traitlets.Int(0, config=True, help="bitwidth of displayed binary integers, if 0 adjusted accordingly")
chop = traitlets.Bool(True, config=True, help="replace small numbers with zero")
eng_units_prefixes = traitlets.Bool(True, config=True, help="engineering units prefixes (e.g. 2k=2000)")
units_prefixes = traitlets.Bool(False, config=True, help="units prefixes (e.g. 2k=2000)")
precision = property(
lambda calcpy: calcpy.shell.run_line_magic('precision', ''),
lambda calcpy, p: calcpy.shell.run_line_magic('precision', p))
Expand All @@ -57,7 +57,7 @@ def __init__(self, shell=None, **kwargs):
''''''
super(CalcPy, self).__init__(shell, **kwargs)

self._eng_units_prefixes_dict = { 'G': transformers.IntegerUnitPrefix(1e9), 'M': transformers.IntegerUnitPrefix(1e6), 'k': transformers.IntegerUnitPrefix(1e3),
self._units_prefixes_dict = { 'G': transformers.IntegerUnitPrefix(1e9), 'M': transformers.IntegerUnitPrefix(1e6), 'k': transformers.IntegerUnitPrefix(1e3),
'm': transformers.PowUnitPrefix(10, -3), 'u': transformers.PowUnitPrefix(10, -6), 'n': transformers.PowUnitPrefix(10, -9), 'p': transformers.PowUnitPrefix(10, -12) }

self.user_startup_path = os.path.join(shell.profile_dir.location, 'user_startup.py')
Expand Down Expand Up @@ -92,14 +92,14 @@ def _previewer_changed(change):
self.unload_previewer()
self.observe(_previewer_changed, names='previewer')

def _eng_units_prefixes_changed(change):
def _units_prefixes_changed(change):
if change.new == True:
self.push(self._eng_units_prefixes_dict, interactive=False)
self.push(self._units_prefixes_dict, interactive=False)
if change.new == False:
for key in self._eng_units_prefixes_dict:
for key in self._units_prefixes_dict:
self.shell.user_ns.pop(key, None)
self.shell.user_ns_hidden.pop(key, None)
self.observe(_eng_units_prefixes_changed, names='eng_units_prefixes')
self.observe(_units_prefixes_changed, names='units_prefixes')

CalcPy.__doc__ = "CalcPy\n"
for trait_name, trait in sorted(self.traits(config=True).items()):
Expand Down Expand Up @@ -163,8 +163,8 @@ def show_usage():
ip.show_usage = show_usage

ip.run_cell('from calcpy.user import *', store_history=False)
if ip.calcpy.eng_units_prefixes:
ip.push(ip.calcpy._eng_units_prefixes_dict, interactive=False)
if ip.calcpy.units_prefixes:
ip.push(ip.calcpy._units_prefixes_dict, interactive=False)

formatters.init(ip)
transformers.init(ip)
Expand Down

0 comments on commit 6385356

Please sign in to comment.