FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

filter: keep order · python-webuntis/python-webuntis@d2e4d3b · GitHub

Commit d2e4d3b

Browse files
committed
filter: keep order
1 parent e96595f commit d2e4d3b

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

‎webuntis/objects.py‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ def filter(self, **criterions):
117117
bar = [kl for kl in s.klassen()
118118
if kl.id in {'1A', '2A', '3A', '4A'}]
119119
120+
# Or you can use a list: this keeps the order: the first element
121+
# of the result corresponds to the first element in the filter
122+
# Important after using combine()
123+
bar = s.klassen().filter(name=['1A', '2A', '3A', '4A'])
124+
# --> bar[0].name == '1A'
125+
120126
# Since ``filter`` returns a ListResult itself too, we can chain
121127
# multiple calls together:
122128
bar = s.klassen().filter(id=4, name='7A') # is the same as
@@ -151,6 +157,14 @@ def meets_criterions(item):
151157

152158
return True
153159

160+
if isinstance(criterions[0][1], list):
161+
return type(self)(
162+
parent=self,
163+
data=[self.filter(**{key: v})[0]
164+
for key, values in criterions
165+
for v in values
166+
])
167+
154168
return type(self)(
155169
parent=self,
156170
data=[item for item in self if meets_criterions(item)]
@@ -306,7 +320,7 @@ def klassen(self):
306320
this period."""
307321

308322
return self._session.klassen(from_cache=True).filter(
309-
id=set([kl[u'id'] for kl in self._data[u'kl']])
323+
id=list([kl[u'id'] for kl in self._data[u'kl']])
310324
)
311325

312326
@lazyproperty
@@ -315,7 +329,7 @@ def teachers(self):
315329
which are attending this period."""
316330

317331
return self._session.teachers(from_cache=True).filter(
318-
id=set([te[u'id'] for te in self._data[u'te']])
332+
id=list([te[u'id'] for te in self._data[u'te']])
319333
)
320334

321335
@lazyproperty
@@ -326,7 +340,7 @@ def subjects(self):
326340
their own period."""
327341

328342
return self._session.subjects(from_cache=True).filter(
329-
id=set([su[u'id'] for su in self._data[u'su']])
343+
id=list([su[u'id'] for su in self._data[u'su']])
330344
)
331345

332346
@lazyproperty
@@ -336,7 +350,7 @@ def rooms(self):
336350
lesson that is actually occuring at multiple locations (?)."""
337351

338352
return self._session.rooms(from_cache=True).filter(
339-
id=set([ro[u'id'] for ro in self._data[u'ro']])
353+
id=list([ro[u'id'] for ro in self._data[u'ro']])
340354
)
341355

342356
@lazyproperty
@@ -356,7 +370,7 @@ def code(self):
356370
def original_teachers(self):
357371
""" Support for original teachers """
358372
try:
359-
return self._session.teachers(from_cache=True).filter(id=set([te[u'orgid'] for te in self._data[u'te']]))
373+
return self._session.teachers(from_cache=True).filter(id=list([te[u'orgid'] for te in self._data[u'te']]))
360374
except KeyError:
361375
pass
362376
return []
@@ -365,7 +379,7 @@ def original_teachers(self):
365379
def original_rooms(self):
366380
""" Support for original rooms """
367381
try:
368-
return self._session.rooms(from_cache=True).filter(id=set([ro[u'orgid'] for ro in self._data[u'ro']]))
382+
return self._session.rooms(from_cache=True).filter(id=list([ro[u'orgid'] for ro in self._data[u'ro']]))
369383
except KeyError:
370384
pass
371385
return []
@@ -877,7 +891,7 @@ def subject(self):
877891
def teachers(self):
878892
"""@TODO: untested - always empty"""
879893
try:
880-
tes = set(int(te) for te in self._data[u'teacherIds'] if te)
894+
tes = list(int(te) for te in self._data[u'teacherIds'] if te)
881895
except ValueError:
882896
return []
883897

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL