V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  siteshen  ›  全部回复第 9 页 / 共 22 页
回复总数  421
1 ... 5  6  7  8  9  10  11  12  13  14 ... 22  
2018-01-08 08:01:27 +08:00
回复了 Livid 创建的主题 DevOps 用 tmux 同时启动和监控后端开发环境中的所有程序
https://github.com/tmuxinator/tmuxinator 让这个更方便,可管理多个独立的 tmux session。

tmuxinator project1-dev
tmuxinator project2-stage
tmuxinator project3-prod
2018-01-07 18:19:23 +08:00
回复了 fengye1996 创建的主题 成都 成都配眼镜推荐
宝岛眼镜有点售后服务。几年前在龙泉的宝岛眼镜配过一次眼镜,一段时间后,他们打电话问过眼镜的使用情况。
2018-01-01 14:32:29 +08:00
回复了 yearliny 创建的主题 Python Django 的 models 如何实现 30 天后自动删除?
class Post(models.Model):
expires_at = DateField()


def delete_post(post):
post.update(expires_at=now() + timedelta(days=30))

def list_posts():
Post.query.filter(expires_at < now() + timedelta(days=30)).all()
@wb14123 对对对 @_@
我一般是简单的用 filter_by,filter_by 处理不了的用 filter。至于结果如何,可以配置日志看看生成的 SQL 语句。
http://docs.sqlalchemy.org/en/latest/core/engines.html#configuring-logging



另 @wb14123 我们似曾相识?
2017-12-28 13:42:23 +08:00
回复了 leisurelylicht 创建的主题 Python 求问关于 vim 编译支持 Python 的问题
别想同时支持 python2 和 python3 了,对代码和其他工具( flake8 等)要求太高。
也许可以根据「正在编辑」的文件动态设置要使用的 python 版本。

根据当前正在编辑的文件是 python2 还是 python3 (在 vim script 判断? project 判断?),然后使用对应的 python 版:

def is_python3(src):
return project_name in ['proj1', 'proj2'] or first_line.contains('python3')

if is_python3(src)
alias python = python3
else
alias python = python2
2017-12-19 22:43:17 +08:00
回复了 waterlaw 创建的主题 Python postgresql 9.3.5 数据库存储 字典型数据遇到的一个问题
看起来像是函数 `get_prep_value` 的问题,不知道你的 JSONField 怎么写出来的。
我的话,会在 Django 自带的 JSONField 上调整,楼主可以试试。

```
from django.contrib.postgres.fields.jsonb import JSONField as JSONBField

class JSONField(JSONBField):
def db_type(self, connection):
return 'json'
```
2017-12-16 03:00:41 +08:00
回复了 jaychenjun 创建的主题 JavaScript 一道初级的算法题
这段代码确实是用了些数学知识,再用了一点点 trick,因而比较难以理解。
我这给不了严格的数学证明,只能帮助你直观理解下。

S0 = a
S1 = ax + b = S0*x + b
S2 = ax^2 + bx + c = (ax + b)x + c = S1*x + c
S3 = ax^3 + bx^2 + cx + d = S2*x + d
...
Sn = S{n_1}*x + 常数项

特定到这道题,x = 2,a, b, c, .. 为 0 或者 1 (输入为二进制数组)
a * x + b = a * 2 + b = (a<<1) + b = a << 1 + b
最后一步用的小 trick:因为 a << 1 mod 2 = 0,b 为 0 或 1,所以 a<<1 | b = a * 2 + b
2017-12-14 21:13:01 +08:00
回复了 piaochen 创建的主题 Python 关于 Django Model 中,如何设置字段的默认值,并作用在表里?
2017-12-14 00:17:13 +08:00
回复了 yantianqi 创建的主题 程序员 path.join 和 path.resolve 有什么区别
以后提这种问题出门右拐找 https://www.v2ex.com/go/js,要不就在标题里加上 JavaScript 字样,免得浪费大家时间。
2017-12-07 13:04:10 +08:00
回复了 fe619742721 创建的主题 分享发现 来算算你朋友圈的咪蒙率是多少, ayawawa 率又是多少
@siteshen 群发助手看到了总数了。咪蒙率 16%
2017-12-07 13:01:31 +08:00
回复了 fe619742721 创建的主题 分享发现 来算算你朋友圈的咪蒙率是多少, ayawawa 率又是多少
45 个关注。大概 1/3,我是不是也该关注一波了。

搭车问下怎么查看好友总数的?
2017-12-07 09:32:44 +08:00
回复了 conn4575 创建的主题 Python Python 异常信息获取的最佳实践是什么
BaseException.args 是 python2.5 新增的属性,可以知道用户传递的是什么内容。

print(Exception(1, 2, 3).args)
# (1, 2, 3)

print(Exception('xyz').args)
# ('xyz',)

print(Exception().args)
# ()

初步了解了下 PHP 的 getMessage() 类似 python 中限定了参数为字符串的异常。

python 的异常里的内容可以是任何东西,写入的是啥,取出来的就是啥了。
异常 catcher 并不能做什么,只能规范异常 raiser 的行为。

如果 catcher 和 raiser 都是可控制的代码,可以自定义的异常,用这种方式规范代码。示例:

class SomethingError(Exception):
def __init__(self, code, message, blablabla):
self.code = code
self.message = message
self.blabla = blabla

然而即使如此,raiser 仍然可以不按“规范” raise Somthing('string', 666, []) 。
python 语言本身没有限制用户的行为,靠用户自觉。
2017-11-23 07:31:56 +08:00
回复了 k9982874 创建的主题 分享发现 golang 是目前为止用过的最难受的语言
说得很对,这些楼主想要的几乎都没有,(除了 go 明确是支持自动推导的静态类型)。

另外 template ( c++)和范型( java?-不很清楚这块),函数默认值( python )和函数重载( c++/java )都能解决同样的问题,有一个就可以了。(当然了 go 都没有……)
2017-11-21 22:05:45 +08:00
回复了 winglight2016 创建的主题 Python 请教一个 Python 列表解析的问题
楼上虽然说明了有些内容会转成 tuple,不过以前没见过这种写法,还是不清楚是怎么转成 tuple 的,就研究了下。

https://docs.python.org/3/reference/expressions.html#slicings

The semantics for a slicing are as follows. The primary is indexed (using the same __getitem__() method as normal subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose start, stop and step attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting None for missing expressions.

对于表达式 a[`.+`] 如果 正则 `.+` 至少包含一个逗号,则 key 为一个 tuple,否则 key 为 slice。
下面的示例能更清楚地说明上面的规则:

$ cat test.py && python3 test.py
class A():
def __getitem__(self, *args, **kwargs):
print('args:', len(args), args[0])

a = A()
a[1:]
a[:2]
a[1:2]
a[1,:]
a[:,2]
a[1,:,2]
# end of test.py

args: 1 slice(1, None, None)
args: 1 slice(None, 2, None)
args: 1 slice(1, 2, None)
args: 1 (1, slice(None, None, None))
args: 1 (slice(None, None, None), 2)
args: 1 (1, slice(None, None, None), 2)
2017-11-09 18:02:00 +08:00
回复了 RuiQ 创建的主题 Python 关于 SQLAlchemy 的一个问题
这个能解决你的问题,可以去研究下。
http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#enabling-two-phase-commit

For backends which support two-phase operation (currently MySQL and PostgreSQL), the session can be instructed to use two-phase commit semantics. This will coordinate the committing of transactions across databases so that the transaction is either committed or rolled back in all databases.
2017-11-09 00:07:53 +08:00
回复了 RuiQ 创建的主题 Python 关于 SQLAlchemy 的一个问题
@ryd994 我觉得题主的意思是,a_session 和 b_session 访问的是不同的数据库,没法在一个 transaction 处理。
登录和其他不一样,不需要死搬硬套,给几个我一直在使用的 API 设计示例:
{
"meta:" {"success": true},
"data": {},
}

登录 POST /auth/login {"username": "", "password": ""} -> {"token": ""}

读取我的信息 GET /me/profile -> {"me": {}}
读取用户信息 GET /users/1024/profile -> {"user": {}}

关注用户 POST /users/1024/follow
用户关注列表 GET /users/1024/following-user -> {"users": []}
2017-10-19 13:03:55 +08:00
回复了 northisland 创建的主题 Python 问 2 个关于 Python 的简单问题。困扰我很久
1. 不知道是否可以删。我的建议是不要删,这样就不用关心 with 的作用域的问题;
2. open(path).read() 肯定不会自动 close 文件,和 python 哲学 "Explicit is better than implicit" 冲突。

ps: 可以使用函数 `json.load` 少敲几个字符。
1 ... 5  6  7  8  9  10  11  12  13  14 ... 22  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1289 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 36ms · UTC 23:30 · PVG 07:30 · LAX 16:30 · JFK 19:30
Developed with CodeLauncher
♥ Do have faith in what you're doing.