News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Python questions  (Read 549 times)

MysticLord

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Python questions
« on: September 19, 2020, 06:36:18 pm »
I'll post all my python questions (there will be many, many more in the future) in this thread.

I have a table.
Code: [Select]
FB 2C D2 93 40 00 00 00 00 00 00 00 EA C8 61 00
F9 AA F0 D7 C0 00 00 00 00 00 00 00 FD 28 CA 00
E2 49 B4 8B 50 00 00 00 00 00 00 00 F2 4B CE 00
E8 99 A4 4B B0 00 00 00 00 00 00 00 E1 37 D7 00
82 0E 00 C3 40 00 00 00 00 00 00 00 DE 89 19 00
F5 F1 FB 1F F0 00 00 00 00 00 00 00 E3 FE E6 00
FA E7 D2 DF D0 00 00 00 00 00 00 00 F5 EC F5 00
FF F9 FF 1F F0 00 00 00 00 00 00 00 F4 BD 79 00
FB C6 92 D3 20 00 00 00 00 00 00 00 FF FA FE 00
FB C9 54 03 D0 00 00 00 00 00 00 00 FF 13 0A 00
C9 96 92 CB B0 00 00 00 00 00 00 00 C5 1E BD 00
FB EE D2 C3 77 00 00 00 00 00 07 FC EA E9 F9 00
FD F9 FD 57 E0 00 00 00 00 00 00 00 F7 7D 7D 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
It's located at 0xE3FC in SCUS_942.30 (Saga Frontier). It's 256 bytes long, 16x16. It represents the Spark Talents lists in Saga Frontier, which are a list of bit flags which determine whether it's hard or not to spark specific skills. Each bit represents a single skill. The big gap in the middle are all the skills that can't be sparked by anyone. Each row is a list, which are assigned to human and half-human characters.



I first want to switch the columns and rows of the table, which I think is called transposing a matrix in Python. Can't force users to install some esoteric math module and I am not willing to spend a month figuring out licensing BS.

Language is Python 3.

How do I:
1. Read this into a list of lists?
2. Transpose it?

Regarding #1, search engines just return reads from text files which are totally useless.

I wrote code whose output must be seen to be believed.

Code 1:
Code: [Select]
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            tblList1.append(scus.read(16))
            #tblList1.insert(cntr, scus.read(16))
            cntr += 1
            for i in tblList1:
                for j in i:
                    print(hex(j)[2:].zfill(2) + "-", end='')
                print()

Output 1:
https://pastebin.com/9rr4XDsD

Code 2:
Code: [Select]
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            #tblList1.append(scus.read(16))
            tblList1.insert(cntr, scus.read(16))
            cntr += 1
            for i in tblList1:
                for j in i:
                    print(hex(j)[2:].zfill(2) + "-", end='')
                print()

Output 2:
https://pastebin.com/e6Aq26KB

I don't know if it's a "feature" of lists in Python, or something to do with the read() method used in binary mode, but it's apparently adding redundant lines over and over again. What should be a 16x16 list becomes a 138x16 list.



Assuming I ever get the code above to work, I then need to switch the rows and columns (aka transpose the matrix).

Once that's done, I need to move from printing 16 bytes per column to 256 ones (1) or zeroes (0) to represent talent or no-talent for each skill. I also need to add a space between each digit per row so it can safely be pasted into a spreadsheet.

edit

LOL my dumb *** missed the indentation = bracket, code fixed.

Disregard I am stoopidt!

Code: [Select]
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            #tblList1.append(scus.read(16))
            tblList1.insert(cntr, scus.read(16))
            cntr += 1
        for i in tblList1:
            for j in i:
                print(hex(j)[2:].zfill(2) + "-", end='')
            print()

Output:
Code: [Select]
$ python extractSparkTalents.py SCUS_942.30
fb-2c-d2-93-40-00-00-00-00-00-00-00-ea-c8-61-00-
f9-aa-f0-d7-c0-00-00-00-00-00-00-00-fd-28-ca-00-
e2-49-b4-8b-50-00-00-00-00-00-00-00-f2-4b-ce-00-
e8-99-a4-4b-b0-00-00-00-00-00-00-00-e1-37-d7-00-
82-0e-00-c3-40-00-00-00-00-00-00-00-de-89-19-00-
f5-f1-fb-1f-f0-00-00-00-00-00-00-00-e3-fe-e6-00-
fa-e7-d2-df-d0-00-00-00-00-00-00-00-f5-ec-f5-00-
ff-f9-ff-1f-f0-00-00-00-00-00-00-00-f4-bd-79-00-
fb-c6-92-d3-20-00-00-00-00-00-00-00-ff-fa-fe-00-
fb-c9-54-03-d0-00-00-00-00-00-00-00-ff-13-0a-00-
c9-96-92-cb-b0-00-00-00-00-00-00-00-c5-1e-bd-00-
fb-ee-d2-c3-77-00-00-00-00-00-07-fc-ea-e9-f9-00-
fd-f9-fd-57-e0-00-00-00-00-00-00-00-f7-7d-7d-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-

I'll transpose it tomorrow, time for a break.
« Last Edit: September 19, 2020, 06:46:47 pm by MysticLord »

Raeven0

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: Python questions
« Reply #1 on: September 20, 2020, 10:43:42 am »
Can't force users to install some esoteric math module
Blessings upon you, wise sage, for your labors to make Python's user experience slightly less miserable.

MysticLord

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Re: Python questions
« Reply #2 on: September 23, 2020, 06:13:08 pm »
This code is only transposing the bottom-left half of the matrix. SCUS_942.30 is SaGa Frontier, but you could change the baseAddr field to be anything on a dummy file with 16*16 byte data that is perhaps easier to visualize.

Code: [Select]
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            #tblList1.append(scus.read(16))
            tblList1.insert(cntr, bytearray(scus.read(16)))
            cntr += 1
        for a in tblList1:
            for b in a:
                print(hex(b)[2:].zfill(2) + "-", end='')
            print()
        print()
        #tblList2 = []
        tblList2 = tblList1
        for i in range(len(tblList1)):
            for j in range(len(tblList1[0])):
                tblList2[j][i] = tblList1[i][j]
        for c in tblList2:
            for d in c:
                print(hex(d)[2:].zfill(2) + "-", end='')
            print()

Output:
Code: [Select]
$ python extractSparkTalents.py SCUS_942.30
fb-2c-d2-93-40-00-00-00-00-00-00-00-ea-c8-61-00-
f9-aa-f0-d7-c0-00-00-00-00-00-00-00-fd-28-ca-00-
e2-49-b4-8b-50-00-00-00-00-00-00-00-f2-4b-ce-00-
e8-99-a4-4b-b0-00-00-00-00-00-00-00-e1-37-d7-00-
82-0e-00-c3-40-00-00-00-00-00-00-00-de-89-19-00-
f5-f1-fb-1f-f0-00-00-00-00-00-00-00-e3-fe-e6-00-
fa-e7-d2-df-d0-00-00-00-00-00-00-00-f5-ec-f5-00-
ff-f9-ff-1f-f0-00-00-00-00-00-00-00-f4-bd-79-00-
fb-c6-92-d3-20-00-00-00-00-00-00-00-ff-fa-fe-00-
fb-c9-54-03-d0-00-00-00-00-00-00-00-ff-13-0a-00-
c9-96-92-cb-b0-00-00-00-00-00-00-00-c5-1e-bd-00-
fb-ee-d2-c3-77-00-00-00-00-00-07-fc-ea-e9-f9-00-
fd-f9-fd-57-e0-00-00-00-00-00-00-00-f7-7d-7d-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-

fb-2c-d2-93-40-00-00-00-00-00-00-00-ea-c8-61-00-
2c-aa-f0-d7-c0-00-00-00-00-00-00-00-fd-28-ca-00-
d2-f0-b4-8b-50-00-00-00-00-00-00-00-f2-4b-ce-00-
93-d7-8b-4b-b0-00-00-00-00-00-00-00-e1-37-d7-00-
40-c0-50-b0-40-00-00-00-00-00-00-00-de-89-19-00-
00-00-00-00-00-00-00-00-00-00-00-00-e3-fe-e6-00-
00-00-00-00-00-00-00-00-00-00-00-00-f5-ec-f5-00-
00-00-00-00-00-00-00-00-00-00-00-00-f4-bd-79-00-
00-00-00-00-00-00-00-00-00-00-00-00-ff-fa-fe-00-
00-00-00-00-00-00-00-00-00-00-00-00-ff-13-0a-00-
00-00-00-00-00-00-00-00-00-00-00-00-c5-1e-bd-00-
00-00-00-00-00-00-00-00-00-00-00-fc-ea-e9-f9-00-
ea-fd-f2-e1-de-e3-f5-f4-ff-ff-c5-ea-f7-7d-7d-00-
c8-28-4b-37-89-fe-ec-bd-fa-13-1e-e9-7d-00-00-00-
61-ca-ce-d7-19-e6-f5-79-fe-0a-bd-f9-7d-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-

Output with transposed portions highlighted:
Spoiler:
$ python extractSparkTalents.py SCUS_942.30
fb-2c-d2-93-40-00-00-00-00-00-00-00-ea-c8-61-00-
f9-aa-f0-d7-c0-00-00-00-00-00-00-00-fd-28-ca-00-
e2-49-b4-8b-50-00-00-00-00-00-00-00-f2-4b-ce-00-
e8-99-a4-4b-b0-00-00-00-00-00-00-00-e1-37-d7-00-
82-0e-00-c3-40-00-00-00-00-00-00-00-de-89-19-00-
f5-f1-fb-1f-f0-00-00-00-00-00-00-00-e3-fe-e6-00-
fa-e7-d2-df-d0-00-00-00-00-00-00-00-f5-ec-f5-00-
ff-f9-ff-1f-f0-00-00-00-00-00-00-00-f4-bd-79-00-
fb-c6-92-d3-20-00-00-00-00-00-00-00-ff-fa-fe-00-
fb-c9-54-03-d0-00-00-00-00-00-00-00-ff-13-0a-00-
c9-96-92-cb-b0-00-00-00-00-00-00-00-c5-1e-bd-00-
fb-ee-d2-c3-77-00-00-00-00-00-07-fc-ea-e9-f9-00-
fd-f9-fd-57-e0-00-00-00-00-00-00-00-f7-7d-7d-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-

fb-2c-d2-93-40-00-00-00-00-00-00-00-ea-c8-61-00-
2c-aa-f0-d7-c0-00-00-00-00-00-00-00-fd-28-ca-00-
d2-f0-b4-8b-50-00-00-00-00-00-00-00-f2-4b-ce-00-
93-d7-8b-4b-b0-00-00-00-00-00-00-00-e1-37-d7-00-
40-c0-50-b0-40-00-00-00-00-00-00-00-de-89-19-00-
00-00-00-00-00-00-00-00-00-00-00-00-e3-fe-e6-00-//unable to tell where transposition ends
00-00-00-00-00-00-00-00-00-00-00-00-f5-ec-f5-00-//^
00-00-00-00-00-00-00-00-00-00-00-00-f4-bd-79-00-//^
00-00-00-00-00-00-00-00-00-00-00-00-ff-fa-fe-00-//^
00-00-00-00-00-00-00-00-00-00-00-00-ff-13-0a-00-//^
00-00-00-00-00-00-00-00-00-00-00-00-c5-1e-bd-00-//^
00-00-00-00-00-00-00-00-00-00-00-fc-ea-e9-f9-00-
ea-fd-f2-e1-de-e3-f5-f4-ff-ff-c5-ea-f7-7d-7d-00-
c8-28-4b-37-89-fe-ec-bd-fa-13-1e-e9-7d-00-00-00-//ending point is an educated guess
61-ca-ce-d7-19-e6-f5-79-fe-0a-bd-f9-7d-00-00-00-//^
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-//unable to tell if transposed

edit

Apparently the Finn (naturally, it's a Finn) who made Thonny didn't see fit to include breakpoints in his precious editor, instead deciding that some insane crap he defecated on his keyboard was good enough for us:
https://github.com/thonny/thonny/issues/1335

So now I have step through the entire ****ing program or read a bunch of other stuff and do research and yadda yadda yadda yadda stuff I don't care about yadda yadda yadda I just want to ****ing program yadda yadda yadda God kill me now.
« Last Edit: September 23, 2020, 09:22:58 pm by MysticLord »