Skip to contentSkip to conversation

qapi: provide a friendly string representation of QAPI classes

PATCH Applied Daniel P. Berrangé 3 participants 5 messages Oct 18, 2023 – Oct 19, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 qapi: provide a friendly string representation of QAPI classes

If printing a QAPI schema object for debugging we get the classname and a hex value for the instance:

<qapi.schema.QAPISchemaEnumType object at 0x7f0ab4c2dad0> <qapi.schema.QAPISchemaObjectType object at 0x7f0ab4c2dd90> <qapi.schema.QAPISchemaArrayType object at 0x7f0ab4c2df90>

With this change we instead get the classname and the human friendly name of the QAPI type instance:

<QAPISchemaEnumType:CpuS390State at 0x7f0ab4c2dad0> <QAPISchemaObjectType:CpuInfoS390 at 0x7f0ab4c2dd90> <QAPISchemaArrayType:CpuInfoFastList at 0x7f0ab4c2df90>

---

Changed in v3:

- Retain the object hex ID in the new representation

 scripts/qapi/schema.py | 6 ++++++
 1 file changed, 6 insertions(+)
scripts/qapi/schema.py +6 -0
index 231ebf61ba..39c11bb52a 100644
@@ -73,6 +73,12 @@ def __init__(self, name: str, info, doc, ifcond=None, features=None):
         self.features = features or []
         self._checked = False
 
+    def __repr__(self):
+        if self.name is not None:
+            return "<%s:%s at 0x%x>" % (type(self).__name__, self.name, id(self))
+        else:
+            return "<%s at 0x%x>" % (type(self).__name__, id(self))
+
     def c_name(self):
         return c_name(self.name)
 
Signature · 1 line
2.41.0
#1 lore raw
Quoted 30 lines · Daniel P. Berrangé

If printing a QAPI schema object for debugging we get the classname and a hex value for the instance:

<qapi.schema.QAPISchemaEnumType object at 0x7f0ab4c2dad0> <qapi.schema.QAPISchemaObjectType object at 0x7f0ab4c2dd90> <qapi.schema.QAPISchemaArrayType object at 0x7f0ab4c2df90>

With this change we instead get the classname and the human friendly name of the QAPI type instance:

<QAPISchemaEnumType:CpuS390State at 0x7f0ab4c2dad0> <QAPISchemaObjectType:CpuInfoS390 at 0x7f0ab4c2dd90> <QAPISchemaArrayType:CpuInfoFastList at 0x7f0ab4c2df90>

---

Changed in v3:

- Retain the object hex ID in the new representation

 scripts/qapi/schema.py | 6 ++++++
 1 file changed, 6 insertions(+)
scripts/qapi/schema.py +0 -0
index 231ebf61ba..39c11bb52a 100644
@@ -73,6 +73,12 @@ def __init__(self, name: str, info, doc, ifcond=None, features=None):
         self.features = features or []

scripts/qapi/schema.py def __init__(self, name: str, info, doc, ifcond=None, features=None): · line 83

Quoted 10 lines
scripts/qapi/schema.py +6 -0
         self._checked = False
 
+    def __repr__(self):
+        if self.name is not None:
+            return "<%s:%s at 0x%x>" % (type(self).__name__, self.name, id(self))
+        else:
+            return "<%s at 0x%x>" % (type(self).__name__, id(self))
+
     def c_name(self):
         return c_name(self.name)

Looks good now.

Except I generally prefer

if COND:
SUITE1
else:
SUITE2

over

if not COND:
SUITE2
else:
SUITE1

because it avoids mental double-negation.

Mind if I swap things? Like so:

    def __repr__(self):
        if self.name is None:
            return "<%s at 0x%x>" % (type(self).__name__, id(self))
        else:
            return "<%s:%s at 0x%x>" % (type(self).__name__,
                                        self.name, id(self))
#2 lore raw
Quoted 61 lines · Markus Armbruster
Quoted 30 lines · Daniel P. Berrangé

If printing a QAPI schema object for debugging we get the classname and a hex value for the instance:

<qapi.schema.QAPISchemaEnumType object at 0x7f0ab4c2dad0> <qapi.schema.QAPISchemaObjectType object at 0x7f0ab4c2dd90> <qapi.schema.QAPISchemaArrayType object at 0x7f0ab4c2df90>

With this change we instead get the classname and the human friendly name of the QAPI type instance:

<QAPISchemaEnumType:CpuS390State at 0x7f0ab4c2dad0> <QAPISchemaObjectType:CpuInfoS390 at 0x7f0ab4c2dd90> <QAPISchemaArrayType:CpuInfoFastList at 0x7f0ab4c2df90>

---

Changed in v3:

- Retain the object hex ID in the new representation

 scripts/qapi/schema.py | 6 ++++++
 1 file changed, 6 insertions(+)
scripts/qapi/schema.py +0 -0
index 231ebf61ba..39c11bb52a 100644
@@ -73,6 +73,12 @@ def __init__(self, name: str, info, doc, ifcond=None, features=None):
         self.features = features or []

scripts/qapi/schema.py def __init__(self, name: str, info, doc, ifcond=None, features=None): · line 83

Quoted 10 lines
scripts/qapi/schema.py +6 -0
         self._checked = False
 
+    def __repr__(self):
+        if self.name is not None:
+            return "<%s:%s at 0x%x>" % (type(self).__name__, self.name, id(self))
+        else:
+            return "<%s at 0x%x>" % (type(self).__name__, id(self))
+
     def c_name(self):
         return c_name(self.name)

Looks good now.

Except I generally prefer

if COND:
SUITE1
else:
SUITE2

over

if not COND:
SUITE2
else:
SUITE1

because it avoids mental double-negation.

Quoted 8 lines

Mind if I swap things? Like so:

    def __repr__(self):
        if self.name is None:
            return "<%s at 0x%x>" % (type(self).__name__, id(self))
        else:
            return "<%s:%s at 0x%x>" % (type(self).__name__,
                                        self.name, id(self))

Sure, fine with me.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 21 lines · Daniel P. Berrangé
Quoted 18 lines · Markus Armbruster
Quoted 16 lines · Daniel P. Berrangé

If printing a QAPI schema object for debugging we get the classname and a hex value for the instance:

   <qapi.schema.QAPISchemaEnumType object at 0x7f0ab4c2dad0>
   <qapi.schema.QAPISchemaObjectType object at 0x7f0ab4c2dd90>
   <qapi.schema.QAPISchemaArrayType object at 0x7f0ab4c2df90>

With this change we instead get the classname and the human friendly name of the QAPI type instance:

   <QAPISchemaEnumType:CpuS390State at 0x7f0ab4c2dad0>
   <QAPISchemaObjectType:CpuInfoS390 at 0x7f0ab4c2dd90>
   <QAPISchemaArrayType:CpuInfoFastList at 0x7f0ab4c2df90>

---

Quoted 8 lines
Quoted 8 lines

Mind if I swap things? Like so:

     def __repr__(self):
         if self.name is None:
             return "<%s at 0x%x>" % (type(self).__name__, id(self))
         else:
             return "<%s:%s at 0x%x>" % (type(self).__name__,
                                         self.name, id(self))
Quoted 5 lines

Sure, fine with me.

With regards,
Daniel

#4 lore raw
Quoted 24 lines · Philippe Mathieu-Daudé
Quoted 23 lines · Daniel P. Berrangé
Quoted 22 lines · Markus Armbruster
Quoted 10 lines · Daniel P. Berrangé

If printing a QAPI schema object for debugging we get the classname and a hex value for the instance:

   <qapi.schema.QAPISchemaEnumType object at 0x7f0ab4c2dad0>
   <qapi.schema.QAPISchemaObjectType object at 0x7f0ab4c2dd90>
   <qapi.schema.QAPISchemaArrayType object at 0x7f0ab4c2df90>

With this change we instead get the classname and the human friendly name of the QAPI type instance:

Quoted 6 lines
   <QAPISchemaEnumType:CpuS390State at 0x7f0ab4c2dad0>
   <QAPISchemaObjectType:CpuInfoS390 at 0x7f0ab4c2dd90>
   <QAPISchemaArrayType:CpuInfoFastList at 0x7f0ab4c2df90>

---

Mind if I swap things? Like so:

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
     def __repr__(self):
         if self.name is None:
             return "<%s at 0x%x>" % (type(self).__name__, id(self))
         else:
             return "<%s:%s at 0x%x>" % (type(self).__name__,
                                         self.name, id(self))

Actually, I'd like to make this

    def __repr__(self):
        if self.name is None:
            return "<%s at 0x%x>" % (type(self).__name__, id(self))
        return "<%s:%s at 0x%x>" % type(self).__name__, self.name, id(self)

to avoid pylint's refactoring checker message

+scripts/qapi/schema.py:77:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return)

Queued. Thanks!