Refactor for better string concatenation

Using `+` to concatenate strings isn’t very memory efficient. Not sure
if there are real-world implications for how it’s being used here, but
can’t hurt to use `.join` instead.

Rewriting it as a generator also lets us remove some unneeded variable
assignment.
This commit is contained in:
Chris Hill-Scott
2017-04-03 11:51:47 +01:00
parent f9264ea399
commit 7a412873d3

View File

@@ -267,18 +267,22 @@ def persist_letter(
def build_dvla_file(self, job_id): def build_dvla_file(self, job_id):
try: try:
if all_notifications_are_created_for_job(job_id): if all_notifications_are_created_for_job(job_id):
notifications = dao_get_all_notifications_for_job(job_id) file_contents = '\n'.join(
file = "" str(LetterDVLATemplate(
for n in notifications: notification.template.__dict__,
t = {"content": n.template.content, "subject": n.template.subject} notification.personalisation,
# This unique id is a 7 digits requested by DVLA, not known if this number needs to be sequential. # This unique id is a 7 digits requested by DVLA, not known
unique_id = int(''.join(map(str, random.sample(range(9), 7)))) # if this number needs to be sequential.
template = LetterDVLATemplate(t, n.personalisation, unique_id) numeric_id=int(''.join(map(str, random.sample(range(9), 7)))),
file = file + str(template) + "\n" ))
s3upload(filedata=file, for notification in dao_get_all_notifications_for_job(job_id)
region=current_app.config['AWS_REGION'], )
bucket_name=current_app.config['DVLA_UPLOAD_BUCKET_NAME'], s3upload(
file_location="{}-dvla-job.text".format(job_id)) filedata=file_contents + '\n',
region=current_app.config['AWS_REGION'],
bucket_name=current_app.config['DVLA_UPLOAD_BUCKET_NAME'],
file_location="{}-dvla-job.text".format(job_id)
)
else: else:
current_app.logger.info("All notifications for job {} are not persisted".format(job_id)) current_app.logger.info("All notifications for job {} are not persisted".format(job_id))
self.retry(queue="retry", exc="All notifications for job {} are not persisted".format(job_id)) self.retry(queue="retry", exc="All notifications for job {} are not persisted".format(job_id))