Skip to main content

Simple IO eliminating the boilerplate.

Project description

CleanIO is a simple project illustrating how to use the yield statement to make reading and writing text files simple and to remove the clutter and boilerplate from your mainstream code.

Usage

Read a Text File

To read a text file.

  1. Add an import statement

    from cleanio import CleanRead

  2. Create an instance of the CleanRead class and pass it the desired filename or path

    cr = CleanRead(<<file name or path>>)

  3. Accept each line of the file from the generator (e.g. with a for statement) from the clean_read method

    for line in cr.clean_read():

  4. When the generator finishes, it will return a StopIteration exception (which the for statement will handle automatically

The file is opened, read, and closed automatically with no extra boilerplate needed.

Write a Text File

To write a text file:

  1. Add an import statement.

    from cleanio import CleanWrite

  2. Create an instance of the CleanWrite class and pass it the desired filename or path.

    cw = CleanWrite(<<file name or path>>)

  3. Send each line of the file to the generator via the clean_writeline method

    cw.clean_writeline(<<line>>)

  4. When done, notify the generator

    cw.clean_close()

The file is opened, written, and closed automatically with no extra boilerplate needed.

Comments

  • Read or write as many files as you need.

  • This has not been tested with async calls.

  • This code has been formatted with the blue library.

Structure

CleanIO consists of two classes, one for reading a text file and one for writing a text file.

Each class uses the yield statement for maintaining the position of the file and its place in the method. Other “boilerplate” code, such as the open, starting the generator, and closing is handled automatically in the class.

Possible Future Enhancements

There are several possible enhancement to this module that come to mind.

Currently this module is designed to read and write text files. It could be enhanced to also read and write binary files.

Currently the CleanWrite class assumes it will be getting one line at a time (without the newline or ‘\n’ at the end). It could be enhanced to output partial lines or multiple lines at a time if the user desires to put the line endings on themselves.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cleanio-0.1.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cleanio-0.1.4-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file cleanio-0.1.4.tar.gz.

File metadata

  • Download URL: cleanio-0.1.4.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.10.5-1-MANJARO

File hashes

Hashes for cleanio-0.1.4.tar.gz
Algorithm Hash digest
SHA256 cc4bb2ba837c17a8669969f80c0b119e51d349634590644281621499bd322229
MD5 51ba8fec6a3f42f5d7ada4516629e70c
BLAKE2b-256 d14b83c64aefd1acd2f0133205f829c75e4d3a35f5bb87fa987343cbf310ca53

See more details on using hashes here.

File details

Details for the file cleanio-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: cleanio-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.10.5-1-MANJARO

File hashes

Hashes for cleanio-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 297af38115c248a677b210f6771dfdc356926aacaf14b3cf7a6a8c36fdfc2d03
MD5 a6af1ebeb5cbeafdc25aab231812f89c
BLAKE2b-256 f2b44659a2a0bad1086fb3f2dc8217225178a2824fb3a25f14af30a816f1704c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page