|
1 | | -# Python FWF |
| 1 | +<!-- markdownlint-disable MD013 --> |
| 2 | +# python-pyfwf |
2 | 3 |
|
3 | | -[](https://opensource.org/licenses/MIT) |
4 | | -[](https://pypi.org/project/pyfwf/) |
| 4 | +[](LICENSE) |
| 5 | +[](https://www.python.org/) |
5 | 6 | [](https://github.com/fixed-width-file/fwf-compliance-tests) |
6 | 7 | [](https://github.com/fixed-width-file/python-pyfwf/actions/workflows/qa.yml) |
7 | 8 | [](https://codecov.io/gh/fixed-width-file/python-pyfwf) |
8 | | -[](https://fixed-width-file.github.io/python-pyfwf/) |
9 | 9 | [](https://github.com/pre-commit/pre-commit) |
10 | 10 |
|
11 | | -Python library for reading and manipulating **fixed width files (FWF)**. |
| 11 | +**python-pyfwf** is a fast, type-safe Python 3.9+ library for parsing, validating, and exporting **Fixed Width Files (FWF)**. |
12 | 12 |
|
13 | | -> See each package's [documentation](https://fixed-width-file.github.io/python-pyfwf/) |
14 | | -> for details and usage examples. |
| 13 | +It is part of the [Fixed Width File Ecosystem](https://fixed-width-file.github.io/) and fully implements the **[fwf-compliance-tests v1.0.0](https://github.com/fixed-width-file/fwf-compliance-tests)** specification. |
15 | 14 |
|
16 | | -This library is necessary because large banks and the Brazilian government use a |
17 | | -batch file model that has 3 data blocks: |
| 15 | +--- |
18 | 16 |
|
19 | | -1. The **header** line identifies the file type, does not describe the file |
20 | | - structure, and usually starts the line with the number 1 to indicate it |
21 | | - is the header. |
22 | | -2. The **detail** contains the data and may have more than one type of detail. |
23 | | - For example, if it starts with 2 it represents a state, if it starts with |
24 | | - 3 it represents a municipality of the state that came before it, and each |
25 | | - type of detail has its own data structure. |
26 | | -3. The **footer** line signs the file, that is, it may have a line counter |
27 | | - field or another field to validate if the file is complete, and usually |
28 | | - starts the line with the number 9 to indicate it is the footer. |
| 17 | +## 🌟 Key Features |
29 | 18 |
|
30 | | -Thus, this library uses descriptors to define how the data should be read, |
31 | | -having **file**, **header**, **detail**, and **footer** descriptors. |
| 19 | +- **Type-Safe Columns**: `CharColumn`, `RightCharColumn`, `PositiveIntegerColumn`, `PositiveDecimalColumn`, `DateColumn`, `TimeColumn`, and `DateTimeColumn`. |
| 20 | +- **Flexible Descriptors**: Structured records with `HeaderRowDescriptor`, `DetailRowDescriptor`, and `FooterRowDescriptor`. |
| 21 | +- **Multiple Output Renders**: Export layout specifications to Markdown, ReStructuredText (RST), or HTML tables via `RenderUtils`. |
| 22 | +- **Full Test Suite & Compliance**: 100% compliant with `fwf-compliance-tests` v1.0.0. |
| 23 | +- **Git Hooks Ready**: Pre-configured `pre-commit` and `pre-push` hooks. |
32 | 24 |
|
33 | | -## Compare with others packages |
| 25 | +--- |
34 | 26 |
|
35 | | -<!-- markdownlint-disable MD013 --> |
36 | | -| Package | Main Focus / Features | API Style | Typed Columns | Header/Footer | Documentation | Test Coverage | |
37 | | -|------------------------------------------------------------|-----------------------------------------------------------------------------------------|--------------|---------------|---------------|---------------|---------------| |
38 | | -| **pyfwf** | Flexible, typed columns, descriptors, header/footer, 100% coverage | Pythonic/OOP | Yes | Yes | Extensive | 100% | |
39 | | -| [fwf](https://pypi.org/project/fwf/) | Simple FWF reader/writer, minimal configuration | Functional | No | No | Minimal | Unknown | |
40 | | -| [microtrade-fwf](https://pypi.org/project/microtrade-fwf/) | Basic FWF parsing, focused on simplicity, limited features | Functional | No | No | Minimal | Unknown | |
41 | | -| [petl-fwf](https://pypi.org/project/petl-fwf/) | FWF support as part of [petl](https://petl.readthedocs.io/) ETL toolkit, table-oriented | Table/ETL | No | No | Good (petl) | Good (petl) | |
42 | | -<!-- markdownlint-enable MD013 --> |
43 | | - |
44 | | -**Summary of differences:** |
45 | | - |
46 | | -- **pyfwf** offers an object-oriented API, support for typed columns |
47 | | - (int, decimal, date, etc.), header/footer definition, and full test coverage. |
48 | | - Ideal for scenarios that require validation and strict data structure. |
49 | | -- **fwf** and **microtrade-fwf** are simpler solutions, with fewer validation |
50 | | - and configuration options, aimed at quick and basic usage. |
51 | | -- **petl-fwf** integrates FWF reading into the petl ecosystem, useful for ETL, |
52 | | - but without a focus on type validation or detailed file structure. |
53 | | - |
54 | | -## Features |
55 | | - |
56 | | -- 📖 Read fixed-width format files with custom column definitions |
57 | | -- 🔧 Support for typed columns (integer, decimal, date, time, etc.) |
58 | | -- 📋 Header and footer row handling |
59 | | -- 🎯 Simple and intuitive API |
60 | | -- ✅ Fully tested (100% coverage) |
61 | | - |
62 | | -## Installation |
| 27 | +## 🚀 Installation |
| 28 | + |
| 29 | +Install via `pip`: |
63 | 30 |
|
64 | 31 | ```bash |
65 | 32 | pip install pyfwf |
66 | 33 | ``` |
67 | 34 |
|
68 | | -## Quick Start |
| 35 | +--- |
| 36 | + |
| 37 | +## 💡 Quickstart |
69 | 38 |
|
70 | 39 | ```python |
71 | 40 | from pyfwf.columns import CharColumn, PositiveIntegerColumn |
72 | 41 | from pyfwf.descriptors import DetailRowDescriptor, FileDescriptor |
73 | 42 | from pyfwf.readers import Reader |
74 | 43 |
|
75 | | -# Define columns |
76 | | -detail = DetailRowDescriptor([ |
77 | | - CharColumn(name='name', pos=1, size=20), |
78 | | - PositiveIntegerColumn(name='age', pos=21, size=3), |
79 | | -]) |
| 44 | +# 1. Define columns |
| 45 | +name_col = CharColumn('name', 20, 'User Name') |
| 46 | +age_col = PositiveIntegerColumn('age', 3, 'Age in years') |
80 | 47 |
|
81 | | -# Create file descriptor |
82 | | -fd = FileDescriptor(line_size=23, details=[detail]) |
| 48 | +# 2. Define row and file descriptors |
| 49 | +detail = DetailRowDescriptor([name_col, age_col]) |
| 50 | +file_descriptor = FileDescriptor([detail]) |
83 | 51 |
|
84 | | -# Read file |
85 | | -with open('data.fwf', 'r') as f: |
86 | | - reader = Reader(f, fd) |
87 | | - for row in reader: |
88 | | - print(row) |
89 | | -``` |
| 52 | +# 3. Read fixed-width file content |
| 53 | +content = "KELSON MEDEIROS 045\nMARIA SILVA 030\n" |
| 54 | +reader = Reader(content, file_descriptor, "\n") |
90 | 55 |
|
91 | | -## Development & Pre-commit Hooks |
| 56 | +for row in reader: |
| 57 | + print(f"Name: {row['name']} | Age: {row['age']}") |
| 58 | +``` |
92 | 59 |
|
93 | | -This project uses [`pre-commit`](https://pre-commit.com/) to enforce |
94 | | -code quality, linting, formatting, and unit tests before committing and pushing. |
| 60 | +--- |
95 | 61 |
|
96 | | -### Installing Git Hooks |
| 62 | +## 🧪 Testing & Compliance |
97 | 63 |
|
98 | | -To set up the pre-commit and pre-push hooks locally: |
| 64 | +Run all unit tests using pytest: |
99 | 65 |
|
100 | 66 | ```bash |
101 | | -# Install development dependencies |
102 | | -pip install -e ".[dev]" |
| 67 | +pytest |
| 68 | +``` |
103 | 69 |
|
104 | | -# Install pre-commit and pre-push hooks |
| 70 | +--- |
| 71 | + |
| 72 | +## ⚓ Pre-Commit & Pre-Push Setup |
| 73 | + |
| 74 | +Set up pre-commit and pre-push hooks: |
| 75 | + |
| 76 | +```bash |
105 | 77 | pre-commit install |
106 | 78 | pre-commit install --hook-type pre-push |
107 | 79 | ``` |
108 | 80 |
|
109 | | -### Running Hooks Manually |
110 | | - |
111 | | -You can execute the hooks manually against all files at any time: |
| 81 | +Run checks manually: |
112 | 82 |
|
113 | 83 | ```bash |
114 | | -# Run commit-stage hooks (Black, Ruff, doc8, markdownlint, whitespace) |
115 | 84 | pre-commit run --all-files |
116 | | - |
117 | | -# Run push-stage hooks (pytest & 100% coverage gate) |
118 | | -pre-commit run --hook-stage pre-push --all-files |
119 | 85 | ``` |
120 | 86 |
|
121 | | -## Security |
122 | | - |
123 | | -Please report vulnerabilities according to [SECURITY.md](SECURITY.md). |
| 87 | +--- |
124 | 88 |
|
125 | | -## Author |
| 89 | +## 📄 License |
126 | 90 |
|
127 | | -Kelson da Costa Medeiros <kelsoncm@gmail.com> |
| 91 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments