def test_init_params(self): p1 = model.Person(ident="urn:uuid:1234") self.assertEqual(p1.id, "urn:uuid:1234") p2 = model.Person(ident="http://schema.org/Foo") self.assertEqual(p2.id, "schema:Foo") p3 = model.Name(content="Test") self.assertEqual(p3.content, "Test") c = model.MonetaryAmount(value=10) self.assertEqual(c.value, 10) n = model.Name(value="Rob") self.assertEqual(n.content, "Rob") i = model.Identifier(content="xyz123") self.assertEqual(i.content, "xyz123") i2 = model.Identifier(value="abc") self.assertEqual(i2.content, "abc")
def extract_monetary_amount(data, add_citations=False, currency_mapping=CURRENCY_MAPPING, source_mapping=None, truncate_label_digits=2): ''' Returns a `MonetaryAmount`, `StartingPrice`, or `EstimatedPrice` object based on properties of the supplied `data` dict. If no amount or currency data is found in found, returns `None`. For `EstimatedPrice`, values will be accessed from these keys: - amount: `est_price_amount` or `est_price` - currency: `est_price_currency` or `est_price_curr` - note: `est_price_note` or `est_price_desc` - bibliographic statement: `est_price_citation` For `StartingPrice`, values will be accessed from these keys: - amount: `start_price_amount` or `start_price` - currency: `start_price_currency` or `start_price_curr` - note: `start_price_note` or `start_price_desc` - bibliographic statement: `start_price_citation` For `MonetaryAmount` prices, values will be accessed from these keys: - amount: `price_amount` or `price` - currency: `price_currency` or `price_curr` - note: `price_note` or `price_desc` - bibliographic statement: `price_citation` ''' amount_type = 'Price' if 'price' in data or 'price_amount' in data or 'amount' in data: amnt = model.MonetaryAmount(ident='') price_amount = data.get('price_amount', data.get('price', data.get('amount'))) price_currency = data.get( 'currency', data.get('price_currency', data.get('price_curr'))) note = data.get('price_note', data.get('price_desc', data.get('note'))) cite = data.get('price_citation', data.get('citation')) source = data.get('price_source', '') elif 'est_price' in data or 'est_price_amount' in data: amnt = vocab.EstimatedPrice(ident='') price_amount = data.get('est_price_amount', data.get('est_price')) price_currency = data.get( 'est_price_currency', data.get('est_price_curr', data.get('currency'))) amount_type = 'Estimated Price' note = data.get('est_price_note', data.get('est_price_desc', data.get('note'))) cite = data.get('est_price_citation', data.get('citation')) source = data.get('est_price_source', data.get('est_price_so', '')) elif 'start_price' in data or 'start_price_amount' in data: amnt = vocab.StartingPrice(ident='') price_amount = data.get('start_price_amount', data.get('start_price')) price_currency = data.get( 'start_price_currency', data.get('start_price_curr', data.get('currency'))) amount_type = 'Starting Price' note = data.get('start_price_note', data.get('start_price_desc', data.get('note'))) cite = data.get('start_price_citation', data.get('citation')) source = data.get('start_price_source', data.get('start_price_so', '')) elif 'ask_price' in data or 'ask_price_amount' in data: amnt = vocab.AskingPrice(ident='') price_amount = data.get('ask_price_amount', data.get('ask_price')) price_currency = data.get( 'ask_price_currency', data.get('ask_price_curr', data.get('currency'))) amount_type = 'Asking Price' note = data.get('ask_price_note', data.get('ask_price_desc', data.get('note'))) cite = data.get('ask_price_citation', data.get('citation')) source = data.get('ask_price_source', data.get('ask_price_so', '')) else: return None price_amount_label = price_amount if price_amount or price_currency: if cite and add_citations: amnt.referred_to_by = vocab.BibliographyStatement(ident='', content=cite) if note: amnt.referred_to_by = vocab.Note(ident='', content=note) if price_amount: try: value = price_amount value = value.replace('[?]', '') value = value.replace('?', '') value = value.strip() if re.search(re.compile(r',\d\d\d'), value): value = value.replace(',', '') value = float(value) label_fmt = '{:,.%df}' % truncate_label_digits price_amount_label = label_fmt.format(value) amnt.value = value except ValueError: amnt._label = price_amount amnt.identified_by = model.Name(ident='', content=price_amount) # warnings.warn(f'*** Not a numeric price amount: {value}') if price_currency: price_currency_key = price_currency try: price_currency_key = currency_mapping[ price_currency_key.lower()] except KeyError: pass if isinstance(price_currency_key, model.BaseResource): amnt.currency = price_currency_key elif price_currency_key in vocab.instances: amnt.currency = vocab.instances[price_currency_key] else: warnings.warn('*** No currency instance defined for %s' % (price_currency_key, )) if price_amount_label and price_currency: amnt._label = '%s %s' % (price_amount_label, price_currency) elif price_amount: amnt._label = '%s' % (price_amount, ) return amnt return None
def extract_monetary_amount(data): ''' Returns a `MonetaryAmount`, `StartingPrice`, or `EstimatedPrice` object based on properties of the supplied `data` dict. If no amount or currency data is found in found, returns `None`. For `EstimatedPrice`, values will be accessed from these keys: - amount: `est_price_amount` or `est_price` - currency: `est_price_currency` or `est_price_curr` - note: `est_price_note` or `est_price_desc` - bibliographic statement: `est_price_citation` For `StartingPrice`, values will be accessed from these keys: - amount: `start_price_amount` or `start_price` - currency: `start_price_currency` or `start_price_curr` - note: `start_price_note` or `start_price_desc` - bibliographic statement: `start_price_citation` For `MonetaryAmount` prices, values will be accessed from these keys: - amount: `price_amount` or `price` - currency: `price_currency` or `price_curr` - note: `price_note` or `price_desc` - bibliographic statement: `price_citation` ''' amount_type = 'Price' if 'est_price' in data: amnt = vocab.EstimatedPrice() price_amount = data.get('est_price_amount', data.get('est_price')) price_currency = data.get('est_price_currency', data.get('est_price_curr')) amount_type = 'Estimated Price' note = data.get('est_price_note', data.get('est_price_desc')) cite = data.get('est_price_citation') elif 'start_price' in data: amnt = vocab.StartingPrice() price_amount = data.get('start_price_amount', data.get('start_price')) price_currency = data.get('start_price_currency', data.get('start_price_curr')) amount_type = 'Starting Price' note = data.get('start_price_note', data.get('start_price_desc')) cite = data.get('start_price_citation') else: amnt = model.MonetaryAmount() price_amount = data.get('price_amount', data.get('price')) price_currency = data.get('price_currency', data.get('price_curr')) note = data.get('price_note', data.get('price_desc')) cite = data.get('price_citation') if price_amount or price_currency: if cite: amnt.referred_to_by = vocab.BibliographyStatement(content=cite) if note: amnt.referred_to_by = vocab.Note(content=note) if price_amount: try: value = price_amount value = value.replace('[?]', '') value = value.replace('?', '') value = value.strip() price_amount = float(value) amnt.value = price_amount except ValueError: amnt._label = price_amount amnt.identified_by = model.Name(content=price_amount) # warnings.warn(f'*** Not a numeric price amount: {value}') if price_currency: if price_currency in CURRENCY_MAPPING: try: price_currency = CURRENCY_MAPPING[price_currency.lower()] except KeyError: pass if price_currency in vocab.instances: amnt.currency = vocab.instances[price_currency] else: warnings.warn('*** No currency instance defined for %s' % (price_currency,)) if price_amount and price_currency: amnt._label = '%s %s' % (price_amount, price_currency) elif price_amount: amnt._label = '%s' % (price_amount,) return amnt return None
def test_add_classification(self): amnt = model.MonetaryAmount(ident='') amnt.value = 7.0 self.assertNotIn('Asking Price', factory.toString(amnt)) vocab.add_classification(amnt, vocab.AskingPrice) self.assertIn('Asking Price', factory.toString(amnt))