A simple price parser for extracting currency and value from strings (currently used as a Pydantic datatype only).
Project description
💰 Price Parser Reworkd: Extract Prices with Ease 💰
Price Parser Reworkd
Price Parser
is a Python library created by reworkd team that provides Pydantic-compatible data types for parsing and extracting price-related information from strings. It allows you to effortlessly handle numeric values, currency symbols, and currency names in a structured and validated way using Pydantic models.
Setup and Installation
Install the library via pip:
pip install price-parser-reworkd
Features
- Extract numeric price values from strings.
- Support for more than 60 currencies
- Detect currency symbols and convert them into readable currency names.
- Built on Pydantic for easy integration with modern Python projects.
- Lightweight and production-ready.
- Can handle price unavailable phrases
Usage
ParserTypeCurrency
Extract both the numeric price value and the currency details (symbol and name).
from pydantic import BaseModel
from price_parser import ParserTypeCurrency
class PriceModel(BaseModel):
price: ParserTypeCurrency
# Example 1: Valid input with currency symbol
price_info = PriceModel(price="$19.99")
print(price_info.price)
# Output: {'currency': 'USD', 'currency_symbol': '$', 'amount': 19.99}
# Example 2: Valid input without a currency
price_info = PriceModel(price=1500.75)
print(price_info.price)
# Output: {'currency': None, 'currency_symbol': None, 'amount': 1500.75}
# Example 3: String with known currency name
price_info = PriceModel(price="EUR 49.99")
print(price_info.price)
# Output: {'currency': 'EUR', 'currency_symbol': '€', 'amount': 49.99}
Example Use Cases
Full Price Details
Extract both the price value and currency details:
from pydantic import BaseModel
from price_parser import ParserTypeCurrency
class CombinedModel(BaseModel):
full_price_details_USD: ParserTypeCurrency
full_price_details_EUR: ParserTypeCurrency
# Input data
data = {
"full_price_details_USD": "USD 19.99",
"full_price_details_EUR": "€1.234",
}
# Parse the data
price_info = CombinedModel(**data)
print(price_info.full_price_details_USD) # Output: {'currency': 'USD', 'currency_symbol': '$', 'amount': 19.99}
print(price_info.full_price_details_EUR) # Output: {'currency': 'EUR', 'currency_symbol': '€', 'amount': 1234.0}
E-Commerce Data Processing
Ideal for processing pricing data from product listings.
from pydantic import BaseModel
from price_parser import ParserTypeCurrency
class ProductModel(BaseModel):
price: ParserTypeCurrency
def parse_and_display_products(products):
"""
Parses a list of product prices using a Pydantic model and displays the extracted information.
Args:
products (list): A list of price strings to be parsed.
"""
for product in products:
try:
product_data = ProductModel(price=product)
info = product_data.price
print(
f"Price: {info['amount']}, Currency: {info['currency']}, Symbol: {info['currency_symbol']}"
)
except ValueError as e:
print(f"Error parsing product price '{product}': {e}")
products = ["$19.99", "€15.50", "₹1200"]
parse_and_display_products(products)
Tests
Run unit tests to verify functionality:
pytest
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve the package.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Running the Package
You can use the library directly in your Python projects. For development, clone the repository and use Poetry to manage dependencies:
git clone https://github.com/mohamedmamdouh22/price-parser.git
cd price-parser
poetry install
Happy Parsing! 💰
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file price_parser_reworkd-0.2.1.tar.gz
.
File metadata
- Download URL: price_parser_reworkd-0.2.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60c91ab2a862b36a60ec538a223c74ab16b156016349c3eadb343bcf8b3766e3 |
|
MD5 | 9f96a9ea300ba35ebd10e0c8ad696395 |
|
BLAKE2b-256 | 971b63861622f6267f717f7fd810c4f1523daebea7e82db82dac966c388417d7 |
File details
Details for the file price_parser_reworkd-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: price_parser_reworkd-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37a27a2c4efea9bbaefb8ab1d79f15e667313efcc717c45d4b7ffda8d0c00ffd |
|
MD5 | 042c766d27a99d140f74addde1d0ccd2 |
|
BLAKE2b-256 | 10e88791445a9573795a5f10a460dedad84d922e2bcb6336d8bea335f70bcb60 |