python# common modules and simple usage

Source: Internet
Author: User

Directory

    • Random Stochastic module
    • Os folder module:
    • Time module:
    • Matplotlab.pyplot Drawing Module
    • Mpl_toolkits.mplot3d Drawing 3D Diagram module
    • Pygame
    • Requests
      • Request URL:

Article guide

Random Stochastic module
import random

Code = Random.choice (stock_list) # Random selection of elements from a list below is the module I often use, in order to facilitate the use, not have special needs, individuals feel that in the beginning, each module is very deep learning, as long as they know their usual methods of the line. Always update, please search using.

Random randomly Select module:

import randoma = [1, 2, 3, 4, 5]print(random.choice(a))  # 随机从列表中抽取一个元素code = random.choice(stock_list)  # 从一个列表中随机选取元素
Os folder module:
import os# 设置默认文件路径os.chdir()os.chdir(u‘C:/Users/Ocean/OneDrive/class5/data/input_data/stock_data‘)df = pd.read_csv(‘sz300001.csv‘)

Program root directory Address, Os.pardir: Parent Directory

root_path = os.path.abspath(os.path.join(current_file, os.pardir, os.pardir))  # 两级父目录print root_path

Enter the data root directory address

input_data_path = os.path.abspath(os.path.join(root_path, ‘data‘, ‘input_data‘))
Time module:
import time

Get Current date

date_now = time.strftime(‘%Y-%m-%d‘, time.localtime(time.time()))

Timer

start = time.time()end = time.time()used_time = str(end - start)print "used_time: " + used_time
Matplotlab.pyplot Drawing Module
import matplotlib.pyplot as plt

Add a blank canvas

fig = plt.figure(figsize=(12,5))

Set an area on a blank canvas

ax = fig.add_subplot(1,1,1)

Set the title of a drawing block

ax.set_title(str(code))ax.set_xlabel(‘Time‘)  # 设置横坐标x轴的名字ax.set_ylabel(‘Return‘)  # 设置Y轴

Draw a 2D line chart and set the name ' Stock_return '

Plot scatter plots

plt.scatter(df[‘ma_long‘], df[‘final_ratio‘], label=‘ma_long‘)

There are more graphics that can be drawn, and if there is really a need, you can search online again

plt.legend(loc=‘best‘)  # 显示图线的名字plt.show()  # 绘出图像结果
Mpl_toolkits.mplot3d Drawing 3D Diagram module
from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = Axes3D(fig)ax.scatter(df[‘ma_long‘],df[‘ma_short‘],df[‘final_ratio‘], c=‘b‘) #绘制数据点# 设置坐标轴名字ax.set_zlabel(‘final_ratio‘) #坐标轴ax.set_ylabel(‘ma_short‘)ax.set_xlabel(‘ma_long‘)plt.show()
Pygame

Mounting module

$sudo pip3 install pygame

To verify the installation:

$python3 -m pygame.examples.aliens

Pygame Quick Start

    • 1 new aircraft War project
    • 2 Understanding images and achieving image rendering
    • Understanding game loops and Game clocks

The above way of getting started does not introduce too much, Baidu on the internet a bit, everywhere

Requests

Installation:

$sudo pip3 install requests
Request URL:

Non-parametric GET request:

 r=requests.get("http://pythontab.com/justTest")

Request With reference GET :

payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}r = requests.get("http://pythontab.com/justTest", params=payload)

Request Result:

>>>print r.urlhttp://pythontab.com/justTest?key2=value2&key1=value1

POST request:

r = requests.post("http://pythontab.com/postTest", data = {"key":"value"})

As we know, the POST request parameter is passed with the data keyword parameter.
Now the data parameter is passed a dictionary, we can also pass in a JSON format, as follows:

>>> import json>>> import requests>>> payload = {"key":"value"}>>> r = requests.post("http://pythontab.com/postTest", data = json.dumps(payload))

Here is no longer too much to explain, Baidu is the best answer.

Welcome attention:

python# common modules and simple usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.