Skip to content

Instantly share code, notes, and snippets.

@thieu1995
Last active March 24, 2022 03:26
Show Gist options
  • Save thieu1995/dda27bd82fa4913f86a4bbd020a0c09b to your computer and use it in GitHub Desktop.
Save thieu1995/dda27bd82fa4913f86a4bbd020a0c09b to your computer and use it in GitHub Desktop.
from functools import total_ordering
@total_ordering
class WSN:
def __init__(self, mec_id, cpu_require, priority_level, generate_time, age):
self.mec_id = mec_id
self.cpu_require = cpu_require
self.priority_level = priority_level
self.generate_time = generate_time
self.age = age
def __str__(self):
return f"ID: {self.mec_id}, cpu: {self.cpu_require}, priority: {self.priority_level}, created time: {self.generate_time}, age: {self.age}"
def __eq__(self, other):
return self.age == other.age and self.mec_id == other.mec_id and self.priority_level == other.priority_level
def __gt__(self, other):
return (self.age == other.age and self.mec_id == other.mec_id) or \
(self.age == other.age and self.mec_id != other.mec_id and self.priority_level < other.priority_level)
def __lt__(self, other):
return self.age == other.age and self.mec_id != other.mec_id and self.priority_level > other.priority_level
w1 = WSN(10, 100, 2, "34 days ago", 34)
w2 = WSN(10, 70, 4, "50 days ago", 34)
w3 = WSN(11, 100, 6, "10 days ago", 34)
w4 = WSN(11, 120, 2, "15 days ago", 34)
list_wsn = [w1, w2, w3, w4]
list_wsn_sorted = sorted(list_wsn)
for w in list_wsn_sorted:
print(w)
# ID: 11, cpu: 100, priority: 6, created time: 10 days ago, age: 34
# ID: 10, cpu: 100, priority: 2, created time: 34 days ago, age: 34
# ID: 10, cpu: 70, priority: 4, created time: 50 days ago, age: 34
# ID: 11, cpu: 120, priority: 2, created time: 15 days ago, age: 34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment