我创建了一个包含多个小型用户定义函数的模块。其中许多函数需要不同的库函数。现在,我想知道什么是更好的做法 - 导入模块中的所有库,还是导入函数内的库?在性能方面有什么区别吗?
from math import exp
from numpy import mean,random
def logit(x):
return exp(x)/(1+exp(x))
def sample_mean(mu,sigma,size):
return mean(random.normal(mu,sigma,size))
Run Code Online (Sandbox Code Playgroud)
或者
def logit(x):
from math import exp
return exp(x)/(1+exp(x))
def sample_mean(mu,sigma,size):
from numpy import mean,random
return mean(random.normal(mu,sigma,size))
Run Code Online (Sandbox Code Playgroud)
这只是一个示例代码来解释我的困境。不要告诉我有现成的函数可以代替这些用户定义的函数。我已经知道了
我依稀记得execution_timeout的默认值是15分钟或30分钟,但我在任何地方都找不到相关文档。默认值是多少?
我知道如何获取工作表名称列表。我正在使用的Excel文件有多张纸。如何顺序选择第一个?我不知道工作表的名称,但是我需要选择第一个。我将如何处理?
我有一个netcdf文件,其中有几个值<0.我想用一个值(比如-1)替换所有值.我如何使用netCDF4做到这一点?我在这样的文件中阅读:
import netCDF4
dset = netCDF4.Dataset('test.nc')
dset[dset.variables['var'] < 0] = -1
Run Code Online (Sandbox Code Playgroud) 当我在这些行中执行python3 manage.py shell:
In [1]: from dream_site.models import Mentor, Mentee, Team
In [2]: a = Mentee.objects.get( pk = 1 ).name
In [3]: b = Mentor.objects.get(pk=1).name
In [4]: print( a + b )
Run Code Online (Sandbox Code Playgroud)
它会按预期打印正确的输出,但是models.py当我有以下代码时,它会给我int() argument must be a string or a number错误:
def __str__(self): # Belonging to Team object
return ( str( self.pk ) + " - " + Mentor.objects.get( pk = self.mentor_id ).name + " - " + Mentee.objects.get( pk = self.mentee_id ).name ) …Run Code Online (Sandbox Code Playgroud) Python:3.4.3
Django:1.9.7
异常类型:TypeError
异常值:“_io._IOBase”对象的描述符“fileno”需要参数
异常位置:/usr/lib/python3/dist-packages/PIL/ImageFile.py在 _save,第 454 行
这是我在终端中测试过的代码 -
import urllib.request
from PIL import Image
from io import BytesIO
url = 'http://s.inyourpocket.com/gallery/108367.jpg'
i = Image.open(BytesIO(urllib.request.urlopen(url).read()))
img_file = BytesIO()
i.save(img_file, 'JPEG')
Run Code Online (Sandbox Code Playgroud)
该代码在终端中运行得很好,但是一旦在 Django 服务器上进行测试,它就会给我这些错误 -
File "PATH/utils.py", line 124, in pil_to_django
image.save(img_file, 'JPEG')
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1468, in save
save_handler(self, fp, filename)
File "/usr/lib/python3/dist-packages/PIL/JpegImagePlugin.py", line 579, in _save
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)], bufsize)
File "/usr/lib/python3/dist-packages/PIL/ImageFile.py", line 454, in _save
fh = fp.fileno()
TypeError: descriptor 'fileno' of '_io._IOBase' object …Run Code Online (Sandbox Code Playgroud) 我使用以下Python代码删除BigQuery中一个表中的所有数据,当我在运行代码几秒钟后检查该表时,我发现该表现在是空的,因此删除肯定成功了。但是,作业状态仍然显示它处于 RUNNING 状态。
from google.cloud import bigquery
import time
client = bigquery.Client()
job_config = bigquery.QueryJobConfig()
job_config.priority = bigquery.QueryPriority.INTERACTIVE
sql = """DELETE FROM <table_name> WHERE TRUE"""
query_job = client.query( sql, job_config= job_config )
query_job = client.get_job( query_job.job_id ) # API request - fetches job
while query_job.state == "RUNNING":
print( "Job {} is currently in state {}".format( query_job.job_id, query_job.state ) )
time.sleep( 5 )
if query_job.errors != None:
print( "Query Failed." )
raise Exception( "Query Failed. Error: [ %s ]." % query_job.error_result …Run Code Online (Sandbox Code Playgroud)