pypercard.datastore

PyperCard is a simple HyperCard inspired framework for PyScript for building graphical apps in Python.

Based on original pre-COVID work by Nicholas H.Tollervey.

Copyright (c) 2023 Anaconda Inc.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Module Contents

Classes

DataStore

A simple key/value data store.

API

class pypercard.datastore.DataStore(**kwargs)

A simple key/value data store.

Wraps a JavaScript Storage object for browser based data storage. Looks and feels mostly like a Python dict but has the same characteristics as a JavaScript localStorage object.

For more information see:

https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

Initialization

The underlying Storage object is an instance of Window.localStorage (it persists between browser opening/closing). Any **kwargs are added to the dictionary. All keys are silently prepended with the value of self.namespace.

clear()

Removes all items from the data store.

copy()

Returns a Python dict copy of the data store.

get(key, default=None)

Return the value of the item with the specified key.

items()

Yield over the key/value pairs in the data store.

keys()

Returns a list of keys stored by the user.

pop(key, default=None)

Pop the specified item from the data store and return the associated value.

abstract popitem()

Makes no sense given the underlying JavaScript Storage object’s behaviour. Raises a NotImplementedError.

setdefault(key, value=None)

Returns the value of the item with the specified key.

If the key does not exist, insert the key, with the specified value.

Default value is None.

update(iterable)

For each key/value pair in the iterable, insert them into the data store.

values()

Return a list of the values stored in the data store.

_namespace_key(key)

Convenience method to create a properly namespaced key.

__len__()

The number of items in the data store.

__getitem__(key)

Get and JSON deserialize the item stored against the given key.

__setitem__(key, value)

Set the value (as a JSON string) against the given key.

The underlying JavaScript Storage only stored values as strings.

__delitem__(key)

Delete the item stored against the given key.

__iter__()

Return an iterator over the keys.

__contains__(key)

Checks if a key is in the datastore.