mirror of
https://github.com/boostorg/python.git
synced 2026-02-21 15:22:12 +00:00
str, dict, and tuple!
[SVN r14517]
This commit is contained in:
@@ -61,9 +61,14 @@ bpl-test builtin_converters : test_builtin_converters.py test_builtin_converters
|
||||
bpl-test test_pointer_adoption ;
|
||||
bpl-test operators ;
|
||||
bpl-test callbacks ;
|
||||
|
||||
bpl-test object ;
|
||||
bpl-test list ;
|
||||
bpl-test long ;
|
||||
bpl-test dict ;
|
||||
bpl-test tuple ;
|
||||
bpl-test str ;
|
||||
|
||||
bpl-test virtual_functions ;
|
||||
bpl-test back_reference ;
|
||||
bpl-test implicit ;
|
||||
|
||||
82
test/dict.cpp
Normal file
82
test/dict.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/dict.hpp>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
object new_dict()
|
||||
{
|
||||
return dict();
|
||||
}
|
||||
|
||||
object data_dict()
|
||||
{
|
||||
dict tmp1;
|
||||
tmp1["key1"] = "value1";
|
||||
|
||||
dict tmp2;
|
||||
tmp2["key2"] = "value2";
|
||||
tmp1[1] = tmp2;
|
||||
return tmp1;
|
||||
}
|
||||
|
||||
object dict_from_sequence(object sequence)
|
||||
{
|
||||
return dict(sequence);
|
||||
}
|
||||
|
||||
object dict_keys(dict data)
|
||||
{
|
||||
return data.keys();
|
||||
}
|
||||
|
||||
object dict_values(dict data)
|
||||
{
|
||||
return data.values();
|
||||
}
|
||||
|
||||
object dict_items(dict data)
|
||||
{
|
||||
return data.items();
|
||||
}
|
||||
|
||||
void work_with_dict(dict data1, dict data2)
|
||||
{
|
||||
if (!data1.has_key("k1")) {
|
||||
throw std::runtime_error("dict does not have key 'k1'");
|
||||
}
|
||||
data1.update(data2);
|
||||
}
|
||||
|
||||
void test_templates(object print)
|
||||
{
|
||||
std::string key = "key";
|
||||
|
||||
dict tmp;
|
||||
tmp[1] = "a test string";
|
||||
print(tmp.get(1));
|
||||
//print(tmp[1]);
|
||||
tmp[1.5] = 13;
|
||||
print(tmp.get(1.5));
|
||||
print(tmp);
|
||||
print(tmp.get(2,"default"));
|
||||
print(tmp.has_key(key));
|
||||
print(tmp.setdefault(3,"default"));
|
||||
//print(tmp[3]);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(dict_ext)
|
||||
{
|
||||
module("dict_ext")
|
||||
.def("new_dict", new_dict)
|
||||
.def("data_dict", data_dict)
|
||||
.def("dict_keys", dict_keys)
|
||||
.def("dict_values", dict_values)
|
||||
.def("dict_items", dict_items)
|
||||
.def("dict_from_sequence", dict_from_sequence)
|
||||
.def("work_with_dict", work_with_dict)
|
||||
.def("test_templates", test_templates)
|
||||
;
|
||||
}
|
||||
40
test/dict.py
Normal file
40
test/dict.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""
|
||||
>>> from dict_ext import *
|
||||
>>> def printer(*args):
|
||||
... for x in args: print x,
|
||||
... print
|
||||
...
|
||||
>>> print new_dict()
|
||||
{}
|
||||
>>> print data_dict()
|
||||
{1: {'key2': 'value2'}, 'key1': 'value1'}
|
||||
>>> tmp = data_dict()
|
||||
>>> print dict_keys(tmp)
|
||||
[1, 'key1']
|
||||
>>> print dict_values(tmp)
|
||||
[{'key2': 'value2'}, 'value1']
|
||||
>>> print dict_items(tmp)
|
||||
[(1, {'key2': 'value2'}), ('key1', 'value1')]
|
||||
>>> print dict_from_sequence([(1,1),(2,2),(3,3)])
|
||||
{1: 1, 2: 2, 3: 3}
|
||||
>>> test_templates(printer)
|
||||
a test string
|
||||
13
|
||||
{1.5: 13, 1: 'a test string'}
|
||||
default
|
||||
0
|
||||
default
|
||||
"""
|
||||
|
||||
def run(args = None):
|
||||
import sys
|
||||
import doctest
|
||||
|
||||
if args is not None:
|
||||
sys.argv = args
|
||||
return doctest.testmod(sys.modules.get(__name__))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "running..."
|
||||
import sys
|
||||
sys.exit(run()[0])
|
||||
68
test/str.cpp
Normal file
68
test/str.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/str.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
object convert_to_string(object data)
|
||||
{
|
||||
return str(data);
|
||||
}
|
||||
|
||||
void work_with_string(object print)
|
||||
{
|
||||
str data("this is a demo string");
|
||||
print(data.split(" "));
|
||||
print(data.split(" ",3));
|
||||
print(str("<->").join(data.split(" ")));
|
||||
print(data.capitalize());
|
||||
print('[' + data.center(30) + ']');
|
||||
print(data.count("t"));
|
||||
print(data.encode("utf-8"));
|
||||
print(data.decode("utf-8"));
|
||||
print(data.endswith("xx"));
|
||||
print(data.startswith("test"));
|
||||
print(data.splitlines());
|
||||
print(data.strip());
|
||||
print(data.swapcase());
|
||||
print(data.title());
|
||||
|
||||
print("find");
|
||||
print(data.find("demo"));
|
||||
print(data.find("demo"),3,5);
|
||||
print(data.find(std::string("demo")));
|
||||
print(data.find(std::string("demo"),9));
|
||||
|
||||
print("expandtabs");
|
||||
str tabstr("\t\ttab\tdemo\t!");
|
||||
print(tabstr.expandtabs());
|
||||
print(tabstr.expandtabs(4));
|
||||
print(tabstr.expandtabs(7.9));
|
||||
|
||||
print("operators");
|
||||
print( str("part1") + str("part2") );
|
||||
// print( str("a test string").slice(3,_) );
|
||||
// print( str("another test")[5] );
|
||||
|
||||
print(data.replace("demo",std::string("blabla")));
|
||||
print(data.rfind("i",5));
|
||||
print(data.rindex("i",5));
|
||||
print(data.startswith("asdf"));
|
||||
print(data.endswith("asdf"));
|
||||
print(data.translate(str('a')*256));
|
||||
|
||||
|
||||
bool tmp = data.isalnum() || data.isalpha() || data.isdigit() || data.islower() ||
|
||||
data.isspace() || data.istitle() || data.isupper();
|
||||
(void)tmp; // ignored.
|
||||
}
|
||||
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(str_ext)
|
||||
{
|
||||
module("str_ext")
|
||||
.def("convert_to_string",convert_to_string)
|
||||
.def("work_with_string",work_with_string)
|
||||
;
|
||||
}
|
||||
|
||||
52
test/str.py
Normal file
52
test/str.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
>>> from str_ext import *
|
||||
>>> def printer(*args):
|
||||
... for x in args: print x,
|
||||
... print
|
||||
...
|
||||
>>> work_with_string(printer)
|
||||
['this', 'is', 'a', 'demo', 'string']
|
||||
['this', 'is', 'a', 'demo string']
|
||||
this<->is<->a<->demo<->string
|
||||
This is a demo string
|
||||
[ this is a demo string ]
|
||||
2
|
||||
this is a demo string
|
||||
this is a demo string
|
||||
0
|
||||
0
|
||||
['this is a demo string']
|
||||
this is a demo string
|
||||
THIS IS A DEMO STRING
|
||||
This Is A Demo String
|
||||
find
|
||||
10
|
||||
10 3 5
|
||||
10
|
||||
10
|
||||
expandtabs
|
||||
tab demo !
|
||||
tab demo !
|
||||
tab demo !
|
||||
operators
|
||||
part1part2
|
||||
this is a blabla string
|
||||
18
|
||||
18
|
||||
0
|
||||
0
|
||||
aaaaaaaaaaaaaaaaaaaaa
|
||||
"""
|
||||
|
||||
def run(args = None):
|
||||
import sys
|
||||
import doctest
|
||||
|
||||
if args is not None:
|
||||
sys.argv = args
|
||||
return doctest.testmod(sys.modules.get(__name__))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "running..."
|
||||
import sys
|
||||
sys.exit(run()[0])
|
||||
23
test/tuple.cpp
Normal file
23
test/tuple.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/tuple.hpp>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
object convert_to_tuple(object data)
|
||||
{
|
||||
return tuple(data);
|
||||
}
|
||||
|
||||
void test_operators(tuple t1, tuple t2, object print)
|
||||
{
|
||||
print(t1 + t2);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(tuple_ext)
|
||||
{
|
||||
module("tuple_ext")
|
||||
.def("convert_to_tuple",convert_to_tuple)
|
||||
.def("test_operators",test_operators)
|
||||
;
|
||||
}
|
||||
26
test/tuple.py
Normal file
26
test/tuple.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
>>> from tuple_ext import *
|
||||
>>> def printer(*args):
|
||||
... for x in args: print x,
|
||||
... print
|
||||
...
|
||||
>>> print convert_to_tuple("this is a test string")
|
||||
('t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', ' ', 's', 't', 'r', 'i', 'n', 'g')
|
||||
>>> t1 = convert_to_tuple("this is")
|
||||
>>> t2 = (1,2,3,4)
|
||||
>>> test_operators(t1,t2,printer)
|
||||
('t', 'h', 'i', 's', ' ', 'i', 's', 1, 2, 3, 4)
|
||||
"""
|
||||
|
||||
def run(args = None):
|
||||
import sys
|
||||
import doctest
|
||||
|
||||
if args is not None:
|
||||
sys.argv = args
|
||||
return doctest.testmod(sys.modules.get(__name__))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "running..."
|
||||
import sys
|
||||
sys.exit(run()[0])
|
||||
Reference in New Issue
Block a user