pycse.utils#

These are some convenience utilities.

Float comparisons#

Floating point math is hard.

0.3 == 0.1 + 0.2
False

pycse.utils provides several floating point comparison operators:

  • feq a == b

  • flt a < b

  • fgt a > b

  • fle a <= b

  • fge a >= b

from pycse.utils import feq

feq(0.3, 0.1 + 0.2)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 1
----> 1 from pycse.utils import feq
      3 feq(0.3, 0.1 + 0.2)

File /opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/pycse/__init__.py:24
     12 from .PYCSE import (
     13     polyfit,
     14     regress,
   (...)
     20     lbic,
     21 )
     22 from .utils import feq, flt, fgt, fle, fge, read_gsheet
---> 24 from .hashcache import hashcache
     26 # from .beginner import *
     29 from IPython import get_ipython

File /opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/pycse/hashcache.py:84
     82 import inspect
     83 import joblib
---> 84 import orjson
     85 import os
     86 from pathlib import Path

ModuleNotFoundError: No module named 'orjson'

Temporarily ignore errors#

This context manager wraps code in a try/except/finally clause so you can ignore some errors.

1 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[3], line 1
----> 1 1 / 0

ZeroDivisionError: division by zero
from pycse.utils import ignore_exception
with ignore_exception(ZeroDivisionError):
    1 / 0
caught division by zero
done

Read a Google Sheet into a pandas Dataframe#

You can use a url to a Google Sheet that is publicly visible to read it into a pandas Dataframe.

from pycse.utils import read_gsheet
read_gsheet('https://docs.google.com/spreadsheets/d/1Qh4H5lHw_HOScAZqvII1VRPwCpPEJTHwkbo3a323azg/')
x y
0 1 2
1 2 4
2 3 6