csv-validation 0.0.8__tar.gz → 0.0.9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {csv_validation-0.0.8 → csv_validation-0.0.9}/Cargo.lock +1 -1
- {csv_validation-0.0.8 → csv_validation-0.0.9}/Cargo.toml +1 -1
- {csv_validation-0.0.8 → csv_validation-0.0.9}/PKG-INFO +33 -9
- {csv_validation-0.0.8 → csv_validation-0.0.9}/README.md +32 -8
- {csv_validation-0.0.8 → csv_validation-0.0.9}/src/lib.rs +47 -13
- {csv_validation-0.0.8 → csv_validation-0.0.9}/.github/workflows/CI.yml +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/.gitignore +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/LICENSE +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/build.sh +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/pyproject.toml +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/test/empty_values.csv +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/test/test_file.csv +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/test/test_file.csv.gz +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.9}/test/test_validations.yml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: csv_validation
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.9
|
|
4
4
|
Classifier: Programming Language :: Rust
|
|
5
5
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
6
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
@@ -8,7 +8,7 @@ License-File: LICENSE
|
|
|
8
8
|
Requires-Python: >=3.8
|
|
9
9
|
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
10
10
|
|
|
11
|
-
# CSV Validation
|
|
11
|
+
# CSV Validation Library
|
|
12
12
|
|
|
13
13
|
Blazing fast format validations for your CSV files
|
|
14
14
|
|
|
@@ -59,6 +59,10 @@ columns:
|
|
|
59
59
|
"""
|
|
60
60
|
|
|
61
61
|
validator = CSVValidator.from_string(validation_rules)
|
|
62
|
+
# Optionally set a custom column separator
|
|
63
|
+
validator.set_separator(";") # Default is comma (,)
|
|
64
|
+
|
|
65
|
+
# Validate a CSV file
|
|
62
66
|
is_valid = validator.validate("data.csv")
|
|
63
67
|
```
|
|
64
68
|
|
|
@@ -126,13 +130,33 @@ columns:
|
|
|
126
130
|
# other validations...
|
|
127
131
|
```
|
|
128
132
|
|
|
129
|
-
|
|
133
|
+
### Supported Formats
|
|
134
|
+
|
|
135
|
+
- `integer`: Any integer number (positive or negative)
|
|
136
|
+
- `positive integer`: Any positive integer number
|
|
137
|
+
- `decimal`: Any decimal number (positive or negative)
|
|
138
|
+
- `positive decimal point`: Any positive decimal number using point as decimal separator
|
|
139
|
+
- `positive decimal comma`: Any positive decimal number using comma as decimal separator
|
|
140
|
+
- `decimal scientific`: Any decimal number in scientific notation
|
|
141
|
+
- `non_empty`: Any non-empty value
|
|
142
|
+
|
|
143
|
+
### Column Separators
|
|
144
|
+
|
|
145
|
+
By default, the library uses comma (,) as the column separator. You can change this using the `set_separator` method:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
validator = CSVValidator.from_file("rules.yml")
|
|
149
|
+
validator.set_separator(";") # Use semicolon as separator
|
|
150
|
+
validator.set_separator("\t") # Use tab as separator
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Error Handling
|
|
130
154
|
|
|
131
|
-
The
|
|
132
|
-
-
|
|
133
|
-
-
|
|
134
|
-
- Sample of invalid values
|
|
135
|
-
-
|
|
155
|
+
The library provides detailed validation reports through Python's logging system. When a validation fails, you'll get information about:
|
|
156
|
+
- Which columns failed validation
|
|
157
|
+
- What type of validation failed
|
|
158
|
+
- Sample of invalid values
|
|
159
|
+
- Number of rows that failed each validation
|
|
136
160
|
|
|
137
161
|
## Development
|
|
138
162
|
|
|
@@ -142,7 +166,7 @@ The validation process provides detailed feedback including:
|
|
|
142
166
|
- Python 3.6+ (for Python bindings)
|
|
143
167
|
- Cargo and standard Rust tooling
|
|
144
168
|
|
|
145
|
-
### Building from Source
|
|
169
|
+
### Building from Source (Rust version)
|
|
146
170
|
|
|
147
171
|
```bash
|
|
148
172
|
# Clone the repository
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# CSV Validation
|
|
1
|
+
# CSV Validation Library
|
|
2
2
|
|
|
3
3
|
Blazing fast format validations for your CSV files
|
|
4
4
|
|
|
@@ -49,6 +49,10 @@ columns:
|
|
|
49
49
|
"""
|
|
50
50
|
|
|
51
51
|
validator = CSVValidator.from_string(validation_rules)
|
|
52
|
+
# Optionally set a custom column separator
|
|
53
|
+
validator.set_separator(";") # Default is comma (,)
|
|
54
|
+
|
|
55
|
+
# Validate a CSV file
|
|
52
56
|
is_valid = validator.validate("data.csv")
|
|
53
57
|
```
|
|
54
58
|
|
|
@@ -116,13 +120,33 @@ columns:
|
|
|
116
120
|
# other validations...
|
|
117
121
|
```
|
|
118
122
|
|
|
119
|
-
|
|
123
|
+
### Supported Formats
|
|
124
|
+
|
|
125
|
+
- `integer`: Any integer number (positive or negative)
|
|
126
|
+
- `positive integer`: Any positive integer number
|
|
127
|
+
- `decimal`: Any decimal number (positive or negative)
|
|
128
|
+
- `positive decimal point`: Any positive decimal number using point as decimal separator
|
|
129
|
+
- `positive decimal comma`: Any positive decimal number using comma as decimal separator
|
|
130
|
+
- `decimal scientific`: Any decimal number in scientific notation
|
|
131
|
+
- `non_empty`: Any non-empty value
|
|
132
|
+
|
|
133
|
+
### Column Separators
|
|
134
|
+
|
|
135
|
+
By default, the library uses comma (,) as the column separator. You can change this using the `set_separator` method:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
validator = CSVValidator.from_file("rules.yml")
|
|
139
|
+
validator.set_separator(";") # Use semicolon as separator
|
|
140
|
+
validator.set_separator("\t") # Use tab as separator
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Error Handling
|
|
120
144
|
|
|
121
|
-
The
|
|
122
|
-
-
|
|
123
|
-
-
|
|
124
|
-
- Sample of invalid values
|
|
125
|
-
-
|
|
145
|
+
The library provides detailed validation reports through Python's logging system. When a validation fails, you'll get information about:
|
|
146
|
+
- Which columns failed validation
|
|
147
|
+
- What type of validation failed
|
|
148
|
+
- Sample of invalid values
|
|
149
|
+
- Number of rows that failed each validation
|
|
126
150
|
|
|
127
151
|
## Development
|
|
128
152
|
|
|
@@ -132,7 +156,7 @@ The validation process provides detailed feedback including:
|
|
|
132
156
|
- Python 3.6+ (for Python bindings)
|
|
133
157
|
- Cargo and standard Rust tooling
|
|
134
158
|
|
|
135
|
-
### Building from Source
|
|
159
|
+
### Building from Source (Rust version)
|
|
136
160
|
|
|
137
161
|
```bash
|
|
138
162
|
# Clone the repository
|
|
@@ -5,7 +5,7 @@ use std::fs::File;
|
|
|
5
5
|
use std::io::{BufReader, Read};
|
|
6
6
|
use std::iter::zip;
|
|
7
7
|
use std::path::Path;
|
|
8
|
-
use csv::Reader;
|
|
8
|
+
use csv::{Reader, ReaderBuilder};
|
|
9
9
|
use flate2::bufread::GzDecoder;
|
|
10
10
|
use log::{debug, error, info};
|
|
11
11
|
use pyo3::exceptions::{PyRuntimeError};
|
|
@@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize};
|
|
|
16
16
|
use crate::Validation::{RegularExpression};
|
|
17
17
|
|
|
18
18
|
const MAX_SAMPLE_SIZE:u16 = 10;
|
|
19
|
+
const DEFAULT_COLUMN_SEPARATOR:u8 = b',';
|
|
19
20
|
|
|
20
21
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
21
22
|
enum Validation {
|
|
@@ -121,16 +122,20 @@ fn get_regex_string_for_values(values: &Vec<String>) -> String {
|
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
/// Infers the file compression type and returns the corresponding buffered reader
|
|
124
|
-
fn get_reader_from(path: &str) -> PyResult<Reader<Box<dyn Read>>> {
|
|
125
|
+
fn get_reader_from(path: &str, separator: u8) -> PyResult<Reader<Box<dyn Read>>> {
|
|
125
126
|
let buf_reader = BufReader::new(File::open(Path::new(path))?);
|
|
126
127
|
if is_gzip_file(path)? {
|
|
127
128
|
debug!("File is gzipped");
|
|
128
129
|
let read_capacity = 10 * 1024_usize.pow(2);
|
|
129
130
|
let reader = BufReader::with_capacity(read_capacity, GzDecoder::new(buf_reader));
|
|
130
|
-
Ok(
|
|
131
|
+
Ok(ReaderBuilder::new()
|
|
132
|
+
.delimiter(separator)
|
|
133
|
+
.from_reader(Box::new(reader)))
|
|
131
134
|
}
|
|
132
135
|
else {
|
|
133
|
-
Ok(
|
|
136
|
+
Ok(ReaderBuilder::new()
|
|
137
|
+
.delimiter(separator)
|
|
138
|
+
.from_reader(Box::new(buf_reader)))
|
|
134
139
|
}
|
|
135
140
|
}
|
|
136
141
|
|
|
@@ -258,7 +263,8 @@ fn get_regex_for_format(format: &str) -> PyResult<String> {
|
|
|
258
263
|
"integer" => Ok(String::from("^$|^-?\\d+$")),
|
|
259
264
|
"positive integer" => Ok(String::from("^$|^\\d+$")),
|
|
260
265
|
"decimal" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?$")),
|
|
261
|
-
"positive decimal" => Ok(String::from("^$|^\\d+(\\.\\d+)?$")),
|
|
266
|
+
"positive decimal point" | "positive decimal" => Ok(String::from("^$|^\\d+(\\.\\d+)?$")),
|
|
267
|
+
"positive decimal comma" => Ok(String::from("^$|^\\d+(,\\d+)?$")),
|
|
262
268
|
"decimal scientific" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$")),
|
|
263
269
|
"non_empty" => Ok(String::from("^.+$")),
|
|
264
270
|
_ => Err(PyRuntimeError::new_err(format!("Unknown format: {format}")))
|
|
@@ -276,15 +282,17 @@ fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<C
|
|
|
276
282
|
|
|
277
283
|
if expected_column_names != headers {
|
|
278
284
|
if expected_column_names.len() != headers.len() {
|
|
285
|
+
info!("The number of columns in the CSV file doesn't match the validations:");
|
|
279
286
|
let expected_columns_set: HashSet<String> = expected_column_names.iter().cloned().collect();
|
|
280
287
|
let headers_set: HashSet<String> = headers.iter().cloned().collect();
|
|
281
|
-
|
|
282
|
-
|
|
288
|
+
info!("These column names in the CSV file were not expected: {:?}", headers_set.difference(&expected_columns_set));
|
|
289
|
+
info!("These expected column names were missing in the CSV file: {:?}", expected_columns_set.difference(&headers_set));
|
|
283
290
|
}
|
|
284
291
|
else {
|
|
292
|
+
info!("The CSV file has the same number of columns than the validations but some names are different:");
|
|
285
293
|
for (expected_column, header) in zip(expected_column_names, headers) {
|
|
286
294
|
if expected_column != header {
|
|
287
|
-
|
|
295
|
+
info!("{:?} != {:?}", expected_column, header);
|
|
288
296
|
}
|
|
289
297
|
}
|
|
290
298
|
}
|
|
@@ -296,7 +304,8 @@ fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<C
|
|
|
296
304
|
#[pyclass]
|
|
297
305
|
struct CSVValidator {
|
|
298
306
|
validations: Vec<ColumnValidations>,
|
|
299
|
-
regex_map: HashMap<String, Regex
|
|
307
|
+
regex_map: HashMap<String, Regex>,
|
|
308
|
+
separator: u8
|
|
300
309
|
}
|
|
301
310
|
|
|
302
311
|
#[pymethods]
|
|
@@ -305,7 +314,8 @@ impl CSVValidator {
|
|
|
305
314
|
fn new() -> Self {
|
|
306
315
|
CSVValidator {
|
|
307
316
|
validations: Vec::new(),
|
|
308
|
-
regex_map: HashMap::new()
|
|
317
|
+
regex_map: HashMap::new(),
|
|
318
|
+
separator: DEFAULT_COLUMN_SEPARATOR
|
|
309
319
|
}
|
|
310
320
|
}
|
|
311
321
|
|
|
@@ -336,12 +346,22 @@ impl CSVValidator {
|
|
|
336
346
|
}
|
|
337
347
|
}
|
|
338
348
|
|
|
339
|
-
Ok(CSVValidator { validations, regex_map })
|
|
349
|
+
Ok(CSVValidator { validations, regex_map, separator: DEFAULT_COLUMN_SEPARATOR })
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
fn set_separator(&mut self, separator: String) -> PyResult<()> {
|
|
353
|
+
if separator.len() == 1 {
|
|
354
|
+
self.separator = separator.chars().next().unwrap() as u8;
|
|
355
|
+
Ok(())
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
Err(PyRuntimeError::new_err(format!("Wrong separator {separator}. It can only have one character")))
|
|
359
|
+
}
|
|
340
360
|
}
|
|
341
361
|
|
|
342
362
|
fn validate(&self, file_path: &str) -> PyResult<bool> {
|
|
343
363
|
// Build the CSV reader
|
|
344
|
-
let mut rdr = get_reader_from(file_path)?;
|
|
364
|
+
let mut rdr = get_reader_from(file_path, self.separator)?;
|
|
345
365
|
|
|
346
366
|
// First validation: Ensure column names and order are exactly as expected
|
|
347
367
|
if validate_column_names(&mut rdr, &self.validations)? {
|
|
@@ -396,6 +416,7 @@ impl CSVValidator {
|
|
|
396
416
|
// TODO: Decide how to return the results of the validations
|
|
397
417
|
let _validation_result_json = serde_json::to_string(&column_validation_summaries).unwrap();
|
|
398
418
|
|
|
419
|
+
let mut failed_columns = HashMap::new();
|
|
399
420
|
info!("VALIDATIONS SUMMARY");
|
|
400
421
|
info!("==================================================================================");
|
|
401
422
|
info!("Rows: {} | Columns: {}", validated_rows, column_validation_summaries.len());
|
|
@@ -409,6 +430,13 @@ impl CSVValidator {
|
|
|
409
430
|
};
|
|
410
431
|
info!("\tValidation {:?} => Wrong Rows: {}{}", validation_summary.validation,
|
|
411
432
|
validation_summary.wrong_rows, wrong_values_sample);
|
|
433
|
+
if validation_summary.wrong_rows > 0 {
|
|
434
|
+
let column_name = &column_validation_summary.column_name;
|
|
435
|
+
if !failed_columns.contains_key(column_name) {
|
|
436
|
+
failed_columns.insert(column_name.clone(), vec!());
|
|
437
|
+
}
|
|
438
|
+
failed_columns.get_mut(column_name).unwrap().push(validation_summary);
|
|
439
|
+
}
|
|
412
440
|
}
|
|
413
441
|
}
|
|
414
442
|
|
|
@@ -416,7 +444,13 @@ impl CSVValidator {
|
|
|
416
444
|
info!("OK: File matches the validations");
|
|
417
445
|
}
|
|
418
446
|
else {
|
|
419
|
-
info!("NO OK: File DOESN'T match validations");
|
|
447
|
+
info!("NO OK: File DOESN'T match validations. Failed Validations:");
|
|
448
|
+
for (column_name, failed_validations) in failed_columns.into_iter() {
|
|
449
|
+
info!("COLUMN '{}' :", column_name);
|
|
450
|
+
for validation_summary in failed_validations {
|
|
451
|
+
info!("\tValidation {:?} => Wrong Rows: {}", validation_summary.validation, validation_summary.wrong_rows)
|
|
452
|
+
}
|
|
453
|
+
}
|
|
420
454
|
}
|
|
421
455
|
Ok(is_valid_file)
|
|
422
456
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|