Weird encoding error, different behavior when called as executable vs shell script

I have a script that tells how much battery my laptop has left. I would like a universal solution for python 2 & python 3, if possible. With my own tinkering I seem to only be cable to get python 2 OR python 3 to work. Here is the code:

#battery.py
#!/usr/bin/env python
# coding=UTF-8

import math
import subprocess

p = subprocess.Popen(["ioreg", "-rc", "AppleSmartBattery"], stdout=subprocess.PIPE)
output = p.communicate()[0]

o_max = [l for l in output.splitlines() if b'MaxCapacity' in l][0]
o_cur = [l for l in output.splitlines() if b'CurrentCapacity' in l][0]

b_max = float(o_max.rpartition(b'=')[-1].strip())
b_cur = float(o_cur.rpartition(b'=')[-1].strip())

charge = b_cur / b_max
charge_threshold = int(math.ceil(10 * charge))

# Output

total_slots, slots = 10, []
filled = int(math.ceil(charge_threshold * (total_slots / 10.0))) * u"u25CF"
empty = (total_slots - len(filled)) * u"u25CB"

#out = (filled + empty).decode('utf-8')
out = u' '.join((filled, empty)).encode('utf8').strip()
import sys

color_green = '%{[32m%}'
color_yellow = '%{[1;33m%}'
color_red = '%{[31m%}'
color_reset = '%{[00m%}'
color_out = (
    color_green if len(filled) > 6
    else color_yellow if len(filled) > 3
    else color_red
)

# out = color_out + out + color_reset #works with py2
out = color_out + out.decode('utf-8','ignore') + color_reset #works with py3
sys.stdout.write(out)

When run through the python exectubales python and python3 the script gives output as expected:

$ python batcharge.py
%{%}●●●●●●●●●●%{%}%

$ python3 batcharge.py
%{%}●●●●●●●●●●%{%}%

However when the shell calls the file to display its output in my prompt, I get a traceback error from python2:

Traceback (most recent call last):
  File "/usr/local/bin/batcharge.py", line 40, in <module>
    sys.stdout.write(out)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-18: ordinal not in range(128)

It works fine when I am using a virtualenv with python3 as the default python. Please help.


Source: shell

Leave a Reply