Open masterPiece93 opened 1 year ago
TO RESEARCH
"""
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)
class Print:
def __lshift__(self, obj):
print(obj)
return obj
p=Print()
p << f"very cool {1}"
class Print:
def __lshift__(self, obj):
print(obj)
return obj
p=Print()
class Print:
def __lshift__(self, obj):
print(obj)
return obj
p=Print()
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
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
when I want to apply series of transformations on a value :
Desired :
want to apply
.format
onspecification["headers"]
dict values in my code .Old Code :
Modified Approach :