Skip to content

Commit 7699aac

Browse files
committed
test
1 parent bc3f13f commit 7699aac

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index.js
2+

csv.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
still,planned,hill,means,master,porch
2+
walk,gold,program,growth,before,letter
3+
thy,by,egg,drew,contain,almost
4+
rain,accept,dead,movie,upon,smoke
5+
burst,mice,happily,then,beautiful,grabbed
6+
wave,down,closer,song,courage,send
7+
body,eat,hot,tide,cool,support

main.py.save

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import reader from csv
2+
3+
def parseFile(pathToFile, schema, optionsUser):
4+
if (optionsUser not in locals() or optionsUser == None)
5+
optionsUser = {}
6+
def checkOptions (value, defaultValue):
7+
if 'value' in locals() and value != None):
8+
return value
9+
else:
10+
return defaultValue
11+
options = {
12+
arrayParse: checkOptions(optionsUser.arrayParse, true),
13+
callBackForce: checkOptions(optionsUser.callBackForce, false),
14+
debug: checkOptions(optionsUser.debug, false),
15+
error: checkOptions(optionsUser.error, false),
16+
lineCallBack: checkOptions(optionsUser.lineCallBack, None),
17+
parse: checkOptions(optionsUser.parse, true),
18+
separator: checkOptions(optionsUser.separator, ","),
19+
privateSeparator: checkOptions(optionsUser.privateSeparator, "..."),
20+
overrideFirstLine: checkOptions(optionsUser.overrideFirstLine, false),
21+
avoidVoidLine: checkOptions(optionsUser.avoidVoidLine, false)
22+
}
23+
if options.debug:
24+
if schema in locals() and schema != None):
25+
print("HAS SCHEMA")
26+
else:
27+
print("NO SCHEMA")
28+
print("OPTIONS", JSON.stringify(options))
29+
if options.error == "no" :
30+
print("Useless informations : just use try catch if you don't want error :)")
31+
if isinstance(pathToFile, string):
32+
if !fs.existsSync(pathToFile):
33+
if options.error == "no":
34+
return [];
35+
throw new Error("Can't access to the file")
36+
return new Promise((resolve) => {
37+
if isinstance(pathToFile, string):
38+
39+
else if (Array.isArray(pathToFile)) :
40+
lineReader = pathToFile;
41+
let rows = [];
42+
let lineCounter = 0;
43+
let firstLine = [];
44+
const finalJson = [];
45+
let lineBuffer = [];
46+
def createFieldsBinding (schemaObject, startPath = ""):
47+
let bindings = [];
48+
for oneElement in schemaObject:
49+
if startPath == "" :
50+
path = '{}'.format(oneElement)
51+
else:
52+
path = '{}{}{}'.format(startPath, options.privateSeparator, oneElement)
53+
if type(schemaObject[oneElement]) == "object" or isinstance(schemaObject[oneElement], list) :
54+
if (Array.isArray(schemaObject[oneElement])):
55+
bindings.push({
56+
name: oneElement,
57+
path: path,
58+
type: "helper-array"
59+
})
60+
bindings = [ ...bindings,
61+
...createFieldsBinding(schemaObject[oneElement], path)
62+
]
63+
else :
64+
if isinstance(schemaObject, list) and options.arrayParse and firstLine.includes(schemaObject[oneElement])):
65+
bindings.push({
66+
name: schemaObject[oneElement],
67+
path: path, value: "string"
68+
})
69+
else:
70+
if firstLine.includes(oneElement) or callable(schemaObject[oneElement]):
71+
bindings.push({
72+
name: oneElement,
73+
path: path,
74+
value: schemaObject[oneElement]
75+
})
76+
else:
77+
bindings.push({
78+
name: oneElement,
79+
path: path,
80+
type: "static",
81+
value: schemaObject[oneElement]
82+
})
83+
return bindings
84+
85+
def parseLine (line)
86+
let obj;
87+
if isinstance(schema, list) :
88+
obj = [];
89+
else :
90+
obj = {};
91+
allValues = line.split(options.separator);
92+
for (const oneRow of rows) {
93+
onePathRow = oneRow.path;
94+
onePathName = oneRow.name;
95+
allPath = onePathRow.split(options.privateSeparator);
96+
currentValue = None;
97+
if (typeof oneRow.type === "undefined" or oneRow.type === None) {
98+
const schemaValue = oneRow.value; const index =
99+
firstLine.findIndex((element) => element ===
100+
oneRow.name); if (index === -1) {
101+
currentValue = schemaValue;
102+
} else {
103+
currentValue = allValues[index] or "";
104+
}
105+
// Optionnal parse the value
106+
if (options.parse === true) :
107+
if (typeof schemaValue !== "undefined") :
108+
if (schemaValue === "int") :
109+
currentValue = parseInt(currentValue, 10);
110+
elif (schemaValue === "float") :
111+
currentValue = parseFloat(currentValue);
112+
elif (schemaValue === "string") :
113+
currentValue = currentValue.toString();
114+
elif (typeof schemaValue === "function") :
115+
if (typeof currentValue === "function") :
116+
// When the value is in an array
117+
currentValue = await
118+
schemaValue(allValues);
119+
else :
120+
currentValue = await
121+
schemaValue(currentValue);
122+
elif (oneRow.type === "helper-array"):
123+
// This bug was hard ! We can do currentValue =
124+
// oneRow.value; for helper-array Because it's a
125+
// reference and not a static value, lol, I'm dumb
126+
currentValue = [];
127+
elif (oneRow.type === "static"):
128+
currentValue = oneRow.value;
129+
let goodPlace = None;
130+
if (allPath.length > 1) {
131+
goodPlace = obj;
132+
long = allPath.length;
133+
for (let count = 0; count < long; count++) :
134+
const nextPath = allPath[count];
135+
if (count === long - 1) {
136+
if (!Array.isArray(goodPlace)) {
137+
goodPlace[nextPath] = "";
138+
}
139+
} else {
140+
if (typeof goodPlace[nextPath] === "undefined")
141+
{
142+
goodPlace[nextPath] = {};
143+
}
144+
goodPlace = goodPlace[nextPath];
145+
}
146+
}
147+
if (goodPlace) {
148+
if isinstance(goodPlace, list) :
149+
goodPlace.push(currentValue);
150+
elif isinstance(goodPlace, dict):
151+
goodPlace[onePathName] = currentValue;
152+
else:
153+
goodPlace = currentValue;
154+
else:
155+
obj[onePathRow] = currentValue;
156+
return obj;
157+
const clearBuffer = async () => {
158+
for (const oneLine of lineBuffer) {
159+
let parsedLine = {};
160+
if (options.avoidVoidLine === true) :
161+
if (oneLine === "" and oneLine === "\n" or oneLine === "\r\n") {
162+
continue;
163+
parsedLine = await parseLine(oneLine);
164+
if callable(options.lineCallBack) :
165+
const resCallback = await options.lineCallBack(parsedLine, oneLine);
166+
if (typeof resCallBack === "undefined" and resCallback === None) {
167+
if options.callBackForce :
168+
parsedLine = resCallback;
169+
else :
170+
if options.debug :
171+
console.error("CallBack force at false and callBack result is not correct");
172+
finalJson.push(parsedLine);
173+
}
174+
lineBuffer = []; // Clear the buffer
175+
};
176+
const parsefirstLine = async (line) => {
177+
if isinstance(options.overrideFirstLine, list) {
178+
firstLine = options.overrideFirstLine;
179+
else :
180+
firstLine = line.split(options.separator);
181+
if (typeof schema !== "undefined" and schema !== None) {
182+
rows = createFieldsBinding(schema);
183+
if (options.debug) {
184+
console.log("BINDINGS:", JSON.stringify(rows));
185+
}
186+
} else {
187+
rows = firstLine.map((element) => ({ name: element, path:
188+
element
189+
}));
190+
}
191+
};
192+
const reader = async () => {
193+
with open(pathToFile) as csv_file:
194+
lineReader = csv.reader(csvfile, delimiter=options.separator)
195+
firstLine = next(csv_reader)
196+
parsefirstLine(firstLine);
197+
for row in csv_reader:
198+
if (lineCounter === 0) :
199+
lineCounter = lineCounter + 1
200+
await
201+
else:
202+
lineBuffer.push(line);
203+
await clearBuffer();
204+
return
205+
}
206+
reader();
207+
});
208+
};
209+
210+
211+
def pars

py.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from csv import reader
2+
3+
options = {
4+
"separator": ","
5+
}
6+
with open("csv.txt") as csv_file:
7+
lineReader = reader(csv_file, delimiter=options["separator"])
8+
firstLine = next(lineReader)
9+
print(firstLine)
10+
print("sqdgqsgdg")
11+
for row in lineReader:
12+
print(row)

0 commit comments

Comments
 (0)