ordered-rectangles


Nameordered-rectangles JSON
Version 0.0.6 PyPI version JSON
download
home_pagehttps://github.com/PasaOpasen/rectangles-text-view-util
SummaryEnvironment to python dictionary parser util
upload_time2023-11-28 12:19:50
maintainerDemetry Pascal
docs_urlNone
authorDemetry Pascal
requires_python
licenseMIT
keywords rectangles
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://badge.fury.io/py/ordered-rectangles.svg)](https://pypi.org/project/ordered-rectangles/)
[![Downloads](https://pepy.tech/badge/ordered-rectangles)](https://pepy.tech/project/ordered-rectangles)
[![Downloads](https://pepy.tech/badge/ordered-rectangles/month)](https://pepy.tech/project/ordered-rectangles)
[![Downloads](https://pepy.tech/badge/ordered-rectangles/week)](https://pepy.tech/project/ordered-rectangles)

# ordered_rectangles

```
pip install ordered-rectangles

pip install ordered-rectangles[extra]
```

This package is an utility purposed to:
* view float rectangles relations (relative positions and the order) in the text format
* change rectangles order by changing rectangles labels in the text format view file

Usual imports:
```python
from ordered_rectangles import OrderedRectangles, RectTextViewer, read_text, read_json, write_text, write_json
```

Usage example:
```python
class OrderedRectangles:
    """
    stores `(x1, y1, x2, y2)` numpy-ordered rectangles and provides an ability to
        1. show its text map which reflects the rectangles order and positions with different disretization level
        2. save/load the rectangles with/without the map to json file
        3. change the map manually to change the rectangles order easily
```

```python
# How to create an object:
>>> d = OrderedRectangles([(0.1, 0.2, 0.3, 0.4), (0.1, 0.6, 0.2, 1.1)])

# How to view the map with `units` discretization level:
>>> units = 15
>>> mp = d.get_order_map(units=units); print(mp) # doctest: +NORMALIZE_WHITESPACE
    1#### 2#######
    #   # #      #
    #   # ########
    #####

# Use `show_order_map` method to simplify this step:
>>> d.show_order_map(units=units, show_order=False) # doctest: +NORMALIZE_WHITESPACE
    ##### ########
    #   # #      #
    #   # ########
    #####

# Let's try bigger example:
>>> d = OrderedRectangles(
...     [
...         (0.1, 0.2, 0.3, 0.4), (0.1, 0.6, 0.2, 1.1),
...         (0.1, 1.2, 0.3, 1.4), (0.1, 1.5, 0.25, 2.3),
...         (0.35, 0.2, 0.6, 0.5), (0.4, 0.6, 0.6, 1.4)
...     ]
... )

# `units` <= 0 means to search the best value automatically
>>> d.show_order_map(units=0)  # doctest: +NORMALIZE_WHITESPACE
    1########      2###################  3######## 4##############################
    #       #      #                  #  #       # #                             #
    #       #      #                  #  #       # #                             #
    #       #      #                  #  #       # #                             #
    #       #      ####################  #       # #                             #
    #       #                            #       # #                             #
    #       #                            #       # ###############################
    #       #                            #       #
    #########                            #########
    5############
    #           #  6##############################
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #############  ###############################

# I can swap 5th and 6th rectangles programmatically:
>>> d[5], d[6] = d[6], d[5]
>>> d.show_order_map(units=0)  # doctest: +NORMALIZE_WHITESPACE
    1########      2###################  3######## 4##############################
    #       #      #                  #  #       # #                             #
    #       #      #                  #  #       # #                             #
    #       #      #                  #  #       # #                             #
    #       #      ####################  #       # #                             #
    #       #                            #       # #                             #
    #       #                            #       # ###############################
    #       #                            #       #
    #########                            #########
    6############
    #           #  5##############################
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #           #  #                             #
    #############  ###############################

# The key feature is that I can change this order by changing the string map:
>>> d3 = d[3]; d4 = d[4]
>>> s = d.get_order_map(units=0).translate({ord('4'): '3', ord('3'): '4'})
>>> d.load_order_map(s)
>>> assert d[3] == d4 and d[4] == d3

# However it's not efficiet to change the rectangles order by changing the string map programmatically.
# Instead, it is supposed that u will save the map to some text file, change numbers manually and load the map from
#     the file. `load_order_map` methods supports file path as an input too.

# Finally there is a way to save and load the rectangles as JSON:
>>> tempdir = 'tmp'
>>> jsdir = os.path.join(tempdir, 'separate-jsons'); mkdir(jsdir)
>>> mapdir = os.path.join(tempdir, 'separate-maps'); mkdir(mapdir)
>>> js_file = os.path.join(jsdir, 'rects.json')
>>> map_file = os.path.join(mapdir, 'rects.txt')
>>> d.to_json(path=js_file, save_map=map_file, units=0)
>>> dct = read_json(js_file); print(str(dct).replace(os.sep, '/').replace('//', '/'))
{'rects': [[0.1, 0.2, 0.3, 0.4], [0.1, 0.6, 0.2, 1.1], [0.1, 1.5, 0.25, 2.3], [0.1, 1.2, 0.3, 1.4], [0.4, 0.6, 0.6, 1.4], [0.35, 0.2, 0.6, 0.5]], 'map': '../separate-maps/rects.txt'}

# As u can see, tha path to maps file is saved as absolute or relative to the json file.
# U can change the order in the map manually and reload rectangles

>>> write_text(map_file, read_text(map_file).translate({ord('1'): '2', ord('2'): '1'}))
>>> d2 = OrderedRectangles.from_json(js_file)
>>> assert d[1] == d2[2] and d[2] == d2[1]
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PasaOpasen/rectangles-text-view-util",
    "name": "ordered-rectangles",
    "maintainer": "Demetry Pascal",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "rectangles",
    "author": "Demetry Pascal",
    "author_email": "qtckpuhdsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/61/af/ddd6a03efa763c982025488e7f3c6380c512adae04d79bfab941930adcb2/ordered_rectangles-0.0.6.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://badge.fury.io/py/ordered-rectangles.svg)](https://pypi.org/project/ordered-rectangles/)\n[![Downloads](https://pepy.tech/badge/ordered-rectangles)](https://pepy.tech/project/ordered-rectangles)\n[![Downloads](https://pepy.tech/badge/ordered-rectangles/month)](https://pepy.tech/project/ordered-rectangles)\n[![Downloads](https://pepy.tech/badge/ordered-rectangles/week)](https://pepy.tech/project/ordered-rectangles)\n\n# ordered_rectangles\n\n```\npip install ordered-rectangles\n\npip install ordered-rectangles[extra]\n```\n\nThis package is an utility purposed to:\n* view float rectangles relations (relative positions and the order) in the text format\n* change rectangles order by changing rectangles labels in the text format view file\n\nUsual imports:\n```python\nfrom ordered_rectangles import OrderedRectangles, RectTextViewer, read_text, read_json, write_text, write_json\n```\n\nUsage example:\n```python\nclass OrderedRectangles:\n    \"\"\"\n    stores `(x1, y1, x2, y2)` numpy-ordered rectangles and provides an ability to\n        1. show its text map which reflects the rectangles order and positions with different disretization level\n        2. save/load the rectangles with/without the map to json file\n        3. change the map manually to change the rectangles order easily\n```\n\n```python\n# How to create an object:\n>>> d = OrderedRectangles([(0.1, 0.2, 0.3, 0.4), (0.1, 0.6, 0.2, 1.1)])\n\n# How to view the map with `units` discretization level:\n>>> units = 15\n>>> mp = d.get_order_map(units=units); print(mp) # doctest: +NORMALIZE_WHITESPACE\n    1#### 2#######\n    #   # #      #\n    #   # ########\n    #####\n\n# Use `show_order_map` method to simplify this step:\n>>> d.show_order_map(units=units, show_order=False) # doctest: +NORMALIZE_WHITESPACE\n    ##### ########\n    #   # #      #\n    #   # ########\n    #####\n\n# Let's try bigger example:\n>>> d = OrderedRectangles(\n...     [\n...         (0.1, 0.2, 0.3, 0.4), (0.1, 0.6, 0.2, 1.1),\n...         (0.1, 1.2, 0.3, 1.4), (0.1, 1.5, 0.25, 2.3),\n...         (0.35, 0.2, 0.6, 0.5), (0.4, 0.6, 0.6, 1.4)\n...     ]\n... )\n\n# `units` <= 0 means to search the best value automatically\n>>> d.show_order_map(units=0)  # doctest: +NORMALIZE_WHITESPACE\n    1########      2###################  3######## 4##############################\n    #       #      #                  #  #       # #                             #\n    #       #      #                  #  #       # #                             #\n    #       #      #                  #  #       # #                             #\n    #       #      ####################  #       # #                             #\n    #       #                            #       # #                             #\n    #       #                            #       # ###############################\n    #       #                            #       #\n    #########                            #########\n    5############\n    #           #  6##############################\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #############  ###############################\n\n# I can swap 5th and 6th rectangles programmatically:\n>>> d[5], d[6] = d[6], d[5]\n>>> d.show_order_map(units=0)  # doctest: +NORMALIZE_WHITESPACE\n    1########      2###################  3######## 4##############################\n    #       #      #                  #  #       # #                             #\n    #       #      #                  #  #       # #                             #\n    #       #      #                  #  #       # #                             #\n    #       #      ####################  #       # #                             #\n    #       #                            #       # #                             #\n    #       #                            #       # ###############################\n    #       #                            #       #\n    #########                            #########\n    6############\n    #           #  5##############################\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #           #  #                             #\n    #############  ###############################\n\n# The key feature is that I can change this order by changing the string map:\n>>> d3 = d[3]; d4 = d[4]\n>>> s = d.get_order_map(units=0).translate({ord('4'): '3', ord('3'): '4'})\n>>> d.load_order_map(s)\n>>> assert d[3] == d4 and d[4] == d3\n\n# However it's not efficiet to change the rectangles order by changing the string map programmatically.\n# Instead, it is supposed that u will save the map to some text file, change numbers manually and load the map from\n#     the file. `load_order_map` methods supports file path as an input too.\n\n# Finally there is a way to save and load the rectangles as JSON:\n>>> tempdir = 'tmp'\n>>> jsdir = os.path.join(tempdir, 'separate-jsons'); mkdir(jsdir)\n>>> mapdir = os.path.join(tempdir, 'separate-maps'); mkdir(mapdir)\n>>> js_file = os.path.join(jsdir, 'rects.json')\n>>> map_file = os.path.join(mapdir, 'rects.txt')\n>>> d.to_json(path=js_file, save_map=map_file, units=0)\n>>> dct = read_json(js_file); print(str(dct).replace(os.sep, '/').replace('//', '/'))\n{'rects': [[0.1, 0.2, 0.3, 0.4], [0.1, 0.6, 0.2, 1.1], [0.1, 1.5, 0.25, 2.3], [0.1, 1.2, 0.3, 1.4], [0.4, 0.6, 0.6, 1.4], [0.35, 0.2, 0.6, 0.5]], 'map': '../separate-maps/rects.txt'}\n\n# As u can see, tha path to maps file is saved as absolute or relative to the json file.\n# U can change the order in the map manually and reload rectangles\n\n>>> write_text(map_file, read_text(map_file).translate({ord('1'): '2', ord('2'): '1'}))\n>>> d2 = OrderedRectangles.from_json(js_file)\n>>> assert d[1] == d2[2] and d[2] == d2[1]\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Environment to python dictionary parser util",
    "version": "0.0.6",
    "project_urls": {
        "Homepage": "https://github.com/PasaOpasen/rectangles-text-view-util"
    },
    "split_keywords": [
        "rectangles"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c8776fb7e1f006e5263b919596aa923c4e442e5b7614cbcd59f58790d90609e1",
                "md5": "b6f9ed5feb969c71d85c18337162bb00",
                "sha256": "b2af605864cef6c363dedbde4a597cadcfe97d3e7efc94f982ee7f8eab363872"
            },
            "downloads": -1,
            "filename": "ordered_rectangles-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b6f9ed5feb969c71d85c18337162bb00",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13590,
            "upload_time": "2023-11-28T12:19:48",
            "upload_time_iso_8601": "2023-11-28T12:19:48.920876Z",
            "url": "https://files.pythonhosted.org/packages/c8/77/6fb7e1f006e5263b919596aa923c4e442e5b7614cbcd59f58790d90609e1/ordered_rectangles-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "61afddd6a03efa763c982025488e7f3c6380c512adae04d79bfab941930adcb2",
                "md5": "faa2312e4ee124649bf9bd849ff95524",
                "sha256": "9bcbabb45fd466b2f5cc91362365f7a80d01c6b3a018ab81eb71d6d7ccf2c364"
            },
            "downloads": -1,
            "filename": "ordered_rectangles-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "faa2312e4ee124649bf9bd849ff95524",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 13822,
            "upload_time": "2023-11-28T12:19:50",
            "upload_time_iso_8601": "2023-11-28T12:19:50.354944Z",
            "url": "https://files.pythonhosted.org/packages/61/af/ddd6a03efa763c982025488e7f3c6380c512adae04d79bfab941930adcb2/ordered_rectangles-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-28 12:19:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PasaOpasen",
    "github_project": "rectangles-text-view-util",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "ordered-rectangles"
}
        
Elapsed time: 0.17851s