Skip to content

Commit

Permalink
Add long entity id support
Browse files Browse the repository at this point in the history
Fix #343

Co-Authored-By: Dennis Priskorn <dennis@priskorn.se>
  • Loading branch information
LeMyst and dpriskorn committed Jun 15, 2022
1 parent c1e08ca commit 8bdcfea
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 39 deletions.
3 changes: 3 additions & 0 deletions test/test_entity_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ def test_write_required_ref(self):
item = wbi.item.get('Q582')
item.claims.get('P2581')[0].references.references.pop()
assert item.write_required(base_filter=[BaseDataType(prop_nr='P2581')], use_refs=True)

def test_long_item_id(self):
assert wbi.item.get('Item:Q582').id == 'Q582'
3 changes: 3 additions & 0 deletions test/test_entity_lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ def test_get(self):

def test_get_json(self):
assert wbi.lexeme.get('L5').get_json()['forms'][0]['representations']['es']['value'] == 'pinos'

def test_long_item_id(self):
assert wbi.lexeme.get('Lexeme:L582').id == 'L582'
3 changes: 3 additions & 0 deletions test/test_entity_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ def test_get_json(self):

def test_create_property(self):
wbi.property.new(datatype='wikibase-item')

def test_long_item_id(self):
assert wbi.property.get('Property:P582').id == 'P582'
69 changes: 36 additions & 33 deletions test/test_wbi_core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest
from copy import deepcopy

from wikibaseintegrator import WikibaseIntegrator, datatypes
from wikibaseintegrator.datatypes import (URL, CommonsMedia, ExternalID, Form, GeoShape, GlobeCoordinate, Lexeme, Math, MonolingualText, MusicalNotation, Property, Quantity,
from wikibaseintegrator import WikibaseIntegrator
from wikibaseintegrator.datatypes import (URL, CommonsMedia, ExternalID, Form, GeoShape, GlobeCoordinate, Item, Lexeme, Math, MonolingualText, MusicalNotation, Property, Quantity,
Sense, String, TabularData, Time)
from wikibaseintegrator.datatypes.extra import EDTF, LocalMedia
from wikibaseintegrator.entities import ItemEntity
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_get(self):
assert item.labels.get('es') == "Tierra"

def test_basedatatype_action_if_exists(self):
instances = [datatypes.Item(prop_nr='P31', value='Q1234'), datatypes.Item(prop_nr='P31', value='Q1234')]
instances = [Item(prop_nr='P31', value='Q1234'), Item(prop_nr='P31', value='Q1234')]
item_original = wbi.item.get('Q2')
len_claims_original = len([x.mainsnak.datavalue['value']['id'] for x in item_original.claims.get('P31')])

Expand Down Expand Up @@ -214,36 +214,39 @@ def test_new_item_creation(self):
String(value='test1', prop_nr='P1'),
String(value='test2', prop_nr='1'),
String(value='test3', prop_nr=1),
Math(value="xxx", prop_nr="P2"),
ExternalID(value="xxx", prop_nr="P3"),
datatypes.Item(value="Q123", prop_nr="P4"),
datatypes.Item(value="123", prop_nr="P4"),
datatypes.Item(value=123, prop_nr="P4"),
Time(time='-0458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr="P5"),
Time(time='+458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr="P5"),
Time(time='+2021-01-01T00:00:00Z', before=1, after=2, precision=3, timezone=4, prop_nr="P5"),
Time(time='now', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr="P5"),
URL(value="http://www.wikidata.org", prop_nr="P6"),
URL(value="https://www.wikidata.org", prop_nr="P6"),
URL(value="ftp://example.com", prop_nr="P6"),
URL(value="ssh://user@server/project.git", prop_nr="P6"),
URL(value="svn+ssh://user@server:8888/path", prop_nr="P6"),
MonolingualText(text="xxx", language="fr", prop_nr="P7"),
Quantity(amount=-5.04, prop_nr="P8"),
Quantity(amount=5.06, upper_bound=9.99, lower_bound=-2.22, unit="Q11573", prop_nr="P8"),
CommonsMedia(value="xxx", prop_nr="P9"),
GlobeCoordinate(latitude=1.2345, longitude=-1.2345, precision=12, prop_nr="P10"),
GeoShape(value="Data:xxx.map", prop_nr="P11"),
Property(value="P123", prop_nr="P12"),
Property(value="123", prop_nr="P12"),
Property(value=123, prop_nr="P12"),
TabularData(value="Data:Taipei+Population.tab", prop_nr="P13"),
MusicalNotation(value="\relative c' { c d e f | g2 g | a4 a a a | g1 |}", prop_nr="P14"),
Lexeme(value="L123", prop_nr="P15"),
Lexeme(value="123", prop_nr="P15"),
Lexeme(value=123, prop_nr="P15"),
Form(value="L123-F123", prop_nr="P16"),
Sense(value="L123-S123", prop_nr="P17")
Math(value='xxx', prop_nr='P2'),
ExternalID(value='xxx', prop_nr='P3'),
Item(value='Q123', prop_nr='P4'),
Item(value='123', prop_nr='P4'),
Item(value=123, prop_nr='P4'),
Item(value='Item:Q123', prop_nr='P4'),
Time(time='-0458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
Time(time='+458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
Time(time='+2021-01-01T00:00:00Z', before=1, after=2, precision=3, timezone=4, prop_nr='P5'),
Time(time='now', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
URL(value="http://www.wikidata.org", prop_nr='P6'),
URL(value="https://www.wikidata.org", prop_nr='P6'),
URL(value="ftp://example.com", prop_nr='P6'),
URL(value="ssh://user@server/project.git", prop_nr='P6'),
URL(value="svn+ssh://user@server:8888/path", prop_nr='P6'),
MonolingualText(text='xxx', language='fr', prop_nr='P7'),
Quantity(amount=-5.04, prop_nr='P8'),
Quantity(amount=5.06, upper_bound=9.99, lower_bound=-2.22, unit='Q11573', prop_nr='P8'),
CommonsMedia(value='xxx', prop_nr='P9'),
GlobeCoordinate(latitude=1.2345, longitude=-1.2345, precision=12, prop_nr='P10'),
GeoShape(value='Data:xxx.map', prop_nr='P11'),
Property(value='P123', prop_nr='P12'),
Property(value='123', prop_nr='P12'),
Property(value=123, prop_nr='P12'),
Property(value='Property:P123', prop_nr='P12'),
TabularData(value="Data:Taipei+Population.tab", prop_nr='P13'),
MusicalNotation(value="\relative c' { c d e f | g2 g | a4 a a a | g1 |}", prop_nr='P14'),
Lexeme(value='L123', prop_nr='P15'),
Lexeme(value='123', prop_nr='P15'),
Lexeme(value=123, prop_nr='P15'),
Lexeme(value='Lexeme:L123', prop_nr='P15'),
Form(value='L123-F123', prop_nr='P16'),
Sense(value='L123-S123', prop_nr='P17')
]

for d in data:
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/datatypes/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def set_value(self, value: Union[str, int] = None):

if value:
if isinstance(value, str):
pattern = re.compile(r'^Q?([0-9]+)$')
pattern = re.compile(r'^(?:[a-zA-Z]+:)?Q?([0-9]+)$')
matches = pattern.match(value)

if not matches:
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/datatypes/lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def set_value(self, value: Union[str, int] = None):

if value:
if isinstance(value, str):
pattern = re.compile(r'^L?([0-9]+)$')
pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$')
matches = pattern.match(value)

if not matches:
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/datatypes/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def set_value(self, value: Union[str, int] = None):

if value:
if isinstance(value, str):
pattern = re.compile(r'^P?([0-9]+)$')
pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$')
matches = pattern.match(value)

if not matches:
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/entities/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get(self, entity_id: Union[str, int] = None, **kwargs: Any) -> ItemEntity:
raise ValueError("You must provide an entity_id")

if isinstance(entity_id, str):
pattern = re.compile(r'^Q?([0-9]+)$')
pattern = re.compile(r'^(?:[a-zA-Z]+:)?Q?([0-9]+)$')
matches = pattern.match(entity_id)

if not matches:
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/entities/lexeme.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def new(self, **kwargs: Any) -> LexemeEntity:

def get(self, entity_id: Union[str, int], **kwargs: Any) -> LexemeEntity:
if isinstance(entity_id, str):
pattern = re.compile(r'^L?([0-9]+)$')
pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$')
matches = pattern.match(entity_id)

if not matches:
Expand Down
2 changes: 1 addition & 1 deletion wikibaseintegrator/entities/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def new(self, **kwargs: Any) -> PropertyEntity:

def get(self, entity_id: Union[str, int], **kwargs: Any) -> PropertyEntity:
if isinstance(entity_id, str):
pattern = re.compile(r'^P?([0-9]+)$')
pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$')
matches = pattern.match(entity_id)

if not matches:
Expand Down

1 comment on commit 8bdcfea

@dpriskorn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Please sign in to comment.