File tree Expand file tree Collapse file tree 4 files changed +39
-1
lines changed Expand file tree Collapse file tree 4 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -940,7 +940,7 @@ The C++ signature of ``find_values`` is::
940
940
// Returns underlying Item (which contains either Pair or Loop).
941
941
Item* item();
942
942
943
- // Returns pointer to the DOM structure containing the whole table .
943
+ // If it's in Loop, returns pointer to Loop; otherwise, nullptr .
944
944
Loop* get_loop() const;
945
945
946
946
// Get raw value (no bounds checking).
@@ -952,6 +952,9 @@ The C++ signature of ``find_values`` is::
952
952
// Short-cut for cif::as_string(column.at(n)).
953
953
std::string str(int n) const;
954
954
955
+ // Erases item for name-value pair; removes column for Loop
956
+ void erase();
957
+
955
958
``Column `` also provides support for C++11 range-based ``for ``::
956
959
957
960
// mmCIF _chem_comp_atom is usually a table, but not always
@@ -1011,6 +1014,16 @@ to this ``Loop`` in the DOM. Otherwise it returns ``None``.
1011
1014
>>> column.get_loop()
1012
1015
<gemmi.cif.Loop 12 x 7>
1013
1016
1017
+ ``erase() `` removes the column from a loop, if this column is in a loop;
1018
+ if it's a tag-value pair it erases the containing item.
1019
+
1020
+ .. doctest ::
1021
+
1022
+ >>> loop = column.get_loop()
1023
+ >>> column.erase()
1024
+ >>> loop # one column less
1025
+ <gemmi.cif.Loop 12 x 6>
1026
+
1014
1027
Table
1015
1028
=====
1016
1029
Original file line number Diff line number Diff line change @@ -199,6 +199,11 @@ struct Loop {
199
199
int n = find_tag (column_name);
200
200
if (n == -1 )
201
201
fail (" remove_column(): tag not found: " + column_name);
202
+ remove_column_at (n);
203
+ }
204
+
205
+ // / \pre: n < tags.size()
206
+ void remove_column_at (size_t n) {
202
207
tags.erase (tags.begin () + n);
203
208
vector_remove_column (values, tags.size (), n);
204
209
}
@@ -264,6 +269,8 @@ class Column {
264
269
Item* item () { return item_; }
265
270
size_t col () const { return col_; }
266
271
272
+ void erase ();
273
+
267
274
private:
268
275
Item* item_;
269
276
size_t col_; // for loop this is a column index in item_->loop
@@ -657,6 +664,13 @@ inline std::string* Column::get_tag() {
657
664
return &item_->pair [0 ];
658
665
}
659
666
667
+ inline void Column::erase () {
668
+ if (Loop* loop = get_loop ())
669
+ loop->remove_column_at (col_);
670
+ else if (item_)
671
+ item_->erase ();
672
+ }
673
+
660
674
inline Loop* Column::get_loop () const {
661
675
return item_ && item_->type == ItemType::Loop ? &item_->loop : nullptr ;
662
676
}
Original file line number Diff line number Diff line change @@ -370,6 +370,7 @@ void add_cif(py::module& cif) {
370
370
self.at (idx) = value;
371
371
})
372
372
.def (" str" , &Column::str, py::arg (" index" ))
373
+ .def (" erase" , &Column::erase)
373
374
.def (" __repr__" , [](const Column &self) {
374
375
std::string s = " <gemmi.cif.Column " ;
375
376
if (const std::string* tag = self.get_tag ())
Original file line number Diff line number Diff line change @@ -350,6 +350,16 @@ def test_case_sensitivity(self):
350
350
self .assertEqual (block .get_index ('_lBBB' ), 5 )
351
351
self .assertEqual (block .find_pair ('_Three' ), ('_thrEE' , '3' ))
352
352
353
+ values .erase ()
354
+ block .find_values ('_two' ).erase ()
355
+ block .find_values ('_nonloop_b' ).erase ()
356
+ expected = """\
357
+ data_test
358
+ _One 1 _thrEE 3
359
+ _NonLoop_a alpha
360
+ loop_ _lbBb _ln B 1 D 2"""
361
+ self .assertEqual (block .as_string ().split (), expected .split ())
362
+
353
363
class TestQuote (unittest .TestCase ):
354
364
def test_quote (self ):
355
365
self .assertEqual (cif .quote ('a.b-c' ), 'a.b-c' )
You can’t perform that action at this time.
0 commit comments