Neat Tricks from other Libs

Small tricks and small usage libs I had no other place to put on

IPython

Displaying audio

t = np.linspace(0,5,44100*5)
sine = np.sin(2*np.pi*520*t)
audio = Audio(sine, rate=44100)

display(audio)

Displaying Progress Bar

HTML style

pb = ProgressBar(12)
for _ in pb: time.sleep(0.5)

Old school

for i in range(11):
    pb.progress = i
    print('\r', pb, sep='', end='')
    time.sleep(0.5)
[==================================================          ] 10/12

Changing ProgressBar properties

pb2 = ProgressBar(35)
pb2.html_width = '70ex'
pb2.text_width = 50
pb2.display()
for i in range(11):
    pb2.progress = i
    print('\r', pb2, sep='', end='')
    time.sleep(0.5)
[==============                                    ] 10/35

Displaying HTML

html = '''<table>
  <tr>
    <th>Method</th>
    <th>Pros</th>
    <th>Cons</th>
  </tr>
  <tr>
    <td>KS-test</td>
    <td>Easy to implement, widely used, suitable for continuous data</td>
    <td>Assumes normal distribution, may not work well for small sample sizes</td>
  </tr>
  <tr>
    <td>t-test</td>
    <td>Easy to implement, widely used, suitable for continuous data</td>
    <td>Assumes normal distribution, may not work well for small sample sizes, requires equal variances</td>
  </tr>
  <tr>
    <td>Jensen-Shannon</td>
    <td>Works well for both continuous and discrete data, does not assume normal distribution</td>
    <td>May be computationally expensive for large datasets, may not work well for high-dimensional data</td>
  </tr>
  <tr>
    <td>Wasserstein distance</td>
    <td>Works well for both continuous and discrete data, does not assume normal distribution</td>
    <td>May be computationally expensive for large datasets, may not work well for high-dimensional data</td>
  </tr>
  <tr>
    <td>PSI</td>
    <td>Can detect both magnitude and direction of drift, suitable for both continuous and categorical data</td>
    <td>May be sensitive to small changes, may require a large reference dataset</td>
  </tr>
</table>'''
display(HTML(html))
Method Pros Cons
KS-test Easy to implement, widely used, suitable for continuous data Assumes normal distribution, may not work well for small sample sizes
t-test Easy to implement, widely used, suitable for continuous data Assumes normal distribution, may not work well for small sample sizes, requires equal variances
Jensen-Shannon Works well for both continuous and discrete data, does not assume normal distribution May be computationally expensive for large datasets, may not work well for high-dimensional data
Wasserstein distance Works well for both continuous and discrete data, does not assume normal distribution May be computationally expensive for large datasets, may not work well for high-dimensional data
PSI Can detect both magnitude and direction of drift, suitable for both continuous and categorical data May be sensitive to small changes, may require a large reference dataset

Display Math expressions

Math('$ {x_{a}}^2$')

\(\displaystyle {x_{a}}^2\)

back to top

PyAutoGUI

pa.FAILSAFE = True
w,h = pa.size()
random.seed(42)

def moveWithin():
    pa.moveTo(random.randint(0,w),random.randint(0,h), duration=2)

while True:
    try:
        moveWithin()
    except pa.FailSafeException:
        print(f"Stopping with the randomness 'cause: {pa.FailSafeException}")
        break
print(pa.position())
Stopping with the randomness 'cause: <class 'pyautogui.FailSafeException'>
Point(x=0, y=1079)

Redlines

Find differences between two texts

t1 = 'She sells seashells in Seychelles'
t2 = 'She shells seysells in Seachelles'
diff = Redlines(t2, t1)
display(Markdown(diff.output_markdown))

She shells seysells sells seashells in SeachellesSeychelles

back to top