Git Product home page Git Product logo

Comments (9)

luckydonald avatar luckydonald commented on June 19, 2024

There was one python 3 version which dislike u""strings.
Can you check if

foo = u"bar"

works for you?
(Just try that quickly, in the interactive shell or something.)

If not, you can either remove all us before strings, or upgrade python 3 to some newer version
(which sadly means compiling by yourself... It will take some time, but is worth it in the end - I did that on my Pi too.)

from pytg.

luckydonald avatar luckydonald commented on June 19, 2024

Edit: Just remove the u in front of the strings. (Python 3+)

"text"
#instead of:
u"text"

Another option, compatible with all Python versions is to import the function pytg use to convert all strings to the right format:

from pytg.encoding import to_unicode as u

This will get any kind of string (byte-encoded or already unicode) correctly to utf-8 unicode (of the according native type str in python 3, or unicode in python 2).

Then do

foo = u("bar")

(wrap the string with u(), which is th imported to_unicode())

from pytg.

securedirective avatar securedirective commented on June 19, 2024

You are correct. In my Python 3.2.3 interactive terminal, foo = u"bar" causes a syntax error. According to https://docs.python.org/3/whatsnew/3.3.html?highlight=unicode, that syntax was added back in Python 3.3.

For the benefit of others, may I suggest that your Github README.md be updated to read "Works with Python 2.7 and 3.3" instead of "Works with Python 2.7 and 3"?

I tried your second suggestion in an interactive terminal. But even the line from pytg.encoding import to_unicode as u causes the following syntax error immediately:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pytg/__init__.py", line 9, in <module>
    from .encoding import to_unicode as u
  File "pytg/encoding.py", line 36
    return u"\\"
               ^
SyntaxError: invalid syntax

I'm not an avid Python programmer, so it sounds like I'm better off sticking with Python 2.7 for now, on the Raspberry Pi.

from pytg.

luckydonald avatar luckydonald commented on June 19, 2024
# now just change the  
u"stuff"  #python 2 (and python 3.3+)
# to
"stuff"  # python 3.0+
# or, after importing
from pytg.encoding import to_unicode as u
# to
u("stuff")  # all pythons

But I'll add that. Actually only the examples are not python 3.0-3.3 compatible, pytg itself is.

from pytg.

securedirective avatar securedirective commented on June 19, 2024

I don't understand. The syntax error is not happening in my script or even the setup.py script. It is happening in encoding.py itself, in the very to_unicode function that you suggest I import as 'u'. It won't even import, because right there Python 3.2 shows the syntax error.

Now, assuming you meant modifying lines 33 and 34 in your encoding.py (replacing b'' with '' and replacing u"" with ""), this at least allows sudo python3 setup.py install to succeed. But ultimately, when I run my own script to use pytg, I just get more of the same syntax errors in the DictObject/encoding.py instead (which looks almost identical to yours).

from pytg.

luckydonald avatar luckydonald commented on June 19, 2024

Ohh.
I thought that were the example files.
Can you check if it works, if you remove line 33 to 34 in encoding.py?
(The if x == b'\\': part)

from pytg.

securedirective avatar securedirective commented on June 19, 2024

Yes, that seems to do the trick. It's a workaround, at least.

I removed lines 33-34 in pytg/encoding.py:

        if isinstance(x, binary_type):
                #if x == b'\\':
                #       return u"\\"
                return x.decode("utf-8")

That solves the original error. But I just get the same error for DictObject instead. So I also removed lines 30-31 in /usr/local/lib/python3.2/dist-packages/DictObject-0.1.1-py3.2.egg/DictObject/encoding.py:

        if isinstance(x, binary_type):
                #if x == b'\\':
                #       return u"\\"
                return x.decode("utf-8")

Now, everything works in the script that I built to use pytg. For your ping example to work, though, I must edit examples/ping.py and replace all uses of u"........" with "........" (lines 57,58,59,61,64).

Thanks for your help! This is the first time I've ever worked with Unicode in Python, so I wasn't sure at first what I was even looking at or why the syntax errors were occurring. Now, to continue my Raspberry Pi project... :)

from pytg.

luckydonald avatar luckydonald commented on June 19, 2024

Your welcome. I will update pytg as well as DictObject (and several other projects where I use that file) to reflect that.

from pytg.

luckydonald avatar luckydonald commented on June 19, 2024

It should now be fixed.
If importing luckydonaldUtils fails, install it with pip install luckydonald-utils manually.

from pytg.

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.