Package: textwrapre
textwrapreA Python package to split text into smaller blocks based on regular expressions.It's useful to split large texts into smaller ones that can't exceed a certain limit,but need to be split where a certain regular expression matches.pip install textwrapreUsagefromtextwrapreimportwrapreimportregextext=r"""Python was created in the early 1990s by Guido van Rossum at Stichting MathematischCentrum (CWI, see https://www.cwi.nl/) in the Netherlands as a successor of alanguage called ABC. Guido remains Python’s principal author, although it includesmany contributions from others.In 1995, Guido continued his work on Python at the Corporation forNational Research Initiatives(CNRI, see https://www.cnri.reston.va.us/) in Reston,Virginia where he released several versions of the software.""".strip()# Split the text using the default regex separatorsplitted=wrapre(text,blocksize=150,raisewhenlonger=True,removenewlines_from_result=True)forsinsplitted:print(len(s),s)# Split the text using a custom regex separatorsplitted=wrapre(text,blocksize=50,regexsep=r"[\r\n\s]+",raisewhenlonger=True,removenewlines_from_result=False,flags=regex.I)forsinsplitted:print(len(s),s)# Split the text and raise an exception when the blocks are bigger than the limitsplitted=wrapre(text,blocksize=20,regexsep=r"[\r\n\s]+",raisewhenlonger=True,flags=regex.I)85Pythonwascreatedintheearly1990sbyGuidovanRossumatStichtingMathematisch79Centrum(CWI,seehttps://www.cwi.nl/)intheNetherlandsasasuccessorofa115languagecalledABC.GuidoremainsPython’sprincipalauthor,althoughitincludesmanycontributionsfromothers.99In1995,GuidocontinuedhisworkonPythonattheCorporationforNationalResearchInitiatives115(CNRI,seehttps://www.cnri.reston.va.us/)inReston,Virginiawherehereleasedseveralversionsofthesoftware.---------------------47Pythonwascreatedintheearly1990sbyGuido46vanRossumatStichtingMathematischCentrum38(CWI,seehttps://www.cwi.nl/)inthe49Netherlandsasasuccessorofalanguagecalled46ABC.GuidoremainsPython’sprincipalauthor,45althoughitincludesmanycontributionsfrom46others.In1995,Guidocontinuedhisworkon49PythonattheCorporationforNationalResearch24Initiatives(CNRI,see44https://www.cnri.reston.va.us/)inReston,47Virginiawherehereleasedseveralversionsof13thesoftware.---------------------Traceback(mostrecentcalllast):File"C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\pydevconsole.py",line364,inruncodecoro=func()File"<input>",line41,in<module>File"C:\ProgramData\anaconda3\envs\adda\textwrapre.py",line73,inwrapreraiseValueError(ValueError:Someblocksarebiggerthanthelimit!Tryagainwithanotherseparatororabiggerlimit!Parametersdefwrapre(text:Union[str,bytes],blocksize:int,regexsep:str=r"[\r\n]",raisewhenlonger:bool=True,removenewlines_from_result:bool=False,*args,**kwargs)->List[Union[str,bytes]]:"""Split a text into blocks of a given size using a regex expression.:param text: the text to be splitted:param blocksize: the maximum size of each block:param regexsep: the regex expression used as separator (default: r"[\r\n]"):param raisewhenlonger: whether or not to raise an exception when a block is bigger than the limit (default: True):param removenewlines_from_result: whether or not to remove new lines from the result (default: False):param *args: additional arguments passed to the regex.compile() function:param **kwargs: additional keyword arguments passed to the regex.compile() function:return: a list of blocks"""