pax_global_header00006660000000000000000000000064147024023370014514gustar00rootroot0000000000000052 comment=8f5753e224abc2962bfa8c0e639a8e976dc15164 fralau-super-collections-8f5753e/000077500000000000000000000000001470240233700170065ustar00rootroot00000000000000fralau-super-collections-8f5753e/.gitignore000066400000000000000000000006751470240233700210060ustar00rootroot00000000000000# 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-8f5753e/LICENSE000066400000000000000000000020651470240233700200160ustar00rootroot00000000000000MIT 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-8f5753e/README.md000066400000000000000000000214621470240233700202720ustar00rootroot00000000000000
# Python Super Collections **Dictionaries 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) - [How it works](#how-it-works) - [Superdicts](#superdicts) - [Superlists](#superlists) - [Why Combining SuperDicts with SuperLists?](#why-combining-superdicts-with-superlists) - [Install](#install) - [From the repository](#from-the-repository) - [Usage](#usage) - [Remarks](#remarks) - [Restrictions](#restrictions) - [Does it work?](#does-it-work) - [When are superdictionaries and superlists _not_ recommended?](#when-are-superdictionaries-and-superlists-not-recommended) - [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) ## How it works 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 (**SuperDict** a **SuperList**) comes handy. ### 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. ### 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. ## 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. ## 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. ## 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-8f5753e/pyproject.toml000066400000000000000000000015251470240233700217250ustar00rootroot00000000000000[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "super_collections" version = "0.5.3" description = "file: README.md" authors = [ { name = "Laurent Franceschetti"} ] license = { file = "LICENSE" } 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" ] [project.urls] Source = "https://github.com/fralau/super-collections" [tool.setuptools.packages.find] where = ["src"] [project.optional-dependencies] test = [ "pytest>=7.0" ] [tool.pytest.ini_options] testpaths = ["test"]fralau-super-collections-8f5753e/src/000077500000000000000000000000001470240233700175755ustar00rootroot00000000000000fralau-super-collections-8f5753e/src/super_collections/000077500000000000000000000000001470240233700233315ustar00rootroot00000000000000fralau-super-collections-8f5753e/src/super_collections/__init__.py000066400000000000000000000154241470240233700254500ustar00rootroot00000000000000""" 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 import hjson # ------------------------------------- # Low-level fixtures # ------------------------------------- from collections import UserDict DICT_TYPES = dict, UserDict class CustomEncoder(json.JSONEncoder): """ Custom encoder for JSON serialization. Used for debugging purposes. """ def default(self, obj: Any) -> Any: if isinstance(obj, datetime): return obj.isoformat() if 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: print(f"CANNOT INTERPRET {obj.__class__}") return str(obj) # ------------------------------------- # Collections # ------------------------------------- class SuperDict(dict): """ A dictionary with key accessibles 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 super().__init__(*args, **kwargs) 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 # if name.startswith('_'): # raise AttributeError(ERR_MSG) 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=CustomEncoder) 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=CustomEncoder) 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, SuperListfralau-super-collections-8f5753e/test/000077500000000000000000000000001470240233700177655ustar00rootroot00000000000000fralau-super-collections-8f5753e/test/solar_system.json000066400000000000000000000140601470240233700234050ustar00rootroot00000000000000{ "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-8f5753e/test/test_basic.py000066400000000000000000000061041470240233700224600ustar00rootroot00000000000000import os import json import pytest 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_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-8f5753e/test/test_pandoc.json000066400000000000000000000231411470240233700231640ustar00rootroot00000000000000{ "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-8f5753e/test/test_pandoc.md000066400000000000000000000006751470240233700226220ustar00rootroot00000000000000# 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-8f5753e/test/test_pandoc.py000066400000000000000000000034611470240233700226460ustar00rootroot00000000000000""" 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-8f5753e/update_pypi.sh000077500000000000000000000024411470240233700216710ustar00rootroot00000000000000# ------------------------------------------------------------- # 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 python3 -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