Skip to content

Commit bc19998

Browse files
authored
feat(bindings/python): Add repr for metadata (#5783)
* refactor(bindings/python)!: add repr for meta for python bindings for better print and debug Signed-off-by: yihong0618 <[email protected]> * fix: add tests for repr address comments Signed-off-by: yihong0618 <[email protected]> * fix: tests Signed-off-by: yihong0618 <[email protected]> * fix: flaky tests Signed-off-by: yihong0618 <[email protected]> * fix: address comments Signed-off-by: yihong0618 <[email protected]> --------- Signed-off-by: yihong0618 <[email protected]>
1 parent 2401802 commit bc19998

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

bindings/python/src/metadata.rs

+16
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,27 @@ impl Metadata {
9191
pub fn mode(&self) -> EntryMode {
9292
EntryMode(self.0.mode())
9393
}
94+
9495
/// Last modified time
9596
#[getter]
9697
pub fn last_modified(&self) -> Option<DateTime<Utc>> {
9798
self.0.last_modified()
9899
}
100+
pub fn __repr__(&self) -> String {
101+
let last_modified_str = match self.0.last_modified() {
102+
Some(dt) => dt.format("%Y-%m-%dT%H:%M:%S").to_string(),
103+
None => "None".to_string(),
104+
};
105+
106+
format!(
107+
"Metadata(mode={}, content_length={}, content_type={}, last_modified={}, etag={})",
108+
self.0.mode(),
109+
self.0.content_length(),
110+
self.0.content_type().unwrap_or("None"),
111+
last_modified_str,
112+
self.0.etag().unwrap_or("None"),
113+
)
114+
}
99115
}
100116

101117
#[pyclass(module = "opendal")]

bindings/python/tests/test_write.py

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def test_sync_write(service_name, operator, async_operator):
3636
assert metadata.mode.is_file()
3737
assert metadata.content_length == size
3838

39+
last_modified = (
40+
metadata.last_modified.strftime("%Y-%m-%dT%H:%M:%S")
41+
if metadata.last_modified
42+
else None
43+
)
44+
assert (
45+
repr(metadata)
46+
== f"Metadata(mode=file, content_length={metadata.content_length}, content_type={metadata.content_type}, last_modified={last_modified}, etag={metadata.etag})"
47+
)
48+
3949
operator.delete(filename)
4050

4151

@@ -51,6 +61,16 @@ def test_sync_write_path(service_name, operator, async_operator):
5161
assert metadata.mode.is_file()
5262
assert metadata.content_length == size
5363

64+
last_modified = (
65+
metadata.last_modified.strftime("%Y-%m-%dT%H:%M:%S")
66+
if metadata.last_modified
67+
else None
68+
)
69+
assert (
70+
repr(metadata)
71+
== f"Metadata(mode=file, content_length={metadata.content_length}, content_type={metadata.content_type}, last_modified={last_modified}, etag={metadata.etag})"
72+
)
73+
5474
operator.delete(filename)
5575

5676

0 commit comments

Comments
 (0)