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

Python learning notes_ Day09

編輯:Python

Sort the list

  • List sorted sort The method has a name key Parameters of
  • Parameters key Request to pass in a function , This function processes each item in the list , The result of processing is used as the basis for sorting
>>> alist
[('172.40.58.150', 10), ('172.40.58.124', 6), ('172.40.58.101', 10), ('127.0.0.1', 121), ('192.168.4.254', 103), ('192.168.2.254', 110), ('201.1.1.254', 173), ('201.1.2.254', 119), ('172.40.0.54', 391), ('172.40.50.116', 244)]
>>> def last(item):
... return item[-1]
>>> alist.sort(key=last)
>>> alist
[('172.40.58.124', 6), ('172.40.58.150', 10), ('172.40.58.101', 10), ('192.168.4.254', 103), ('192.168.2.254', 110), ('201.1.2.254', 119), ('127.0.0.1', 121), ('201.1.1.254', 173), ('172.40.50.116', 244), ('172.40.0.54', 391)]

python Package management

python Official module file site :https://pypi.org/

Install the module :

pip install Module name

Because the installation uses a foreign site , It may be slow . Domestic mirror sites can be used

# mkdir -p ~/.pip
# vim ~/.pip/pip.conf
[global]
index-url = http://pypi.douban.com/simple/
[install]
trusted-host=pypi.douban.com

install wget

(nsd1903) [[email protected] day04]# pip install wget

Local installation

# ls /linux-soft/05
zzg_pypkgs.tar.gz
# tar xzf /linux-soft/05/zzg_pypkgs.tar.gz -C ~
# pip install pymysql_pkgs/*

Start virtual machine , install mariadb-server And start the .

Create a nsd1903 The database of , And authorize the physical host to access .

Create database

Create a database for a small company , It is used to record the basic information of employees and their salary payment .

full name 、 Gender 、 Position 、 department 、 Contact information 、 Date of birth 、 Wage day 、 Basic salary 、 Bonus 、 Paid in wages

Relational database , Data redundancy should be minimized . To eliminate redundancy , You can put data into different tables .

The employee table : full name 、 Gender 、 Position 、 department 、 Contact information 、 Date of birth

payroll : full name 、 Wage day 、 Basic salary 、 Bonus 、 Paid in wages

The paradigm of relational data

  • The so-called first paradigm (1NF) In a relational model , A specification requirement for domain addition , All domains should be atomic , That is, each column of the database table is an indivisible atomic data item
  • The contact information can be divided into :email / Phone number / Home address
  • Second normal form (2NF) It's in the first paradigm (1NF) Based on , That is, it meets the second norm (2NF) We must first satisfy the first paradigm (1NF). Second normal form (2NF) It is required that each instance or record in the database table can be uniquely distinguished . Simply speaking , You need a primary key .
  • The employee table needs to add employees ID A primary key
  • It is not appropriate to use any primary key for the payroll , You can add a separate id A primary key
  • The third paradigm is that attributes do not depend on other non primary attributes , That is to say, to satisfy 2NF On the basis of , Any non primary property must not be passed dependent on the primary property . Non primary attributes cannot depend on other non primary attributes .
  • In the payroll , The actual salary depends on the basic salary and bonus , It should not appear in the table , When needed , Calculate temporarily through the program .

The final table :

The employee table : staff ID、 full name 、 Gender 、 Position 、 department ID、email、phone、 Date of birth

Departmental table : department ID、 Department name

payroll :id、 staff ID、 Wage day 、 Basic salary 、 Bonus

pymysql Use of modules

  1. Create a connection to the database
  2. Create a database for operation ( surface ) The cursor of , It is equivalent to the file object opened when operating the file
  3. Execute... By cursor sql sentence
  4. If it involves changes to the database , You need to perform commit
  5. Close cursor 、 Close the connection
# Create database
MariaDB [(none)]> CREATE DATABASE nsd1903 DEFAULT CHARSET utf8;

sqlalchemy

(nsd1903) [[email protected] day04]# pip install sqlalchemy_pkgs/SQLAlchemy-1.2.14.tar.gz

ORM: Object relation mapping

  • Object: object , Corresponding python Of class
  • Relationship: Relationship , Corresponding to relational database
  • Mapping: mapping
  • hold sqlalchemy Classes in are associated with tables
  • Associate variables in a class with fields in a table
  • Associate an instance of a class with a record of a table
  • Each field in the table is associated with sqlalchemy Of Column Class association
  • The type of the field is the same as sqlalchemy Related class associations
MariaDB [nsd1903]> CREATE DATABASE tedu1903 DEFAULT CHARSET utf8;

When adding, deleting, modifying, and querying a database , You need to create a session .


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