Giter Club home page Giter Club logo

python-adt-extension's Introduction

adt-extension

latest release

Python abstract data structure (ADT) extension.

Install:

pip install adt-extension

Import:

from adt_extension import Set, SwitchDict

Extensions

Currently the package have the ADT extensions:

Class Extension of Description
Set set Set with element type and validation rule.
SwitchDict dict Dictionary with the possibility of behavior of a switch case.

Set

Set with element type and validation rule for the elements that will be inserted into the set.

Example:

from adt_extension import Set

# A set with only even integers
set_int_even = Set(element_type=int, rule=lambda x: (x % 2 == 0))

# Elements that satisfies the element type and validation rule
set_int_even.update({4, 6})
# Elements that satisfies the element type, but not validation rule
set_int_even.update({5})
# Elements that not satisfies the element type
set_int_even.update({"qwe", True})

print(set_int_even)
# Output:
# Set({4, 6})

# Remove element type
set_int_even.element_type = None
# Remove validation rule
set_int_even.rule = None

SwitchDict

Dictionary with the possibility to perform a function or return a value when trying to access a nonexistent index in the dictionary class.

Example:

from adt_extension import SwitchDict

# Same behavior of a normal dictionary
switch_dict = SwitchDict({
    'Apartament': 125,
    'House': 250,
    'Condominium': 300,
})

# Add default case
switch_dict.default_case = 999

# List example
properties_list = [
    'Apartament',
    'House',
    'Condominium',
    'Treehouse',
    'Hotel',
]

# Get values
properties_values = [ switch_dict[ x ] for x in properties_list ]
print(properties_values)
# Output:
# [ 125, 250, 300, 999, 999 ]

# Remove default case, becomes a normal dictionary
switch_dict.default_case = None

python-adt-extension's People

Contributors

alvarofpp avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

python-adt-extension's Issues

Implement the Set class

Set

Create the Set class, inheriting from set, with the following methods:

  • set_rule: method that defines the validation rule of the elements that will be inserted into the set;
  • Parameter in __init__: any parameter that may be a function that returns bool, to serve as a validation rule for the elements that will be inserted into the set;
  • _validate: check if the element meets the validation rule.

Implement the SwitchDict class

SwitchDict

Create the SwitchDict class, inheriting from dictionary, with the possibility to perform a function or return a value when trying to access a nonexistent index in the dictionary.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.