rpm-software-management / dnf5

Next-generation RPM package management system
Other
250 stars 86 forks source link

python: add rich comparision methods to `libdnf5.rpm.Package` #1104

Open gotmax23 opened 10 months ago

gotmax23 commented 10 months ago

The libdnf C++ API provides a dnf_package_cmp function and the Python hawkey.Package binding in turn exposed rich comparison methods (__lt__, __gt__, __le__, and __ge__). I used this in Python with sorted() to print package query results in a consistent order. Would it be possible to implement the same thing in libdnf5's C++ API and Python bindings? I implemented this myself in fedrq1, but it'd be nice to have this in libdnf5 itself.

j-mracek commented 10 months ago

Thank you for the suggestion. SWIG automatically generates it if corresponding operators are available in C++. Right now only 3 operators are defined. The operator bool operator< should be implemented differently to provide requested functionality. It should compare name according to string, arch, and then EVR according to libsolv evr comparison.

class Package {
public:
    // @replaces libdnf:libdnf/hy-package.h:function:dnf_package_get_identical(DnfPackage * pkg)
    bool operator==(const Package & other) const noexcept;
    bool operator!=(const Package & other) const noexcept;
    bool operator<(const Package & other) const noexcept { return id < other.id; }

Additional information: https://github.com/swig/swig/blob/master/Lib/python/pyopers.swg