Giter VIP home page Giter VIP logo

contextmanager's Introduction

ContextManager

A Python context manager for saving and loading objects to/from memory and files.

Features

  • In-memory storage
  • File persistence (Pickle and JSON formats)
  • Context names for identifying objects
  • List contexts method
  • Clear method for deleting all objects and files

Usage

To use the ContextManager, simply import it and create a new instance. You can then use the save and load methods to save and load objects to/from memory and files.

Here is an example of using the ContextManager to save and load an object to/from memory:

from context import ContextManager

# Create a new context manager
cm = ContextManager()

# Save an object to the context manager
cm.save('my_object', {'key': 'value'})

# Load the object from the context manager
loaded_object = cm.load('my_object')

# Print the loaded object
print(loaded_object)  # {'key': 'value'}

You can also use the ContextManager to save and load objects to/from files. To do this, you can use the save_to_file and load_from_file methods.

Here is an example of using the ContextManager to save and load an object to/from a Pickle file:

from context import ContextManager

# Create a new context manager
cm = ContextManager()

# Save an object to a Pickle file
cm.save_to_file('my_object.pickle', {'key': 'value'})

# Load the object from the Pickle file
loaded_object = cm.load_from_file('my_object.pickle')

# Print the loaded object
print(loaded_object)  # {'key': 'value'}

You can also use the ContextManager to list all contexts and clear all objects and files. To do this, you can use the list_contexts and clear methods.

Here is an example of using the ContextManager to list all contexts and clear all objects and files:

from context import ContextManager

# Create a new context manager
cm = ContextManager()

# Save some objects to the context manager
cm.save('my_object1', {'key1': 'value1'})
cm.save('my_object2', {'key2': 'value2'})

# List all contexts
print(cm.list_contexts())  # ['my_object1', 'my_object2']

# Clear all objects and files
cm.clear()

# List all contexts (should be empty)
print(cm.list_contexts())  # []

Installation

Copy the context.py file into your project directory.

License

MIT License. See LICENSE file for details.

Author

Tiago Bernardo

contextmanager's People

Contributors

apoll011 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.