我必须创建PDF文件,其中需要在页面左下方添加像页脚一样的行.
以下代码正在运行:
import StringIO
from reportlab.pdfgen import canvas
import uuid
def test(pdf_file_name="abc.pdf", pdf_size=(432, 648), font_details=("Times-Roman", 9)):
# create a new PDF with Reportla
text_to_add = "I am writing here.."
new_pdf = "test_%s.pdf"%(str(uuid.uuid4()))
packet = StringIO.StringIO()
packet.seek(0)
c = canvas.Canvas(pdf_file_name, pagesize = pdf_size)
#- Get the length of text in a PDF.
text_len = c.stringWidth(text_to_add, font_details[0], font_details[1])
#- take margin 20 and 20 in both axis
#- Adjust starting point on x axis according to text_len
x = pdf_size[0]-20 - text_len …Run Code Online (Sandbox Code Playgroud) 最近我陷入了困境.算法部分需要计算长度为K的滑动窗口的最大元素之和.其中K的范围是1 <= K <= N(阵列的N长度).
示例如果我有一个数组A作为5,3,12,4
长度为1的5 + 3 + 12 + 4 = 24
滑动窗口:长度为2的5 + 12 + 12 = 29
滑动窗口:长度为3的12 + 12 = 24
滑动窗口:长度为4的滑动窗口:12
Final answer is 24,29,24,12.
我试过这个O(N ^ 2).对于长度为K的每个滑动窗口,我可以用O(N)计算最大值.由于K高达N.因此,总体复杂度变为O(N ^ 2).
我正在寻找O(N)或O(NlogN)或与此算法类似的东西,因为N可能高达10 ^ 5.
注意:数组中的元素可以大到10 ^ 9,因此输出最终答案为模10 ^ 9 + 7
编辑:我实际上想要找到K的每个值(即从0到N)的答案线性时间或O(NlogN)不在O(KN)或O(KNlogN)中,其中K = {1,2,3,...... N}
我有附加到 DynamoDB 更改事件的 Lambda 函数。当我更改/修改 DynamoDB 中的测试机表中的项目时,Lambda 会触发两次。
我正在将IsMachineOn值从修改True为False,它触发Test-Machine-On-alert-statusLambda 函数两次。
我不明白为什么两次 lambda 是触发器。
我观察到Lambda 参数records发生了微小变化。event
对于第一个触发器,
NewImage["IsMachineOn"]["BOOL"]的值为False
OldImage["IsMachineOn"]["BOOL"]的值为True
对于第二个触发器,
NewImage["IsMachineOn"]["BOOL"]的值为False
OldImage["IsMachineOn"]["BOOL"]的值为False
我打开了业务逻辑,NewImage["IsMachineOn"]["BOOL"]==False以便我的业务逻辑运行两次。
有两件事:
我想从输入内容中获取JS文件名,其中包含jqueryRE作为子字符串.
这是我的代码:
第1步:从内容中提取JS文件.
>>> data = """ <script type="text/javascript" src="js/jquery-1.9.1.min.js"/>
... <script type="text/javascript" src="js/jquery-migrate-1.2.1.min.js"/>
... <script type="text/javascript" src="js/jquery-ui.min.js"/>
... <script type="text/javascript" src="js/abc_bsub.js"/>
... <script type="text/javascript" src="js/abc_core.js"/>
... <script type="text/javascript" src="js/abc_explore.js"/>
... <script type="text/javascript" src="js/abc_qaa.js"/>"""
>>> import re
>>> re.findall('src="js/([^"]+)"', data)
['jquery-1.9.1.min.js', 'jquery-migrate-1.2.1.min.js', 'jquery-ui.min.js', 'abc_bsub.js', 'abc_core.js', 'abc_explore.js', 'abc_qaa.js']
Run Code Online (Sandbox Code Playgroud)
第2步:获取具有子字符串的JS文件 jquery
>>> [ii for ii in re.findall('src="js/([^"]+)"', data) if "jquery" in ii]
['jquery-1.9.1.min.js', 'jquery-migrate-1.2.1.min.js', 'jquery-ui.min.js']
Run Code Online (Sandbox Code Playgroud)
我可以在上面的步骤1中执行步骤2表示RE模式以获得结果吗?
我有以下格式的字典:
演示代码:
>>> import pprint
>>> pprint.pprint(data)
{'lookup': {'F01': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F02': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F03': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n …Run Code Online (Sandbox Code Playgroud) 哪个是从函数返回布尔值的最佳实践:
代码1:
(status, response) = self.generate_pdf(html_data)
if not status:
return (False, response)
Run Code Online (Sandbox Code Playgroud)
代码2:
(status, response) = self.generate_pdf(html_data)
if not status:
return (status, response)
Run Code Online (Sandbox Code Playgroud)
根据我的回归硬编码值(False或True)是好的.
[更多细节]我总是从每个函数返回两个参数,status并且response.A status是布尔对象,即值为True或False.A response是字典对象.
由于status有一个值False,即status所指的地址False.
>>> id(False)
493227104
>>> status = False
>>> id(status')
493227104
Run Code Online (Sandbox Code Playgroud)
那么返回引用变量名是好的做法还是实际的bool值?
我必须找到在 django 项目中运行查询所花费的时间,即python manage.py shell
代码:
>>> import timeit
>>> d = {"a":1, "b":2}
>>> def a1():
... for i in d:
... a = i, d[i]
...
>>> a1()
>>> print "Time 1:", timeit.timeit('a1()', 'from __main__ import a1 as a1')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib64/python2.6/timeit.py", line 227, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "/usr/lib64/python2.6/timeit.py", line 193, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 3, in inner
ImportError: …Run Code Online (Sandbox Code Playgroud) 我从 boto3 组织的主 AWS 账户中获取所有子账户。代码运行良好。我可以获得儿童帐户列表。但是,如果您再次运行我的 AWS Lambda 函数,则它无法获取子帐户。
出现以下错误:
Error while getting AWS Accounts : An error occurred (TooManyRequestsException) when calling the ListAccounts operation: AWS Organizations can't complete your request because another request is already in progress. Try again later
Run Code Online (Sandbox Code Playgroud)
20 到 30 分钟后,我可以看到我的代码一次又一次地引发上述异常。
我通过 AWS Gateway + AWS Lambda 运行此代码。
任何想法?
代码:
import boto3
class Organizations(object):
"""AWS Organization"""
def __init__(self, access_key, secret_access_key, session_token=None):
self.client = boto3.client('organizations',
aws_access_key_id=access_key,
aws_secret_access_key=secret_access_key,
aws_session_token=session_token
)
def get_accounts(self, next_token=None, max_results=None):
"""Get Accounts List"""
if next_token …Run Code Online (Sandbox Code Playgroud) python ×6
aws-lambda ×2
algorithm ×1
boolean ×1
boto3 ×1
comparison ×1
dictionary ×1
django ×1
list ×1
pdf ×1
python-2.7 ×1
python-3.x ×1
regex ×1
reportlab ×1
string ×1
tuples ×1