前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从A到Z,26个实用Python模块/函数速览

从A到Z,26个实用Python模块/函数速览

作者头像
Python猫
发布2019-04-10 10:09:46
8270
发布2019-04-10 10:09:46
举报
文章被收录于专栏:Python无止境Python无止境

花下猫说:今天听了左耳朵耗子的《左耳听风》专栏,我受到启发,所以尝试转载一篇英文技术文章和大家分享。获取第一手的信息源,锻炼英文阅读能力,以期长足的技术进步。文末也附上了一篇翻译文章的链接,方便大家对照阅读。这种形式是一个尝试,若你觉得有帮助,麻烦在文末点个赞,这样我会更有动力,继续采集优秀的英文技术文章与大家共读。

原文地址:http://t.cn/RFbYlD1

Python is one of the world’s most popular, in-demand programming languages. This is for many reasons:

  • it’s easy to learn
  • it’s super versatile
  • it has a huge range of modules and libraries

I use Python daily as an integral part of my job as a data scientist. Along the way, I’ve picked up a few useful tricks and tips.

Here, I’ve made an attempt at sharing some of them in an A-Z format.

Most of these ‘tricks’ are things I’ve used or stumbled upon during my day-to-day work. Some I found while browsing the Python Standard Library docs. A few others I found searching through PyPi.

However, credit where it is due — I discovered four or five of them over at awesome-python.com. This is a curated list of hundreds of interesting Python tools and modules. It is worth browsing for inspiration!

all or any

One of the many reasons why Python is such a popular language is because it is readable and expressive.

It is often joked that Python is ‘executable pseudocode’. But when you can write code like this, it’s difficult to argue otherwise:

代码语言:javascript
复制
x = [True, True, False]
代码语言:javascript
复制
if any(x):
    print("At least one True")
代码语言:javascript
复制
if all(x):
    print("Not one False")
代码语言:javascript
复制
if any(x) and not all(x):
    print("At least one True and one False")
bashplotlib

You want to plot graphs in the console?

代码语言:javascript
复制
$ pip install bashplotlib

You can have graphs in the console.

collections

Python has some great default datatypes, but sometimes they just won’t behave exactly how you’d like them to.

Luckily, the Python Standard Library offers the collections module. This handy add-on provides you with further datatypes.

代码语言:javascript
复制
from collections import OrderedDict, Counter
代码语言:javascript
复制
# Remembers the order the keys are added!
x = OrderedDict(a=1, b=2, c=3)
代码语言:javascript
复制
# Counts the frequency of each character
y = Counter("Hello World!")
dir

Ever wondered how you can look inside a Python object and see what attributes it has? Of course you have.

From the command line:

代码语言:javascript
复制
>>> dir()
>>> dir("Hello World")
>>> dir(dir)

This can be a really useful feature when running Python interactively, and for dynamically exploring objects and modules you are working with.

Read more here.

emoji

Yes, really.

代码语言:javascript
复制
$ pip install emoji

Don’t pretend you’re not gonna try it out…

代码语言:javascript
复制
from emoji import emojize
代码语言:javascript
复制
print(emojize(":thumbs_up:"))

?

from future import

One consequence of Python’s popularity is that there are always new versions under development. New versions mean new features — unless your version is out-of-date.

Fear not, however. The future module lets you import functionality from future versions of Python. It’s literally like time travel, or magic, or something.

代码语言:javascript
复制
from __future__ import print_function
代码语言:javascript
复制
print("Hello World!")

Why not have a go importing curly braces?

geopy

Geography can be a challenging terrain for programmers to navigate (ha, a pun!). But the geopy module makes it unnervingly easy.

代码语言:javascript
复制
$ pip install geopy

It works by abstracting the APIs of a range of different geocoding services. It enables you to obtain a place’s full street address, latitude, longitude, and even altitude.

There’s also a useful distance class. It calculates the distance between two locations in your favorite unit of measurement.

代码语言:javascript
复制
from geopy import GoogleV3
代码语言:javascript
复制
place = "221b Baker Street, London"
location = GoogleV3().geocode(place)
代码语言:javascript
复制
print(location.address)
print(location.location)
howdoi

Stuck on a coding problem and can’t remember that solution you saw before? Need to check StackOverflow, but don’t want to leave the terminal?

Then you need this useful command line tool.

代码语言:javascript
复制
$ pip install howdoi

Ask it whatever question you have, and it’ll do its best to return an answer.

代码语言:javascript
复制
$ howdoi vertical align css
$ howdoi for loop in java
$ howdoi undo commits in git

Be aware though — it scrapes code from top answers from StackOverflow. It might not always give the most helpful information…

代码语言:javascript
复制
$ howdoi exit vim
inspect

Python’s inspect module is great for understanding what is happening behind the scenes. You can even call its methods on itself!

The code sample below uses inspect.getsource() to print its own source code. It also uses inspect.getmodule() to print the module in which it was defined.

The last line of code prints out its own line number.

代码语言:javascript
复制
import inspect
代码语言:javascript
复制
print(inspect.getsource(inspect.getsource))
print(inspect.getmodule(inspect.getmodule))
print(inspect.currentframe().f_lineno)

Of course, beyond these trivial uses, the inspect module can prove useful for understanding what your code is doing. You could also use it for writing self-documenting code.

Jedi

The Jedi library is an autocompletion and code analysis library. It makes writing code quicker and more productive.

Unless you’re developing your own IDE, you’ll probably be most interested in using Jedi as an editor plugin. Luckily, there are already loads available!

You may already be using Jedi, however. The IPython project makes use of Jedi for its code autocompletion functionality.

**kwargs

When learning any language, there are many milestones along the way. With Python, understanding the mysterious **kwargs syntax probably counts as one.

The double-asterisk in front of a dictionary object lets you pass the contents of that dictionary as named arguments to a function.

The dictionary’s keys are the argument names, and the values are the values passed to the function. You don’t even need to call it kwargs!

代码语言:javascript
复制
dictionary = {"a": 1, "b": 2}
代码语言:javascript
复制
def someFunction(a, b):
    print(a + b)
    return
代码语言:javascript
复制
# these do the same thing:
someFunction(**dictionary)
someFunction(a=1, b=2)

This is useful when you want to write functions that can handle named arguments not defined in advance.

List comprehensions

One of my favourite things about programming in Python are its list comprehensions.

These expressions make it easy to write very clean code that reads almost like natural language.

You can read more about how to use them here.

代码语言:javascript
复制
numbers = [1,2,3,4,5,6,7]
evens = [x for x in numbers if x % 2 is 0]
odds = [y for y in numbers if y not in evens]
代码语言:javascript
复制
cities = ['London', 'Dublin', 'Oslo']
代码语言:javascript
复制
def visit(city):
    print("Welcome to "+city)
代码语言:javascript
复制
for city in cities:
    visit(city)
map

Python supports functional programming through a number of inbuilt features. One of the most useful is the map() function — especially in combination with lambda functions.

代码语言:javascript
复制
x = [1, 2, 3]
y = map(lambda x : x + 1 , x)
代码语言:javascript
复制
# prints out [2,3,4]
print(list(y))

In the example above, map() applies a simple lambda function to each element in x. It returns a map object, which can be converted to some iterable object such as a list or tuple.

newspaper3k

If you haven’t seen it already, then be prepared to have your mind blown by Python’s newspaper module.

It lets you retrieve news articles and associated meta-data from a range of leading international publications. You can retrieve images, text and author names.

It even has some inbuilt NLP functionality.

So if you were thinking of using BeautifulSoup or some other DIY webscraping library for your next project, save yourself the time and effort and $ pip install newspaper3k instead.

Operator overloading

Python provides support for operator overloading, which is one of those terms that make you sound like a legit computer scientist.

It’s actually a simple concept. Ever wondered why Python lets you use the +operator to add numbers and also to concatenate strings? That’s operator overloading in action.

You can define objects which use Python’s standard operator symbols in their own specific way. This lets you use them in contexts relevant to the objects you’re working with.

代码语言:javascript
复制
class Thing:
    def __init__(self, value):
        self.__value = value
    def __gt__(self, other):
        return self.__value > other.__value
    def __lt__(self, other):
        return self.__value < other.__value
代码语言:javascript
复制
something = Thing(100)
nothing = Thing(0)
代码语言:javascript
复制
# True
something > nothing
代码语言:javascript
复制
# False
something < nothing
代码语言:javascript
复制
# Error
something + nothing
pprint

Python’s default print function does its job. But try printing out any large, nested object, and the result is rather ugly.

Here’s where the Standard Library’s pretty-print module steps in. This prints out complex structured objects in an easy-to-read format.

A must-have for any Python developer who works with non-trivial data structures.

代码语言:javascript
复制
import requests
import pprint
代码语言:javascript
复制
url = 'https://randomuser.me/api/?results=1'
users = requests.get(url).json()
代码语言:javascript
复制
pprint.pprint(users)
Queue

Python supports multithreading, and this is facilitated by the Standard Library’s Queue module.

This module lets you implement queue data structures. These are data structures that let you add and retrieve entries according to a specific rule.

‘First in, first out’ (or FIFO) queues let you retrieve objects in the order they were added. ‘Last in, first out’ (LIFO) queues let you access the most recently added objects first.

Finally, priority queues let you retrieve objects according to the order in which they are sorted.

Here’s an example of how to use queues for multithreaded programming in Python.

repr

When defining a class or an object in Python, it is useful to provide an ‘official’ way of representing that object as a string. For example:

代码语言:javascript
复制
>>> file = open('file.txt', 'r')
>>> print(file)
<open file 'file.txt', mode 'r' at 0x10d30aaf0>

This makes debugging code a lot easier. Add it to your class definitions as below:

代码语言:javascript
复制
class someClass:
    def __repr__(self):
        return "<some description here>"
代码语言:javascript
复制
someInstance = someClass()
代码语言:javascript
复制
# prints <some description here>
print(someInstance)
sh

Python makes a great scripting language. Sometimes using the standard os and subprocess libraries can be a bit of a headache.

The sh library provides a neat alternative.

It lets you call any program as if it were an ordinary function — useful for automating workflows and tasks, all from within Python.

代码语言:javascript
复制
import sh
代码语言:javascript
复制
sh.pwd()
sh.mkdir('new_folder')
sh.touch('new_file.txt')
sh.whoami()
sh.echo('This is great!')
Type hints

Python is a dynamically-typed language. You don’t need to specify datatypes when you define variables, functions, classes etc.

This allows for rapid development times. However, there are few things more annoying than a runtime error caused by a simple typing issue.

Since Python 3.5, you have the option to provide type hints when defining functions.

代码语言:javascript
复制
def addTwo(x : Int) -> Int:
    return x + 2

You can also define type aliases:

代码语言:javascript
复制
from typing import List
代码语言:javascript
复制
Vector = List[float]
Matrix = List[Vector]
代码语言:javascript
复制
def addMatrix(a : Matrix, b : Matrix) -> Matrix:
  result = []
  for i,row in enumerate(a):
    result_row =[]
    for j, col in enumerate(row):
      result_row += [a[i][j] + b[i][j]]
    result += [result_row]
  return result
代码语言:javascript
复制
x = [[1.0, 0.0], [0.0, 1.0]]
y = [[2.0, 1.0], [0.0, -2.0]]
代码语言:javascript
复制
z = addMatrix(x, y)

Although not compulsory, type annotations can make your code easier to understand.

They also allow you to use type checking tools to catch those stray TypeErrors before runtime. Probably worthwhile if you are working on large, complex projects!

uuid

A quick and easy way to generate Universally Unique IDs (or ‘UUIDs’) is through the Python Standard Library’s uuid module.

代码语言:javascript
复制
import uuid
代码语言:javascript
复制
user_id = uuid.uuid4()
print(user_id)

This creates a randomized 128-bit number that will almost certainly be unique.

In fact, there are over 2¹²² possible UUIDs that can be generated. That’s over five undecillion (or 5,000,000,000,000,000,000,000,000,000,000,000,000).

The probability of finding duplicates in a given set is extremely low. Even with a trillion UUIDs, the probability of a duplicate existing is much, much less than one-in-a-billion.

Pretty good for two lines of code.

Virtual environments

This is probably my favorite Python thing of all.

Chances are you are working on multiple Python projects at any one time. Unfortunately, sometimes two projects will rely on different versions of the same dependency. Which do you install on your system?

Luckily, Python’s support for virtual environments lets you have the best of both worlds. From the command line:

代码语言:javascript
复制
python -m venv my-project
source my-project/bin/activate
pip install all-the-modules 

Now you can have standalone versions and installations of Python running on the same machine. Sorted!

wikipedia

Wikipedia has a great API that allows users programmatic access to an unrivalled body of completely free knowledge and information.

The wikipedia module makes accessing this API almost embarrassingly convenient.

代码语言:javascript
复制
import wikipedia
代码语言:javascript
复制
result = wikipedia.page('freeCodeCamp')
print(result.summary)
for link in result.links:
    print(link)

Like the real site, the module provides support for multiple languages, page disambiguation, random page retrieval, and even has a donate() method.

xkcd

Humour is a key feature of the Python language — after all, it is named after the British comedy sketch show Monty Python’s Flying Circus. Much of Python’s official documentation references the show’s most famous sketches.

The sense of humour isn’t restricted to the docs, though. Have a go running the line below:

代码语言:javascript
复制
import antigravity

Never change, Python. Never change.

YAML

YAML stands for ‘YAML Ain’t Markup Language’. It is a data formatting language, and is a superset of JSON.

Unlike JSON, it can store more complex objects and refer to its own elements. You can also write comments, making it particularly suited to writing configuration files.

The PyYAML module lets you use YAML with Python. Install with:

代码语言:javascript
复制
$ pip install pyyaml

And then import into your projects:

代码语言:javascript
复制
import yaml

PyYAML lets you store Python objects of any datatype, and instances of any user-defined classes also.

zip

One last trick for ya, and it really is a cool one. Ever needed to form a dictionary out of two lists?

代码语言:javascript
复制
keys = ['a', 'b', 'c']
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))

The zip() inbuilt function takes a number of iterable objects and returns a list of tuples. Each tuple groups the elements of the input objects by their positional index.

You can also ‘unzip’ objects by calling *zip() on them.

Thanks for reading!

So there you have it, an A-Z of Python tricks — hopefully you’ve found something useful for your next project.

Python’s a very diverse and well-developed language, so there’s bound to be many features I haven’t got round to including.

Please share any of your own favorite Python tricks by leaving a response below!

中文翻译:http://t.cn/EwzgO5Y

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python猫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • all or any
  • bashplotlib
  • collections
  • dir
  • emoji
  • from future import
  • geopy
  • howdoi
  • inspect
  • Jedi
  • **kwargs
  • List comprehensions
  • map
  • newspaper3k
  • Operator overloading
  • pprint
  • Queue
  • repr
  • sh
  • Type hints
  • uuid
  • Virtual environments
  • wikipedia
  • xkcd
  • YAML
  • zip
  • Thanks for reading!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档