Skip to main content

File access utils

Project description

ke

File access utils

To install: pip install ke

Overview

The ke package provides a comprehensive set of utilities for file access and manipulation, designed to simplify the process of reading, writing, and managing files both locally and on Amazon S3. It abstracts file paths and extensions handling, supports environment-based configurations, and integrates seamlessly with pandas for data handling.

Features

  • Local and S3 File Access: Simplify file operations in local filesystems and Amazon S3.
  • Environment Variable Integration: Use environment variables to manage file paths dynamically.
  • Extension Handling: Automatically manage file extensions when accessing files.
  • Read and Write Operations: Facilitate common file operations such as read, write, and append, with special handling for text and binary data.
  • Data Serialization: Utilize pickle for object serialization and deserialization.
  • Pandas Integration: Special methods to handle pandas objects effectively when loading from or saving to files.

Usage Examples

Basic File Access

To create a file accessor for local files:

from ke import for_local

# Create a file accessor for local files in the default root directory
local_accessor = for_local()

# Save a string to a file
local_accessor.save("Hello, World!", "hello.txt")

# Load a string from a file
content = local_accessor.load("hello.txt")
print(content)

Accessing Files on Amazon S3

To handle files on Amazon S3:

from ke import for_s3

# Create a file accessor for S3 with a specific relative root
s3_accessor = for_s3(relative_root='my/data/folder')

# Save data to a file on S3
s3_accessor.save("Data to save", "data.txt")

# Load data from a file on S3
data = s3_accessor.load("data.txt")
print(data)

Using Environment Variables

To use an environment variable for the root folder:

import os
from ke import for_local

# Assume 'MY_DATA_PATH' is an environment variable set to the data directory path
os.environ['MY_DATA_PATH'] = '/path/to/data'

# Create a file accessor that uses this environment variable
data_accessor = for_local(root_folder=os.environ['MY_DATA_PATH'])

# Work with files in the specified directory
data_accessor.save("Sample data", "sample.txt")

Working with Pandas

To save and load pandas DataFrames:

import pandas as pd
from ke import for_local

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Create a local file accessor
local_accessor = for_local()

# Save DataFrame to a pickle file
local_accessor.save(df, "dataframe.pkl")

# Load DataFrame from a pickle file
loaded_df = local_accessor.load("dataframe.pkl")
print(loaded_df)

Classes and Methods

  • Accessor: The main class used for creating file accessors. It handles the creation of file paths, loading, and saving of files.
  • for_local(): Factory function to create an Accessor for local file operations.
  • for_s3(): Factory function to create an Accessor for Amazon S3 file operations.
  • ms_data_path(): Utility function to construct a full path given a relative root, based on a predefined or environment-specified base directory.

Installation

To install the package, use pip:

pip install ke

For more details on the implementation and additional functionalities, refer to the source code documentation within the package.

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

ke-0.0.7.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

ke-0.0.7-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file ke-0.0.7.tar.gz.

File metadata

  • Download URL: ke-0.0.7.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for ke-0.0.7.tar.gz
Algorithm Hash digest
SHA256 403c66360954664c5f914e7407966545bde734a2a5e23b73d34225a6e178e827
MD5 6747e7229ebd6993ca4045999cde646f
BLAKE2b-256 d557926a57596ddab460c18814596dfedfedf90e415fac395e4a6eecaf4348d2

See more details on using hashes here.

File details

Details for the file ke-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: ke-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for ke-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d885e1253d1281740e4ad59d8305990eaafa6b9f896e0e5d357b202b36095e
MD5 11aa59eed4a39230b3eb5bc3f2bdf7aa
BLAKE2b-256 59a57bdd0f2ad18029e32c13eb8c7b3ca305c05b99439ba49c1eddd3155589af

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