Giter Club home page Giter Club logo

ifsugar's Introduction

Overview

motivation

The purpose of ifsugar is to make python source code be ++easier understood by human++.

Install

ifsugar is only supported by ++python3.5 or above++, because the matrix multiplication operator: a @ b is not available in lower python version.

pip3 install ifsugar

Example

from ifsugar import _if, _try, _times
# or you can: from ifsugar import *

if __name__ == "__main__":
    if "test _if":
        x = 42
        x @= 24 @_if([1, 2, 3])
        assert x == 24
        x @= 42 @_if({})
        assert x == 24

    if "test _try":
        x = None
        with _try:
            x = 3 / 0
        assert x is None
        with "successful code" @_try:
            y = -9
        assert y == -9

    if "test _times":
        n = []
        m = list(n.append(0) or len(n) for _ in 5 @_times)
        assert m == [1, 2, 3, 4, 5]
        for _ in 3 @_times:
            m.pop()
            n.pop()

        assert m == [1, 2] and n == [0, 0]
        

Sugar: _if

x @= A @_if(B)

is equivalent to x = A if B else x or if B: x = A

Motivation

When reading a source code, it is so important to know ++what you are going to do++ BEFORE ++how to do++.

That is why Ruby supports code if condition and code unless conditional syntax. For example:

#!/usr/bin/ruby
$var =  1
print "1 -- Value is set\n" if $var
print "2 -- Value is set\n" unless $var

Since unless is not intuitive (my personal opinion), ifsugar doesn't adopt _unless

Limitation

both x and A in x @= A @_if(B) cannot be Matrix type.

Sugar: _try

with _try:
    ...code...
    

is equivalent to

try:
    ...code...
except:
    pass
    

_try with comment

with "I am comment" @_try:
    ...code...
    

is equivalent to

if "I am comment":
    try:
        ...code...
    except:
        pass
    

Sugar: _times

for _ in n @_times:
    ...code...

is equivalent to

import itertools
for _ in itertools.repeat(None, n):
    ...code...

i.e. n @_times is equivalent to itertools.repeat(None, n)

Sugar: _as

_as @ globals()  # init once and at beginning
... ...
[({} @_as).clear(), _.setdefault(1, 9)]

is equivalent to

[_ := {}, _.clear(), _.setdefault(1, 9)]

ifsugar's People

Contributors

qorzj avatar

Watchers

 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.