首页 \ 问答 \ 使用Boost.Python将boost :: gregorian :: date暴露给Python(Exposing boost::gregorian::date to Python using Boost.Python)

使用Boost.Python将boost :: gregorian :: date暴露给Python(Exposing boost::gregorian::date to Python using Boost.Python)

我想使用Boost.Python为Python提供boost :: gregorian :: date。 但是当Boost日期类中没有一个函数时,如何创建一个像样的__str__函数呢? 我想这样写:

BOOST_PYTHON_MODULE(mymodule)
{
    class_<boost::gregorian::date>("Date")
        .add_property("year", &boost::gregorian::date::year)
        .add_property("month", &boost::gregorian::date::month)
        .def("__str__", ???)
    ;
}

I'd like to make boost::gregorian::date available to Python using Boost.Python. But how do I create a decent __str__ function when one is not available on the Boost date class? I'd like to write it like this:

BOOST_PYTHON_MODULE(mymodule)
{
    class_<boost::gregorian::date>("Date")
        .add_property("year", &boost::gregorian::date::year)
        .add_property("month", &boost::gregorian::date::month)
        .def("__str__", ???)
    ;
}

原文:https://stackoverflow.com/questions/17068111
更新时间:2021-09-14 13:09

最满意答案

怎么样的:

template <typename T> T foo(unsigned short length)
{
    return (T(1) << (length * 4)) - 1;
}

How about something like:

template <typename T> T foo(unsigned short length)
{
    return (T(1) << (length * 4)) - 1;
}

相关问答

更多
  • 我编译你的第一个例子,在g++进行优化,并生成与你的第三个解决方案相同的代码。 事实上,用一个小小的bitset(320位)完全展开它。 没有调用函数来确保a和b的内容在main是未知的,它实际上已经优化了整个东西(知道两者都是0)。 课程:编写明显的可读代码并让编译器处理它。 I compiled your first example with optimization in g++ and it produced code identical to your third solution. In fac ...
  • 对于&运算符(以及大多数其他运算符),在计算运算之前,任何小于int操作数都将被提升为int 。 从C99标准(6.5.10 - 描述按位AND运算符): 通常的算术转换是在操作数上执行的。 (6.3.1.8 - 描述通常的算术转换): 在两个操作数上执行整数提升 (6.3.1.1 - 描述整数提升): 如果int可以表示原始类型的所有值,则该值将转换为int ; 否则,它将转换为unsigned int 。 这些被称为整数促销。 For the & operator (and most other ope ...
  • 您需要确定您感兴趣的不同部分的位范围。查看列表,主要类型有2个十六进制数字,子类型有4个数字。 使用AND( & )将它们分开。 major = type & 0xff0000; subtype = type & 0x00ffff; You need to determine the bit ranges of the different pieces you're interested in. Looking at the list, there are 2 hex digits devoted to ...
  • 是的,gcc会优化它,因为它是一个完全不变的表达式。 要检查这个汇编代码,例如使用这个工具https://gcc.godbolt.org/ #include #define SOME_CONSTANT 0x111UL #define SOME_OFFSET 2 #define SOME_MASK 7 #define SOME_VALUE ((SOME_CONSTANT) << (SOME_OFFSET)) & (SOME_MASK) void foo() { pri ...
  • 在C中,算法至少在int的大小下执行。 这意味着, ~0xFC将返回一个int 。 甚至更多,这个值是0xFF03 ,超出了char的范围(有符号或不是)。 因此,作业会给出两个警告。 你可以试试 signed char sc = ~0xFC & 0xFF; 看看编译器是否知道结果是在char范围内(gcc在添加& 0xFF后不会抱怨)。 你也可以尝试添加一个明确的演员: signed char sc = (signed char)~0xFC; // also possible: // signed ch ...
  • 怎么样的: template T foo(unsigned short length) { return (T(1) << (length * 4)) - 1; } How about something like: template T foo(unsigned short length) { return (T(1) << (length * 4)) - 1; }
  • 与要翻转的位进行XOR。 int c = 0x10; // 10000b int m = 0x08; // 01000b c ^= m; // 11000b XOR with the bits that you want to flip. int c = 0x10; // 10000b int m = 0x08; // 01000b c ^= m; // 11000b
  • 最好的方法是将表达式转换为布尔值。 这提高了可读性: bool result; result = 0 != (a & 8); 与0的比较可能是不必要的,因为编译的代码将在变量b写入true或false 。 但这避免了一个警告: warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) 如果您不喜欢Yoda演讲,则可能会颠倒顺序或比较参数。 这种风格可以帮助避免C编程语言中的意外分配。 The ...
  • 重载结构中的成员: struct X { X& operator ^= (const X& other) { //... return *this; } }; Overload the member in the struct: struct X { X& operator ^= (const X& other) { //... return *this; } };
  • 来自维基百科: 按位AND运算符是单个&符号:&。 它只是AND的一种表示,它对操作数的位而不是操作数的真值进行处理。 按位二进制AND以二进制形式对数字的每个位置中的位进行逻辑AND(如上表所示)。 在您的示例110010和1 , 1被认为是000001 ,然后每个位都是有效的,您将得到结果。 事实上,我使用这种方法: 1&number来检查偶数和奇数。 这是如何: if(1 & num) printf("it is odd"); else printf("it is even"); 这是它的 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)