Python Forum
Why is Python so hard to maintain
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is Python so hard to maintain
#11
(Aug-15-2019, 03:42 PM)RMJFlack Wrote: But WHY would they break existing code by redefining the type of the result of struct.pack, rather than introduce a NEW function struct.pack2byte or some such.
For Python 2 to 3 there was core changes to language that could not be fixed easy with new function/class or something else.
This was discussed for many years as breaking backward compatibility should not be taken easy.
As mention 2 to 3 is the only time Python broke backward compatibility.

When talk about core features like eg Unicode one of the biggest changes in Python 3,it could not be done with just new function/class.
# Python 2.7
>>> chess = 'Chess is cool ♟♜♞ on a rainy day ☂'
>>> chess
'Chess is cool \xe2\x99\x9f\xe2\x99\x9c\xe2\x99\x9e on a rainy day \xe2\x98\x82' 
# Python 3.7
>>> chess = 'Chess is cool ♟♜♞ on a rainy day ☂'
>>> chess
'Chess is cool ♟♜♞ on a rainy day ☂' 
Reply
#12
Ok, executive summary of the current problem (I should probably post separate specific threads) but ...

I have a Python 3.3 script (about 4 years old) that creates a bmp file from data (a sort of contour map). I found somewhere, and my bad I forget where, some sample code that does the guts of the bmp generation and wrapped my application around that, and it all worked fine. The code uses numpy and struct.

Somehow in porting to a new computer, numpy missed the boat. Trying to reinstall numpy to Python33 gives errors, even with forcing version 1.11.3 which is supposed to be correct for Python 3.3

I can install current numpy to my Python37.

BUT in Python 3.7, struct.pack is changed to byte type from string, and I'm having a real tough time trying to fix the code to with with it. (I think I should maybe put those specifics in a separate thread).

(Aug-15-2019, 04:09 PM)snippsat Wrote:
(Aug-15-2019, 03:42 PM)RMJFlack Wrote: But WHY would they break existing code by redefining the type of the result of struct.pack, rather than introduce a NEW function struct.pack2byte or some such.
For Python 2 to 3 there was core changes to language that could not be fixed easy with new function/class or something else.
This was discussed for many years as breaking backward compatibility should not be taken easy.
As mention 2 to 3 is the only time Python broke backward compatibility.

When talk about core features like eg Unicode one of the biggest changes in Python 3,it could not be done with just new function/class.
# Python 2.7
>>> chess = 'Chess is cool ♟♜♞ on a rainy day ☂'
>>> chess
'Chess is cool \xe2\x99\x9f\xe2\x99\x9c\xe2\x99\x9e on a rainy day \xe2\x98\x82' 
# Python 3.7
>>> chess = 'Chess is cool ♟♜♞ on a rainy day ☂'
>>> chess
'Chess is cool ♟♜♞ on a rainy day ☂' 

Is the change in the internal binary representation of chess, or in how it is displayed on the terminal? ie if chess is written to a native file in each case do the file contents differ?

Can you point me to any tutorial on converting code using struct.pack from 3.3 to >=3.5?
What I probably need to do is write a cover function pack2string that produces the same result in 3.7 as 3.3 did and then just apply that in every reference to struct.pack ie
 bmpfile += pack2string(struct.pack(....)) 
And we are off topic and into the weeds, my apologies.
Reply
#13
(Aug-15-2019, 04:16 PM)RMJFlack Wrote: BUT in Python 3.7, struct.pack is changed to byte type from string, and I'm having a real tough time trying to fix the code to with with it.
struct.pack() in 3.3.
Quote:struct.pack(fmt, v1, v2, ...)

Return a bytes object containing the values v1, v2, ... packed according to the format string fmt. The arguments must match the values required by the format exactly.
struct.pack() in 3.7.
Quote:struct.pack(format, v1, v2, ...)

Return a bytes object containing the values v1, v2, … packed according to the format string format. The arguments must match the values required by the format exactly.

So, in both 3.3 and 3.7 struct.pack() returns bytes object. There is no change according to official docs. Probably the problem is somewhere else in your code.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
(Aug-15-2019, 05:01 PM)buran Wrote:
(Aug-15-2019, 04:16 PM)RMJFlack Wrote: BUT in Python 3.7, struct.pack is changed to byte type from string, and I'm having a real tough time trying to fix the code to with with it.
struct.pack() in 3.3.
Quote:struct.pack(fmt, v1, v2, ...)

Return a bytes object containing the values v1, v2, ... packed according to the format string fmt. The arguments must match the values required by the format exactly.
struct.pack() in 3.7.
Quote:struct.pack(format, v1, v2, ...)

Return a bytes object containing the values v1, v2, … packed according to the format string format. The arguments must match the values required by the format exactly.

So, in both 3.3 and 3.7 struct.pack() returns bytes object. There is no change according to official docs. Probably the problem is somewhere else in your code.

Hmmm ... you are right, It is IS 4 years since I looked at this code, and it was originally in 2.7. So Im not sure what the 3.3 version was about.
It seems to me the best way forward is to move to 3.7
I just need to make sure that the file created by struct.pack() in 3.7 (with bytes output) is the same as in 2.7 with string output.
Would this be the correct 'conversion' code:
def pack2string(byt):
    s = ""
    for b in byt:
        s += chr(b)
    return s

pixels += pack2string(struct.pack('<BBB',b,g,r))
I believe that in 2.7 each pixel formatted by struct.pack() is a triple of chr; in 3.3 it is a triple of int. Is that correct?
Reply
#15
can you provide minimal example with sample data so we can see what expected output(i.e. from python2)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
(Aug-15-2019, 07:04 PM)buran Wrote: can you provide minimal example with sample data so we can see what expected output(i.e. from python2)

I will try too. (Have to make a cut down version of the full prpgram).

Aha, its coming together! It seems that in 2.7, a file that is opened as 'wb' will accept writing of strings. In 3.7, it expects bytes. So actually all my fussing about in the 3.7 version about converting struct.pack() back to strings (in order to do outfile.write(...) ) is actually unnecessary.
They didn't just change struct.pack(), they changed write() too. I'll post the cleaned up version in separate thread for critique, but I have two working test programs in 2.7 and 3.7.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Hire a Python Programmer? It's hard because I don't know pythonforumuser 1 732 Feb-10-2020, 12:02 PM
Last Post: metulburr
  how hard to translate this to python? Skaperen 4 2,263 Oct-18-2017, 07:37 AM
Last Post: buran
  Should Learn Python The Hard Way's be in the forums list of books Yoriz 16 9,276 Nov-09-2016, 05:47 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020