kisa


Namekisa JSON
Version 0.1.0a1 PyPI version JSON
download
home_page
SummaryKisa is an advanced, comprehensive Object Oriented System written for Python.
upload_time2023-01-11 23:04:11
maintainer
docs_urlNone
author
requires_python>=3.6
license
keywords oop object oriented object oriented programming
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Kisa - Python Object Oriented System

Kisa is an advanced, comprehensive Object Oriented System written for Python.

Kisa takes on itself many of the repetitive, unnecessary code sections and provides them automatically, such as:

* Auto generated:
    * Getters (See [Private/Public Attributes](#private_public_attributes))
    * Setters (See [Private/Public Attributes](#private_public_attributes))
    * Constructor (See [Constructor](#constructor))
* A better inheritance system (including abstract, interfaces. See [Inheritance](#inheritance))
* Lazy attributes (See [Lazy Attributes](#lazy_attributes))
* Enforcement of:
    * Attribute type - Including [*Recursive Types* **BETA**](#recursive_types) (types as strings)
    * Inheritance:
        * Abstract class/interface cannot be created
        * Abstract methods implemented
    * Existance of attributes
* Attribute modifiers (before/around/after) available for every:
    * Attribute
    * Method
    * Some of Python native methods, such as:
        * `__init__` (See [Python special methods](#python_special_methods))
        * `__new__` (See [Python special methods](#python_special_methods))
    * Static methods (See [Static Methods](#kisa_static_methods))
    * Static attributes (See [`kisa.StaticInfo`](#kisa_static_info))

Kisa is designed to make classes in Python faster to write, maintain, better organized, and safer.

# Install

In order to install Kisa, simply run the following command:

```bash
pip install kisa
```

## Import

```python
import kisa
```

# Create class:

The only thing required in order to create Kisa class is to pass the `metaclass=kisa.Class` argument at class creation

```python
import kisa

class EmptyClass(metaclass=kisa.Class):
    pass
```

# Class Attributes:

In Kisa, unlike Python native OOP, attributes are defined at class decleration and not during runtime. This cannot be modified.

Kisa has 2 type of attributes:

* kisa.Info - Describes regular attributes. See [`kisa.Info`](#kisa_info)
* kisa.StaticInfo - Describes static attributes. See [`kisa.StaticInfo`](#kisa_static_info)

Kisa attribute decleration is as follows:

```python
class Person(metaclass=kisa.Class):
    amount_created = kisa.StaticInfo(final=False, type=int, default=0)

    firstname = kisa.Info(final=False, type=str, required=True)
    lastname = kisa.Info(final=False, type=str, required=True)
    birth_country = kisa.Info(final=True, type=str, required=True)

    # Otherwise all objects would share the same list
    friends = kisa.Info(final=True, type=list, required=False, default=lambda: [])

    age = kisa.Info(final=False, type=int, required=True, allow_none=False)
```

## <a id="kisa_info"></a> kisa.Info:

`kisa.Info` Class is used to define attribute parameters:

* `final` - Is variable final, i.e. cannot be modified. (default `False`)
* `type` - Type of the attribute (default `object`). For recursive type (class A has type of class A) see [Recursive Types **BETA**](#recursive_types)
* `required` - Is attribute required to be passed to constructor. (default `True`)
* `default` - Default value - Note that if value is `callable` (i.e. a function/lambda) the default value will be the return value of the function (default `None`). See [`default`](#default)
* `allow_none` - Could the attribute be None (default `True`) - If `False`, it will raise Exception when trying to set the attribute value to `None`
* `lazy` - Is attribute is lazy (default `False`) - If `True`, its value will be assigned only when it's value is required (See [Lazy Attributes](#lazy_attributes))

## <a id="default"></a> default

When declaring new attribute in Kisa, we pass the `deafult` attribute to default the attribute value to anything we'd like. It can be:

* Primitive (e.g. `string` or `int`)
* Reference (e.g. `list`, `dict` or some object) **This option is highly discouraged! use [lambda expression](#default_lambda_expression) instead**
    * NOTE: if you'll pass a reference, it will be shared with all of the class objects.
* Lambda expression - See [`default` lambda expression](#default_lambda_expression)

## <a id="default_lambda_expression"></a> argument ```default``` - lambda expression

Kisa automatically detects `callabale` objects (i.e. method/lambda),
calls them and the attribute value to the returned value.

```python
class SchoolClass(metaclass=kisa.Class):
    # NOTE: If we didnt use lambda, all objects would point to the same list
    student_list = kisa.Info(default=lambda: [])
```

Say we want to default to a method rather then a lambda (perhaps since we require more complicated initialization process).
It can be problematic to reference to a method since the method will only be declared later.

For instance, the following will **NOT** work:

```python
# This will NOT work
class Logger(metaclass=kisa.Class):
    # Will raise Exception stating "init_log_file" does not exist
    log_file = kisa.Info(default=init_log_file)

    def init_log_file(self):
        file_path="/var/log/my_logger.log"
        if not self.file_exists(file_path):
            self.create_file(file_path)
        return self.open_file(file_path, mode="w")

    # Rest of the code
    # ...
```

Kisa can pass to default methods either 0 or 1 parameters, depends on what the method expects to receive:
* 0 paramters - Kisa will pass no parameters to the method
* 1 parameters - Kisa will pass the self object to the method

Given that. We can accomplish multi-line initialization by using lambda with self parameter, and from there calling the initialization method

Example:

```python
# This will work
class Logger(metaclass=kisa.Class):
    # 0 parameters
    debug_level = kisa.Info(default = lambda: "production")

    # 1 parameters
    log_file = kisa.Info(default=lambda self: self.init_log_file())

    def init_log_file(self):
        file_path="/var/log/my_logger.log"
        if not self.file_exists(file_path):
            self.create_file(file_path)
        return self.open_file(file_path, mode="w")

    # Rest of the code
    # ...
```

# <a id="attributes_assignment_order"></a> Attributes Assignment Order

Another thing that might prove useful, is using an attribute value during another attribute default initialization. Kisa can handle those requests and will handle those requests and will assign attributes values in the order they are required by the default values

Example:

```python
class A(metaclass=kisa.Class):
    a = kisa.Info(final=True,  default=lambda self: self.b() + self.d() + 1)
    b = kisa.Info(final=True,  default=lambda self: self.c() + 1)
    c = kisa.Info(final=True,  default=0)
    d = kisa.Info(final=False, required=True)

a=A(d=3)
print(a.a()) # prints 5
print(a.b()) # prints 1
print(a.c()) # prints 0
```

In this example, the execution order is as follows:

1. `d` is assigned - User provided values are always assigned first.
2. `c` is assigned - Required by `b`.
3. `b` is assigned - Required by `a`.
4. `a` is assigned - Nobody requires it.

If we'll take a look at the logger we've shown before, it will prove useful:

```python
class Logger(metaclass=kisa.Class):
    debug_level = kisa.Info(type=str, default="production")
    file_path = kisa.Info(type=str, default="/var/log/my_logger.log")

    log_file = kisa.Info(default=lambda self: self.init_log_file())

    def init_log_file(self):
        if not self.file_exists(file_path.file_path()):
            self.create_file(file_path.file_path())
        self.write_log("Initialized Log File")
        return self.open_file(file_path.file_path(), mode="w")

    def write_log(self, msg_log_level, msg):
        if msg_log_level == self.debug_level():
            print(msg)

    # Rest of the code
    # ...
```

The example would first create `file_path`, then `debug_level` and only then it will complete `log_file`. This example is safe to use and the assignment order is enforced

## Static Info - `kisa.StaticInfo`

See [Static Attributes - kisa.StaticInfo](#kisa_static_info)

## <a id="lazy_attributes"></a> Lazy Attributes

A lazy attribute is an attribute which his value is retreived only when required and not at construction. One instance when this is useful is when an attribute usage is questionable, but also expensive to retrieve, so we'll avoid it until we must get it.

Lazy attribute works exactly like regular attribute, but its value will be calculated when:

* Using the attribute getter for the first time - Its value will be the value retrieved from `default`.
* Its value was given from the constructor - In this case, the default value will NOT be calculated.
* Its value was given from the setter - In this case, the default value will NOT be calculated.

Examples:

```python
class DataProcessor(metaclass=kisa.Class):
    file_name      = kisa.Info(type=str, allow_none=False, required=True)
    processed_data = kisa.Info(type=str, allow_none=False, default="get_data", lazy=True)

    def get_data(self):
        print(f"Getting data for {self.file_name()}")
        with open(self.file_name()) as f:
            data = f.read()

        # do some processing on the data
        # ...

        return processed_data

    def post_data(self, data):
        # Post the data online
        # ...

# NOTE: This file is very very large! (1~2gb)
data_processor = DataProcessor(file_name="/var/log/app.log")

# do some actions...

# Only now it will get processed_data value and would print its log
post_data(data_processor.processed_data())
```

# <a id="constructor"></a> Constructor:

Kisa automatically generates the constructor by itself:

```python
class Person(metaclass=kisa.Class):
    firstname = kisa.Info(final=False, type=str, required=True)
    lastname = kisa.Info(final=False, type=str, required=True)
    favorite_food = kisa.Info(final=False, type=str, required=True)
    friends = kisa.Info(final=True, type=int, required=False, default=lambda: [])

# NOTE: friends is generated automatically
myself = Person(firstname="Noam", lastname="Nis", favorite_food="Pizza")
```

* Note: It is possible to modify the construction process, as explained in the [Overriding constructor](#overriding_constructor) section

# <a id="recursive_types"></a> Recursive Types - **BETA**

**IMPORTANT** - Recursive Types is in **beta** and might not detect the classes.

A common practice when writing classes is to use recursive types, i.e. having classes requiring themselves (class `A` has attribute of type `A`).
Another issue that might arise is the usage of a class that will only be declared later in the file.

Example (The following code will not work):

```python
# Example 1 - Will not work
class A(metaclass=kisa.Class):
    a = kisa.Info(type=A)
```

```python
# Example 2 - Will not work
class A(metaclass=kisa.Class):
    b = kisa.Info(type=B)

class B(metaclass=kisa.Class):
    a = kisa.Info(type=A)
```

```python
# Example 3 - Will not work (class B doesn't exist at time of creating class A)
class A(metaclass=kisa.Class):
    b = kisa.Info(type=B)

class B(metaclass=kisa.Class):
    pass
```

In order to solve this issue, Kisa supports to pass types by string and not by reference.

Kisa is designed to handle:

* local package type
* other package type

A couple examples:

```python
# Example 1
class A(metaclass=kisa.Class):
    a = kisa.Info(type="A")
```

```python
# Example 2
class A(metaclass=kisa.Class):
    b = kisa.Info(type="B")

class B(metaclass=kisa.Class):
    a = kisa.Info(type=A)
```

```python
# Example 3
class A(metaclass=kisa.Class):
    b = kisa.Info(type="B")

class B(metaclass=kisa.Class):
    pass
```

This will also work:

```python
class Person(metaclass=kisa.Class):
    fullname = kisa.Info(type="str", required=True)
    age = kisa.Info(type="int", required=True)
```

**NOTE** - When passing type as a string, the type validation will only occur when trying to create the first instance, not before

# <a id="private_public_attributes"></a> Private/Public Attributes:

In Kisa, all attributes are Private and can only be accessed via Get/Set method.
Those methods are generated automatically, and can be modified.
This is explained with greater detail in the [Custom getter/setter](#custom_getter_setter) section.

## getter/setter:

* NOTE: setter also returns the value it has set

Use as follows:
```python
myself = Person(firstname="Noam", lastname="Nis", favorite_food="Pizza")

myself.firstname() # Getter (returns "Noam")
myself.favorite_food("Burger") # Setter (returns "Burger")

```

Please note that the get/set methods are actually the same method but passed with different number of parameters:

* When called with 1 parameter - it would be treated as a setter method.
* When called with no parameters - it will be treated as a getter method.

## <a id="custom_getter_setter"></a>Custom getter/setter:

We can also override Kisa default getter/setter by using the decorator

* `@kisa.getter('attribute_name')` - expects to decorate a function that receives `self` and the attribute value
```python
class Person(metaclass=kisa.Class):
    firstname = kisa.Info(final=False, type=str, required=True)

    @kisa.getter('firstname')
    def get_firstname(self, firstname_value):
        return f"Mr. {firstname_value}"

noam_person = Person(firstname="Noam")
dani_person = Person(firstname="Dani")

print(noam_person.firstname()) # prints "Mr Noam"
print(dani_person.firstname()) # prints "Mr Dani"
```

* `@kisa.setter(attribute_name)` - expects to decorate a function that receives `self` and the new value to be set
    * Note: The value that is set to the attribute at the end is the value that is returned

```python
class Employee(metaclass=kisa.Class):
    salary = kisa.Info(final=False, type=str, required=True)

    @kisa.setter('salary')
    def set_salary(self, new_salary):
        if new_salary <= 0:
            raise Exception("Salary must be bigger then 0!")
        return new_salary

noam = Employee(salary=15)

noam.salary(-30) # Raises exception: Salary must be bigger then 0!
```

# <a id="attribute_modifier"></a>Attribute Modifier:

Attribute Modifier is a way to redirect an attribute/function call and maniputlate it, Kisa has 3 Attribute Modifiers:

* before - Called before every call to the attribute/function
    * Receives:
        * self
        * attribute name
        * `*args`
        * `**kwargs`
* around - Called to surround every call to the attribute/function, and can decide if it wants to call the function at all and with what parameters (see [Around Modifier](#around_modifier))
    * Receives:
        * self
        * attribute name
        * next call - Receives
        * `*args`
        * `**kwargs`
* after - Called after every call to the function
    * Receives:
        * self
        * attribute name
        * `*args`
        * `**kwargs`

## <a id="around_modifier"></a> Around Modifier

Around Modifier allows us to manipulate the method call:
* Decide what args goes to the original method - The args passes to `next_call` method
* What value the original method will finally return - The value returned from the `around` method

## Call Order:

1. all before methods, same order in code
2. all around methods, same order in code (this includes the method call itself)
3. all after methods, same order in code

## Example:

```python
class Sound(metaclass=kisa.Class):
    # ...

    def play(self, filename):
        # plays a sound from file
        pass

    @kisa.before("play", "play_reverse")
    def play_intro_sound(self, attr_name, filename):
        # plays intro sound before every sound play
        pass

    @kisa.around("play", "play_reverse")
    def validate_sound_exists(self, attr_name, next_call, filename):
        # if file exists, play normally, otherwise play an error sound
        if self.file_exists(filename):
            return next_call(filename)
        else:
            return next_call("error_sound.mp3")

    @kisa.after("play", "play_reverse")
    def play_exit_sound(self, attr_name, filename):
        # plays exit sound after every sound play
        pass
```

**NOTE:** the attribute modifier itself, which is a function won't exist in the class/objects

```python
class ModifierClass(metaclass=kisa.Class):
    def foo(self):
        pass

    @kisa.before("foo")
    def bar(self):
        pass

obj = ModifierClass()
obj.bar() # Throws exception, bar doesn't exist
```

## Modifying multiple attributes at once

We can pass before/around/after a list of attributes, kisa will hook to all of the attributes in the list

## Example:

The following example shows part of a logger system that creates a log file for each log level

```python
import os

class Logger(metaclass=kisa.Class):
    info_file  = kisa.Info(type=str, required=True, allow_none=False, final=False)
    warn_file  = kisa.Info(type=str, required=True, allow_none=False, final=False)
    error_file = kisa.Info(type=str, required=True, allow_none=False, final=False)

    @kisa.before("info_file", "warn_file", "error_file")
    def create_if_not_exists(self, attr_name, *args):
        if len(args) == 0:
            # Getter, skip
            return
        else:
            # Setter
            file_name = args[0]
            if not os.path.exists(file_name):
                # Create file
                open(file_name, "w").close()

# Creates 3 files: ./info.txt ./warn.txt ./error.txt
logger = Logger(info_file="./info.txt", warn_file="./warn.txt", error_files="./error.txt")
```

## <a id="python_special_methods"></a> Python special methods (`__init__`, `__setattr__`, `__getattribute__`, `__call__`, etc...)

Kisa supports Attribute Modifiers for Python native methods. Currently supports:

* `__init__`: Allows you to modify the constructor, or run your own things - see [Overriding constructor](#overriding_constructor) for more details
* `__new__`: Allows you to modify the `__new__` call during object creation, in order to modify it (See [Singleton](#singleton) for example)

# Static attributes/methods

## <a id="kisa_static_info"></a> Static Attributes - `kisa.StaticInfo`

In order to declare static attributes we use `kisa.StaticInfo`. `kisa.StaticInfo` behaves exactly like [Regular Attributes (`kisa.Info`)](#kisa_info) except for 2 differences:
* `required` - is **NOT** available for static attributes. This is so since static attributes are not constructed.
* `default` - lambda **can get only 0** attributes.

Example:

```python
class Male(metaclass=kisa.Class):
    nickname = kisa.StaticInfo(final=True, type=str, default=lambda: "Mr.")

print(Male.nickname()) # prints "Mr."

male_obj = Male()

print(male_obj.nickname()) # prints "Mr."

male_obj.nickname("Sir") # We can modify static attributes from instances

print(male_obj.nickname()) # prints "Sir"
print(Male.nickname()) # prints "Sir"
```

## <a id="kisa_static_methods"></a> Static Methods - `@kisa.static`

In order to define static methods, simply use the `@kisa.static` decorator and Kisa will create a static method itself

Example:

```python
class Math(metaclass=kisa.Class):
    @kisa.static
    def add(a, b):
        return a + b

print(Math.add(1, 2)) # prints 3

my_math = Math()
print(my_math.add(1, 2)) # prints 3
```

## Static **Modifiers**

Static Modifiers, work exactly as regular [Attribute Modifiers](#attribute_modifier).
Static Modifiers are automatically detected and act as static methods themselves

Example:

```python
class Math(metaclass=kisa.Class):
    @kisa.static
    def add(a, b):
        return a + b

    @kisa.before("add")
    def before_add(a, b):
        print("Calling a+b")

class Person(metaclass=kisa.Class):
    amount_created = kisa.StaticInfo(final=True, type=int, default=lambda: 0)

    @kisa.before("amount_created")
    def before_amount_changed(new_val=None):
        print(f"New val = {new_val}")

Person.amount_created(1)
```

## <a id="singleton"></a> Singleton

We can use `StaticInfo` in order to create Singleton:

```python
class Logger(metaclass=kisa.Class):
    _initialized = kisa.StaticInfo(type=bool, default=False, allow_none=False)
    # NOTE: Why we use lazy is explained after the example
    _singleton = kisa.StaticInfo(type="Logger", lazy=True)

    name=kisa.Info(type=str)

    @kisa.around('__new__')
    def get_singleton(cls, attr_name, next_call, *args, **kwargs):
        if Logger._singleton() is None:
            Logger._singleton(next_call(*args, **kwargs))
        return Logger._singleton()

    @kisa.around('__init__')
    def init(self, attr_name, next_call, *args, **kwargs):
        if self._initialized() is None:
            next_call(*args, **kwargs)
            self._initialized(True)


l1 = Logger(name="NoamLogger")
l2 = Logger()

print(l1 == l2) # Prints "True"
```

Why `_singleton` is `lazy`?

The reason lies in the fact that static attributes values are computed during class creation.
That means that by that time there is no `Logger` class to be found.
When `_singleton` is `lazy`, kisa will look for type `Logger` only when we'll assign a value to it.
By that time `Logger` will already be defined.

# <a id="inheritance"></a> Inheritance:

Inheritance works the same as in python OOP:

```python
class Vehicle(metaclass=kisa.Class):
    wheels_amount = kisa.Info(type=int, final=True)

    def drive(self, dest):
        print(f"Driving to: {dest} with {self.wheels_amount()} wheels")

class Car(metaclass=kisa.Class, extends=Vehicle):
    def drive(self):
        print("A car driving...")
        self._super().wheels_amount()
```

**NOTE:**

* the native `super()``` method does not work in Kisa, use instead ```self._super()` instead
* Kisa does not support Python native inheritance, only 1 inheritance of class is allowed, via `extends=<extended_class>` as seen in the example

# Abstract Class

Kisa supports abstract classes

```python
class Shape(metaclass=kisa.AbstractClass):
    @kisa.abstract
    def get_circumference(self):
        pass


class Quadrangle(metaclass=kisa.AbstractClass, extends=Shape):
    a = kisa.Info(type=int, required=True)
    b = kisa.Info(type=int, required=True)
    c = kisa.Info(type=int, required=True)
    d = kisa.Info(type=int, required=True)

    def get_circumference(self):
        return self.a() + self.b() + self.c() + self.d()


class Rectangle(metaclass=kisa.Class, extends=Quadrangle):
    pass

```

# Interface

Kisa supports interfaces

* There can be multiple interfaces in a single class/interface
* Interface can only contain `@kisa.abstract` methods (no attributes/implementations)

```python
class Savable(metaclass=kisa.Interface):
    @kisa.abstract
    def save():
        pass

class Loadable(metaclass=kisa.Interface):
    @kisa.abstract
    def load():
        pass

# A safe class is a class that can be saved/loaded to/from hard disk
class ISafeClass(metaclass=kisa.Interface, implements=[Savable, Loadable]):
    pass

class ModifiableConfiguration(metaclass=kisa.Class, implements=ISafeClass):
    def save(self, path):
        # TODO: Save to path
        pass

    def load(self, path):
        # TODO: Load object from path
        pass
```

* Note: For single implemented interface, it is not required to be passed as list
* Note: Kisa currently does not support method signature enforcement

## <a id="overriding_constructor"></a> Overriding constructor

* **NOTE:** Even though the following example uses inheritance, you are by no means required to use inheritance in order to override the constructor

Say we want Rectangle to receive only arguments `a` and `b`, and set the arguments `c` and `d` to equal `a` and `b` accordingly. we can achieve this by applying `kisa.around` to `__init__` constructor.

This in turn will enable you to intercept the call to the constructor before it is called, make your own changes and then let the process proceed normally.

```python
class Shape(metaclass=kisa.AbstractClass):
    @kisa.abstract
    def get_circumference(self):
        pass

class Quadrangle(metaclass=kisa.AbstractClass, extends=Shape):
    a = kisa.Info(type=int, required=True)
    b = kisa.Info(type=int, required=True)
    c = kisa.Info(type=int, required=True)
    d = kisa.Info(type=int, required=True)

    def get_circumference(self):
        return self.a() + self.b() + self.c() + self.d()

class Rectangle(metaclass=kisa.Class, extends=Quadrangle):
    @kisa.around("__init__")
    def around_init(self, attr_name, next_call, a: int, b: int):
        # NOTE: Since Python __init__ doesn't return anything,
        #       we are not required to return the value of next(...)
        next_call(a=a, b=b, c=a, d=b) # Calls Kisa internal constructor

# r = Rectangle(a=1, b=2, c=3, d=4) - Throws Exception, expects only params 'a' and 'b'
r = Rectangle(a=1, b=2) # a=1, b=2, c=1, d=2
```

# A full example:

```python

import kisa

class Savable(metaclass=kisa.Interface):
    @kisa.abstract
    def save():
        pass

class Loadable(metaclass=kisa.Interface):
    @kisa.abstract
    def load():
        pass

# A safe class is a class that can be saved/loaded to/from hard disk
class ISafeClass(metaclass=kisa.Interface, implements=[Savable, Loadable]):
    pass

class Shape(metaclass=kisa.AbstractClass, implements=ISafeClass):
    @kisa.abstract
    def get_circumference():
        pass

    @kisa.abstract
    def get_area():
        pass

    def load(self):
        print("Loading...")
        print("Loaded successfully")

    def save(self):
        print("Saving...")
        print("Saved successfully")


class Quadrangle(metaclass=kisa.AbstractClass, extends=Shape):
    a = kisa.Info(type=int, required=True)
    b = kisa.Info(type=int, required=True)
    c = kisa.Info(type=int, required=True)
    d = kisa.Info(type=int, required=True)

    def get_circumference(self):
        return self.a() + self.b() + self.c() + self.d()


class Rectangle(metaclass=kisa.Class, extends=Quadrangle):

    @kisa.around("__init__")
    def around_init(self, attr_name, next_call, a: int, b: int):
        next_call(a=a, b=b, c=a, d=b)

    def get_self(self):
        return f"{self.a()}x{self.b()}"

    def get_area(self):
        return self.a() * self.b()

    def describe(self, desc):
        return f">> {desc}"


r = Rectangle(a=3, b=2)

# Prints 10
print(r.get_circumference())

# Prints 6
print(r.get_area())

# Prints Saving...
#        Saved successfully
r.save()

# Prints Loading...
#        Loaded successfully
r.load()
```

# Author

Noam Nisanov - `noam.nisanov@gmail.com`

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "kisa",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "Noam Nisanov <noam.nisanov@gmail.com>",
    "keywords": "OOP,Object Oriented,Object Oriented Programming",
    "author": "",
    "author_email": "Noam Nisanov <noam.nisanov@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/d3/9f/8f10ccd870fb94771126f6dbe877525ba8484e5ec5c73197f61d0f256457/kisa-0.1.0a1.tar.gz",
    "platform": null,
    "description": "\n# Kisa - Python Object Oriented System\n\nKisa is an advanced, comprehensive Object Oriented System written for Python.\n\nKisa takes on itself many of the repetitive, unnecessary code sections and provides them automatically, such as:\n\n* Auto generated:\n    * Getters (See [Private/Public Attributes](#private_public_attributes))\n    * Setters (See [Private/Public Attributes](#private_public_attributes))\n    * Constructor (See [Constructor](#constructor))\n* A better inheritance system (including abstract, interfaces. See [Inheritance](#inheritance))\n* Lazy attributes (See [Lazy Attributes](#lazy_attributes))\n* Enforcement of:\n    * Attribute type - Including [*Recursive Types* **BETA**](#recursive_types) (types as strings)\n    * Inheritance:\n        * Abstract class/interface cannot be created\n        * Abstract methods implemented\n    * Existance of attributes\n* Attribute modifiers (before/around/after) available for every:\n    * Attribute\n    * Method\n    * Some of Python native methods, such as:\n        * `__init__` (See [Python special methods](#python_special_methods))\n        * `__new__` (See [Python special methods](#python_special_methods))\n    * Static methods (See [Static Methods](#kisa_static_methods))\n    * Static attributes (See [`kisa.StaticInfo`](#kisa_static_info))\n\nKisa is designed to make classes in Python faster to write, maintain, better organized, and safer.\n\n# Install\n\nIn order to install Kisa, simply run the following command:\n\n```bash\npip install kisa\n```\n\n## Import\n\n```python\nimport kisa\n```\n\n# Create class:\n\nThe only thing required in order to create Kisa class is to pass the `metaclass=kisa.Class` argument at class creation\n\n```python\nimport kisa\n\nclass EmptyClass(metaclass=kisa.Class):\n    pass\n```\n\n# Class Attributes:\n\nIn Kisa, unlike Python native OOP, attributes are defined at class decleration and not during runtime. This cannot be modified.\n\nKisa has 2 type of attributes:\n\n* kisa.Info - Describes regular attributes. See [`kisa.Info`](#kisa_info)\n* kisa.StaticInfo - Describes static attributes. See [`kisa.StaticInfo`](#kisa_static_info)\n\nKisa attribute decleration is as follows:\n\n```python\nclass Person(metaclass=kisa.Class):\n    amount_created = kisa.StaticInfo(final=False, type=int, default=0)\n\n    firstname = kisa.Info(final=False, type=str, required=True)\n    lastname = kisa.Info(final=False, type=str, required=True)\n    birth_country = kisa.Info(final=True, type=str, required=True)\n\n    # Otherwise all objects would share the same list\n    friends = kisa.Info(final=True, type=list, required=False, default=lambda: [])\n\n    age = kisa.Info(final=False, type=int, required=True, allow_none=False)\n```\n\n## <a id=\"kisa_info\"></a> kisa.Info:\n\n`kisa.Info` Class is used to define attribute parameters:\n\n* `final` - Is variable final, i.e. cannot be modified. (default `False`)\n* `type` - Type of the attribute (default `object`). For recursive type (class A has type of class A) see [Recursive Types **BETA**](#recursive_types)\n* `required` - Is attribute required to be passed to constructor. (default `True`)\n* `default` - Default value - Note that if value is `callable` (i.e. a function/lambda) the default value will be the return value of the function (default `None`). See [`default`](#default)\n* `allow_none` - Could the attribute be None (default `True`) - If `False`, it will raise Exception when trying to set the attribute value to `None`\n* `lazy` - Is attribute is lazy (default `False`) - If `True`, its value will be assigned only when it's value is required (See [Lazy Attributes](#lazy_attributes))\n\n## <a id=\"default\"></a> default\n\nWhen declaring new attribute in Kisa, we pass the `deafult` attribute to default the attribute value to anything we'd like. It can be:\n\n* Primitive (e.g. `string` or `int`)\n* Reference (e.g. `list`, `dict` or some object) **This option is highly discouraged! use [lambda expression](#default_lambda_expression) instead**\n    * NOTE: if you'll pass a reference, it will be shared with all of the class objects.\n* Lambda expression - See [`default` lambda expression](#default_lambda_expression)\n\n## <a id=\"default_lambda_expression\"></a> argument ```default``` - lambda expression\n\nKisa automatically detects `callabale` objects (i.e. method/lambda),\ncalls them and the attribute value to the returned value.\n\n```python\nclass SchoolClass(metaclass=kisa.Class):\n    # NOTE: If we didnt use lambda, all objects would point to the same list\n    student_list = kisa.Info(default=lambda: [])\n```\n\nSay we want to default to a method rather then a lambda (perhaps since we require more complicated initialization process).\nIt can be problematic to reference to a method since the method will only be declared later.\n\nFor instance, the following will **NOT** work:\n\n```python\n# This will NOT work\nclass Logger(metaclass=kisa.Class):\n    # Will raise Exception stating \"init_log_file\" does not exist\n    log_file = kisa.Info(default=init_log_file)\n\n    def init_log_file(self):\n        file_path=\"/var/log/my_logger.log\"\n        if not self.file_exists(file_path):\n            self.create_file(file_path)\n        return self.open_file(file_path, mode=\"w\")\n\n    # Rest of the code\n    # ...\n```\n\nKisa can pass to default methods either 0 or 1 parameters, depends on what the method expects to receive:\n* 0 paramters - Kisa will pass no parameters to the method\n* 1 parameters - Kisa will pass the self object to the method\n\nGiven that. We can accomplish multi-line initialization by using lambda with self parameter, and from there calling the initialization method\n\nExample:\n\n```python\n# This will work\nclass Logger(metaclass=kisa.Class):\n    # 0 parameters\n    debug_level = kisa.Info(default = lambda: \"production\")\n\n    # 1 parameters\n    log_file = kisa.Info(default=lambda self: self.init_log_file())\n\n    def init_log_file(self):\n        file_path=\"/var/log/my_logger.log\"\n        if not self.file_exists(file_path):\n            self.create_file(file_path)\n        return self.open_file(file_path, mode=\"w\")\n\n    # Rest of the code\n    # ...\n```\n\n# <a id=\"attributes_assignment_order\"></a> Attributes Assignment Order\n\nAnother thing that might prove useful, is using an attribute value during another attribute default initialization. Kisa can handle those requests and will handle those requests and will assign attributes values in the order they are required by the default values\n\nExample:\n\n```python\nclass A(metaclass=kisa.Class):\n    a = kisa.Info(final=True,  default=lambda self: self.b() + self.d() + 1)\n    b = kisa.Info(final=True,  default=lambda self: self.c() + 1)\n    c = kisa.Info(final=True,  default=0)\n    d = kisa.Info(final=False, required=True)\n\na=A(d=3)\nprint(a.a()) # prints 5\nprint(a.b()) # prints 1\nprint(a.c()) # prints 0\n```\n\nIn this example, the execution order is as follows:\n\n1. `d` is assigned - User provided values are always assigned first.\n2. `c` is assigned - Required by `b`.\n3. `b` is assigned - Required by `a`.\n4. `a` is assigned - Nobody requires it.\n\nIf we'll take a look at the logger we've shown before, it will prove useful:\n\n```python\nclass Logger(metaclass=kisa.Class):\n    debug_level = kisa.Info(type=str, default=\"production\")\n    file_path = kisa.Info(type=str, default=\"/var/log/my_logger.log\")\n\n    log_file = kisa.Info(default=lambda self: self.init_log_file())\n\n    def init_log_file(self):\n        if not self.file_exists(file_path.file_path()):\n            self.create_file(file_path.file_path())\n        self.write_log(\"Initialized Log File\")\n        return self.open_file(file_path.file_path(), mode=\"w\")\n\n    def write_log(self, msg_log_level, msg):\n        if msg_log_level == self.debug_level():\n            print(msg)\n\n    # Rest of the code\n    # ...\n```\n\nThe example would first create `file_path`, then `debug_level` and only then it will complete `log_file`. This example is safe to use and the assignment order is enforced\n\n## Static Info - `kisa.StaticInfo`\n\nSee [Static Attributes - kisa.StaticInfo](#kisa_static_info)\n\n## <a id=\"lazy_attributes\"></a> Lazy Attributes\n\nA lazy attribute is an attribute which his value is retreived only when required and not at construction. One instance when this is useful is when an attribute usage is questionable, but also expensive to retrieve, so we'll avoid it until we must get it.\n\nLazy attribute works exactly like regular attribute, but its value will be calculated when:\n\n* Using the attribute getter for the first time - Its value will be the value retrieved from `default`.\n* Its value was given from the constructor - In this case, the default value will NOT be calculated.\n* Its value was given from the setter - In this case, the default value will NOT be calculated.\n\nExamples:\n\n```python\nclass DataProcessor(metaclass=kisa.Class):\n    file_name      = kisa.Info(type=str, allow_none=False, required=True)\n    processed_data = kisa.Info(type=str, allow_none=False, default=\"get_data\", lazy=True)\n\n    def get_data(self):\n        print(f\"Getting data for {self.file_name()}\")\n        with open(self.file_name()) as f:\n            data = f.read()\n\n        # do some processing on the data\n        # ...\n\n        return processed_data\n\n    def post_data(self, data):\n        # Post the data online\n        # ...\n\n# NOTE: This file is very very large! (1~2gb)\ndata_processor = DataProcessor(file_name=\"/var/log/app.log\")\n\n# do some actions...\n\n# Only now it will get processed_data value and would print its log\npost_data(data_processor.processed_data())\n```\n\n# <a id=\"constructor\"></a> Constructor:\n\nKisa automatically generates the constructor by itself:\n\n```python\nclass Person(metaclass=kisa.Class):\n    firstname = kisa.Info(final=False, type=str, required=True)\n    lastname = kisa.Info(final=False, type=str, required=True)\n    favorite_food = kisa.Info(final=False, type=str, required=True)\n    friends = kisa.Info(final=True, type=int, required=False, default=lambda: [])\n\n# NOTE: friends is generated automatically\nmyself = Person(firstname=\"Noam\", lastname=\"Nis\", favorite_food=\"Pizza\")\n```\n\n* Note: It is possible to modify the construction process, as explained in the [Overriding constructor](#overriding_constructor) section\n\n# <a id=\"recursive_types\"></a> Recursive Types - **BETA**\n\n**IMPORTANT** - Recursive Types is in **beta** and might not detect the classes.\n\nA common practice when writing classes is to use recursive types, i.e. having classes requiring themselves (class `A` has attribute of type `A`).\nAnother issue that might arise is the usage of a class that will only be declared later in the file.\n\nExample (The following code will not work):\n\n```python\n# Example 1 - Will not work\nclass A(metaclass=kisa.Class):\n    a = kisa.Info(type=A)\n```\n\n```python\n# Example 2 - Will not work\nclass A(metaclass=kisa.Class):\n    b = kisa.Info(type=B)\n\nclass B(metaclass=kisa.Class):\n    a = kisa.Info(type=A)\n```\n\n```python\n# Example 3 - Will not work (class B doesn't exist at time of creating class A)\nclass A(metaclass=kisa.Class):\n    b = kisa.Info(type=B)\n\nclass B(metaclass=kisa.Class):\n    pass\n```\n\nIn order to solve this issue, Kisa supports to pass types by string and not by reference.\n\nKisa is designed to handle:\n\n* local package type\n* other package type\n\nA couple examples:\n\n```python\n# Example 1\nclass A(metaclass=kisa.Class):\n    a = kisa.Info(type=\"A\")\n```\n\n```python\n# Example 2\nclass A(metaclass=kisa.Class):\n    b = kisa.Info(type=\"B\")\n\nclass B(metaclass=kisa.Class):\n    a = kisa.Info(type=A)\n```\n\n```python\n# Example 3\nclass A(metaclass=kisa.Class):\n    b = kisa.Info(type=\"B\")\n\nclass B(metaclass=kisa.Class):\n    pass\n```\n\nThis will also work:\n\n```python\nclass Person(metaclass=kisa.Class):\n    fullname = kisa.Info(type=\"str\", required=True)\n    age = kisa.Info(type=\"int\", required=True)\n```\n\n**NOTE** - When passing type as a string, the type validation will only occur when trying to create the first instance, not before\n\n# <a id=\"private_public_attributes\"></a> Private/Public Attributes:\n\nIn Kisa, all attributes are Private and can only be accessed via Get/Set method.\nThose methods are generated automatically, and can be modified.\nThis is explained with greater detail in the [Custom getter/setter](#custom_getter_setter) section.\n\n## getter/setter:\n\n* NOTE: setter also returns the value it has set\n\nUse as follows:\n```python\nmyself = Person(firstname=\"Noam\", lastname=\"Nis\", favorite_food=\"Pizza\")\n\nmyself.firstname() # Getter (returns \"Noam\")\nmyself.favorite_food(\"Burger\") # Setter (returns \"Burger\")\n\n```\n\nPlease note that the get/set methods are actually the same method but passed with different number of parameters:\n\n* When called with 1 parameter - it would be treated as a setter method.\n* When called with no parameters - it will be treated as a getter method.\n\n## <a id=\"custom_getter_setter\"></a>Custom getter/setter:\n\nWe can also override Kisa default getter/setter by using the decorator\n\n* `@kisa.getter('attribute_name')` - expects to decorate a function that receives `self` and the attribute value\n```python\nclass Person(metaclass=kisa.Class):\n    firstname = kisa.Info(final=False, type=str, required=True)\n\n    @kisa.getter('firstname')\n    def get_firstname(self, firstname_value):\n        return f\"Mr. {firstname_value}\"\n\nnoam_person = Person(firstname=\"Noam\")\ndani_person = Person(firstname=\"Dani\")\n\nprint(noam_person.firstname()) # prints \"Mr Noam\"\nprint(dani_person.firstname()) # prints \"Mr Dani\"\n```\n\n* `@kisa.setter(attribute_name)` - expects to decorate a function that receives `self` and the new value to be set\n    * Note: The value that is set to the attribute at the end is the value that is returned\n\n```python\nclass Employee(metaclass=kisa.Class):\n    salary = kisa.Info(final=False, type=str, required=True)\n\n    @kisa.setter('salary')\n    def set_salary(self, new_salary):\n        if new_salary <= 0:\n            raise Exception(\"Salary must be bigger then 0!\")\n        return new_salary\n\nnoam = Employee(salary=15)\n\nnoam.salary(-30) # Raises exception: Salary must be bigger then 0!\n```\n\n# <a id=\"attribute_modifier\"></a>Attribute Modifier:\n\nAttribute Modifier is a way to redirect an attribute/function call and maniputlate it, Kisa has 3 Attribute Modifiers:\n\n* before - Called before every call to the attribute/function\n    * Receives:\n        * self\n        * attribute name\n        * `*args`\n        * `**kwargs`\n* around - Called to surround every call to the attribute/function, and can decide if it wants to call the function at all and with what parameters (see [Around Modifier](#around_modifier))\n    * Receives:\n        * self\n        * attribute name\n        * next call - Receives\n        * `*args`\n        * `**kwargs`\n* after - Called after every call to the function\n    * Receives:\n        * self\n        * attribute name\n        * `*args`\n        * `**kwargs`\n\n## <a id=\"around_modifier\"></a> Around Modifier\n\nAround Modifier allows us to manipulate the method call:\n* Decide what args goes to the original method - The args passes to `next_call` method\n* What value the original method will finally return - The value returned from the `around` method\n\n## Call Order:\n\n1. all before methods, same order in code\n2. all around methods, same order in code (this includes the method call itself)\n3. all after methods, same order in code\n\n## Example:\n\n```python\nclass Sound(metaclass=kisa.Class):\n    # ...\n\n    def play(self, filename):\n        # plays a sound from file\n        pass\n\n    @kisa.before(\"play\", \"play_reverse\")\n    def play_intro_sound(self, attr_name, filename):\n        # plays intro sound before every sound play\n        pass\n\n    @kisa.around(\"play\", \"play_reverse\")\n    def validate_sound_exists(self, attr_name, next_call, filename):\n        # if file exists, play normally, otherwise play an error sound\n        if self.file_exists(filename):\n            return next_call(filename)\n        else:\n            return next_call(\"error_sound.mp3\")\n\n    @kisa.after(\"play\", \"play_reverse\")\n    def play_exit_sound(self, attr_name, filename):\n        # plays exit sound after every sound play\n        pass\n```\n\n**NOTE:** the attribute modifier itself, which is a function won't exist in the class/objects\n\n```python\nclass ModifierClass(metaclass=kisa.Class):\n    def foo(self):\n        pass\n\n    @kisa.before(\"foo\")\n    def bar(self):\n        pass\n\nobj = ModifierClass()\nobj.bar() # Throws exception, bar doesn't exist\n```\n\n## Modifying multiple attributes at once\n\nWe can pass before/around/after a list of attributes, kisa will hook to all of the attributes in the list\n\n## Example:\n\nThe following example shows part of a logger system that creates a log file for each log level\n\n```python\nimport os\n\nclass Logger(metaclass=kisa.Class):\n    info_file  = kisa.Info(type=str, required=True, allow_none=False, final=False)\n    warn_file  = kisa.Info(type=str, required=True, allow_none=False, final=False)\n    error_file = kisa.Info(type=str, required=True, allow_none=False, final=False)\n\n    @kisa.before(\"info_file\", \"warn_file\", \"error_file\")\n    def create_if_not_exists(self, attr_name, *args):\n        if len(args) == 0:\n            # Getter, skip\n            return\n        else:\n            # Setter\n            file_name = args[0]\n            if not os.path.exists(file_name):\n                # Create file\n                open(file_name, \"w\").close()\n\n# Creates 3 files: ./info.txt ./warn.txt ./error.txt\nlogger = Logger(info_file=\"./info.txt\", warn_file=\"./warn.txt\", error_files=\"./error.txt\")\n```\n\n## <a id=\"python_special_methods\"></a> Python special methods (`__init__`, `__setattr__`, `__getattribute__`, `__call__`, etc...)\n\nKisa supports Attribute Modifiers for Python native methods. Currently supports:\n\n* `__init__`: Allows you to modify the constructor, or run your own things - see [Overriding constructor](#overriding_constructor) for more details\n* `__new__`: Allows you to modify the `__new__` call during object creation, in order to modify it (See [Singleton](#singleton) for example)\n\n# Static attributes/methods\n\n## <a id=\"kisa_static_info\"></a> Static Attributes - `kisa.StaticInfo`\n\nIn order to declare static attributes we use `kisa.StaticInfo`. `kisa.StaticInfo` behaves exactly like [Regular Attributes (`kisa.Info`)](#kisa_info) except for 2 differences:\n* `required` - is **NOT** available for static attributes. This is so since static attributes are not constructed.\n* `default` - lambda **can get only 0** attributes.\n\nExample:\n\n```python\nclass Male(metaclass=kisa.Class):\n    nickname = kisa.StaticInfo(final=True, type=str, default=lambda: \"Mr.\")\n\nprint(Male.nickname()) # prints \"Mr.\"\n\nmale_obj = Male()\n\nprint(male_obj.nickname()) # prints \"Mr.\"\n\nmale_obj.nickname(\"Sir\") # We can modify static attributes from instances\n\nprint(male_obj.nickname()) # prints \"Sir\"\nprint(Male.nickname()) # prints \"Sir\"\n```\n\n## <a id=\"kisa_static_methods\"></a> Static Methods - `@kisa.static`\n\nIn order to define static methods, simply use the `@kisa.static` decorator and Kisa will create a static method itself\n\nExample:\n\n```python\nclass Math(metaclass=kisa.Class):\n    @kisa.static\n    def add(a, b):\n        return a + b\n\nprint(Math.add(1, 2)) # prints 3\n\nmy_math = Math()\nprint(my_math.add(1, 2)) # prints 3\n```\n\n## Static **Modifiers**\n\nStatic Modifiers, work exactly as regular [Attribute Modifiers](#attribute_modifier).\nStatic Modifiers are automatically detected and act as static methods themselves\n\nExample:\n\n```python\nclass Math(metaclass=kisa.Class):\n    @kisa.static\n    def add(a, b):\n        return a + b\n\n    @kisa.before(\"add\")\n    def before_add(a, b):\n        print(\"Calling a+b\")\n\nclass Person(metaclass=kisa.Class):\n    amount_created = kisa.StaticInfo(final=True, type=int, default=lambda: 0)\n\n    @kisa.before(\"amount_created\")\n    def before_amount_changed(new_val=None):\n        print(f\"New val = {new_val}\")\n\nPerson.amount_created(1)\n```\n\n## <a id=\"singleton\"></a> Singleton\n\nWe can use `StaticInfo` in order to create Singleton:\n\n```python\nclass Logger(metaclass=kisa.Class):\n    _initialized = kisa.StaticInfo(type=bool, default=False, allow_none=False)\n    # NOTE: Why we use lazy is explained after the example\n    _singleton = kisa.StaticInfo(type=\"Logger\", lazy=True)\n\n    name=kisa.Info(type=str)\n\n    @kisa.around('__new__')\n    def get_singleton(cls, attr_name, next_call, *args, **kwargs):\n        if Logger._singleton() is None:\n            Logger._singleton(next_call(*args, **kwargs))\n        return Logger._singleton()\n\n    @kisa.around('__init__')\n    def init(self, attr_name, next_call, *args, **kwargs):\n        if self._initialized() is None:\n            next_call(*args, **kwargs)\n            self._initialized(True)\n\n\nl1 = Logger(name=\"NoamLogger\")\nl2 = Logger()\n\nprint(l1 == l2) # Prints \"True\"\n```\n\nWhy `_singleton` is `lazy`?\n\nThe reason lies in the fact that static attributes values are computed during class creation.\nThat means that by that time there is no `Logger` class to be found.\nWhen `_singleton` is `lazy`, kisa will look for type `Logger` only when we'll assign a value to it.\nBy that time `Logger` will already be defined.\n\n# <a id=\"inheritance\"></a> Inheritance:\n\nInheritance works the same as in python OOP:\n\n```python\nclass Vehicle(metaclass=kisa.Class):\n    wheels_amount = kisa.Info(type=int, final=True)\n\n    def drive(self, dest):\n        print(f\"Driving to: {dest} with {self.wheels_amount()} wheels\")\n\nclass Car(metaclass=kisa.Class, extends=Vehicle):\n    def drive(self):\n        print(\"A car driving...\")\n        self._super().wheels_amount()\n```\n\n**NOTE:**\n\n* the native `super()``` method does not work in Kisa, use instead ```self._super()` instead\n* Kisa does not support Python native inheritance, only 1 inheritance of class is allowed, via `extends=<extended_class>` as seen in the example\n\n# Abstract Class\n\nKisa supports abstract classes\n\n```python\nclass Shape(metaclass=kisa.AbstractClass):\n    @kisa.abstract\n    def get_circumference(self):\n        pass\n\n\nclass Quadrangle(metaclass=kisa.AbstractClass, extends=Shape):\n    a = kisa.Info(type=int, required=True)\n    b = kisa.Info(type=int, required=True)\n    c = kisa.Info(type=int, required=True)\n    d = kisa.Info(type=int, required=True)\n\n    def get_circumference(self):\n        return self.a() + self.b() + self.c() + self.d()\n\n\nclass Rectangle(metaclass=kisa.Class, extends=Quadrangle):\n    pass\n\n```\n\n# Interface\n\nKisa supports interfaces\n\n* There can be multiple interfaces in a single class/interface\n* Interface can only contain `@kisa.abstract` methods (no attributes/implementations)\n\n```python\nclass Savable(metaclass=kisa.Interface):\n    @kisa.abstract\n    def save():\n        pass\n\nclass Loadable(metaclass=kisa.Interface):\n    @kisa.abstract\n    def load():\n        pass\n\n# A safe class is a class that can be saved/loaded to/from hard disk\nclass ISafeClass(metaclass=kisa.Interface, implements=[Savable, Loadable]):\n    pass\n\nclass ModifiableConfiguration(metaclass=kisa.Class, implements=ISafeClass):\n    def save(self, path):\n        # TODO: Save to path\n        pass\n\n    def load(self, path):\n        # TODO: Load object from path\n        pass\n```\n\n* Note: For single implemented interface, it is not required to be passed as list\n* Note: Kisa currently does not support method signature enforcement\n\n## <a id=\"overriding_constructor\"></a> Overriding constructor\n\n* **NOTE:** Even though the following example uses inheritance, you are by no means required to use inheritance in order to override the constructor\n\nSay we want Rectangle to receive only arguments `a` and `b`, and set the arguments `c` and `d` to equal `a` and `b` accordingly. we can achieve this by applying `kisa.around` to `__init__` constructor.\n\nThis in turn will enable you to intercept the call to the constructor before it is called, make your own changes and then let the process proceed normally.\n\n```python\nclass Shape(metaclass=kisa.AbstractClass):\n    @kisa.abstract\n    def get_circumference(self):\n        pass\n\nclass Quadrangle(metaclass=kisa.AbstractClass, extends=Shape):\n    a = kisa.Info(type=int, required=True)\n    b = kisa.Info(type=int, required=True)\n    c = kisa.Info(type=int, required=True)\n    d = kisa.Info(type=int, required=True)\n\n    def get_circumference(self):\n        return self.a() + self.b() + self.c() + self.d()\n\nclass Rectangle(metaclass=kisa.Class, extends=Quadrangle):\n    @kisa.around(\"__init__\")\n    def around_init(self, attr_name, next_call, a: int, b: int):\n        # NOTE: Since Python __init__ doesn't return anything,\n        #       we are not required to return the value of next(...)\n        next_call(a=a, b=b, c=a, d=b) # Calls Kisa internal constructor\n\n# r = Rectangle(a=1, b=2, c=3, d=4) - Throws Exception, expects only params 'a' and 'b'\nr = Rectangle(a=1, b=2) # a=1, b=2, c=1, d=2\n```\n\n# A full example:\n\n```python\n\nimport kisa\n\nclass Savable(metaclass=kisa.Interface):\n    @kisa.abstract\n    def save():\n        pass\n\nclass Loadable(metaclass=kisa.Interface):\n    @kisa.abstract\n    def load():\n        pass\n\n# A safe class is a class that can be saved/loaded to/from hard disk\nclass ISafeClass(metaclass=kisa.Interface, implements=[Savable, Loadable]):\n    pass\n\nclass Shape(metaclass=kisa.AbstractClass, implements=ISafeClass):\n    @kisa.abstract\n    def get_circumference():\n        pass\n\n    @kisa.abstract\n    def get_area():\n        pass\n\n    def load(self):\n        print(\"Loading...\")\n        print(\"Loaded successfully\")\n\n    def save(self):\n        print(\"Saving...\")\n        print(\"Saved successfully\")\n\n\nclass Quadrangle(metaclass=kisa.AbstractClass, extends=Shape):\n    a = kisa.Info(type=int, required=True)\n    b = kisa.Info(type=int, required=True)\n    c = kisa.Info(type=int, required=True)\n    d = kisa.Info(type=int, required=True)\n\n    def get_circumference(self):\n        return self.a() + self.b() + self.c() + self.d()\n\n\nclass Rectangle(metaclass=kisa.Class, extends=Quadrangle):\n\n    @kisa.around(\"__init__\")\n    def around_init(self, attr_name, next_call, a: int, b: int):\n        next_call(a=a, b=b, c=a, d=b)\n\n    def get_self(self):\n        return f\"{self.a()}x{self.b()}\"\n\n    def get_area(self):\n        return self.a() * self.b()\n\n    def describe(self, desc):\n        return f\">> {desc}\"\n\n\nr = Rectangle(a=3, b=2)\n\n# Prints 10\nprint(r.get_circumference())\n\n# Prints 6\nprint(r.get_area())\n\n# Prints Saving...\n#        Saved successfully\nr.save()\n\n# Prints Loading...\n#        Loaded successfully\nr.load()\n```\n\n# Author\n\nNoam Nisanov - `noam.nisanov@gmail.com`\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Kisa is an advanced, comprehensive Object Oriented System written for Python.",
    "version": "0.1.0a1",
    "split_keywords": [
        "oop",
        "object oriented",
        "object oriented programming"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f991fe0b00bce5cac5469d9f2c622c48aa15512499b4b48a9f30054cf530dca9",
                "md5": "355f1796a2132f22f1154b37f8ccd518",
                "sha256": "ebeaa82aa204d1c9aa2d077f577ddaec48cff8f36facc5a648380fb099d8768e"
            },
            "downloads": -1,
            "filename": "kisa-0.1.0a1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "355f1796a2132f22f1154b37f8ccd518",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 16830,
            "upload_time": "2023-01-11T23:04:09",
            "upload_time_iso_8601": "2023-01-11T23:04:09.804136Z",
            "url": "https://files.pythonhosted.org/packages/f9/91/fe0b00bce5cac5469d9f2c622c48aa15512499b4b48a9f30054cf530dca9/kisa-0.1.0a1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d39f8f10ccd870fb94771126f6dbe877525ba8484e5ec5c73197f61d0f256457",
                "md5": "6710ffdde41039b0c316625a64e4826e",
                "sha256": "157080ac67bccce96dc477f5cb3921559bfdf5b4d14d6b9a06d994ab64b863d1"
            },
            "downloads": -1,
            "filename": "kisa-0.1.0a1.tar.gz",
            "has_sig": false,
            "md5_digest": "6710ffdde41039b0c316625a64e4826e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 23555,
            "upload_time": "2023-01-11T23:04:11",
            "upload_time_iso_8601": "2023-01-11T23:04:11.835266Z",
            "url": "https://files.pythonhosted.org/packages/d3/9f/8f10ccd870fb94771126f6dbe877525ba8484e5ec5c73197f61d0f256457/kisa-0.1.0a1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-11 23:04:11",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "kisa"
}
        
Elapsed time: 0.08336s