Coder Social home page Coder Social logo

koffiisen / smartswitchcase Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 14 KB

Python library of implementation of Switch Case

Home Page: https://github.com/koffiisen/SmartSwitchCase

License: MIT License

Python 100.00%
python python3 python2 switch switchcase library libraries

smartswitchcase's Introduction

PyPI version PyPI version PyPI version

PyPI install PyPI version PyPI download

  • BigQuery | Google cloud <==>

Downloads Downloads Downloads

Python library of implementation of Switch Case (SmartSwitchCase)

SmartSwitchCase is a simple library for use switch case statement.

Requirements

Python >= 2.7 must be installed.

Install

smartswitchcase is released on PyPI, so all you need is:

$ pip install smartswitchcase

To upgrade to latest version:

$ pip install --upgrade smartswitchcase

Usage

Basic Usage

from smartswitchcase import SmartSwitchCase,Case

var = 2


def first():
    print("I'm ... 1")


def two():
    print("I'm ... 2")

# Initialisation
swc = SmartSwitchCase(var)
# Add case
swc.case(Case(1, first))
swc.case(Case(2, two))
# Add default statement
swc.default(lambda: "I'm ... Default")
# Run
swc.exc()

# >>> CONSOLE : I'm ... 2 <<<

Advance Usage

from smartswitchcase import SmartSwitchCase, Case
import random

obj = random.randint(1, 11)


def first():
    return "I'm ... 1"


def two():
    return "I'm ... 2"


# Initialisation
swc = SmartSwitchCase(obj)
# Add case
swc.case(Case(1, first))
swc.case(Case(2, two))
swc.case(Case(3, lambda: "I'm ... 3"))
swc.case(Case(4, lambda: "I'm ... 4"))
swc.case(Case(5, lambda: "I'm ... 5"))
swc.case(Case(6, lambda: "I'm ... 6"))
swc.case(Case(7, lambda: obj * 7))
swc.case(Case(8, lambda: 888))
swc.case(Case(9, lambda: 999))
# Add default statement
swc.default(lambda: "I'm ... Default")
# Run
swc.exc()
# If your statement return a result you can get her after execution
result = swc.result()
# Show the result
print(result)

Advance Usage Example (LIST)

from smartswitchcase import SmartSwitchCase, Case

# Initialisation
swc = SmartSwitchCase([1, 2, 3, 4, 5])
# Add case
swc.case(Case([6, 7, 8], lambda: "[6, 7, 8] Match with [1, 2, 3, 4, 5]"))
swc.case(Case([9, 10, 11], lambda: "[6, 7, 8] Match with [1, 2, 3, 4, 5]"))
swc.case(Case([1, 2, 3, 4, 5], lambda: "[1, 2, 3, 4, 5] Match with [1, 2, 3, 4, 5]"))
swc.case(Case([78, 17, 98], lambda: "[78, 17, 98] Match with [1, 2, 3, 4, 5]"))
# Add default statement
swc.default(lambda: "I'm ... Default [1, 2, 3, 4, 5]")
# Run
swc.exc()
# If your statement return a result you can get her after execution
result = swc.result()
# Show the result
print(result)

Advance Usage Example (DICT)

from smartswitchcase import SmartSwitchCase, Case

mydict = {
    "data": {
        "1": {"name": "Joel"},
        "2": {"name": "Github & Python"}
    }
}

# Initialisation
swc = SmartSwitchCase(mydict)
# Add case
swc.case(Case({"data": {"1": {"name": "Hi"}}}, lambda: "Maybe 1"))
swc.case(Case({"data": {"1": {"name": "Git"}, "2": {"name": "Github & Python"}, }}, lambda: "Maybe 2"))
swc.case(Case({"data": {"1": {"name": "Joel"}, "2": {"name": "Github & Python"}, }}, lambda: "Maybe 3"))
swc.case(Case({"data": {"1": {"name": "PyPi"}, "2": {"name": "Github & Python"}, }}, lambda: "Maybe 4"))
swc.case(Case({"data": {"1": {"name": "Dict"}, "2": {"name": "Github & Python"}, }}, lambda: "Maybe 5"))
# Run
swc.exc()
# If your statement return a result you can get her after execution
result = swc.result()
# Show the result
print(result)

Documentation

  • SmartSwitchCase : Take one parameter (The statement value you want tested)
  • Case Take two parameters (value, func):
    • value of case
    • func is the execution function if case is match

Project structure:

  • smartqwitchcase - source code of a package
  • examples - working examples

Contribute

  1. If unsure, open an issue for a discussion
  2. Create a fork
  3. Make your change
  4. Make a pull request
  5. Happy contribution!

For support or coffee :)

screenshot

smartswitchcase's People

Contributors

kodjo49 avatar koffiisen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.