Skip to content

Commit 3467d24

Browse files
committed
remove python/tostr.hpp
replace tostr() with cat(), snprintf_z() and nb::str::format
1 parent a928991 commit 3467d24

File tree

7 files changed

+31
-73
lines changed

7 files changed

+31
-73
lines changed

docs/grid.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ or when iterating the grid:
236236
>>> for point in grid:
237237
... if point.value != 0.: print(point)
238238
<gemmi.FloatGridBase.Point (0, 0, 0) -> 0.125>
239-
<gemmi.FloatGridBase.Point (1, 1, 1) -> 7>
240-
<gemmi.FloatGridBase.Point (11, 1, 11) -> 7>
239+
<gemmi.FloatGridBase.Point (1, 1, 1) -> 7.0>
240+
<gemmi.FloatGridBase.Point (11, 1, 11) -> 7.0>
241241

242242
The point can be converted to its index (position in the array):
243243

@@ -266,7 +266,7 @@ The other way around, we can find the grid point nearest to a position:
266266
.. doctest::
267267

268268
>>> grid.get_nearest_point(_)
269-
<gemmi.FloatGridBase.Point (6, 6, 6) -> 0>
269+
<gemmi.FloatGridBase.Point (6, 6, 6) -> 0.0>
270270

271271

272272
Common operations
@@ -504,7 +504,7 @@ The primary use for MaskedGrid is working with asymmetric unit (asu) only:
504504
>>> for point in asu:
505505
... if point.value != 0: print(point)
506506
<gemmi.FloatGridBase.Point (0, 0, 0) -> 0.125>
507-
<gemmi.FloatGridBase.Point (1, 1, 1) -> 7>
507+
<gemmi.FloatGridBase.Point (1, 1, 1) -> 7.0>
508508

509509

510510
.. _solventmask:

docs/hkl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ Each item in AsuData has two properties, hkl and value:
11081108
.. doctest::
11091109

11101110
>>> asu_data[158]
1111-
<gemmi.ComplexHklValue (-6,2,5) (-1.37694,-0.190087)>
1111+
<gemmi.ComplexHklValue (-6,2,5) (-1.376941204071045-0.19008657336235046j)>
11121112
>>> _.hkl, _.value
11131113
([-6, 2, 5], (-1.376941204071045-0.19008657336235046j))
11141114

python/grid.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ bool operator>(const std::complex<float>& a, const std::complex<float>& b) {
2424
#include "gemmi/solmask.hpp" // for SolventMasker, mask_points_in_constant_radius
2525
#include "gemmi/blob.hpp" // for Blob, find_blobs_by_flood_fill
2626
#include "gemmi/asumask.hpp" // for MaskedGrid
27-
#include "tostr.hpp"
2827

2928
using namespace gemmi;
3029

@@ -53,8 +52,8 @@ nb::class_<GridBase<T>, GridMeta> add_grid_base(nb::module_& m, const char* name
5352
[](const GrPoint& self) { return *self.value; },
5453
[](GrPoint& self, T x) { *self.value = x; })
5554
.def("__repr__", [=](const GrPoint& self) {
56-
return tostr("<gemmi.", name, ".Point (", self.u, ", ", self.v, ", ",
57-
self.w, ") -> ", +*self.value, '>');
55+
return nb::str("<gemmi.{}.Point ({}, {}, {}) -> {}>")
56+
.format(name, self.u, self.v, self.w, self.value);
5857
});
5958

6059
auto to_array = [](GrBase& gr) { return grid_to_array(gr, gr.data); };

python/hkl.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "gemmi/unitcell.hpp"
1212
#include "gemmi/refln.hpp"
1313
#include "gemmi/fourier.hpp" // for get_size_for_hkl, get_f_phi_on_grid, ...
14-
#include "tostr.hpp"
1514
#include "gemmi/fprime.hpp" // for cromer_liberman
1615
#include "gemmi/reciproc.hpp" // for count_reflections, make_miller_vector
1716
#include "gemmi/cif2mtz.hpp" // for CifToMtz
@@ -24,19 +23,6 @@ using namespace gemmi;
2423

2524
NB_MAKE_OPAQUE(std::vector<ReflnBlock>)
2625

27-
namespace gemmi {
28-
// operator<< is used by stl_bind for vector's __repr__
29-
inline std::ostream& operator<< (std::ostream& os, const ReflnBlock& rb) {
30-
os << "<gemmi.ReflnBlock " << rb.block.name << " with ";
31-
if (rb.default_loop)
32-
os << rb.default_loop->width() << " x " << rb.default_loop->length();
33-
else
34-
os << " no ";
35-
os << " loop>";
36-
return os;
37-
}
38-
}
39-
4026
void add_hkl(nb::module_& m) {
4127
nb::class_<ReflnBlock> pyReflnBlock(m, "ReflnBlock");
4228
nb::bind_vector<std::vector<ReflnBlock>, rv_ri>(m, "ReflnBlocks");
@@ -128,8 +114,15 @@ void add_hkl(nb::module_& m) {
128114
.def("is_unmerged", &ReflnBlock::is_unmerged)
129115
.def("use_unmerged", &ReflnBlock::use_unmerged)
130116
.def("__bool__", [](const ReflnBlock& self) { return self.ok(); })
131-
.def("__repr__", [](const ReflnBlock& self) { return tostr(self); })
132-
;
117+
.def("__repr__", [](const ReflnBlock& self) {
118+
std::string s = cat("<gemmi.ReflnBlock ", self.block.name, " with ");
119+
if (self.default_loop)
120+
cat_to(s, self.default_loop->width(), " x ", self.default_loop->length());
121+
else
122+
s += " no ";
123+
s += " loop>";
124+
return s;
125+
});
133126
m.def("as_refln_blocks",
134127
[](cif::Document& d) { return as_refln_blocks(std::move(d.blocks)); });
135128
m.def("hkl_cif_as_refln_block", &hkl_cif_as_refln_block, nb::arg("block"));

python/recgrid.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@
99

1010
#include "gemmi/recgrid.hpp"
1111
#include "gemmi/fourier.hpp" // for get_size_for_hkl, get_f_phi_on_grid
12-
#include "tostr.hpp"
12+
#include "gemmi/sprintf.hpp"
1313

1414
using namespace gemmi;
1515

16-
namespace gemmi {
17-
std::ostream& operator<< (std::ostream& os, const ValueSigma<float>& vs) {
18-
os << vs.value << " +/- " << vs.sigma;
19-
return os;
20-
}
21-
}
22-
16+
namespace {
2317
template<typename T, typename Func>
2418
auto make_new_column(const AsuData<T>& asu_data, Func f) {
2519
if (!asu_data.unit_cell().is_crystal())
@@ -71,9 +65,8 @@ void add_asudata(nb::module_& m, const std::string& prefix) {
7165
.def_ro("hkl", &HklValue<T>::hkl)
7266
.def_rw("value", &HklValue<T>::value)
7367
.def("__repr__", [prefix](const HklValue<T>& self) {
74-
return tostr("<gemmi.", prefix, "HklValue (",
75-
self.hkl[0], ',', self.hkl[1], ',', self.hkl[2], ") ",
76-
self.value, '>');
68+
return nb::str("<gemmi.{}HklValue ({},{},{}) {}>")
69+
.format(prefix, self.hkl[0], self.hkl[1], self.hkl[2], self.value);
7770
});
7871

7972
using AsuData = AsuData<T>;
@@ -145,7 +138,7 @@ void add_asudata(nb::module_& m, const std::string& prefix) {
145138
return new AsuData(self);
146139
})
147140
.def("__repr__", [prefix](const AsuData& self) {
148-
return tostr("<gemmi.", prefix, "AsuData with ", self.v.size(), " values>");
141+
return cat("<gemmi.", prefix, "AsuData with ", self.v.size(), " values>");
149142
});
150143
add_to_asu_data(asu_data);
151144
}
@@ -211,17 +204,21 @@ void add_asudata_and_recgrid(nb::module_& m,
211204
nb::arg("with_000")=false, nb::arg("with_sys_abs")=false,
212205
nb::arg("mott_bethe")=false)
213206
.def("__repr__", [=](const RecGr& self) {
214-
return tostr("<gemmi.", rgrid_name, '(', self.nu, ", ", self.nv, ", ", self.nw, ")>");
207+
return cat("<gemmi.", rgrid_name, '(', self.nu, ", ", self.nv, ", ", self.nw, ")>");
215208
});
216209
}
217210

211+
} // anonymous namespace
212+
218213
void add_recgrid(nb::module_& m) {
219214
using VS = ValueSigma<float>;
220215
nb::class_<VS>(m, "ValueSigma")
221216
.def_rw("value", &VS::value)
222217
.def_rw("sigma", &VS::sigma)
223218
.def("__repr__", [](const VS& self) {
224-
return tostr("<gemmi.ValueSigma(", self.value, ", ", self.sigma, ")>");
219+
char buf[64];
220+
snprintf_z(buf, 64, "<gemmi.ValueSigma(%g, %g)>", self.value, self.sigma);
221+
return std::string(buf);
225222
});
226223
nb::class_<ComplexCorrelation>(m, "ComplexCorrelation")
227224
.def_ro("n", &ComplexCorrelation::n)

python/tostr.hpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

python/unitcell.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2017 Global Phasing Ltd.
22

33
#include "gemmi/unitcell.hpp"
4-
#include "tostr.hpp" // tostr
54
#include "gemmi/cellred.hpp" // GruberVector
65
#include "gemmi/twin.hpp" // find_lattice_2fold_ops
76
#include "gemmi/sprintf.hpp" // snprintf_z
@@ -73,8 +72,10 @@ template<typename T> void add_smat33(nb::module_& m, const char* name) {
7372
.def("transformed_by", &M::template transformed_by<T>)
7473
.def("calculate_eigenvalues", &M::calculate_eigenvalues)
7574
.def("__repr__", [name](const M& m) {
76-
return tostr("<gemmi.", name, '(', m.u11, ", ", m.u22, ", ", m.u33, ", ",
77-
m.u12, ", ", m.u13, ", ", m.u23, + ")>");
75+
char buf[128];
76+
snprintf_z(buf, 128, "<gemmi.%s(%g, %g, %g, %g, %g, %g)>",
77+
name, m.u11, m.u22, m.u33, m.u12, m.u13, m.u23);
78+
return std::string(buf);
7879
});
7980
}
8081

0 commit comments

Comments
 (0)