Python
Hello Summary
CONSTANT=10
# recursive, exception, conditions, lambda
def fib(n):
if n < 0:
raise Exception('n={}, should be positive'.format(n))
if n == 0:
return 0
check_n = lambda n: n == 1 or n == 2
if check_n(n):
return 1
return fib(n-1) + fib(n-2)
# class, attribute, method
class math:
def __init__(self, c=CONSTANT):
self.c = c
@staticmethod
def addition(a, b=2):
return a+b
def times_ten_and_hundred(n):
return n*self.c, n*self.c**c
if __name__ == '__main__':
# variables
a, b = 1, 3
# list/array(l), tuple(t), dict(d)
l, t, d= [a, b], (a, b), {'a': a, 'b': b}
try:
fib(-1)
except Exception as e:
print("error: {}".format(e)) # error: n=-1, should be positive
print(math.addition(a, b=b)) # 4
print(math.addition(d['a'])) # 3
# instance
r1, r2 = math().times_ten_and_hundred(5)
assert r1 == 50 # ok
assert r2 == 500 # ok
Data Structure
List
>>> a = []
>>> a.append(1)
>>> a.append(2)
>>> a.append(3)
>>> a.pop() # stack <=> lifo
3
>>> a.pop(0) # queue <=> fifo (if implemented on linkedlist, complexity O(1) to add/del element at beginning)
1
>>> a
[2]
Note: a list might have different type of elements (int, dict, ...), while import array
cannot.
Linked List
See linked list
Sorting algorithms
See sorting
Concurrency
Futures (old manner)
from concurrent import futures
import time
def task(seq):
print('Start:', time.strftime('%X'))
time.sleep(max(seq))
print('End:', time.strftime('%X'))
return list(reversed(seq))
def main():
max_workers = 2
timeout = 300
jobs = []
not_done = set()
pendings = 0
with futures.ProcessPoolExecutor(max_workers=max_workers) as pool:
for seq in [[3,2,1], [10,5,0]]:
if pendings > max_workers:
_u, not_done = futures.wait(not_done, timeout, return_when=futures.FIRST_COMPLETED)
pendings = len(not_done)
print(f"{pendings} jobs pending")
job = pool.submit(task, seq)
jobs.append(job)
not_done.add(job)
pendings += 1
done, not_done = futures.wait(jobs, timeout, return_when=futures.ALL_COMPLETED)
results = [d.result() for d in done]
return results
>>> r = main()
Start: 15:08:39
End: 15:08:49
Start: 15:08:49
End: 15:08:52
>>> r
[[1, 2, 3], [0, 5, 10]]
Async (recommended)
import asyncio
import time
async def coroutine(seq):
await asyncio.sleep(max(seq))
return list(reversed(seq))
async def main():
t = asyncio.create_task(coroutine([3, 2, 1]))
t2 = asyncio.create_task(coroutine([10, 5, 0])) # Python 3.7+
print('Start:', time.strftime('%X'))
a1 = await asyncio.gather(t, t2)
print('End:', time.strftime('%X')) # Should be 10 seconds
print(f'Both tasks done: {all((t.done(), t2.done()))}')
print('Start:', time.strftime('%X'))
a2 = await asyncio.gather(coroutine([1,2,3]), coroutine([1,2,2]))
print('End:', time.strftime('%X')) # Should be 3 seconds
return a1, a2
Note: don't use time.sleep()
>>> asyncio.run(main())
Start: 15:08:39
End: 15:08:49
Both tasks done: True
Start: 15:08:49
End: 15:08:52
>>> a1
[[1, 2, 3], [0, 5, 10]]
Libraries
Machine Learning
- tslearn: time series analysis
- scikit-learn: classical machine learning
- sktime: time series model (classification, clustering) compatible with scikit-learn interfaces
- tsfresh: time series feature extraction
- DESlib: easy-to-use ensemble learning library focused on the implementation of the state-of-the-art techniques for dynamic classifier and ensemble selection
- PyOD: outlier detection
- river: online machine learning with streaming data
- unionml: deploy machine learning microservices
- Flyte: workflow automation platform for complex, mission-critical data, and ML processes at scale
- ALEPython: Accumulated Local Effects (or ALE) interepretability
Statistics
- Pingouin: ANOVA, post-hocs (parametric and non-parametric), multivariate
- statsmodels (see also scikit posthocs)
Misc
- modin: scale pandas (using dask or ray)
- streamlit: create web apps for machine learning projects
- luma.lcd: display drivers for HD44780, PCD8544, ST7735, ST7789, ST7567, HT1621, UC1701X, ILI9341
- ruptures: time series change point detection in Python
- SymPy: algebra system
- Adafruit CircuitPython RFM69: CircuitPython RFM69 packet radio module. This supports basic RadioHead-compatible sending and receiving of packets with RFM69 series radios (433/915Mhz).
Numpy
>>> a
array([0.34399327, 0.51971385, 0.42075315, 0.65919112])
>>> b
array([0.42685801, 0.52210862, 0.52210862, 0.52210862])
>>> a*b
array([0.14683628, 0.27134708, 0.21967885, 0.34416937])
>>> np.sum(a*b)
0.9820315790772367
>>> a.dot(np.transpose(b))
0.9820315790772366
>>> a.dot(b.T)
0.9820315790772366
>>> a.dot(b)
0.9820315790772366
Pandas
Combine multiple rows of lists into one big list
lst = df['col_of_lists'].explode()