自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python僅用3行代碼就能輸出花式字符串圖集,同事直呼666!

開發(fā) 后端
大家在日常的Python開發(fā)中,日志打印的卻枯燥無比。今天就來教大家打印出讓同事羨慕,卻讓領(lǐng)導(dǎo)崩潰的代碼輸出。

 [[343700]]

高逼格的日志

相信Java程序員看到上面的圖,一定不會(huì)陌生。沒錯(cuò),springboot的啟動(dòng)日志。不知道其他人怎么想,我第一次看到這個(gè)啟動(dòng)日志的時(shí)候,就覺得好炫酷。然而,大家在日常的Python開發(fā)中,日志打印的卻枯燥無比。今天就來教大家打印出讓同事羨慕,卻讓領(lǐng)導(dǎo)崩潰的代碼輸出。

字符串圖集鼻祖:figlet

Linux 下有一個(gè)好玩的命令: figlet 這個(gè)命令是把一些信息用大的”字體”打印出來.

之所以說這個(gè)命令好玩,是因?yàn)樗С趾芏喾N的字符花式輸出,這里截取幾個(gè)示例,感興趣的朋友可以去它的官網(wǎng)查看完整演示內(nèi)容:http://www.figlet.org/examples.html

Python為什么為什么這么火,就是因?yàn)檫@貨的模塊太多了,所以figlet當(dāng)然也逃不出Python的五指山。下面給大家來說說Python如何實(shí)現(xiàn)figlet的功能。

figlet的pyhton實(shí)現(xiàn)

萬年不變的套路,yaml模塊的python實(shí)現(xiàn)是pyyaml,所以figlet的python模塊大家應(yīng)該也猜到了,就是pyfiglet。 先來看看他的官網(wǎng):https://pypi.org/project/pyfiglet/

因?yàn)椴皇鞘裁从薪涞哪K,所以沒什么特別的說明,其中看到這么一段:

USAGE

You can use pyfiglet in one of two ways. First, it operates on the commandline as C figlet does and supports most of the same options. Run with --help to see a full list of tweaks. Mostly you will only use -f to change the font. It defaults to standard.flf.

tools/pyfiglet 'text to render'

Pyfiglet is also a library that can be used in python code:

from pyfiglet import Figlet f = Figlet(font='slant') print f.renderText('text to render')

三行代碼,就能實(shí)現(xiàn)字符文字的打印,是不是很簡單?快下載模塊試試吧。 模塊下載: pip install pyfiglet

pyfiglet使用講解

讓我們先來按照示例打印看看效果: 

  1. from pyfiglet import Figlet  
  2. f = Figlet(font='slant' 
  3. print(f.renderText('Python'))  
  4. output:  
  5.     ____        __  __  
  6.    / __ \__  __/ /_/ /_  ____  ____ 
  7.    / /_/ / / / / __/ __ \/ __ \/ __ \  
  8.  / ____/ /_/ / /_/ / / / /_/ / / / /  
  9. /_/    \__, /\__/_/ /_/\____/_/ /_/  
  10.       /____/ 

有時(shí)候,裝B就是這么不費(fèi)吹灰之力。那么,它還有什么其他功能呢?讓我們看看它的源碼: 

  1. class Figlet(object):  
  2.     """  
  3.     Main figlet class.  
  4.     """  
  5.     def __init__(self, font=DEFAULT_FONTdirection='auto',   
  6.                  justify='auto',width=80):  
  7.     ...  
  8. def main():  
  9.     parser = OptionParser(version=__version__ 
  10.                           usage='%prog [options] [text..]' 
  11.     parser.add_option('-f', '--font', default=DEFAULT_FONT 
  12.                       help='font to render with (default: %default)' 
  13.                       metavar='FONT' 
  14.     parser.add_option('-D', '--direction', type='choice' 
  15.                       choices=('auto', 'left-to-right', 'right-to-left'),  
  16.                       default='auto'metavar='DIRECTION' 
  17.                       help='set direction text will be formatted in '  
  18.                            '(default: %default)') 

Figlet提供了font、direction、justfity、width四個(gè)字段,剛才說這個(gè)模塊的花式字體多,來看看有多少吧: 

  1. from pyfiglet import Figlet, FigletFont  
  2. print(FigletFont().getFonts())  
  3. f = Figlet(font='5lineoblique' 
  4. print(f.renderText('Breeze Python'))  
  5. output:  
  6.     //   ) )                                         
  7.     //___/ /   __      ___      ___     ___       ___     
  8.    / __  (   //  ) ) //___) ) //___) )    / /   //___) )   
  9.  //    ) ) //      //       //          / /   //          
  10. //____/ / //      ((____   ((____      / /__ ((____                                                      
  11.                                                
  12.      //   ) )                                       
  13.     //___/ /         __  ___ / __      ___       __     
  14.    / ____ / //   / /  / /   //   ) ) //   ) ) //   ) )   
  15.  //       ((___/ /  / /   //   / / //   / / //   / /    
  16. //            / /  / /   //   / / ((___/ / //   / /    

這么多的字體,你挨個(gè)測試,都能玩一天...

大家看源碼的時(shí)候要注意,只要是帶了OptionParser、argparse類似的模塊,基本都是可以在命令行直接執(zhí)行的。不信你看:

其他實(shí)現(xiàn)與拓展

上面的實(shí)現(xiàn)方式,已經(jīng)簡單到極致了,那么還有什么更方便和好玩的?推薦大家兩個(gè)網(wǎng)站:

在線轉(zhuǎn)換字符:http://patorjk.com/software/taag

ASCII藝術(shù)字(圖)集:https://www.bootschool.net/ascii-art

bootschool不僅可以生成在線圖集,還搜集了很多有趣的字符畫,我們可以通過它的ascii藝術(shù)字圖頁簽獲取,比如我選擇人物:

網(wǎng)頁下面還有很多同類型的圖,這個(gè)網(wǎng)站更能讓你從天亮玩到天黑。怎么樣,介紹了這么多有趣的字符圖集,還不快點(diǎn)贊、關(guān)注支持下我。 

 

責(zé)任編輯:龐桂玉 來源: 菜鳥學(xué)Python
相關(guān)推薦

2020-09-27 10:55:10

代碼Java字符串

2021-07-20 06:37:33

CTO代碼程序員

2022-05-07 07:33:55

TypeScript條件類型

2022-04-29 06:54:48

TS 映射類型User 類型

2021-06-21 09:37:05

代碼開源可視化

2021-03-08 15:04:48

編程Python代碼

2010-03-12 17:35:00

Python字符串

2010-03-22 17:53:50

Python字符Python字符串

2022-05-09 14:04:27

Python字符串格式化輸出

2022-06-08 08:01:28

模板字面量類型

2009-09-17 11:16:44

LINQ代碼生成

2015-02-09 10:43:00

JavaScript

2020-06-28 08:26:41

Python開發(fā)工具

2021-04-19 10:38:06

代碼開發(fā)工具

2023-08-26 20:21:58

字符KotlinJava

2023-08-21 10:28:00

字符串字符Python

2010-03-23 09:47:38

Python隨機(jī)數(shù)Python隨機(jī)字符串

2022-12-20 08:32:02

2016-12-30 13:32:24

字符串算法代碼

2023-08-15 09:00:00

人工智能破解密碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號