Ví dụ về tempfile python
Ứng dụng của bạn phải ghi dữ liệu vào một tệp tạm thời để xử lý lại chúng trước khi hiển thị cho người dùng. Tệp sẽ được tạo tạm thời và quét của chúng tôi khi ứng dụng khởi động lại hoặc tắt. Làm thế nào để chúng tôi tạo ra một tập tin như vậy? Show Mục lục Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách tạo một tệp tạm thời trong Java. Có hai phương thức tĩnh 1 trong lớp Tệp Java, phương thức này sẽ giúp tạo tệp tạm thời trên vị trí thư mục TEMP mặc định và một phương thức khác được sử dụng để tạo tệp tạm thời trên vị trí thư mục đã chỉ địnhLàm thế nào để thực hiệnTôi trình bày một triển khai giới thiệu bằng một số ngôn ngữ được sử dụng rộng rãi. Việc triển khai chi tiết có thể hơi khác một chút JavaTrong ví dụ dưới đây, chúng tôi đã tạo hai tệp tạm thời. Trong cuộc gọi phương thức đầu tiên, tệp tạm thời được tạo trên vị trí thư mục TEMP mặc định của cửa sổ. Trong cuộc gọi phương thức thứ hai, chúng tôi đã chỉ định thư mục mà tệp sẽ được tạo package com.itersdesktop.javatechs; import java.io.File; import java.io.IOException; public class TempFileExample { public static void main(String[] args) { File tempFile = new File() try { File tempFile = File.createTempFile("my-data-file", ".dat"); System.out.println("Temp file On Default Location: " + tempFile.getAbsolutePath()); tempFile = File.createTempFile("my-data-file", ".dat", new File("C:/Users/tom/Temp")); System.out.println("Temp file On Specified Location: " + tempFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } finally { tempFile.deleteOnExit(); System.out.println("Exit!"); } } } hấp dẫnHãy cùng xem một ví dụ dưới đây. Có vẻ dễ dàng và rõ ràng File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}") Những gì chúng tôi đã sử dụng là phương pháp 2 để tạo một tệp có tên là 3 có thể được tìm thấy tại vị trí 4con trănĐể tạo một tệp tạm thời trong Python3. 7, chúng ta có thể sử dụng mô-đun 5. Mô-đun này tạo các tệp và thư mục tạm thời. Nó hoạt động trên tất cả các nền tảng được hỗ trợ. , , và là các giao diện cấp cao cung cấp tính năng dọn dẹp tự động và có thể được sử dụng làm trình quản lý bối cảnh. và là các chức năng cấp thấp hơn yêu cầu dọn dẹp thủ công>>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removedTóm tắt Tôi đã chỉ cho bạn một số triển khai mẫu để tạo tệp tạm thời trong Java, Groovy và Python. Mẹo này có thể giúp bạn tăng tốc công việc của mình bằng cách nào đó. Mã hóa vui vẻ AWS Lambda là dịch vụ điện toán cho phép bạn chạy mã mà không cần cung cấp hoặc quản lý máy chủ. Bạn có thể tạo các hàm Lambda và thêm chúng dưới dạng hành động trong quy trình của mình. Vì Lambda cho phép bạn viết các hàm để thực hiện hầu hết mọi tác vụ, nên bạn có thể tùy chỉnh cách thức hoạt động của đường dẫn của mình Không ghi nhật ký sự kiện JSON mà CodePipeline gửi tới Lambda vì điều này có thể dẫn đến thông tin đăng nhập của người dùng được ghi vào Nhật ký CloudWatch. Vai trò CodePipeline sử dụng sự kiện JSON để chuyển thông tin đăng nhập tạm thời cho Lambda trong trường 6. Đối với một sự kiện ví dụ, xemDưới đây là một số cách có thể sử dụng hàm Lambda trong quy trình
Việc tạo và chạy các hàm Lambda có thể khiến tài khoản AWS của bạn bị tính phí. Để biết thêm thông tin, xem Giá cả Chủ đề này giả định rằng bạn đã quen thuộc với AWS CodePipeline và AWS Lambda, đồng thời biết cách tạo quy trình, chức năng cũng như các chính sách và vai trò IAM mà chúng phụ thuộc vào. Chủ đề này chỉ cho bạn cách
Chủ đề này bao gồm các hàm mẫu để minh họa tính linh hoạt khi làm việc với các hàm Lambda trong CodePipeline
Mỗi chức năng mẫu bao gồm thông tin về các quyền mà bạn phải thêm vào vai trò. Để biết thông tin về giới hạn trong AWS Lambda, hãy xem Giới hạn trong Hướng dẫn dành cho nhà phát triển AWS Lambda Mã mẫu, vai trò và chính sách có trong chủ đề này chỉ là ví dụ và được cung cấp nguyên trạng Bước 1. Tạo một đường ống dẫnTrong bước này, bạn tạo một đường dẫn để sau này bạn thêm hàm Lambda vào đó. Đây là cùng một quy trình bạn đã tạo trong hướng dẫn CodePipeline. Nếu đường dẫn đó vẫn được định cấu hình cho tài khoản của bạn và ở cùng Khu vực mà bạn định tạo hàm Lambda, thì bạn có thể bỏ qua bước này Để tạo đường ống
Chính sách vai trò dịch vụ mặc định cho CodePipeline bao gồm các quyền Lambda cần thiết để gọi chức năng. Tuy nhiên, nếu bạn đã sửa đổi vai trò dịch vụ mặc định hoặc chọn một vai trò khác, hãy đảm bảo rằng chính sách dành cho vai trò đó cho phép các quyền 1 và 2. Mặt khác, các đường ống bao gồm các hành động Lambda không thành côngBước 2. Tạo hàm LambdaTrong bước này, bạn tạo một hàm Lambda để thực hiện yêu cầu HTTP và kiểm tra một dòng văn bản trên trang web. Trong bước này, bạn cũng phải tạo chính sách IAM và vai trò thực thi Lambda. Để biết thêm thông tin, hãy xem trong Hướng dẫn dành cho nhà phát triển AWS Lambda Để tạo vai trò thực thi
Cách tạo hàm Lambda mẫu để sử dụng với CodePipeline
Bước 3. Thêm hàm Lambda vào quy trình trong bảng điều khiển CodePipelineTrong bước này, bạn thêm một giai đoạn mới vào quy trình bán hàng của mình, sau đó thêm một hành động Lambda để gọi hàm của bạn đến giai đoạn đó Để thêm một giai đoạn
Bước 4. Kiểm tra quy trình bán hàng bằng hàm LambdaĐể kiểm tra chức năng, hãy phát hành thay đổi gần đây nhất thông qua quy trình Để sử dụng bảng điều khiển để chạy phiên bản phần mềm mới nhất thông qua một đường dẫn
Bước 5. Bước tiếp theoBây giờ, bạn đã tạo thành công một hàm Lambda và thêm nó dưới dạng một hành động trong quy trình bán hàng, bạn có thể thử cách sau
Sau khi bạn đã thử nghiệm xong chức năng Lambda, hãy cân nhắc xóa chức năng này khỏi quy trình của bạn, xóa nó khỏi AWS Lambda và xóa vai trò khỏi IAM để tránh các khoản phí có thể xảy ra. Để biết thêm thông tin, hãy xem Chỉnh sửa quy trình trong CodePipeline, Xóa quy trình trong CodePipeline và Xóa vai trò hoặc Hồ sơ phiên bản Sự kiện JSON mẫuVí dụ sau đây cho thấy một sự kiện JSON mẫu được CodePipeline gửi tới Lambda. Cấu trúc của sự kiện này tương tự như phản hồi đối với File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")73, nhưng không có kiểu dữ liệu File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")74 và File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")75. Hai chi tiết cấu hình hành động, File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")76 và File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")77, được bao gồm trong cả sự kiện JSON và phản hồi đối với API File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")78. Các giá trị trong File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")79 là ví dụ hoặc giải thích, không phải giá trị thực File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")7 Chức năng mẫu bổ sungCác hàm Lambda mẫu sau đây minh họa chức năng bổ sung mà bạn có thể sử dụng cho quy trình bán hàng của mình trong CodePipeline. Để sử dụng các chức năng này, bạn có thể phải sửa đổi chính sách cho vai trò thực thi Lambda, như đã lưu ý trong phần giới thiệu cho từng mẫu Hàm Python mẫu sử dụng mẫu AWS CloudFormationMẫu sau đây cho thấy một chức năng tạo hoặc cập nhật ngăn xếp dựa trên mẫu AWS CloudFormation được cung cấp. Mẫu tạo bộ chứa Amazon S3. Nó chỉ dành cho mục đích trình diễn, để giảm thiểu chi phí. Tốt nhất, bạn nên xóa ngăn xếp trước khi tải bất kỳ thứ gì lên thùng. Nếu bạn tải tệp lên bộ chứa, bạn không thể xóa bộ chứa khi xóa ngăn xếp. Bạn phải xóa thủ công mọi thứ trong nhóm trước khi có thể xóa chính nhóm đó Mẫu Python này giả định rằng bạn có một quy trình sử dụng bộ chứa Amazon S3 làm hành động nguồn hoặc bạn có quyền truy cập vào bộ chứa Amazon S3 đã được phiên bản mà bạn có thể sử dụng với quy trình. Bạn tạo mẫu AWS CloudFormation, nén và tải lên bộ chứa đó dưới dạng. tập tin nén. Sau đó, bạn phải thêm một hành động nguồn vào quy trình của mình để truy xuất hành động này. tệp zip từ thùng Khi Amazon S3 là nhà cung cấp nguồn cho quy trình của bạn, bạn có thể nén hoặc nhiều tệp nguồn của mình thành một. nén và tải lên. zip vào nhóm nguồn của bạn. Bạn cũng có thể tải lên một tệp đã giải nén; . tệp zip sẽ không thành công Mẫu này chứng minh
Để sử dụng hàm Lambda mẫu này, chính sách dành cho vai trò thực thi Lambda phải có quyền >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed63 trong AWS CloudFormation, Amazon S3 và CodePipeline, như thể hiện trong chính sách mẫu này >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6 Để tạo mẫu AWS CloudFormation, hãy mở bất kỳ trình soạn thảo văn bản thuần túy nào rồi sao chép và dán đoạn mã sau >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8 Lưu tệp này dưới dạng tệp JSON có tên >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed64 trong thư mục có tên >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed65. Tạo một nén (. zip) của thư mục này và tệp có tên >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed66, đồng thời tải tệp nén lên bộ chứa Amazon S3 đã được phiên bản. Nếu bạn đã định cấu hình bộ chứa cho quy trình của mình, thì bạn có thể sử dụng bộ chứa đó. Tiếp theo, hãy chỉnh sửa quy trình của bạn để thêm hành động nguồn truy xuất. tập tin nén. Đặt tên cho đầu ra cho hành động này là >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: .. fp.write(b'Hello world!') .. fp.seek(0) .. fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: .. print('created temporary directory', tmpdirname) >>> # directory and contents have been removed67. Để biết thêm thông tin, hãy xem Chỉnh sửa quy trình trong CodePipeline Hàm Lambda mẫu mong đợi các tên tệp và cấu trúc nén này. Tuy nhiên, bạn có thể thay thế mẫu AWS CloudFormation của riêng mình cho mẫu này. Nếu bạn sử dụng mẫu của riêng mình, hãy đảm bảo bạn sửa đổi chính sách cho vai trò thực thi Lambda để cho phép mọi chức năng bổ sung mà mẫu AWS CloudFormation của bạn yêu cầu Để thêm đoạn mã sau làm hàm trong Lambda
|