Skip to content

Commit

Permalink
Add fragment property to URIRef (#1991)
Browse files Browse the repository at this point in the history
Added a property `fragment` to `URIRef` so it is easy to access the URI's fragment:

```python
>>> URIRef("http://example.com/some/path/#some-fragment").fragment
'some-fragment'
>>> URIRef("http://example.com/some/path/").fragment
''
```

## Why is this useful?

The non-fragment part of the URI often just serves a namespace purpose, e.g. `https://brickschema.org/schema/Brick#`.

Actual "things" use that namespace, and have the "thing" name as the fragment, e.g. `https://brickschema.org/schema/Brick#Valve_Position_Sensor`

For display purposes, it's nice to get to the "thing" name, i.e. the fragment easily.
  • Loading branch information
ottokruse committed Jun 17, 2022
1 parent d263009 commit a6e3da4
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rdflib/term.py
Expand Up @@ -314,6 +314,18 @@ def defrag(self) -> "URIRef":
else:
return self

@property
def fragment(self) -> str:
"""
Return the URL Fragment
>>> URIRef("http://example.com/some/path/#some-fragment").fragment
'some-fragment'
>>> URIRef("http://example.com/some/path/").fragment
''
"""
return urlparse(self).fragment

def __reduce__(self) -> Tuple[Type["URIRef"], Tuple[str]]:
return (URIRef, (str(self),))

Expand Down

0 comments on commit a6e3da4

Please sign in to comment.