File: PyView.cpp

package info (click to toggle)
metakit 2.4.3-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,468 kB
  • ctags: 3,548
  • sloc: xml: 29,455; cpp: 23,339; sh: 9,051; tcl: 1,195; python: 577; makefile: 254; ansic: 14
file content (1267 lines) | stat: -rwxr-xr-x 37,819 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
// PyView.cpp --
// $Id: PyView.cpp,v 1.12 2002/01/03 19:51:36 wcvs Exp $
// This is part of MetaKit, the homepage is http://www.equi4.com/metakit/
//
//  Copyright 1999 McMillan Enterprises, Inc. -- www.mcmillan-inc.com
//  Copyright (C) 1999-2001 Jean-Claude Wippler <jcw@equi4.com>
//
//  View class implementation
//  setsize method added by J. Barnard

#include "PyView.h"
#include "PyProperty.h"
#include "PyRowRef.h"
#include <PWOMSequence.h>
#include <PWONumber.h>
#include <PWOMapping.h>
#include <PWOCallable.h>

static void MustBeView(PyObject* o)
  { if (!PyGenericView_Check(o)) Fail(PyExc_TypeError, "Arg must be a view object"); }

static char *setsize__doc =
"setsize(nrows) -- adjust the number of rows in a view";

static PyObject *PyView_setsize(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    PWONumber nrows = PWONumber(args[0]);
    o->SetSize((int) nrows);
    return nrows.disOwn();
  }
  catch (...) { return 0; }
}

static char* structure__doc = 
"structure() -- return list of properties";

static PyObject* PyView_structure(PyView *o, PyObject* _args) {
  try {
    return o->structure();
  }
  catch (...) { return 0; }
}

static char* insert__doc = 
"insert(position, obj) -- coerce obj (or keyword args) to row and insert";

static PyObject* PyView_insert(PyView *o, PyObject* _args, PyObject* kwargs) {
  try {
    PWOSequence args(_args);
    if (args.len() == 1)
      o->insertAt(PWONumber(args[0]), kwargs);
    else
      o->insertAt(PWONumber (args[0]), args[1]);
    Py_INCREF(Py_None);
    return Py_None;
  }
  catch (...) { return 0; }
}

static char* append__doc = 
"append(obj) -- coerce obj (or keyword args) to row and append, returns pos";

static PyObject* PyView_append(PyView *o, PyObject* _args, PyObject* kwargs) {
  try {
    PWOSequence args(_args);
    PWONumber ndx(o->GetSize());
    if (args.len() == 0)
      o->insertAt(ndx, kwargs);
    else
      o->insertAt(ndx, args[0]);
    return ndx.disOwn();
  }
  catch (...) { return 0; }
}

static char* delete__doc = 
"delete(position) -- delete row at specified position";

static PyObject* PyView_delete(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    int ndx = PWONumber (args[0]);
    PWOTuple seq;
    o->setSlice(ndx, ndx+1, seq);
    Py_INCREF(Py_None);
    return Py_None;
  }
  catch (...) { return 0; }
}

static char* addproperty__doc = 
"addproperty(property) -- add temp column to view (use getas() for persistent columns)";

static PyObject* PyView_addproperty(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    PWOBase prop(args[0]);
    if (!PyProperty_Check((PyObject* )prop))
      Fail(PyExc_TypeError, "Not a Property object");
    PWONumber rslt(o->AddProperty(*(PyProperty *)(PyObject* )prop));
    return rslt.disOwn();
  }
  catch (...) { return 0; }
}

static char* select__doc = 
"select(criteria) -- return virtual view with selected rows\n"
"select(crit_lo, crit_hi) -- select rows in specified range (inclusive)\n"
"  criteria may be keyword args or dictionary";

static PyObject* PyView_select(PyView *o, PyObject* _args, PyObject* kwargs) {
  try {
    c4_Row temp;
    PWOSequence args(_args);
    if (args.len() == 0) 
    {
      o->makeRow(temp, kwargs, false);
      return new PyView(o->Select(temp), o, o->computeState(NOTIFIABLE));
    }
    if (args.len() == 1) 
    {
      o->makeRow(temp, args[0], false);
      return new PyView(o->Select(temp), o, o->computeState(NOTIFIABLE));
    }

    if (PyObject_Length(args[0]) > 0)
      o->makeRow(temp, args[0], false);

    c4_Row temp2; // force an error if neither boundary has useful values
    if (temp.Container().NumProperties() == 0 || PyObject_Length(args[1]) > 0)
      o->makeRow(temp2, args[1], false);
    
    return new PyView(o->SelectRange(temp, temp2), o, o->computeState(NOTIFIABLE));
  }
  catch (...) { return 0; }
}

static char* sort__doc = 
"sort() -- return virtual sorted view (native key order)\n"
"sort(property...) -- sort on the specified properties";

static PyObject* PyView_sort(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    if (args.len()) {
      PyView crit;
      crit.addProperties(args);
      return new PyView (o->SortOn(crit), o, o->computeState(FINALNOTIFIABLE));
    }
    return new PyView (o->Sort(), o, o->computeState(FINALNOTIFIABLE));	
  }
  catch (...) { return 0; }
}

static char* sortrev__doc = 
"sortrev(props,propsdown) -- return sorted view, with optional reversed order\n"
" arguments are lists of properties";

static PyObject* PyView_sortrev(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);

    PWOSequence all(args[0]);
    PyView propsAll;
    propsAll.addProperties(all);

    PWOSequence down(args[1]);
    PyView propsDown;
    propsDown.addProperties(down);

    return new PyView (o->SortOnReverse(propsAll, propsDown), 0, o->computeState(FINALNOTIFIABLE));
  }
  catch (...) { return 0; }
}

static char* project__doc = 
"project(property...) -- returns virtual view with only the named columns";

static PyObject* PyView_project(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    PyView crit;
    crit.addProperties(args);
    return new PyView (o->Project(crit), 0, o->computeState(NOTIFIABLE));
  }
  catch (...) { return 0; }
}

static char* flatten__doc = 
"flatten(subview_property, outer) -- produces 'flat' view from nested view\n"
" outer defaults to 0";

static PyObject* PyView_flatten(PyView *o, PyObject *_args, PyObject *_kwargs) {
  try {
    PWOSequence args(_args);
    PWOMapping kwargs;
    if (_kwargs)
        kwargs = PWOBase(_kwargs);
    if (!PyProperty_Check((PyObject*)args[0])) 
      Fail(PyExc_TypeError, "First arg must be a property object identifying the subview");
    const c4_Property& subview = *(PyProperty *)(PyObject* )args[0];
    bool outer = false;
    if (args.len() > 1) {
      PWONumber flag(args[1]);
      if ((int)flag > 0)
        outer = true;
    }
    if (kwargs.hasKey("outer")) {
      if (int(PWONumber(kwargs["outer"])))
        outer = true;
    }
    return new PyView (o->JoinProp((const c4_ViewProp&) subview, outer), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* join__doc = 
"join(otherview, property..., outer) -- join views on properties of same name and type\n"
" outer defaults to 0";

static PyObject* PyView_join(PyView *o, PyObject* _args, PyObject *_kwargs) {
  PWOMapping kwargs;
  try {
    PWOSequence args(_args);
    if (_kwargs)
        kwargs = PWOBase(_kwargs);
    MustBeView(args[0]);
    PyView *other = (PyView *)(PyObject* )args[0];
    bool outer = false;
    int last = args.len();
    if (PyInt_Check((PyObject*)args[last-1])) {
      PWONumber flag(args[--last]);
      if ((int)flag > 0)
        outer = true;
    }
    if (kwargs.hasKey("outer")) {
      if (int(PWONumber(kwargs["outer"])))
        outer = true;
    }
    PyView crit;
    crit.addProperties(args.getSlice(1,last));
    return new PyView (o->Join(crit, *other, outer), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* groupby__doc = 
"groupby(property..., 'subname') -- group by given properties, creating subviews";

static PyObject* PyView_groupby(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    int last = args.len();
    PWOString subname (args[--last]);
    PyView crit;
    crit.addProperties(args.getSlice(0,last));
    c4_ViewProp sub (subname);
    return new PyView (o->GroupBy(crit, sub), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* counts__doc = 
"counts(property..., 'name') -- group by given properties, adding a count property";

static PyObject* PyView_counts(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    int last = args.len();
    PWOString name (args[--last]);
    PyView crit;
    crit.addProperties(args.getSlice(0,last));
    c4_IntProp count (name);
    return new PyView (o->Counts(crit, count), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* rename__doc = 
"rename('oldname', 'newname') -- derive a view with one property renamed";

static PyObject* PyView_rename(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    
    PWOString oldName (args[0]);
    int n = o->FindPropIndexByName(oldName);
    if (n < 0)
      Fail(PyExc_TypeError, "Property not found in view");
    const c4_Property& oProp = o->NthProperty(n);

    PWOString newName (args[1]);
    c4_Property nProp (oProp.Type(), newName);

    return new PyView (o->Rename(oProp, nProp), 0, o->computeState(RWVIEWER));
  }
  catch (...) { return 0; }
}

static char* unique__doc = 
"unique() -- returns a view without duplicate rows, i.e. a set";

static PyObject *PyView_unique(PyView *o, PyObject *_args) {
  try {
    return new PyView(o->Unique(), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* product__doc = 
"product(view2) -- produce the cartesian product of both views";

static PyObject *PyView_product(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->Product(*(PyView *)(PyObject *)args[0]), 0, o->computeState(ROVIEWER));		
  }
  catch (...) { return 0; }
}

static char* union__doc = 
"union(view2) -- produce the set union of both views";

static PyObject *PyView_union(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->Union(*(PyView *)(PyObject *)args[0]), 0, o->computeState(ROVIEWER));	
  }
  catch (...) { return 0; }
}

static char* intersect__doc = 
"intersect(view2) -- produce the set intersection of both views";

static PyObject *PyView_intersect(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->Intersect(*(PyView* )(PyObject *)args[0]), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* different__doc = 
"different(view2) -- produce the set difference of both views (XOR)";

static PyObject *PyView_different(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->Different(*(PyView* )(PyObject *)args[0]), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* minus__doc = 
"minus(view2) -- all rows in view, but not in view2";

static PyObject *PyView_minus(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->Minus(*(PyView* )(PyObject *)args[0]), 0, o->computeState(ROVIEWER));
  }
  catch (...) { return 0; }
}

static char* remapwith__doc = 
"remapwith(view2) -- remap rows according to first (int) prop in view2";

static PyObject *PyView_remapwith(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->RemapWith(*(PyView* )(PyObject *)args[0]), 0, o->computeState(RWVIEWER));
  }
  catch (...) { return 0; }
}

static char* pair__doc = 
"pair(view2) -- concatenate rows pairwise, side by side";

static PyObject *PyView_pair(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    return new PyView(o->Pair(*(PyView* )(PyObject *)args[0]), 0, o->computeState(MVIEWER));
  }
  catch (...) { return 0; }
}

static char* hash__doc = 
"hash(mapview,numkeys) -- create a hashed view mapping\n"
" numkeys defaults to 1\n"
" without args, creates a temporary hash on one key";

static PyObject *PyView_hash(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);

    c4_View map;			
    if (args.len() > 0) {
      MustBeView(args[0]);
      map = *(PyView*)(PyObject*)args[0];	
    }
    int numkeys = args.len() <= 1 ? 1 : (int) PWONumber(args[1]);
    return new PyView(o->Hash(map, numkeys), 0, o->computeState(MVIEWER));
  }
  catch (...) { return 0; }
}

static char* blocked__doc = 
"blocked() -- create a blocked/balanced view mapping";

static PyObject *PyView_blocked(PyView *o, PyObject *_args) {
  try {
    return new PyView(o->Blocked(), 0, o->computeState(MVIEWER));
  }
  catch (...) { return 0; }
}

static char* ordered__doc = 
"ordered(numkeys) -- create a order-maintaining view mapping\n"
" numkeys defaults to 1";

static PyObject *PyView_ordered(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    int numkeys = args.len() <= 0 ? 1 : (int) PWONumber(args[0]);
    return new PyView(o->Ordered(numkeys), 0, o->computeState(MVIEWER));
  }
  catch (...) { return 0; }
}

static char* indexed__doc = 
"indexed(map, property..., unique) -- create a mapped view which manages an index\n"
" unique defaults to 0 (not unique)";

static PyObject* PyView_indexed(PyView *o, PyObject* _args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    PyView *other = (PyView *)(PyObject* )args[0];
    bool unique = false;
    int last = args.len();
    if (PyInt_Check((PyObject*)args[last-1])) {
      PWONumber flag(args[--last]);		//XXX kwargs?
      if ((int)flag > 0)
        unique = true;
    }
    PyView crit;
    crit.addProperties(args.getSlice(1,last));
    return new PyView (o->Indexed(crit, *other, unique), 0, o->computeState(MVIEWER));
  }
  catch (...) { return 0; }
}

static char* find__doc = 
"find(criteria, start) -- return index of row found, matching criteria\n"
" criteria maybe keyword args, or a dictionary";

static PyObject *PyView_find(PyView *o, PyObject *_args, PyObject* _kwargs) {
  PWONumber start(0);
  PWOMapping crit;
  try {
    PWOSequence args(_args);
    if (_kwargs) {
      PWOMapping kwargs(_kwargs);
      if (kwargs.hasKey("start")) {
        start = kwargs["start"];
        kwargs.delItem("start");
      }
      crit = kwargs;
    }
    int numargs = args.len();
    for (int i=0; i<numargs; ++i) {
      if (PyNumber_Check((PyObject*)args[i]))
        start = args[i];
      else
        crit = args[i];
    }
    c4_Row temp;
    o->makeRow(temp, crit, false);
    return PWONumber(o->Find(temp, start)).disOwn();
  }
  catch (...) { return 0; }
}

static char* search__doc = 
"search(criteria) -- binary search (native view order), returns match or insert pos";

static PyObject *PyView_search(PyView *o, PyObject *_args, PyObject* kwargs) {
  try {
    PWOSequence args(_args);
    if (args.len() != 0)
      kwargs = args[0];
    c4_Row temp;
    o->makeRow(temp, kwargs, false);
    return PWONumber(o->Search(temp)).disOwn();
  }
  catch (...) { return 0; }
}

static char* locate__doc = 
"locate(criteria) -- binary search, returns tuple with pos and count";

static PyObject *PyView_locate(PyView *o, PyObject *_args, PyObject* kwargs) {
  try {
    PWOSequence args(_args);
    if (args.len() != 0)
      kwargs = args[0];
    c4_Row temp;
    o->makeRow(temp, kwargs, false);
    int pos = 0;
    PWONumber n (o->Locate(temp, &pos));
    PWONumber r (pos);
    PWOTuple tmp(2);
    tmp.setItem(0,r);
    tmp.setItem(1,n);
    return tmp.disOwn();
  }
  catch (...) { return 0; }
}

static char* access__doc = 
"access(memoprop, rownum, offset, length=0) -- get (partial) memo property contents";

static PyObject *PyView_access(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    if (!PyProperty_Check((PyObject *)args[0]))
      Fail(PyExc_TypeError, "First arg must be a property");

    c4_BytesProp& prop = *(c4_BytesProp*)(c4_Property*)(PyProperty*)(PyObject*)args[0];

    int index = PyInt_AsLong(args[1]);
    if (index < 0 || index >= o->GetSize())
      Fail(PyExc_IndexError, "Index out of range");

    c4_RowRef row = o->GetAt(index);

    long offset = PyInt_AsLong(args[2]);
    int length = args.len() == 3 ? 0 : PyInt_AsLong(args[3]);
    if (length <= 0)
    {
      length = prop(row).GetSize() - offset;
      if (length < 0)
        length = 0;
    }

    PyObject* buffer = PyString_FromStringAndSize(0, length);
    int o = 0;

    while (o < length)
    {
      c4_Bytes buf = prop(row).Access(offset + o, length - o);
      int n = buf.Size();
      if (n == 0)
        break;
      memcpy(PyString_AS_STRING(buffer) + o, buf.Contents(), n);
      o += n;
    }

    if (o < length)
      _PyString_Resize(&buffer, o);

    return buffer;
  }
  catch (...) { return 0; }
}

static char* modify__doc = 
"modify(memoprop, rownum, string, offset, diff=0) -- store (partial) memo contents\n"
"diff removes (<0) or inserts (>0) bytes, and is adjusted to within sensible range";

static PyObject *PyView_modify(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    if (!PyProperty_Check((PyObject *)args[0]))
      Fail(PyExc_TypeError, "First arg must be a property");

    c4_BytesProp& prop = *(c4_BytesProp*)(c4_Property*)(PyProperty*)(PyObject*)args[0];

    int index = PWONumber(args[1]);
    if (index < 0 || index >= o->GetSize())
      Fail(PyExc_IndexError, "Index out of range");

    c4_RowRef row = o->GetAt(index);

    PWOString buffer (args[2]);
    c4_Bytes data ((void*)(const char*)buffer, buffer.len());

    long offset = PWONumber(args[3]);
    int diff = args.len() == 4 ? 0 : (int) PWONumber(args[4]);

    if (!prop(row).Modify(data, offset, diff))
      Fail(PyExc_TypeError, "Failed to modify memo field");

    Py_INCREF(Py_None);
    return Py_None;
  }
  catch (...) { return 0; }
}

static char* itemsize__doc = 
"itemsize(prop, rownum=0) -- return size of item (rownum only needed for S/B/M types)\n"
"with integer fields, a result of -1/-2/-4 means 1/2/4 bits per value, respectively";

static PyObject *PyView_itemsize(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    if (!PyProperty_Check((PyObject *)args[0]))
      Fail(PyExc_TypeError, "First arg must be a property");

    c4_BytesProp& prop = *(c4_BytesProp*)(c4_Property*)(PyProperty*)(PyObject*)args[0];
    int index = args.len() == 1 ? 0 : (int) PWONumber(args[1]);
    if (index < 0 || index >= o->GetSize())
      Fail(PyExc_IndexError, "Index out of range");

    return PWONumber(prop(o->GetAt(index)).GetSize()).disOwn();
  }
  catch (...) { return 0; }
}

static char* relocrows__doc = 
"relocrows(from, count, dest, pos) -- relocate rows within views of same storage\n"
"from is source offset, count is number of rows, pos is destination offset\n"
"both views must have a compatible structure (field names may differ)";

static PyObject *PyView_relocrows(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    if (!PyView_Check((PyObject *)args[2]))
      Fail(PyExc_TypeError, "Third arg must be a view object");
    
    PyView& dest = *(PyView *)(PyObject *)args[2];  	
    
    int from = PWONumber(args[0]);
    if (from < 0)
      from += o->GetSize();
    int count = PWONumber(args[1]);
    if (from < 0 || count < 0 || from + count > o->GetSize())
      Fail(PyExc_IndexError, "Source index out of range");

    int pos = PWONumber(args[3]);
    if (pos < 0)
      pos += dest.GetSize();
    if (pos < 0 || pos > dest.GetSize())
      Fail(PyExc_IndexError, "Destination index out of range");
    
    if (!o->RelocateRows(from, count, dest, pos))
      Fail(PyExc_TypeError, "Failed to relocate rows");

    Py_INCREF(Py_None);
    return Py_None;
  }
  catch (...) { return 0; }
}

static char* map__doc = 
"map(func, subset=None) -- apply func to each row of view,\n"
"or (if subset specified) to each row in view that is also in subset.\n"
"Returns None: view is mutated\n"
"func must have the signature func(row), and may mutate row.\n"
"subset must be a subset of view: eg, customers.map(func, customers.select(....)).\n";

static PyObject *PyView_map(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    PWOCallable func(args[0]);
    if (args.len() > 1) {
      if (!PyView_Check((PyObject *)args[1]))
        Fail(PyExc_TypeError, "Second arg must be a view object");
    
      PyView& subset = *(PyView *)(PyObject *)args[1];	
      
      o->map(func, subset);
    }
    else
      o->map(func);

    Py_INCREF(Py_None);
    return Py_None;
  }
  catch (...) { return 0; }
}
static char* filter__doc = 
"filter(func) -- return a new view containing the indices of those rows satisfying func.\n"
"  func must have the signature func(row), and should return a false value to omit row.";

static PyObject *PyView_filter(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    PWOCallable func(args[0]);
    return o->filter(func);
  }
  catch (...) { return 0; }
}
static char* reduce__doc = 
"reduce(func, start=0) -- return the result of applying func(row, lastresult) to\n"
"each row in view.\n";

static PyObject *PyView_reduce(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    PWOCallable func(args[0]);
    PWONumber start(0);
    if (args.len() > 1) 
      start = args[1];
    return o->reduce(func, start);
  }
  catch (...) { return 0; }
}
static char* remove__doc = 
"remove(indices) -- remove all rows whose indices are in subset from view\n"
"Not the same as minus, because unique is not required, and view is not reordered.\n";

static PyObject *PyView_remove(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);
    
    PyView& subset = *(PyView *)(PyObject *)args[0];	
    o->remove(subset);
    Py_INCREF(Py_None);
    return Py_None;
  }
  catch (...) { return 0; }
}
static char *indices__doc =
"indices(subset) -- returns a view containing the indices in view of the rows of subset";

static PyObject *PyView_indices(PyView *o, PyObject *_args) {
  try {
    PWOSequence args(_args);
    MustBeView(args[0]);

    PyView& subset = *(PyView *)(PyObject *)args[0];	
    return o->indices(subset);
  }
  catch (...) { return 0; }
}
static char *copy__doc =
"copy() -- returns a copy of the view\n";

static PyObject *PyView_copy(PyView *o, PyObject *_args) {
  try {
    return new PyView(o->Duplicate());
  }
  catch (...) { return 0; }
}

static PyMethodDef ViewMethods[] = {
  {"setsize", (PyCFunction)PyView_setsize, METH_VARARGS, setsize__doc},
  {"insert", (PyCFunction)PyView_insert, METH_VARARGS | METH_KEYWORDS, insert__doc},
  {"append", (PyCFunction)PyView_append, METH_VARARGS | METH_KEYWORDS, append__doc},
  {"delete", (PyCFunction)PyView_delete, METH_VARARGS, delete__doc},
  {"structure", (PyCFunction)PyView_structure, METH_VARARGS, structure__doc},
  {"select", (PyCFunction)PyView_select, METH_VARARGS | METH_KEYWORDS, select__doc},
  {"addproperty", (PyCFunction)PyView_addproperty, METH_VARARGS, addproperty__doc},
  {"sort", (PyCFunction)PyView_sort, METH_VARARGS, sort__doc},
  {"sortrev", (PyCFunction)PyView_sortrev, METH_VARARGS, sortrev__doc},
  {"project", (PyCFunction)PyView_project, METH_VARARGS, project__doc},
  {"flatten", (PyCFunction)PyView_flatten, METH_VARARGS | METH_KEYWORDS, flatten__doc},
  {"join", (PyCFunction)PyView_join, METH_VARARGS | METH_KEYWORDS, join__doc},
  {"groupby", (PyCFunction)PyView_groupby, METH_VARARGS, groupby__doc},
  {"counts", (PyCFunction)PyView_counts, METH_VARARGS, counts__doc},
  {"product", (PyCFunction)PyView_product, METH_VARARGS, product__doc},
  {"union", (PyCFunction)PyView_union, METH_VARARGS, union__doc},
  {"intersect", (PyCFunction)PyView_intersect, METH_VARARGS, intersect__doc},
  {"different", (PyCFunction)PyView_different, METH_VARARGS, different__doc},
  {"minus", (PyCFunction)PyView_minus, METH_VARARGS, minus__doc},
  {"remapwith", (PyCFunction)PyView_remapwith, METH_VARARGS, remapwith__doc},
  {"pair", (PyCFunction)PyView_pair, METH_VARARGS, pair__doc},
  {"rename", (PyCFunction)PyView_rename, METH_VARARGS, rename__doc},
  {"unique", (PyCFunction)PyView_unique, METH_VARARGS, unique__doc},
  {"hash", (PyCFunction)PyView_hash, METH_VARARGS, hash__doc},
  {"blocked", (PyCFunction)PyView_blocked, METH_VARARGS, blocked__doc},
  {"ordered", (PyCFunction)PyView_ordered, METH_VARARGS, ordered__doc},
  {"indexed", (PyCFunction)PyView_indexed, METH_VARARGS, indexed__doc},
  {"find", (PyCFunction)PyView_find, METH_VARARGS | METH_KEYWORDS, find__doc},
  {"search", (PyCFunction)PyView_search, METH_VARARGS | METH_KEYWORDS, search__doc},
  {"locate", (PyCFunction)PyView_locate, METH_VARARGS | METH_KEYWORDS, locate__doc},
  {"access", (PyCFunction)PyView_access, METH_VARARGS, access__doc},
  {"modify", (PyCFunction)PyView_modify, METH_VARARGS, modify__doc},
  {"itemsize", (PyCFunction)PyView_itemsize, METH_VARARGS, itemsize__doc},
// {"relocrows", (PyCFunction)PyView_relocrows, METH_VARARGS, relocrows__doc},
  {"map", (PyCFunction)PyView_map, METH_VARARGS, map__doc},
  {"filter", (PyCFunction)PyView_filter, METH_VARARGS, filter__doc},
  {"reduce", (PyCFunction)PyView_reduce, METH_VARARGS, reduce__doc},
  {"remove", (PyCFunction)PyView_remove, METH_VARARGS, remove__doc},
  {"indices", (PyCFunction)PyView_indices, METH_VARARGS, indices__doc},
  {"copy", (PyCFunction)PyView_copy, METH_VARARGS, copy__doc},
  {0, 0, 0, 0}
};
static PyMethodDef ViewerMethods[] = {
  {"structure", (PyCFunction)PyView_structure, METH_VARARGS, structure__doc},
  {"select", (PyCFunction)PyView_select, METH_VARARGS | METH_KEYWORDS, select__doc},
  {"addproperty", (PyCFunction)PyView_addproperty, METH_VARARGS, addproperty__doc},
  {"sort", (PyCFunction)PyView_sort, METH_VARARGS, sort__doc},
  {"sortrev", (PyCFunction)PyView_sortrev, METH_VARARGS, sortrev__doc},
  {"project", (PyCFunction)PyView_project, METH_VARARGS, project__doc},
  {"flatten", (PyCFunction)PyView_flatten, METH_VARARGS | METH_KEYWORDS, flatten__doc},
  {"join", (PyCFunction)PyView_join, METH_VARARGS | METH_KEYWORDS, join__doc},
  {"groupby", (PyCFunction)PyView_groupby, METH_VARARGS, groupby__doc},
  {"counts", (PyCFunction)PyView_counts, METH_VARARGS, counts__doc},
  {"product", (PyCFunction)PyView_product, METH_VARARGS, product__doc},
  {"union", (PyCFunction)PyView_union, METH_VARARGS, union__doc},
  {"intersect", (PyCFunction)PyView_intersect, METH_VARARGS, intersect__doc},
  {"different", (PyCFunction)PyView_different, METH_VARARGS, different__doc},
  {"minus", (PyCFunction)PyView_minus, METH_VARARGS, minus__doc},
  {"remapwith", (PyCFunction)PyView_remapwith, METH_VARARGS, remapwith__doc},
  {"pair", (PyCFunction)PyView_pair, METH_VARARGS, pair__doc},
  {"rename", (PyCFunction)PyView_rename, METH_VARARGS, rename__doc},
  {"unique", (PyCFunction)PyView_unique, METH_VARARGS, unique__doc},
  {"hash", (PyCFunction)PyView_hash, METH_VARARGS, hash__doc},
  {"blocked", (PyCFunction)PyView_blocked, METH_VARARGS, blocked__doc},
  {"ordered", (PyCFunction)PyView_ordered, METH_VARARGS, ordered__doc},
  {"indexed", (PyCFunction)PyView_indexed, METH_VARARGS, indexed__doc},
  {"find", (PyCFunction)PyView_find, METH_VARARGS | METH_KEYWORDS, find__doc},
  {"search", (PyCFunction)PyView_search, METH_VARARGS | METH_KEYWORDS, search__doc},
  {"locate", (PyCFunction)PyView_locate, METH_VARARGS | METH_KEYWORDS, locate__doc},
  {"access", (PyCFunction)PyView_access, METH_VARARGS, access__doc},
  {"modify", (PyCFunction)PyView_modify, METH_VARARGS, modify__doc},
  {"itemsize", (PyCFunction)PyView_itemsize, METH_VARARGS, itemsize__doc},
  {"map", (PyCFunction)PyView_map, METH_VARARGS, map__doc},
  {"filter", (PyCFunction)PyView_filter, METH_VARARGS, filter__doc},
  {"reduce", (PyCFunction)PyView_reduce, METH_VARARGS, reduce__doc},
  {"indices", (PyCFunction)PyView_indices, METH_VARARGS, indices__doc},
  {"copy", (PyCFunction)PyView_copy, METH_VARARGS, copy__doc},
  {0, 0, 0, 0}
};

/*
  Duplicate(deep=0)  (__copy__ and __deepcopy__ as methods, too)
  Clone()
*/
static int PyView_length(PyView *o) {
  try {
    return o->GetSize();
  }
  catch (...) { return -1; }
}

static PyObject* PyView_concat(PyView *o, PyView *other) {
  try {
    if (other->ob_type != &PyViewtype)
      Fail(PyExc_TypeError, "Not a PyView");
    return new PyView(o->Concat(*other), 0, o->computeState(RWVIEWER));
  }
  catch (...) { return 0; }
}

static PyObject* PyView_repeat(PyView *o, int n) {
  try {
    PyView* tmp = new PyView;
    while (--n >= 0) 
      tmp = new PyView(tmp->Concat(*o), 0, o->computeState(RWVIEWER)); //!! a huge stack of views?
    return tmp;
  }
  catch (...) { return 0; }
}

static PyObject* PyView_getitem(PyView *o, int n) {
  try {
    PyObject *rslt = o->getItem(n);
    if (rslt == 0)
      PyErr_SetString(PyExc_IndexError, "Index out of range");
    return rslt;
  }
  catch (...) { return 0; }
}

static PyObject* PyView_getslice(PyView *o, int s, int e) {
  try {
    return o->getSlice(s,e);
  }
  catch (...) { return 0; }
}

static int PyView_setitem(PyView *o, int n, PyObject* v) {
  try {
    if (n < 0)
      n += o->GetSize();
    if (n >= o->GetSize() || n < 0)
      Fail(PyExc_IndexError, "Index out of range");
    if (v == 0) {
      o->RemoveAt(n);
      return 0;
    }

    return o->setItem(n,v);
  }
  catch (...) { return -1; }
}

static int PyView_setslice(PyView *o, int s, int e, PyObject* v) {
  try {
    if (v == 0) {
      PWOTuple seq;
      return o->setSlice(s,e,seq);
    }

    PWOSequence seq(v);
    return o->setSlice(s,e,seq);
  }
  catch (...) { return -1; }
}

static PySequenceMethods ViewAsSeq = {
  (inquiry)PyView_length, //sq_length
  (binaryfunc)PyView_concat, //sq_concat
  (intargfunc)PyView_repeat, //sq_repeat
  (intargfunc)PyView_getitem, //sq_item
  (intintargfunc)PyView_getslice, //sq_slice
  (intobjargproc)PyView_setitem, //sq_ass_item
  (intintobjargproc)PyView_setslice, //sq_ass_slice
};
static PySequenceMethods ViewerAsSeq = {
  (inquiry)PyView_length, //sq_length
  (binaryfunc)0, //sq_concat
  (intargfunc)0, //sq_repeat
  (intargfunc)PyView_getitem, //sq_item
  (intintargfunc)PyView_getslice, //sq_slice
  (intobjargproc)0, //sq_ass_item
  (intintobjargproc)0, //sq_ass_slice
};


static void PyView_dealloc(PyView *o) {
  //o->~PyView();
  delete o;
}

static int PyView_print(PyView *o, FILE *f, int) {
  fprintf(f, "<PyView object at %x>", (int)o);
  return 0;
}
static int PyViewer_print(PyView *o, FILE *f, int) {
  fprintf(f, "<PyViewer object at %x>", (int)o);
  return 0;
}
static int PyROViewer_print(PyView *o, FILE *f, int) {
  fprintf(f, "<PyROViewer object at %x>", (int)o);
  return 0;
}

static PyObject* PyView_getattr(PyView *o, char *nm) {
  PyObject *rslt;
  try {
    rslt = Py_FindMethod(ViewMethods, o, nm);
    if (rslt)
      return rslt;
    PyErr_Clear();
    int ndx = o->FindPropIndexByName(nm);
    if (ndx < 0) 
      Fail(PyExc_AttributeError, nm);
    return new PyProperty(o->NthProperty(ndx));
  }
  catch (...) { return 0; }
}
static PyObject* PyViewer_getattr(PyView *o, char *nm) {
  PyObject *rslt;
  try {
    rslt = Py_FindMethod(ViewerMethods, o, nm);
    if (rslt)
      return rslt;
    PyErr_Clear();
    int ndx = o->FindPropIndexByName(nm);
    if (ndx < 0) 
      Fail(PyExc_AttributeError, nm);
    return new PyProperty(o->NthProperty(ndx));
  }
  catch (...) { return 0; }
}


PyTypeObject PyViewtype = {
  PyObject_HEAD_INIT(&PyType_Type)
  0,
  "PyView",
  sizeof(PyView),
  0,
  (destructor)PyView_dealloc, /*tp_dealloc*/
  (printfunc)PyView_print, /*tp_print*/
  (getattrfunc)PyView_getattr, /*tp_getattr*/
  0,    /*tp_setattr*/
  (cmpfunc)0, /*tp_compare*/
  (reprfunc)0, /*tp_repr*/
  0,    /*tp_as_number*/
  &ViewAsSeq, /*tp_as_sequence*/
  0,    /*tp_as_mapping*/
};
PyTypeObject PyViewertype = {
  PyObject_HEAD_INIT(&PyType_Type)
  0,
  "PyViewer",
  sizeof(PyView),
  0,
  (destructor)PyView_dealloc, /*tp_dealloc*/
  (printfunc)PyViewer_print, /*tp_print*/
  (getattrfunc)PyViewer_getattr, /*tp_getattr*/
  0,    /*tp_setattr*/
  (cmpfunc)0, /*tp_compare*/
  (reprfunc)0, /*tp_repr*/
  0,    /*tp_as_number*/
  &ViewerAsSeq, /*tp_as_sequence*/
  0,    /*tp_as_mapping*/
};
PyTypeObject PyROViewertype = {
  PyObject_HEAD_INIT(&PyType_Type)
  0,
  "PyROViewer",
  sizeof(PyView),
  0,
  (destructor)PyView_dealloc, /*tp_dealloc*/
  (printfunc)PyROViewer_print, /*tp_print*/
  (getattrfunc)PyViewer_getattr, /*tp_getattr*/
  0,    /*tp_setattr*/
  (cmpfunc)0, /*tp_compare*/
  (reprfunc)0, /*tp_repr*/
  0,    /*tp_as_number*/
  &ViewerAsSeq, /*tp_as_sequence*/
  0,    /*tp_as_mapping*/
};
int PyView::computeState(int targettype) {
    int newtype = _state | targettype;
    if (newtype > FINALNOTIFIABLE) 
        newtype = ROVIEWER;
    if (_state == FINALNOTIFIABLE)
        newtype = ROVIEWER;
    return newtype;
}
PyTypeObject *getTypeObject(int type) {
    switch (type) {
        case BASE:
        case MVIEWER:
            return &PyViewtype;
            break;
        case NOTIFIABLE:
        case RWVIEWER:
        case FINALNOTIFIABLE:
            return &PyViewertype;
        case ROVIEWER:
            return &PyROViewertype;
    }
    return &PyViewtype;
}


PyObject* PyView_new(PyObject* o, PyObject* _args) {
  return new PyView;
}

PyView::PyView() : PyHead(PyViewtype), _base (0), _state(BASE) {
}

PyView::PyView(const c4_View& o, PyView *owner, int state)
  : PyHead(PyViewtype), c4_View(o), _base (owner), _state(state) {
  ob_type = getTypeObject(_state);
  if (owner && owner->_base)
    _base = owner->_base;
}

void PyView::makeRow(c4_Row& tmp, PyObject* o, bool useDefaults) {
  for (int i=0; i < NumProperties(); i++) {
    const c4_Property& prop = NthProperty(i);
    PyObject* attr = 0;
    if (o) {
      if (PyDict_Check(o))
      {
        attr = PyDict_GetItemString(o, (char *)prop.Name());
        Py_XINCREF(attr);
      }
      else if (PySequence_Check(o))
      {
        attr = PySequence_GetItem(o, i);
      }
      else
      {
        attr = PyObject_GetAttrString(o, (char *)prop.Name());
	if (attr == 0 && i == 0 && NumProperties() == 1) 
	{
	  PyErr_Clear();
	  attr = o;
	  Py_XINCREF(attr);
	}
      }
    }
    if (attr)
    {
      try { PyRowRef::setFromPython(tmp, prop, attr); }
      catch (...) { Py_DECREF(attr); throw; }
      Py_DECREF(attr);
    }
    else {
      PyErr_Clear();
      if (useDefaults)
        PyRowRef::setDefault(tmp, prop);
    }
  }
  if (!useDefaults)
    if (tmp.Container().NumProperties() == 0)
      Fail(PyExc_ValueError, "Object has no usable attributes");
}

void PyView::insertAt(int i, PyObject* o) {
  c4_Row temp;
  makeRow(temp, o);
  InsertAt(i, temp);
}

PyObject* PyView::structure() {
  int n = NumProperties();
  PWOList rslt(n);
  for (int i = 0; i < n; i++) {
    rslt[i] = new PyProperty(NthProperty(i));
  }
  return rslt.disOwn();
}

PyView *PyView::getSlice(int s, int e) {
  int sz = GetSize();
  if (s < 0)
    s += sz;
  if (e < 0)
    e += sz;
  if (e > sz)
    e = sz;
  if (s >= 0 && s < sz)
    if (e > s && e <= sz)
      return new PyView(Slice(s,e), 0, computeState(RWVIEWER));
  return new PyView(Clone());
}

int PyView::setSlice(int s, int e, const PWOSequence& lst) {
  int sz = GetSize();
  if (s < 0)
    s += sz;
  if (e < 0)
    e += sz;
  if (e > sz)
    e = sz;
  int i = 0;
  for (; i < lst.len() && s < e; i++, s++)
    setItem(s, lst[i]);
  for (; i < lst.len(); i++, s++)
  {
    if (_base)
      Fail(PyExc_RuntimeError, "Can't insert in this view");
    insertAt(s, lst[i]);
  }
  if (s < e)
    if (_base)
      while (s < e)
      {
        int ndx = _base->GetIndexOf(GetAt(s));
        _base->RemoveAt(ndx, 1);
        --e;
      }
    else
      RemoveAt(s, e - s);
  return 0;
}

PyRowRef *PyView::getItem(int i) {
  if (i < 0)
    i += GetSize();
  if (i >= GetSize() || i < 0)
    return 0;
  if (_base && !(_state & IMMUTABLEROWS)) {
    c4_RowRef derived = GetAt(i);
    int ndx = _base->GetIndexOf(derived);
    if (ndx >= 0)
        return new PyRowRef(_base->GetAt(ndx), _state & IMMUTABLEROWS);
  }
  return  new PyRowRef(GetAt(i), _state & IMMUTABLEROWS);
}

int PyView::setItem(int i, PyObject* v) {
  if (PyGenericRowRef_Check(v))
    return setItemRow(i, *(PyRowRef *)v);
  c4_Row temp;
  makeRow(temp, v, false);
  return setItemRow(i, temp);
}

void PyView::addProperties(const PWOSequence& lst) {
  for (int i=0; i<lst.len(); i++) {
    if (PyProperty_Check((PyObject* )lst[i])) {
      AddProperty(*(PyProperty*)(PyObject* )lst[i]);
    }
  }
}

void PyView::map(const PWOCallable& func) {
  PWOTuple tmp(1);
  for (int i=0; i<GetSize(); ++i) {
    PyRowRef *row = new PyRowRef(GetAt(i));
    PWOBase r2 (row);
    tmp.setItem(0,r2);
    func.call(tmp);
    Py_DECREF(row);
  }
}
void PyView::map(const PWOCallable& func, const PyView& subset) {
  int sz = subset.GetSize();
  PWOTuple tmp(1);
  for (int i=0; i<sz; ++i) {
    PyRowRef *row = new PyRowRef(GetAt(GetIndexOf(subset.GetAt(i))));
    PWOBase r2 (row);
    tmp.setItem(0,r2);
    func.call(tmp);
    Py_DECREF(row);
  }
}
static c4_IntProp _index("index");

PyView *PyView::indices(const PyView& subset) {
  c4_View tmp(_index);
  tmp.SetSize(subset.GetSize());
  c4_Row row;
  for (int i=0; i<subset.GetSize(); ++i) {
    _index(row) = GetIndexOf(subset.GetAt(i));
    tmp.SetAt(i, row);
  }
  return new PyView(tmp);
}
void PyView::remove(const PyView& indices) {
  c4_View tmp = indices.Sort();
  for (int i=indices.GetSize()-1; i >= 0; --i) 
    RemoveAt(_index(tmp.GetAt(i)));
}
PyView *PyView::filter(const PWOCallable& func) {
  c4_View indices(_index);
  c4_Row ndx;
  PWOTuple tmp(1);
  for (int i=0; i<GetSize(); ++i) {
    PyRowRef *row = new PyRowRef(GetAt(i));
    PWOBase r2 (row);
    tmp.setItem(0,r2);
    PWOBase rslt(func.call(tmp));
    if (rslt.isTrue()) {
      _index(ndx) = i;
      indices.Add(ndx);
    }
    Py_DECREF(row);
  }
  return new PyView(indices);
}
PyObject *PyView::reduce(const PWOCallable& func, PWONumber& start) {
  PWONumber accum = start;
  PWOTuple tmp(2);
  for (int i=0; i<GetSize(); ++i) {
    PyRowRef *row = new PyRowRef(GetAt(i));
    PWOBase r2 (row);
    tmp.setItem(0,r2);
    tmp.setItem(1,accum);
    PWOBase rslt(func.call(tmp));
    accum = rslt;
    Py_DECREF(row);
  }
  return accum;
}