赖勇浩(http://laiyonghao.com)
今天(2009年5月31日) OurPNP.org 搞了个聚会活动,弄了十几二十个人在广州海珠广场的堂会呆了五个小时,创下了我在 K 房呆的最长时间纪录。应他们的邀请,我做了个题为《用 python 快速搭建网游服务器》的小演讲,因为那边的电视竟然不能接电脑,所以讲的时候没有能够参照 PPT 来讲,观众看不到 PPT 可能效果一般般。现在通过博客分享一下我的这个 PPT,一如往日,已经转为图片了。敬请不要向我索要 PPT 版本或 PDF 版本,谢谢。
......
为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成datetime类型
1.字符串与time类型的转换
>>> import time
>>> timestr = "time2009-12-14"
>>> t = time.strptime(timestr, "time%Y-%m-%d")
>>> print t
(2009, 12, 14, 0, 0, 0, 0, 348, -1)
>>> type(t)
<type 'time.struct_time'>
>>>
如代码所示使用strptime进行转换,第一个参数是要转换的字符串,第二个参数是字符串中时间的格式
与之对应的有函数strftime,是将time类型转换相应的字符串
下面是格式化符号汇总
%a 星期几的简写 Weekday name, abbr.
%A 星期几的全称 Weekday name, full
%b 月分的简写 Month name, abbr.
%B 月份的全称 Month name, full
%c 标准的日期的时间串 Complete date and time representation
%d 十进制表示的每月的第几天 Day of the month
%H 24小时制的小时 Hour (24-hour clock)
%I 12小时制的小时 Hour (12-hour ......
Python 3 输出重定向使用C/C++
By:gddsky
目标
希望将Python 3的输出重定向到自定义的输出目标。
核心
Python使用sys.stdout、sys.stderr做输出目标,只要我们替换这两个值就可以重定向到我们自定义目标。替换的值的规则在Python的文档中sys (module)的sys.stdout上面说明只要添加一个write函数就可以了。像这样:
write(self, str)
我们只要在这个函数里面调用我们自己的输出函数就可以实现该目标了。
方案
在C++中写一个Python模块,并导入
在C++中利用PyRun_SimpleString建立一个PyObject去替换原始的sys.stdout、sys.stderr
搞定!!!
代码
模块定义部分代码(用python demo下的例子改写的)
static PyObject *console_write(PyObject *self, PyObject* args)
{
if (s_pyConsoleNotify != NULL)
{
const char* pMsgString=0;
if (!PyArg_ParseTuple( args, "s", &pMsgString ))
{
&n ......
Python 3 输出重定向使用C/C++
By:gddsky
目标
希望将Python 3的输出重定向到自定义的输出目标。
核心
Python使用sys.stdout、sys.stderr做输出目标,只要我们替换这两个值就可以重定向到我们自定义目标。替换的值的规则在Python的文档中sys (module)的sys.stdout上面说明只要添加一个write函数就可以了。像这样:
write(self, str)
我们只要在这个函数里面调用我们自己的输出函数就可以实现该目标了。
方案
在C++中写一个Python模块,并导入
在C++中利用PyRun_SimpleString建立一个PyObject去替换原始的sys.stdout、sys.stderr
搞定!!!
代码
模块定义部分代码(用python demo下的例子改写的)
static PyObject *console_write(PyObject *self, PyObject* args)
{
if (s_pyConsoleNotify != NULL)
{
const char* pMsgString=0;
if (!PyArg_ParseTuple( args, "s", &pMsgString ))
{
&n ......
Python 3 输出重定向使用C/C++
By:gddsky
目标
希望将Python 3的输出重定向到自定义的输出目标。
核心
Python使用sys.stdout、sys.stderr做输出目标,只要我们替换这两个值就可以重定向到我们自定义目标。替换的值的规则在Python的文档中sys (module)的sys.stdout上面说明只要添加一个write函数就可以了。像这样:
write(self, str)
我们只要在这个函数里面调用我们自己的输出函数就可以实现该目标了。
方案
在C++中写一个Python模块,并导入
在C++中利用PyRun_SimpleString建立一个PyObject去替换原始的sys.stdout、sys.stderr
搞定!!!
代码
模块定义部分代码(用python demo下的例子改写的)
static PyObject *console_write(PyObject *self, PyObject* args)
{
if (s_pyConsoleNotify != NULL)
{
const char* pMsgString=0;
if (!PyArg_ParseTuple( args, "s", &pMsgString ))
{
&n ......
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 680460288 22 0 262145 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;
mso-font-charset:0;
mso-generic-font-family:roman;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1107304683 0 0 159 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 680460288 22 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-styl ......
订单处置,首先要有一个订单的详细列表(包括数量,价钱啥的)和一个对于个人的一些信息的一个表。因而我们创造两个模型,line_item(列表项),order(列表),其后编者如次
/db/migrate xxx_create_order xxx_line_item
Ruby代码
一.class CreateOrders < ActiveRecord::Migration
二.def self.up
3. create_table :orders do |t|
4. t.string :name
5. t.text :address
6. t.string :email
7. t.string :pay_type, :limit => 十
8. t.timestamps
9. end
十.end
11.
12.def self.down
13. drop_table :orders
14.end
15.d
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.string :name
t.text :address
t.string :email
t.string :pay_type, :limit => 十
t.timestamps
end
end
def self.down
drop_table :orders
end
end
Ruby代码
1. class CreateLineItems < ActiveRecord::Migration
二.def self.up
3. create_table :line_items do |t|
4. t.integer :product_id, :null => false,:options =>
5. "CONSTRAINT fk_line_items_products REFERENCE ......
订单处置,首先要有一个订单的详细列表(包括数量,价钱啥的)和一个对于个人的一些信息的一个表。因而我们创造两个模型,line_item(列表项),order(列表),其后编者如次
/db/migrate xxx_create_order xxx_line_item
Ruby代码
一.class CreateOrders < ActiveRecord::Migration
二.def self.up
3. create_table :orders do |t|
4. t.string :name
5. t.text :address
6. t.string :email
7. t.string :pay_type, :limit => 十
8. t.timestamps
9. end
十.end
11.
12.def self.down
13. drop_table :orders
14.end
15.d
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.string :name
t.text :address
t.string :email
t.string :pay_type, :limit => 十
t.timestamps
end
end
def self.down
drop_table :orders
end
end
Ruby代码
1. class CreateLineItems < ActiveRecord::Migration
二.def self.up
3. create_table :line_items do |t|
4. t.integer :product_id, :null => false,:options =>
5. "CONSTRAINT fk_line_items_products REFERENCE ......