File: queryset.html

package info (click to toggle)
python-exchangelib 5.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,364 kB
  • sloc: python: 25,367; sh: 6; makefile: 5
file content (1318 lines) | stat: -rw-r--r-- 64,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<meta name="generator" content="pdoc3 0.11.6">
<title>exchangelib.queryset API documentation</title>
<meta name="description" content="">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/sanitize.min.css" integrity="sha512-y1dtMcuvtTMJc1yPgEqF0ZjQbhnc/bFhyvIyVNb9Zk5mIGtqVaAB1Ttl28su8AvFMOY0EwRbAe+HCLqj6W7/KA==" crossorigin>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/typography.min.css" integrity="sha512-Y1DYSb995BAfxobCkKepB1BqJJTPrOp3zPL74AWFugHHmmdcvO+C48WLrUOlhGMc0QG7AE3f7gmvvcrmX2fDoA==" crossorigin>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" crossorigin>
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:1.5em;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:2em 0 .50em 0}h3{font-size:1.4em;margin:1.6em 0 .7em 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .2s ease-in-out}a:visited{color:#503}a:hover{color:#b62}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900;font-weight:bold}pre code{font-size:.8em;line-height:1.4em;padding:1em;display:block}code{background:#f3f3f3;font-family:"DejaVu Sans Mono",monospace;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source > summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible;min-width:max-content}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em 1em;margin:1em 0}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul ul{padding-left:1em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin></script>
<script>window.addEventListener('DOMContentLoaded', () => {
hljs.configure({languages: ['bash', 'css', 'diff', 'graphql', 'ini', 'javascript', 'json', 'plaintext', 'python', 'python-repl', 'rust', 'shell', 'sql', 'typescript', 'xml', 'yaml']});
hljs.highlightAll();
/* Collapse source docstrings */
setTimeout(() => {
[...document.querySelectorAll('.hljs.language-python > .hljs-string')]
.filter(el => el.innerHTML.length > 200 && ['"""', "'''"].includes(el.innerHTML.substring(0, 3)))
.forEach(el => {
let d = document.createElement('details');
d.classList.add('hljs-string');
d.innerHTML = '<summary>"""</summary>' + el.innerHTML.substring(3);
el.replaceWith(d);
});
}, 100);
})</script>
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>exchangelib.queryset</code></h1>
</header>
<section id="section-intro">
</section>
<section>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-classes">Classes</h2>
<dl>
<dt id="exchangelib.queryset.QuerySet"><code class="flex name class">
<span>class <span class="ident">QuerySet</span></span>
<span>(</span><span>folder_collection, request_type='item')</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class QuerySet(SearchableMixIn):
    &#34;&#34;&#34;A Django QuerySet-like class for querying items. Defers query until the QuerySet is consumed. Supports
    chaining to build up complex queries.

    Django QuerySet documentation: https://docs.djangoproject.com/en/dev/ref/models/querysets/
    &#34;&#34;&#34;

    VALUES = &#34;values&#34;
    VALUES_LIST = &#34;values_list&#34;
    FLAT = &#34;flat&#34;
    NONE = &#34;none&#34;
    RETURN_TYPES = (VALUES, VALUES_LIST, FLAT, NONE)

    ITEM = &#34;item&#34;
    PERSONA = &#34;persona&#34;
    REQUEST_TYPES = (ITEM, PERSONA)

    def __init__(self, folder_collection, request_type=ITEM):
        from .folders import FolderCollection

        if not isinstance(folder_collection, FolderCollection):
            raise InvalidTypeError(&#34;folder_collection&#34;, folder_collection, FolderCollection)
        self.folder_collection = folder_collection  # A FolderCollection instance
        if request_type not in self.REQUEST_TYPES:
            raise InvalidEnumValue(&#34;request_type&#34;, request_type, self.REQUEST_TYPES)
        self.request_type = request_type
        self.q = Q()  # Default to no restrictions
        self.only_fields = None
        self.order_fields = None
        self.return_format = self.NONE
        self.calendar_view = None
        self.page_size = None
        self.chunk_size = None
        self.max_items = None
        self.offset = 0
        self._depth = None

    def _copy_self(self):
        # When we copy a queryset where the cache has already been filled, we don&#39;t copy the cache. Thus, a copied
        # queryset will fetch results from the server again.
        #
        # All other behaviour would be awkward:
        #
        # qs = QuerySet(f).filter(foo=&#39;bar&#39;)
        # items = list(qs)
        # new_qs = qs.exclude(bar=&#39;baz&#39;)  # This should work, and should fetch from the server
        #
        # Only mutable objects need to be deepcopied. Folder should be the same object
        new_qs = self.__class__(self.folder_collection, request_type=self.request_type)
        new_qs.q = deepcopy(self.q)
        new_qs.only_fields = self.only_fields
        new_qs.order_fields = None if self.order_fields is None else deepcopy(self.order_fields)
        new_qs.return_format = self.return_format
        new_qs.calendar_view = self.calendar_view
        new_qs.page_size = self.page_size
        new_qs.chunk_size = self.chunk_size
        new_qs.max_items = self.max_items
        new_qs.offset = self.offset
        new_qs._depth = self._depth
        return new_qs

    def _get_field_path(self, field_path):
        from .items import Persona

        if self.request_type == self.PERSONA:
            return FieldPath(field=Persona.get_field_by_fieldname(field_path))
        for folder in self.folder_collection:
            with suppress(InvalidField):
                return FieldPath.from_string(field_path=field_path, folder=folder)
        raise InvalidField(f&#34;Unknown field path {field_path!r} on folders {self.folder_collection.folders}&#34;)

    def _get_field_order(self, field_path):
        from .items import Persona

        if self.request_type == self.PERSONA:
            return FieldOrder(
                field_path=FieldPath(field=Persona.get_field_by_fieldname(field_path.lstrip(&#34;-&#34;))),
                reverse=field_path.startswith(&#34;-&#34;),
            )
        for folder in self.folder_collection:
            with suppress(InvalidField):
                return FieldOrder.from_string(field_path=field_path, folder=folder)
        raise InvalidField(f&#34;Unknown field path {field_path!r} on folders {self.folder_collection.folders}&#34;)

    @property
    def _id_field(self):
        return self._get_field_path(&#34;id&#34;)

    @property
    def _changekey_field(self):
        return self._get_field_path(&#34;changekey&#34;)

    def _additional_fields(self):
        if not isinstance(self.only_fields, tuple):
            raise InvalidTypeError(&#34;only_fields&#34;, self.only_fields, tuple)
        # Remove ItemId and ChangeKey. We get them unconditionally
        additional_fields = {f for f in self.only_fields if not f.field.is_attribute}
        if self.request_type != self.ITEM:
            return additional_fields

        # For CalendarItem items, we want to inject internal timezone fields into the requested fields.
        has_start = &#34;start&#34; in {f.field.name for f in additional_fields}
        has_end = &#34;end&#34; in {f.field.name for f in additional_fields}
        meeting_tz_field, start_tz_field, end_tz_field = CalendarItem.timezone_fields()
        if self.folder_collection.account.version.build &lt; EXCHANGE_2010:
            if has_start or has_end:
                additional_fields.add(FieldPath(field=meeting_tz_field))
        else:
            if has_start:
                additional_fields.add(FieldPath(field=start_tz_field))
            if has_end:
                additional_fields.add(FieldPath(field=end_tz_field))
        return additional_fields

    def _format_items(self, items, return_format):
        return {
            self.VALUES: self._as_values,
            self.VALUES_LIST: self._as_values_list,
            self.FLAT: self._as_flat_values_list,
            self.NONE: self._as_items,
        }[return_format](items)

    def _query(self):
        if self.only_fields is None:
            # We didn&#39;t restrict list of field paths. Get all fields from the server, including extended properties.
            if self.request_type == self.PERSONA:
                additional_fields = {}  # GetPersona doesn&#39;t take explicit fields. Don&#39;t bother calculating the list
                complex_fields_requested = True
            else:
                additional_fields = {FieldPath(field=f) for f in self.folder_collection.allowed_item_fields()}
                complex_fields_requested = True
        else:
            additional_fields = self._additional_fields()
            complex_fields_requested = any(f.field.is_complex for f in additional_fields)

        # EWS can do server-side sorting on multiple fields. A caveat is that server-side sorting is not supported
        # for calendar views. In this case, we do all the sorting client-side.
        if self.calendar_view:
            must_sort_clientside = bool(self.order_fields)
            order_fields = None
        else:
            must_sort_clientside = False
            order_fields = self.order_fields

        if must_sort_clientside:
            # Also fetch order_by fields that we only need for client-side sorting.
            extra_order_fields = {f.field_path for f in self.order_fields} - additional_fields
            if extra_order_fields:
                additional_fields.update(extra_order_fields)
        else:
            extra_order_fields = set()

        find_kwargs = dict(
            shape=ID_ONLY,  # Always use IdOnly here, because AllProperties doesn&#39;t actually get *all* properties
            depth=self._depth,
            additional_fields=additional_fields,
            order_fields=order_fields,
            page_size=self.page_size,
            max_items=self.max_items,
            offset=self.offset,
        )
        if self.request_type == self.PERSONA:
            if complex_fields_requested:
                find_kwargs[&#34;additional_fields&#34;] = None
                items = self.folder_collection.account.fetch_personas(
                    ids=self.folder_collection.find_people(self.q, **find_kwargs)
                )
            else:
                if not additional_fields:
                    find_kwargs[&#34;additional_fields&#34;] = None
                items = self.folder_collection.find_people(self.q, **find_kwargs)
        else:
            find_kwargs[&#34;calendar_view&#34;] = self.calendar_view
            if complex_fields_requested:
                # The FindItem service does not support complex field types. Tell find_items() to return
                # (id, changekey) tuples, and pass that to fetch().
                find_kwargs[&#34;additional_fields&#34;] = None
                unfiltered_items = self.folder_collection.account.fetch(
                    ids=self.folder_collection.find_items(self.q, **find_kwargs),
                    only_fields=additional_fields,
                    chunk_size=self.chunk_size,
                )
                # We may be unlucky that the item disappeared between the FindItem and the GetItem calls
                items = filter(lambda i: not isinstance(i, MISSING_ITEM_ERRORS), unfiltered_items)
            else:
                if not additional_fields:
                    # If additional_fields is the empty set, we only requested ID and changekey fields. We can then
                    # take a shortcut by using (shape=ID_ONLY, additional_fields=None) to tell find_items() to return
                    # (id, changekey) tuples. We&#39;ll post-process those later.
                    find_kwargs[&#34;additional_fields&#34;] = None
                items = self.folder_collection.find_items(self.q, **find_kwargs)

        if not must_sort_clientside:
            return items

        # Resort to client-side sorting of the order_by fields. This is greedy. Sorting in Python is stable, so when
        # sorting on multiple fields, we can just do a sort on each of the requested fields in reverse order. Reverse
        # each sort operation if the field was marked as such.
        for f in reversed(self.order_fields):
            try:
                items = sorted(items, key=lambda i: _get_sort_value_or_default(i, f), reverse=f.reverse)
            except TypeError as e:
                if &#34;unorderable types&#34; not in e.args[0]:
                    raise
                raise ValueError(
                    f&#34;Cannot sort on field {f.field_path!r}. The field has no default value defined, and there are &#34;
                    f&#34;either items with None values for this field, or the query contains exception instances &#34;
                    f&#34;(original error: {e}).&#34;
                )
        if not extra_order_fields:
            return items

        # Nullify the fields we only needed for sorting before returning
        return (_rinse_item(i, extra_order_fields) for i in items)

    def __iter__(self):
        # Fill cache if this is the first iteration. Return an iterator over the results. Make this non-greedy by
        # filling the cache while we are iterating.
        #
        if self.q.is_never():
            return

        log.debug(&#34;Initializing cache&#34;)
        yield from self._format_items(items=self._query(), return_format=self.return_format)

    # Do not implement __len__. The implementation of list() tries to preallocate memory by calling __len__ on the
    # given sequence, before calling __iter__. If we implemented __len__, we would end up calling FindItems twice, once
    # to get the result of self.count(), and once to return the actual result.
    #
    # Also, according to https://stackoverflow.com/questions/37189968/how-to-have-list-consume-iter-without-calling-len,
    # a __len__ implementation should be cheap. That does not hold for self.count().
    #
    # def __len__(self):
    #     return self.count()

    def __getitem__(self, idx_or_slice):
        # Support indexing and slicing. This is non-greedy when possible (slicing start, stop and step are not negative,
        # and we&#39;re ordering on at most one field), and will only fill the cache if the entire query is iterated.
        if isinstance(idx_or_slice, int):
            return self._getitem_idx(idx_or_slice)
        return self._getitem_slice(idx_or_slice)

    def _getitem_idx(self, idx):
        if idx &lt; 0:
            # Support negative indexes by reversing the queryset and negating the index value
            reverse_idx = -(idx + 1)
            return self.reverse()[reverse_idx]
        # Optimize by setting an exact offset and fetching only 1 item
        new_qs = self._copy_self()
        new_qs.max_items = 1
        new_qs.page_size = 1
        new_qs.offset = idx
        # The iterator will return at most 1 item
        for item in new_qs.__iter__():
            return item
        raise IndexError()

    def _getitem_slice(self, s):
        from .services import FindItem

        if ((s.start or 0) &lt; 0) or ((s.stop or 0) &lt; 0) or ((s.step or 0) &lt; 0):
            # islice() does not support negative start, stop and step. Make sure cache is full by iterating the full
            # query result, and then slice on the cache.
            return list(self.__iter__())[s]
        # Optimize by setting an exact offset and max_items value
        new_qs = self._copy_self()
        if s.start is not None and s.stop is not None:
            new_qs.offset = s.start
            new_qs.max_items = s.stop - s.start
        elif s.start is not None:
            new_qs.offset = s.start
        elif s.stop is not None:
            new_qs.max_items = s.stop
        if new_qs.page_size is None and new_qs.max_items is not None and new_qs.max_items &lt; FindItem.PAGE_SIZE:
            new_qs.page_size = new_qs.max_items
        return islice(new_qs.__iter__(), None, None, s.step)

    def _item_yielder(self, iterable, item_func, id_only_func, changekey_only_func, id_and_changekey_func):
        # Transforms results from the server according to the given transform functions. Makes sure to pass on
        # Exception instances unaltered.
        if self.only_fields:
            has_non_attribute_fields = bool({f for f in self.only_fields if not f.field.is_attribute})
        else:
            has_non_attribute_fields = True
        if not has_non_attribute_fields:
            # _query() will return an iterator of (id, changekey) tuples
            if self._changekey_field not in self.only_fields:
                transform_func = id_only_func
            elif self._id_field not in self.only_fields:
                transform_func = changekey_only_func
            else:
                transform_func = id_and_changekey_func
            for i in iterable:
                if isinstance(i, Exception):
                    yield i
                    continue
                yield transform_func(*i)
            return
        for i in iterable:
            if isinstance(i, Exception):
                yield i
                continue
            yield item_func(i)

    def _as_items(self, iterable):
        from .items import Item

        return self._item_yielder(
            iterable=iterable,
            item_func=lambda i: i,
            id_only_func=lambda item_id, changekey: Item(id=item_id),
            changekey_only_func=lambda item_id, changekey: Item(changekey=changekey),
            id_and_changekey_func=lambda item_id, changekey: Item(id=item_id, changekey=changekey),
        )

    def _as_values(self, iterable):
        if not self.only_fields:
            raise ValueError(&#34;values() requires at least one field name&#34;)
        return self._item_yielder(
            iterable=iterable,
            item_func=lambda i: {f.path: _get_value_or_default(f, i) for f in self.only_fields},
            id_only_func=lambda item_id, changekey: {&#34;id&#34;: item_id},
            changekey_only_func=lambda item_id, changekey: {&#34;changekey&#34;: changekey},
            id_and_changekey_func=lambda item_id, changekey: {&#34;id&#34;: item_id, &#34;changekey&#34;: changekey},
        )

    def _as_values_list(self, iterable):
        if not self.only_fields:
            raise ValueError(&#34;values_list() requires at least one field name&#34;)
        return self._item_yielder(
            iterable=iterable,
            item_func=lambda i: tuple(_get_value_or_default(f, i) for f in self.only_fields),
            id_only_func=lambda item_id, changekey: (item_id,),
            changekey_only_func=lambda item_id, changekey: (changekey,),
            id_and_changekey_func=lambda item_id, changekey: (item_id, changekey),
        )

    def _as_flat_values_list(self, iterable):
        if not self.only_fields or len(self.only_fields) != 1:
            raise ValueError(&#34;flat=True requires exactly one field name&#34;)
        return self._item_yielder(
            iterable=iterable,
            item_func=lambda i: _get_value_or_default(self.only_fields[0], i),
            id_only_func=lambda item_id, changekey: item_id,
            changekey_only_func=lambda item_id, changekey: changekey,
            id_and_changekey_func=None,  # Can never be called
        )

    ###############################
    #
    # Methods that support chaining
    #
    ###############################
    # Return copies of self, so this works as expected:
    #
    # foo_qs = my_folder.filter(...)
    # foo_qs.filter(foo=&#39;bar&#39;)
    # foo_qs.filter(foo=&#39;baz&#39;)  # Should not be affected by the previous statement
    #
    def all(self):
        &#34;&#34;&#34; &#34;&#34;&#34;
        new_qs = self._copy_self()
        return new_qs

    def none(self):
        &#34;&#34;&#34; &#34;&#34;&#34;
        new_qs = self._copy_self()
        new_qs.q = Q(conn_type=Q.NEVER)
        return new_qs

    def filter(self, *args, **kwargs):
        new_qs = self._copy_self()
        q = Q(*args, **kwargs)
        new_qs.q = new_qs.q &amp; q
        return new_qs

    def exclude(self, *args, **kwargs):
        new_qs = self._copy_self()
        q = ~Q(*args, **kwargs)
        new_qs.q = new_qs.q &amp; q
        return new_qs

    def people(self):
        &#34;&#34;&#34;Change the queryset to search the folder for Personas instead of Items.&#34;&#34;&#34;
        new_qs = self._copy_self()
        new_qs.request_type = self.PERSONA
        return new_qs

    def only(self, *args):
        &#34;&#34;&#34;Fetch only the specified field names. All other item fields will be &#39;None&#39;.&#34;&#34;&#34;
        try:
            only_fields = tuple(self._get_field_path(arg) for arg in args)
        except ValueError as e:
            raise ValueError(f&#34;{e.args[0]} in only()&#34;)
        new_qs = self._copy_self()
        new_qs.only_fields = only_fields
        return new_qs

    def order_by(self, *args):
        &#34;&#34;&#34;

        :return: The QuerySet in reverse order. EWS only supports server-side sorting on a single field. Sorting on
          multiple fields is implemented client-side and will therefore make the query greedy.
        &#34;&#34;&#34;
        try:
            order_fields = tuple(self._get_field_order(arg) for arg in args)
        except ValueError as e:
            raise ValueError(f&#34;{e.args[0]} in order_by()&#34;)
        new_qs = self._copy_self()
        new_qs.order_fields = order_fields
        return new_qs

    def reverse(self):
        &#34;&#34;&#34;Reverses the ordering of the queryset.&#34;&#34;&#34;
        if not self.order_fields:
            raise ValueError(&#34;Reversing only makes sense if there are order_by fields&#34;)
        new_qs = self._copy_self()
        for f in new_qs.order_fields:
            f.reverse = not f.reverse
        return new_qs

    def values(self, *args):
        try:
            only_fields = tuple(self._get_field_path(arg) for arg in args)
        except ValueError as e:
            raise ValueError(f&#34;{e.args[0]} in values()&#34;)
        new_qs = self._copy_self()
        new_qs.only_fields = only_fields
        new_qs.return_format = self.VALUES
        return new_qs

    def values_list(self, *args, **kwargs):
        &#34;&#34;&#34;Return the values of the specified field names as a list of lists. If called with flat=True and only one
        field name, returns a list of values.
        &#34;&#34;&#34;
        flat = kwargs.pop(&#34;flat&#34;, False)
        if kwargs:
            raise AttributeError(f&#34;Unknown kwargs: {kwargs}&#34;)
        if flat and len(args) != 1:
            raise ValueError(&#34;flat=True requires exactly one field name&#34;)
        try:
            only_fields = tuple(self._get_field_path(arg) for arg in args)
        except ValueError as e:
            raise ValueError(f&#34;{e.args[0]} in values_list()&#34;)
        new_qs = self._copy_self()
        new_qs.only_fields = only_fields
        new_qs.return_format = self.FLAT if flat else self.VALUES_LIST
        return new_qs

    def depth(self, depth):
        &#34;&#34;&#34;Specify the search depth. Possible values are: SHALLOW, ASSOCIATED or DEEP.

        :param depth:
        &#34;&#34;&#34;
        new_qs = self._copy_self()
        new_qs._depth = depth
        return new_qs

    ###########################
    #
    # Methods that end chaining
    #
    ###########################

    def get(self, *args, **kwargs):
        &#34;&#34;&#34;Assume the query will return exactly one item. Return that item.&#34;&#34;&#34;
        if not args and set(kwargs) in ({&#34;id&#34;}, {&#34;id&#34;, &#34;changekey&#34;}):
            # We allow calling get(id=..., changekey=...) to get a single item, but only if exactly these two
            # kwargs are present.
            account = self.folder_collection.account
            item_id = self._id_field.field.clean(kwargs[&#34;id&#34;], version=account.version)
            changekey = self._changekey_field.field.clean(kwargs.get(&#34;changekey&#34;), version=account.version)
            # The folders we&#39;re querying may not support all fields
            if self.only_fields is None:
                only_fields = {FieldPath(field=f) for f in self.folder_collection.allowed_item_fields()}
            else:
                only_fields = self.only_fields
            items = list(account.fetch(ids=[(item_id, changekey)], only_fields=only_fields))
        else:
            new_qs = self.filter(*args, **kwargs)
            items = list(new_qs.__iter__())
        if not items:
            raise DoesNotExist()
        if len(items) != 1:
            raise MultipleObjectsReturned()
        item = items[0]
        if isinstance(item, Exception):
            raise item
        return item

    def count(self, page_size=1000):
        &#34;&#34;&#34;Get the query count, with as little effort as possible

        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        &#34;&#34;&#34;
        new_qs = self._copy_self()
        new_qs.only_fields = ()
        new_qs.order_fields = None
        new_qs.return_format = self.NONE
        new_qs.page_size = page_size
        # &#39;chunk_size&#39; not needed since we never need to call GetItem
        return len(list(new_qs.__iter__()))

    def exists(self):
        &#34;&#34;&#34;Find out if the query contains any hits, with as little effort as possible.&#34;&#34;&#34;
        new_qs = self._copy_self()
        new_qs.max_items = 1
        return new_qs.count(page_size=1) &gt; 0

    def _id_only_copy_self(self):
        new_qs = self._copy_self()
        new_qs.only_fields = ()
        new_qs.order_fields = None
        new_qs.return_format = self.NONE
        return new_qs

    def delete(self, page_size=1000, chunk_size=100, **delete_kwargs):
        &#34;&#34;&#34;Delete the items matching the query, with as little effort as possible

        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        :param chunk_size: The number of items to delete per request. (Default value = 100)
        :param delete_kwargs:
        &#34;&#34;&#34;
        ids = self._id_only_copy_self()
        ids.page_size = page_size
        return self.folder_collection.account.bulk_delete(ids=ids, chunk_size=chunk_size, **delete_kwargs)

    def send(self, page_size=1000, chunk_size=100, **send_kwargs):
        &#34;&#34;&#34;Send the items matching the query, with as little effort as possible

        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        :param chunk_size: The number of items to send per request. (Default value = 100)
        :param send_kwargs:
        &#34;&#34;&#34;
        ids = self._id_only_copy_self()
        ids.page_size = page_size
        return self.folder_collection.account.bulk_send(ids=ids, chunk_size=chunk_size, **send_kwargs)

    def copy(self, to_folder, page_size=1000, chunk_size=100, **copy_kwargs):
        &#34;&#34;&#34;Copy the items matching the query, with as little effort as possible

        :param to_folder:
        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        :param chunk_size: The number of items to copy per request. (Default value = 100)
        :param copy_kwargs:
        &#34;&#34;&#34;
        ids = self._id_only_copy_self()
        ids.page_size = page_size
        return self.folder_collection.account.bulk_copy(
            ids=ids, to_folder=to_folder, chunk_size=chunk_size, **copy_kwargs
        )

    def move(self, to_folder, page_size=1000, chunk_size=100):
        &#34;&#34;&#34;Move the items matching the query, with as little effort as possible. &#39;page_size&#39; is the number of items
        to fetch and move per request. We&#39;re only fetching the IDs, so keep it high.

        :param to_folder:
        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        :param chunk_size: The number of items to move per request. (Default value = 100)
        &#34;&#34;&#34;
        ids = self._id_only_copy_self()
        ids.page_size = page_size
        return self.folder_collection.account.bulk_move(
            ids=ids,
            to_folder=to_folder,
            chunk_size=chunk_size,
        )

    def archive(self, to_folder, page_size=1000, chunk_size=100):
        &#34;&#34;&#34;Archive the items matching the query, with as little effort as possible. &#39;page_size&#39; is the number of items
        to fetch and move per request. We&#39;re only fetching the IDs, so keep it high.

        :param to_folder:
        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        :param chunk_size: The number of items to archive per request. (Default value = 100)
        &#34;&#34;&#34;
        ids = self._id_only_copy_self()
        ids.page_size = page_size
        return self.folder_collection.account.bulk_archive(
            ids=ids,
            to_folder=to_folder,
            chunk_size=chunk_size,
        )

    def mark_as_junk(self, page_size=1000, chunk_size=1000, **mark_as_junk_kwargs):
        &#34;&#34;&#34;Mark the items matching the query as junk, with as little effort as possible. &#39;page_size&#39; is the number of
        items to fetch and mark per request. We&#39;re only fetching the IDs, so keep it high.

        :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
        (Default value = 1000)
        :param chunk_size: The number of items to mark as junk per request. (Default value = 100)
        :param mark_as_junk_kwargs:
        &#34;&#34;&#34;
        ids = self._id_only_copy_self()
        ids.page_size = page_size
        return self.folder_collection.account.bulk_mark_as_junk(ids=ids, chunk_size=chunk_size, **mark_as_junk_kwargs)

    def __str__(self):
        fmt_args = [(&#34;q&#34;, str(self.q)), (&#34;folders&#34;, f&#34;[{&#39;, &#39;.join(str(f) for f in self.folder_collection.folders)}]&#34;)]
        args_str = &#34;, &#34;.join(f&#34;{k}={v}&#34; for k, v in fmt_args)
        return f&#34;{self.__class__.__name__}({args_str})&#34;</code></pre>
</details>
<div class="desc"><p>A Django QuerySet-like class for querying items. Defers query until the QuerySet is consumed. Supports
chaining to build up complex queries.</p>
<p>Django QuerySet documentation: <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/">https://docs.djangoproject.com/en/dev/ref/models/querysets/</a></p></div>
<h3>Ancestors</h3>
<ul class="hlist">
<li><a title="exchangelib.queryset.SearchableMixIn" href="#exchangelib.queryset.SearchableMixIn">SearchableMixIn</a></li>
</ul>
<h3>Class variables</h3>
<dl>
<dt id="exchangelib.queryset.QuerySet.FLAT"><code class="name">var <span class="ident">FLAT</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.ITEM"><code class="name">var <span class="ident">ITEM</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.NONE"><code class="name">var <span class="ident">NONE</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.PERSONA"><code class="name">var <span class="ident">PERSONA</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.REQUEST_TYPES"><code class="name">var <span class="ident">REQUEST_TYPES</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.RETURN_TYPES"><code class="name">var <span class="ident">RETURN_TYPES</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.VALUES"><code class="name">var <span class="ident">VALUES</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.VALUES_LIST"><code class="name">var <span class="ident">VALUES_LIST</span></code></dt>
<dd>
<div class="desc"><p>The type of the None singleton.</p></div>
</dd>
</dl>
<h3>Methods</h3>
<dl>
<dt id="exchangelib.queryset.QuerySet.archive"><code class="name flex">
<span>def <span class="ident">archive</span></span>(<span>self, to_folder, page_size=1000, chunk_size=100)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def archive(self, to_folder, page_size=1000, chunk_size=100):
    &#34;&#34;&#34;Archive the items matching the query, with as little effort as possible. &#39;page_size&#39; is the number of items
    to fetch and move per request. We&#39;re only fetching the IDs, so keep it high.

    :param to_folder:
    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    :param chunk_size: The number of items to archive per request. (Default value = 100)
    &#34;&#34;&#34;
    ids = self._id_only_copy_self()
    ids.page_size = page_size
    return self.folder_collection.account.bulk_archive(
        ids=ids,
        to_folder=to_folder,
        chunk_size=chunk_size,
    )</code></pre>
</details>
<div class="desc"><p>Archive the items matching the query, with as little effort as possible. 'page_size' is the number of items
to fetch and move per request. We're only fetching the IDs, so keep it high.</p>
<p>:param to_folder:
:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)
:param chunk_size: The number of items to archive per request. (Default value = 100)</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.copy"><code class="name flex">
<span>def <span class="ident">copy</span></span>(<span>self, to_folder, page_size=1000, chunk_size=100, **copy_kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def copy(self, to_folder, page_size=1000, chunk_size=100, **copy_kwargs):
    &#34;&#34;&#34;Copy the items matching the query, with as little effort as possible

    :param to_folder:
    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    :param chunk_size: The number of items to copy per request. (Default value = 100)
    :param copy_kwargs:
    &#34;&#34;&#34;
    ids = self._id_only_copy_self()
    ids.page_size = page_size
    return self.folder_collection.account.bulk_copy(
        ids=ids, to_folder=to_folder, chunk_size=chunk_size, **copy_kwargs
    )</code></pre>
</details>
<div class="desc"><p>Copy the items matching the query, with as little effort as possible</p>
<p>:param to_folder:
:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)
:param chunk_size: The number of items to copy per request. (Default value = 100)
:param copy_kwargs:</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.count"><code class="name flex">
<span>def <span class="ident">count</span></span>(<span>self, page_size=1000)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def count(self, page_size=1000):
    &#34;&#34;&#34;Get the query count, with as little effort as possible

    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    &#34;&#34;&#34;
    new_qs = self._copy_self()
    new_qs.only_fields = ()
    new_qs.order_fields = None
    new_qs.return_format = self.NONE
    new_qs.page_size = page_size
    # &#39;chunk_size&#39; not needed since we never need to call GetItem
    return len(list(new_qs.__iter__()))</code></pre>
</details>
<div class="desc"><p>Get the query count, with as little effort as possible</p>
<p>:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.delete"><code class="name flex">
<span>def <span class="ident">delete</span></span>(<span>self, page_size=1000, chunk_size=100, **delete_kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def delete(self, page_size=1000, chunk_size=100, **delete_kwargs):
    &#34;&#34;&#34;Delete the items matching the query, with as little effort as possible

    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    :param chunk_size: The number of items to delete per request. (Default value = 100)
    :param delete_kwargs:
    &#34;&#34;&#34;
    ids = self._id_only_copy_self()
    ids.page_size = page_size
    return self.folder_collection.account.bulk_delete(ids=ids, chunk_size=chunk_size, **delete_kwargs)</code></pre>
</details>
<div class="desc"><p>Delete the items matching the query, with as little effort as possible</p>
<p>:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)
:param chunk_size: The number of items to delete per request. (Default value = 100)
:param delete_kwargs:</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.depth"><code class="name flex">
<span>def <span class="ident">depth</span></span>(<span>self, depth)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def depth(self, depth):
    &#34;&#34;&#34;Specify the search depth. Possible values are: SHALLOW, ASSOCIATED or DEEP.

    :param depth:
    &#34;&#34;&#34;
    new_qs = self._copy_self()
    new_qs._depth = depth
    return new_qs</code></pre>
</details>
<div class="desc"><p>Specify the search depth. Possible values are: SHALLOW, ASSOCIATED or DEEP.</p>
<p>:param depth:</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.exists"><code class="name flex">
<span>def <span class="ident">exists</span></span>(<span>self)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def exists(self):
    &#34;&#34;&#34;Find out if the query contains any hits, with as little effort as possible.&#34;&#34;&#34;
    new_qs = self._copy_self()
    new_qs.max_items = 1
    return new_qs.count(page_size=1) &gt; 0</code></pre>
</details>
<div class="desc"><p>Find out if the query contains any hits, with as little effort as possible.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.get"><code class="name flex">
<span>def <span class="ident">get</span></span>(<span>self, *args, **kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def get(self, *args, **kwargs):
    &#34;&#34;&#34;Assume the query will return exactly one item. Return that item.&#34;&#34;&#34;
    if not args and set(kwargs) in ({&#34;id&#34;}, {&#34;id&#34;, &#34;changekey&#34;}):
        # We allow calling get(id=..., changekey=...) to get a single item, but only if exactly these two
        # kwargs are present.
        account = self.folder_collection.account
        item_id = self._id_field.field.clean(kwargs[&#34;id&#34;], version=account.version)
        changekey = self._changekey_field.field.clean(kwargs.get(&#34;changekey&#34;), version=account.version)
        # The folders we&#39;re querying may not support all fields
        if self.only_fields is None:
            only_fields = {FieldPath(field=f) for f in self.folder_collection.allowed_item_fields()}
        else:
            only_fields = self.only_fields
        items = list(account.fetch(ids=[(item_id, changekey)], only_fields=only_fields))
    else:
        new_qs = self.filter(*args, **kwargs)
        items = list(new_qs.__iter__())
    if not items:
        raise DoesNotExist()
    if len(items) != 1:
        raise MultipleObjectsReturned()
    item = items[0]
    if isinstance(item, Exception):
        raise item
    return item</code></pre>
</details>
<div class="desc"><p>Assume the query will return exactly one item. Return that item.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.mark_as_junk"><code class="name flex">
<span>def <span class="ident">mark_as_junk</span></span>(<span>self, page_size=1000, chunk_size=1000, **mark_as_junk_kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def mark_as_junk(self, page_size=1000, chunk_size=1000, **mark_as_junk_kwargs):
    &#34;&#34;&#34;Mark the items matching the query as junk, with as little effort as possible. &#39;page_size&#39; is the number of
    items to fetch and mark per request. We&#39;re only fetching the IDs, so keep it high.

    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    :param chunk_size: The number of items to mark as junk per request. (Default value = 100)
    :param mark_as_junk_kwargs:
    &#34;&#34;&#34;
    ids = self._id_only_copy_self()
    ids.page_size = page_size
    return self.folder_collection.account.bulk_mark_as_junk(ids=ids, chunk_size=chunk_size, **mark_as_junk_kwargs)</code></pre>
</details>
<div class="desc"><p>Mark the items matching the query as junk, with as little effort as possible. 'page_size' is the number of
items to fetch and mark per request. We're only fetching the IDs, so keep it high.</p>
<p>:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)
:param chunk_size: The number of items to mark as junk per request. (Default value = 100)
:param mark_as_junk_kwargs:</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.move"><code class="name flex">
<span>def <span class="ident">move</span></span>(<span>self, to_folder, page_size=1000, chunk_size=100)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def move(self, to_folder, page_size=1000, chunk_size=100):
    &#34;&#34;&#34;Move the items matching the query, with as little effort as possible. &#39;page_size&#39; is the number of items
    to fetch and move per request. We&#39;re only fetching the IDs, so keep it high.

    :param to_folder:
    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    :param chunk_size: The number of items to move per request. (Default value = 100)
    &#34;&#34;&#34;
    ids = self._id_only_copy_self()
    ids.page_size = page_size
    return self.folder_collection.account.bulk_move(
        ids=ids,
        to_folder=to_folder,
        chunk_size=chunk_size,
    )</code></pre>
</details>
<div class="desc"><p>Move the items matching the query, with as little effort as possible. 'page_size' is the number of items
to fetch and move per request. We're only fetching the IDs, so keep it high.</p>
<p>:param to_folder:
:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)
:param chunk_size: The number of items to move per request. (Default value = 100)</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.only"><code class="name flex">
<span>def <span class="ident">only</span></span>(<span>self, *args)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def only(self, *args):
    &#34;&#34;&#34;Fetch only the specified field names. All other item fields will be &#39;None&#39;.&#34;&#34;&#34;
    try:
        only_fields = tuple(self._get_field_path(arg) for arg in args)
    except ValueError as e:
        raise ValueError(f&#34;{e.args[0]} in only()&#34;)
    new_qs = self._copy_self()
    new_qs.only_fields = only_fields
    return new_qs</code></pre>
</details>
<div class="desc"><p>Fetch only the specified field names. All other item fields will be 'None'.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.order_by"><code class="name flex">
<span>def <span class="ident">order_by</span></span>(<span>self, *args)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def order_by(self, *args):
    &#34;&#34;&#34;

    :return: The QuerySet in reverse order. EWS only supports server-side sorting on a single field. Sorting on
      multiple fields is implemented client-side and will therefore make the query greedy.
    &#34;&#34;&#34;
    try:
        order_fields = tuple(self._get_field_order(arg) for arg in args)
    except ValueError as e:
        raise ValueError(f&#34;{e.args[0]} in order_by()&#34;)
    new_qs = self._copy_self()
    new_qs.order_fields = order_fields
    return new_qs</code></pre>
</details>
<div class="desc"><p>:return: The QuerySet in reverse order. EWS only supports server-side sorting on a single field. Sorting on
multiple fields is implemented client-side and will therefore make the query greedy.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.people"><code class="name flex">
<span>def <span class="ident">people</span></span>(<span>self)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def people(self):
    &#34;&#34;&#34;Change the queryset to search the folder for Personas instead of Items.&#34;&#34;&#34;
    new_qs = self._copy_self()
    new_qs.request_type = self.PERSONA
    return new_qs</code></pre>
</details>
<div class="desc"><p>Change the queryset to search the folder for Personas instead of Items.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.reverse"><code class="name flex">
<span>def <span class="ident">reverse</span></span>(<span>self)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def reverse(self):
    &#34;&#34;&#34;Reverses the ordering of the queryset.&#34;&#34;&#34;
    if not self.order_fields:
        raise ValueError(&#34;Reversing only makes sense if there are order_by fields&#34;)
    new_qs = self._copy_self()
    for f in new_qs.order_fields:
        f.reverse = not f.reverse
    return new_qs</code></pre>
</details>
<div class="desc"><p>Reverses the ordering of the queryset.</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.send"><code class="name flex">
<span>def <span class="ident">send</span></span>(<span>self, page_size=1000, chunk_size=100, **send_kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def send(self, page_size=1000, chunk_size=100, **send_kwargs):
    &#34;&#34;&#34;Send the items matching the query, with as little effort as possible

    :param page_size: The number of items to fetch per request. We&#39;re only fetching the IDs, so keep it high.
    (Default value = 1000)
    :param chunk_size: The number of items to send per request. (Default value = 100)
    :param send_kwargs:
    &#34;&#34;&#34;
    ids = self._id_only_copy_self()
    ids.page_size = page_size
    return self.folder_collection.account.bulk_send(ids=ids, chunk_size=chunk_size, **send_kwargs)</code></pre>
</details>
<div class="desc"><p>Send the items matching the query, with as little effort as possible</p>
<p>:param page_size: The number of items to fetch per request. We're only fetching the IDs, so keep it high.
(Default value = 1000)
:param chunk_size: The number of items to send per request. (Default value = 100)
:param send_kwargs:</p></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.values"><code class="name flex">
<span>def <span class="ident">values</span></span>(<span>self, *args)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def values(self, *args):
    try:
        only_fields = tuple(self._get_field_path(arg) for arg in args)
    except ValueError as e:
        raise ValueError(f&#34;{e.args[0]} in values()&#34;)
    new_qs = self._copy_self()
    new_qs.only_fields = only_fields
    new_qs.return_format = self.VALUES
    return new_qs</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="exchangelib.queryset.QuerySet.values_list"><code class="name flex">
<span>def <span class="ident">values_list</span></span>(<span>self, *args, **kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def values_list(self, *args, **kwargs):
    &#34;&#34;&#34;Return the values of the specified field names as a list of lists. If called with flat=True and only one
    field name, returns a list of values.
    &#34;&#34;&#34;
    flat = kwargs.pop(&#34;flat&#34;, False)
    if kwargs:
        raise AttributeError(f&#34;Unknown kwargs: {kwargs}&#34;)
    if flat and len(args) != 1:
        raise ValueError(&#34;flat=True requires exactly one field name&#34;)
    try:
        only_fields = tuple(self._get_field_path(arg) for arg in args)
    except ValueError as e:
        raise ValueError(f&#34;{e.args[0]} in values_list()&#34;)
    new_qs = self._copy_self()
    new_qs.only_fields = only_fields
    new_qs.return_format = self.FLAT if flat else self.VALUES_LIST
    return new_qs</code></pre>
</details>
<div class="desc"><p>Return the values of the specified field names as a list of lists. If called with flat=True and only one
field name, returns a list of values.</p></div>
</dd>
</dl>
<h3>Inherited members</h3>
<ul class="hlist">
<li><code><b><a title="exchangelib.queryset.SearchableMixIn" href="#exchangelib.queryset.SearchableMixIn">SearchableMixIn</a></b></code>:
<ul class="hlist">
<li><code><a title="exchangelib.queryset.SearchableMixIn.all" href="#exchangelib.queryset.SearchableMixIn.all">all</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.exclude" href="#exchangelib.queryset.SearchableMixIn.exclude">exclude</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.filter" href="#exchangelib.queryset.SearchableMixIn.filter">filter</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.none" href="#exchangelib.queryset.SearchableMixIn.none">none</a></code></li>
</ul>
</li>
</ul>
</dd>
<dt id="exchangelib.queryset.SearchableMixIn"><code class="flex name class">
<span>class <span class="ident">SearchableMixIn</span></span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class SearchableMixIn:
    &#34;&#34;&#34;Implement a search API for inheritance.&#34;&#34;&#34;

    @abc.abstractmethod
    def get(self, *args, **kwargs):
        &#34;&#34;&#34;Return a single object&#34;&#34;&#34;

    @abc.abstractmethod
    def all(self):
        &#34;&#34;&#34;Return all objects, unfiltered&#34;&#34;&#34;

    @abc.abstractmethod
    def none(self):
        &#34;&#34;&#34;Return an empty result&#34;&#34;&#34;

    @abc.abstractmethod
    def filter(self, *args, **kwargs):
        &#34;&#34;&#34;Apply filters to a query&#34;&#34;&#34;

    @abc.abstractmethod
    def exclude(self, *args, **kwargs):
        &#34;&#34;&#34;Apply filters to a query&#34;&#34;&#34;

    @abc.abstractmethod
    def people(self):
        &#34;&#34;&#34;Search for personas&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Implement a search API for inheritance.</p></div>
<h3>Subclasses</h3>
<ul class="hlist">
<li><a title="exchangelib.folders.base.BaseFolder" href="folders/base.html#exchangelib.folders.base.BaseFolder">BaseFolder</a></li>
<li><a title="exchangelib.folders.collections.FolderCollection" href="folders/collections.html#exchangelib.folders.collections.FolderCollection">FolderCollection</a></li>
<li><a title="exchangelib.queryset.QuerySet" href="#exchangelib.queryset.QuerySet">QuerySet</a></li>
</ul>
<h3>Methods</h3>
<dl>
<dt id="exchangelib.queryset.SearchableMixIn.all"><code class="name flex">
<span>def <span class="ident">all</span></span>(<span>self)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@abc.abstractmethod
def all(self):
    &#34;&#34;&#34;Return all objects, unfiltered&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Return all objects, unfiltered</p></div>
</dd>
<dt id="exchangelib.queryset.SearchableMixIn.exclude"><code class="name flex">
<span>def <span class="ident">exclude</span></span>(<span>self, *args, **kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@abc.abstractmethod
def exclude(self, *args, **kwargs):
    &#34;&#34;&#34;Apply filters to a query&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Apply filters to a query</p></div>
</dd>
<dt id="exchangelib.queryset.SearchableMixIn.filter"><code class="name flex">
<span>def <span class="ident">filter</span></span>(<span>self, *args, **kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@abc.abstractmethod
def filter(self, *args, **kwargs):
    &#34;&#34;&#34;Apply filters to a query&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Apply filters to a query</p></div>
</dd>
<dt id="exchangelib.queryset.SearchableMixIn.get"><code class="name flex">
<span>def <span class="ident">get</span></span>(<span>self, *args, **kwargs)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@abc.abstractmethod
def get(self, *args, **kwargs):
    &#34;&#34;&#34;Return a single object&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Return a single object</p></div>
</dd>
<dt id="exchangelib.queryset.SearchableMixIn.none"><code class="name flex">
<span>def <span class="ident">none</span></span>(<span>self)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@abc.abstractmethod
def none(self):
    &#34;&#34;&#34;Return an empty result&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Return an empty result</p></div>
</dd>
<dt id="exchangelib.queryset.SearchableMixIn.people"><code class="name flex">
<span>def <span class="ident">people</span></span>(<span>self)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@abc.abstractmethod
def people(self):
    &#34;&#34;&#34;Search for personas&#34;&#34;&#34;</code></pre>
</details>
<div class="desc"><p>Search for personas</p></div>
</dd>
</dl>
</dd>
</dl>
</section>
</article>
<nav id="sidebar">
<div class="toc">
<ul></ul>
</div>
<ul id="index">
<li><h3>Super-module</h3>
<ul>
<li><code><a title="exchangelib" href="index.html">exchangelib</a></code></li>
</ul>
</li>
<li><h3><a href="#header-classes">Classes</a></h3>
<ul>
<li>
<h4><code><a title="exchangelib.queryset.QuerySet" href="#exchangelib.queryset.QuerySet">QuerySet</a></code></h4>
<ul class="two-column">
<li><code><a title="exchangelib.queryset.QuerySet.FLAT" href="#exchangelib.queryset.QuerySet.FLAT">FLAT</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.ITEM" href="#exchangelib.queryset.QuerySet.ITEM">ITEM</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.NONE" href="#exchangelib.queryset.QuerySet.NONE">NONE</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.PERSONA" href="#exchangelib.queryset.QuerySet.PERSONA">PERSONA</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.REQUEST_TYPES" href="#exchangelib.queryset.QuerySet.REQUEST_TYPES">REQUEST_TYPES</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.RETURN_TYPES" href="#exchangelib.queryset.QuerySet.RETURN_TYPES">RETURN_TYPES</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.VALUES" href="#exchangelib.queryset.QuerySet.VALUES">VALUES</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.VALUES_LIST" href="#exchangelib.queryset.QuerySet.VALUES_LIST">VALUES_LIST</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.archive" href="#exchangelib.queryset.QuerySet.archive">archive</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.copy" href="#exchangelib.queryset.QuerySet.copy">copy</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.count" href="#exchangelib.queryset.QuerySet.count">count</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.delete" href="#exchangelib.queryset.QuerySet.delete">delete</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.depth" href="#exchangelib.queryset.QuerySet.depth">depth</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.exists" href="#exchangelib.queryset.QuerySet.exists">exists</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.get" href="#exchangelib.queryset.QuerySet.get">get</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.mark_as_junk" href="#exchangelib.queryset.QuerySet.mark_as_junk">mark_as_junk</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.move" href="#exchangelib.queryset.QuerySet.move">move</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.only" href="#exchangelib.queryset.QuerySet.only">only</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.order_by" href="#exchangelib.queryset.QuerySet.order_by">order_by</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.people" href="#exchangelib.queryset.QuerySet.people">people</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.reverse" href="#exchangelib.queryset.QuerySet.reverse">reverse</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.send" href="#exchangelib.queryset.QuerySet.send">send</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.values" href="#exchangelib.queryset.QuerySet.values">values</a></code></li>
<li><code><a title="exchangelib.queryset.QuerySet.values_list" href="#exchangelib.queryset.QuerySet.values_list">values_list</a></code></li>
</ul>
</li>
<li>
<h4><code><a title="exchangelib.queryset.SearchableMixIn" href="#exchangelib.queryset.SearchableMixIn">SearchableMixIn</a></code></h4>
<ul class="two-column">
<li><code><a title="exchangelib.queryset.SearchableMixIn.all" href="#exchangelib.queryset.SearchableMixIn.all">all</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.exclude" href="#exchangelib.queryset.SearchableMixIn.exclude">exclude</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.filter" href="#exchangelib.queryset.SearchableMixIn.filter">filter</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.get" href="#exchangelib.queryset.SearchableMixIn.get">get</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.none" href="#exchangelib.queryset.SearchableMixIn.none">none</a></code></li>
<li><code><a title="exchangelib.queryset.SearchableMixIn.people" href="#exchangelib.queryset.SearchableMixIn.people">people</a></code></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</main>
<footer id="footer">
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.11.6</a>.</p>
</footer>
</body>
</html>