Saturday, January 27, 2024

Convertion format XLSX into CSV on Fedora 39 (Python 3.12.1)

 Task itself 

The file say XX_YY.xlsx contains information about a set of N computational processes that can be executed in parallel or sequentially. We will say that process B depends on process A if the execution of process B requires the results of process A. In this case, the processes can only be executed sequentially. Information about processes is presented in the file in the form of a table. The first line of the table indicates the process identifier (ID), the second line of the table indicates its execution time in milliseconds, the third line lists them with the separator “;” IDs of the processes on which this process depends. If the process is independent, then the table shows the value 0. Determine the minimum time after which the entire set of processes will complete execution, provided that all processes independent of each other can be executed in parallel.













Following below is updated code which  works on F39 (python 3.12.1)

(.env) boris@fedora:~/KG2024/D22$ cat djs22Solve.py

(.env) boris@fedora39wks:~/KG2024/D22$ cat djs22Solve.py

import openpyxl

import csv as Csv

from csv import reader

# input excel file path

inputExcel = './22_23.xlsx'

newWorkbook = openpyxl.load_workbook(inputExcel)

# getting the active workbook sheet(Bydefault-->Sheet1)

worksheet = newWorkbook.active

# Opening a output csv file in write mode

F = open("./22_23.csv", 'w')

OutCsv = Csv.writer(F,delimiter=";")

for eachrow in worksheet.rows:

    OutCsv.writerow([cell.value for cell in eachrow])

F.close()

"""

Code right below is original version proposed, which required manual conversion Excel spreadsheet to csv format on Fedora 39, Python version 3.12

"""

def f(d):

    if d[2] == [0]:

        return d[1]

    else:

        maxx = 0

        for i in d[2]:

            if maxx < f(index[i - 1]):

                maxx = f(index[i - 1])

        return maxx + d[1]

with open("./22_23.csv") as F:

    s = reader(F, delimiter=';', quotechar='"')

    next(s)

    index = []

    for i in s:

          index.append([int(i[0]), int(i[1]), list(map(int, str(i[2]).split(';')))])

    for i in range(len(index)):

        print(i + 1, f(index[i]))

Runtime phase

(.env) boris@fedora39wks:~/KG2024/D22$ python djs22Solve.py

1 9

2 18

3 26

4 22

5 26

6 29

7 7

8 33

9 33

10 39

11 35

12 36

13 37

14 44 <== that is an answer

15 43

Running same module in PyCharm (Python 3.12.1) installed on Fedora 39 via snapd (PyCharm version 23.3.3)



ALT+Enter performs import openpyxl in current project ( this time PyCharm has been installed via snap, noticed on 02/02/24 )
Another sample running project with numpy and matplotlib imported via PyCharm&&Python 3.12.1 on Fedora 39
"""
Python Code
"""
import matplotlib.pyplot as plt
import numpy as np
cos = np.cos
sin = np.sin
sqrt = np.sqrt
pi = np.pi
def surf(u, v):
    half = (0 <= u) & (u < pi)
    r = 4*(1 - cos(u)/2)
    x = 6*cos(u)*(1 + sin(u)) + r*cos(v + pi)
    x[half] = (
        (6*cos(u)*(1 + sin(u)) + r*cos(u)*cos(v))[half])
    y = 16 * sin(u)
    y[half] = (16*sin(u) + r*sin(u)*cos(v))[half]
    z = r * sin(v)
    return x, y, z
u, v = np.linspace(0, 2*pi, 40), np.linspace(0, 2*pi, 40)
ux, vx =  np.meshgrid(u,v)
x, y, z = surf(ux, vx)
fig = plt.figure()
ax = plt.axes(projection='3d')
plot = ax.plot_surface(x, y, z, rstride = 1, \
        cstride = 1, cmap = plt.get_cmap('jet'),\
        linewidth = 0, antialiased = False)
plt.show()




No comments:

Post a Comment