我有一个名为的脚本requests.py导入请求包.该脚本无法访问包中的属性,也无法导入它们.为什么这不起作用,我该如何解决?
以下代码提出了一个问题AttributeError.
import requests
res = requests.get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
以下代码提出了一个问题ImportError.
from requests import get
res = get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get' …Run Code Online (Sandbox Code Playgroud) 我正在尝试从环境变量中读取 python 脚本中的字典。
这是我的 python 脚本的代码:
desired_cap_default = [
{'platform': 'Windows 7', 'browserName': 'firefox', 'version': '24.0'},
{'platform': 'OS X 10.10', 'browserName': 'chrome', 'version': '45.0'},
{'platform': 'Windows XP', 'browserName': 'chrome', 'version': '40.0'},
{'platform': 'OS X 10.10', 'browserName': 'safari', 'version': '8.0'},
# {'platform': 'Windows XP', 'browserName': 'firefox', 'version': '10.0', 'screenResolution': '1600x1200',
# 'videoUploadOnPass': False, 'commandTimeout': 120}
]
browser = os.getenv('TESTING_BROWSERS', desired_cap_default)
Run Code Online (Sandbox Code Playgroud)
这就是我指定环境变量的方式(但没有将其识别为字典)
TESTING_BROWSERS="[{'platform': 'Windows 7', 'browserName': 'firefox', 'version': '24.0'}, \
{'platform': 'OS X 10.10', 'browserName': 'chrome', 'version': '45.0'}, \
{'platform': 'Windows XP', …Run Code Online (Sandbox Code Playgroud) 我正在尝试模拟一个特定的boto3函数.我的模块Cleanup导入boto3.清理也有一个"清洁"类.在init期间,cleaner创建了一个ec2客户端:
self.ec2_client = boto3.client('ec2')
Run Code Online (Sandbox Code Playgroud)
我想模拟ec2客户端方法:desribe_tags(),python说:
<bound method EC2.describe_tags of <botocore.client.EC2 object at 0x7fd98660add0>>
Run Code Online (Sandbox Code Playgroud)
我得到的最远的是在我的测试文件中导入botocore并尝试:
mock.patch(Cleaner.botocore.client.EC2.describe_tags)
Run Code Online (Sandbox Code Playgroud)
失败的是:
AttributeError: 'module' object has no attribute 'EC2'
Run Code Online (Sandbox Code Playgroud)
我该如何模仿这种方法?
清理看起来像:
import boto3
class cleaner(object):
def __init__(self):
self.ec2_client = boto3.client('ec2')
Run Code Online (Sandbox Code Playgroud)
ec2_client对象是具有desribe_tags()方法的对象.它是一个botocore.client.EC2对象,但我从不直接导入botocore.
目标:成功执行特定的 tox 命令,并让它为“仅”匹配的特定命令运行。
例子: tox -e py35-integration
tox应该只为 py35-integration 运行,不包括默认或独立py35定义。
我尝试了两种不同的方法,据我所知,这是尝试做我想做的事情的两种方法。
flake8命令是为了轻松隔离不同的命令并向我指示正在运行的内容。这并不是我真正想要运行的命令的指示。此外,ini 文件仅显示相关部分。
[tox]
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
py27: python -m testtools.run discover
py35: python -m testtools.run discover
py27-integration: flake8 {posargs}
py35-integration: flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)
使用这种方法,这里的理解是我希望在tox -e py27-integration不运行为py27命令定义的内容的情况下运行。这不是正在发生的事情。相反,它将同时运行py27和py27-integration。
[tox]
envlist = {py27,py35}, {py27,py35}-integration
[testenv]
commands =
python -m testtools.run discover
[testenv:integration]
commands =
flake8 {posargs}
Run Code Online (Sandbox Code Playgroud)
现在,我在这里明确地隔离了一个“子”环境,它有自己的命令来运行“集成”。
但是,不幸的是,我遇到了与正在执行的所有匹配模式“py27”完全相同的行为。 …
我使用以下代码读取pdf文件,但它没有读取它。可能是什么原因?
>>> import os
>>> from PyPDF2 import PdfFileReader, PdfFileWriter
>>> path = "/Users/Rahul/Desktop/Dfiles/"
>>> dirs = os.listdir( path )
>>> directory = "/Users/Rahul/Desktop/Dfiles/106_2015_34-76357.pdf"
>>> f = open(directory, 'rb')
>>> reader = PdfFileReader(f)
>>> contents = reader.getPage(0).extractText().split('\n')
>>> f.close()
>>> print contents
Run Code Online (Sandbox Code Playgroud)
输出是 [u''] 而不是读取内容。
我在 Django 中有一个模型,它有三个字段,这些字段是根据一个字段的值计算的。特别是其中一个字段的值将需要查询另一个表中的记录(我将使用最后十个值的平均值)。
我不确定将这个功能放在模型类中、模型表单中、视图中的最佳位置是什么?
任何建议将不胜感激 - 谢谢
该模型如下所示:
class slide_library(models.Model):
slide_name = models.Charfield(max_length = 6, primary_key = True)
reference_value = models.FloatField(default= '0')
last_mean = models.FloatField(default= '0')
esd = models.FloatField(default= '0')
criteria = models.Charfield(max_length= 10)
Run Code Online (Sandbox Code Playgroud) 我看过其他类似问题的帖子。我知道如何生成N个正整数。我也知道如何限制随机生成的整数的总和。唯一的问题是满足N个值均未超出指定范围的条件。
例如,generate_ints(n, total, low, high)应生成n个值数组,以使每个值介于低值和高值之间,并且总和加起来。任何指针/帮助将不胜感激。
例如generate_ints(4, 40, 4, 15)应该生成类似
[7,10,13,10]
Run Code Online (Sandbox Code Playgroud)
我不在乎数字是否重复,只要它们不高度偏斜即可。我np.randon.randint(5,15,n)用来选择整数。
到目前为止,我已经尝试了以下方法,但是它不起作用-
import numpy as np
import random
from random import uniform as rand
total=50
n=10
low=2
high=15
result=[]
m=0
nobs=1
while nobs <= n:
if m >= (total - low):
last_num= total -new_tot
result.append(last_num)
else:
next_num=np.random.randint(low,high,1)
new_tot = sum(result) + next_num
result.append(next_num)
m=new_tot
nobs +=1
print result
print sum(result)
Run Code Online (Sandbox Code Playgroud)
再次感谢。
输入: blah.(2/2)
所需输出: blah
输入可以是“ blah。(n / n)”,其中n可以是任何一位数字。
如何使用正则表达式实现“等等”?这是我目前无法使用的正则表达式:
m = re.sub('[.[0-9] /\ [0-9]]{6}$', '', m)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用带有Python和JSON的Google Maps地理编码器,但一直被告知我有一个错误的请求:
add = "Buckingham Palace, London, SW1A 1AA"
geocode_url = "https://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false®ion=uk" % add
print geocode_url
req = urllib2.urlopen(geocode_url)
jsonResponse = json.loads(f.read())
pprint.pprint(nest)
Run Code Online (Sandbox Code Playgroud)
这失败了urllib2.HTTPError: HTTP Error 400: Bad Request.
但如果我只是复制和粘贴
https://maps.googleapis.com/maps/api/geocode/json?address=Buckingham%20Palace,%20London,%20SW1A%201AA&sensor=false®ion=uk
Run Code Online (Sandbox Code Playgroud)
进入浏览器栏,它工作正常.
我的要求有什么问题?
尝试使用循环和列表将 5 添加到元组元素。
t=(10,20,30,40,50)
lst = []
for i in t:
lst[i]=t[i]+5
t = tuple(lst)
print(t)
Run Code Online (Sandbox Code Playgroud) python ×10
bash ×1
django ×1
exception ×1
geocoding ×1
google-maps ×1
json ×1
mocking ×1
numpy ×1
pypdf ×1
python-2.7 ×1
python-3.x ×1
random ×1
range ×1
regex ×1
shadowing ×1
testing ×1
tox ×1
unit-testing ×1