External applications are expected to interact with classes and functions defined here.
Schemas
The module contains data schemas for use by external application
Builders
The module also contains data builders for use by external application
LemmaQueryBuilder
A query builder to fetch lemma data and its relationships.
Important
If lemma
and konsep
is both None
, it will raise a ValueError
.
Parameters:
Name |
Type |
Description |
Default |
lemma |
Optional[str]
|
The lemma to match to. Defaults to None. |
None
|
konsep |
Optional[str]
|
The query contains these words in the konsep. Defaults to None. |
None
|
Source code in samudra/interfaces.py
| class LemmaQueryBuilder:
"""A query builder to fetch lemma data and its relationships.
!!! important
If `lemma` and `konsep` is both `None`, it will raise a `ValueError`.
Args:
lemma (Optional[str], optional): The lemma to match to. Defaults to None.
konsep (Optional[str], optional): The query contains these words in the konsep. Defaults to None.
"""
_query_stmt = models.Lemma.select()
def __init__(
self, *, lemma: Optional[str] = None, konsep: Optional[str] = None
) -> None:
if (lemma == None) and (konsep == None):
whereclause = None
raise ValueError(
f"Please specify query. `lemma` dan `konsep` cannot both be None."
)
elif lemma and konsep:
whereclause = models.Lemma.nama.contains(
lemma
) or models.Konsep.keterangan.contains(konsep)
elif lemma:
whereclause = models.Lemma.nama.contains(lemma)
elif konsep:
whereclause = models.Konsep.keterangan.contains(konsep)
self._query_stmt = (
self._query_stmt.join_from(models.Lemma, models.Konsep, JOIN.LEFT_OUTER)
.where(whereclause)
.join_from(models.Konsep, models.GolonganKata, JOIN.LEFT_OUTER)
)
def get_cakupan(self) -> "LemmaQueryBuilder":
"""Fetch related `cakupan`.
Returns:
LemmaQueryBuilder: Returns self to continue building the query
"""
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.CakupanXKonsep, JOIN.LEFT_OUTER
).join(models.Cakupan, JOIN.LEFT_OUTER)
return self
def get_kata_asing(self) -> "LemmaQueryBuilder":
"""Fetch related `kata_asing`.
Returns:
LemmaQueryBuilder: Returns self to continue building the query
"""
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.KataAsingXKonsep, JOIN.LEFT_OUTER
).join(models.KataAsing, JOIN.LEFT_OUTER)
return self
def collect(self) -> Optional[LemmaData]:
"""Execute the query
Returns:
Optional[LemmaData]: Returns None if data matching query does not exist.
"""
try:
return LemmaData.from_orm(pw.prefetch(self._query_stmt.get()))
except pw.DoesNotExist:
return None
|
collect()
Execute the query
Returns:
Type |
Description |
Optional[LemmaData]
|
Optional[LemmaData]: Returns None if data matching query does not exist. |
Source code in samudra/interfaces.py
| def collect(self) -> Optional[LemmaData]:
"""Execute the query
Returns:
Optional[LemmaData]: Returns None if data matching query does not exist.
"""
try:
return LemmaData.from_orm(pw.prefetch(self._query_stmt.get()))
except pw.DoesNotExist:
return None
|
get_cakupan()
Fetch related cakupan
.
Returns:
Name | Type |
Description |
LemmaQueryBuilder |
LemmaQueryBuilder
|
Returns self to continue building the query |
Source code in samudra/interfaces.py
| def get_cakupan(self) -> "LemmaQueryBuilder":
"""Fetch related `cakupan`.
Returns:
LemmaQueryBuilder: Returns self to continue building the query
"""
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.CakupanXKonsep, JOIN.LEFT_OUTER
).join(models.Cakupan, JOIN.LEFT_OUTER)
return self
|
get_kata_asing()
Fetch related kata_asing
.
Returns:
Name | Type |
Description |
LemmaQueryBuilder |
LemmaQueryBuilder
|
Returns self to continue building the query |
Source code in samudra/interfaces.py
| def get_kata_asing(self) -> "LemmaQueryBuilder":
"""Fetch related `kata_asing`.
Returns:
LemmaQueryBuilder: Returns self to continue building the query
"""
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.KataAsingXKonsep, JOIN.LEFT_OUTER
).join(models.KataAsing, JOIN.LEFT_OUTER)
return self
|
NewLemmaBuilder
A builder to insert new lemma and its related data
Parameters:
Name |
Type |
Description |
Default |
lemma |
str
|
lemma name |
required
|
konsep |
str
|
konsep detail |
required
|
golongan |
str
|
existing GolonganKata |
required
|
Source code in samudra/interfaces.py
| class NewLemmaBuilder:
"""A builder to insert new lemma and its related data
Args:
lemma (str): lemma name
konsep (str): konsep detail
golongan (str): existing [GolonganKata][samudra.models.core.konsep.GolonganKata]
"""
def __init__(self, konsep: str, lemma: str, golongan: str) -> None:
self.lemma = get_or_init_record(models.Lemma, nama=lemma)
self.golongan = models.GolonganKata.get(id=golongan)
self.konsep = get_or_init_record(
models.Konsep, lemma=self.lemma, golongan=self.golongan, keterangan=konsep
)
self.to_save: List[pw.Model] = [self.lemma, self.golongan, self.konsep]
def save(self) -> None:
"""Saves the built record."""
while len(self.to_save) != 0:
record = self.to_save.pop(0)
record.update() # Fill previously NULL values
record.save()
def set_cakupan(self, nama: str) -> "NewLemmaBuilder":
"""Attach the cakupan with the following nama
Args:
nama (str): cakupan name
Returns:
NewLemmaBuilder: Returns self to continue building
"""
self.cakupan = get_or_init_record(models.Cakupan, nama=nama)
self.cakupan_x_konsep = get_or_init_record(
models.CakupanXKonsep, cakupan=self.cakupan, konsep=self.konsep
)
self.to_save.extend([self.cakupan, self.cakupan_x_konsep])
return self
def set_kata_asing(self, nama: str, bahasa: str) -> "NewLemmaBuilder":
"""Attach the kata_asing with the following description
Args:
nama (str): foreign word
bahasa (str): language of the word
Returns:
NewLemmaBuilder: Returns self to continue building
"""
self.kata_asing = get_or_init_record(models.KataAsing, nama=nama, bahasa=bahasa)
self.kata_asing_x_konsep = get_or_init_record(
models.KataAsingXKonsep, kata_asing=self.cakupan, konsep=self.konsep
)
self.to_save.extend([self.kata_asing, self.kata_asing_x_konsep])
return self
|
save()
Saves the built record.
Source code in samudra/interfaces.py
| def save(self) -> None:
"""Saves the built record."""
while len(self.to_save) != 0:
record = self.to_save.pop(0)
record.update() # Fill previously NULL values
record.save()
|
set_cakupan(nama)
Attach the cakupan with the following nama
Parameters:
Name |
Type |
Description |
Default |
nama |
str
|
cakupan name |
required
|
Returns:
Name | Type |
Description |
NewLemmaBuilder |
NewLemmaBuilder
|
Returns self to continue building |
Source code in samudra/interfaces.py
| def set_cakupan(self, nama: str) -> "NewLemmaBuilder":
"""Attach the cakupan with the following nama
Args:
nama (str): cakupan name
Returns:
NewLemmaBuilder: Returns self to continue building
"""
self.cakupan = get_or_init_record(models.Cakupan, nama=nama)
self.cakupan_x_konsep = get_or_init_record(
models.CakupanXKonsep, cakupan=self.cakupan, konsep=self.konsep
)
self.to_save.extend([self.cakupan, self.cakupan_x_konsep])
return self
|
set_kata_asing(nama, bahasa)
Attach the kata_asing with the following description
Parameters:
Name |
Type |
Description |
Default |
nama |
str
|
foreign word |
required
|
bahasa |
str
|
language of the word |
required
|
Returns:
Name | Type |
Description |
NewLemmaBuilder |
NewLemmaBuilder
|
Returns self to continue building |
Source code in samudra/interfaces.py
| def set_kata_asing(self, nama: str, bahasa: str) -> "NewLemmaBuilder":
"""Attach the kata_asing with the following description
Args:
nama (str): foreign word
bahasa (str): language of the word
Returns:
NewLemmaBuilder: Returns self to continue building
"""
self.kata_asing = get_or_init_record(models.KataAsing, nama=nama, bahasa=bahasa)
self.kata_asing_x_konsep = get_or_init_record(
models.KataAsingXKonsep, kata_asing=self.cakupan, konsep=self.konsep
)
self.to_save.extend([self.kata_asing, self.kata_asing_x_konsep])
return self
|
get_or_init_record(model, *args, **kwargs)
Gets a record or initializes a new one without saving
Parameters:
Name |
Type |
Description |
Default |
model |
pw.Model
|
A model |
required
|
Returns:
Type |
Description |
pw.Model
|
pw.Model: Model instance |
Source code in samudra/interfaces.py
| def get_or_init_record(model: pw.Model, *args, **kwargs) -> pw.Model:
"""Gets a record or initializes a new one without saving
Args:
model (pw.Model): A model
Returns:
pw.Model: Model instance
"""
if data := model.get_or_none(*args, **kwargs) is None:
data = model(*args, **kwargs)
return data
|