Sudoku checker issues with Python - python

I'm trying to create a sudoku checker in Python. I found a version here in another thread, but it does not work properly. I wonder what is the issue?
I receive the following error:
Traceback (most recent call last):
File "C:\Users\Omistaja\Downloads\sudoku_checker_template.py", line 72, in <module>
main()
File "C:\Users\Omistaja\Downloads\sudoku_checker_template.py", line 63, in main
is_valid = check_sudoku_grid(grid)
File "C:\Users\Omistaja\Downloads\sudoku_checker_template.py", line 20, in check_sudoku_grid
if grid[row][col] < 1 or type(grid[row][col]) is not type(1):
TypeError: '<' not supported between instances of 'NoneType' and 'int'
Anyway, below is the whole thing. Only the check_sudoku_grid should be modified, the rest should work. Thanks for your help!
from grids import GRID_NAMES, GRID_RETURNS, GRIDS, GRIDS_BIG, GRIDS_SMALL
GRID_SIZE = 9 # Length of one side of the sudoku
SUBGRID_SIZE = 3 # Length of one side of a cell of the sudoku
def check_sudoku_grid(grid):
"""
Parameter : GRID_SIZE * GRID_SIZE two-dimensional list
Return value : Boolean (True/False)
Checks whether a sudoku grid is valid
ie. doesn't contain any duplicates (besides None)
in any row, column or cell.
"""
for row in range(len(grid)):
for col in range(len(grid)):
# check value is an int
if grid[row][col] < 1 or type(grid[row][col]) is not type(1):
return False
# check value is within 1 through n.
# for example a 2x2 grid should not have the value 8 in it
elif grid[row][col] > len(grid):
return False
# check the rows
for row in grid:
if sorted(list(set(row))) != sorted(row):
return False
# check the cols
cols = []
for col in range(len(grid)):
for row in grid:
cols += [row[col]]
# set will get unique values, its converted to list so you can compare
# it's sorted so the comparison is done correctly.
if sorted(list(set(cols))) != sorted(cols):
return False
cols = []
# if you get past all the false checks return True
return True
def print_grid(grid):
for i in range(GRID_SIZE):
row = ""
for j in range(GRID_SIZE):
try:
val = int(grid[i][j])
except TypeError:
val = "_"
except ValueError:
val = grid[i][j]
row += "{} ".format(val)
if j % SUBGRID_SIZE == SUBGRID_SIZE - 1:
row += " "
print(row)
if i % SUBGRID_SIZE == SUBGRID_SIZE - 1:
print()
def main():
i = 0
for grid in GRIDS:
is_valid = check_sudoku_grid(grid)
print("This grid {:s}.".format(GRID_NAMES[i]))
print("Your function should return: {:s}".format(GRID_RETURNS[i]))
print("Your function returns: {}".format(is_valid))
print_grid(grid)
i += 1
main()

GRID_NAMES = ["is valid", "is valid containing None values", "is valid containing None values (2)", \
"has an invalid row", "has an invalid column", "has an invalid subgrid"]
GRID_RETURNS = ["True","True","True","False","False","False"]
n = None
a = 'a'
b = 'b'
c = 'c'
d = 'd'
e = 'e'
f = 'f'
g = 'g'
GRID_VALID = [[7,3,5, 6,1,4, 8,9,2],
[8,4,2, 9,7,3, 5,6,1],
[9,6,1, 2,8,5, 3,7,4],
[2,8,6, 3,4,9, 1,5,7],
[4,1,3, 8,5,7, 9,2,6],
[5,7,9, 1,2,6, 4,3,8],
[1,5,7, 4,9,2, 6,8,3],
[6,9,4, 7,3,8, 2,1,5],
[3,2,8, 5,6,1, 7,4,9]
]
GRID_VALID_NONE = [[7,3,5, 6,1,4, 8,9,2],
[8,4,2, 9,7,3, 5,6,1],
[9,6,1, 2,8,5, 3,7,4],
[2,n,n, 3,4,n, 1,5,7],
[4,1,3, 8,5,7, 9,2,6],
[5,7,9, 1,2,6, 4,3,8],
[1,5,7, 4,9,n, 6,8,3],
[6,9,4, 7,3,8, 2,1,5],
[n,2,8, 5,6,1, 7,4,9]
]
GRID_VALID_NONE_2 = [[7,3,5, 6,1,4, n,9,2],
[8,4,2, 9,7,3, 5,6,1],
[n,n,1, 2,8,5, 3,7,4],
[2,n,n, 3,4,n, 1,5,7],
[4,1,3, 8,5,7, 9,2,6],
[5,n,9, 1,2,6, 4,3,8],
[1,5,7, 4,9,n, n,8,3],
[6,9,4, 7,3,8, 2,1,5],
[n,2,8, 5,6,1, 7,4,n]
]
GRID_INVALID_SUBGRID = [[7,3,5, 6,1,4, 8,9,2],
[8,4,2, 9,7,3, 5,6,1],
[9,6,1, 2,8,5, 3,7,4],
[2,8,6, 3,4,9, 1,5,7],
[4,1,3, n,5,7, 9,2,6],
[5,7,9, 1,2,6, 4,3,8],
[1,5,7, 4,9,2, 6,8,3],
[6,9,4, 7,3,8, 2,1,5],
[3,2,n, 8,6,1, 7,4,9]
]
GRID_INVALID_ROW = [[7,3,5, 6,1,4, 8,9,2],
[8,4,2, 9,7,3, 5,6,1],
[9,6,1, 2,8,5, 3,7,4],
[2,8,6, 3,4,9, 1,5,7],
[4,1,3, 8,5,7, 9,2,6],
[5,7,9, 1,2,6, 4,3,8],
[1,5,7, 4,9,2, 6,8,n],
[6,9,4, 7,3,8, 2,1,3],
[3,2,8, 5,6,1, 7,4,9]
]
GRID_INVALID_COLUMN = [[7,3,5, 6,1,4, 8,9,2],
[8,4,2, 9,7,3, 5,6,1],
[9,6,1, 2,8,5, 3,7,4],
[2,8,6, 3,4,9, 1,5,7],
[4,1,3, 8,5,7, 9,2,6],
[5,7,9, 1,2,6, 4,3,8],
[1,5,n, 4,9,2, 6,8,3],
[6,9,4, 7,3,8, 2,1,5],
[7,2,8, 5,6,1, n,4,9]
]
GRIDS = [GRID_VALID, GRID_VALID_NONE, GRID_VALID_NONE_2, \
GRID_INVALID_ROW, GRID_INVALID_COLUMN, GRID_INVALID_SUBGRID]
GRID_SMALL_VALID = [[1,2, 3,4],
[3,4, 1,2],
[2,3, 4,1],
[4,1, 2,3]]
GRID_SMALL_VALID_NONE = [[1,n, 3,4],
[3,4, n,n],
[2,n, 4,1],
[4,1, n,3]]
GRID_SMALL_VALID_NONE_2 = [[1,n, 3,4],
[n,n, n,2],
[2,n, 4,1],
[4,n, 2,3]]
GRID_SMALL_INVALID_ROW = [[1,2, 3,n],
[2,3, 4,4],
[3,4, 1,2],
[4,1, 2,3]]
GRID_SMALL_INVALID_COLUMN = [[1,2, 3,4],
[2,3, 4,1],
[3,4, n,1],
[4,1, 2,3]]
GRID_SMALL_INVALID_SUBGRID = [[1,2, 3,4],
[2,3, 4,1],
[3,4, 1,2],
[4,1, 2,3]]
GRIDS_SMALL = [GRID_SMALL_VALID, GRID_SMALL_VALID_NONE, GRID_SMALL_VALID_NONE_2, \
GRID_SMALL_INVALID_ROW, GRID_SMALL_INVALID_COLUMN, GRID_SMALL_INVALID_SUBGRID]
GRID_BIG_VALID = [[4,a,9,f, 1,7,d,8, 6,e,2,c, g,5,3,b],
[2,5,3,1, f,4,b,g, d,9,8,7, 6,a,c,e],
[e,6,d,c, 3,a,5,2, g,b,1,4, 8,f,9,7],
[b,7,g,8, 6,e,9,c, 5,3,a,f, 1,2,d,4],
[8,g,b,4, d,f,e,9, 2,5,7,3, c,1,a,6],
[1,e,6,d, c,8,4,5, a,g,9,b, 2,3,7,f],
[a,f,5,3, 2,1,6,7, 4,c,e,8, 9,b,g,d],
[c,2,7,9, b,3,g,a, f,d,6,1, 4,8,e,5],
[9,4,1,a, e,2,3,d, b,f,c,6, 7,g,5,8],
[5,8,e,g, 7,9,1,6, 3,4,d,a, b,c,f,2],
[7,3,f,6, g,b,c,4, 8,2,5,9, e,d,1,a],
[d,c,2,b, a,5,8,f, 7,1,g,e, 3,6,4,9],
[f,9,8,2, 4,c,7,3, 1,a,b,d, 5,e,6,g],
[6,d,c,5, 9,g,f,1, e,8,4,2, a,7,b,3],
[g,b,4,7, 8,d,a,e, c,6,3,5, f,9,2,1],
[3,1,a,e, 5,6,2,b, 9,7,f,g, d,4,8,c]
]
GRID_BIG_VALID_NONE = [[4,a,9,n, 1,7,d,8, 6,e,n,c, g,5,3,n],
[n,5,3,1, f,n,b,g, d,9,8,7, 6,a,c,e],
[e,6,d,c, 3,a,5,2, g,b,n,4, n,f,n,7],
[b,7,n,8, n,e,9,c, n,3,a,f, 1,2,d,4],
[8,g,b,4, d,f,n,9, 2,5,7,n, c,1,a,6],
[1,e,n,d, c,n,4,5, a,g,n,b, 2,3,7,f],
[a,f,n,3, 2,1,n,7, n,n,e,8, 9,b,g,n],
[c,2,7,9, b,3,g,a, f,d,6,1, 4,n,n,5],
[9,4,1,a, e,n,3,d, b,f,c,6, 7,g,5,8],
[5,n,e,g, 7,9,n,6, 3,4,d,a, b,n,f,2],
[7,3,f,6, g,b,c,4, n,n,5,9, e,d,n,a],
[n,n,n,b, a,5,8,f, 7,1,n,e, 3,6,4,9],
[f,9,8,2, 4,c,7,3, n,n,b,d, 5,e,6,g],
[6,n,c,5, 9,n,f,1, e,n,4,2, a,7,n,3],
[g,b,4,7, 8,d,a,e, c,6,n,5, f,9,2,n],
[3,1,n,n, n,6,2,b, 9,7,f,g, d,4,8,c]
]
GRID_BIG_VALID_NONE_2 = [[4,a,9,f, 1,7,d,n, 6,e,n,n, g,n,3,b],
[2,5,3,1, f,4,b,g, d,n,8,7, 6,a,n,e],
[e,6,d,c, 3,a,n,2, g,b,1,4, 8,f,9,7],
[b,7,g,n, n,e,9,c, 5,3,a,n, n,2,d,4],
[8,g,b,4, d,f,e,n, 2,5,7,3, c,1,a,6],
[1,n,6,d, n,n,4,n, a,g,n,b, 2,3,7,f],
[a,f,5,3, 2,1,6,7, 4,c,e,8, 9,b,g,d],
[c,2,7,n, b,3,g,a, f,d,6,1, 4,8,e,5],
[9,4,1,a, e,2,n,n, b,f,c,n, 7,g,5,8],
[5,n,e,g, 7,9,n,6, 3,4,d,a, b,c,f,2],
[7,3,f,6, g,b,c,4, 8,n,n,n, e,d,1,a],
[d,c,2,n, a,n,8,f, 7,1,g,n, 3,6,n,9],
[f,n,8,2, 4,c,7,3, 1,a,b,d, n,e,6,n],
[6,d,c,5, 9,g,f,1, e,8,4,2, a,7,b,3],
[g,b,n,7, 8,d,a,e, n,6,n,5, f,n,2,n],
[n,1,a,e, n,6,2,b, 9,n,f,g, d,n,8,c]
]
GRID_BIG_INVALID_ROW = [[4,a,9,f, 1,7,d,8, 6,e,2,c, g,5,3,b],
[2,5,3,1, f,4,b,g, d,9,8,7, 6,a,c,e],
[e,6,d,c, 3,a,5,2, g,b,1,4, 8,f,9,7],
[b,7,g,8, 6,e,9,c, 5,3,a,f, 1,2,d,4],
[8,g,b,4, d,f,e,9, 2,5,7,3, c,1,a,6],
[1,e,6,d, c,n,4,5, a,g,9,b, 2,3,7,f],
[a,f,5,3, 2,1,6,7, 4,c,e,8, 9,b,g,d],
[c,2,7,9, b,3,g,a, f,d,6,1, 4,8,e,5],
[9,4,1,a, e,2,3,d, b,f,c,6, 7,g,5,8],
[5,8,e,g, 7,9,1,6, 3,4,d,a, b,c,f,2],
[7,3,f,6, g,b,c,4, 8,2,5,9, e,d,1,a],
[d,c,2,b, a,8,8,f, 7,1,g,e, 3,6,4,9],
[f,9,8,2, 4,c,7,3, 1,a,b,d, 5,e,6,g],
[6,d,c,5, 9,g,f,1, e,8,4,2, a,7,b,3],
[g,b,4,7, 8,d,a,e, c,6,3,5, f,9,2,1],
[3,1,a,e, 5,6,2,b, 9,7,f,g, d,4,8,c]
]
GRID_BIG_INVALID_COLUMN = [[4,a,9,f, 1,7,d,8, 6,e,2,c, g,5,3,b],
[2,5,3,1, f,4,b,g, d,9,8,7, 6,a,c,e],
[e,6,d,c, 3,a,5,2, g,b,1,4, 8,f,9,7],
[b,7,g,8, 6,e,9,c, 5,3,a,f, 1,2,d,4],
[8,g,b,4, d,f,e,9, 2,5,7,3, c,1,a,6],
[1,e,6,d, c,8,4,5, a,g,9,b, 2,3,7,f],
[a,f,5,3, 2,1,6,n, 4,c,e,8, 9,b,7,d],
[c,2,7,9, b,3,g,a, f,d,6,1, 4,8,e,5],
[9,4,1,a, e,2,3,d, b,f,c,6, 7,g,5,8],
[5,8,e,g, 7,9,1,6, 3,4,d,a, b,c,f,2],
[7,3,f,6, g,b,c,4, 8,2,5,9, e,d,1,a],
[d,c,2,b, a,5,8,f, 7,1,g,e, 3,6,4,9],
[f,9,8,2, 4,c,7,3, 1,a,b,d, 5,e,6,g],
[6,d,c,5, 9,g,f,1, e,8,4,2, a,7,b,3],
[g,b,4,7, 8,d,a,e, c,6,3,5, f,9,2,1],
[3,1,a,e, 5,6,2,b, 9,7,f,g, d,4,8,c]
]
GRID_BIG_INVALID_SUBGRID = [[4,a,9,f, 1,7,d,8, 6,e,2,c, g,5,3,b],
[2,5,3,1, f,4,b,g, d,9,8,7, 6,a,c,e],
[e,6,d,c, 3,a,5,2, g,b,1,4, 8,f,9,7],
[b,7,g,8, 6,e,9,c, 5,3,a,f, 1,2,d,4],
[8,g,b,4, d,f,e,9, 2,5,7,3, c,1,a,6],
[1,e,6,d, c,8,4,5, a,g,9,b, 2,3,7,f],
[a,f,5,3, 2,1,6,7, 4,c,e,8, 9,b,g,d],
[c,2,7,9, b,3,g,a, f,d,6,1, 4,8,e,5],
[9,4,1,a, e,2,3,d, b,f,c,6, 7,g,5,8],
[5,8,e,g, 7,9,1,6, 3,n,d,a, b,c,f,2],
[7,3,f,6, g,b,c,4, 8,2,5,9, e,d,1,a],
[d,c,2,b, a,5,8,f, 7,1,g,e, 3,6,4,9],
[f,9,8,2, 4,c,7,3, 1,a,b,d, 5,e,6,g],
[6,d,c,5, 9,g,f,1, e,8,4,2, a,7,b,3],
[g,b,n,7, 8,d,a,e, c,4,3,5, f,9,2,1],
[3,1,a,e, 5,6,2,b, 9,7,f,g, d,4,8,c]
]
GRIDS_BIG = [GRID_BIG_VALID, GRID_BIG_VALID_NONE, GRID_BIG_VALID_NONE_2, \
GRID_BIG_INVALID_ROW, GRID_BIG_INVALID_COLUMN, GRID_BIG_INVALID_SUBGRID]

Related

Invalid character "\u64e" in token Pylance

What is the meaning of this error Invalid character "\u64e" in token Pylance the read error line under Acc for this code, How can fixed it?
err = calculateCError()
print('Error is:', err, '%')
َAcc = 100 - err
print('َAccuracy is:',َAcc , '%')
Here's how to debug something like this:
s = """err = calculateCError()
print('Error is:', err, '%')
َAcc = 100 - err
print('َAccuracy is:',َAcc , '%')"""
print([hex(ord(c)) for c in s])
['0x65', '0x72', '0x72', '0x20', '0x3d', '0x20', '0x63', '0x61', '0x6c',
'0x63', '0x75', '0x6c', '0x61', '0x74', '0x65', '0x43', '0x45', '0x72',
'0x72', '0x6f', '0x72', '0x28', '0x29', '0xa', '0x70', '0x72', '0x69',
'0x6e', '0x74', '0x28', '0x27', '0x45', '0x72', '0x72', '0x6f', '0x72',
'0x20', '0x69', '0x73', '0x3a', '0x27', '0x2c', '0x20', '0x65', '0x72',
'0x72', '0x2c', '0x20', '0x27', '0x25', '0x27', '0x29', '0xa', '0x64e',
'0x41', '0x63', '0x63', '0x20', '0x3d', '0x20', '0x31', '0x30', '0x30',
'0x20', '0x2d', '0x20', '0x65', '0x72', '0x72', '0xa', '0x70', '0x72',
'0x69', '0x6e', '0x74', '0x28', '0x27', '0x64e', '0x41', '0x63', '0x63',
'0x75', '0x72', '0x61', '0x63', '0x79', '0x20', '0x69', '0x73', '0x3a',
'0x27', '0x2c', '0x64e', '0x41', '0x63', '0x63', '0x20', '0x2c', '0x20',
'0x27', '0x25', '0x27', '0x29']
And sure enough, there are three instances of 0x64E, always appearing before 0x41 (A). In fact, if you look carefully at your A characters, you will notice a faint slanted accent line above the A. This is called Arabic Fatha in Unicode. Here is a 320% zoom from my browser showing it more obviously:

(Sympy) Why do I get an unsupported operand error when trying to solve a specific equation block using Newton's Method despite other blocks working?

I have a system of 58 equations (in which some equations are non-linear and/or multivariate) which I have separated into blocks (via Trajan's algorithm) for easier solving and faster computational time. A solution for the whole system definitely exists as I've tried it on a software (Engineering Equation Solver (EES)) and I'm getting the right answers.
Now, I'm trying to recreate/convert my original system on EES to Python via (mostly) Sympy. Right now, I'm near completion and all blocks can be solved EXCEPT one (it's maddening!!!).
The code in question relates to "Block 2" (B2) and Newtons Method where:
The equation/function matrix is "B2FunctionMatrix" and consists of 21 equations.
The variable, guess (initial), next guess and Jacobian matrices are labelled accordingly in the code.
The tolerance is 0.05
The solved function matrix is "B2fsolve"
The solved jacobian matrix is "B2jsolve"
The solved function matrix with respect to the jacobian is "B2delta_x0"
The residuals matrix is "B2guessVariance"
Throughout the code I have placed numerous "placeholders" to see where the code gets up to upon execution. The code is as follows:
B2FunctionMatrix = Matrix([[fxdot_m], [fa_2], [fMValve], [fPValve], [fq_3c], [fC_vm], [fq_m], [fq_1], [fC_vp], [fq_2], [fmix], [fp1q_m], [fh_L1], [fCV1q_m], [fp2q_m], [fh_L2], [fp3q_m], [fh_L3], [fCV1q_m], [fp4q_m], [fh_L4]]).subs(([rho, guessrho], [g, guessg], [m_m, guessm_m], [m_p, guessm_p], [a_1, guessa_1], [a_p, guessa_p], [k_spr, guessk_spr], [theta, guesstheta], [P_sp, guessP_sp], [C_vfo, guessC_vfo], [C_valpha, guessC_valpha], [C_vbeta, guessC_vbeta], [a_alpha, guessa_alpha], [a_beta, guessa_beta], [h_pump, guessh_pump], [h_atm, guessh_atm], [f, guessf], [L1, guessL1], [L2, guessL2], [L3, guessL3], [L4, guessL4], [D1, guessD1], [D2, guessD2], [D3, guessD3], [D4, guessD4], [A1, guessA1], [A2, guessA2], [A3, guessA3], [A4, guessA4], [P_spscale, guessP_spscale]))
B2VariableMatrix = Matrix([[x_m], [a_2], [q_3c], [h_in], [h_out], [h_c], [q_m], [x_p], [C_vm], [C_vp], [h_t], [q_1], [q_2], [h_L1], [h_L2], [h_L3], [h_L4], [v1], [v2], [v3], [v4]])
print('Block 2 (Multiple Unknown Equations)')
dof2 = abs(B2VariableMatrix.shape[0] - B2FunctionMatrix.shape[0])
if dof2 == 0:
B2guessmatrix = B2VariableMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq3_c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
B2iter_n = 0
B2tolerance = Matrix.ones(B2FunctionMatrix.shape[0], 1) * tolerance
B2guessVariance = Matrix.ones(B2FunctionMatrix.shape[0], 1) * 10
B2JacobianMatrix = B2FunctionMatrix.jacobian(B2VariableMatrix)
while B2guessVariance[0] > B2tolerance[0] or B2guessVariance[1] > B2tolerance[1]:
print('placeholder 1')
B2fsolve = B2FunctionMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq3_c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
print('placeholder 2')
B2jsolve = B2JacobianMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq3_c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
print('placeholder 3')
B2delta_x0, fv = B2jsolve.gauss_jordan_solve(-1*B2fsolve)
print('placeholder 4')
B2guessmatrix = B2VariableMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq3_c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
B2nextguessmatrix = B2delta_x0 + B2guessmatrix
guessx_m = B2nextguessmatrix[0]
guessa_2 = B2nextguessmatrix[1]
guessq_3c = B2nextguessmatrix[2]
guessh_in = B2nextguessmatrix[3]
guessh_out = B2nextguessmatrix[4]
guessh_c = B2nextguessmatrix[5]
guessq_m = B2nextguessmatrix[6]
guessx_p = B2nextguessmatrix[7]
guessC_vm = B2nextguessmatrix[8]
guessC_vp = B2nextguessmatrix[9]
guessh_t = B2nextguessmatrix[10]
guessq_1 = B2nextguessmatrix[11]
guessq_2 = B2nextguessmatrix[12]
guessh_L1 = B2nextguessmatrix[13]
guessh_L2 = B2nextguessmatrix[14]
guessh_L3 = B2nextguessmatrix[15]
guessh_L4 = B2nextguessmatrix[16]
guessv1 = B2nextguessmatrix[17]
guessv2 = B2nextguessmatrix[18]
guessv3 = B2nextguessmatrix[19]
guessv4 = B2nextguessmatrix[20]
B2guessVariance = abs(B2nextguessmatrix - B2guessmatrix)
B2guessmatrix = B2nextguessmatrix
print('placeholder 5')
B2iter_n += 1
print(f'{B2iter_n} iterations have been completed so far. Moving onto the next iteration...')
if B2iter_n >= max_iter:
print('The maximum Number of iterations has been reached')
break
if B2guessVariance[0] <= B2tolerance[0] or B2guessVariance[1] <= B2tolerance[1]:
print('The solution Matrix is')
display(B2nextguessmatrix)
print(f'Which was achieved after {B2iter_n} iterations with a tolerance of {tolerance}.')
print(f'Displayed as integers, the solutions for variable {x_m} and {a_2} converge at {sp.Float(B2nextguessmatrix[0])} and {sp.Float(B2nextguessmatrix[1])} as floats respectively.')
else:
print('The Equation set has no solution or the initial guesses are too far from the solution.')
elif dof2 !=0:
print(f'This system has {B2FunctionMatrix.shape[0]} equations and {B2VariableMatrix.shape[0]} variables which represents a d.o.f value of {dof2} which ≠ 0. Therefore, the system cannot be solved.')
I only get to "placeholder 3" in the code. To be clear, the output I get is:
Block 2 (Multiple Unknown Equations)
placeholder 1
placeholder 2
placeholder 3
Until I receive the following error which has me very confused:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\cache.py in wrapper(*args, **kwargs)
71 try:
---> 72 retval = cfunc(*args, **kwargs)
73 except TypeError:
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\operations.py in __new__(cls, evaluate, _sympify, *args)
84
---> 85 c_part, nc_part, order_symbols = cls.flatten(args)
86 is_commutative = not nc_part
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\mul.py in flatten(cls, seq)
690 # order commutative part canonically
--> 691 _mulsort(c_part)
692
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\mul.py in _mulsort(args)
30 # in-place sorting of args
---> 31 args.sort(key=_args_sortkey)
32
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\basic.py in compare(self, other)
229 else:
--> 230 c = (l > r) - (l < r)
231 if c:
TypeError: unsupported operand type(s) for -: 'StrictGreaterThan' and 'StrictLessThan'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-22-9ad901ca6789> in <module>
20 B2jsolve = B2JacobianMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq3_c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
21 print('placeholder 3')
---> 22 B2delta_x0, fv = B2jsolve.gauss_jordan_solve(-1*B2fsolve)
23 print('placeholder 4')
24 B2guessmatrix = B2VariableMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq3_c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\matrices.py in gauss_jordan_solve(self, B, freevar)
2158
2159 def gauss_jordan_solve(self, B, freevar=False):
-> 2160 return _gauss_jordan_solve(self, B, freevar=freevar)
2161
2162 def pinv_solve(self, B, arbitrary_matrix=None):
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\solvers.py in _gauss_jordan_solve(M, B, freevar)
563
564 # solve by reduced row echelon form
--> 565 A, pivots = aug.rref(simplify=True)
566 A, v = A[:, :-B_cols], A[:, -B_cols:]
567 pivots = list(filter(lambda p: p < col, pivots))
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\matrices.py in rref(self, iszerofunc, simplify, pivots, normalize_last)
169 normalize_last=True):
170 return _rref(self, iszerofunc=iszerofunc, simplify=simplify,
--> 171 pivots=pivots, normalize_last=normalize_last)
172
173 echelon_form.__doc__ = _echelon_form.__doc__
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\reductions.py in _rref(M, iszerofunc, simplify, pivots, normalize_last)
304
305 mat, pivot_cols, _ = _row_reduce(M, iszerofunc, simpfunc,
--> 306 normalize_last, normalize=True, zero_above=True)
307
308 if pivots:
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\reductions.py in _row_reduce(M, iszerofunc, simpfunc, normalize_last, normalize, zero_above)
127 mat, pivot_cols, swaps = _row_reduce_list(list(M), M.rows, M.cols, M.one,
128 iszerofunc, simpfunc, normalize_last=normalize_last,
--> 129 normalize=normalize, zero_above=zero_above)
130
131 return M._new(M.rows, M.cols, mat), pivot_cols, swaps
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\reductions.py in _row_reduce_list(mat, rows, cols, one, iszerofunc, simpfunc, normalize_last, normalize, zero_above)
67 pivot_offset, pivot_val, \
68 assumed_nonzero, newly_determined = _find_reasonable_pivot(
---> 69 get_col(piv_col)[piv_row:], iszerofunc, simpfunc)
70
71 # _find_reasonable_pivot may have simplified some things
D:\ProgramData\Anaconda3\lib\site-packages\sympy\matrices\determinant.py in _find_reasonable_pivot(col, iszerofunc, simpfunc)
80 if possible_zeros[i] is not None:
81 continue
---> 82 simped = simpfunc(x)
83 is_zero = iszerofunc(simped)
84 if is_zero == True or is_zero == False:
D:\ProgramData\Anaconda3\lib\site-packages\sympy\simplify\simplify.py in simplify(expr, ratio, measure, rational, inverse, doit, **kwargs)
658 if expr.has(Piecewise):
659 # Fold into a single Piecewise
--> 660 expr = piecewise_fold(expr)
661 # Apply doit, if doit=True
662 expr = done(expr)
D:\ProgramData\Anaconda3\lib\site-packages\sympy\functions\elementary\piecewise.py in piecewise_fold(expr)
1127 args = expr.args
1128 # fold
-> 1129 folded = list(map(piecewise_fold, args))
1130 for ec in cartes(*[
1131 (i.args if isinstance(i, Piecewise) else
D:\ProgramData\Anaconda3\lib\site-packages\sympy\functions\elementary\piecewise.py in piecewise_fold(expr)
1132 [(i, true)]) for i in folded]):
1133 e, c = zip(*ec)
-> 1134 new_args.append((expr.func(*e), And(*c)))
1135
1136 return Piecewise(*new_args)
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\cache.py in wrapper(*args, **kwargs)
72 retval = cfunc(*args, **kwargs)
73 except TypeError:
---> 74 retval = func(*args, **kwargs)
75 return retval
76
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\operations.py in __new__(cls, evaluate, _sympify, *args)
83 return args[0]
84
---> 85 c_part, nc_part, order_symbols = cls.flatten(args)
86 is_commutative = not nc_part
87 obj = cls._from_args(c_part + nc_part, is_commutative)
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\mul.py in flatten(cls, seq)
689
690 # order commutative part canonically
--> 691 _mulsort(c_part)
692
693 # current code expects coeff to be always in slot-0
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\mul.py in _mulsort(args)
29 def _mulsort(args):
30 # in-place sorting of args
---> 31 args.sort(key=_args_sortkey)
32
33
D:\ProgramData\Anaconda3\lib\site-packages\sympy\core\basic.py in compare(self, other)
228 c = l.compare(r)
229 else:
--> 230 c = (l > r) - (l < r)
231 if c:
232 return c
TypeError: unsupported operand type(s) for -: 'StrictGreaterThan' and 'StrictLessThan'
The other 3 blocks solve completely fine. I was thinking that maybe it has something to do with the big size of the sparse Jacobian matrix in the block ("B2Jsolve" which is a sparse 21 x 21 square matrix)? Any ideas would be very appreciated! Thank you for reading :)
The output for B2fsolve is as follows:
B2fsolve
The output for B2Jsolve is as follows:
B2jsolve (This is a merged jpg file only with good resolution when you zoom in to make it clearer!)
EDIT (Adding source code): Here is the simplified version of the code which I've added ##headings to so that it's a bit clearer. The solving block is at the bottom of the code. I think that the problems may be to do with taking the Jacobian (B2jsolve) of some equations which use Sympy's sign function (original equations on lines 33, 34, 36, 47 & 55).
## 1.0 CODE SETTINGS, LIBRARIES & PHYSICAL CONSTANTS ##
from sympy.interactive import printing
printing.init_printing(use_latex = True)
import numpy as np
import sympy as sp
import math
import functools
from sympy import Matrix
from sympy.functions import sign
rho, g = sp.symbols('rho g')
cos = sp.cos
e = sp.exp
pi = np.pi
sqrt = sp.sqrt
## 2.0 PRV Matrix Constants ##
m_m, m_p, a_1, a_p, k_spr, theta = sp.symbols('m_m m_p a_1 a_p k_spr theta')
## 3.0 PRV FORCE BALANCE RELATIONSHIPS ##
xdot_m, a_2, q_3c, x_m, x_m0, t_0, t_f, t, P_sp = sp.symbols('xdot_m a_2 q_3c x_m x_m0 t_0 t_f t P_sp')
fa_2 = Matrix([a_2 - (1/(3700*(0.02732 - x_m)))])
fxdot_m = Matrix([0 - 3700*(0.02732 - x_m) * q_3c])
## 4.0 MAIN VALVE & PILOT VALVE FORCE BALANCES (m_m d^2m_m/dt^2) ##
h_in, h_out, h_c, q_m, x_p = sp.symbols('h_in h_out h_c q_m x_p')
fMValve = Matrix([rho * g * (h_in * a_1 + h_out * (a_2 - a_1) - h_c * a_2) - m_m * g * cos(theta) + (rho * ((q_m)**2)/a_1) - 0])
fPValve = Matrix([k_spr * (P_sp - x_p) - rho * g * h_out * a_p + m_p * g - 0])
## 5.0 FLOW EQUATIONS ##
C_vm, C_vfo, C_vp, C_vnvout, C_vnvin, h_t, q_1, q_2 = sp.symbols('C_vm C_vfo C_vp C_vnvout C_vnvin h_t q_1 q_2')
fC_vm = Matrix([0.02107 - 0.02962 * e(-51.1322 * x_m) + 0.0109 * e(-261 * x_m) - 0.00325 * e(-683.17 * x_m) + 0.0009 * e(-399.5 * x_m) - C_vm])
fq_m = Matrix([C_vm * (sqrt((abs(h_in - h_out)))) * sign(h_in - h_out) - q_m])
fq_1 = Matrix([C_vfo * (sqrt(abs(h_in - h_t))) * sign(h_in - h_t) - q_1])
fC_vp = Matrix([0.0000753 * (1 - e(-1135 * x_p)) - C_vp])
fq_2 = Matrix([C_vp * (sqrt(abs(h_t - h_out))) * sign(h_t - h_out) - q_2])
fmix = Matrix([q_1 + q_3c - q_2])
fq_3c = Matrix([h_c - h_t])
## 6.0 NETWORK EQUATIONS FROM PUMP TO ATMOSPHERIC OUTLET ##
C_valpha, C_vbeta, a_alpha, a_beta, h_pump, h_L1, h_L2, h_L3, h_L4, h_atm, f, L1, L2, L3, L4, D1, D2, D3, D4, A1, A2, A3, A4, v1, v2, v3, v4 = sp.symbols('C_valpha C_vbeta a_alpha a_beta h_pump h_L1 h_L2 h_L3 h_L4 h_atm f L1 L2 L3 L4 D1 D2 D3 D4 A1 A2 A3 A4 v1 v2 v3 v4')
#Pipe1
fp1q_m = Matrix([q_m - (A1 * v1)])
fh_L1 = Matrix([h_L1 - (f*(L1/D1)*((v1**2)/(2*g)))])
#AlphaValve
fCV1q_m = Matrix([C_valpha * a_alpha * (sqrt(abs(h_pump - h_L1 - (h_in + h_L2)))) * sign(h_pump - h_L1 - (h_in + h_L2)) - q_m])
#Pipe2
fp2q_m = Matrix([q_m - (A2 * v2)])
fh_L2 = Matrix([h_L2 - (f*(L2/D2)*((v2**2)/(2*g)))])
#Pipe3
fp3q_m = Matrix([q_m - A3 * v3])
fh_L3 = Matrix([h_L3 - (f*(L3/D3)*((v3**2)/(2*g)))])
#BetaValve
fCV2q_m = Matrix([C_vbeta * a_beta * (sqrt(abs(h_out - h_L3 - (h_atm + h_L4)))) * sign(h_out - h_L3 - (h_atm + h_L4)) - q_m])
#Pipe4
fp4q_m = Matrix([q_m - (A4 * v4)])
fh_L4 = Matrix([h_L4 - (f*(L4/D4)*((v4**2)/(2*g)))])
q_mscale, q_1scale, q_2scale, q_3cscale, x_mscale, x_pscale, P_spscale, a_2scale = sp.symbols('q_mscale q_1scale q_2scale q_3cscale x_mscale x_pscale P_spscale a_2scale')
## 7.1 INITIAL GUESSES & SOLVER CONDITIONS ##
guessrho, guessg, guessm_m, guessm_p, guessa_1, guessa_p, guessk_spr, guesstheta, guessP_sp, guessx_m, guessa_2, guessq_3c, guessh_in, guessh_out, guessh_c, guessq_m, guessx_p, guessC_vm, guessC_vfo, guessC_vp, guessh_t, guessq_1, guessq_2, guessC_valpha, guessC_vbeta, guessa_alpha, guessa_beta, guessh_pump, guessh_L1, guessh_L2, guessh_L3, guessh_L4, guessh_atm, guessf, guessL1, guessL2, guessL3, guessL4, guessD1, guessD2, guessD3, guessD4, guessA1, guessA2, guessA3, guessA4, guessv1, guessv2, guessv3, guessv4, guessq_mscale, guessq_1scale, guessq_2scale, guessq_3cscale, guessx_mscale, guessx_pscale, guessP_spscale, guessa_2scale = sp.symbols('guessrho guessg guessm_m guessm_p guessa_1 guessa_p guessk_spr guesstheta guessP_sp guessx_m guessa_2 guessq_3c guessh_in guessh_out guessh_c guessq_m guessx_p guessC_vm guessC_vfo guessC_vp guessh_t guessq_1 guessq_2 guessC_valpha guessC_vbeta guessa_alpha guessa_beta guessh_pump guessh_L1 guessh_L2 guessh_L3 guessh_L4 guessh_atm guessf guessL1 guessL2 guessL3 guessL4 guessD1 guessD2 guessD3 guessD4 guessA1 guessA2 guessA3 guessA4 guessv1 guessv2 guessv3 guessv4 guessq_mscale guessq_1scale guessq_2scale guessq_3cscale guessx_mscale guessx_pscale guessP_spscale guessa_2scale')
guessrho = 997 # physical constant
guessg = 9.81 # physical constant
guessm_m = 8 # physical constant
guessm_p = 0.1 # physical constant
guessa_1 = 0.0078 # physical constant
guessa_p = 0.00196 # physical constant
guessk_spr = 70000 # physical constant
guesstheta = 0 #0.000000000000000000000001 # physical constant 31/5/21 CHANGED SO IT'S NON-ZERO
guessP_sp = 0.00700000000
guessx_m = 0.013647064
guessa_2 = 0.01977
guessq_3c = -2.578E-24 #0.000000000000000000000001 # physical constant 31/5/21 CHANGED SO IT'S NON-ZERO
guessh_in = 41.38
guessh_out = 23.465726
guessh_c = 30.65
guessq_m = 0.02811
guessx_p = 0.0005878
guessC_vm = 0.006642
guessC_vfo = 0.00003
guessC_vp = 0.00003666
guessh_t = 30.65
guessq_1 = 0.00009826
guessq_2 = 0.00009826
guessC_valpha = 1
guessC_vbeta = 0.6
guessa_alpha = 0.01
guessa_beta = 0.01
guessh_pump = 50
guessh_L1 = 0.1959
guessh_L2 = 0.5224
guessh_L3 = 0.6529
guessh_L4 = 0.8619
guessh_atm = 0 #0.000000000000000000000001 # 31/5/21 CHANGED SO IT'S NON-ZERO
guessf = 0.02
guessL1 = 1.5
guessL2 = 4
guessL3 = 5
guessL4 = 6.6
guessD1 = 0.1
guessD2 = 0.1
guessD3 = 0.1
guessD4 = 0.1
guessA1 = 0.007854
guessA2 = 0.007854
guessA3 = 0.007854
guessA4 = 0.007854
guessv1 = 3.579
guessv2 = 3.579
guessv3 = 3.579
guessv4 = 3.579
guessq_mscale = 101.2 #q_m * 3600
guessq_1scale = 0.3537 #q_1 * 3600
guessq_2scale = 0.3537 #q_2 * 3600
guessq_3cscale = -9.281E-21 #q_3c * 36000
guessx_mscale = 13.6470637 #x_m * 1000
guessx_pscale = 0.5878 #x_p * 1000
guessP_spscale = 7 #P_sp * 1000
guessa_2scale = 197.7 #a_2 * (100^2)
#Broad Loop Solver Conditions
tolerance = 0.05
max_iter = 10
#!-!#! 7.2.2 BLOCK 2 (MULTIPLE UNKNOWN EQUATIONS) (THE PROBLEMS ARE WITH THIS BLOCK!!!!) #!-!#!-!#!-
print('*PROBLEM* SECTION 7.2.2 BLOCK 2 (MULTIPLE UNKNOWN EQUATIONS)')
B2FunctionMatrix = Matrix([[fxdot_m], [fa_2], [fMValve], [fPValve], [fq_3c], [fC_vm], [fq_m], [fq_1], [fC_vp], [fq_2], [fmix], [fp1q_m], [fh_L1], [fCV1q_m], [fp2q_m], [fh_L2], [fp3q_m], [fh_L3], [fCV1q_m], [fp4q_m], [fh_L4]]).subs(([rho, guessrho], [g, guessg], [m_m, guessm_m], [m_p, guessm_p], [a_1, guessa_1], [a_p, guessa_p], [k_spr, guessk_spr], [theta, guesstheta], [P_sp, guessP_sp], [C_vfo, guessC_vfo], [C_valpha, guessC_valpha], [C_vbeta, guessC_vbeta], [a_alpha, guessa_alpha], [a_beta, guessa_beta], [h_pump, guessh_pump], [h_atm, guessh_atm], [f, guessf], [L1, guessL1], [L2, guessL2], [L3, guessL3], [L4, guessL4], [D1, guessD1], [D2, guessD2], [D3, guessD3], [D4, guessD4], [A1, guessA1], [A2, guessA2], [A3, guessA3], [A4, guessA4], [P_spscale, guessP_spscale]))
B2VariableMatrix = Matrix([[x_m], [a_2], [q_3c], [h_in], [h_out], [h_c], [q_m], [x_p], [C_vm], [C_vp], [h_t], [q_1], [q_2], [h_L1], [h_L2], [h_L3], [h_L4], [v1], [v2], [v3], [v4]])
print('Block 2 (Multiple Unknown Equations)')
dof2 = abs(B2VariableMatrix.shape[0] - B2FunctionMatrix.shape[0])
if dof2 == 0:
B2guessmatrix = B2VariableMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq_3c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
#display(B2guessmatrix)
#display(B2FunctionMatrix)
B2iter_n = 0
B2tolerance = Matrix.ones(B2FunctionMatrix.shape[0], 1) * tolerance
B2guessVariance = Matrix.ones(B2FunctionMatrix.shape[0], 1) * 10
B2JacobianMatrix = B2FunctionMatrix.jacobian(B2VariableMatrix)
#display(B2JacobianMatrix)
while (B2guessVariance[0,0] >= B2tolerance[0,0]) and (B2guessVariance[1,0] >= B2tolerance[1,0]) and (B2guessVariance[2,0] >= B2tolerance[2,0]) and (B2guessVariance[3,0] >= B2tolerance[3,0]) and (B2guessVariance[4,0] >= B2tolerance[4,0]) and (B2guessVariance[5,0] >= B2tolerance[5,0]) and (B2guessVariance[6,0] >= B2tolerance[6,0]) and (B2guessVariance[7,0] >= B2tolerance[7,0]) and (B2guessVariance[8,0] >= B2tolerance[8,0]) and (B2guessVariance[9,0] >= B2tolerance[9,0]) and (B2guessVariance[10,0] >= B2tolerance[10,0]) and (B2guessVariance[11,0] >= B2tolerance[11,0]) and (B2guessVariance[12,0] >= B2tolerance[12,0]) and (B2guessVariance[13,0] >= B2tolerance[13,0]) and (B2guessVariance[14,0] >= B2tolerance[14,0]) and (B2guessVariance[15,0] >= B2tolerance[15,0]) and (B2guessVariance[16,0] >= B2tolerance[16,0]) and (B2guessVariance[17,0] >= B2tolerance[17,0]) and (B2guessVariance[18,0] >= B2tolerance[18,0]) and (B2guessVariance[19,0] >= B2tolerance[19,0]) and (B2guessVariance[20,0] >= B2tolerance[20,0]):
print('placeholder 1 (displays B2fsolve, the function matrix with guess values substituted)')
B2fsolve = B2FunctionMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq_3c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
print(repr(B2fsolve))
#display(B2fsolve)
print('placeholder 2 (displays B2jsolve, the jacobian matrix with guess values substituted)')
B2jsolve = B2JacobianMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq_3c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
print(repr(B2jsolve))
#display(B2jsolve)
print('placeholder 3')
B2delta_x0, fv = B2jsolve.gauss_jordan_solve(-1*B2fsolve)
#The code does not run past this point, please help!
print(repr(B2delta_x0))
#display(B2delta_x0)
print('placeholder 4')
B2guessmatrix = B2VariableMatrix.subs(([x_m, guessx_m], [a_2, guessa_2], [q_3c, guessq_3c], [h_in, guessh_in], [h_out, guessh_out], [h_c, guessh_c], [q_m, guessq_m], [x_p, guessx_p], [C_vm, guessC_vm], [C_vp, guessC_vp], [h_t, guessh_t], [q_1, guessq_1], [q_2, guessq_2], [h_L1, guessh_L1], [h_L2, guessh_L2], [h_L3, guessh_L3], [h_L4, guessh_L4], [v1, guessv1], [v2, guessv2], [v3, guessv3], [v4, guessv4]))
B2nextguessmatrix = B2delta_x0 + B2guessmatrix
guessx_m = B2nextguessmatrix[0]
guessa_2 = B2nextguessmatrix[1]
guessq_3c = B2nextguessmatrix[2]
guessh_in = B2nextguessmatrix[3]
guessh_out = B2nextguessmatrix[4]
guessh_c = B2nextguessmatrix[5]
guessq_m = B2nextguessmatrix[6]
guessx_p = B2nextguessmatrix[7]
guessC_vm = B2nextguessmatrix[8]
guessC_vp = B2nextguessmatrix[9]
guessh_t = B2nextguessmatrix[10]
guessq_1 = B2nextguessmatrix[11]
guessq_2 = B2nextguessmatrix[12]
guessh_L1 = B2nextguessmatrix[13]
guessh_L2 = B2nextguessmatrix[14]
guessh_L3 = B2nextguessmatrix[15]
guessh_L4 = B2nextguessmatrix[16]
guessv1 = B2nextguessmatrix[17]
guessv2 = B2nextguessmatrix[18]
guessv3 = B2nextguessmatrix[19]
guessv4 = B2nextguessmatrix[20]
B2guessVariance = abs(B2nextguessmatrix - B2guessmatrix)
B2guessmatrix = B2nextguessmatrix
print('placeholder 5')
B2iter_n += 1
print(f'{B2iter_n} iterations have been completed so far. Moving onto the next iteration...')
if B2iter_n >= max_iter:
print('The maximum Number of iterations has been reached')
break
if (B2guessVariance[0,0] < B2tolerance[0,0]) and (B2guessVariance[1,0] < B2tolerance[1,0]) and (B2guessVariance[2,0] < B2tolerance[2,0]) and (B2guessVariance[3,0] < B2tolerance[3,0]) and (B2guessVariance[4,0] < B2tolerance[4,0]) and (B2guessVariance[5,0] < B2tolerance[5,0]) and (B2guessVariance[6,0] < B2tolerance[6,0]) and (B2guessVariance[7,0] < B2tolerance[7,0]) and (B2guessVariance[8,0] < B2tolerance[8,0]) and (B2guessVariance[9,0] < B2tolerance[9,0]) and (B2guessVariance[10,0] < B2tolerance[10,0]) and (B2guessVariance[11,0] < B2tolerance[11,0]) and (B2guessVariance[12,0] < B2tolerance[12,0]) and (B2guessVariance[13,0] < B2tolerance[13,0]) and (B2guessVariance[14,0] < B2tolerance[14,0]) and (B2guessVariance[15,0] < B2tolerance[15,0]) and (B2guessVariance[16,0] < B2tolerance[16,0]) and (B2guessVariance[17,0] < B2tolerance[17,0]) and (B2guessVariance[18,0] < B2tolerance[18,0]) and (B2guessVariance[19,0] < B2tolerance[19,0]) and (B2guessVariance[20,0] < B2tolerance[20,0]):
print('The solution Matrix is')
print(repr(B2nextguessmatrix))
#display(B2nextguessmatrix)
print(f'Which was achieved after {B2iter_n} iterations with a tolerance of {tolerance}.')
print(f'Displayed as integers, the solutions for variable {x_m} and {a_2} converge at {sp.Float(B2nextguessmatrix[0])} and {sp.Float(B2nextguessmatrix[1])} as floats respectively.')
else:
print('The Equation set has no solution or the initial guesses are too far from the solution.')
elif dof2 !=0:
print(f'This system has {B2FunctionMatrix.shape[0]} equations and {B2VariableMatrix.shape[0]} variables which represents a d.o.f value of {dof2} which ≠ 0. Therefore, the system cannot be solved.')

Print new line each array index in a loop reach certain number

I have this array (as an example of my problem) :
[0.8067, 0.7152, 0.4551, 0.7519, 0.3419, 0.7161, 0.3793, 0.6859, 0.4205, 0.5129, 0.5534, 0.5995, 0.4999, 0.5136, 0.8194, 0.4855, 0.6822, 0.4924, 0.5359, 0.4083, 0.5078, 0.7260, 0.6876, 0.7033, 0.5777, 0.4515, 0.5460,0.5842, 0.7296, 0.7570, 0.4579, 0.3252, 0.4683, 0.3646, 0.3220, 0.4150,0.3263, 0.6402, 0.3184, 0.7087, 0.2958, 0.5384, 0.5462, 0.3933, 0.2963,0.6883, 0.4766, 0.5430, 0.4943, 0.2810, 0.4785, 0.5618, 0.6941, 0.4943,0.3793, 0.7629, 0.6058, 0.6419, 0.4902, 0.3158, 0.7923, 0.7335, 0.5624,0.5390, 0.5337, 0.8333, 0.7519, 0.6591, 0.5301, 0.3020, 0.8187, 0.8084,0.3412, 0.7912, 0.6240, 0.4296, 0.4908, 0.6560, 0.7366, 0.5219, 0.8128,0.3683, 0.6037, 0.4570, 0.3640, 0.4717, 0.5948, 0.6294, 0.8222, 0.7323,0.4344, 0.4371, 0.5013, 0.2913, 0.4335, 0.4046, 0.6788, 0.5917, 0.8369,0.8983, 0.3981, 0.8857, 0.4309, 0.6197, 0.7020, 0.3666, 0.5837, 0.3259,0.7193, 0.3719, 0.7098, 0.4088, 0.5421, 0.5039, 0.3664, 0.5499, 0.8648, 0.3217, 0.7696, 0.5970, 0.4611, 0.3465, 0.6396, 0.6688, 0.5773, 0.7444,0.7232, 0.5695, 0.5801, 0.5218, 0.8099, 0.6983, 0.5733, 0.3286, 0.6736, 0.6470, 0.9196, 0.7589, 0.7610, 0.8454, 0.6261, 0.6229, 0.7600, 0.5022,0.3035, 0.5229, 0.5353, 0.4962, 0.8466, 0.1817, 0.5271, 0.6928, 0.7898,0.4182, 0.5234, 0.4112, 0.4812, 0.7522, 0.4209, 0.7217, 0.6545, 0.6954,0.3139, 0.5253, 0.5467, 0.3606, 0.6640, 0.7399, 0.7965, 0.5742, 0.5729,0.6213, 0.7981, 0.5613, 0.4904, 0.7292, 0.5686, 0.8421, 0.7316, 0.6408,0.6550, 0.3902, 0.5353, 0.5459, 0.4035, 0.3390, 0.4407, 0.7370, 0.4466,0.4029, 0.8216, 0.4862, 0.7136, 0.3544, 0.7967, 0.2909, 0.4384, 0.5505,0.6768, 0.5122, 0.6042, 0.5240, 0.4299, 0.3714, 0.6224, 0.6549, 0.7901,0.7289, 0.7580, 0.5656, 0.7841, 0.7520, 0.8379, 0.4449, 0.4860, 0.6904,0.7279, 0.6378, 0.4493, 0.5407, 0.6737, 0.5260, 0.8009, 0.6307, 0.6026,0.5197, 0.7532, 0.4754, 0.6674, 0.6768, 0.7643, 0.6705, 0.7871, 0.6135, 0.7762, 0.6081, 0.4060, 0.3688, 0.5848, 0.4235, 0.6011, 0.6949, 0.4410,0.8054, 0.7706, 0.3644, 0.6820, 0.6351, 0.4282, 0.4613, 0.7392, 0.5208,0.4409, 0.5589, 0.3401, 0.5811, 0.7109, 0.3245, 0.5017, 0.6650, 0.5949,0.5680, 0.4445, 0.7482, 0.3044, 0.7760, 0.4396, 0.4067, 0.3840, 0.7426,0.5989, 0.5169, 0.7056, 0.4329, 0.5555]
What i want to do is each time the index value of the array reach 9, it will print a new line before it continues printing the next array value.
So what i expect it will print the array like this :
[0.8067, 0.7152, 0.4551, 0.7519, 0.3419, 0.7161, 0.3793, 0.6859, 0.4205,
0.5129, 0.5534, 0.5995, 0.4999, 0.5136, 0.8194, 0.4855, 0.6822, 0.4924,
0.5359, 0.4083, 0.5078, 0.7260, 0.6876, 0.7033, 0.5777, 0.4515, 0.5460,
0.5842, 0.7296, 0.7570, 0.4579, 0.3252, 0.4683, 0.3646, 0.3220, 0.4150,
0.3263, 0.6402, 0.3184, 0.7087, 0.2958, 0.5384, 0.5462, 0.3933, 0.2963,
0.6883, 0.4766, 0.5430, 0.4943, 0.2810, 0.4785, 0.5618, 0.6941, 0.4943,
0.3793, 0.7629, 0.6058, 0.6419, 0.4902, 0.3158, 0.7923, 0.7335, 0.5624,
0.5390, 0.5337, 0.8333, 0.7519, 0.6591, 0.5301, 0.3020, 0.8187, 0.8084,
0.3412, 0.7912, 0.6240, 0.4296, 0.4908, 0.6560, 0.7366, 0.5219, 0.8128,
0.3683, 0.6037, 0.4570, 0.3640, 0.4717, 0.5948, 0.6294, 0.8222, 0.7323,
0.4344, 0.4371, 0.5013, 0.2913, 0.4335, 0.4046, 0.6788, 0.5917, 0.8369,
0.8983, 0.3981, 0.8857, 0.4309, 0.6197, 0.7020, 0.3666, 0.5837, 0.3259,
0.7193, 0.3719, 0.7098, 0.4088, 0.5421, 0.5039, 0.3664, 0.5499, 0.8648,
0.3217, 0.7696, 0.5970, 0.4611, 0.3465, 0.6396, 0.6688, 0.5773, 0.7444,
0.7232, 0.5695, 0.5801, 0.5218, 0.8099, 0.6983, 0.5733, 0.3286, 0.6736,
0.6470, 0.9196, 0.7589, 0.7610, 0.8454, 0.6261, 0.6229, 0.7600, 0.5022,
0.3035, 0.5229, 0.5353, 0.4962, 0.8466, 0.1817, 0.5271, 0.6928, 0.7898,
0.4182, 0.5234, 0.4112, 0.4812, 0.7522, 0.4209, 0.7217, 0.6545, 0.6954,
0.3139, 0.5253, 0.5467, 0.3606, 0.6640, 0.7399, 0.7965, 0.5742, 0.5729,
0.6213, 0.7981, 0.5613, 0.4904, 0.7292, 0.5686, 0.8421, 0.7316, 0.6408,
0.6550, 0.3902, 0.5353, 0.5459, 0.4035, 0.3390, 0.4407, 0.7370, 0.4466,
0.4029, 0.8216, 0.4862, 0.7136 ]
How can i achieve this? Thank you in advance.
Try this
for i in range(len(arr)):
if i%9 == 0:
print()
print(arr[i], end = " ")

Convert log file into json file using python

I am new to python. I am trying to convert the log file in to json file using python script. I created a main file and a del6 file. Here, it will convert the log file and write into a new json file. On execution, it shows me the following error.
Traceback (most recent call last):
File "main.py", line 23, in <module>
main()
File "main.py", line 14, in main
print toJson(sys.argv[2])
File "/home/paulsteven/BEAT/apache/del6.py", line 46, in toJson
entries = readfile(file)
File "/home/paulsteven/BEAT/apache/del6.py", line 21, in readfile
filecontent[index] = line2dict(line)
File "/home/paulsteven/BEAT/apache/del6.py", line 39, in line2dict
res = m.groupdict()
AttributeError: 'NoneType' object has no attribute 'groupdict'
I tried this link log to json
But it doesn't give me proper solution.
Is there any way to solve this.
Here is my sample log file:
February 14 2019, 15:38:47 172.217.160.132 www.google.com up tcp-tcp# www.google.com 172.217.160.132
February 14 2019, 15:38:47 104.28.4.86 www.smackcoders.com up tcp-tcp# www.smackcoders.com 104.28.4.86
The output should be like:
{"1": {"timestamp": "February 14 2019, 15:38:47", "monitorip": "172.217.160.132 ", "monitorhost": "www.google.com", "monitorstatus": "up", "monitorid": "tcp-tcp# www.google.com", "resolveip": "172.217.160.132"}, "2": {"timestamp": "February 14 2019, 15:38:47", "monitorip": "104.28.4.86", "monitorhost": "www.smackcoders.com", "monitorstatus": "up", "monitorid": "tcp-tcp# www.smackcoders.com", "resolveip": "104.28.4.86"}
Here is main python code:
import sys
from del6 import *
def main():
if len(sys.argv) < 3:
print "Incorrect Syntax. Usage: python main.py -f <filename>"
sys.exit(2)
elif sys.argv[1] != "-f":
print "Invalid switch '"+sys.argv[1]+"'"
sys.exit(2)
elif os.path.isfile(sys.argv[2]) == False:
print "File does not exist"
sys.exit(2)
print toJson(sys.argv[2])
text_file = open("tcp.json", "a+")
text_file.write(toJson(sys.argv[2]))
text_file.write("\n")
text_file.close()
if __name__ == "__main__":
main()
Here's my del6 code:
import fileinput
import re
import os
try: import simplejson as json
except ImportError: import json
#read input file and return entries' Dict Object
def readfile(file):
filecontent = {}
index = 0
#check necessary file size checking
statinfo = os.stat(file)
#just a guestimate. I believe a single entry contains atleast 150 chars
if statinfo.st_size < 150:
print "Not a valid access_log file. It does not have enough data"
else:
for line in fileinput.input(file):
index = index+1
if line != "\n": #don't read newlines
filecontent[index] = line2dict(line)
fileinput.close()
return filecontent
#gets a line of string from Log and convert it into Dict Object
def line2dict(line):
#Snippet, thanks to http://www.seehuhn.de/blog/52
parts = [
r'(?P<timestamp>\S+)',
r'(?P<monitorip>\S+)',
r'(?P<monitorhost>\S+)',
r'(?P<monitorstatus>\S+)',
r'"(?P<monitorid>\S+)"',
r'(?P<resolveip>\S+)',
]
pattern = re.compile(r'\s+'.join(parts)+r'\s*\Z')
m = pattern.match(line)
res = m.groupdict()
return res
#to get jSon of entire Log
#returns JSON object
def toJson(file):
#get dict object for each entry
entries = readfile(file)
return json.JSONEncoder().encode(entries)
I see that columns divided by double tab. So based on that:
i = 1
result = {}
with open('log.txt') as f:
lines = f.readlines()
for line in lines:
r = line.split('\t\t')
result[i] = {'timestamp': r[0], 'monitorip': r[1], 'monitorhost': r[2], 'monitorstatus': r[3], 'monitorid': r[4], 'resolveip': r[5]}
i += 1
Output:
{1: {'timestamp': 'February 14 2019, 15:38:47', 'monitorip': '172.217.160.132', 'monitorhost': 'www.google.com', 'monitorstatus': 'up', 'monitorid': 'tcp-tcp# www.google.com', 'resolveip': '172.217.160.132\n'}, 2: {'timestamp': 'February 14 2019, 15:38:47', 'monitorip': '104.28.4.86', 'monitorhost': 'www.smackcoders.com', 'monitorstatus': 'up', 'monitorid': 'tcp-tcp# www.smackcoders.com', 'resolveip': '104.28.4.86'}}
Or if you want to have list of dicts, which is more natural, then:
result = []
with open('log.txt') as f:
lines = f.readlines()
for line in lines:
r = line.split('\t\t')
result.append({'timestamp': r[0], 'monitorip': r[1], 'monitorhost': r[2], 'monitorstatus': r[3], 'monitorid': r[4], 'resolveip': r[5]})
Output:
[{'timestamp': 'February 14 2019, 15:38:47', 'monitorip': '172.217.160.132', 'monitorhost': 'www.google.com', 'monitorstatus': 'up', 'monitorid': 'tcp-tcp# www.google.com', 'resolveip': '172.217.160.132\n'}, {'timestamp': 'February 14 2019, 15:38:47', 'monitorip': '104.28.4.86', 'monitorhost': 'www.smackcoders.com', 'monitorstatus': 'up', 'monitorid': 'tcp-tcp# www.smackcoders.com', 'resolveip': '104.28.4.86'}]
Thanks for the answer.
To save it in a JSON file:
import json
i = 1
result = {}
with open('tcp.log') as f:
lines = f.readlines()
for line in lines:
r = line.split('\t\t')
result[i] = {'timestamp': r[0], 'monitorip': r[1], 'monitorhost': r[2], 'monitorstatus': r[3], 'monitorid': r[4], 'resolveip': r[5]}
i += 1
print(result)
with open('data.json', 'w') as fp:
json.dump(result, fp)
Below is a generic approach to the problem.The function 'log_lines_to_json' will handle any text file where the fields are separated by 'field_delimiter' and the field names are 'field_names'
FIELD_NAMES = ['timestamp', 'monitorip', 'monitorhost', 'monitorstatus', 'monitorid', 'resolveip']
FIELD_DELIMITER = '\t\t'
def log_lines_to_json(log_file, field_names, field_delimiter):
result = []
with open(log_file) as f:
lines = f.readlines()
for line in lines:
fields = line.split(field_delimiter)
result.append({field_name: fields[idx] for idx, field_name in enumerate(field_names)})
return result
entries = log_lines_to_json('log.txt', FIELD_NAMES, FIELD_DELIMITER)
for entry in entries:
print(entry)
Output:
{'monitorid': 'tcp-tcp# www.google.com', 'monitorstatus': 'up', 'timestamp': 'February 14 2019, 15:38:47', 'monitorhost': 'www.google.com', 'monitorip': '172.217.160.132', 'resolveip': '172.217.160.132\n'}
{'monitorid': 'tcp-tcp# www.smackcoders.com', 'monitorstatus': 'up', 'timestamp': 'February 14 2019, 15:38:47', 'monitorhost': 'www.smackcoders.com', 'monitorip': '104.28.4.86', 'resolveip': '104.28.4.86'}

Python NtQueryDirectoryFile (File information structure)

I've written a simple (test) script to list files in a selected directory. Not using FindFirstFile;
only native API.
When I execute the script and watch, Win32API monitor tells me STATUS_SUCCESS.
My File Information buffer is c_buffer(1024), not using a Unicode buffer to see the raw data.
So after call NtQueryDirectoryFile all is ok.
When I write c_buffer in raw mode to console to see the files in the directory, the output is not structured.
I created a FILE_DIRECTORY_INFORMATION structure but either it does not work Windows 7 X86 or there's a problem in my code.
My Question:
Please tell me which FILE_DIRECTORY_INFORMATION structure use on Windows 7 X86 or any variants
from ctypes import *
hFile = windll.kernel32.CreateFileW("C:\\a",0x80000000,0,0,3,0x02000000,0)
class Info(Union):
_fields_ = [('STATUS',c_long),
('Pointer',c_ulong),]
class io_stat(Structure):
_fields_ = [('Stat',Info),
('Information',c_ulong),]
class FILE_OBJECT(Structure):
_fields_ = [('Next',c_ulong),
('FileIndex',c_ulong),
('ctime',c_longlong),
('lat',c_longlong),
('wtime',c_longlong),
('ch',c_longlong),
('Endogfile',c_longlong),
('allo',c_longlong),
('Fileattr',c_ulong),
('Filenalen',c_ulong),
('Filename',c_wchar * 2),]
b = io_stat()
a = c_buffer(1024)
windll.ntdll.NtQueryDirectoryFile(hFile,0,0,0,byref(b),byref(a),sizeof(a), 1,0,None,0)
print(a.raw)
Not optimized.
NtQueryDirectoryFile should be called in a loop until it returns STATUS_NO_MORE_FILES. If either the returned status is STATUS_BUFFER_OVERFLOW or the status is successful (non-negative) with the status block Information as 0, then double the buffer size and try again. For each successful pass, copy the FILE_DIRECTORY_INFORMATION records out of the buffer. Each record has to be sized to include the FileName. You've reached the end when the Next field is 0.
The following example subclasses FILE_DIRECTORY_INFORMATION as a DirEntry class that has a listbuf class method to list the records in a queried buffer. It skips the "." and ".." entries. It uses this class in an ntlistdir function that lists the DirEntry records for a given directory via NtQueryDirectoryFile. It supports passing an open file descriptor as the path argument, which is like how os.listdir works on POSIX systems.
ctypes definitions
import os
import msvcrt
import ctypes
from ctypes import wintypes
ntdll = ctypes.WinDLL('ntdll')
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
def NtError(status):
err = ntdll.RtlNtStatusToDosError(status)
return ctypes.WinError(err)
NTSTATUS = wintypes.LONG
STATUS_BUFFER_OVERFLOW = NTSTATUS(0x80000005).value
STATUS_NO_MORE_FILES = NTSTATUS(0x80000006).value
STATUS_INFO_LENGTH_MISMATCH = NTSTATUS(0xC0000004).value
ERROR_DIRECTORY = 0x010B
INVALID_HANDLE_VALUE = wintypes.HANDLE(-1).value
GENERIC_READ = 0x80000000
FILE_SHARE_READ = 1
OPEN_EXISTING = 3
FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
FILE_ATTRIBUTE_DIRECTORY = 0x0010
FILE_INFORMATION_CLASS = wintypes.ULONG
FileDirectoryInformation = 1
FileBasicInformation = 4
LPSECURITY_ATTRIBUTES = wintypes.LPVOID
PIO_APC_ROUTINE = wintypes.LPVOID
ULONG_PTR = wintypes.WPARAM
class UNICODE_STRING(ctypes.Structure):
_fields_ = (('Length', wintypes.USHORT),
('MaximumLength', wintypes.USHORT),
('Buffer', wintypes.LPWSTR))
PUNICODE_STRING = ctypes.POINTER(UNICODE_STRING)
class IO_STATUS_BLOCK(ctypes.Structure):
class _STATUS(ctypes.Union):
_fields_ = (('Status', NTSTATUS),
('Pointer', wintypes.LPVOID))
_anonymous_ = '_Status',
_fields_ = (('_Status', _STATUS),
('Information', ULONG_PTR))
PIO_STATUS_BLOCK = ctypes.POINTER(IO_STATUS_BLOCK)
ntdll.NtQueryInformationFile.restype = NTSTATUS
ntdll.NtQueryInformationFile.argtypes = (
wintypes.HANDLE, # In FileHandle
PIO_STATUS_BLOCK, # Out IoStatusBlock
wintypes.LPVOID, # Out FileInformation
wintypes.ULONG, # In Length
FILE_INFORMATION_CLASS) # In FileInformationClass
ntdll.NtQueryDirectoryFile.restype = NTSTATUS
ntdll.NtQueryDirectoryFile.argtypes = (
wintypes.HANDLE, # In FileHandle
wintypes.HANDLE, # In_opt Event
PIO_APC_ROUTINE, # In_opt ApcRoutine
wintypes.LPVOID, # In_opt ApcContext
PIO_STATUS_BLOCK, # Out IoStatusBlock
wintypes.LPVOID, # Out FileInformation
wintypes.ULONG, # In Length
FILE_INFORMATION_CLASS, # In FileInformationClass
wintypes.BOOLEAN, # In ReturnSingleEntry
PUNICODE_STRING, # In_opt FileName
wintypes.BOOLEAN) # In RestartScan
kernel32.CreateFileW.restype = wintypes.HANDLE
kernel32.CreateFileW.argtypes = (
wintypes.LPCWSTR, # In lpFileName
wintypes.DWORD, # In dwDesiredAccess
wintypes.DWORD, # In dwShareMode
LPSECURITY_ATTRIBUTES, # In_opt lpSecurityAttributes
wintypes.DWORD, # In dwCreationDisposition
wintypes.DWORD, # In dwFlagsAndAttributes
wintypes.HANDLE) # In_opt hTemplateFile
class FILE_BASIC_INFORMATION(ctypes.Structure):
_fields_ = (('CreationTime', wintypes.LARGE_INTEGER),
('LastAccessTime', wintypes.LARGE_INTEGER),
('LastWriteTime', wintypes.LARGE_INTEGER),
('ChangeTime', wintypes.LARGE_INTEGER),
('FileAttributes', wintypes.ULONG))
class FILE_DIRECTORY_INFORMATION(ctypes.Structure):
_fields_ = (('_Next', wintypes.ULONG),
('FileIndex', wintypes.ULONG),
('CreationTime', wintypes.LARGE_INTEGER),
('LastAccessTime', wintypes.LARGE_INTEGER),
('LastWriteTime', wintypes.LARGE_INTEGER),
('ChangeTime', wintypes.LARGE_INTEGER),
('EndOfFile', wintypes.LARGE_INTEGER),
('AllocationSize', wintypes.LARGE_INTEGER),
('FileAttributes', wintypes.ULONG),
('FileNameLength', wintypes.ULONG),
('_FileName', wintypes.WCHAR * 1))
#property
def FileName(self):
addr = ctypes.addressof(self) + type(self)._FileName.offset
size = self.FileNameLength // ctypes.sizeof(wintypes.WCHAR)
return (wintypes.WCHAR * size).from_address(addr).value
DirEntry and ntlistdir
class DirEntry(FILE_DIRECTORY_INFORMATION):
def __repr__(self):
return '<{} {!r}>'.format(self.__class__.__name__, self.FileName)
#classmethod
def listbuf(cls, buf):
result = []
base_size = ctypes.sizeof(cls) - ctypes.sizeof(wintypes.WCHAR)
offset = 0
while True:
fdi = cls.from_buffer(buf, offset)
if fdi.FileNameLength and fdi.FileName not in ('.', '..'):
cfdi = cls()
size = base_size + fdi.FileNameLength
ctypes.resize(cfdi, size)
ctypes.memmove(ctypes.byref(cfdi), ctypes.byref(fdi), size)
result.append(cfdi)
if fdi._Next:
offset += fdi._Next
else:
break
return result
def isdir(path):
if not isinstance(path, int):
return os.path.isdir(path)
try:
hFile = msvcrt.get_osfhandle(path)
except IOError:
return False
iosb = IO_STATUS_BLOCK()
info = FILE_BASIC_INFORMATION()
status = ntdll.NtQueryInformationFile(hFile, ctypes.byref(iosb),
ctypes.byref(info), ctypes.sizeof(info),
FileBasicInformation)
return bool(status >= 0 and info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
def ntlistdir(path=None):
result = []
if path is None:
path = os.getcwd()
if isinstance(path, int):
close = False
fd = path
hFile = msvcrt.get_osfhandle(fd)
else:
close = True
hFile = kernel32.CreateFileW(path, GENERIC_READ, FILE_SHARE_READ,
None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, None)
if hFile == INVALID_HANDLE_VALUE:
raise ctypes.WinError(ctypes.get_last_error())
fd = msvcrt.open_osfhandle(hFile, os.O_RDONLY)
try:
if not isdir(fd):
raise ctypes.WinError(ERROR_DIRECTORY)
iosb = IO_STATUS_BLOCK()
info = (ctypes.c_char * 4096)()
while True:
status = ntdll.NtQueryDirectoryFile(hFile, None, None, None,
ctypes.byref(iosb), ctypes.byref(info),
ctypes.sizeof(info), FileDirectoryInformation,
False, None, False)
if (status == STATUS_BUFFER_OVERFLOW or
iosb.Information == 0 and status >= 0):
info = (ctypes.c_char * (ctypes.sizeof(info) * 2))()
elif status == STATUS_NO_MORE_FILES:
break
elif status >= 0:
sublist = DirEntry.listbuf(info)
result.extend(sublist)
else:
raise NtError(status)
finally:
if close:
os.close(fd)
return result
Example
if __name__ == '__main__':
import sys
for entry in ntlistdir(sys.exec_prefix):
print(entry)
Output:
<DirEntry 'DLLs'>
<DirEntry 'include'>
<DirEntry 'Lib'>
<DirEntry 'libs'>
<DirEntry 'LICENSE.txt'>
<DirEntry 'NEWS.txt'>
<DirEntry 'python.exe'>
<DirEntry 'python.pdb'>
<DirEntry 'python3.dll'>
<DirEntry 'python36.dll'>
<DirEntry 'python36.pdb'>
<DirEntry 'python36_d.dll'>
<DirEntry 'python36_d.pdb'>
<DirEntry 'python3_d.dll'>
<DirEntry 'pythonw.exe'>
<DirEntry 'pythonw.pdb'>
<DirEntry 'pythonw_d.exe'>
<DirEntry 'pythonw_d.pdb'>
<DirEntry 'python_d.exe'>
<DirEntry 'python_d.pdb'>
<DirEntry 'Scripts'>
<DirEntry 'tcl'>
<DirEntry 'Tools'>
<DirEntry 'vcruntime140.dll'>

Categories

Resources