0

I found a really old Python example (Mandelbrot) on my harddisk which worked years ago. But now I cannot fix it ... :-(

Traceback (most recent call last):
  File "/Volumes/SSD300/Python/Mandel.py", line 11, in <module>
    for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
TypeError: can only concatenate str (not "bytes") to str

Python 3.9.0 on macOS

_                                      =   (
                                        255,
                                      lambda
                               V       ,B,c
                             :c   and Y(V*V+B,B,  c
                               -1)if(abs(V)<6)else
               (              2+c-4*abs(V)**-0.4)/i
                 )  ;v,      x=1500,1000;C=range(v*x
                  );import  struct;P=struct.pack;M,\
            j  ='<QIIHHHH',open('M.bmp','wb').write
for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
            i  ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9
                  *i-950*T  **99,T*70-880*T**18+701*
                 T  **9     ,T*i**(1-T**45*2)))(sum(
               [              Y(0,(A%3/3.+X%v+(X/v+
                               A/3/3.-x/2)/1j)*2.5
                             /x   -2.7,i)**2 for  \
                               A       in C
                                      [:9]])
                                        /9)
                                       )   )
2
  • 1
    struct.pack returned a bytes object, but this code attempted to concatenate it to the string 'BM', which is not possible Commented Dec 1, 2020 at 20:30
  • Clever formatting, but totally unreadable. Commented Dec 1, 2020 at 20:35

1 Answer 1

1

Presumably, P is returning a bytes object, while 'BM' remains a str. Simplest fix is to change 'BM' to b'BM', but given this code likely comes from Python 2, it probably expects to iterate over length 1 strings, not ints, so it might make sense to .decode('latin-1') the result of P to get a str that represents the raw bytes.

Porting from Python 2 to Python 3 when str was used in Python 2 in a way that's not entirely text or raw bytes oriented is tricky; this is one of those cases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.