我目前在我的主程序包中有一个名为的文件main.go.如何在main.go不创建单独的包的情况下将内容拆分为多个文件,因为代码不可重用.
我想要一个像这样的目录结构:
$ ls foo
main.go
bar.go
Run Code Online (Sandbox Code Playgroud)
bar.go
package main
import "fmt"
func Bar() {
fmt.Println("Bar")
}
Run Code Online (Sandbox Code Playgroud)
然后进去 main.go
package main
func main() {
Bar()
}
Run Code Online (Sandbox Code Playgroud)
但是go run main.go给了我:
# command-line-arguments
./main.go:4:2: undefined: Bar
Run Code Online (Sandbox Code Playgroud) 在这段代码中,我可以从条形函数内部打印计数器的值
def foo():
counter = 1
def bar():
print("bar", counter)
return bar
bar = foo()
bar()
Run Code Online (Sandbox Code Playgroud)
但是当我尝试从bar函数内部递增计数器时,我得到一个UnboundLocalError错误.
UnboundLocalError: local variable 'counter' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
带有增量语句的代码片段.
def foo():
counter = 1
def bar():
counter += 1
print("bar", counter)
return bar
bar = foo()
bar()
Run Code Online (Sandbox Code Playgroud)
您是否只在Python闭包中对外部函数中的变量具有读取权限?
我正在使用nginx作为代理服务器将请求转发到我的gunicorn服务器上.当我运行时,sudo nginx -t -c /etc/nginx/sites-enabled/mysite我得到以下错误.
[emerg]: unknown directive "upstream" in /etc/nginx/sites-enabled/mysite:1
configuration file /etc/nginx/sites-enabled/mysite test failed
Run Code Online (Sandbox Code Playgroud)
知道怎么解决吗?这是我的nginx配置:
upstream gunicorn_mysite {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
server_name example.com;
access_log /usr/local/django/logs/nginx/mysite_access.log;
error_log /usr/local/django/logs/nginx/mysite_error.log;
location / {
proxy_pass http://gunicorn_mysite;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在运行Ubuntu 10.04,我的nginx版本是0.7.65,我是从apt安装的.
这是我运行nginx -V时的输出
nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-http_gzip_static_module --with-http_realip_module --with-mail --with-mail_ssl_module --with-ipv6 --add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair
Run Code Online (Sandbox Code Playgroud) 我正在使用PIL在图像上绘制文字.我如何包装一串文字.这是我的代码:
text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
image = Image.open("/tmp/background-image.jpg")
draw = ImageDraw.Draw(image)
font = …Run Code Online (Sandbox Code Playgroud) 如果请求URL不使用AJAX,应该返回什么状态代码?400?
鉴于此示例 Starlette 应用程序具有开放的 websocket 连接,您如何关闭 Starlette 应用程序?我在 uvicorn 上运行。每当我按下Ctrl+C输出时,Waiting for background tasks to complete.它就会永远挂起。
from starlette.applications import Starlette
app = Starlette()
@app.websocket_route('/ws')
async def ws(websocket):
await websocket.accept()
while True:
# How to interrupt this while loop on the shutdown event?
await asyncio.sleep(0.1)
await websocket.close()
Run Code Online (Sandbox Code Playgroud)
我尝试在关闭事件上切换 bool 变量,但该变量永远不会更新。它总是False。
例如。
app.state.is_shutting_down = False
@app.on_event('shutdown')
async def shutdown():
app.state.is_shutting_down = True
@app.websocket_route('/ws')
async def ws(websocket):
await websocket.accept()
while app.state.is_shutting_down is False:
Run Code Online (Sandbox Code Playgroud) 如何检查字符串是否具有翻译值?我正在使用AngularJS和AngularTranslate.
我只想显示一个已翻译过的值.如果没有可用的翻译,Angular Translate将显示未翻译的字符串.
我开始这样做了:
<div ng-if="question.text | translate != question.text">{{ question.text | translate }}</div>
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为比较发生在翻译过滤器完成它的工作之前.(至少我认为这是发生的事情).
我最终做的是:
.filter('isTranslated', function(){
return function(translatedVal, originalVal){
return (translatedVal === originalVal) ? false : true;
}
Run Code Online (Sandbox Code Playgroud)
})
<div ng-if="question.text | translate | isTranslated:question.text">{{ question.text | translate }}</div>
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我想知道是否有更好的方法这样做?
我正在尝试使用fabric来部署Django项目,当我运行时出现此错误hg pull:
[myusername.webfactional.com] run: hg pull
[myusername.webfactional.com] out: remote: Warning: Permanently added the RSA host key for IP address '207.223.240.181' to the list of known hosts.
[myusername.webfactional.com] out: remote: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[myusername.webfactional.com] err: abort: no suitable response from remote hg!
Fatal error: run() encountered an error (return code 255) while executing 'hg pull'
Run Code Online (Sandbox Code Playgroud)
我可以运行其他mercurial命令hg status,hg log就像我的fab文件一样好.
我在服务器上生成了一个SSH密钥,并将其添加到我的bitbucket帐户.这可以工作,因为我可以SSH并运行hg pull,它工作正常,它只在使用结构时.
这是我的fabfile:
from __future__ import with_statement
from fabric.api import *
env.hosts = ['myusername.webfactional.com']
env.user …Run Code Online (Sandbox Code Playgroud) Go有类似Python的in关键字吗?我想检查一个值是否在列表中.
例如在Python中:
x = 'red'
if x in ['red', 'green', 'yellow', 'blue']:
print "found"
else:
print "not found"
Run Code Online (Sandbox Code Playgroud)
在Go中,我想出了使用set惯用法,但我不认为这是理想的,因为我必须指定一个我没有使用的int值.
x := "red"
valid := map[string]int{"red": 0, "green": 0,"yellow": 0, "blue": 0}
if _, ok := valid[x]; ok {
fmt.Println("found")
} else {
fmt.Println("not found")
}
Run Code Online (Sandbox Code Playgroud)
我知道有一个in关键字可能与泛型有关.有没有办法使用go generate或其他东西来做到这一点?
我是 GraphQL 新手。我正在使用Graphene-Django并且有一个名为 的突变CreateUser。它需要三个参数username, email, password。
如何验证数据并返回多个错误?
我想要这样的东西回来。
{
"name":[
"Ensure this field has at least 2 characters."
],
"email":[
"This field may not be blank."
],
"password":[
"This field may not be blank."
]
}
Run Code Online (Sandbox Code Playgroud)
所以我可以在表单上呈现错误,如下所示:
到目前为止我的代码:
from django.contrib.auth.models import User as UserModel
from graphene_django import DjangoObjectType
import graphene
class User(DjangoObjectType):
class Meta:
model = UserModel
only_fields = 'id', 'username', 'email'
class Query(graphene.ObjectType):
users = graphene.List(User)
user = graphene.Field(User, id=graphene.Int())
def …Run Code Online (Sandbox Code Playgroud)