Skip to main content

Environment to python dictionary parser util

Project description

PyPI version Downloads Downloads Downloads

ordered_rectangles

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:

from ordered_rectangles import OrderedRectangles, RectTextViewer, read_text, read_json, write_text, write_json

Usage example:

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
# 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]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ordered_rectangles-0.0.4.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

ordered_rectangles-0.0.4-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file ordered_rectangles-0.0.4.tar.gz.

File metadata

  • Download URL: ordered_rectangles-0.0.4.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.6

File hashes

Hashes for ordered_rectangles-0.0.4.tar.gz
Algorithm Hash digest
SHA256 570b7ec5a55c27b29c529716ac64fd93155bac6b72dadf29f3a772b88973f205
MD5 c0cc5666678e7e4560d17fabad3d990c
BLAKE2b-256 00866f63acede4a4c305e4b6fff74854a1d0086adb048a14cc47074fb37d3156

See more details on using hashes here.

Provenance

File details

Details for the file ordered_rectangles-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for ordered_rectangles-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d44d9cd38bca6b8850c73f2ac06583cc16e884ea7790151543d5f84b238ae0a1
MD5 89f67d1ee3254d9e451764a629e248a0
BLAKE2b-256 536e9753264238a94c57d33a30cd16a346e181795994786e89b881a5dbcfe0b4

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page