Skip to content

Fixed rate command #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pysrt/srttime.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ def __imul__(self, ratio):
self.ordinal = int(round(self.ordinal * ratio))
return self

def __div__(self, ratio):
return self.from_ordinal(int(round(self.ordinal / ratio)))

def __idiv__(self, ratio):
self.ordinal = int(round(self.ordinal / ratio))
return self

def __truediv__(self, ratio):
return self.__div__(ratio)

def __itruediv__(self, ratio):
return self.__idiv__(ratio)

@classmethod
def coerce(cls, other):
"""
Expand Down Expand Up @@ -130,7 +143,7 @@ def shift(self, *args, **kwargs):
All arguments are optional and have a default value of 0.
"""
if 'ratio' in kwargs:
self *= kwargs.pop('ratio')
self /= kwargs.pop('ratio')
self += self.__class__(*args, **kwargs)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/test_srtfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_shift(self):
srt_file.shift(1, 1, 1, 1)
self.assertEqual(srt_file[0].end, (1, 1, 1, 1))
srt_file.shift(ratio=2)
self.assertEqual(srt_file[0].end, (2, 2, 2, 2))
self.assertEqual(srt_file[0].end, (0, 30, 30, 500))


class TestText(unittest.TestCase):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_srtitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def setUp(self):
def test_italics_tag(self):
self.item.text = "<i>Hello world !</i>"
self.assertEqual(self.item.text_without_tags,'Hello world !')

def test_bold_tag(self):
self.item.text = "<b>Hello world !</b>"
self.assertEqual(self.item.text_without_tags,'Hello world !')
Expand Down Expand Up @@ -126,10 +126,10 @@ def test_shift_down(self):

def test_shift_by_ratio(self):
self.item.shift(ratio=2)
self.assertEqual(self.item.start, {'minutes': 2})
self.assertEqual(self.item.end, {'minutes': 2, 'seconds': 40})
self.assertEqual(self.item.duration, (0, 0, 40, 0))
self.assertEqual(self.item.characters_per_second, 0.325)
self.assertEqual(self.item.start, {'seconds': 30})
self.assertEqual(self.item.end, {'seconds': 40})
self.assertEqual(self.item.duration, (0, 0, 10, 0))
self.assertEqual(self.item.characters_per_second, 1.3)


class TestOperators(unittest.TestCase):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_srttime.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,15 @@ def test_imul(self):
self.time *= 0.5
self.assertEqual(self.time, (1, 2, 3, 4))

def test_div(self):
self.assertEqual(self.time / 0.5, SubRipTime(2, 4, 6, 8))
self.assertEqual(self.time / 2, (0, 31, 1, 502))

def test_idiv(self):
self.time /= 0.5
self.assertEqual(self.time, (2, 4, 6, 8))
self.time /= 2
self.assertEqual(self.time, (1, 2, 3, 4))

if __name__ == '__main__':
unittest.main()