Skip to content

Instantly share code, notes, and snippets.

@salma71
Last active December 18, 2020 23:36
Show Gist options
  • Save salma71/e3a2b58cb4eaa2f44cd8da3561187a9d to your computer and use it in GitHub Desktop.
Save salma71/e3a2b58cb4eaa2f44cd8da3561187a9d to your computer and use it in GitHub Desktop.
#cleaning.py
import pickle
import functools
import math
class Process:
def __init__(self, *args):
self.args = args
self.fun = lambda x: x+2
@classmethod
def encode_col(self, col):
"""[encode columns names from letters to and ID on base 26 ]
"D" -> returns 4
"AA" -> 27
"ZZ" -> 702
Args:
col ([string]): [columns name is alphabetic upper case base 26]
"""
return (functools.reduce(
lambda res, c: res * 26 + ord(c) - ord('A') + 1, col, 0
))
def __getstate__(self):
# this method is called when you are
# going to pickle the class, to know what to pickle
state = self.__dict__.copy()
# don't pickle the parameter fun. otherwise will raise
# AttributeError: Can't pickle local object 'Process.__init__.<locals>.<lambda>'
del state['fun']
return state
def __setstate__(self, state):
self.__dict__.update(state)
def main():
a = Process()
print(a.__dict__)
with open('test_pickle.pkl', 'wb') as f:
pickle.dump(a, f, -1)
f.close()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment