Skip to content

Commit 07a674d

Browse files
authored
Automatic to_<tag> Python method. (#9)
1 parent 5ed17b6 commit 07a674d

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

docs/changelog.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Change Log
22
==========
33

4-
v0.3 (not yet released)
5-
-----------------------
4+
v0.3 (July 22, 2018)
5+
--------------------
66

77
* Throw and catch errors in `read`.
88

@@ -18,6 +18,9 @@ v0.3 (not yet released)
1818
* General commands: ``store --pop`` to remove current store element
1919
`#8 <https://github.com/msoeken/alice/pull/8>`_
2020

21+
* Automatic ``to_<tag>`` in Python interface as shortcut for ``write_<tag>(log=True)["contents"]``
22+
`#9 <https://github.com/msoeken/alice/pull/9>`_
23+
2124
v0.2 (May 7, 2018)
2225
------------------
2326

include/alice/api.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,37 @@ struct insert_write_commands<CLI, Tuple, 0>
164164
}
165165
};
166166

167+
#if defined ALICE_PYTHON
168+
template<typename CLI, typename Tuple, std::size_t Index>
169+
struct make_special_write_commands
170+
{
171+
make_special_write_commands( CLI& cli, py::module& m )
172+
{
173+
make_special_write_commands<CLI, Tuple, Index - 1> irc( cli, m );
174+
175+
using tag_type = std::tuple_element_t<Index - 1, Tuple>;
176+
//cli.template insert_write_command<tag_type>( fmt::format( "write_{}", alice_globals::get().write_tags[Index - 1] ), alice_globals::get().write_names[Index - 1] );
177+
178+
auto const& tag = alice_globals::get().write_tags[Index - 1];
179+
const auto name = fmt::format( "write_{}", tag );
180+
auto const& cmd = cli.env->commands().at( name );
181+
m.def( fmt::format( "to_{}", tag ).c_str(), [cmd, name]( py::kwargs kwargs ) -> py::str {
182+
auto pargs = detail::make_args( name, kwargs );
183+
pargs.push_back( "--log" );
184+
cmd->run( pargs );
185+
const auto log = cmd->log();
186+
return py::str( log["contents"].template get<std::string>() );
187+
} );
188+
}
189+
};
190+
191+
template<typename CLI, typename Tuple>
192+
struct make_special_write_commands<CLI, Tuple, 0>
193+
{
194+
make_special_write_commands( CLI& cli, py::module& m ) {}
195+
};
196+
#endif
197+
167198
/*! \brief Returns a one-line string to show when printing store contents
168199
169200
This macro is used to return a string that is shown in the output of
@@ -501,6 +532,7 @@ PYBIND11_MODULE(prefix, m) \
501532
{ \
502533
_ALICE_MAIN_BODY(prefix) \
503534
alice::detail::create_python_module( cli, m ); \
535+
make_special_write_commands<cli_t, alice_write_tags, std::tuple_size<alice_write_tags>::value> swc( cli, m ); \
504536
}
505537
#elif defined ALICE_CINTERFACE
506538
#define ALICE_MAIN(prefix) \

include/alice/detail/python.hpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,39 @@ class return_value_dict
172172
std::string repr_html;
173173
};
174174

175+
inline std::vector<std::string> make_args( const std::string& name, py::kwargs kwargs )
176+
{
177+
std::vector<std::string> pargs = {name};
178+
179+
for ( const auto& kp : kwargs )
180+
{
181+
// get the key as string
182+
const auto skey = kp.first.cast<std::string>();
183+
const auto value = kp.second;
184+
185+
// TODO cast float to string?
186+
if ( py::isinstance<py::bool_>( value ) )
187+
{
188+
if ( value.cast<bool>() )
189+
{
190+
pargs.push_back( "--" + skey );
191+
}
192+
}
193+
else if ( py::isinstance<py::int_>( value ) )
194+
{
195+
pargs.push_back( "--" + skey );
196+
pargs.push_back( std::to_string( value.cast<int>() ) );
197+
}
198+
else
199+
{
200+
pargs.push_back( "--" + skey );
201+
pargs.push_back( value.cast<std::string>() );
202+
}
203+
}
204+
205+
return pargs;
206+
}
207+
175208
template<typename CLI>
176209
void create_python_module( CLI& cli, py::module& m )
177210
{
@@ -187,34 +220,7 @@ void create_python_module( CLI& cli, py::module& m )
187220
for ( const auto& p : cli.env->commands() )
188221
{
189222
m.def( p.first.c_str(), [p]( py::kwargs kwargs ) -> py::object {
190-
std::vector<std::string> pargs = {p.first};
191-
192-
for ( const auto& kp : kwargs )
193-
{
194-
// get the key as string
195-
const auto skey = kp.first.cast<std::string>();
196-
const auto value = kp.second;
197-
198-
// TODO cast float to string?
199-
if ( py::isinstance<py::bool_>( value ) )
200-
{
201-
if ( value.cast<bool>() )
202-
{
203-
pargs.push_back( "--" + skey );
204-
}
205-
}
206-
else if ( py::isinstance<py::int_>( value ) )
207-
{
208-
pargs.push_back( "--" + skey );
209-
pargs.push_back( std::to_string( value.cast<int>() ) );
210-
}
211-
else
212-
{
213-
pargs.push_back( "--" + skey );
214-
pargs.push_back( value.cast<std::string>() );
215-
}
216-
}
217-
p.second->run( pargs );
223+
p.second->run( make_args( p.first, kwargs ) );
218224

219225
const auto log = p.second->log();
220226

0 commit comments

Comments
 (0)