我想在shell脚本中激活虚拟环境,所以我写了一个简单的脚本如下:
#!/bin/bash
source ~/env/lib/bin/activate
#nohup python mock_run.py
#echo $! > save_pid.txt
Run Code Online (Sandbox Code Playgroud)
我用脚本启动脚本sh start.sh,但是我得到如下错误:
start.sh: 3: start.sh: source: not found
Run Code Online (Sandbox Code Playgroud)
我运行source ~/env/lib/bin/activate没问题,为什么不能在shell脚本中?
我想得到今天的开始时间,我这样做的方式如下:
from datetime import datetime
datetime.strptime(datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d')
Run Code Online (Sandbox Code Playgroud)
我有更好的方法吗?
我想upsert(update or insert) a list of record,其实我知道mongodbbulk从mongodb3.0开始就支持操作了。
我想知道是否mongoengine支持bulk upsert operation在mongoengine (0.10.0)。
如果没有,我想知道如何upsert记录列表,我知道mongoengine支持这样的insert批处理:
class User(Document):
username = StringField(required=True)
password = StringFiedl(required=True)
meta = {'db_alias': 'user_info', 'collection': 'user',
'indexes': [{'fields': ['username'], 'unique': True}]
}
def save_users(self, users):
Users.objects.insert(users) # raise mongoengine.errors.NotUniqueError
Run Code Online (Sandbox Code Playgroud) 我在 Mac 和 Ubuntu 中工作,所以我想让我的脚本在 Mac 和 Ubuntu 中工作,但sed -i在这两个系统中的工作方式不同:
在 Ubuntu 中,它应该是:
sed -i'' 's/Alt/Dog/g' /tmp/example.txt
Run Code Online (Sandbox Code Playgroud)
在 Mac 中,它应该是:
sed -i '' 's/Alt/Dog/g' /tmp/example.txt
Run Code Online (Sandbox Code Playgroud) 我想在命令行中打印所有匹配项,如下所示:
perl -0777 -nle 'print "$1\n" if /ModelProxy\("([\w*.]+)"\);/g' test.txt
Run Code Online (Sandbox Code Playgroud)
像test.txt这样:
var orderform = new ModelProxy("orderform.b2b.*");
var orderform2b = new ModelProxy("web.orderform_2b.*");
var o2oOrderform = new ModelProxy("orderform.o2o.*");
var logistics = new ModelProxy("supply.logistics.*");
var tyreurgent = new ModelProxy("o2o.tyreurgent.*");
var common = new ModelProxy("web.common.file.*");
var cartPipeData = load("pipe/cartPipeData");
var tsCartPipeData = load("pipe/tsCartPipeData").default;
Run Code Online (Sandbox Code Playgroud)
我想要的是:
orderform.b2b.*
web.orderform_2b.*
orderform.o2o.*
supply.logistics.*
o2o.tyreurgent.*
web.common.file.*
Run Code Online (Sandbox Code Playgroud)
但我得到了第一个:
orderform.b2b.*
Run Code Online (Sandbox Code Playgroud) 我想使用正则表达式组替换字符串golang,如下所示python:
re.sub(r"(\d.*?)[a-z]+(\d.*?)", r"\1 \2", "123abc123") # python code
Run Code Online (Sandbox Code Playgroud)
那么如何在golang中实现呢?
我dict喜欢这样的:
{
('America', 25, 'm', 'IT'): 10000,
('America', 22, 'm', 'IT'): 8999,
('Japan', 24, 'f', 'IT'): 9999,
('Japan', 23, 'f', 'IT'): 9000
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望('America', 'm', 'IT')在此示例中使用key获取所有结果.在上面,那将是:
{25: 10000, 22: 8999}
Run Code Online (Sandbox Code Playgroud)
我目前的解决方案如下:
res = dict()
for key, cnt in stats.items():
country, age, sex, job = key
try:
res[(country, sex, job)][age] = cnt
except KeyError as e:
res[(country, sex, job)] = {}
res[(country, sex, job)][age] = cnt
print res['America', 'm', 'IT']
Run Code Online (Sandbox Code Playgroud)
我有更好的方法吗?因为这段代码看起来并不那么简单.
我想is在裸体块中定义一个类并进行测试,如下所示:
use Test::More tests => 1;
{
package Foofle;
use parent qw(Animal);
sub sound { 'foof' }
is(
Foofle->speak,
"A Foofle goes foof!\n",
"An Animal subclass does the right thing"
);
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误Undefined subroutine &Foofle::is called at t/Animal.t,所以如果我补充一下
use Test::More;
Run Code Online (Sandbox Code Playgroud)
在裸体块中,它运行正常.
或者我走出is裸露的街区,
use Test::More tests => 1;
{
package Foofle;
use parent qw(Animal);
sub sound { 'foof' }
}
is(
Foofle->speak,
"A Foofle goes foof!\n",
"An Animal subclass does the right thing"
);
Run Code Online (Sandbox Code Playgroud)
它运行正常.但 …
我想在perl中运行shell命令如下:
tar --exclude="*/node_modules" \
--exclude="*/vendor" \
--exclude='.git' \
-zvcf /tmp/robot.tgz .
Run Code Online (Sandbox Code Playgroud)
但似乎perl无法执行此操作:
`tar --exclude="cv/node_modules" \
--exclude="*/vendor" \
--exclude='.git' \
-zvcf /tmp/robot.tgz .`;
Run Code Online (Sandbox Code Playgroud)
这是错误:
tar: Must specify one of -c, -r, -t, -u, -x
sh: line 1: --exclude=*/vendor: No such file or directory
sh: line 2: --exclude=.git: command not found
sh: line 3: -zvcf: command not found
Run Code Online (Sandbox Code Playgroud)
似乎perl将每一行视为一个命令.
我想做的是这样的:
class MyThread(threading.Thread):
def __init__(self, host, port):
threading.Thread.__init__(self)
# self._sock = self.initsocket(host, port)
self._id = random.randint(0, 100)
def run(self):
for i in range(3):
print("current id: {}".format(self._id))
def main():
ts = []
for i in range(5):
t = MyThread("localhost", 3001)
t.start()
ts.append(t)
for t in ts:
t.join()
Run Code Online (Sandbox Code Playgroud)
我得到了这些输出:
current id: 10
current id: 10
current id: 13
current id: 43
current id: 13
current id: 10
current id: 83
current id: 83
current id: 83
current id: 13
current id: 98 …Run Code Online (Sandbox Code Playgroud) 我想在 golang 中调用一个接受接口切片作为参数的方法,但我发现我无法将其传递为这样的写法:
type Base interface {
Run()
}
type A struct {
name string
}
func (a *A) Run() {
fmt.Printf("%s is running\n", a.name)
}
func foo1(b Base) {
b.Run()
}
func foo2(s []Base) {
for _, b := range s {
b.Run()
}
}
func TestInterface(t *testing.T) {
dog := &A{name: "a dog"}
foo1(dog)
// cat := A{name: "a cat"}
// foo1(cat)
s := []*A{dog}
foo2(s)
}
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
cannot use s (type []*A) as type []Base in argument …Run Code Online (Sandbox Code Playgroud) python ×4
perl ×3
go ×2
regex ×2
shell ×2
bash ×1
datetime ×1
dictionary ×1
interface ×1
macos ×1
mongodb ×1
mongoengine ×1
regex-group ×1
sed ×1
time ×1
tuples ×1
ubuntu ×1
upsert ×1
virtualenv ×1