python基礎之模塊,面向對象

hashnode

什麼是hash?python

         hash是一種算法,該算法接受傳入的內容,通過運算獲得一串hash值web

爲什麼用hash?算法

         hash值有三大特性:編程

         1.只要傳入的內容同樣,獲得的hash值必然同樣ide

         2.只要使用的hash算法固定,不管傳入的內容多大,獲得的hash值的長度是固定的加密

         3.不能夠用hash值逆推出來的內容spa

         基於1.2能夠用於下載文件時作一致性校驗等設計

         基於2.3能夠用於密碼的加密等code

如何使用hash

         import hashlib

         m = hashlib.md5()  # 至關於造工廠

         m.update(‘hello’.encode(‘utf-8’))  # 至關於送原料

         m.hexdigest()  # 至關於產出產品

 

shelve

import shelve  # 序列化,將內容以字典的形式存入文件
f=shelve.open(r'sheve.txt')
# f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
# f['stu2_info']={'name':'gangdan','age':53}
# f['school_info']={'website':'http://www.pypy.org','city':'beijing'}
print(f['stu1_info']['hobby'])
f.close()

 

xml

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
xml源碼

python中操做

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
 
#遍歷xml文檔
for child in root:
    print('========>',child.tag,child.attrib,child.attrib['name'])
    for i in child:
        print(i.tag,i.attrib,i.text)
 
#只遍歷year 節點
for node in root.iter('year'):
    print(node.tag,node.text)
#---------------------------------------

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter('year'):
    new_year=int(node.text)+1
    node.text=str(new_year)
    node.set('updated','yes')
    node.set('version','1.0')
tree.write('test.xml')
 
 
#刪除node
for country in root.findall('country'):
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)
 
tree.write('output.xml')

configparser

[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31

[section2]
k1 = v1
ini文件

python中讀取

import configparser

config=configparser.ConfigParser()
config.read('a.cfg')

#查看全部的標題
res=config.sections() #['section1', 'section2']
print(res)

#查看標題section1下全部key=value的key
options=config.options('section1')
print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

#查看標題section1下全部key=value的(key,value)格式
item_list=config.items('section1')
print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

#查看標題section1下user的值=>字符串格式
val=config.get('section1','user')
print(val) #egon

#查看標題section1下age的值=>整數格式
val1=config.getint('section1','age')
print(val1) #18

#查看標題section1下is_admin的值=>布爾值格式
val2=config.getboolean('section1','is_admin')
print(val2) #True

#查看標題section1下salary的值=>浮點型格式
val3=config.getfloat('section1','salary')
print(val3) #31.0

改寫
import configparser

config=configparser.ConfigParser()
config.read('a.cfg',encoding='utf-8')


#刪除整個標題section2
config.remove_section('section2')

#刪除標題section1下的某個k1和k2
config.remove_option('section1','k1')
config.remove_option('section1','k2')

#判斷是否存在某個標題
print(config.has_section('section1'))

#判斷標題section1下是否有user
print(config.has_option('section1',''))


#添加一個標題
config.add_section('egon')

#在標題egon下添加name=egon,age=18的配置
config.set('egon','name','egon')
config.set('egon','age',18) #報錯,必須是字符串

#最後將修改的內容寫入文件,完成最終的修改
config.write(open('a.cfg','w'))

面向對象編程

面向對象編程的核心是對象二字,對象是特徵與技能的結合體。

優勢:擴展性強

缺點:複雜程度要高於面向過程

應用場景:需求常常變化的軟件,通常需求的變化都集中在用戶層,互聯網應用,企業內部軟件,遊戲等都是面向對象的程序設計大顯身手的好地方

 

類:種類,分類,類別

對象是特徵與技能的結合體,類是一系列對象類似的特徵與技能的結合體

強調:站的角度不一樣,總結出的類大相徑庭

現實中:先有對象,再有類

程序中:必須先定義類,後調用類來產生對象

定義:類體代碼會在定義階段馬上執行,產生類名稱空間

class OldboyStudent:
    school='oldboy'
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
    def learn(self):
        print('%s is learning' %self.name) #新增self.name
    def eat(self):
        print('%s is eating' %self.name)
    def sleep(self):
        print('%s is sleeping' %self.name)
s1=OldboyStudent('李坦克','',18)
s2=OldboyStudent('王大炮','',38)
s3=OldboyStudent('牛榴彈','',78)

調用類產生對象,調用類的過程,又稱爲實例化,結果稱爲對象/實例

相關文章
相關標籤/搜索