【问题标题】:Creating a list of functions?创建函数列表?
【发布时间】:2017-07-31 14:47:42
【问题描述】:

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

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()')

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

【问题讨论】:

  • 我添加了一些基本格式并内联了您的图像。请edit您的问题并将您的代码粘贴为 text 而不是发布屏幕截图。您可以选择它并按 Ctrl+K 或单击 {} 按钮以正确格式化。
  • 我试过了。 {} 按钮和 Ctrl K 不做任何事情,它只是说代码格式不正确,因为它不是预期的,即使它是。
  • @Chris 好!!它终于奏效了!我把代码正常放进去
  • 通常不适合删除原始问题以用不同的内容替换它。如果您有第二个问题,请单独提交。事实上,这个问题没有任何意义。你没有解释你想要做什么,也没有清楚地说明出了什么问题。您也未能使用您正在使用的编程语言标记问题(这比您使用循环这一事实更重要 - 几乎所有代码都这样做)。
  • @Blckknght 我创建了一个新问题,希望它更容易理解。 stackoverflow.com/questions/45408813/…

标签: python loops while-loop


【解决方案1】:

如果我理解你的意思(是的,我读了你的替代问题,但我更喜欢这个问题),你想要一个可以轻松转换成和转换出的符号的文本表示。如果那是正确的,那么我建议像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 角度编码什么/如何编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    相关资源
    最近更新 更多