Giter VIP home page Giter VIP logo

Comments (4)

felagund avatar felagund commented on July 25, 2024

I read on the internet that str() should not be used when dealing with unicode and .encode() and .decode() should be used. This poses two problems:

  1. Python 3 compatibility
  2. SrtItem does not know about encoding of its .text attribute as far as I can see

from pysrt.

byroot avatar byroot commented on July 25, 2024

SrtItem does not know about encoding of its .text attribute as far as I can see

Yes it's by design. SubRipFile deal with all the encoding mess, but SubRipItem is fully unicode and abstracted from encoding stuffs. So SubRipItem.str() in Python 2 do not make sense. Actually before the merge of py2 and 3 code bases that method did not exist.

I guess we have no other choices than to have two different implementations, and to conditionally define them based on python version, something like:

class SubRipItem:
    if is_py3:
        def __str__(self):
            #actual implementation
    else:
        def __unicode__(self):
            # same implementation

        def __str__(self):
            raise NotImplementedError

from pysrt.

felagund avatar felagund commented on July 25, 2024

Would not it be better if __str__(self) pointed to unicode(self)? Only we would need to know which encoding to use, right, which we don't by design, if I understand it correctly.

Then, should not

self.position = str(position)
self.text = str(text)

in __init__ method of SubRipItem be

self.position = unicode(position)
self.text = unicode(text)

instead?

And in write_into method of SubRipFile, we will need to call either unicode(item) or str(item) based on whether we are using Python 2 or 3, right?

from pysrt.

byroot avatar byroot commented on July 25, 2024

we will need to call either unicode(item) or str(item) based on whether we are using Python 2 or 3, right?

It's already the case: https://github.com/byroot/pysrt/blob/master/pysrt/compat.py#L18

from pysrt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.