程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Good Python practice skills

編輯:Python

Python Is the most popular in the world 、 One of the most popular programming languages , Yes, there are many reasons .

  • It's easy to learn
  • There are super many functions
  • It has a large number of modules and Libraries

As a data worker , We use it every day Python Handle most of the work . In the process , We will continue to learn some useful skills and tricks .

ad locum , I tried to A - Z Share some of these tips in the beginning format , And these methods are briefly introduced in this paper , If you are interested in one or more of them , You can check the official documents through the references at the end of the article . I hope it can help you .

all or any

Python One of the many reasons why languages are so popular , Because it has good readability and expressiveness .

People often joke that Python Is executable pseudocode . When you can write code like this , It's hard to refute .

x = [True, True, False]
if any(x):
print(" At least one True")
if all(x):
print(" Is full of True")
if any(x) and not all(x):
print(" At least one True And a False")

bashplotlib

Have you ever thought about drawing graphics in the console ?

Bashplotlib It's a Python library , He can help us on the command line ( A rough environment ) Draw data in .

# Module installation
pip install bashplotlib
# Draw examples
import numpy as np
from bashplotlib.histpgram import plot_hist
arr = np.ramdom.normal(size=1000, loc=0, scale=1)
plot_hist(arr, bincount=50)

collections

Python There are some great default data types , But sometimes their behavior doesn't exactly meet your expectations .

Fortunately, ,Python The standard library provides collections modular [1]. This convenient add-on provides you with more data types .

from collections import OrderedDict, Counter
# Remember the order in which keys are added !
x = OrderedDict(a=1, b=2, c=3)
# Count the frequency of each character
y = Counter("Hello World!")

dir

Have you ever thought about how to check Python Object and see what properties it has ? Enter... On the command line :

dir()
dir("Hello World")
dir(dir)

When running interactively Python And dynamically explore the objects and modules you are using , This can be a very useful feature . Read more here functions[2] Related content .

emoji

emoji[3] It is a visual emotional symbol used in wireless communication in Japan , Drawing refers to drawing , Words refer to characters , Can be used to represent a variety of expressions , A smiling face means a smile 、 Cake means food, etc . On Chinese mainland ,emoji Is often called “ Little yellow face ”, Or call it emoji.

# Install the module
pip install emoji
# Make a try
from emoji import emojize
print(emojize(":thumbs_up:"))

from future import

Python One of the results of popularity , There are always new versions under development . The new version means new features —— Unless your version is out of date .

But don't worry . Use this __future__ modular [4] Can help you use Python Future version import function . Literally , It's like time travel 、 Magic or something .

from __future__ import print_function
print("Hello World!")
geogy

Geography , This is a challenging area for most programmers . When getting geographic information or drawing maps , There will be many problems . This geopy modular [5] Make geography related content very easy .

pip install geopy

It abstracts a series of different geocoding services API Come to work . Through it , You can get the full street address of a place 、 latitude 、 Longitude and even altitude .

There is also a useful distance class . It calculates the distance between two positions in your preferred unit of measurement .

from geopy import GoogleV3
place = "221b Baker Street, London"
location = GoogleV3().geocode(place)
print(location.address)
print(location.location)
howdoi

When you use terminal When programming the terminal , By having a problem in StackOverflow Search for answers on , After that, it will return to the terminal to continue programming , Sometimes you don't remember the solution you found before , You need to review StackOverflow, But I don't want to leave the terminal , Then you need to use this useful command line tool howdoi[6].

pip install howdoi

Whatever problems you have , You can ask it , It will try its best to reply .

howdoi vertical align css
howdoi for loop in java
howdoi undo commits in git

But please pay attention to —— It will be from StackOverflow Grab the code from the best answer . It may not always provide the most useful information …

howdoi exit vim

inspect

Python Of inspect modular [7] Perfect for understanding what's going on behind the scenes . You can even call its own methods !

The following code example inspect.getsource() For printing your own source code . inspect.getmodule() It is also used to print the module that defines it .

The last line of code prints its own line number .

import inspect
print(inspect.getsource(inspect.getsource))
print(inspect.getmodule(inspect.getmodule))
print(inspect.currentframe().f_lineno)

Of course , Except for these trivial uses ,inspect Modules can prove useful for understanding what your code is doing . You can also use it to write self documenting code .

Jedi

Jedi Library is an autocomplete and code analysis library . It makes writing code faster 、 More efficient .

Unless you're developing your own IDE, Otherwise you may use Jedi [8] I'm interested in being an editor plug-in . Fortunately, , There are already loads available !

**kwargs

When learning any language , There will be many milestones . Use Python And understand the mysterious **kwargs Grammar may count as an important milestone .

Double asterisk in front of dictionary object **kwargs[9] Allows you to pass the contents of the dictionary as named parameters to the function .

The key of the dictionary is the parameter name , Value is the value passed to the function . You don't even need to call it kwargs!

dictionary = {"a": 1, "b": 2}
def someFunction(a, b):
print(a + b)
return
# These do the same thing :
someFunction(**dictionary)
someFunction(a=1, b=2)

When you want to write a function that can handle undefined named parameters , It's very useful .

list (list) The derived type

About Python Programming , One of my favorite things is its list derivation [10].

These expressions make it easy to write very smooth code , Almost like natural language .

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]
cities = ['London', 'Dublin', 'Oslo']
def visit(city):
print("Welcome to "+city)
for city in cities:
visit(city)

map

Python Support functional programming with many built-in features . The most useful map() One of the functions is the function —— Especially with lambda function [11] When used in combination .

x = [1, 2, 3]
y = map(lambda x : x + 1, x)
# Print out [2,3,4]
print(list(y))

In the example above ,map() Put a simple lambda Function applied to x. It returns a mapping object , The object can be converted into some iteratable objects , For example, list or tuple .

newspaper3k

If you haven't seen it yet , Then be ready to be Python newspaper module [12] The module shocked to . It enables you to retrieve news articles and related metadata from a series of leading international publications . You can retrieve images 、 Text and author's name . It even has some built-in NLP function [13].

therefore , If you are considering using in your next project BeautifulSoup Or something else DIY Web crawling Library , Using this module can save you a lot of time and energy .

pip install newspaper3k

Operator overloading

Python Provides for operator overloading [14] Support , This is one of the terms that makes you sound like a legitimate computer scientist .

This is actually a simple concept . Have you ever thought about why Python Allow you to use + Operator to add numbers and connection strings ? This is what operator overloading does .

You can define how to use... In your own specific way Python The object of the standard operator symbol . And you can use them in the context of the object you are using .

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
something = Thing(100)
nothing = Thing(0)
# True
something > nothing
# False
something < nothing
# Error
something + nothing

pprint

Python Default print The function does its job . But if you try to use print Function prints out any large nested objects , The result is rather ugly . The beautiful printing module of this standard library pprint[15] You can print out complex structured objects in an easy to read format .

This is anything that uses non trivial data structures Python A must-have for developers .

import requests
import pprint
url = 'https://randomuser.me/api/?results=1'
users = requests.get(url).json()
pprint.pprint(users)

Queue

Python Standard library Queue The module implementation supports multithreading . This module allows you to implement queue data structures . These are data structures that allow you to add and retrieve entries according to specific rules .

“ fifo ”(FIFO) Queues allow you to retrieve objects in the order they are added .“ Last in, first out ”(LIFO) Queues allow you to access recently added objects first .

Last , Priority queues allow you to retrieve objects according to their sort order .

This is a how in Python Queue is used in Queue[16] Examples of multithreaded programming .

repr

stay Python When a class or object is defined in , Provides an... That represents the object as a string “ official ” This method is very useful . for example :

>>> file = open('file.txt', 'r')
>>> print(file)
<open file 'file.txt', mode 'r' at 0x10d30aaf0>

This makes it easier to debug code . Add it to your class definition , As shown below :

class someClass:
def __repr__(self):
return "<some description here>"
someInstance = someClass()
# Print <some description here>
print(someInstance)

sh

Python It's a great scripting language . Sometimes standard os and subprocess Ku may have a headache .

The SH library [17] Allows you to call any program like a normal function —— Useful for automating workflows and tasks .

import sh
sh.pwd()
sh.mkdir('new_folder')
sh.touch('new_file.txt')
sh.whoami()
sh.echo('This is great!')

Type hints

Python It is a dynamically typed language . Defining variables 、 function 、 Class does not need to specify the data type . This allows fast development time . however , Nothing is more annoying than runtime errors caused by simple input problems .

from Python 3.5[18] Start , You can choose to provide type hints when defining functions .

def addTwo(x : Int) -> Int:
return x + 2

You can also define type aliases .

from typing import List
Vector = List[float]
Matrix = List[Vector]
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
x = [[1.0, 0.0], [0.0, 1.0]]
y = [[2.0, 1.0], [0.0, -2.0]]
z = addMatrix(x, y)

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

They also allow you to use type checking tools , Catch those stray before running TypeError. If you are dealing with large 、 Complex projects , It's very useful !

uuid

adopt Python Standard library uuid modular [19] Generate universal unique ID( or “UUID”) A quick and simple method .

import uuid
user_id = uuid.uuid4()
print(user_id)

This will create a random 128 Digit number , That number is almost certainly unique . in fact , Can generate more than 2¹²² It's possible UUID. This is more than five decimal ( or 5,000,000,000,000,000,000,000,000,000,000,000,000).

The probability of finding duplicates in a given set is very low . Even if there are a trillion UUID, The possibility of repetition is also far less than one in a billion .

Virtual environments

You may be in multiple... At the same time Python Work on projects . Unfortunately , Sometimes two projects will depend on different versions of the same dependency . What did you install on your system ?

Fortunately, ,Python Support for A virtual environment [20] So you can have the best of both worlds . From the command line :

python -m venv my-project
source my-project/bin/activate
pip install all-the-modules

Now? , You can run on the same machine Python Stand alone version and installation of .

wikipedia

Wikipedia has a great API, It allows users to programmatically access unparalleled and completely free knowledge and information . stay wikipedia modular [21] Make access to the API Very convenient .

import wikipedia
result = wikipedia.page('freeCodeCamp')
print(result.summary)
for link in result.links:
print(link)

Just like the real site , The module provides multilingual support 、 Page disambiguation 、 Random page search , There's even one donate() Method .

xkcd

Humor is Python A key feature of language , It is based on English comedy skits Python Flying Circus [22] Named .Python Many official documents cite the program's most famous sketches . however ,Python Humor is not limited to documents . Try running the following line :

import antigravity

YAML

YAML[23] refer to “ Unmarked language ” . It's a data format language , yes JSON Superset .

And JSON Different , It can store more complex objects and reference its own elements . You can also write notes , Make it especially suitable for writing configuration files . The PyYAML modular [24] You can use YAML Use Python.

Install and then import into your project :

pip install pyyaml
import yaml

PyYAML Allows you to store... Of any data type Python object , And any instances of user-defined classes .

zip

The finale is also a great module . Have you ever encountered the need to form a dictionary from two lists ?

keys = ['a', 'b', 'c']
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))

The zip() Built in functions require a series of iteratable objects , And return a tuple list . Each tuple groups the elements of the input object according to the location index .

You can also call objects to “ decompression ” object *zip().

At the end

Python It is a very diverse and well developed language , So there will certainly be many functions I haven't considered . If you want to know more about python modular , You can refer to awesome-python[25].

Reference material
[1]collections modular : https://docs.python.org/3/library/collections.html

[2]functions: https://docs.python.org/3/library/functions.html#dir

[3]emoji: https://pypi.org/project/emoji/

[4]__future__ modular : https://docs.python.org/2/library/future.html

[5]geopy modular : https://geopy.readthedocs.io/en/latest/

[6]howdoi: https://github.com/gleitz/howdoi

[7]inspect modular : https://docs.python.org/3/library/inspect.html

[8]Jedi : https://jedi.readthedocs.io/en/latest/docs/usage.html

[9]**kwargs: https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments

[10] List derivation : https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

[11]lambda function : https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions

[12]Python newspaper module : https://pypi.org/project/newspaper3k/

[13] Built in NLP function : https://newspaper.readthedocs.io/en/latest/user_guide/quickstart.html#performing-nlp-on-an-article

[14] Operator overloaded : https://docs.python.org/3/reference/datamodel.html#special-method-names

[15]pprint: https://docs.python.org/3/library/pprint.html

[16]Queue: https://www.tutorialspoint.com/python3/python_multithreading.htm

[17]SH library : http://amoffat.github.io/sh/

[18]Python 3.5: https://docs.python.org/3/library/typing.html

[19]uuid modular : https://docs.python.org/3/library/uuid.html

[20] A virtual environment : https://docs.python.org/3/tutorial/venv.html

[21]wikipedia modular : https://wikipedia.readthedocs.io/en/latest/quickstart.html

[22]Python Flying Circus : https://en.wikipedia.org/wiki/Monty_Python’s_Flying_Circus

[23]YAML: http://yaml.org/

[24]PyYAML modular : https://pyyaml.org/wiki/PyYAMLDocumentation

[25]awesome-python: https://awesome-python.com/

Copyright notice
Original author : data STUDIO( You know ID)
Link to the original text :https://zhuanlan.zhihu.com/p/43


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved