创建函数列表?

2024-04-18 22:21:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个乐谱应用程序,现在我需要创建一个列表(或者任何可以让我这样做的东西),将所有这些信息(见下文)存储为一个项目,然后打印出来,或者更好地将其插入到我的代码中,由一系列函数操作。。。你知道吗

print('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()')

所有打印出来的都是这样的东西。。。你知道吗

Note(8, '4R', 4, 'c', 'Ethnote').ExNote()

当硬编码到我的代码中时,它会遍历这些类函数并将第八个音符打印到我的乐谱上。。。。你知道吗

class Note:
    def __init__(self, Num, staff, measure, note, notetype):
        self.staff = staff
        self.measure = measure
        self.note = note
        self.notetype = notetype
        self.Num = Num
    def Wmeasure(self):
        return (self.measure-1)*153

    def Wnotetype(self):
        if self.notetype == 'Ethnote':
            X= {'1':x+5, '2':x+22, '3':x+39, '4':x+56, '5':x+73, '6':x+90, '7':x+107, '8':x+124}
        elif self.notetype == 'Fourthnote':
            X={'1':x+19, '2':x+50, '3':x+81, '4':x+112}
        elif self.notetype == 'Halfnote':
            X={'1':x+39, '2':x+90}
        elif self.notetype == 'note1':
            X={'1':x+64, '2': x+64}
        return X[str(self.Num)]
    def Wnote(self):
        YL={'b': y+76, 'a': y+80, 'g':y+84, 'f':y+88, 'e':y+92, 'd':y+96, 'c':y+100, 'b2':y+104, 'a2':y+108, 'a3': y+112}
        YR= {'c': 62, 'd': 58, 'e': 54, 'f': 50, 'g':46, 'a':42, 'b':38,
         'c2':34, 'd2':28 , 'e2':24, 'f2':20, 'g2':16, 'a2':12, 'b2':8, 'c3':4, 'd3':0}
        if self.staff in ['1L', '2L', '3L', '4L']:
        #self.staff == '1L': # or '2L' or '3L' or '4L':
            return YL[self.note] #+ self.Wstaff()
        else: #if self.staff == '1R' or '2R' or '3R' or '4R':
            return YR[self.note] #+ self.Wstaff()
    def Wstaff(self):
        if self.staff in ['1L', '1R']:
            j = 0
        elif self.staff in ['2L', '2R']:
            j = 160
        elif self.staff in ['3L', '3R']:
            j = 320
        elif self.staff in ['4L', '4R']:
            j = 480
        return j
    def getcoord(self):
        return (self.Wmeasure() + self.Wnotetype()), (self.Wstaff() + self.Wnote())
    def ExNote(self):
        if self.notetype == 'Ethnote':
            screen.blit(EthnoteIMG, self.getcoord())
        elif self.notetype == 'Fourthnote':
            screen.blit(FourthnoteIMG, self.getcoord())
        elif self.notetype == 'Halfnote':
            screen.blit(HalfnoteIMG, self.getcoord())
        elif self.notetype == 'note1':
            screen.blit(note1IMG, self.getcoord())

所以我的下一步是列一个清单或者其他什么东西来储存这个。。。你知道吗

('Note' + '(' + str(wnote) + ', ' + repr(staff) + ', ' + str(measure) + ', ' + repr(note) + ', ' + repr(notetype) + ')' + '.ExNote()')

。。。作为一个项目,然后我必须创建一个函数,以获取该列表中的所有项目,并以某种方式将它们插入到我的代码中,因为仅仅将它们打印出来不会起任何作用。
好的,所以我试了这个方法,虽然不能解决整个问题,但肯定能让我更接近,但它不起作用,我也不知道为什么。我在一个单独的文件中进行了测试,因为这样更容易,而且没有任何错误

Note on Sheet Music IMAGE


Tags: orinselfreturnifdefnotestaff
1条回答
网友
1楼 · 发布于 2024-04-18 22:21:17

如果我理解你(是的,我读了你的另一个问题,但我更喜欢这个),你想要一个文本表示你的符号,你可以很容易地转换和退出。如果这是正确的,那么我建议使用类似jsonpickle的方法,大致如下所示:

from note import Note
import jsonpickle

note = Note(8, '4R', 4, 'c', 'Ethnote')

text = jsonpickle.encode(note)

print(text)

object = jsonpickle.decode(text)

object.ExNote()

输出

> python3 test.py
{"py/object": "note.Note", "Num": 8, "measure": 4, "note": "c", "notetype": "Ethnote", "staff": "4R"}
...

(然后是一堆错误,因为我没有加载Pygame或者任何你用来做screen.blit()的东西)。你知道吗

您可以定制jsonpickle来决定从JSON的角度来编码什么/如何编码。你知道吗

相关问题 更多 >