-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileDecoding.py
70 lines (57 loc) · 2.07 KB
/
FileDecoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
SnownyPython - Python maze game with Turtle module
Author: Jimmy F.
Date: 23/03/2021
Version: 0.1
This class allows to decode the files allowing
the maze to exist
Usage:
N/A
Enter:
The files to decode
Results:
The decoded files
"""
from CONFIGS import * # All constant variables
def tryCastToInt(number):
"""
Allows to cast variable in int
number: Int | Number to try to parse
Example: tryCastToInt("6") = 6
Returns: Int | The int, or 0 if the parse don't work
"""
try:
return int(number)
except:
print("Error! Impossible to parse this variable")
return 0
def lire_matrice(fichier):
"""
Allows to interpret the file in a matrix
fichier: String | File name to read
Example: lire_matrice("plan_chateau.txt") = [[1,0,1,1],[0,0,1,0]]
Returns: 2D List | Matrix
"""
matrix = [] # Matrix (2D list) of the game plan
with open(fichier) as file:
data = file.readlines() # Contains all rows as a list (1 row = 1 item)
for i in data: # i will have the line
IntList = [] # Matrix which will contain in item integer
for j in i.split(): # j will have each digit of the line i
IntList.append(tryCastToInt(j))
matrix.append(IntList)
return matrix
def creer_dictionnaire(fichier_des_objets):
"""
Reads and interprets the object file
fichier_des_objets: String | File name
Example: creer_dictionnaire("dico_portes.txt") = {(3, 4): ('Lequel est un mot réservé Python : elif ou elsif ?', 'elif'), .....}
Returns: Dictionary | Item dictionary for Object or Questions, format = cell:item (tuple:string)
"""
dictObj = {} # Item dictionary, format = cell:item (tuple:string)
with open(fichier_des_objets, mode="r", encoding="utf-8") as file:
data = file.readlines() # Contains all rows as a list (1 row = 1 item)
for i in data:
cell, item = eval(i) # Defines according to the type of cast a value to the variable
dictObj[cell] = item
return dictObj