Python - Also allow creating Tokenizer from_buffer

This commit is contained in:
Anthony MOI
2020-05-21 16:33:52 -04:00
parent 0e890d0d05
commit c205afe7a5
2 changed files with 24 additions and 0 deletions

View File

@@ -253,6 +253,18 @@ impl Tokenizer {
})
}
#[staticmethod]
fn from_buffer(buffer: &PyBytes) -> PyResult<Self> {
let tokenizer: tk::tokenizer::Tokenizer = serde_json::from_slice(buffer.as_bytes())
.map_err(|e| {
exceptions::Exception::py_err(format!(
"Cannot instantiate Tokenizer from buffer: {}",
e.to_string()
))
})?;
Ok(Self { tokenizer })
}
#[args(pretty = false)]
fn to_str(&self, pretty: bool) -> PyResult<String> {
ToPyResult(self.tokenizer.to_string(pretty)).into()

View File

@@ -272,6 +272,18 @@ class Tokenizer:
path: str:
Path to a file containing a Tokenizer
Returns:
Tokenizer
"""
pass
@staticmethod
def from_buffer(buffer: bytes) -> Tokenizer:
""" Instantiate a new Tokenizer from the given buffer
Args:
buffer: bytes:
A buffer used to instantiate a new Tokenizer
Returns:
Tokenizer
"""