Loading ...
Sorry, an error occurred while loading the content.

[Cheetahtemplate-discuss] unicode and auto calling str

Expand Messages
  • William Dode
    Hi, If the variable is not unicode, cheetah call str on the variable, not unicode. Then it doesn t work or raise an exception : # -*- encoding: utf8 -*- class
    Message 1 of 3 , Aug 16, 2008
    • 0 Attachment
      Hi,

      If the variable is not unicode, cheetah call str on the variable, not
      unicode. Then it doesn't work or raise an exception :




      # -*- encoding: utf8 -*-
      class Z:
      def __unicode__(self):
      return u'à'

      from Cheetah.Template import Template
      z=Z()
      print Template('#unicode utf8\n z=$z', [{'z':z}])

      z=<__main__.Z instance at 0xa0779cc>





      # -*- encoding: utf8 -*-
      class Z:
      def __str__(self):
      return u'à'

      from Cheetah.Template import Template
      z=Z()
      print Template('#unicode utf8\n z=$z', [{'z':z}])

      File "/var/lib/python-support/python2.5/Cheetah/Filters.py", line 51,
      in filter
      filtered = str(val)
      UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 0: ordinal not in range(128)




      any idea ?

      --
      William Dodé - http://flibuste.net
      Informaticien Indépendant


      -------------------------------------------------------------------------
      This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
      Build the coolest Linux based applications with Moblin SDK & win great prizes
      Grand prize is a trip for two to an Open Source event anywhere in the world
      http://moblin-contest.org/redirect.php?banner_id=100&url=/
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • William Dode
      ... I could find a solution looking at the source code of Cheetah.Filters : import types class Z: def __str__(self): return u à from Cheetah.Template import
      Message 2 of 3 , Aug 16, 2008
      • 0 Attachment
        On 16-08-2008, William Dode wrote:
        > Hi,
        >
        > If the variable is not unicode, cheetah call str on the variable, not
        > unicode. Then it doesn't work or raise an exception :

        I could find a solution looking at the source code of Cheetah.Filters :

        import types
        class Z:
        def __str__(self):
        return u'à'

        from Cheetah.Template import Template
        from Cheetah.Filters import Filter

        class MyFilter(Filter):
        def filter(self, s, **kargs):
        return Filter.filter(self, s, str=unicode, **kargs)

        z=Z()

        r =Template('#encoding utf8\nz=$z', [{'z':z}], filter=MyFilter)
        print unicode(r)

        Is there an other way ?

        --
        William Dodé - http://flibuste.net
        Informaticien Indépendant


        -------------------------------------------------------------------------
        This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
        Build the coolest Linux based applications with Moblin SDK & win great prizes
        Grand prize is a trip for two to an Open Source event anywhere in the world
        http://moblin-contest.org/redirect.php?banner_id=100&url=/
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      • Tavis Rudd
        William, One solution to this would be to use hasattr(x, __unicode__ ) in the filter. I d rather not change the default filter to do this as it would lead to
        Message 3 of 3 , Nov 10, 2008
        • 0 Attachment
          William,
          One solution to this would be to use hasattr(x, '__unicode__') in
          the filter. I'd rather not change the default filter to do this as it
          would lead to a performance hit and possibly errors for
          most users of Cheetah. I think it's best for you to create your own
          filter subclass for this situation.
          Tavis

          On Sat, 16 Aug 2008, William Dode wrote:

          > Hi,
          >
          > If the variable is not unicode, cheetah call str on the variable, not
          > unicode. Then it doesn't work or raise an exception :
          >
          >
          >
          >
          > # -*- encoding: utf8 -*-
          > class Z:
          > def __unicode__(self):
          > return u'à'
          >
          > from Cheetah.Template import Template
          > z=Z()
          > print Template('#unicode utf8\n z=$z', [{'z':z}])
          >
          > z=<__main__.Z instance at 0xa0779cc>
          >
          >
          >
          >
          >
          > # -*- encoding: utf8 -*-
          > class Z:
          > def __str__(self):
          > return u'à'
          >
          > from Cheetah.Template import Template
          > z=Z()
          > print Template('#unicode utf8\n z=$z', [{'z':z}])
          >
          > File "/var/lib/python-support/python2.5/Cheetah/Filters.py", line 51,
          > in filter
          > filtered = str(val)
          > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 0: ordinal not in range(128)
          >
          >
          >
          >
          > any idea ?
          >
          > --
          > William Dodé - http://flibuste.net
          > Informaticien Indépendant
          >
          >
          > -------------------------------------------------------------------------
          > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
          > Build the coolest Linux based applications with Moblin SDK & win great prizes
          > Grand prize is a trip for two to an Open Source event anywhere in the world
          > http://moblin-contest.org/redirect.php?banner_id=100&url=/
          > _______________________________________________
          > Cheetahtemplate-discuss mailing list
          > Cheetahtemplate-discuss@...
          > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.