Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 102 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,16 @@ Ideal for **form validation, data cleaning, and big data applications**.

## Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Quick Start](#quick-start)
- [Features](#features)
- [Command-Line Usage](#command-line-usage)
- [Big Data Support](#big-data-support)
- [Contributing](#contributing)
- [License](#license)

---

## Features

- ✅ 50+ countries included, with postal code regex patterns
- ✅ Validate postal codes by **country code** or **country name**
```python
from postal_regex.core import validate

validate("IN", "110001") # True
validate("India", "110001") # True
validate("US", "12345-6789") # True
````

* ✅ Normalize country identifiers

```python
from postal_regex.core import normalize

normalize("United States") # "US"
normalize("India") # "IN"
```

* ✅ Works with **Pandas and Spark DataFrames**

```python
import pandas as pd
from postal_regex.bulk import validate_dataframe

df = pd.DataFrame({"country": ["US", "FR"], "postal_code": ["90210", "75001"]})
df_validated = validate_dataframe(df, country_col="country", postal_col="postal_code")
print(df_validated)
```

* ✅ JSON schema ensures consistent data structure
* ✅ Precompiled regex for fast Python validation

---

## Installation

```bash
Expand All @@ -75,37 +39,61 @@ pip install -e .

---

## Big Data Support
## Quick Start

Validate postal codes in **large datasets** with Spark or Pandas.
```python
from postal_regex.core import validate

### Spark Example
# Validate by country code
validate("IN", "110001") # True
validate("US", "12345-6789") # True

# By country name
validate("India", "110001") # True

# Invalid returns False
validate("US", "ABCDE") # False
```

---

## Features

- ✅ 50+ countries included, with postal code regex patterns
- ✅ Validate postal codes by **country code** or **country name**
- ✅ Normalize country identifiers

```python
from pyspark.sql import SparkSession
from postal_regex.bulk import validate_spark_dataframe
from postal_regex.core import normalize

spark = SparkSession.builder.getOrCreate()
df = spark.createDataFrame([
{"country": "FR", "postal_code": "75001"},
{"country": "DE", "postal_code": "10115"}
])
df_validated = validate_spark_dataframe(df, country_col="country", postal_col="postal_code")
df_validated.show()
normalize("United States") # "US"
normalize("India") # "IN"
```

### Pandas Example
- ✅ Works with **Pandas and Spark DataFrames**
- ✅ JSON schema ensures consistent data structure
- ✅ Precompiled regex for fast Python validation

### Examples for Recently Added Countries (Indonesia, Bangladesh, Pakistan, Sri Lanka, Nepal)

```python
import pandas as pd
from postal_regex.bulk import validate_dataframe
from postal_regex.core import validate

df = pd.DataFrame({
"country": ["FR", "DE"],
"postal_code": ["75001", "10115"]
})
df_validated = validate_dataframe(df, country_col="country", postal_col="postal_code")
print(df_validated)
# Indonesia (ID)
validate("ID", "12345") # **Expected: True** (valid 5-digit code)
validate("Indonesia", "12345") # **Expected: True**

# Bangladesh (BD)
validate("BD", "1205") # **Expected: True** (valid 4-digit code)

# Pakistan (PK)
validate("PK", "44000") # **Expected: True** (valid 5-digit code)

# Sri Lanka (LK)
validate("LK", "00300") # **Expected: True** (valid 5-digit code, e.g., Colombo)

# Nepal (NP)
validate("NP", "44600") # **Expected: True** (valid 5-digit code, e.g., Kathmandu)
```

---
Expand All @@ -125,7 +113,7 @@ python -m postal_regex.cli validate <postal_code1> <postal_code2> ... <country>
**Examples:**

Validate a single code for India:
```
```sh
python -m postal_regex.cli validate 110001 IN
```
Output:
Expand All @@ -134,7 +122,7 @@ Output:
```

Validate multiple codes for the United States:
```
```sh
python -m postal_regex.cli validate 12345 90210 US
```
Output:
Expand All @@ -144,30 +132,77 @@ Output:
```

You can also use country names:
```
```sh
python -m postal_regex.cli validate 110001 India
```

### View Validation Statistics

To view local validation statistics:
```
```sh
python -m postal_regex.cli stats
```

To reset statistics:
```
```sh
python -m postal_regex.cli stats --reset
```

---

## Big Data Support

Validate postal codes in **large datasets** with Spark or Pandas.

### Spark Example

```python
from pyspark.sql import SparkSession
from postal_regex.bulk import validate_spark_dataframe

spark = SparkSession.builder.getOrCreate()
df = spark.createDataFrame([
{"country": "FR", "postal_code": "75001"},
{"country": "DE", "postal_code": "10115"}
])
df_validated = validate_spark_dataframe(df, country_col="country", postal_col="postal_code")
df_validated.show()
```

### Pandas Example

```python
import pandas as pd
from postal_regex.bulk import validate_dataframe

df = pd.DataFrame({
"country": ["FR", "DE"],
"postal_code": ["75001", "10115"]
})
df_validated = validate_dataframe(df, country_col="country", postal_col="postal_code")
print(df_validated)
```

---

## Contributing

We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
We ❤️ contributions! Help expand coverage or improve docs.

1. Fork the repo and create a feature branch: `git checkout -b feat/new-country`.
2. Add/update patterns in `postal_regex/data/` (follow JSON schema).
3. Run tests: `pytest`.
4. Commit and push: `git commit -m "Add postal patterns for [Country]"`.
5. Open a Pull Request—reference any related issue.

See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines, including testing new patterns and updating examples.

---

## License

MIT License. See [LICENSE](LICENSE) for details.
MIT License. See [LICENSE](LICENSE) for details.

---

⭐ **Star this repo** if it's useful! Found a bug or missing country? [Open an issue](https://github.com/ankitgadling/postal-regex/issues). Questions? Join the discussion!