Package: printy
PrintyPrinty is alightandcross-platformlibrary that extends the functionalities of the
built-in functionsprint()andinput(). Printy stands out for its
simplicity and for being and easy to use library, it lets you colorize and apply some standard formats
to your text with an intuitive and friendly API based onflags.NOTE: Printy manages the most common and simple tasks when it comes to print
text and to validate some input. If you want to have more control over the
console output check outRichby @willmcgugan,
an amazing library that let's you do much more cool things!!Table of ContentsInstallationHow to use it?Using global flagsUsing inline flagsUntrusted sourcesBackground ColorsWhat about input()?Curious?APIprinty()inputy()List 1: flagsList 2: typesList 2: conditionsChangelogDependenciesContributingContributorsInstallationyou can either clone this repository or install it via pippipinstallprintyHow to use it?Once you install printy, you can find a short but concise documentation about the
available flags and the syntax by running the following command on your console:python-mprintyThis will print out some instructions right away.Using global flagsFirst of all, import printy:fromprintyimportprintyPrinty is still a 'print' statement, so you can use it as it is:printy("text with no format")You can use a global set of flags to specify a format you want to apply to the text,
let's say we want to colorize a text with a bold blue and also adding an underline:printy("Text with a bold blue color and underlined",'bBU')Using inline flagsAlthough applying a global format is interesting, it is not as much as applying
some specific format to some section of the text only. For that, printy uses a
intuitive syntax to accomplish that goal. Use the [] to specify the flags to use
for formatting the text, right before the text, and the @ to finish the formatting
section:printy("Predefined format [rI]This is red and with italic style@ also predefined format")The text that is not surrounded by the format syntax will remain with the predefined
format.But you can always override this predefined format for inline format specifying the flags
in the 'predefined' parameterprinty("Now this is blue [rI]Still red italic@ and also blue",predefined="b")Or, you can override the whole format without changing the inline format with a global flag:printy("Now i am still blue, [rI]and also me@, and me as well ","b")You can combine it with f-strings:a=60printy(f"The day has [yB]{24*a}@ minutes")Printy also supports reading from a file, just pass the path to your file
in the file parameter:# NOTE: Here, it is necessary to specify the flags (if you want)# in the 'flags' parameterprinty(file="/path/to/your/file/file.extension",flags="cU")You can also pretty print your dictionaries, lists, tuples, sets, and objects:my_dict={'id':71,'zip_codes':['050001','050005','050011','050015','050024'],'code':'05001','country':{'code':'co'},'city_translations':[{'language_code':'es','name':'Medellín'}],'flag':None}printy(my_dict)my_dict={'id':71,'zip_codes':['050001','050005','050011','050015','050024'],'code':'05001','country':{'code':'co'},'city_translations':[{'language_code':'es','name':'Medellín'}],'flag':None}printy(my_dict,indentation=2)New in v2.2.0Untrusted sourcesWhen dealing with untrusted sources, like, user input, we need to ensure the text is properly escaped
before we pass it to printy. For that, we can use the funtionescapeintegrated with printy.Let's say we have andemailvariable that it's fill by an untrusted source:fromprintyimportprinty,escape# Comes from an untrusted sourceemail='example@example.com'# Without escaping itprinty(f'This is your email: [nB]{email}@')# Escaping itprinty(f'This is your email: [nB]{escape(email)}@')New in v2.2.0Background ColorsNow, we can define the background color of the text, either on inline formats or with global flags, we simply pass the color flag between two brackets:fromprintyimportprinty# Global formatprinty('Green bold text over a red background','nB{r}')# Inline formatprinty('Normal Text [nB{r}]Green bold text over a red background@ Also normal')What about input()?Printy also includes an alternative function for the builtin input(), that, not only
lets us applies formats to the prompted message (if passed), but also, we can force
the user to enter a certain type of data.fromprintyimportinputyLet's say we want to get an integer from the user's input, for that, we can set
type='int' in the 'inputy' function (we can specify formats the same way we'd do
with printy)fruits=["Apple","Orange","Pineapple"]fruit=inputy("Select a fruit: ",options=fruits,condition="i")qty=inputy("How many [yBU]%ss@ do you want?"%fruit,predefined="rB",type="int",condition="+")confirmation=inputy("Are you sure you want [r]%d@%ss?"%(qty,fruit),type="bool",options=["y","n"],condition="i")In all of the above examples, if the user enters a value with a type other than
the one specified in 'type' (default is 'str'), the message will show again and will prompt also a warning
(and so on until the user enters a valid value according to the type)You can pass certain conditions to validate the input, for example, you can
passcondition="+"on an input with type 'int' to force the user to enter
a positive integer (valid also for 'float'), check the complete options belowThe best partis that the returned value's type is also the one of the specified
type, therefore, from the above examples, bothfruitwill be str,qtywill be integer, andconfirmationwill be a boolean, so, you're gonna get the information right as you need it.New in v2.1.0You can also add some restriction for numbers: max_digits and max_decimalsCurious?If you want to know what's behind the scenes, you can get the text with all the ANSI escaped sequences,
for that, use theraw_format()function.fromprintyimportraw_formatraw_text=raw_format("Some [rB]formatted@ [yIU]text@")print(repr(raw_text))print(raw_text)For convenience, we have stored all colors and formats flags in list, in case you need them:fromprintyimportCOLORS,FORMATSprint(COLORS)print(FORMATS)APIprinty()ParameterstypeDescriptionvaluestrrequiredValue to be formattedflagsstroptionalGlobal flags to be applied, they can be passed in the 'value' with the following syntax: [flags]value@ (checkList 1for more info)predefinedstroptionalA set of flags to apply to the value as its predefined valuefilestroptionalA path to a file where we want to read the value fromendstroptionalA value to be appended to the value, default is '\n'prettybooloptionalTrue if we want to pretty print objects, False if we do not (default True)indentationintoptionalIndentation when pretty printing dictionaries or any iterable (default 4)inputy()plus printy() parametersParameterstypeDescriptiontypestroptionalType of value we want the user to enter (checkList 2for more info)optionslistoptionalValid only for types 'str' and 'bool', a list of options to scope the valuerender_optionsbooloptionalSpecify whether we want to display the options to the user or notdefaultstroptionalIf no value is entered, this one will be taken, make sure that it belongs to the options list (if passed)conditionstroptionalA character that applies certain restrictions to the value (checkList 3for mor infomax_digitsintoptionalAdds a restriction for numbers about the maximum number of digits that it should havemax_decimalsintoptionalAdds a restriction for numbers about the maximum number of decimals that it should haveList 1 'flags'COLORSk - Applies a black color to the textg - Applies a grey color to the textw - Applies a white color to the text<r - Applies a darkred color to the textr - Applies a red color to the textr> - Applies a lightred color to the text<n - Applies a darkgreen color to the textn - Applies a green color to the textn> - Applies a lightgreen color to the text<y - Applies a darkyellow color to the texty - Applies a yellow color to the texty> - Applies a lightyellow color to the text<b - Applies a darkblue color to the textb - Applies a blue color to the textb> - Applies a lightblue color to the text<m - Applies a darkmagenta color to the textm - Applies a magenta color to the textm> - Applies a lightmagenta color to the text<c - Applies a darkcyan color to the textc - Applies a cyan color to the textc> - Applies a lightcyan color to the text<o - Applies a darkorange color to the texto - Applies a orange color to the texto> - Applies a lightorange color to the text<p - Applies a darkpurple color to the textp - Applies a purple color to the textp> - Applies a lightpurple color to the textFORMATSB - Applies a bold font weight to the textU - Applies an underline to the textI - Applies an italic font type to the textH - Highlights the textS - Crosses out the text, aka StrikeD - Dim effectList 2 'types''int': Value must be an integer or a string that can be turn into an integer, returns the value as an integer'float': Value must be a float or a string that can be turn into a float, returns the value as a float'bool': A string matching 'True' or 'False' if no options are passed, otherwise, a string that matches one of the options, returns the value as a boolean'str': The default type, if 'options' is passed, then the string must match one of the options or its item number.List 3 'conditions''+': Valid for 'int' and 'float' types only. The value must be apositivenumber'-': Valid for 'int' and 'float' types only. The value must be anegativenumber'i': valid for 'str' and 'bool' types only. The value is case insensitive, by default it is case sensitiveChangelogChangelog.mdDependenciesPrinty currently supports Python 3.5 and up. Printy is a cross-platform libraryContributingPlease feel free to contact me if you want to be part of the project and contribute.
Fork or clone, push to your fork, make a pull request, let's make this a better app
every day!!Contributors ✨Thanks goes to these wonderful people (emoji key):Edgardo Obregón💻⚠️💡🤔🚧📖🐛farahduk🤔💻🚧Mihir Singh⚠️💻musicprogram📓This project follows theall-contributorsspecification. Contributions of any kind welcome!