-2

I am working on a sheet music app and now I need to create a list (or whatever will let me do this) that will store all of this info (see below) as one item and then print it out or better yet insert it into my code to be manipulated by a bunch of functions...

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

All that prints out something like this...

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

Which when hardcoded into my code goes through these class functions and print out an Eighth note onto my sheet music....

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

So my next step is to make a list or something that stores this...

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

... as one item and then I have to make a function that takes all the items in that list and somehow inserts them into my code seeing as just printing them out won't do anything.
Ok so I tried this which doesn't solve the whole problem but would definitely get me a lot closer BUT it doesn't work and I don't know why. I tested it all out in a separate file because that's easier and there are no errors or anything

Note on Sheet Music IMAGE

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I've added some basic formatting and inlined your image. Please [edit] your question and paste your code as _text_ instead of posting a screenshot. You can select it and press Ctrl+K or click the `{}` button to format it properly. – ChrisGPT was on strike Jul 29 '17 at 01:50
  • I tried. The {} button and Ctrl K dont do anything and it just says the code isnt formatted correctly because it isnt intended even though it is. – Tyler Bloom Jul 29 '17 at 01:55
  • @Chris OK!! It finally worked! I put the code in normally – Tyler Bloom Jul 31 '17 at 02:09
  • It's ususally not really appropriate to remove an original question to replace it with something different. If you have a second question, submit it separately. As it is, this question doesn't make any sense. You don't explain what you're trying to do, nor clearly say what's going wrong. You've also failed to tag the question with the programming language you're using (which is far more important than the fact that you're using loops-almost all code does). – Blckknght Jul 31 '17 at 02:49
  • @Blckknght I created a new question which is hopefully easier to understand. https://stackoverflow.com/questions/45408813/function-with-input-prints-out-nothing – Tyler Bloom Jul 31 '17 at 06:29

1 Answers1

0

If I understand you (and yes I read your alternate quesion but I like this one better) you want a text representation of your notation that you can easily convert into and out of. If that's correct, then I suggest something like jsonpickle which would roughly look like:

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

OUTPUT

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

(Then a bunch of errors since I don't have Pygame loaded or whatever you're using to do your screen.blit()).

You can customize jsonpickle to decide what/how things are encoded from a JSON perspective.

cdlane
  • 40,441
  • 5
  • 32
  • 81