Source code for mimesis.providers.finance

# -*- coding: utf-8 -*-

"""Business data provider."""

from typing import Any, List

from mimesis.data import (
    CRYPTOCURRENCY_ISO_CODES,
    CRYPTOCURRENCY_SYMBOLS,
    CURRENCY_ISO_CODES,
    CURRENCY_SYMBOLS,
    STOCK_EXCHANGES,
    STOCK_NAMES,
    STOCK_TICKERS,
)
from mimesis.providers.base import BaseDataProvider

__all__ = ["Finance"]


[docs]class Finance(BaseDataProvider): """Class for generating finance data."""
[docs] def __init__(self, *args: Any, **kwargs: Any) -> None: """Initialize attributes. :param locale: Current locale. """ super().__init__(*args, **kwargs) self._datafile = "finance.json" self._pull(self._datafile)
[docs] class Meta: """Class for metadata.""" name = "finance"
[docs] def company(self) -> str: """Get a random company name. :return: Company name. """ names: List[str] = self.extract(["company", "name"]) return self.random.choice(names)
[docs] def company_type(self, abbr: bool = False) -> str: """Get a random type of business entity. :param abbr: Abbreviated company type. :return: Types of business entity. """ key = "abbr" if abbr else "title" company_types: List[str] = self.extract(["company", "type", key]) return self.random.choice(company_types)
[docs] def currency_iso_code(self, allow_random: bool = False) -> str: """Get code of the currency for current locale. :param allow_random: Get a random ISO code. :return: Currency code. """ code: str = self.extract(["currency-code"]) if allow_random: return self.random.choice(CURRENCY_ISO_CODES) return code
[docs] def cryptocurrency_iso_code(self) -> str: """Get symbol of random cryptocurrency. :return: Symbol of cryptocurrency. """ return self.random.choice(CRYPTOCURRENCY_ISO_CODES)
[docs] def currency_symbol(self) -> str: """Get a currency symbol for current locale. :return: Currency symbol. """ return CURRENCY_SYMBOLS[self.locale]
[docs] def cryptocurrency_symbol(self) -> str: """Get a cryptocurrency symbol. :return: Symbol of cryptocurrency. """ return self.random.choice(CRYPTOCURRENCY_SYMBOLS)
[docs] def price(self, minimum: float = 500, maximum: float = 1500) -> float: """Generate random price. :param minimum: Minimum value of price. :param maximum: Maximum value of price. :return: Price. """ return self.random.uniform( minimum, maximum, precision=2, )
[docs] def price_in_btc(self, minimum: float = 0, maximum: float = 2) -> float: """Generate random price in BTC. :param minimum: Minimum value of price. :param maximum: Maximum value of price. :return: Price in BTC. """ return self.random.uniform( minimum, maximum, precision=7, )
[docs] def stock_ticker(self) -> str: """Returns random stock ticker. :return: Ticker. """ return self.random.choice(STOCK_TICKERS)
[docs] def stock_name(self) -> str: """Returns stock name. :return: Stock name. """ return self.random.choice(STOCK_NAMES)
[docs] def stock_exchange(self) -> str: """Returns stock exchange name. :return: Returns exchange name. """ return self.random.choice(STOCK_EXCHANGES)