masterPiece93 / PythonEssentials

0 stars 0 forks source link

IDEAS #2

Open masterPiece93 opened 1 year ago

masterPiece93 commented 1 year ago

when I want to apply series of transformations on a value :

Desired :

want to apply .format on specification["headers"] dict values in my code .

Old Code :

    s = json.dumps(specification["headers"]).strip('{').strip('}')
    s='{'+s.format(**record)+'}'
    __headers = json.loads(s)

Modified Approach :

    class Fn:
        """[Series Of Transformations]
        where applied, series of Fn should be read in reversed order(bottom-to-top)
        """
        convert_to_str = lambda v: json.dumps(v) #1
        remove_braces = lambda s: s.strip('{').strip('}') #2
        apply_py_format = lambda s,m: '{'+s.format(**m)+'}' #3
        convert_to_dict = lambda s: json.loads(s) #4

    __body=Fn.convert_to_dict(    #4
            Fn.apply_py_format(    #3
            Fn.remove_braces(    #2
            Fn.convert_to_str(    #1
            specification["headers"]
            )
            ),record
            )
            )
masterPiece93 commented 1 year ago

TO RESEARCH

  1. can I manually place my code in virtualenv , & import it in my Program ??
  2. can I host my python code libraries on my server & allow others to do pip install . ??
masterPiece93 commented 1 year ago

Piping Implementation for series of transformations :-


"""
value: dict = T(value)
result = value >> get("age") >> add(3) >> to_str() >> T.decorate_with('*')
"""

class T:
    def __init__(self,obj):
        self._obj = obj
    @property
    def value(self):
        return self._obj
    def __rshift__(self, other):
        if callable(other):
            self._obj = other(self._obj)
        return self
    def __str__(self):
        return str(self._obj)
    decorate_with = lambda v: lambda obj: '*' + obj + '*'
value: dict = T({"age":24})
get = lambda v: lambda obj: obj.get(v,0)
add = lambda v: lambda obj: obj + v
to_str = lambda: lambda obj: str(obj)

result = value >> get("age") >> add(3) >> to_str() >> T.decorate_with('*')
print(type(result))
print(result.value)
masterPiece93 commented 1 year ago
class Print:
    def __lshift__(self, obj):
        print(obj)
        return obj
p=Print()
p << f"very cool {1}"
masterPiece93 commented 1 year ago
class Print:
    def __lshift__(self, obj):
        print(obj)
        return obj
p=Print()
masterPiece93 commented 1 year ago
class Print:
    def __lshift__(self, obj):
        print(obj)
        return obj
p=Print()
masterPiece93 commented 1 year ago
class Print:
    bcolors = dict(
            HEADING = '\033[95m'
        ,   BLUE = '\033[94m'
        ,   CYAN = '\033[96m'
        ,   GREEN = '\033[92m'
        ,   WARN = '\033[93m'
        ,   FAIL = '\033[91m'
        ,   ENDC = '\033[0m'
        ,   BOLD = '\033[1m'
        ,   UNDERLINE = '\033[4m'
    )
    def __init__(self,color=None):
        color: str = color.upper() if color else None
        self.start = self.bcolors.get(color)
        self.end = self.bcolors['ENDC']
    def __lshift__(self, obj):
        if self.start:
            print(self.start,obj,self.end)
        else:
            print(obj)
        return obj
heading=Print('heading')
p=Print()
warn = Print('warn')
bold = Print('bold')

heading<<"ankit"
warn<<1
warn<<bold<<2<<1<<3
masterPiece93 commented 1 year ago

ENCODING FAILSAFE READ_CSV

def read_csv(self):
    """Read Csv with encoding safe .
    Various Solutions are provided.
    Preffered : Use the Latest one.
    Usage:
        read_csv().v1(your_data_in_bytes)
        read_csv().v2(your_data_in_bytes)
        .
        .
        read_csv().vn(your_data_in_bytes)
    """
    __versions__ = (
            'v1'
        ,   'v2'
        ,   'v3'
        ,   'v4'
        ,   'v5'
        ,   'v6'
    )
    solutions = namedtuple('solutions',__versions__)
    # avilable solutions :-
    def sol1(data):
        return pd.read_csv(BytesIO(data), encoding='latin1')
    def sol2(data):
        """if not `utf-8` then `latin1`
        `latin-1` useually decode almost all the characters
        """
        try:
            return pd.read_csv(BytesIO(data))
        except:
            return sol1(data)
    def sol3(data):
        """Detect the File Encoding & use it"""
        # with open('your_file.csv', 'rb') as f:
        #     enc = chardet.detect(f.read())
        enc = chardet.detect(BytesIO(data).read())
        return pd.read_csv(BytesIO(data), encoding=enc['encoding'])
    def sol4(data):
        """Ignore the characters that can't be decoded"""
        return pd.read_csv(BytesIO(data), encoding_errors="ignore")
    def sol5(data):
        return pd.read_csv(BytesIO(data), encoding="unicode_escape")
    def sol6(data):
        """Hybrid Solution"""
        try:
            return sol2(data)
        except:
            try:
                return sol3(data)
            except:
                return sol4(data)

    available_solutions = solutions(
            v1=sol1
        ,   v2=sol2
        ,   v3=sol3
        ,   v4=sol4
        ,   v5=sol5
        ,   v6=sol6
    )
    return available_solutions