10个有趣python模块

目录

一、说明

二、应用模块

2.1 文章裁剪Pyperclip

2.2 表情符Emoji

2.3 Howdoi

2.4 Wikipedia

2.5 New types at runtime

2.6 Disassemble Python

2.7 Antigravity

2.8 sys.exit()

2.9 urllib

2.10 Turtle


一、说明

        Python 是一种高级、解释型和通用动态编程语言,侧重于代码的可读性。它在许多组织中使用,因为它支持多种编程范例。它还执行自动内存管理。它是世界上最受欢迎的编程语言之一。这是有很多原因的:

  • 这很容易学习。

  • 它超级多才多艺。

  • 它有大量的模块和库。

        事实上,它可以支持绝大多数第三方模块,它就像蛋糕上的樱桃。有一些非常有趣的模块被认为值得与他人分享。在本文中,讨论了一些模块,无论您是初学者还是专业人士,它们都会派上用场。由于它们中的大多数都是第三方模块,因此它们不是 Python 内置的,需要安装。可以在此处看到第三方模块的安装

.

二、应用模块

2.1 文章裁剪Pyperclip

        创建此模块是为了在 Python 中启用跨平台复制粘贴,而这在以前是不存在的。 pyperclip 模块具有 copy() 和 paste() 函数,可以将文本发送到计算机的剪贴板并从中接收文本。将程序的输出发送到剪贴板将使粘贴到电子邮件、文字处理器或其他一些软件变得容易。

# Python program to
# demonstrate pyperclip module


# This will import pyperclip

import pyperclip
pyperclip.copy("Hello world !")
pyperclip.paste()
pyperclip.copy("Isn't pyperclip interesting?")
pyperclip.paste()

        当然,如果程序之外的某些东西改变了剪贴板的内容,paste() 函数将返回它。例如,如果将这句话复制到剪贴板,然后调用 paste(),输出将如下所示:

        “例如,如果将这句话复制到剪贴板,然后调用 paste(),输出将如下所示:”

2.2 表情符Emoji

        表情符号已成为表达和增强简单无聊文本的一种方式。现在,同样的 gem 也可以用在 Python 程序中。对真的!您现在拥有在代码中使用表情符号的终极权力。为此,需要安装表情符号模块。

        在终端。使用:

pip install emoji 

升级到最新的表情包。这是如何完成的:

pip install emoji --upgrade
from emoji import emojize

print(emojize(":thumbs_up:"))

        使用表情符号备忘单找到您最喜欢的表情符号。或者,可以使用表情符号模块中的 encode() 函数将 Unicode 转换为表情符号:

import emojis

emojified = emojis.encode("There is a :snake: in my boot !")

print(emojified)

试试吧!

2.3 Howdoi

        卡在编码问题上?想在不离开终端的情况下访问 StackOverflow?使用 howdoi,您可以做到!通过以下方式安装 howdoi 模块:

pip install howdoi

        或者通过以下方式从 Python 安装:

python setup.py install

        问你有什么问题,它会尽力回答。

howdoi make trees in Python

howdoi commit in git

        从现在开始,您无需打开这些浏览器进行快速搜索,也无需获得大量广告和干扰。只是你好!

howdoi use Howdoi in Python

2.4 Wikipedia

        好像 howdoi 还不够,我们现在可以导入整个维基百科了!是的,我们现在可以使用维基百科模块在 Python 中导入维基百科。使用 Python 的不间断知识流来满足日常需求。
安装为:

pip install wikipedia

        并将其用作:

import wikipedia

result = wikipedia.page("GeeksforGeeks")

print(result.summary)

        如果您希望从摘要中获取特定数量的句子,只需将其作为参数传递给 summary() 函数:

import wikipedia

print(wikipedia.summary("Debugging", sentences = 2))

2.5 New types at runtime

        这可以以完全动态的方式创建新类型。这与创建课程相同,但您可以向朋友展示一些新内容。

# Python program to

# create new type object

  

  

# Creates a new type object

NewType = type("NewType", (object, ), {"attr": "hello newtype"})

New = NewType()

  

# Print the type of object

print(type(New))

  

# Print the attribute of object

print(New.attr)

Output:

<class '__main__.NewType'>
hello newtype

        上面的代码与以下代码相同:

# Creates a class

class NewType:

    attr = "hello newtype"

  

# Initialize an object

New = NewType()

  

# Print the type of object

print(type(New))

  

# Print the attribute of object

print(New.attr)

Output:

<class '__main__.NewType'>
hello newtype

        可能不是最好的模块,但仍然值得一试!

2.6 Disassemble Python

        有没有想过 python 在幕后做了什么?有了标准库模块dis,就可以轻松查看了。

# This will import

# dis module

import dis

  

  

def test(number):

    return (str(number)+str(number))

  

def newFunc(string):

    print("Hello", string)

  

# This will display the

# disassembly of test():

dis.dis(test)

  

# This will display the

# disassembly of newFunc()

dis.dis(newFunc)

Output:

Result:
  8           0 LOAD_GLOBAL              0 (str)
              3 LOAD_FAST                0 (number)
              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
              9 LOAD_GLOBAL              0 (str)
             12 LOAD_FAST                0 (number)
             15 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             18 BINARY_ADD
             19 RETURN_VALUE
  

  3           0 LOAD_GLOBAL              0 (print)
              3 LOAD_CONST               1 ('Hello')
              6 LOAD_FAST                0 (string)
              9 CALL_FUNCTION            2 (2 positional, 0 keyword pair)
             12 POP_TOP
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE

这太厉害了,也太神奇了!

2.7 Antigravity

        这个模块在这里的原因是因为这很有趣!它基本上是 Python 3 中的彩蛋,用于 Google App Engines。它被添加到 Google App Engines 只是作为一种娱乐用户的媒介。

安装:

pip install antigravity

        然后在您的 IDE 中键入它以查看魔术:

import antigravity

        这将在您的 Web 浏览器中打开一个页面,其中包含为您的乐趣而开发的 Python 的滑稽摘要。恭喜!您知道有能力飞行或现在有能力访问此链接 xkcd:Python。

2.8 sys.exit()

        您可能以前使用过 sys 模块,但您知道您可以使用它提前退出您的程序吗?我们可以通过调用 sys.exit() 函数使程序终止。由于此函数在 sys 模块中,因此首先应导入 sys 模块。这不是第三方模块,而是内置于 Python 中,因此无需安装。

# This will import 

# sys module

import sys

  

while True:

    print("Type 'exit' to exit")

    response = input()

    if response == "exit":

        print("Exiting the program")

        sys.exit()

    print("You typed", response)

If the input is :

"Geeky"
"GeeksforGeeks"
"exit"

The output will be :

Type 'exit' to exit
You typed Geeky

Type 'exit' to exit
You typed GeeksforGeeks

Type 'exit' to exit
Exiting the program

2.9 urllib

        urllib 模块是 python 的 URL 处理模块。它用于获取 URL(统一资源定位器)。它使用 urlopen 函数并能够使用各种不同的协议获取 URL。 Urllib 是一个包,它收集了几个用于处理 URL 的模块,例如:

  • urllib.request 用于打开和阅读。
  • urllib.parse 用于解析 URL
  • 引发异常的 urllib.error
  • urllib.robotparser 用于解析 robot.txt 文件

# This will import urlopen

# class from urllib module

from urllib.request import urlopen

  

  

page = urlopen("GeeksforGeeks | A computer science portal for geeks")

print(page.headers)

输出将是:

Server: Apache
Strict-Transport-Security: max-age=3600; includeSubDomains
Access-Control-Allow-Credentials: true
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=UTF-8
X-Akamai-Transformed: 9 - 0 pmb=mRUM,3
Vary: Accept-Encoding
Cache-Control: must-revalidate, max-age=3, s-maxage=21600
Date: Fri, 04 Oct 2019 04:57:37 GMT
Transfer-Encoding: chunked
Connection: close
Connection: Transfer-Encoding
Server-Timing: cdn-cache; desc=HIT
Server-Timing: edge; dur=1

您还可以使用 read() 函数查看网站的编码:

# This will import urlopen

# class from urllib module

  

  

from urllib.request import urlopen

page=urlopen("GeeksforGeeks | A computer science portal for geeks")

  

# Fetches the code 

# of the web page

content = page.read()

  

print(content)

Output:

2.10 Turtle

        是的,可以进口乌龟。别担心它并不慢。 Turtle 是一个用于绘制的 Python 模块。它有一个庞大的应用程序和许多方法,您可以在此处了解这些方法。但是只要掌握一些基础知识,就可以完成非常酷的事情。该模块内置于 Python 中,因此无需安装。

# This will import turtle module

import turtle

  

  

myTurtle = turtle.Turtle()

myWin = turtle.Screen()

  

# Turtle to draw a spiral

def drawSpiral(myTurtle, linelen):

    myTurtle.forward(linelen)

    myTurtle.right(90)

    drawSpiral(myTurtle, linelen-10)

  

drawSpiral(myTurtle, 80)

myWin.exitonclick()

Output:

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无水先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值