add python snippet to find srt timestamp difference

This commit is contained in:
Julin S 2023-05-18 22:01:27 +05:30
parent 439ef691d3
commit db2d6701c2
3 changed files with 103 additions and 1 deletions

View File

@ -2,4 +2,9 @@
Some scripts that I made for myself..
- pdfscript.py: process pdf files
- submixer.sh: merge ~.srt~ files into ~.ass~ files
- submixer.sh: merge two ~.srt~ files into a ~.ass~ file
- interests.py: Find simple interest and compound interest
- calsh.sh: An interactive interface to the ~cal~ command
- z-eyes.sh: take breaks to rest eys while on computer (20-20-20 rule)
- subs-helper.py: Find difference between two srt timestamps

45
interests.py Normal file
View File

@ -0,0 +1,45 @@
"""
Functions to find simple and compound interests.
"""
def comp_interest(principal: float,
rate: float,
comp_freq: int,
duration: float) -> float:
"""
principal: principal amount
rate: Rate interest as float (100% is 100)
duration: Duration in years
comp_freq: Number of compounding periods in a year
interest = principal*((1 + rate/100)**n) - principal
"""
rate /= 100 * comp_freq
rv = (1 + rate) ** (comp_freq * duration)
rv = principal * rv
rv -= principal
return rv
def simp_interest(principal: float,
rate: float,
duration: float) -> float:
"""
principal: principal amount
rate: Rate interest as float (100% is 100)
duration: Duration in years
comp_freq: Number of compounding periods in a year
interest = principal * duration * rate
"""
return principal * duration * rate/100
#import pytest
#
#@pytest.mark.parametrize("principal,rate,comp_freq,duration,expected", [
# (12000, 10, 2, 1.5, 1891.5),
# (1000, 20, 1, 2, 440.0),
#])
#def test_comp_interest(principal, rate, comp_freq, duration, expected):
# rv = comp_interest(principal, rate, comp_freq, duration)
# assert round(rv, 5) == expected

52
subs-helper.py Normal file
View File

@ -0,0 +1,52 @@
# A snippet to calculate difference between two srt timestamps
def str2msecs(timestr: str) -> int:
"""
Convert srt timestamp to a millisecond int value
>>> str2msecs("00:00:00,052")
52
"""
hrs_str, mins_str, rem = timestr.split(":")
secs_str, msecs_str = rem.split(",")
hrs, mins = int(hrs_str), int(mins_str)
secs, msecs = int(secs_str), int(msecs_str)
rv = hrs*3600 + mins*60 + secs
return rv*1000 + msecs
def msecs2str(msecs: float) -> str:
"""
Convert a millisecond int value to srt timestamp
>>> msecs2str(52)
'00:00:00,052'
>>> msecs2str(52.488)
'00:00:00,052'
"""
hrs, rem = divmod(msecs, 3600000)
mins, rem = divmod(rem, 60000)
secs, msecs = divmod(rem, 1000)
hrs, mins, secs, msecs = int(hrs), int(mins), int(secs), int(msecs)
# https://docs.python.org/3/library/string.html#format-specification-mini-language
return f"{hrs:02d}:{mins:02d}:{secs:02d},{msecs:03d}"
def foo(s1: str, s2: str) -> float:
"""
Take 2 timestamps and find difference between them
ie,
foo(s1, s2) = s1 - s2
>>> foo("00:00:59,128", "00:00:06,640")
52.488
"""
a = str2msecs(s1)
b = str2msecs(s2)
#return msecs2str(a-b)
return (a-b) / 1000
a = foo("00:00:59,128", "00:00:06,640")
# 52.488
print(a)
print(msecs2str(a))