Skip to main content

Yet Another Python Library

Project description

pylib

pylib

                 Python License

A python library that helps writing py projects much easier and faster.

Table of contents

This library covers multiple aspects, including:

Installation

You can simply install this library through pip, via following commad:

python3 -m pip install pylib-0xe

Documentation

Buffer IO

This module provides several ways to read, write and edit buffers. You can define file, str and standard-input buffers.

for example you can simply read a whole file like this:

reader = BufferReader(FileBuffer(file_path, "r+"))
while not reader.end_of_buffer():
    line = reader.next_line()

or you can define a string as a buffer and treat it in the same way:

reader = BufferReader(StringBuffer('some awesome text'))
while not reader.end_of_buffer():
    a, b, c = reader.next_int(), reader.next_string(), reader.next_char()

you can also read from standard_input and write to standard_output in this way:

reader = BufferReader(StandardInputBuffer())
writer = BufferWriter(StandardOutputBuffer())
while not reader.end_of_buffer():
    a, b, c = reader.next_int(), reader.next_string(), reader.next_char()
    writer.write_line(f"We have recieved these: ({a}, {b}, {c})")

Data

obj = DataTransferObject.from_dict({'a': 123})
print(obj.a)
#  123

DataTransferObject is a dataclass itself. The best practice to use this dto class is inheritting it as the base class for your model. For example If your new model is named Bob and has two parameters name and height with types str and Optional[int] respectively, it should be implemented like this:

@dataclass
class Bob(DataTransferObject):
  name: str
  height: int | None = 185

and then you can instantiate it in this way:

  bob_marley = Bob({name: "Bob Marley"})

and you can use bob_marley.name and bob_marley.height in your code since now on. Another cool feature of this dto is implementing mapper function for any variable of your choice. It works this way that you can define a mapper function with this style: VARIABLENAME_mapper. It recieves it's argument from the dictionary you provided to instantiate it and convert the input to whatever you implement it in the function. This could be useful if the types of the input data differs from the expected type provided in the class or if you want to change the value of the variable in some way before creating it. For example if you want Bob to convert it's name to upper_case letters, it could be implemented like this:

@dataclass
class Bob(DataTransferObject):
  name: str
  height: int | None = 185

  def name_mapper(name: str) -> str:
    return name.lower()

Debug Tools

  • debug_text: Alternative way to debuging the code via prints into the stderr. example:
debug_text('%B%USome Object%E [%c#%%E] -> %r%%E', 12, {"a": 34})
[Some Object [#12] -> {'a': 34}]
  • list of options:

    1. %c: cyan color
    2. %r: red color
    3. %b: blue color
    4. %g: green color
    5. %y: yellow color
    6. %H: alternative color
    7. %B: bold text
    8. %U: underlined text
    9. %E: clear modifiers
  • TerminalProcess: A neat progress bar for long tasks.

Config

  • Config: A facade class to read json config files easily. I'ts so powerful when you provide your configs in the hierarchical pattern. example usage: under the configs folder, you have several .json config files, or folders containing configs. Let's assume we want to access humans.male.height attribute from mammals.json. We have two approaches to achieve it:

    1. Config('mammals').get('humans.male.height)
    2. Config.read('mammals.humans.male.height)

    It could have default value if there is no correspond attribute was found. like we could have written Config.read(..., default=180)

File

  • File: A class that contains some useful functions to deal with files. Some of them are:
    • read_json(file_path)
    • read_csv(file_path)
    • append_to_file(file_path, string)
    • get_all_files(directory_path, extension)

Json

  • JsonHelper: With this class, you can read, write and merge json files with dot notations. Selector example (for file.json):
{
    "a": {
        "b": {
            "c": {
                "f": "g"
            }
        },
        "d": [1, 2],
        "e": {}
    }
}
json_file = File.read_json('file.json')
JsonHelper.selector_get_value(json_file, 'a.b.c.f') # g
JsonHelper.selector_get_value(json_file, 'a.d') # [1, 2]

Path

  • PathHelper: Provides absolute pathing for the project. Then you can use releative pathing after reaching the project root. As an example:
path = PathHelper.from_root(__file__, 'assets', 'imgs', '1.png')

It will construct the path from the root of the project to the desired file, for this specific example, the file should be accessible under this path: $project_root/assets/imgs/1.png. This function tries to go back from __file__ directory to reach the root directory. The default root directories are src and root. You can specify the root directory name by passing the root_name=YOUR_ROOT_DIR_NAME as a kwarg. Then the above example could be rewritten as something like this:

path = PathHelper.from_root(..., root_name="custom_name")

The best practice to use it with the custom root directory is to write a new PathHelper class that extends PathHelper and apply your custom root_name to it. You can also get rid of __file__ argument in this way. It should be implemented something like this:

from pylib_0xe.path.path_helper import PathHelper as PH


class PathHelper(PH):
  @classmethod
  def root(cls, *path: str) -> str:
    return cls.from_root(__file__, *path, root_name="custom_name")

Argument

  • ArgumentParser: Useful tool to reading arguments passed to a python program executed via command line interface (terminal). for example if you run your program as follow:
python3 main.py --color green --size 2 --fast --O2

you can access the arguments through:

ArgumentParser.get_value('color') -> green
ArgumentParser.get_value('size') -> 2
ArgumentParser.is_option('O2') -> true

String

Algorithms

Graph

Math

  • Geometry: A neat implemented 2d-geometry library. Some of the usefull functions that it provides are:
    • translate(expression, *points): recieves arithmatic expression and the points afterwards. Returns the answer of the expression. example: translate('* + *.', p1, p2, p3, scalar) = ((p1 * p2) + p3) *. scalar
    • side_sign(p1, p2, p3): Returns in which side of the p1->p2 line, p3 is located.
    • inside_polygon(points, p)
    • segment_intersection(l1, l2)

Paradigms

String Processing

  • LIS: Longest Increasing Subsequence implementation.

Trees

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

pylib-0xe-0.0.5.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

pylib_0xe-0.0.5-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file pylib-0xe-0.0.5.tar.gz.

File metadata

  • Download URL: pylib-0xe-0.0.5.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pylib-0xe-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a6408950fca0f14c8ffe426eef6b579d0bd7685505a14359961e34e4710222a0
MD5 1bcaa7415596a5e3c70de57f63612545
BLAKE2b-256 f7ab861aabaab43699a1bd3521b1e8864eb776cddb357baa849f6df511e07d36

See more details on using hashes here.

File details

Details for the file pylib_0xe-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: pylib_0xe-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pylib_0xe-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5ffe183529c1dbd21eee259d684b5c2977c130874560edeecd18ad1abab67f4c
MD5 a6642351e14953e96195646a964d2f80
BLAKE2b-256 76e4cffc770bfea04e53e14388c7457b98ad7b6f7a6687ffa8ae342f16cd2ec1

See more details on using hashes here.

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