converting uncompressed to compressed public key is so much easier than you think! you just have to know what they mean. here are the easy fast steps:
1. take the uncompressed public key and drop its first byte (which is 0x04)
2. split the remaining 64 bytes into 2x 32 bytes
3. take the first 32 byte, this is your (x coordinate) compressed public key after next step
4. take the second 32 bytes (y coordinate) if it was even add 0x02 otherwise 0x03 to the beginning of step 3
the function you posted seems to be first generating a "random private key" which is not what you asked for here.
additionally you can make it faster by skipping the % operation and using a bitwise AND operator with 1
also zfill(64) may need to change to zfill(33)