Skip to content

Commit

Permalink
Python __repr__ (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn authored Nov 27, 2023
1 parent e88b6e5 commit 094a2e4
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 100 deletions.
12 changes: 7 additions & 5 deletions chia-bls/src/gtelement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl GTElement {
}
}
#[cfg(feature = "py-bindings")]
#[cfg_attr(feature = "py-bindings", pymethods)]
#[pymethods]
impl GTElement {
#[classattr]
#[pyo3(name = "SIZE")]
Expand All @@ -63,9 +63,8 @@ impl GTElement {
Ok(Self::from_bytes(&bytes))
}

pub fn __repr__(&self) -> String {
let bytes = self.to_bytes();
format!("<GTElement {}>", &hex::encode(bytes))
fn __str__(&self) -> pyo3::PyResult<String> {
Ok(hex::encode(self.to_bytes()))
}

pub fn __mul__(&self, rhs: &Self) -> Self {
Expand Down Expand Up @@ -137,7 +136,10 @@ impl FromJsonDict for GTElement {

impl fmt::Debug for GTElement {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&hex::encode(self.to_bytes()))
formatter.write_fmt(format_args!(
"<GTElement {}>",
&hex::encode(self.to_bytes())
))
}
}

Expand Down
17 changes: 11 additions & 6 deletions chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl PublicKey {
}

#[cfg(feature = "py-bindings")]
#[cfg_attr(feature = "py-bindings", pymethods)]
#[pymethods]
impl PublicKey {
#[classattr]
const SIZE: usize = 48;
Expand Down Expand Up @@ -187,9 +187,8 @@ impl PublicKey {
self.get_fingerprint()
}

pub fn __repr__(&self) -> String {
let bytes = self.to_bytes();
format!("<G1Element {}>", &hex::encode(bytes))
fn __str__(&self) -> pyo3::PyResult<String> {
Ok(hex::encode(self.to_bytes()))
}

pub fn __add__(&self, rhs: &Self) -> Self {
Expand Down Expand Up @@ -290,7 +289,10 @@ impl Add<&PublicKey> for PublicKey {

impl fmt::Debug for PublicKey {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&hex::encode(self.to_bytes()))
formatter.write_fmt(format_args!(
"<G1Element {}>",
&hex::encode(self.to_bytes())
))
}
}

Expand Down Expand Up @@ -602,7 +604,10 @@ mod tests {
let mut data = [0u8; 48];
data[0] = 0xc0;
let pk = PublicKey::from_bytes(&data).unwrap();
assert_eq!(format!("{:?}", pk), hex::encode(data));
assert_eq!(
format!("{:?}", pk),
format!("<G1Element {}>", hex::encode(data))
);
}

#[test]
Expand Down
14 changes: 8 additions & 6 deletions chia-bls/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ impl AddAssign<&SecretKey> for SecretKey {

impl fmt::Debug for SecretKey {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&hex::encode(self.to_bytes()))
formatter.write_fmt(format_args!(
"<PrivateKey {}>",
&hex::encode(self.to_bytes())
))
}
}

Expand Down Expand Up @@ -260,7 +263,7 @@ impl DerivableKey for SecretKey {
}

#[cfg(feature = "py-bindings")]
#[cfg_attr(feature = "py-bindings", pymethods)]
#[pymethods]
impl SecretKey {
#[classattr]
const PRIVATE_KEY_SIZE: usize = 32;
Expand All @@ -273,9 +276,8 @@ impl SecretKey {
self.public_key()
}

pub fn __repr__(&self) -> String {
let bytes = self.to_bytes();
format!("<PrivateKey {}>", &hex::encode(bytes))
fn __str__(&self) -> pyo3::PyResult<String> {
Ok(hex::encode(self.to_bytes()))
}
}

Expand Down Expand Up @@ -411,7 +413,7 @@ mod tests {
fn test_debug() {
let sk_hex = "52d75c4707e39595b27314547f9723e5530c01198af3fc5849d9a7af65631efb";
let sk = SecretKey::from_bytes(&<[u8; 32]>::from_hex(sk_hex).unwrap()).unwrap();
assert_eq!(format!("{:?}", sk), sk_hex);
assert_eq!(format!("{:?}", sk), format!("<PrivateKey {}>", sk_hex));
}

#[test]
Expand Down
17 changes: 11 additions & 6 deletions chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ impl Hash for Signature {

impl fmt::Debug for Signature {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&hex::encode(self.to_bytes()))
formatter.write_fmt(format_args!(
"<G2Element {}>",
&hex::encode(self.to_bytes())
))
}
}

Expand Down Expand Up @@ -272,7 +275,7 @@ impl<N> ToClvm<N> for Signature {
}

#[cfg(feature = "py-bindings")]
#[cfg_attr(feature = "py-bindings", pymethods)]
#[pymethods]
impl Signature {
#[classattr]
const SIZE: usize = 96;
Expand All @@ -299,9 +302,8 @@ impl Signature {
Self::generator()
}

pub fn __repr__(&self) -> String {
let bytes = self.to_bytes();
format!("<G2Element {}>", &hex::encode(bytes))
fn __str__(&self) -> pyo3::PyResult<String> {
Ok(hex::encode(self.to_bytes()))
}

pub fn __add__(&self, rhs: &Self) -> Self {
Expand Down Expand Up @@ -1057,7 +1059,10 @@ mod tests {
let mut data = [0u8; 96];
data[0] = 0xc0;
let sig = Signature::from_bytes(&data).unwrap();
assert_eq!(format!("{:?}", sig), hex::encode(data));
assert_eq!(
format!("{:?}", sig),
format!("<G2Element {}>", hex::encode(data))
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion chia-protocol/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Coin {
}

#[cfg(feature = "py-bindings")]
#[cfg_attr(feature = "py-bindings", pymethods)]
#[pymethods]
impl Coin {
fn name<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<&'p pyo3::types::PyBytes> {
Ok(pyo3::types::PyBytes::new(py, &self.coin_id()))
Expand Down
4 changes: 2 additions & 2 deletions chia_py_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
let mut py_protocol = quote! {
#[pyo3::pymethods]
impl #ident {
fn __str__(&self) -> pyo3::PyResult<String> {
Ok(format!("{:?}", self))
fn __repr__(&self) -> pyo3::PyResult<String> {
Ok(format!("{self:?}"))
}

fn __richcmp__(&self, other: pyo3::PyRef<Self>, op: pyo3::class::basic::CompareOp) -> pyo3::Py<pyo3::PyAny> {
Expand Down
Loading

0 comments on commit 094a2e4

Please sign in to comment.