object py_rotate( tuple args, dict kwargs) { Prim* This = extract<Prim*>( args[0]); if (!kwargs.has_key("angle")) { // This exception is more useful than the keyerror exception below. throw std::invalid_argument( "primitive.rotate(): angle of rotation must be specified."); } double angle = extract<double>(kwargs["angle"]); // The rotation axis, which defaults to the body axis. vector r_axis; if (kwargs.has_key("axis")) r_axis = tovector(kwargs["axis"]); else r_axis = This->get_axis(); // The rotation origin, which defaults to the body position. vector origin; if (kwargs.has_key("origin")) origin = tovector(kwargs["origin"]); else origin = This->get_pos(); This->rotate( angle, r_axis, origin); return object(); }
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); }
int ShzdApi::shzdSendInfoToTrade(dict data) { CShZdMessage msg = CShZdMessage(); //插入信息类型 if (data.has_key("msgtype")) { object msgtype = data["msgtype"]; extract<string> x(msgtype); if (x.check()) { string typestr = x(); msg.SetMsgType(typestr.c_str()); } } //插入字段 boost::python::list keyList = data.keys(); boost::python::list valueList = data.values(); for (int n = 0; n < len(keyList); n++) { //声明 int keyint = 0; string valuestr = ""; //获取整数型的key object key = keyList[n]; extract<string> x1(key); if (x1.check()) { string keystr = x1(); stringstream ss; ss << keystr; ss >> keyint; } //获取字符串的value object value = valueList[n]; extract<string> x2(value); if (x2.check()) { valuestr = x2(); } //添加到msg中 msg.SetTag(keyint, valuestr.c_str()); }
NearestNeighbourSearch(const object pycloud, const SearchType searchType = NNSNabo::KDTREE_LINEAR_HEAP, const Index dim = maxI, const dict params = dict()) { // build cloud eigenFromBoostPython(cloud, pycloud, "cloud"); // build params Nabo::Parameters _params; object it = params.iteritems(); for(int i = 0; i < len(params); ++i) { const tuple item(it.attr("next")()); const std::string key = extract<std::string>(item[0]); const object val(item[1]); const std::string valType(val.ptr()->ob_type->tp_name); if (valType == "int") { const int iVal = extract<int>(val); if (iVal >= 0) _params[key] = (unsigned)iVal; else _params[key] = iVal; } } // create search nns = NNSNabo ::create(cloud, dim, searchType, 0, _params); }
void HuffmanEncoder::encode(dict d) { ofstream out; out.open((path + ".ashf").c_str()); vector < Code > word_concat; for (int i = 0; i < d.size(); i++) { if (d[i].c.q == -1 || d[i].c.r == -1) continue; word_concat.push_back(d[i].c); } // coding of min(i1, i2) vector < string > r_s; int k = 0; for (int i = 0; i < word_concat.size(); i++) { int q_log = get_log(word_concat[i].q); string kemp = ""; for (int i = 0; i <= q_log; i++) { kemp += (1 << i) & word_concat[i].r ? "1" : "0"; } reverse(kemp.begin(), kemp.end()); r_s.push_back(kemp); int r = word_concat[i].q; word_concat[i].q -= k; k = r; } root = build_tree(word_concat); get_codes(root); print_tree(root); string long_string = ""; int q = 0; for (int i = 0; i < word_concat.size(); i++) { string tempor = get_code(word_concat[i].q) + r_s[i] + (word_concat[i].d == true ? "1" : "0"); long_string = long_string + tempor; } int l_str_size = long_string.size(); int cur_p = 0; while (cur_p < l_str_size) { unsigned char c = 0; for (int i = 0; i < min(8, l_str_size - cur_p); i++) { int t = long_string[i + cur_p] == '0' ? 0 : 1; c += (t << i); } cur_p += 8; out << c; } out.close(); }
inline std::map<T1, T2> dict_to_map(const dict& d) { std::map<T1, T2> result; stl_input_iterator<tuple> begin(d.iteritems()), end; std::transform(begin, end, map_inserter(result), boost::bind(tuple_to_pair<T1, T2>(), _1)); return result; }
void getDouble(dict d, string key, double *value) { if (d.has_key(key)) { object o = d[key]; extract<double> x(o); if (x.check()) { *value = x(); } } };
void getShort(dict d, string key, short *value) { if (d.has_key(key)) //检查字典中是否存在该键值 { object o = d[key]; //获取该键值 extract<int> x(o); //创建提取器 if (x.check()) //如果可以提取 { *value = x(); //对目标整数指针赋值 } } };
void getChar(dict d, string key, char *value) { if (d.has_key(key)) { object o = d[key]; extract<string> x(o); if (x.check()) { string s = x(); const char *buffer = s.c_str(); *value = *buffer; } } };
void getStr(dict d, string key, char *value) { if (d.has_key(key)) { object o = d[key]; extract<string> x(o); if (x.check()) { string s = x(); const char *buffer = s.c_str(); //对字符串指针赋值必须使用strcpy_s, vs2013使用strcpy编译通不过 //+1应该是因为C++字符串的结尾符号?不是特别确定,不加这个1会出错 strcpy_s(value, strlen(buffer) + 1, buffer); } } };
void getStr(dict d, string key, char *value) { if (d.has_key(key)) { object o = d[key]; extract<string> x(o); if (x.check()) { string s = x(); const char *buffer = s.c_str(); //对字符串指针赋值必须使用strcpy_s, vs2013使用strcpy编译通不过 //+1应该是因为C++字符串的结尾符号?不是特别确定,不加这个1会出错 #ifdef _MSC_VER //WIN32 strcpy_s(value, strlen(buffer) + 1, buffer); #elif __GNUC__ strncpy(value, buffer, strlen(buffer) + 1); #endif } } };
object dict_items(dict data) { return data.items(); }
object dict_values(dict data) { return data.values(); }
object dict_keys(dict data) { return data.keys(); }