V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  siteshen  ›  全部回复第 9 页 / 共 23 页
回复总数  453
1 ... 5  6  7  8  9  10  11  12  13  14 ... 23  
2018 年 10 月 14 日
回复了 DongDongXie 创建的主题 Linux 新手问个很简单的 Linux 数组读取问题
# 2.sh 错误
# 1.sh 正确
# https://unix.stackexchange.com/questions/155551/how-to-debug-a-bash-script

# 错误
$ cat 2.sh aa=(1 3 5 7)
for i in ${aa[@]}
do
echo ${i}+1
done

$ sh -x 2.sh + aa=(1 3 5 7)
+ for i in '${aa[@]}'
+ echo 1+1
1+1
+ for i in '${aa[@]}'
+ echo 3+1
3+1
+ for i in '${aa[@]}'
+ echo 5+1
5+1
+ for i in '${aa[@]}'
+ echo 7+1
7+1



# 正确
$ cat 1.sh aa=(1 3 5 7);
for i in ${aa[@]}; do
echo $(expr ${i} + 1)
done

$ sh -x 1.sh + aa=(1 3 5 7)
+ for i in '${aa[@]}'
++ expr 1 + 1
+ echo 2
2
+ for i in '${aa[@]}'
++ expr 3 + 1
+ echo 4
4
+ for i in '${aa[@]}'
++ expr 5 + 1
+ echo 6
6
+ for i in '${aa[@]}'
++ expr 7 + 1
+ echo 8
8
2018 年 9 月 10 日
回复了 ruri 创建的主题 Google Google 这次真的隐藏 www 了,元芳你怎么看?
以前我们的网站域名也是不用 www 的,直到看到了这个网站: https://www.yes-www.org/why-use-www/
2018 年 8 月 28 日
回复了 frmongo 创建的主题 Python 小问题
# 实现了一个简单版的,不过有个假定:heads 和 tails 是各自唯一的。
# 如果不是唯一,可以对各个元素增加一个 count 字段,然后比较。

def merge(list_of_list):
heads = {a for (a, b) in list_of_list}
tails = {b for (a, b) in list_of_list}
return heads - tails

print(merge([[1,2],[2,3],[3,4],[4,7]])) # {1}
print(merge([[1,2],[2,3],[3,4],[4,7],[7,1]])) # set()
2018 年 8 月 20 日
回复了 leeeeee 创建的主题 Python 请问一下现在新开 Python 项目都用第三版了吧?
我厂之前简单调研过,python3 绝大部分场景够用,三年前的新项目就在用了。
对题主的遭遇深表同情,遇到个不合格的后端。更要感慨一下 V2EX 的氛围,发帖吐槽不合格的队友,居然会被喷出翔。

数组返回 [] 或者 null 都有一定道理,返回 "" 或 "[]" 就真的匪夷所思了。按这逻辑,数组类型也可能返回 1024, true, false (和 "" 一样同为基本类型),那使用 API 的人岂不是要防天防地防空气了?
2018 年 8 月 11 日
回复了 luzhongqiu 创建的主题 程序员 有多少人愿意为了知识付费的?
@leekafai 然而吃了后不能上 WC,否则记忆面包上的内容会流失掉。
2018 年 7 月 25 日
回复了 Lpl 创建的主题 程序员 GitHub 新玩法,强
那么问题来了,楼主到底 star/fork 了哪些 repo ?
2018 年 7 月 23 日
回复了 tt67wq 创建的主题 程序员 怎么应对喜欢摸来摸去的领导
> 你咋这么清楚呢,莫非。。。

@zqiyun 盆友,西游记了解一下。
2018 年 7 月 23 日
回复了 1ku 创建的主题 程序员 “收入投票”的结果出来啦!
@Ginray 要御姐也可以,不过更容易漏气。
2018 年 7 月 21 日
回复了 lz12366007 创建的主题 酷工作 [字节跳动 /内推]] 北京,成都,武汉,上海,深圳
接收简历的是私人邮箱,不明觉厉。
2018 年 7 月 19 日
回复了 az09py 创建的主题 程序员 怎么获取上次 push 操作时的 commitid
@az09py `$(BRANCH}` 替换为 `$(git rev-parse --abbrev-ref HEAD)` 就行。

另外 @gnaggnoyil 说的 X-Y 问题应该是这个: https://coolshell.cn/articles/10804.html
意思是也许直接问你要解决的(完整的)问题,比问一个你提出的解决方案的一部分更好。
2018 年 7 月 18 日
回复了 az09py 创建的主题 程序员 怎么获取上次 push 操作时的 commitid
还是不太明白"最近一次的 pull/push 的 commit",不过下面的命令能得到远端分支的最后一个 commit

git fetch; git rev-parse origin/${BRANCH}
2018 年 7 月 14 日
回复了 LeungJZ 创建的主题 程序员 向大佬们请教一个关于排行榜的实现问题。
自然月很简单,使用 redis 的 sorted set 实现很方便。

点赞( for month ):zincrby popular:2018-07 1 {post_id}
热帖( for month ):zrevrangebyscore popular:2018-07 +inf -inf limit 0 20

自然周同理(假设今天属于本年的第 38 周,懒得计算了):
点赞( for month ):zincrby popular:2018-w38 1 {post_id}
热帖( for month ):zrevrangebyscore popular:2018-w38 +inf -inf limit 0 20

rolling 周的话复杂些,可以使用写入多次的方式实现。
2018 年 6 月 12 日
回复了 zjsxwc 创建的主题 程序员 你们 Python 是怎么安装依赖的?
# 以前
mkvirtualenv awesome-project
pip install requests
echo "requests" >> requirements.txt
pip freeze -l > requirements.lock
git add requirements.txt requirements.lock

# 现在
touch Pipfile
pipenv install requests
git add Pipfile Pipfile.lock
@6167 放里面是为了最小权限原则,不行的话放外面。下面这个是 drf_yasg.utils 里的代码,用的 is 比较,比我之前写的完善些。

class unset(object):
"""Used as a sentinel value for function parameters not set by the caller where ``None`` would be a valid value."""
pass


def swagger_auto_schema(method=None, methods=None, auto_schema=unset):
if auto_schema is not unset:
data['auto_schema'] = auto_schema
看了下 Django 的代码,直接设置 default 做不到,因为 default 作为 callable 调用时,不会传入任何参数。
你的答案方案在于,没有区分 self.last 的值,是“初始值”还是“被设置的值”。
和 @twor 答案类似,不过用了一个无意义的变量作为 sentinel value。

class Pool():
UNSET = object()

last = models.IntegerFields(default=UNSET)

def save(self):
if self.last == self.UNSET:
self.last = self.amount

super(Pool, self).save()
1. 写出能按距离排序的 SQL 语句;
2. 翻译成对应的 python 代码。

不考虑性能的话:
1. sql_let ordering = ((long - ${x})^2 + (lat - ${y})^2)
2. select * from branch order by ordering where ordering < {distance} desc
3. Branch.objects.filter(RAW_SQL('((long - %s)^2 + (lat - %s)^2) < %s') % (x, y, distance))

另外不知道你生成的 SQL 是怎么样的?看样子是把 `Distance()` 直接当成字符串直接传入到 SQL 语句了。这里需要知道 Distance('location__coordinates', user_coordinates) 对应的 SQL 语句类型(比如是 float ?),然后传入值时也变成对应类型,大约类似这样: `objects.filter(Distance('location__coordinates', user_coordinates) < distance.metres)`
2018 年 5 月 11 日
回复了 SingeeKing 创建的主题 Python 一段有趣的代码
无数人讲过 python 默认参数的“坑”了,我提供另外一个思路,函数尽量不要有副作用。这样无论 python 内部怎么实现,都不会出现问题(当然如果是性能不足,肯定需要研究语言的实现机制)。
1 ... 5  6  7  8  9  10  11  12  13  14 ... 23  
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2604 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 36ms · UTC 11:06 · PVG 19:06 · LAX 03:06 · JFK 06:06
♥ Do have faith in what you're doing.