Skip to main content

A simple price parser for extracting currency and value from strings (currently used as a Pydantic datatype only).

Project description

Price Parser Banner

💰 Price Parser: Extract Prices with Ease 💰

Price Parser

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.
  • Detect currency symbols and convert them into readable currency names.
  • Built on Pydantic for easy integration with modern Python projects.
  • Lightweight and production-ready.

Usage

ParserTypePrice

Extract the numeric price value from a string as a float.

from price_parser import ParserTypePrice

price = ParserTypePrice(price_string="$19.99")
print(price.value)  # Output: 19.99

ParserTypeCurrency

Extract both the numeric price value and the currency details (symbol and name).

from pydantic import BaseModel
from price_parser_reworkd 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

Single Price Extraction

Parse a single price from a string:

from pydantic import BaseModel
from price_parser_reworkd import ParserTypePrice

class PriceModel(BaseModel):
    price: ParserTypePrice()

# Example 1: Parse numeric price from a string
price_info = PriceModel(price="$19.99")
print(price_info.price)  # Output: 19.99

# Example 2: Parse numeric price from an integer
price_info = PriceModel(price=1500)
print(price_info.price)  # Output: 1500.0

# Example 3: Parse numeric price from a float
price_info = PriceModel(price=99.99)
print(price_info.price)  # Output: 99.99

# Example 4: Handle invalid or empty input
try:
    price_info = PriceModel(price="invalid input")
except ValueError as e:
    print(e)  # Output: ValueError with explanation

Full Price Details

Extract both the price value and currency details:

from pydantic import BaseModel
from price_parser_reworkd import ParserTypePrice, ParserTypeCurrency

class CombinedModel(BaseModel):
    numeric_price: ParserTypePrice()
    full_price_details: ParserTypeCurrency

# Input data
data = {
    "numeric_price": "$19.99",
    "full_price_details": "USD 19.99",
}

# Parse the data
price_info = CombinedModel(**data)
print(price_info.numeric_price)       # Output: 19.99
print(price_info.full_price_details)  # Output: {'currency': 'USD', 'currency_symbol': '$', 'amount': 19.99}

E-Commerce Data Processing

Ideal for processing pricing data from product listings.

from pydantic import BaseModel
from price_parser_reworkd 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-reworkd.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

price_parser_reworkd-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

price_parser_reworkd-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file price_parser_reworkd-0.1.0.tar.gz.

File metadata

  • Download URL: price_parser_reworkd-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for price_parser_reworkd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2da29560eaadd7df23c8c3ded958d144ecb871f3e90de4ad9cffd982a9dc409f
MD5 b11813fe331a331f40328c6036b1bfbe
BLAKE2b-256 d3673b376e8df321167dbdd03887d76cf1607776a4c3cc1b0800d5046923404b

See more details on using hashes here.

File details

Details for the file price_parser_reworkd-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for price_parser_reworkd-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70963f100a75fd3f2038d080fde45ee8259c40d6b903d81f9af0ff0da4bbe31d
MD5 70d43be2711aee8471c553345539c4dd
BLAKE2b-256 4baa5cdc575751006e75f25b5ef7aa4e85cc84c96f44115fa0ae9448088e22a2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page