0%

主要用途

1.进行私有状态管理
2.驱动view的显示
当@state 声明的值发生变化时,它所在的struct下的body会重新求值,body里使用的其他View会重新创建。从而使body包含的内容被重新渲染了一遍。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import SwiftUI

struct DetailView:View {
let number:Int
var body: some View{
Text("The Value: \(number)")
}
}

struct AddOneView: View {
@State private var value = 0
var body: some View {
VStack(alignment: .center) {
// Text("The Value: \(value)")
DetailView(number:value)
Button("+1"){
value += 1
}
}
}
}

struct AddOneView_Previews: PreviewProvider {
static var previews: some View {
AddOneView()
}
}

python获取昨天日期

1
2
3
4
5
>>> import datetime
>>> datetime.date.today() + datetime.timedelta(-1)
datetime.date(2021, 5, 10)
>>> (datetime.date.today() + datetime.timedelta(-1)).strftime("%Y%m%d")
'20210510'

经常会出现提交后又做了新的修改,又不想把新修改作为新commit的情况。
可以先直接修改文件。
修改后,

1
git reset --soft HEAD^

这时修改的文件都还在,也就是说对本地文件没做任何修改,把指针后移了一位。
然后将要git add的文件,如果本地已经删除的文件还要再git rm一次将暂存区的文件删除(此时本地已经删除过了,暂存区要像上次提交一样再删除一次)。
这时可以使用之前的commit文本再commit一次了。

阅读全文 »

序列化,反序列化分别使用json.dumps()、json.loads()。

列表:序列化后是str,反序列化是list
元组:序列化后是str,反序列化是list
字典:序列化后是str,反序列化是dict

阅读全文 »

在深度学习中有时需要使用自定义损失函数,或者为了更好的理解各个环节而使用自定义损失函数。
在pytorch中自定义损失函数实现的比较简单,就像实现一个小型网络一样。

代码

1
2
3
4
5
6
7
8
9
10
11
12
class LossFunc(nn.Module):
def __init__(self):
super(LossFunc, self).__init__()
def forward(self, prelist, ylist):
dif=torch.abs(prelist-ylist)
coef=ylist.clone()
coef[coef>=0.5]=2
coef[coef<0.5]=1
dif=torch.mul(dif,coef)
return torch.sum(dif)//ylist.size()[0]
mylossfunc=LossFunc()
mylossfunc=mylossfunc.cuda()
阅读全文 »

在jupyter中日常测试不同表达方式的执行效率,进而做取舍时,直接新开一行以

1
%%timeit

开头即可

示例

1
2
way1=LossFunc()
way1=way1.cuda()
1
2
%%timeit
tmp=way1(torch.rand(10000,1),torch.rand(10000,1))

方法

1.大小写字母

1
2
3
>>> import random, string
>>> "".join(random.sample(string.ascii_letters,9))
'HkJdpZclC'

阅读全文 »

重定向方式

1
2
3
4
5
6
# 1.
> test.txt
# 2.
: > test.txt
# 3.
true > test.txt

使用工具指令

1
2
3
4
5
6
7
8
9
10
# 4.
cat /dev/null > test.txt
# 5.
cp /dev/null test.txt
# 6.
dd if=/dev/null of=test.txt
# 7.
echo -n "" > test.txt # -n是为了是不显示空白行
# 8.
echo -n > test.txt

前言

喜欢在循环、函数里使用多个单行的if判断,如果true直接跳出。
类似常用的传值类型判断。
类似以下:

1
2
3
4
5
6
7
def func(a):
if a=="":print("empty value");return
if a%10==0:print("err vaule");return

for i in range(100):
if key1 not in dict1:print("err key");continue
if dict1[key1]=="":print("empty value");continue

这种句式的好处是逻辑非常清晰。

阅读全文 »