Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping lines in python: breaking on an argument string

I have a script in which a very long argument of type str is passed to a function:

parser = argparse.ArgumentParser(description='Auto-segments a text based on the TANGO algorithm (Rie Kubota Ando and Lillian Lee, "Mostly-Unsupervised Statistical Segmentation of Japanese Kanji Sequences" (Natural Language Engineering, 9(2):127-149, 2003)).')

I'd like to limit line length in this script to 79 chars, which means line breaking in the middle of the string in question. Simply wrapping at 79 yields something like this, which is syntactically ill-formed:

parser = argparse.ArgumentParser(description="Auto-segments a text based on 
    the TANGO algorithm (Rie Kubota Ando and Lillian Lee, 'Mostly-Unsupervis
    ed Statistical Segmentation of Japanese Kanji Sequences' (Natural Langua
    ge Engineering, 9(2):127-149, 2003)).")

PEP 8 has guidelines for breaking lines in various non-argument-string-internal locations, but is there a way to break the line in the middle of an argument string?

(Related but less important question: What is a sensible/conventional way to break natural language text mid-word inside a (python) script?)

like image 665
nahanarts Avatar asked Feb 27 '13 02:02

nahanarts


1 Answers

Literal strings can appear next to each other, and will compile to a single string. Thus:

parser = argparse.ArgumentParser(description="Auto-segments a text based on "
    "the TANGO algorithm (Rie Kubota Ando and Lillian Lee, 'Mostly-Unsupervised "
    "Statistical Segmentation of Japanese Kanji Sequences' (Natural Language "
    "Engineering, 9(2):127-149, 2003)).")

Adjust as desired to fit in 80.

like image 70
Eevee Avatar answered Oct 23 '22 10:10

Eevee