Giter Site home page Giter Site logo

base64file's Introduction

Base64File

An IO wrapper that but reads/writes binary data as base64 text, supporting concurrent reading and writing. Similar to GzipFile, it simulates most of the methods of a standard file object (with the exception of readline).

Usage

base64file.open()

  • Similar to gzip.open() or io.open()
import base64file

# to write text
with base64file.open('some-file.txt', 'rt', encoding='utf8') as f:
    f.write('1234567890')

# to write binary
with base64file.open('some-file.txt', 'rb') as f:
    f.write(b'\1\2\3\4\5')

base64file.Base64File

  • When opening a Base64File without specifying an underlying file, a filename must be provided, and the usual binary modes are supported: r, rb, w, wb, a, ab, x, and xb (optional + to enable concurrent reading/writing, e.g. wb+).
  • When opening a Base64File with a specified underlying file, the mode is inherited from the specified file (replacing text-mode with binary-mode). If a binary-mode file is provided, it is automatically wrapped with a TextIOWrapper.
from base64file import Base64File

# open a new/existing file for concurrent reading and writing
with Base64File('some-file.txt', 'w+') as b:
    b.write(b'\0\1\2\3\4')
    b.seek(0)
    b.write(b'\n')
    print(b.read(2))  # prints b'\1\2'

# wrap an existing file (in this example, open for reading)
with open('some-file.txt', 'r') as f:
    with Base64File(file_obj=f) as b:
        print(b.read(2))  # prints b'\n\1'
        print(b.read())  # prints b'\2\3\4'

# if you need to keep the file open, remember to close it
f = open('some-file.txt', 'w+')
b = Base64File(file_obj=f)
b.write(b'\0\1\2\3\4')
b.seek(0)
b.write(b'\n')
print(b.read(2))  # prints b'\1\2'
b.close()  # this is necessary, otherwise the final 1-2 bytes may not be written
f.close()

base64file.Base85File and base64file.Ascii85File

  • The same as base64file.Base64File, but instead uses b85encode() or a85encode()
  • Note that many implementations of base85 are actually Ascii85 (for example, CyberChef.io)
from base64file import Ascii85File
from base64file import Base85File

base64file's People

Contributors

averykhoo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.