Skip to main content

Access and manipulate dictionaries and JSON files.

Project description

arkivist

Access and manipulate dictionaries and JSON files.

Import

from arkivist.arkivist import update, read

Usage

1. Reading from JSON file

people = read("people.json")

2. Updating JSON file

people = read("people.json")

# item 1
person = {}
person.update({"name": "Abc Xyz"})
person.update({"age": 12})
people.update({"abcxyz": person})

# item 2
person = {}
person.update({"name": "Lmn Opq"})
person.update({"age": 15})
people.update({"lmnopq": person})

# basic usage
update("people.json", people)

# adjust indent
update("people.json", people, indent=3)

# sort and reverse order
update("people.json", people, sort=True, reverse=True)

3a. Arkivist class, show variations

from arkivist import Arkivist


people = Arkivist("test.json")

person = {}
person.update({"name": "Juan"})
people.set({"juan": person})

print("Show all items (unsorted):\t", people.show())
print("Show all items (sorted):\t", people.show(sort=True))
print("Show all items (reverse):\t", people.show(sort=True, reverse=True))

3b. Arkivist class, manual save and search

from arkivist import Arkivist

## autosave = False
people = Arkivist("test.json", autosave=False)

person = {}
person.update({"name": "Maria"})
people.set({"maria": person})

# manual saving
people.save()

# save to another file
people.save(filepath="test.backup.json")

# search in dictionary
maria = people.search("maria", fallback="")
print("Search for Maria:\t\t", maria)

4. Replace and clear dictionary

from arkivist import Arkivist

people = Arkivist("test.json", autosave=False)

# print the number of entries
print("Count:\t\t", people.count())

5. Get the keys and the values

from arkivist import Arkivist

people = Arkivist("test.json", autosave=False)

# get the keys and values
print("Keys:\t\t", people.keys())
print("Values:\t\t", people.values())

6. Invert the dictionary

from arkivist import Arkivist

simple = Arkivist("test.simple.json")

simple.clear()
simple.set({0: "a"})
simple.set({1: "b"})
simple.set({2: "c"})

print("Normal:\t", simple.show())
# inverts the dictionary
# keys and values must be hashable
simple.invert()
print("Invert:\t", simple.show())

7. Replace and clear dictionary

from arkivist import Arkivist

people = Arkivist("test.json", autosave=False)

# print original contents
print("Old:\t\t", people.show())

# print new contents
alien = {"anon": {"name": "Anon"}}
people.replace(alien)
print("New:\t\t", people.show())

# print empty dictionary
people.clear()
print("Clear:\t\t", people.show())

7. Load new dictionary or JSON String

## Example #8
print("\nTest #8")
from arkivist import Arkivist

people = Arkivist("test.json")

# valid dictionary
people.load({"ufo": {"name": "UFO"}})
print("Valid Input: ", people.show())

# valid string
people.load('{"dog": {"name": "Doggy"}}')
print("Valid Input: ", people.show())

# flush invalid input
people.load(1234)
print("Invalid Input: ", people.show())

7. Flatten any nested dictionary

## Example #9
print("\nTest #9")
from arkivist import Arkivist


people = Arkivist("test.json")

# flattens nested dictionary
people.set({"dog": {"name": "Doggy"}})
people.set({"ufo": {"name": "UFO"}})
print("Flatten: ", people.flatten().show())

7. Fetch from web API

## Example #10
print("\nTest #10")
## do not save to file
todo = Arkivist()

# fetch from web api - invalid
todo.fetch("https://www.google.com")
# fetch from web api - valid
todo.fetch("https://jsonplaceholder.typicode.com/todos/1")
print("Show: ", todo.show())

7. Check if empty or not

print("\nTest #11")
## do not save to file
people = Arkivist("test.json").clear()
print("Count:", people.count(), "; Is empty: ", people.is_empty())

# flattens nested dictionary
people.update({"dog": {"name": "Doggy"}})
print("Count:", people.count(), "; Is not empty: ", people.is_not_empty())

7. Yield items

print("\nTest #12")
people = Arkivist("test.json").clear()

people.update({"abc": {"name": "Abc"}})
people.update({"dog": {"name": "Doggy"}})
people.update({"juan": {"name": "Juan"}})
people.update({"ufo": {"name": "UFO"}})
people.update({"xyz": {"name": "xyz"}})

# Get key value pairs
print("\nAll items:")
for key, value in people.items():
    print(key, value)

# Get key value pairs in reverse order
print("\nReverse:")
for key, value in people.items(sort=True, reverse=True):
    print(key, value)

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

arkivist-1.0.14.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

arkivist-1.0.14-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file arkivist-1.0.14.tar.gz.

File metadata

  • Download URL: arkivist-1.0.14.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.0

File hashes

Hashes for arkivist-1.0.14.tar.gz
Algorithm Hash digest
SHA256 a30de79ede03c1ac4f64ae258f3c2ace913676d58da0715aed58b9061803805e
MD5 74aeac6be939ded88ac169f948d5c873
BLAKE2b-256 26ae74b6d44822ded887a48a3035f417ec69e2f7b83c38c3322cf6f1682ae4f5

See more details on using hashes here.

File details

Details for the file arkivist-1.0.14-py3-none-any.whl.

File metadata

  • Download URL: arkivist-1.0.14-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.0

File hashes

Hashes for arkivist-1.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 0f358561332089ae5d1bfa24a4bf0affc3dab85cbd5eecdcafb3e0392eadc362
MD5 3eb5d3b48f40458eda3f422e5523f591
BLAKE2b-256 880b2d3f04a29fc0355db19bc0ee34298dcc7497e307426924a09666aec9b7df

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