最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python開(kāi)發(fā)任意表達(dá)式求值全功能示例

 更新時(shí)間:2022年07月12日 11:44:52   作者:圣手書(shū)生肖讓  
這篇文章主要為大家介紹了python開(kāi)發(fā)任意表達(dá)式求值全功能示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

在之前的基礎(chǔ)上進(jìn)一步實(shí)現(xiàn)了全功能表達(dá)式求值。

  • 已支持浮點(diǎn)數(shù)
  • 已支持字符串的處理,前加一個(gè)"(類(lèi)似lisp語(yǔ)法)
  • 支持減號(hào)/負(fù)號(hào),一符兩用機(jī)制
  • 支持所有算術(shù)運(yùn)算符,包括**,//, %
  • 支持全部7個(gè)比較運(yùn)算符
  • 支持與或非3個(gè)邏輯運(yùn)算符
  • 支持自定義數(shù)學(xué)函數(shù)(代碼中預(yù)設(shè)sin函數(shù)作為示范)
  • 支持外部提供的變量機(jī)制
  • 支持外部設(shè)置函數(shù)(代碼中預(yù)設(shè)isvar函數(shù)作為示范)
  • 支持列表
  • 字典的支持,體現(xiàn)在外部的變量中
  • 結(jié)構(gòu)清晰,易于擴(kuò)展
  • 具有實(shí)用性及學(xué)習(xí)性

與其說(shuō)距離DSL只有一步之遙,不如說(shuō),DSL機(jī)制已經(jīng)實(shí)現(xiàn)。因?yàn)榭梢匀我鈹U(kuò)展函數(shù),而函數(shù)的內(nèi)容

完全可以自行定義。

所以共享給大家,歡迎意見(jiàn)和建議。

完整的源代碼

import math
opDict={}
def addoptr(ch, outLev, inLev, func, parmNum=2):
    obj= {'name':ch, 'out':outLev, 'in':inLev, 'func':func, 'parmNum':parmNum}
    opDict[ch]= obj
def makeList(x):
    if isinstance(x[-2], list):
        x[-2].append(x[-1])
        return x[-2].copy()
    else:
        ret= []
        ret.append(x[-2])
        ret.append(x[-1])
        return ret
addoptr('#', 1, 1, None)
addoptr('(', 90, 2, None)
addoptr(')', 2, None, None)
addoptr('[', 90, 2, None)
addoptr(']', 2, 2, None)
addoptr(',', 8, 9, makeList)
addoptr('&', 13, 14, lambda x: x[-1] and x[-2])
addoptr('and', 13, 14, lambda x: x[-1] and x[-2])
addoptr('|', 11, 12, lambda x: x[-1] or x[-2])
addoptr('or', 11, 12, lambda x: x[-1] or x[-2])
addoptr('~', 16, 17, lambda x: not x[-1],1)
addoptr('not', 16, 17, lambda x: not x[-1],1)
addoptr('=', 22, 23, lambda x: x[-1]==x[-2])
addoptr('>', 22, 23, lambda x: x[-2]>x[-1])
addoptr('<', 22, 23, lambda x: x[-2]<x[-1])
addoptr('>=', 22, 23, lambda x: x[-2]>=x[-1])
addoptr('<=', 22, 23, lambda x: x[-2]<=x[-1])
addoptr('!=', 22, 23, lambda x: x[-2]!=x[-1])
addoptr('<>', 22, 23, lambda x: x[-2]!=x[-1])
addoptr('in', 22, 23, lambda x: x[-2] in x[-1])
addoptr('+', 31, 32, lambda x: x[-2]+x[-1])
addoptr('-', 31, 32, lambda x: x[-2]-x[-1])
addoptr('*', 41, 42, lambda x: x[-2]*x[-1])
addoptr('/', 41, 42, lambda x: x[-2]/x[-1])
addoptr('//', 41, 42, lambda x: x[-2]//x[-1])
addoptr('%', 41, 42, lambda x: x[-2]%x[-1])
addoptr('neg', 51, 52, lambda x: -x[-1],1)
addoptr('**', 55, 56, lambda x: x[-2]**x[-1])
addoptr('sin', 61, 62, lambda x: math.sin(x[-1]),1)
alphabet= [chr(ord('a')+x) for x in range(26)]+[chr(ord('A')+x) for x in range(26)]
# print(opChar)
# print(opSep)
# print(alphabet)
def isfloat(str1):
    try:
        number = float(str1)
    except ValueError:
        return False
    return True
class exprEngine:
    def __init__(this, isVar=None, getValue=None):
        this.opndStack=[]
        this.optrStack=[]
        this.isVar= isVar
        this.getValue= getValue
        # 這個(gè)狀態(tài),特為負(fù)號(hào)/減號(hào)這一特殊符的雙含義號(hào)所設(shè)置
        this.negState=0
        # 內(nèi)建函數(shù)
        if isVar:
            addoptr('isvar', 61, 62, lambda x: isVar(x[-1]),1)
        # 處理識(shí)別
        this.oplen= len(max(opDict, key=lambda x:len(x)))
        this.opChar=[]
        for i in range(this.oplen):
            tmp=[x[0:i+1] for x in opDict if len(x)>=i+1]
            this.opChar.append(tmp)
        this.opSep= [x[0] for x in opDict if x[0] not in alphabet]+[' ', '\t']
        print(this.oplen)
        print(this.opChar)
        print(this.opSep)
    def readWord(this, cond):
        cond= cond.strip()
        if cond=='':
            return '', '#'
        if cond[0] in this.opChar[0]:
            l1=this.oplen
            for i in range(this.oplen):
                if cond[:i+1] not in this.opChar[i]:
                    l1= i
                    break
            print(l1)
            if cond[:l1] in this.opChar[l1-1]:
                return cond[:l1], 'optr'
        part= ''
        for ch in cond:
            if ch in this.opSep:
                break
            part+=ch
        return part, 'opnd'
    def pushoptr(this, optr):
        # 對(duì)負(fù)號(hào)/減號(hào)的特殊處理
        if optr=='-' and this.negState==0:
            # 這種情況,實(shí)際的含義是負(fù)號(hào)
            optr= 'neg'
        op= opDict[optr].copy()
        if len(this.optrStack)==0:
            this.optrStack.append(op)
            return
        opTop= this.optrStack[-1]
        if op['out']> opTop['in']:
            this.optrStack.append(op)
        elif op['out']< opTop['in']:
            this.popoptr()
            # 這里遞歸
            this.pushoptr(optr)
        elif op['out']== opTop['in']:
            # 消括號(hào)對(duì),簡(jiǎn)單彈出
            this.optrStack.pop()
        this.negState=0
    def popoptr(this):
        opTop= this.optrStack[-1]
        a= opTop['parmNum']
        if len(this.opndStack)<a:
            raise Exception('操作數(shù)不足,可能有語(yǔ)法錯(cuò)誤!')
        ret= opTop['func'](this.opndStack[-a:])
        this.opndStack= this.opndStack[:-a]
        this.opndStack.append(ret)
        this.optrStack.pop()
    def pushopnd(this, opnd):
        if opnd[0]=='"':
            # 肯定是字符串
            this.opndStack.append(opnd[1:])
        elif this.isVar and this.isVar(opnd):
            this.opndStack.append(this.getValue(opnd))
        else:
            if opnd.isdigit():
                this.opndStack.append(int(opnd))
            elif isfloat(opnd):
                this.opndStack.append(float(opnd))
            else:
                this.opndStack.append(opnd)
        this.negState=1
    def popopnd(this):
        if len(this.opndStack)==1:
            return this.opndStack[0]
        else:
            print(this.opndStack)
            print(this.optrStack)
            raise Exception('可能存在語(yǔ)法錯(cuò)誤。')
    def eval(this, cond):
        this.optrStack=[]
        this.opndStack=[]
        this.pushoptr('#')
        while True:
            aword,kind= this.readWord(cond)
            print(aword, cond)
            cond= cond[len(aword):].strip()
            if kind=='#':
                this.pushoptr('#')
                break
            elif kind=='optr':
                this.pushoptr(aword)
            else:
                if aword=='':
                    raise Exception('操作數(shù)為空,肯定有哪里錯(cuò)了。')
                this.pushopnd(aword)
            print(this.optrStack)
            print(this.opndStack)
        return this.popopnd()
if __name__=='__main__':
    # print(opDict)
    a= exprEngine()
    # a.addInfo('水位', '低')
    # b= a.eval('3 + 5 *2 = 13 and (3+5)*2=16 & 7-2 in [3,5,7] & 12>=15 or a in [a, b,c]')
    # b= a.eval('sin(-1)<1 and 3+-5=-2')
    # print(b)
    # b= a.eval('7*-3')
    b= a.eval('3**3=27 and 19%5=4 and 21//6=3')
    print(b)

以上就是python開(kāi)發(fā)任意表達(dá)式求值全功能示例的詳細(xì)內(nèi)容,更多關(guān)于python表達(dá)式求值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 實(shí)現(xiàn)把列表中的偶數(shù)變成他的平方

    Python 實(shí)現(xiàn)把列表中的偶數(shù)變成他的平方

    這篇文章主要介紹了Python 實(shí)現(xiàn)把列表中的偶數(shù)變成他的平方,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • Python tkinter之Bind(綁定事件)的使用示例

    Python tkinter之Bind(綁定事件)的使用示例

    這篇文章主要介紹了Python tkinter之Bind(綁定事件)的使用詳解,幫助大家更好的理解和學(xué)習(xí)python的gui開(kāi)發(fā),感興趣的朋友可以了解下
    2021-02-02
  • 使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序

    使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序

    Flask框架是一個(gè)以輕量級(jí)著稱(chēng)的Web開(kāi)發(fā)框架,近兩年來(lái)在Web領(lǐng)域獲得了極高的人氣,這里我們就來(lái)看如何使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序
    2016-06-06
  • Python實(shí)現(xiàn)豎排打印傳單手機(jī)號(hào)碼易撕條

    Python實(shí)現(xiàn)豎排打印傳單手機(jī)號(hào)碼易撕條

    這篇文章主要介紹了Python實(shí)現(xiàn)豎排打印傳單手機(jī)號(hào)碼易撕條,代碼非常簡(jiǎn)單,功能很實(shí)用,推薦給大家,有需要的小伙伴,參考下
    2015-03-03
  • 關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn)

    關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn)

    今天小編就為大家分享一篇關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python將鄰接矩陣輸出成圖的實(shí)現(xiàn)

    python將鄰接矩陣輸出成圖的實(shí)現(xiàn)

    今天小編就為大家分享一篇python將鄰接矩陣輸出成圖的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Python?GUI布局工具Tkinter入門(mén)之旅

    Python?GUI布局工具Tkinter入門(mén)之旅

    這篇文章主要為大家介紹了Python?GUI布局工具Tkinter的基礎(chǔ),Tkinter?作為?Python?的標(biāo)準(zhǔn)庫(kù),是非常流行的?Python?GUI?工具,同時(shí)也是非常容易學(xué)習(xí)的,今天我們就來(lái)開(kāi)啟?Tkinter的入門(mén)之旅
    2022-08-08
  • python flask sqlalchemy連接數(shù)據(jù)庫(kù)流程介紹

    python flask sqlalchemy連接數(shù)據(jù)庫(kù)流程介紹

    這篇文章主要介紹了python flask sqlalchemy連接數(shù)據(jù)庫(kù)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • Python代碼實(shí)現(xiàn)雙鏈表

    Python代碼實(shí)現(xiàn)雙鏈表

    這篇文章主要為大家詳細(xì)介紹了Python代碼實(shí)現(xiàn)雙鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Python中字典的setdefault()方法教程

    Python中字典的setdefault()方法教程

    在學(xué)習(xí)python字典操作方法時(shí),感覺(jué)setdefault()方法,比字典的其它基本操作方法更難理解的同學(xué)比較多,所以想著總結(jié)以下,下面這篇文章主要給大家介紹了Python中字典的setdefault()方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02

最新評(píng)論

兴宁市| 观塘区| 尼勒克县| 汉寿县| 东乌| 富川| 彭水| 沧州市| 周宁县| 万载县| 广安市| 农安县| 黄梅县| 蒙自县| 通化县| 桃园市| 山西省| 康平县| 宣化县| 阿鲁科尔沁旗| 成安县| 大连市| 盈江县| 台南市| 团风县| 乌鲁木齐市| 万载县| 青龙| 泗水县| 延吉市| 上高县| 怀集县| 梧州市| 海宁市| 新乡县| 嵊泗县| 泰来县| 威信县| 寿光市| 苏州市| 从江县|