This article was first published on my public account “Data STUDIO” : 24 Extremely useful Python Tips

Python is one of the most popular and popular programming languages in the world for many reasons.

  • It is easy to learn
  • There are so many features
  • It has a large number of modules and libraries

As data workers, we use Python every day for most of our work. Along the way, we will continue to learn some useful tips and tricks.

I’ve tried to share some of these tips in an A-Z format here, and briefly describe them in this article. If you’re interested in one or more of them, you can check out the official documentation in resources at the end of this article. I hope it helps.

all or any

One of the many reasons the Python language is so popular is that it is very readable and expressive.

People often joke that Python is executable pseudocode. When you can write code like this, it’s hard to argue.

x = [True.True.False]
if any(x):
    print("At least one True")
if all(x):
    print("The whole is True")
if any(x) and not all(x):
    print("At least one True and one False")
Copy the code

bashplotlib

Have you ever thought about drawing graphics in the console?

Bashplotlib is a Python library that helps us draw data on the command line (in a rough environment).

# Module installation
pip install bashplotlib
# draw instance
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)
Copy the code

collections

Python has some great default data types, but sometimes they don’t behave exactly as you’d expect.

Fortunately, the Python standard library provides the Collections module. This handy add-on gives you 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!")
Copy the code

dir

Ever wonder how to look inside a Python object and see what properties it has? On the command line, enter:

dir(a)dir("Hello World") 
dir(dir)
Copy the code

This can be a very useful feature when running Python interactively and dynamically exploring the objects and modules you are using. Read more about functions here.

emoji

Emoji are visual emotional symbols used in Wireless communication in Japan. “picture” refers to pictures, while “text” refers to characters. They can be used to represent a variety of expressions, such as smiling face for laughter, cake for food, etc. In mainland China, they are commonly known as “little yellow faces”, or emoji.

# Install module
pip install emoji
# Try it
from emoji import emojize
print(emojize(":thumbs_up:"))
Copy the code

👍

from __future__ import

As a result of Python’s popularity, there are always new versions under development. New releases mean new features — unless your version is obsolete.

But don’t worry. Using the __future__ module will help you import functionality with future versions of Python. Literally, it’s like time travel or magic or something.

from __future__ import print_function
print("Hello World!")
Copy the code

geogy

Geography is a challenging field for most programmers. There are also problems in obtaining geographic information or drawing maps. This Geopy module makes geoly-related content very easy.

pip install geopy
Copy the code

It works by abstracting an API for a number of different geocoding services. It allows you to get a place’s full street address, 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)
Copy the code

howdoi

When you program with terminal terminal, you will search for the answer on StackOverflow after encountering a problem, and then go back to the terminal to continue programming. Sometimes you don’t remember the solution you found before, and you need to check StackOverflow again without leaving the terminal. At this point you need to use the useful command line tool howdoi.

pip install howdoi
Copy the code

No matter what questions you have, you can ask it and it will try to reply.

howdoi vertical align css
howdoi for loop in java
howdoi undo commits in git
Copy the code

But be warned — it grabs code from StackOverflow’s best answer. It may not always provide the most useful information……

howdoi exit vim
Copy the code

inspect

Python’s Inspect module is perfect for knowing what’s going on behind the scenes. You can even call its own methods!

The following code example inspect.getSource () is used to print your own source code. Inspect.getmodule () is also used to print the module that defines it.

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

import inspect
print(inspect.getsource(inspect.getsource))
print(inspect.getmodule(inspect.getmodule))
print(inspect.currentframe().f_lineno)
Copy the code

Of course, despite these trivial uses, the Inspect module could prove useful for understanding what your code is doing. You can also use it to write self-documenting code.

Jedi

The Jedi library is an autocomplete and code analysis library. It makes writing code faster and more efficient.

Unless you’re developing your own IDE, you might be interested in using Jedi as an editor plug-in. Fortunately, there is already a load available!

**kwargs

There are many milestones in learning any language. Working with Python and understanding the arcane **kwargs syntax may count as an important milestone.

The double star **kwargs in front of the dictionary object allows you to pass the contents of the dictionary to functions as named arguments.

The keys of the dictionary are the parameter names and the values are the values 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)
Copy the code

This is useful when you want to write functions that can handle named parameters that are not predefined.

List derivation

One of my favorite things about Python programming is its list derivations.

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)
Copy the code

map

Python supports functional programming with many built-in features. One of the most useful map() features is functions — especially when combined with lambda functions.

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

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

newspaper3k

If you haven’t seen it yet, prepare to be blown away by the Python Newspaper Module module. It enables you to retrieve news articles and related metadata from a range of leading international publications. You can retrieve images, text, and author names. It even has some built-in NLP features.

So if you’re thinking of using BeautifulSoup or some other DIY web scraping library for your next project, you can save yourself a lot of time and effort by using this module.

pip install newspaper3k
Copy the code

Operator overloading

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

It’s actually a simple concept. Ever wonder why Python allows you to add numbers and concatenation strings using the + operator? That’s what operator overloading is for.

You can define objects that use Python’s standard operator notation in your own particular way. And you can use them in a context that’s relevant to the object you’re working with.

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
Copy the code

pprint

Python’s default print function does its job. But if you try to print out any large nested objects using the print function, the results are pretty ugly. The library’s nifty print module, PPrint, prints complex structured objects in an easy-to-read format.

This is a must-have for any Python developer who uses non-trivial data structures.

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

Queue

The Queue module implementation of the Python standard library supports multithreading. This module lets you implement queue data structures. These are data structures that allow you to add and retrieve items according to specific rules.

FIFO queues let you retrieve objects in order of addition. Last in, first out (LIFO) queues give you first access to recently added objects.

Finally, priority queues allow you to retrieve objects based on the order in which they are sorted.

This is an example of how to use a Queue for multithreaded programming in Python.

__repr__

When defining a class or object in Python, it is useful to provide an “official” way to represent that object as a string. Such as:

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

This makes debugging code much easier. Add it to your class definition as follows:

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

sh

Python is a great scripting language. Sometimes using standard OS and subprocess libraries can be a bit of a headache.

The SH library lets you call any program just 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! ')Copy the code

Type hints

Python is a dynamically typed language. You do not need to specify data types when defining variables, functions, classes, and so on. This allows for fast development times. However, nothing is more annoying than a run-time error caused by a simple input problem.

Starting with Python 3.5, you can choose to provide type hints when defining functions.

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

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)
Copy the code

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

They also allow you to use type-checking tools to catch stray TypeErrors before they run. This is useful if you are working on large, complex projects!

uuid

A quick and easy way to generate a universal unique ID (or “UUID”) from the UUID module of the Python standard library.

import uuid
user_id = uuid.uuid4()
print(user_id)
Copy the code

This creates a random 128-bit number that is almost certainly unique. In fact, more than 2¹²² of possible UUID can be generated. This more than five decimal (or 5000000000000000000000000000000000000).

The probability of finding a repetition in a given set is extremely low. Even with a trillion UUID, the probability of a repeat is far less than one in a billion.

Virtual environments

You may be working on multiple Python projects at the same time. Unfortunately, sometimes two projects will depend on different versions of the same dependency. What have you installed on your system?

Fortunately, Python’s support for virtual environments allows you to 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
Copy the code

You can now run separate versions and installations of Python on the same machine.

wikipedia

Wikipedia has a great API that allows users to programmatically access unmatched and completely free knowledge and information. The Wikipedia module makes accessing the API very easy.

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

Like the real site, the module offers multilingual support, page disambiguation, random page retrieval, and even a donate() method.

xkcd

Humor is a key feature of the Python language, named after the British comedy sketch show Python Flying Circus. Many official Python documents refer to the show’s most famous sketches. However, Python’s humor is not limited to documentation. Try running the following line:

import antigravity
Copy the code

YAML

YAML stands for “unmarked language.” It is a data formatting language that is a superset of JSON.

Unlike JSON, it can store more complex objects and reference its own elements. You can also write comments that make them particularly useful for writing configuration files. The PyYAML module allows you to use Python with YAML.

Install and then import into your project:

pip install pyyaml

import yaml
Copy the code

PyYAML lets you store Python objects of any data type, as well as instances of any user-defined class.

zip

The last one is also a great module. Have you ever had to form a dictionary from two lists?

keys = ['a'.'b'.'c']
vals = [1.2.3]
zipped = dict(zip(keys, vals))
Copy the code

The zip() built-in function takes a list of iterable objects and returns a list of tuples. Each tuple groups the elements of the input object by positional index.

You can also “unzip” the object *zip() by calling it.

Write in the last

Python is a very diverse and well-developed language, so there are certainly many features that I didn’t consider. If you want to learn more about Python modules, see awesome-python.


Copyright statement

Welcome to share, please indicate the author and the original link in the article, thank you for your respect for knowledge and affirmation of this article.

The original author: data STUDIO links: the original mp.weixin.qq.com/s/U28UvPtFc…

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source, infringement reprint will be held responsible