string - TypeError: Type str doesn't support the buffer API -


there's python script (found here) i'm using show laptop's battery percentage part of $prompt. works charm python (aka version 2). doesn't work python3. problem because using pyenv , pyenv-virtualenv work on projects , python command points 3+ version. produces these errors in terminal when battery script evaluated:

traceback (most recent call last):                                                file "/usr/local/bin/batcharge.py", line 9, in <module>     o_max = [l l in output.splitlines() if 'maxcapacity' in l][0]   file "/usr/local/bin/batcharge.py", line 9, in <listcomp>     o_max = [l l in output.splitlines() if 'maxcapacity' in l][0] typeerror: type str doesn't support buffer api 

here file, it's pretty short if can identify going on.

#!/usr/bin/env python # coding=utf-8  import math, subprocess  p = subprocess.popen(["ioreg", "-rc", "applesmartbattery"], stdout=subprocess.pipe) output = p.communicate()[0]  o_max = [l l in output.splitlines() if 'maxcapacity' in l][0] o_cur = [l l in output.splitlines() if 'currentcapacity' in l][0]  b_max = float(o_max.rpartition('=')[-1].strip()) b_cur = float(o_cur.rpartition('=')[-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'▸' empty = (total_slots - len(filled)) * u'▹'  out = (filled + empty).encode('utf-8') 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 sys.stdout.write(out) 

obviously, python3 choking on in list comprehension, yet can't figure out what.

according shell, returned output.splitlines() before program crashes:

[b'+-o applesmartbattery  <class applesmartbattery, id 0x1000001dc, registered, matched, active, busy 0 (1 ms), retain 6>', b'    {', b'      "externalconnected" = yes', b'      "timeremaining" = 0', b'      "instanttimetoempty" = 65535', b'      "externalchargecapable" = yes', b'      "fullpathupdated" = 1431620425', b'      "cellvoltage" = (4148,4147,4147,0)', b'      "voltage" = 12442', b'      "batteryinvalidwakeseconds" = 30', b'      "adapterinfo" = 0', b'      "maxcapacity" = 5886', b'      "permanentfailurestatus" = 0', b'      "manufacturer" = "smp"', b'      "location" = 0', b'      "currentcapacity" = 5727', b'      "legacybatteryinfo" = {"amperage"=0,"flags"=5,"capacity"=5886,"current"=5727,"voltage"=12442,"cycle count"=539}', b'      "firmwareserialnumber" = 27251', b'      "batteryinstalled" = yes', b'      "packreserve" = 200', b'      "cyclecount" = 539', b'      "designcapacity" = 6900', b'      "operationstatus" = 58435', b'      "manufacturedate" = 15675', b'      "avgtimetofull" = 65535', b'      "batteryserialnumber" = "w0040p2abbwza"', b'      "bootpathupdated" = 1430956311', b'      "postdischargewaitseconds" = 120', b'      "temperature" = 3036', b'      "uservisiblepathupdated" = 1431620485', b'      "instantamperage" = 0', b'      "manufacturerdata" = <000000000201000a01580000034b3138033030410341544c00130000>', b'      "maxerr" = 1', b'      "fullycharged" = yes', b'      "devicename" = "bq20z451"', b'      "iogeneralinterest" = "iocommand not serializable"', b'      "amperage" = 0', b'      "ischarging" = no', b'      "designcyclecount9c" = 1000', b'      "postchargewaitseconds" = 120', b'      "avgtimetoempty" = 65535', b'    }', b'    ', b''] 

i have tried other pages here have same error , fixes suggested not work me.

namely, bytes(l, 'utf-8') , l.encode('utf-8') not work. please advise.

quite simply, prepend strings in optional predicate of list comprehension b encode them bytes instead of strings. such, lines 9 , 10 become:

o_max = [l l in output.splitlines() if b'maxcapacity' in l][0] o_cur = [l l in output.splitlines() if b'currentcapacity' in l][0] 

although correctly realized there issue list comprehension, wasn't able figure out what; answer shamefully hiding in plain sight. kept trying convert being stored in l instead of paying attention string in code. leaving here in hopes of helping others.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -