Package: abbrs
abbrsAbbreviationsInstall with:pip install abbrsabbrs/__init__.py:defread_file(filename):withopen(filename,encoding='utf-8')asf:returnf.read()defwrite_file(filename,s):withopen(filename,'w',encoding='utf-8')asf:f.write(s)defrm_hyphen_rf(path):importshutiltry:shutil.rmtree(path)exceptFileNotFoundError:passdefhash(x):fromzlibimportcrc32returncrc32(bytes(str(x),encoding='utf-8'))defcool_hash(x):returnhex(hash(x)).strip('0x')defhash_dir(x,hash=hash):return{i:hash(x.__getattribute__(i))foriindir(x)}# if '_' not in i }defget_time_str(fmt='%c'):importtimereturntime.strftime(fmt)defget_yyyymmdd_time_str():importtimereturnget_time_str('%Y-%m-%d%H-%M-%S')defjson_dump(filename,a):importjsondat=json.dumps(a,ensure_ascii=False,indent='\t')# so that json.decoder.JSONDecodeError won't damage the destination filewithopen(filename,'w',encoding='utf-8')asf:f.write(dat)# json.dump(a, f, ensure_ascii=False, indent='\t')dump_json=json_dumpdefload_json(filename):importjsonwithopen(filename,encoding='utf-8')asf:returnjson.load(f)defprepare_list(l):ifisinstance(l,str):l=l.strip().split()returnldefpack_dict(self,lst):lst=prepare_list(lst)return{i:self.__dict__[i]foriinlst}defload_helper(self,lst,loadfx):lst=prepare_list(lst)foriinlst:self.__dict__[i]=loadfx(i)defcurrent_path():importos.pathreturnos.path.basename(os.getcwd())defnext_version(version_file='version.txt'):SEPARATOR='\n'# style: 'x\nx\nx', without suffixes like 'b1', 'a2'version=read_file(version_file).strip().split(SEPARATOR)iflen(version)!=3:raiseTypeError(version)version=[int(i)foriinversion]version[2]+=1write_file(version_file,SEPARATOR.join(map(str,version)))return'.'.join(map(str,version))defpypi_setup(description,long_description_body,author='YAN Hui Hang, GDUFS',author_email='yanhuihang@126.com',github_prefix='https://gitee.com/yanhuihang/',install=False):importsetuptools,sys,ospackage_name=current_path()long_description=f'''#{package_name}{description}Install with: `pip install{package_name}`{long_description_body}'''write_file("README.md",long_description)# sys.argv.extend('sdist bdist_wheel'.split())sys.argv.append('bdist_wheel')rm_hyphen_rf('dist')setuptools.setup(name=package_name,version=next_version(),author=author,author_email=author_email,description=description,long_description=long_description,long_description_content_type="text/markdown",url=github_prefix+package_name,packages=setuptools.find_packages(),classifiers=["Programming Language :: Python :: 3",# "License :: OSI Approved :: MIT License","Operating System :: OS Independent",],python_requires='>=3.6',entry_points={'console_scripts':[f'{package_name}={package_name}:main'],}ifinstallelse{})os.system('python -m twine upload dist/*')DEFAULT_SHELVE_FILENAME='shelve.out'DEFAULT_ENV_VARIABLE_STOPWORDS='__builtin__ __builtins__ exit get_ipython json np quit shelve time'.split()# __builtins__, my_shelf, modules, etc. can not be shelved.defshelve_restore(globals,filename=DEFAULT_SHELVE_FILENAME):# call with globals()importshelve# WARNING: default open mode of shelve is not 'r', but 'c'# (read & write, create if not present)withshelve.open(filename,'r')asmy_shelf:forkeyinmy_shelf:try:print("'{}'".format(key))globals[key]=my_shelf[key]except:print('ERROR shelving:{0}'.format(key))raise# call with dir()defsave_shelve(dir,globals,filename=DEFAULT_SHELVE_FILENAME,stopwords=DEFAULT_ENV_VARIABLE_STOPWORDS):importshelvewithshelve.open(filename,'n')asmy_shelf:# 'n' for newforkeyindir:try:my_shelf[key]=globals[key]# print('Shelving {} success'.format(key))# except:exceptTypeError:ifkeynotinstopwords:print('ERROR shelving:{0}'.format(key))defsuspend_file(path):importossuffix='.'+get_yyyymmdd_time_str()path2=path+suffixifos.path.exists(path2):suffix2=cool_hash(path2)whileTrue:path3=path2+'.'+suffix2ifos.path.exists(path3):suffix2=cool_hash(path3)else:path2=path3breakos.rename(path,path2)deftable(header,dat):fromprettytableimportPrettyTablex=PrettyTable()x.field_names=['Index']+headerc=1foriindat:x.add_row([c]+i)c+=1returnx