pax_global_header00006660000000000000000000000064150666226620014525gustar00rootroot0000000000000052 comment=550a1cfa79c9f085c069dd33e774358acc371717 fralau-super-collections-550a1cf/000077500000000000000000000000001506662266200170555ustar00rootroot00000000000000fralau-super-collections-550a1cf/.github/000077500000000000000000000000001506662266200204155ustar00rootroot00000000000000fralau-super-collections-550a1cf/.github/workflows/000077500000000000000000000000001506662266200224525ustar00rootroot00000000000000fralau-super-collections-550a1cf/.github/workflows/greetings.yml000066400000000000000000000006661506662266200251740ustar00rootroot00000000000000name: Greetings on: [pull_request_target, issues] jobs: greeting: runs-on: ubuntu-latest permissions: issues: write pull-requests: write steps: - uses: actions/first-interaction@v1 with: repo-token: ${{ secrets.GITHUB_TOKEN }} issue-message: "Welcome to this project and thanks for your contribution!" pr-message: "Message that will be displayed on users' first pull request" fralau-super-collections-550a1cf/.github/workflows/test.yml000066400000000000000000000013151506662266200241540ustar00rootroot00000000000000name: Run Pytest on Multiple Python Versions on: push: branches: - main - master pull_request: jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip pip install -e .[test] # or just pip install pytest if you don't use extras - name: Run tests run: pytest fralau-super-collections-550a1cf/.gitignore000066400000000000000000000006751506662266200210550ustar00rootroot00000000000000# Temporary files *~ .py~ .python-version #Byte-compiled led / optimized / DLL files __pycache__/ *.py[cod] *$py.class # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # MkDocs site/ # Mkdocs-Macros __*/ # Other files (generated by mkdocs-macros or others) cache* # JetBrains PyCharm and other IntelliJ based IDEs .idea/ fralau-super-collections-550a1cf/LICENSE000066400000000000000000000020651506662266200200650ustar00rootroot00000000000000MIT License Copyright (c) 2024 Laurent Franceschetti Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.fralau-super-collections-550a1cf/MANIFEST.in000066400000000000000000000000361506662266200206120ustar00rootroot00000000000000include test/*.json test/*.md fralau-super-collections-550a1cf/README.md000066400000000000000000000374371506662266200203520ustar00rootroot00000000000000
# Python Super Collections **Dictionaries and lists as you dreamed them when you were a kid.** Instantly Convert json and YAML files into objects with attributes.
```python import json from super_collections import SuperDict with open('my_file.json', 'r') as file: data = json.load(file) document = SuperDict(data) print(document.author) # instead of document['author'] for document in document.blocks: # instead of document['blocks'] ... print(document.blocks[3].name) # instead of document['blocks'][3]['name'] -- eek! 🀒 ``` ________ - [Python Super Collections](#python-super-collections) - [What are SuperCollections?](#what-are-supercollections) - [What is the problem?](#what-is-the-problem) - [Definitions](#definitions) - [How it works](#how-it-works) - [Superdicts](#superdicts) - [Superlists](#superlists) - [Why Combining SuperDicts with SuperLists?](#why-combining-superdicts-with-superlists) - [SuperCollection](#supercollection) - [Factory function](#factory-function) - [Install](#install) - [From the repository](#from-the-repository) - [Usage](#usage) - [Export to JSON or Hjson](#export-to-json-or-hjson) - [Export to YAML](#export-to-yaml) - [Remarks](#remarks) - [Restrictions](#restrictions) - [Does it work?](#does-it-work) - [When are superdictionaries and superlists _not_ recommended?](#when-are-superdictionaries-and-superlists-not-recommended) - [Shelves and SuperShelves](#shelves-and-supershelves) - [Metaphor of a shelf](#metaphor-of-a-shelf) - [Definition of a shelf (data structure)](#definition-of-a-shelf-data-structure) - [Iteration through a shelf](#iteration-through-a-shelf) - [SuperShelf](#supershelf) - [Related data structures and ideas](#related-data-structures-and-ideas) - [Standard Python](#standard-python) - [Dot notation on dictionaries](#dot-notation-on-dictionaries) - [Using superlists to complement superdictionaries](#using-superlists-to-complement-superdictionaries) ## What are SuperCollections? ### What is the problem? There are several packages that quickly convert json or YAML files into dictionaries that contain dictionaries, lists etc. If you want to properly use those data structures in Python, one solution is to create specific classes. But sometimes, it is overkill. You just want your app to quickly load structured data and navigate through them. That's where the **super-collections** package comes handy. ### Definitions - A **SuperCollection** is a nested data structure that can encode any type of information (in the same way as a JSON file). It is essentially constituted of dictionaries and lists, where dictionaries can contain lists and vice-versa, and the root can be either a dictionary or a list. - An **elementary datatype** is a non-mutable type: str, int, str, float in Python that is used to build classes. - A **SuperList** is a list that can contain SuperCollections, or elementary datatypes. - A **SuperDict** is a dict that can contain SuperCollections, or elementary datatypes. A key advantage of SuperDicts over dictionaries, is that its keys can be accessed as attributes (providing they are valid Python identifiers and they don't conflict with pre-existing attributes). ## How it works ### Superdicts > πŸ“ **Definition**
A **superdictionnary** is a dictionary whose keys (at least those that are valid identifiers) are automatically accessible as attributes, with the **dot notation*. ```python d = SuperDict({'foo':5, 'bar': 'hello'}) # instead of writing d['foo'] d.foo = 7 ``` > Several other languages, such as Javascript, LUA, Ruby, and PHP offer that **dot notation** > in some form or other. However, implementing that idea is not as > straightforward as it seems. > The idea of superdictionaries in Python has been around for some time > (see the [superdict](https://github.com/itdxer/superdict) packagage by Yuri > Shevchuk, 2015). > πŸ“ **Property**
If a SuperDict object contains a value that is itself a dictionary, that dictionary is then converted in turn into a SuperDict. If the object is not a dict or immediately compatible, it will try the following conversions: - the methods `.asdict()`, `.dict()` or `dump()`, providing they actually generate a `dict`. - if the object is a dataclass, it will apply the `asdict()` function on it. ### Superlists A **superlist** is a list where all dictionary items have been (automagically) converted to **superdictionnaries**. > ⚠️ **Superlists are indispensable**
They were the missing piece of the jigsaw puzzle; > without them, it is not possible to convert deep data structures into supercollections. ### Why Combining SuperDicts with SuperLists? The structure of JSON, YAML or HTML data is generally a deeply nested combination of dictionaries and lists. Using superdictionaries alone would not be sufficient, since lists within the data contained in a list would still contain regular (unconverted) dictionaries; this would require you to switch back to the standard dictionary access method. By combining superdictionaries and superlists, it is possible to ensure that all nested dictionaries within lists will also be converted to SuperDicts, allowing for a consistent dot notation throughout the entire data structure. > πŸ’‘ **Deep conversion**
SuperLists objects, combined with SuperDicts make sure that the most complex > datastructures (from json or YAML) can be recursively converted into > well-behaved Python objects. ### SuperCollection **SuperCollection** is an abstract class containing SuperList and SuperDict. It means that SuperList and SuperDict objects are **instances** of SuperCollection, but they do not inherit from it. ```python obj1 = SuperList([1, 3, 5]) assert isinstance(obj1, SuperCollection) obj2 = SuperDict({'a': 5, 'b':7}) assert isinstance(obj2, SuperCollection) ``` ### Factory function You can use the `super_collect()` function to create a SuperCollection (SuperList or SuperDict) from an object. **It is particularly useful for converting Python data structures such as JSON files.** ```python import json with open(FILENAME, 'r', encoding='utf-8') as f: data = json.load(f) content = super_collect(data) ``` `super_collect()` is designed to work on any list or dict, but it will also attempt to process other types: - all **sequences** (see [definition](https://docs.python.org/3/glossary.html#term-sequence)), in other words objects whose class is registered as instance of `collections.abs.Sequence` will be converted into lists. This applies to `tuple`, `range`, `collections.UserList`, etc. - Special types: `set`, `deque` as well as `ndarray` (Numpy or compatible) and `Series` (Pandas and others; your mileage may vary). - Otherwise, will try to generate a SuperDict. This function is also available as a static method of the `SuperCollection` class: ```python content = SuperCollection.collect(data) ``` ## Install ### From the repository ```sh pip install super-collections ``` ## Usage ```python from super_collections import SuperDict, SuperList d = SuperDict({'foo':5, 'bar': 'hello'}) l = SuperList([5, 7, 'foo', {'foo': 5}]) ``` You can cast any dictionary and list into its "Super" equivalent when you want, and you are off to the races. **The casting is recursive** i.e. in the case above, you can assert: ```python l[-1].foo == 5 ``` All methods of dict and list are available. Those objects are self documented. `d.properties()` is a generator that lists all keys that are available as attributes. The `__dir__()` method (accessible with `dir()`) is properly updated with those additional properties. ```python list(d.properties()) > ['foo', 'bar'] dir(d) > ['__class__', ..., 'bar', 'clear', 'copy', 'foo', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'properties', 'setdefault', 'to_hjson', 'to_json', 'update', 'values'] ``` This means the **auto-complete feature** might be available for the attributes of a SuperDict within a code editor (if the dictionary was statically declared in the code); or in an advanced REPL (such as [bpython](https://bpython-interpreter.org/)). The methods `dict.update(other_dict)` and `list.extend(other_list)` automatically cast the contents into SuperDict and SuperList as needed. ## Export to JSON or Hjson You can export a SuperDict or SuperList to JSON or [Hjson](https://hjson.github.io/), for debug purposes. It is not guaranteed that it will preserve all the meaningful information you want, but all basic types will be preserved. > Python DateTimes are converted into ISO Dates. ```python print (d.to_json()) print (d.to_hjson()) ``` The module also exports a `json_encode()` function, which will attempt to serialize any object in Python to JSON (in an opinionated way). ## Export to YAML If you wish to use PyYAML and guarantee the SuperDict and SuperList behave exactly as dict and list, use the `yaml_support()` function. This works with both `dump()` and `safedump()` ```python from super_collections import SuperDict, SuperList, yaml_support yaml_support() d = SuperDict({"x": 1}) l = SuperList(["a", "b"]) dumped_dict = yaml.dump(d) dumped_list = yaml.dump(l) ``` ## Remarks ### Restrictions 1. In a SuperDict, **only keys that are valid Python identifiers can be accessed as attributes**. If 'bar' is a key of object `foo`, you can write `foo.bar`; but you can't write ~~`foo.hello world`~~ because 'hello world' is not a valid Python identifier; you will have to access that specific value with the "dictionary" notation: `foo['hello world']`. 2. Similarly, you can't use pre-existing methods of the `dict` class: `keys`, `items`, `update`, etc. as properties; as well as the `properties` method itself (wich is specific to SuperDict). In that case again, use the dictionary notation to access the value (`d['items']`, etc.). Those keys that cannot be accessed as attributes are said to be **masked**. If you are uncertain which are available, just use `SuperDict.properties()`. method. 3. Updating a single element (`d['foo']` for a SuperDict and `l[5]` for a SuperList) does not perfom any casting. That's to avoid crazy recursive situations, while giving you fine grain control on what you want to do (just cast with `SuperDict()` and `SuperList()`). ### Does it work? Yes. It is tested with pytest. See the `test` directory for examples. ### When are superdictionaries and superlists _not_ recommended? SuperDicts (and SuperLists) classes are most useful when the program you are writing is consuming loosely structured data (json, YAML, HTML) you have every reason to believe they are sufficiently well-formed: typically data exported from existing APIs or Web sources. > ⚠️ **Caution**
super-collections may not be the best > tool when source data come from a source whose quality > is unsufficiently guaranteed for your needs, or is untrusted. If you want to impose strongly formatted data structures in your code, one solution is to create [dataclasses](https://docs.python.org/3/library/dataclasses.html); especially those of [Pydantic](https://docs.pydantic.dev/latest/concepts/dataclasses/), which make implicit and explicit controls on the integrity of the source data. ## Shelves and SuperShelves ### Metaphor of a shelf Imagine a real shelf in your home, with compartments. You place items in each compartment: books, boxes, a photo frame, etc. Some compartments have labels ("History Books", "Photos"), others don’t. To refer to a compartment, you can use its position ("the third from the left"), or refer to it by its label. If it was a really big shelf, as in a library, and you were given the label of a compartment and no other information, finding it would take some time, because you would have to scan the whole shelf each time. To speed things up, you would have to keep a cardfile with cards for each label, indicating the position of the corresponding compartment in the shelf. ### Definition of a shelf (data structure) A Shelf data structure works the same way. Conceptually, a shelf is very intuitive: you can think about it as a list where you can _also_ use labels for fast retrieval. It’s a line of compartments, each holding one object. If implements **dual addressing**: You can access an item by a **key** that can be either its index (2) or its label, providing it exists ("Receipts"). In other words, a shelf works _both_ as a list and a dictionary, and has the attributes of both classes. You can write: ```python s = Shelf() s.append("x") s.append("y", label="thing") ``` or: ```python s = Shelf() s[0] = "X" s["thing"] = "Y" ``` And then: ```python assert s[0] == "X" assert s[1] == "Y" assert s["thing"] == "Y" assert list(s) == ["X", "Y"] assert list(s.values()) == ["X", "Y"] assert list(s.keys()) == [0, "thing"] ``` Internally, a shelf is a **list of cells**, with a value and an optional **label**. It is complemented by a **cardfile** that converts the labels of the cells into their index. However, you don't have to worry about it: when you add an item to the shelf, change it or delete it, both the list and the carfile are kept up-to-date. You can consult the internals: ```python print(list(s.cells())) ``` It would return a list of mutable objects: ``` [Cell(value='X', label=None), Cell(value='Y', label='thing')] ``` > ⚠️ **Caution**
Do _not_ change the cells' labels directly, > since the cardfile would not be updated. ### Iteration through a shelf **When you iterate through a shelf, you return the values as in a list.** > **⚠️ IMPORTANT NOTE:** This is distinct from a Python dictionary, where iteration returns the keys. * If you want the keys use the `.keys()` method. For items that do not have a label, it returns the index. * If you want the key, value pairs, use the `.items()` method. * The `.values()` method is provided for compatibility with a dictionary. ### SuperShelf A SuperShelf is essentially a shelf that behaves as a super-collection: - it converts recursively all the content into shelves - it exports the labels as attributes (when they are valid Python identifiers and not masked) In other words it behaves as SuperList and a SuperDict at the same time. ## Related data structures and ideas These projects contain ideas that inspired or predated super-collections. ### Standard Python * `collections.namedtuple`: tuples with dot notation ([standard python class](https://docs.python.org/3/library/collections.html#collections.namedtuple)) * `types.SimpleNamespace`: objects with arbitrary attributes ([standard python class](https://docs.python.org/3/library/types.html#types.SimpleNamespace)) * All Python classes have a __dict__ attribute, used at the foundation to implement the dot notation in the language, with the relative standard methods (`__setattr__()`, etc.) and functions (`setattr()`, etc.). * In modern Python, the `dict` class has ordered keys (by insertion order) and is subclassable. ### Dot notation on dictionaries * [addict](https://github.com/mewwts/addict) (Github) * [DotMap](https://github.com/drgrib/dotmap): subclasses and MutableMapping and OrderedDict (Github) * [SuperDict](https://github.com/itdxer/superdict): subclasses `dict` (Github) * [dotty_dict](https://github.com/pawelzny/dotty_dict): wrapper (Github) ### Using superlists to complement superdictionaries * Packages that write to and read from files, such as [shelve](https://docs.python.org/3/library/shelve.html) (standard), json, YAML, [Beautifulsoup](https://code.launchpad.net/beautifulsoup/), etc. heavily rely on a **combination of dictionaries and lists**. BeautifulSoup in particular supports dot notation. * In general, **the construction of any syntactic or semantic tree requires both dictionaries and lists**. fralau-super-collections-550a1cf/cleanup.sh000077500000000000000000000007431506662266200210470ustar00rootroot00000000000000#!/bin/bash # This may be useful to get a clean slate (for creating a distribution) # Find and delete all __pycache__ directories find . -type d -name "__pycache__" -exec rm -rf {} + # Find and delete all directories starting with __ find . -type d -name "__*" -exec rm -rf {} + # Find and delete all .egg-info directories find . -type d -name "*.egg-info" -exec rm -rf {} + # Find and delete all .egg files find . -type f -name "*.egg" -exec rm -f {} + echo "Cleanup complete!"fralau-super-collections-550a1cf/pyproject.toml000066400000000000000000000016601506662266200217740ustar00rootroot00000000000000[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "super_collections" version = "0.6.2" description = "file: README.md" authors = [{ name = "Laurent Franceschetti" }] license = { text = "MIT" } dependencies = ["hjson"] readme = "README.md" requires-python = ">=3.8" classifiers = [ "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", ] [project.urls] Source = "https://github.com/fralau/super-collections" [tool.setuptools.packages.find] where = ["src"] [project.optional-dependencies] test = ["pytest>=7.0", "rich", "pyyaml"] [tool.pytest.ini_options] testpaths = ["test"] fralau-super-collections-550a1cf/src/000077500000000000000000000000001506662266200176445ustar00rootroot00000000000000fralau-super-collections-550a1cf/src/super_collections/000077500000000000000000000000001506662266200234005ustar00rootroot00000000000000fralau-super-collections-550a1cf/src/super_collections/__init__.py000066400000000000000000000474601506662266200255240ustar00rootroot00000000000000""" Super Collections Their purpose is to turn complex input such as json or YAML files into Python objects accessible with attributes, and self documented. The general idea is that those structured files are combinations of lists and dictionaries. (C) Laurent Franceschetti 2024 """ import datetime import json import inspect from typing import Any, Union, Optional, Dict from abc import ABC, abstractmethod import hjson from .shelf import Shelf, Cell # makes import easier # ------------------------------------- # Low-level fixtures # ------------------------------------- from collections import UserDict, deque DICT_TYPES = dict, UserDict class JSONEncoder(json.JSONEncoder): """ Custom encoder for JSON serialization. Used for debugging purposes. It's purpose is to be extremely reliable. """ def default(self, obj: Any) -> Any: TIME_FORMATS = (datetime.datetime, datetime.date, datetime.time) if isinstance(obj, TIME_FORMATS): return obj.isoformat() elif isinstance(obj, UserDict): # for objects used by some packages return dict(obj) elif inspect.isfunction(obj): return f"Function: %s %s" % (inspect.signature(obj), obj.__doc__) try: return super().default(obj) except TypeError: pass # It all else fails, output as best as I can # If the object wants to speak for itself, I’ll let it. If it can’t, I’ll describe it. try: return str(obj) except Exception: pass try: return repr(obj) except Exception: # If all else fails, return the object's type return f"" def json_encode(obj) -> str: """ Encode a json string with the encoder. To be used for debugging purposes. """ return json.dumps(obj, cls=JSONEncoder) def yaml_support(): """ Support yaml format: registers YAML representers for SuperDict and SuperList. Ensures they serialize as _standard_ dicts and lists. Registers with both SafeDumper and Dumper for compatibility. Gracefully fails if PyYAML is not installed. """ try: import yaml except ImportError as e: raise ImportError( "YAML support requires PyYAML. Please install it with `pip install pyyaml`." ) from e from . import SuperDict, SuperList # local import to avoid circularity def plain_dict(dumper, data): return dumper.represent_dict(dict(data)) def plain_list(dumper, data): return dumper.represent_list(list(data)) for Dumper in (yaml.SafeDumper, yaml.Dumper): Dumper.add_representer(SuperDict, plain_dict) Dumper.add_representer(SuperList, plain_list) def is_elementary_type(t): """ Defines an elementary type in Python (str, int, etc.). An elementary type is defined as a type that is treated as a value (does not need to be broken down). Do not confuse this with an atomic type, which cannot be subdivided in Python. """ return (not hasattr(t, '__dict__') and not isinstance(t, (list, dict, tuple, set)) ) # ------------------------------------- # Collections # ------------------------------------- class SuperDict(dict): """ A dictionary with keys accessible as properties (with the dot notation) a['foo'] <=> a.foo As a rule, the Superdict will expose as properties all keys that: 1. Are valid identifiers 2. Are not a standard property or method of the dict, class notably: attributes, clear, copy, fromkeys, get, items, keys, pop, popitem, setdefault, update, values Lists in a SuperDict are converted into SuperLists, whose elements are in turn converted, etc... """ def __init__(self, *args, **kwargs): # Call the superclass's __init__ method try: super().__init__(*args, **kwargs) except TypeError: # try to interpret: obj = get_dict(args[0]) super().__init__(obj) self.__post_init__() def __post_init__(self): "Recursively transform sub-dictionary" for key, value in self.items(): if isinstance(value, SUPER_TYPES): pass elif isinstance(value, DICT_TYPES): self[key] = SuperDict(value) elif isinstance(value, list): self[key] = SuperList(value) def __getattr__(self, name:str): "Allow dot notation on reading" ERR_MSG = "Cannot find attribute '%s'" % name try: return self[name] except KeyError: raise AttributeError(ERR_MSG) def properties(self): """ Generate the valid properties (the dictionary keys that qualify as Python identifiers and are not callables) """ return (item for item in self.keys() if isinstance(item, str) and item.isidentifier() and not callable(getattr(self, item))) def __dir__(self): "List all attributes (for autocompletion, etc.)" return super().__dir__() + list(self.properties()) # ------------------------------------- # Output # ------------------------------------- def __setattr__(self, name, value): "Allow dot notation on writing" # ERR_MSG = "Cannot assign an attribute starting with _ ('%s')" % name # if name.startswith('_'): # raise AttributeError(ERR_MSG) self[name] = value def update(self, other:dict): """ Update the SuperDict with another. If necessary the other dictionary is converted into a SuperDict """ if not isinstance(other, SuperDict): other = SuperDict(other) return super().update(other) # ------------------------------------- # Output # ------------------------------------- def to_json(self): """ Convert to json. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. """ return json.dumps(self, cls=JSONEncoder) def to_hjson(self): """ Convert to hjson. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. """ python_dict = json.loads(self.to_json()) return hjson.dumps(python_dict) def __str__(self): "Print a superdict" return self.to_hjson() def __rich__(self): "Print a superdict (for rich)" r = [f"[bold red]{self.__class__.__name__}:[/]"] r.append(self.to_hjson()) return("\n".join(r)) class SuperList(list): """ A list that supports the SuperDict, to allow recursion within complex structures """ def __init__(self, *args, **kwargs): # Call the superclass's __init__ method super().__init__(*args, **kwargs) self.__post_init__() def __post_init__(self): "Recursively transform sub-list" for index, value in enumerate(self): if isinstance(value, SUPER_TYPES): pass elif isinstance(value, DICT_TYPES): self[index] = SuperDict(value) elif isinstance(value, list): self[index] = SuperList(value) # ------------------------------------- # Modify # ------------------------------------- def extend(self, l): "Extend the list with another one (transforms it first in SuperList)" l = SuperList(l) super().extend(l) def __add__(self, l): "Addition with another list" l = SuperList(l) return SuperList(super().__add__(l)) # ------------------------------------- # Output # ------------------------------------- def to_json(self): """ Convert to json. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. """ return json.dumps(self, cls=JSONEncoder) def to_hjson(self): """ Convert to hjson. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. """ python_dict = json.loads(self.to_json()) return hjson.dumps(python_dict) def __str__(self): "Print a superdict" return self.to_hjson() def __rich__(self): "Print a superdict (for rich)" r = [f"[bold red]{self.__class__.__name__}:[/]"] r.append(self.to_hjson()) return("\n".join(r)) SUPER_TYPES = SuperDict, SuperList # ------------------------------------- # Factory function # ------------------------------------- from collections.abc import Sequence LIST_TYPES = 'ndarray', 'Series' def get_list(obj:Any) -> list: """ Get list from various objects. It is the default choice. It will raise a TypeError if a dict would be probably better suited. """ if isinstance(obj, Sequence): # this includes lists proper return list(obj) elif isinstance(obj, (set, deque)): # Non-sequence standard types that also work return list(obj) elif type(obj).__name__ in LIST_TYPES: # We name check those ones return list(obj) else: raise TypeError(f"Objects of type '{type(obj).__name__}' are not lists") def get_dict(obj: Any) -> Dict[str, object]: """ Extract a dictionary from various object types using introspection only. NOTE: We do not do __dict__, because it's too general and it might take a subclass of list. """ try: return dict(obj) except TypeError: pass # 1. Custom .asdict() method if hasattr(obj, "asdict") and callable(getattr(obj, "asdict")): try: result = obj.asdict() if isinstance(result, dict): return result except Exception: pass # 2. .dict() method (e.g. Pydantic) if hasattr(obj, "dict") and callable(getattr(obj, "dict")): try: result = obj.dict() if isinstance(result, dict): return result except Exception: pass # 3. .dump() method (e.g. Marshmallow) if hasattr(obj, "dump") and callable(getattr(obj, "dump")): try: result = obj.dump() if isinstance(result, dict): return result except Exception: pass # 5. Dataclass fallback try: from dataclasses import is_dataclass, asdict if is_dataclass(obj): return asdict(obj) except ImportError: pass except Exception: pass # 6. No solution: raise an error raise TypeError(f"Cannot convert of type {obj.__class__.__name__}") # These types could be considered as lists but are not SPECIAL_ELEMENTARY_TYPES = str, bytes, bytearray def super_collect(obj:Any) -> Union[SuperDict, SuperList]: """ Factory function: Read an object and dispatch it into either a SuperDict or a SuperList """ if isinstance(obj, SPECIAL_ELEMENTARY_TYPES): raise TypeError(f"Objects of type '{type(obj).__name__}' " "are not accepted (elementary types)") try: list_obj = get_list(obj) return SuperList(list_obj) except TypeError: pass try: dict_obj = get_dict(obj) return SuperDict(dict_obj) except TypeError: raise TypeError(f"Cannot convert this object of type '{type(obj).__name__}'") # ------------------------------------- # SuperShelf # ------------------------------------- class SuperShelf(Shelf): """Shelf subclass that recursively wraps dicts and lists into SuperShelf.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for key, value in self.items(): value = self._collect(value) self[key] = value def _collect(self, obj:Any) -> "SuperShelf": """ Factory function: Read an object and make a SuperShelf """ if is_elementary_type(obj): return obj if isinstance(obj, SuperShelf): return obj elif isinstance(obj, Shelf): return SuperShelf(obj) try: list_obj = get_list(obj) return SuperShelf(list_obj) except TypeError: pass try: dict_obj = get_dict(obj) return SuperShelf(dict_obj) except TypeError: pass raise TypeError(f"Cannot collect object of type '{type(obj).__name__}'") def append(self, value: Any, label: Optional[str] = None) -> None: """Append value to shelf, recursively wrapping containers.""" if isinstance(value, dict) or isinstance(value, list): value = self._collect(value) super().append(value, label=label) @property def is_fully_collected(self) -> bool: """ Validator predicate: Recursively checks whether this SuperShelf contains only: - elementary types - other SuperShelf instances that are themselves fully collected """ for cell in self.cells(): if not isinstance(cell, (SuperShelf, Cell)): # it's one or the other raise ValueError(f"{cell}: this is not a Supershelf or Cell, but {type(cell)}") elif is_elementary_type(cell.value): pass elif isinstance(cell.value, SuperShelf): if not cell.value.is_fully_collected: return False else: return False return True # ------------------------------------- # Keys as attributes # ------------------------------------- def __getattr__(self, name:str): """ Allow dot notation on reading keys, e.g. foo.bar instead of foo['bar']. This is a CONVENIENCE method (for syntactic sugar) and it depends on: - the key being a valid Python identifier. - the attribute not being shadowed by an already existing one in the SuperShelf class. """ ERR_MSG = "Cannot find attribute '%s'" % name try: return self[name] except KeyError: print("Predefined attributes:", list(vars(self).items())) print("Valid:", self.valid_names) print("Object type:", type(self).__name__) raise AttributeError(ERR_MSG) @property def _reserved_names(self): "Return the set of names reserved by the class or instance (methods, attributes)" return set(dir(type(self))) | set(self.__dict__) @property def valid_names(self): """ Generate the valid names (the non-shadowed keys that qualify as Python identifiers) """ return [ item for item in self.keys() if isinstance(item, str) and item.isidentifier() and item not in self._reserved_names ] def find(self, key:str, value:Any): "Return the first item where getattr(item, key) == value (but skip elementary types)" for item in self: try: if getattr(item, key) == value: return item except Exception: continue raise KeyError(f"Key '{key}' not found") # ------------------------------------- # Output # ------------------------------------- @property def _is_strict_list(self) -> bool: "Implementable as list (no labels)" return all(cell.label is None for cell in self.cells()) @property def _is_strict_dict(self) -> bool: "Implementable as dictionary (all keys are labels)" return all(cell.label is not None for cell in self.cells()) def to_json(self, *, indent: int = None) -> str: """ Serialize the SuperCollection to a JSON string using the custom JSONEncoder. Each subcollection is optimized as a list, dict, or hybrid depending on addressing mode. Parameters: indent (int, optional): Number of spaces for pretty-printing. If None, output is compact. Returns: str: JSON string representing the serialized SuperCollection. """ structure = [self._serialize_shelf(shelf) for shelf in self] return json.dumps(structure, indent=indent, cls=JSONEncoder) def _serialize_shelf(self, item) -> Any: """ Serialize a single SuperShelf using the most compact representation: - As a list if all items are unlabelled - As a dict if all items are labelled - As a hybrid list of entries otherwise Parameters: shelf: A Shelf-like object supporting iteration and addressing mode predicates. Returns: A JSON-serializable structure (list or dict). """ # print(f"Item: {item} -> {type(item).__name__}") if is_elementary_type(item): return item elif item._is_strict_list: return [self._serialize_cell(cell) for cell in shelf] elif item._is_strict_dict: return {cell.label: self._serialize_cell(cell) for cell in shelf} else: return [self._serialize_cell(cell) for cell in shelf] def _serialize_cell(self, cell) -> Any: """ Serialize a single cell: - If labelled: as a singleton dict {label: value} - If unlabelled: as raw value Recursively serializes value if it supports .to_json(). Parameters: cell: A Cell-like object with .label and .value attributes. Returns: A JSON-serializable value or singleton dict. """ value = cell.value if hasattr(value, "to_json") and callable(value.to_json): value = value.to_json() return {cell.label: value} if cell.label is not None else value def to_hjson(self): """ Convert to hjson. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. """ obj = json.loads(self.to_json()) return hjson.dumps(obj) def __str__(self): "Print to hjson" return self.to_hjson() def __rich__(self): "Print (for rich)" r = [f"[bold red]{self.__class__.__name__}:[/]"] r.append(self.to_hjson()) return("\n".join(r)) # ------------------------------------- # Super Collection # ------------------------------------- class SuperCollection(ABC): """ The super collection abstract class """ @staticmethod def collect(obj) -> Union[SuperDict, SuperList]: "The factory function" return super_collect(obj) @abstractmethod def to_json(self): """ Convert to json. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. CAUTION: It must be reliable, so well tested. """ pass @abstractmethod def to_hjson(self): """ Convert to hjson. It does not have any claim of fitness for any particular purpose, except showing what's in structure, for string output. """ pass @abstractmethod def __str__(self): "Print (to hjson, in principle)" pass @abstractmethod def __rich__(self): "Print to the rich format" pass SuperCollection.register(SuperList) SuperCollection.register(SuperDict) SuperCollection.register(SuperShelf) fralau-super-collections-550a1cf/src/super_collections/shelf.py000066400000000000000000000211771506662266200250630ustar00rootroot00000000000000""" Shelf class: a labelled list """ from typing import Any, Union, Optional, Dict, List # A label is either str or int LabelType = Union[str, int] class Cell: "A cell is a mutable structure with two items: value and label" __slots__ = ('value', 'label') def __init__(self, value:Any, label:Optional[str]=None): self.value = value self.label = label def __repr__(self): return f"Cell(value={self.value!r}, label={self.label!r})" class Shelf(list): """ A shelf is a dual access stream, by index (int) and by label (str). In other words, it works both as a list and a dictionary. Iterating through a shelf returns the **values** as in a list. Internal implementation ----------------------- It is implemented as a list of mutable cells (value, key). It has an attribute _cardfile (dictionary) that maps each key to each cell. """ def __init__(self, *args: Any, **kwargs: Any) -> None: """Initialize Shelf with positional values and labeled values. - If a single dict is passed as the only arg β†’ treated as labeled input - If a single list is passed as the only arg β†’ treated as unlabeled input - Positional values (args) β†’ appended as unlabeled cells - Keyword values (kwargs) β†’ appended as labeled cells """ super().__init__() self._cardfile: Dict[str, Cell] = {} if len(args) == 1: arg = args[0] if isinstance(arg, Shelf): for cell in arg: self.append(cell.value, label=cell.label) args = () elif isinstance(arg, dict): for label, value in arg.items(): self.append(value, label=label) args = () elif isinstance(arg, list): for value in arg: self.append(value) args = () else: raise TypeError(f"Object of class '{type(arg).__name__}' " "cannot converted into a shelf.") for value in args: self.append(value) for label, value in kwargs.items(): self.append(value, label=label) @classmethod def from_list(cls, values: List[Any]) -> "Shelf": """Construct Shelf from a list of unlabeled values.""" if not isinstance(values, list): raise TypeError("from_list expects a list") shelf = cls() for value in values: shelf.append(value) return shelf @classmethod def from_dict(cls, mapping: Dict[str, Any]) -> "Shelf": """Construct Shelf from a dict of labeled values.""" if not isinstance(mapping, dict): raise TypeError("from_dict expects a dict") shelf = cls() for label, value in mapping.items(): shelf.append(value, label=label) return shelf # -------------------------------- # List-type insertion (fundamental) # -------------------------------- def __iter__(self): for cell in super().__iter__(): yield cell.value def append(self, value, label:Optional[str]=None): cell = Cell(value, label) super().append(cell) if label: if label in self._cardfile: raise ValueError(f"Duplicate label: {label}") self._cardfile[label] = cell def update(self, mapping: Dict[str, Any]) -> None: "Bulk update labeled cells" for label, value in mapping.items(): if label in self._cardfile: self._cardfile[label].value = value else: cell = Cell(value, label) self.append(cell) self._cardfile[label] = cell # -------------------------------- # Common list/dict access and update methods # -------------------------------- def __getitem__(self, key: LabelType): if isinstance(key, int): return super().__getitem__(key).value elif isinstance(key, str): return self._cardfile[key].value raise TypeError("Key must be int or str") def __setitem__(self, key: LabelType, value): if isinstance(key, int): cell = super().__getitem__(key) cell.value = value elif isinstance(key, str): if key in self._cardfile: # exists self._cardfile[key].value = value else: # doesn't exist self.append(value, key) else: raise TypeError("Key must be int or str") def __delitem__(self, key: LabelType): if isinstance(key, int): cell = super().__getitem__(key) super().__delitem__(key) if cell.label: self._cardfile.pop(cell.label, None) elif isinstance(key, str): cell = self._cardfile.pop(key) super().__delitem__(self.index(cell)) else: raise TypeError("Key must be int or str") def __contains__(self, key: LabelType) -> bool: "Check if label or index exists" if isinstance(key, str): return key in self._cardfile return 0 <= key < len(self) def pop(self, key: LabelType = None, default: Any = None) -> Any: """ Remove and return value by label, index, or last item. - If key is a str β†’ remove by label - If key is an int β†’ remove by index - If key is None β†’ remove last item - If key is missing β†’ return default """ try: if key is None: cell = super().pop() elif isinstance(key, str): cell = self._cardfile.pop(key) self.remove(cell) else: cell = super().pop(key) if cell.label is not None: self._cardfile.pop(cell.label, None) return cell.value except (KeyError, IndexError): return default # --------------------- # Dict-specific methods # --------------------- def get(self, key: LabelType, default: Any = None) -> Any: "Safe retrieval by label or index" try: return self[key] except (KeyError, IndexError): return default def keys(self): "Yield ALL keys: labels if present, else indices" return (key for key, _ in self.items()) def values(self): "Yield all cell values" return (value for value in self) def cells(self): """ Yield all cells; this is useful for auditability ⚠️ Do not modify cell.label directly. Use `self.update[label] = value` to do so. """ for cell in super().__iter__(): yield cell def items(self): "Yield (key, value) pairs: label if present, else index" for i, cell in enumerate(super().__iter__()): key = cell.label if cell.label is not None else i yield key, cell.value def rename(self, old_label:str, new_label:str): """ Rename means here "relabel a cell" (assuming the old label exists and the new label is not used yet). """ if not isinstance(old_label, str) or not isinstance(new_label, str): raise KeyError("Labels must be str or int") if new_label in self._cardfile: raise KeyError(f"Label '{new_label}' already exists") try: cell = self._cardfile[old_label] except KeyError: raise KeyError(f"Label '{old_label}' not found") cell.label = new_label del self._cardfile[old_label] self._cardfile[new_label] = cell def update(self, mapping: Dict[str, Any]) -> None: "Bulk update labeled cells" for label, value in mapping.items(): if label in self._cardfile: self._cardfile[label].value = value else: cell = Cell(value, label) self.append(cell) self._cardfile[label] = cell # --------------------- # Specific # --------------------- def get_label(self, index:int): "Get the label from the list index" return super().__getitem__(index).label def get_index(self, label:str): "Get the index from the label" return self.index(self._cardfile[label]) def labels(self): "Yield all labels from labeled cells" return (cell.label for cell in self if cell.label is not None) def __repr__(self): class_name = self.__class__.__name__ return f"{class_name}({list(self)}, cardfile={self._cardfile})"fralau-super-collections-550a1cf/test/000077500000000000000000000000001506662266200200345ustar00rootroot00000000000000fralau-super-collections-550a1cf/test/conftest.py000066400000000000000000000002461506662266200222350ustar00rootroot00000000000000import pytest from rich import print @pytest.fixture(autouse=True) def announce_test(request): print(f"[green]\n\nπŸ”§ Executing: {request.node.name}()[/green]")fralau-super-collections-550a1cf/test/solar_system.json000066400000000000000000000130351506662266200234550ustar00rootroot00000000000000{ "units": { "size": "km", "mass": "kg", "orbit": "km", "length_of_year": "Earth days" }, "planets": [ { "name": "Mercury", "size": 4880, "mass": 3.30e23, "orbit": 57900000, "orbit_from": "Sun", "length_of_year": 88, "moons": [] }, { "name": "Venus", "size": 12104, "mass": 4.87e24, "orbit": 108200000, "orbit_from": "Sun", "length_of_year": 225, "moons": [] }, { "name": "Earth", "size": 12742, "mass": 5.97e24, "orbit": 149600000, "orbit_from": "Sun", "length_of_year": 365.25, "moons": [ { "name": "Moon", "size": 3474, "mass": 7.35e22, "orbit": 384400, "orbit_from": "Earth", "length_of_year": 27.3 } ] }, { "name": "Mars", "size": 6779, "mass": 6.42e23, "orbit": 227900000, "orbit_from": "Sun", "length_of_year": 687, "moons": [ { "name": "Phobos", "size": 22.4, "mass": 1.07e16, "orbit": 9378, "orbit_from": "Mars", "length_of_year": 0.3 }, { "name": "Deimos", "size": 12.4, "mass": 1.48e15, "orbit": 23460, "orbit_from": "Mars", "length_of_year": 1.3 } ] }, { "name": "Jupiter", "size": 139820, "mass": 1.90e27, "orbit": 778500000, "orbit_from": "Sun", "length_of_year": 4333, "moons": [ { "name": "Io", "size": 3643, "mass": 8.93e22, "orbit": 421700, "orbit_from": "Jupiter", "length_of_year": 1.8 }, { "name": "Europa", "size": 3122, "mass": 4.80e22, "orbit": 671100, "orbit_from": "Jupiter", "length_of_year": 3.5 }, { "name": "Ganymede", "size": 5268, "mass": 1.48e23, "orbit": 1070400, "orbit_from": "Jupiter", "length_of_year": 7.2 }, { "name": "Callisto", "size": 4821, "mass": 1.08e23, "orbit": 1882700, "orbit_from": "Jupiter", "length_of_year": 16.7 } ] }, { "name": "Saturn", "size": 116460, "mass": 5.68e26, "orbit": 1430000000, "orbit_from": "Sun", "length_of_year": 10759, "moons": [ { "name": "Titan", "size": 5151, "mass": 1.35e23, "orbit": 1222000, "orbit_from": "Saturn", "length_of_year": 15.9 }, { "name": "Rhea", "size": 1527, "mass": 2.31e21, "orbit": 527000, "orbit_from": "Saturn", "length_of_year": 4.5 } ] }, { "name": "Uranus", "size": 50724, "mass": 8.68e25, "orbit": 2870000000, "orbit_from": "Sun", "length_of_year": 30687, "moons": [ { "name": "Titania", "size": 1578, "mass": 3.53e21, "orbit": 436300, "orbit_from": "Uranus", "length_of_year": 8.7 }, { "name": "Oberon", "size": 1523, "mass": 3.01e21, "orbit": 583500, "orbit_from": "Uranus", "length_of_year": 13.5 } ] }, { "name": "Neptune", "size": 49244, "mass": 1.02e26, "orbit": 4500000000, "orbit_from": "Sun", "length_of_year": 60190, "moons": [ { "name": "Triton", "size": 2707, "mass": 2.14e22, "orbit": 354800, "orbit_from": "Neptune", "length_of_year": 5.9 } ] } ], "planetoids": [ { "name": "Ceres", "size": 940, "mass": 9.39e20, "orbit": 413700000, "orbit_from": "Sun", "length_of_year": 1682, "moons": [] }, { "name": "Pluto", "size": 2377, "mass": 1.31e22, "orbit": 5910000000, "orbit_from": "Sun", "length_of_year": 90560, "moons": [ { "name": "Charon", "size": 1212, "mass": 1.52e21, "orbit": 19570, "orbit_from": "Pluto", "length_of_year": 6.4 } ] }, { "name": "Haumea", "size": 1632, "mass": 4.01e21, "orbit": 6450000000, "orbit_from": "Sun", "length_of_year": 103500, "moons": [ { "name": "Hi'iaka", "size": 310, "mass": 1.79e19, "orbit": 49880, "orbit_from": "Haumea", "length_of_year": 49.1 }, { "name": "Namaka", "size": 170, "mass": 1.79e18, "orbit": 25570, "orbit_from": "Haumea", "length_of_year": 18.3 } ] }, { "name": "Makemake", "size": 1434, "mass": 3.10e21, "orbit": 6850000000, "orbit_from": "Sun", "length_of_year": 112900, "moons": [] }, { "name": "Eris", "size": 2326, "mass": 1.66e22, "orbit": 10120000000, "orbit_from": "Sun", "length_of_year": 203600, "moons": [ { "name": "Dysnomia", "size": 700, "mass": 4.00e20, "orbit": 37370, "orbit_from": "Eris", "length_of_year": 15.8 } ] } ] }fralau-super-collections-550a1cf/test/test_basic.py000066400000000000000000000100011506662266200225160ustar00rootroot00000000000000import os import json from dataclasses import dataclass from typing import Dict import pytest from rich import print as rprint from super_collections import SuperDict, SuperList CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) SOLAR_SYSTEM = 'solar_system.json' def test_dict(): """ Test Superdict """ DATA = """{ "name": "Mercury", "size": 4880, "mass": 3.30e23, "orbit": 57900000, "orbit_from": "Sun", "length_of_year": 88, "moons": [], "foo bar": 50, "items": "foobar" }""" mercury = json.loads(DATA) mercury = SuperDict(mercury) # normal diction assert 'name' in mercury assert 'size' in mercury # attributes assert mercury.name == 'Mercury' assert mercury.size == 4880 assert mercury.orbit_from == "Sun" # discrovery assert 'name' in mercury.properties() assert 'name' in dir(mercury) assert 'orbit_from' in dir(mercury) # invalid identifier invalid_id = 'foo bar' assert invalid_id in mercury assert invalid_id not in dir(mercury) assert mercury[invalid_id] == 50 # but reachable as a dict key # not a valid property (is masked by the standard method `items()`) assert mercury['items'] == 'foobar' assert 'items' not in mercury.properties() assert mercury.items != 'foobar' assert callable(mercury.items) assert ('size', 4880) in mercury.items() def test_update_list(): "Update super lists" l = SuperList(({'foo': 5, 'bar': 5}, 'baz')) assert l[0].foo == 5 l2 = [6, {'barbaz': 45}] l.extend(l2) last = l[-1] assert last.barbaz == 45 l3 = ["hello", {'foobar': 30}] # add a list to a SuperList -> Superlist r1 = l + l3 assert isinstance(r1, SuperList) last = r1[-1] assert last.foobar == 30 # this is a SuperDict # CAUTION: add a SuperList to a List -> list r2 = l3 + l assert not isinstance(r2, SuperList) with pytest.raises(AttributeError): second = r2[1] second.foobar # this is NOT a SuperDict def test_fancy_types(): """ Test fancy classes """ class Person: def __init__(self, name: str, age: int, active: bool = True) -> None: self.name = name self.age = age self.active = active def __repr__(self) -> str: return f"Person(name={self.name!r}, age={self.age}, active={self.active})" def dict(self) -> Dict[str, object]: return { "name": self.name, "age": self.age, "active": self.active } p = Person("Joe", 42) rprint(SuperDict(p).to_json()) # ----------- # Dataclass # ----------- @dataclass class Book: title: str author: str year: int b = Book(title="1984", author="George Orwell", year=1949) rprint(SuperDict(b).to_json()) def test_read_json_file(): """ Read the JSON file using the function and convert it into a SuperDict """ filename = os.path.join(CURRENT_DIR, SOLAR_SYSTEM) with open(filename, 'r') as file: data = json.load(file) solar_system = SuperDict(data) # attributes are recursively available, including with lists: units = solar_system.units assert units.size == 'km' for planet in solar_system.planets: assert isinstance(planet, SuperDict) print (planet.name, planet.size) for moon in planet.moons: print(f"- {moon.name}", moon.size) ADDITIONAL = [{'name': 'Foo', 'size': 45895}, {'name': 'Bar', 'size': 59383}] new_planet_list = solar_system.planets + ADDITIONAL assert isinstance(new_planet_list, SuperList) last_planet = new_planet_list[-1] assert last_planet.name == 'Bar' assert last_planet.size == 59383 with pytest.raises(AttributeError): last_planet.orbit last_planet.orbit = 5930590 assert last_planet.orbit == 5930590 if __name__ == "__main__": pytest.main() fralau-super-collections-550a1cf/test/test_json_output.py000066400000000000000000000052301506662266200240360ustar00rootroot00000000000000""" Testing Super Collections with their json serial output. Several issues have be raised with serialization problem and we need a way to really test different cases. (C) Laurent Franceschetti 2025 """ from datetime import datetime, date, time import json from collections import UserDict from rich import print as rprint from super_collections import SuperDict, SuperList, json_encode # ------------------------- # Encoder (alone) # ------------------------- # 1. Date/time objects def test_datetime_serialization(): dt = datetime(2023, 5, 17, 15, 30) encoded = json_encode(dt) assert '"2023-05-17T15:30:00"' in encoded def test_date_serialization(): d = date(2023, 5, 17) encoded = json_encode(d) assert '"2023-05-17"' in encoded def test_time_serialization(): t = time(15, 30) encoded = json_encode(t) assert '"15:30:00"' in encoded # 2. UserDict conversion def test_userdict_serialization(): ud = UserDict({'a': 1, 'b': 2}) encoded = json_encode(ud) assert '"a": 1' in encoded and '"b": 2' in encoded # 3. Function object def test_function_serialization(): def sample_func(x): """Adds one."""; return x + 1 encoded = json_encode(sample_func) assert "Function:" in encoded assert "Adds one." in encoded # 4. Object with __str__ defined class StrOnly: def __str__(self): return "I am str" def test_str_fallback(): obj = StrOnly() encoded = json_encode(obj) assert '"I am str"' in encoded # 5. Object with neither __str__ nor __repr__ working class BrokenRepr: def __repr__(self): raise Exception("fail") def test_repr_fallback(): obj = BrokenRepr() encoded = json_encode(obj) assert "" in encoded # extreme fallback # 6. Object that uses str (which is repr) class NotSerializable: pass def test_typeerror_fallback(): obj = NotSerializable() encoded = json_encode(obj) rprint(encoded) assert ".NotSerializable object" in encoded # ------------------------- # With Super Collections # ------------------------- CITIES = 'Geneva', 'Lausanne', 'Bern', 'Zurich', 'Sankt-Gallen' MIX1 = 'Foo', 1, datetime(2025, 9, 11, 14, 59), None, {'foo': 5, 'bar': 6}, date(2025, 9, 11) MIX2 = {'Foo': 2, 'Bar': MIX1, 'Baz': CITIES, } def test_simple(): """ Test a simple super-collection """ tree = SuperList(CITIES) t = tree.to_json() rprint(t) assert '"Geneva"' in t print(tree.to_hjson()) def test_mix(): """ Test mixed super-collection """ tree = SuperList(MIX1) t = tree.to_json() rprint(t) assert '"2025-09-11' in t # startswith assert 'null' in t print(tree.to_hjson())fralau-super-collections-550a1cf/test/test_pandoc.json000066400000000000000000000231411506662266200232330ustar00rootroot00000000000000{ "blocks": [ { "t": "Header", "c": [ 1, [ "this-is-a-simple-text-in-markdown", [], [] ], [ { "t": "Str", "c": "This" }, { "t": "Space" }, { "t": "Str", "c": "is" }, { "t": "Space" }, { "t": "Str", "c": "a" }, { "t": "Space" }, { "t": "Str", "c": "simple" }, { "t": "Space" }, { "t": "Str", "c": "text" }, { "t": "Space" }, { "t": "Str", "c": "in" }, { "t": "Space" }, { "t": "Str", "c": "Markdown" } ] ] }, { "t": "Para", "c": [ { "t": "Str", "c": "This" }, { "t": "Space" }, { "t": "Str", "c": "will" }, { "t": "Space" }, { "t": "Str", "c": "work" }, { "t": "Space" }, { "t": "Str", "c": "only" }, { "t": "Space" }, { "t": "Str", "c": "if" }, { "t": "Space" }, { "t": "Str", "c": "you" }, { "t": "Space" }, { "t": "Str", "c": "have" }, { "t": "Space" }, { "t": "Link", "c": [ [ "", [], [] ], [ { "t": "Str", "c": "pandoc" } ], [ "https://pandoc.org/", "" ] ] }, { "t": "Space" }, { "t": "Str", "c": "installed." } ] }, { "t": "Header", "c": [ 2, [ "title-level-2", [], [] ], [ { "t": "Str", "c": "Title" }, { "t": "Space" }, { "t": "Str", "c": "level" }, { "t": "Space" }, { "t": "Str", "c": "2" } ] ] }, { "t": "Para", "c": [ { "t": "Str", "c": "This" }, { "t": "Space" }, { "t": "Str", "c": "is" }, { "t": "Space" }, { "t": "Str", "c": "a" }, { "t": "Space" }, { "t": "Str", "c": "fairly" }, { "t": "Space" }, { "t": "Str", "c": "heavy" }, { "t": "Space" }, { "t": "Str", "c": "package," }, { "t": "Space" }, { "t": "Str", "c": "so" }, { "t": "Space" }, { "t": "Strong", "c": [ { "t": "Str", "c": "we" }, { "t": "Space" }, { "t": "Str", "c": "are" }, { "t": "Space" }, { "t": "Str", "c": "not" }, { "t": "Space" }, { "t": "Str", "c": "installing" }, { "t": "Space" }, { "t": "Str", "c": "it" } ] }, { "t": "Str", "c": "." } ] }, { "t": "Header", "c": [ 2, [ "second-title-level-2", [], [] ], [ { "t": "Str", "c": "Second" }, { "t": "Space" }, { "t": "Str", "c": "title" }, { "t": "Space" }, { "t": "Str", "c": "level" }, { "t": "Space" }, { "t": "Str", "c": "2" } ] ] }, { "t": "Para", "c": [ { "t": "Str", "c": "You" }, { "t": "Space" }, { "t": "Str", "c": "will" }, { "t": "Space" }, { "t": "Str", "c": "see" }, { "t": "Space" }, { "t": "Str", "c": "the" }, { "t": "Space" }, { "t": "Str", "c": "conversion" }, { "t": "Space" }, { "t": "Str", "c": "to" }, { "t": "Space" }, { "t": "Str", "c": "json" }, { "t": "Space" }, { "t": "Str", "c": "(" }, { "t": "Code", "c": [ [ "", [], [] ], "pandoc test_pandoc.md -o test_pandoc.json" ] }, { "t": "Str", "c": ")" } ] } ], "pandoc-api-version": [ 1, 20 ], "meta": {} }fralau-super-collections-550a1cf/test/test_pandoc.md000066400000000000000000000006751506662266200226710ustar00rootroot00000000000000# This is a simple text in Markdown You can only render this file into json if you have [pandoc](https://pandoc.org/) installed. ## Title level 2 Pandoc is a fairly heavy package, so **we are not installing it**. ## Second title level 2 To make things easier, the file `test_pandoc.json` is a result of the conversion to json (`pandoc test_pandoc.md -o test_pandoc.json`) It is that that file which is tested wiht the `test_pandoc.py` file.fralau-super-collections-550a1cf/test/test_pandoc.py000066400000000000000000000034611506662266200227150ustar00rootroot00000000000000""" Testing Super Collections with a pandoc-generated file (https://pandoc.org) The purpose is to show a practical application. It could be done directly on lists and dictionary, but it would be awkward. The code is much more legible and maintainable. (C) Laurent Franceschetti 2024 """ import os import json import pytest from super_collections import SuperDict, SuperList # ------------------------------------- # Constants # ------------------------------------- CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) PANDOC_FILE = 'test_pandoc.json' # ------------------------------------- # Code # ------------------------------------- def strip_format(sl: SuperList) -> str: "Print pandoc text from a super list" if not isinstance(sl, SuperList): raise ValueError(f"{sl} not a list") r = [] for element in sl: if isinstance(element, (str, int)): pass elif isinstance(element, list): r.append(strip_format(element)) elif element.t == 'Str': r.append(element.c) elif element.t == 'Space': r.append(' ') elif isinstance(element.c, list): r.append(strip_format(element.c)) return ''.join(r) def test_read_json_file(): """ Read the JSON file using the function and convert it into a SuperDict """ filename = os.path.join(CURRENT_DIR, PANDOC_FILE) with open(filename, 'r') as file: data = json.load(file) document = SuperDict(data) print('\n----') lines = [] for block in document.blocks: # change the header level if block.t == 'Header': block.c[0] += 1 lines.append(strip_format(block.c)) result = '\n'.join(lines) print(result) assert "fairly heavy" in result assert "Title level 2" in result fralau-super-collections-550a1cf/test/test_shelf.py000066400000000000000000000145651506662266200225610ustar00rootroot00000000000000""" Test the Shelf structure """ import pytest from super_collections.shelf import Shelf, Cell # --------------------- # Simple cases # --------------------- def test_append_and_access_by_index(): """Test that values can be appended and accessed by index and label.""" s = Shelf() s.append("apple") s.append("banana", label="fruit") assert s[0] == "apple" assert s[1] == "banana" assert s["fruit"] == "banana" def test_duplicate_label_raises(): """Test that appending a duplicate label raises ValueError.""" s = Shelf() s.append("carrot", label="veg") with pytest.raises(ValueError): s.append("cucumber", label="veg") def test_get_label_and_index(): """Test retrieval of label from index and index from label.""" s = Shelf() s.append("stone") s.append("leaf", label="plant") assert s.get_label(1) == "plant" assert s.get_index("plant") == 1 def test_setitem_by_index_and_label(): """Test value assignment by index and label.""" s = Shelf() s.append("x") s.append("y", label="thing") s[0] = "X" s["thing"] = "Y" assert s[0] == "X" assert s[1] == "Y" assert s["thing"] == "Y" assert list(s) == ["X", "Y"] assert list(s.values()) == ["X", "Y"] assert list(s.keys()) == [0, "thing"] print(list(s.cells())) def test_set_new(): """Test value assignment (new) by label.""" s = Shelf() s["thing"] = "Y" assert s["thing"] == "Y" print("Set:", s) s["foo"] = "Z" s["foo"] = "A" assert len(s) == 2 assert s["foo"] == 'A' def test_delitem_by_index_and_label(): """Test deletion of cells by index and label, and cardfile cleanup.""" s = Shelf() s.append("a") s.append("b", label="bee") del s[0] assert len(s) == 1 assert "bee" in s del s["bee"] assert len(s) == 0 assert "bee" not in s def test_pop_variants(): """Test pop behavior with no key, index, label, and fallback default.""" s = Shelf() s.append("one") s.append("two", label="second") assert s.pop() == "two" assert s.pop(0) == "one" s.append("three", label="third") assert s.pop("third") == "three" assert s.pop("missing", default="fallback") == "fallback" def test_get_with_default(): """Test safe retrieval with fallback default.""" s = Shelf() s.append("x", label="ex") assert s.get("ex") == "x" assert s.get("why", default="Z") == "Z" def test_keys_values_items(): """Test dict-like access to keys, values, and items.""" s = Shelf() s.append("a") s.append("b", label="bee") s.append("c", label="sea") assert set(s.keys()) == {0, "bee", "sea"} assert list(s.values()) == ["a", "b", "c"] assert dict(s.items()) == {0: "a", "bee": "b", "sea": "c"} def test_rename_label(): """Test relabeling of a cell and cardfile update.""" s = Shelf() s.append("x", label="old") s.rename("old", "new") assert "new" in s assert "old" not in s assert s["new"] == "x" def test_update_existing_and_new(): """Test bulk update of existing and new labeled cells.""" s = Shelf() s.append("a", label="alpha") s.update({"alpha": "A", "beta": "B"}) assert s["alpha"] == "A" assert s["beta"] == "B" def test_contains_behavior(): """Test membership checks for index and label.""" s = Shelf() s.append("x") s.append("y", label="yes") assert 0 in s assert "yes" in s assert "no" not in s assert 2 not in s # --------------------- # Constructors # --------------------- def test_init_with_list(): """Test Shelf initialized with a single list of values.""" s = Shelf(["a", "b", "c"]) assert len(s) == 3 assert s[0] == "a" assert s[1] == "b" assert s[2] == "c" assert list(s.keys()) == [0, 1, 2] def test_init_with_dict(): """Test Shelf initialized with a single dict of labeled values.""" s = Shelf({"x": 1, "y": 2}) assert len(s) == 2 assert s["x"] == 1 assert s["y"] == 2 assert set(s.keys()) == {"x", "y"} def test_init_with_args_and_kwargs(): """Test Shelf initialized with mixed unlabeled and labeled values.""" s = Shelf("a", "b", x="c", y="d") assert len(s) == 4 assert s[0] == "a" assert s[1] == "b" assert s["x"] == "c" assert s["y"] == "d" assert list(s) == ['a', 'b', 'c', 'd'] assert list(s.keys()) == [0, 1, "x", "y"] def test_init_with_list_and_args(): """Test Shelf initialized with a list and additional positional values.""" s = Shelf(["a", "b"], "c") assert len(s) == 2 assert s[0] == ["a", "b"] assert s[1] == "c" assert list(s.keys()) == [0, 1] def test_init_with_dict_and_kwargs(): """Test Shelf initialized with a dict and additional labeled values.""" s = Shelf({"x": 1}, y=2) assert len(s) == 2 assert s["x"] == 1 assert s["y"] == 2 assert list(s) == [1, 2] assert list(s.keys()) == ["x", "y"] def test_from_list_constructor(): """Test Shelf.from_list creates unlabeled cells from a list.""" s = Shelf.from_list(["apple", "banana"]) assert len(s) == 2 assert s[0] == "apple" assert s[1] == "banana" assert list(s.keys()) == [0, 1] def test_from_dict_constructor(): """Test Shelf.from_dict creates labeled cells from a dict.""" s = Shelf.from_dict({"fruit": "apple", "veg": "carrot"}) assert len(s) == 2 assert s["fruit"] == "apple" assert s["veg"] == "carrot" assert list(s) == ['apple', 'carrot'] assert list(s.keys()) == ["fruit", "veg"] def test_init_with_invalid_type(): """Test Shelf initialization with unsupported type raises TypeError.""" with pytest.raises(TypeError): Shelf(42) def test_init_with_duplicate_labels(): """Test Shelf initialization with duplicate labels raises ValueError.""" with pytest.raises(ValueError): Shelf({"x": 1}, x=2) def test_from_dict_with_duplicate_labels(): """Test Shelf.from_dict with duplicate keys is allowed (dict keys are unique).""" s = Shelf.from_dict({"x": 1, "y": 2}) assert s["x"] == 1 assert s["y"] == 2 def test_from_list_with_non_list_raises(): """Test Shelf.from_list with non-list input raises TypeError.""" with pytest.raises(TypeError): Shelf.from_list("not a list") def test_from_dict_with_non_dict_raises(): """Test Shelf.from_dict with non-dict input raises TypeError.""" with pytest.raises(TypeError): Shelf.from_dict(["not", "a", "dict"]) fralau-super-collections-550a1cf/test/test_super_collect.py000066400000000000000000000063741506662266200243220ustar00rootroot00000000000000import os from collections import deque, UserList from collections.abc import Sequence import json from rich import print import pytest from super_collections import SuperList, SuperDict, SuperCollection, super_collect CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) LIST_TYPES = ['CustomListLike'] # Example duck-typed name class CustomListLike: "A custom list, vanilla" def __init__(self): self._data = [1, 2, 3] def __getitem__(self, index): return self._data[index] def __len__(self): return len(self._data) Sequence.register(CustomListLike) class CustomListLike2: "A custom list-like object, of Sequence type" def __init__(self): pass def __getitem__(self, index): if index >= len(self): raise IndexError("Too high") value = index * 2 print("Index, Value:", index, value) return value def __len__(self): return 5 Sequence.register(CustomListLike2) def assert_type(obj, class_): "Syntactic sugar for super_collection test" print(f"Testing '{type(obj).__name__}' as {class_}") coll_obj = super_collect(obj) assert coll_obj is not None, f"Object {type(obj)} translates to None?" assert isinstance(coll_obj, class_) assert isinstance(coll_obj, SuperCollection) # --------------------- # Tests # --------------------- def test_super_collect_list(): obj = super_collect([1, 2]) print("Obj:", type(obj)) print("Obj:", type(obj).__name__) assert isinstance(obj, SuperList) def test_super_collect_tuple(): assert_type((1, 2), SuperList) def test_super_collect_deque(): assert_type(deque([1, 2]), SuperList) def test_super_collect_userlist(): assert_type(UserList([1, 2]), SuperList) def test_super_collect_duck_type(): assert_type(CustomListLike(), SuperList) # Registered as sequence: assert_type(CustomListLike2(), SuperList) def test_super_collect_set(): assert_type({1, 2}, SuperList) def test_super_collect_dict(): obj = {'a': 1} assert_type(obj, SuperDict) @pytest.mark.parametrize("bad_type", ["string", b"bytes", bytearray(b"abc")]) def test_super_collect_invalid_types(bad_type): with pytest.raises(TypeError): print(f"Checking that {bad_type} <{type(bad_type).__name__}> is NOT accepted.") super_collect(bad_type) def test_super_collection_class(): obj1 = SuperList([1, 3, 5]) assert isinstance(obj1, SuperCollection) obj2 = SuperDict({'a': 5, 'b':7}) assert isinstance(obj2, SuperCollection) obj = SuperCollection.collect([obj1, obj2]) print("My SuperCollection object:", obj) assert isinstance(obj, SuperCollection) def test_read_json(): "Test collecting a JSON file into a SuperCollection" # This file is actually a dictionary: FILENAME = os.path.join(CURRENT_DIR, 'solar_system.json') # Open the file and load its contents with open(FILENAME, 'r', encoding='utf-8') as f: data = json.load(f) # Now `data` is a Python structure β€” usually a dict or list content = SuperCollection.collect(data) print(str(content)[:200]) print("(...)") # Make sure it is reflexive content2 = SuperCollection.collect(content) assert content == content2 assert dict(content) == dict(content2) fralau-super-collections-550a1cf/test/test_super_shelf.py000066400000000000000000000140431506662266200237660ustar00rootroot00000000000000import json import pytest from pathlib import Path from super_collections import SuperCollection, SuperShelf, Cell @pytest.fixture def solar_system(): path = Path(__file__).parent / "solar_system.json" with path.open("r", encoding="utf-8") as f: return SuperShelf(json.load(f)) def test_units_are_scalar(solar_system): units = solar_system["units"] assert isinstance(units, SuperShelf) for key in ["size", "mass", "orbit", "length_of_year"]: assert isinstance(units[key], str) def test_planet_count(solar_system): planets = solar_system.planets assert isinstance(planets, SuperShelf) assert len(planets) == 8 def test_moon_access(solar_system): earth = next(p for p in solar_system.planets if p.name == "Earth") moons = earth.moons assert isinstance(moons, SuperShelf) assert moons[0]["name"] == "Moon" def test_moon_access2(solar_system): earth = solar_system.planets.find('name', 'Earth') moons = earth.moons assert isinstance(moons, SuperShelf) assert moons[0].name == "Moon" assert moons.find('name', 'Moon').orbit_from == earth.name == 'Earth' def test_nested_structure(solar_system): jupiter = solar_system.planets.find('name', 'Jupiter') ganymede = jupiter.moons.find('name', 'Ganymede') assert ganymede.orbit_from == "Jupiter" assert ganymede.mass > 1e23 def test_planetoid_presence(solar_system): names = [p["name"] for p in solar_system["planetoids"]] assert "Pluto" in names assert "Eris" in names assert "Ceres" in names def test_recursive_type_integrity(solar_system): def assert_supershelf_structure(obj): if isinstance(obj, SuperShelf): for el in obj: assert_supershelf_structure(el) elif isinstance(obj, Cell): pass else: assert not isinstance(obj, (list, dict)), f"Found raw {type(obj).__name__} outside SuperShelf" assert_supershelf_structure(solar_system) # ------------------------------------- # Property resolution # ------------------------------------- def assert_property_resolution(obj, name): cls = type(obj) # 1. Assert property is defined on the class assert name in cls.__dict__, f"{name} not found in class __dict__" # 2. Assert it's a property descriptor assert isinstance(cls.__dict__[name], property), f"{name} is not a property" # 3. Assert it resolves without falling into __getattr__ try: value = getattr(obj, name) except AttributeError: raise AssertionError(f"{name} triggered __getattr__; descriptor resolution failed") # 4. Optional: assert value type assert isinstance(value, bool), f"{name} did not return a bool (got {type(value)})" def resolve_attr(obj, name: str): cls = type(obj) print(f"πŸ” Resolving attribute: {name}") # Step 1: Class-level descriptor if name in cls.__dict__: attr = cls.__dict__[name] print(f"βœ… Found in class __dict__: {attr!r}") if isinstance(attr, property): print("πŸ”§ It's a property descriptor.") try: val = attr.__get__(obj, cls) print(f"πŸ“¦ Descriptor __get__ returned: {val!r}") return val except Exception as e: print(f"⚠️ Descriptor __get__ raised: {e}") return None else: print("⚠️ Found in class but not a property.") return attr # Step 2: Instance-level attribute if name in obj.__dict__: print(f"βœ… Found in instance __dict__: {obj.__dict__[name]!r}") return obj.__dict__[name] # Step 3: Internal key mapping if hasattr(obj, 'keys') and name in obj.keys(): print(f"πŸ” Found in internal keys: {obj[name]!r}") return obj[name] # Step 4: Fallback to getattr try: val = getattr(obj, name) print(f"🧩 getattr fallback returned: {val!r}") return val except AttributeError as e: print(f"❌ getattr fallback failed: {e}") return None def test_properties(): shelf = SuperShelf('a', 'b') assert isinstance(shelf, SuperCollection) "Check properties" assert '_is_strict_list' in SuperShelf.__dict__, "Property not found in SuperShelf.__dict__" assert isinstance(SuperShelf.__dict__['_is_strict_list'], property), "Not a property" resolve_attr(shelf, '_is_strict_list') resolve_attr(shelf, '_is_strict_dict') assert_property_resolution(shelf, '_is_strict_list') assert_property_resolution(shelf, '_is_strict_dict') def test_predicates_list(): "Test the predicates of a SuperShelf" shelf_list = SuperShelf(['a', 'b']) assert isinstance(shelf_list, SuperCollection) assert shelf_list.is_fully_collected, f"{shelf_list.to_json()} is NOT fully collected" print("Shelf list:", shelf_list) assert '_is_strict_dict' in type(shelf_list).__dict__, "_is_strict_dict not in class dict" print("Type of attribute:", type(getattr(type(shelf_list), '_is_strict_dict', None))) assert isinstance(shelf_list, SuperShelf), "not a SuperShelf, can you believe that?" assert shelf_list._is_strict_list, "Not a list" def test_predicates_dict(): "Test the predicates of a SuperShelf" shelf_dict = SuperShelf({'x': 1, 'y': 2}) assert shelf_dict._is_strict_dict, "Not a dict" def no_test_supershelf_to_json(): "Test conversion of SuperShelf to JSON" # Strict list shelf: no labels shelf_list = SuperShelf(['a', 'b']) assert shelf_list.is_fully_collected # Strict dict shelf: all labelled shelf_dict = SuperShelf({'x': 1, 'y': 2}) # Hybrid shelf: mixed labels and unlabelled values shelf_hybrid = SuperShelf([ {'earth': 3}, 4, {'mars': 5} ]) supershelf = SuperShelf(shelf_list, shelf_dict, shelf_hybrid) assert shelf_list.is_fully_collected json_string = supershelf.to_json(indent=2) print("JSON:", json_string) parsed = json.loads(json_string) assert parsed == [ ["a", "b"], {"x": 1, "y": 2}, [{"earth": 3}, 4, {"mars": 5}] ] fralau-super-collections-550a1cf/test/test_yaml_output.py000066400000000000000000000016231506662266200240310ustar00rootroot00000000000000""" Testing Super Collections with YAML. """ import yaml from datetime import datetime, date from super_collections import SuperDict, SuperList, yaml_support CITIES = 'Geneva', 'Lausanne', 'Bern', 'Zurich', 'Sankt-Gallen' MIX1 = 'Foo', 1, datetime(2025, 9, 11, 14, 59), None, {'foo': 5, 'bar': 6}, date(2025, 9, 11) MIX2 = {'Foo': 2, 'Bar': MIX1, 'Baz': CITIES, } yaml_support() def test_yaml_support_success(): d = SuperDict({"x": 1}) l = SuperList(["a", "b"]) dumped_dict = yaml.dump(d) dumped_list = yaml.dump(l) assert "x: 1" in dumped_dict assert "- a" in dumped_list and "- b" in dumped_list d = SuperDict(MIX2) dumped_dict = yaml.dump(d) print(dumped_dict) def test_yaml_support_safe_dump(): yaml_support() d = SuperDict({"x": 1}) dumped = yaml.safe_dump(d) assert "x: 1" in dumped assert "!SuperDict" not in dumped # no tagging expected fralau-super-collections-550a1cf/update_pypi.sh000077500000000000000000000024411506662266200217400ustar00rootroot00000000000000# ------------------------------------------------------------- # update the package on pypi # 2024-10-12 # # Tip: if you don't want to retype pypi's username every time # define it as an environment variable (TWINE_USERNAME) # # ------------------------------------------------------------- function warn { GREEN='\033[0;32m' NORMAL='\033[0m' echo -e "${GREEN}$1${NORMAL}" } function get_value { # get the value from the config file toml get --toml-path pyproject.toml $1 } # Clean the subdirs, for safety and to guarantee integrity ./cleanup.sh # Check for changes in the files compared to the repository if ! git diff --quiet; then warn "Won't do it: there are changes in the repository. Please commit first!" exit 1 fi # get the project inform package_name=$(get_value project.name) package_version=v$(get_value project.version) # add a 'v' in front (git convention) # update Pypi warn "Rebuilding $package_name..." rm -rf build dist *.egg-info # necessary to guarantee integrity PYTHON=$(which python3.11) $PYTHON -m build if twine upload dist/*; then git push # just in case warn "... create tag $package_version, and push to remote git repo..." git tag $package_version git push --tags warn "Done ($package_version)!" else warn "Failed ($package_version)!" exit 1 fi