V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  wangyongbo  ›  全部回复第 12 页 / 共 24 页
回复总数  475
1 ... 8  9  10  11  12  13  14  15  16  17 ... 24  
2018-09-09 23:06:32 +08:00
回复了 pppguest3962 创建的主题 Python 请教根据嵌套字典,完成组装 SQL 语句的问题。。。
```

StreamData = {
"D1_RSET":{
'sqltype':'int',
'data':'61207'
},
"D2_DCK_time":{
'sqltype': 'DATETIME',
'data': '2018-9-9'
},
"D3_DCK_des":{
'sqltype': 'varchar(10)',
'data': 'TestABC'
},
"D4_Exp":{
'sqltype': 'varchar(80)',
'data': ''
},
"D5_stream_seq":{
'sqltype': 'int',
'data': '123'
},
}

insertoTableSqlStr_Prefix = 'INSERT INTO {tableName} ({colNames}) value ({colValues})'
colNames = []
colValues = []

def f(sqltype="int", data=None, **kwargs):
if not data:
return None

if sqltype == "int":
return str(data) if data else 'null'

return "\"{}\"".format(data)

for col_name, col_value in StreamData.items():
data = f(**col_value)
if data is None:
continue
colValues.append(data)
colNames.append(col_name)

print colValues, colNames

print insertoTableSqlStr_Prefix.format(tableName='test', colNames=",".join(colNames), colValues=",".join(colValues))


```
2018-09-09 23:05:48 +08:00
回复了 pppguest3962 创建的主题 Python 请教根据嵌套字典,完成组装 SQL 语句的问题。。。
insertoTableSqlStr_Prefix = 'INSERT INTO {tableName} ({colNames}) value ({colValues})'
colNames = []
colValues = []

def f(sqltype="int", data=None, **kwargs):
if not data:
return None

if sqltype == "int":
return str(data) if data else 'null'

return "\"{}\"".format(data)

for col_name, col_value in StreamData.items():
data = f(**col_value)
if data is None:
continue
colValues.append(data)
colNames.append(col_name)

print colValues, colNames

print insertoTableSqlStr_Prefix.format(tableName='test', colNames=",".join(colNames), colValues=",".join(colValues))

这样写符合需要吗?

D4_Exp 是不用操作,因为它是空的. 空字符串和 null 还是不一样。 不一定它是个空字符串,就不需要处理。
@gy911201 就是不想一台电脑一直开机。
2018-09-01 19:05:39 +08:00
回复了 sudoz 创建的主题 问与答 便宜点的人体工学椅有哪些值得推荐
2018-09-01 19:05:06 +08:00
回复了 sudoz 创建的主题 问与答 便宜点的人体工学椅有哪些值得推荐
宜家最好的,就 1000 块钱,。
2018-08-31 13:19:02 +08:00
回复了 Tuisku 创建的主题 问与答 关于和 V2er 在交易场景沟通上的问题和疑惑。
这人真不要脸呀。
耐信 奥美拉挫?
铝镁加混悬液?
2018-08-20 11:33:52 +08:00
回复了 youngxu 创建的主题 问与答 父亲反流性食管炎
根据我的经验,还是拉挫类药物有用。 康复心夜 一点儿用都没有
多少钱卖呀?
2018-07-21 23:52:17 +08:00
回复了 Cheez 创建的主题 问与答 Python 如何重载?
2018-07-21 23:49:33 +08:00
回复了 Cheez 创建的主题 问与答 Python 如何重载?
这两天升级 django , 从 1.8 升级到 支持 python2.7 的最后一个版本 1.11.
发现
"Using user.is_authenticated() and user.is_anonymous() as a method "
"is deprecated. Remove the parentheses to use it as an attribute.",
之前的使用方法:user.is_authenticated()
现在的使用方法:user.is_authenticated

我看了一下 django 的实现方法

```
class User():

@property
def is_authenticated(self):
return CallableFalse

```
首先用 property 把它变成了一个属性,但是返回的不是一个 bool, 是一个有__call__ 的对象

CallableFalse = CallableBool(False)


```
class CallableBool:
"""
An boolean-like object that is also callable for backwards compatibility.
"""
do_not_call_in_templates = True

def __init__(self, value):
self.value = value

def __bool__(self):
return self.value

def __call__(self):
warnings.warn(
"Using user.is_authenticated() and user.is_anonymous() as a method "
"is deprecated. Remove the parentheses to use it as an attribute.",
RemovedInDjango20Warning, stacklevel=2
)
return self.value

def __nonzero__(self): # Python 2 compatibility
return self.value

def __repr__(self):
return 'CallableBool(%r)' % self.value

def __eq__(self, other):
return self.value == other

def __ne__(self, other):
return self.value != other

def __or__(self, other):
return bool(self.value or other)

def __hash__(self):
return hash(self.value)

```


你觉得这种实现方式 怎么样?

出了 这种需要兼容的代码, 再也没有见过 类似的代码了。
2018-07-21 23:42:27 +08:00
回复了 txwd 创建的主题 Python 用 Python 写 Web api 的人有多少?
我司 所有的代码都是用的 django 写的。
2018-07-18 17:55:46 +08:00
回复了 wangyongbo 创建的主题 酷工作 [上海] 招中级前端工程师
@zetaoouyang 不行。
1 ... 8  9  10  11  12  13  14  15  16  17 ... 24  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5547 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 33ms · UTC 07:56 · PVG 15:56 · LAX 00:56 · JFK 03:56
Developed with CodeLauncher
♥ Do have faith in what you're doing.