J'ajoute beaucoup de commentaires; partout. J'ai pris le temps d'écrire ce qui se passe sous une forme lisible par l'homme pour chaque ligne de mon algorithme de génération musicale de 500 lignes, et cela m'a sauvé beaucoup de temps, ainsi que mes autres amis qui aidaient.
Voici ce que je l'ai fait (comme départ):
def __init__(self):
self.chromatic = ['C', ['C#', 'Db'], 'D', ['D#', 'Eb'], 'E', 'F', ['F#', 'Gb'], 'G', ['G#', 'Ab'], 'A', ['A#', 'Bb'], 'B']
self.steps = {}
self.steps['major'] = [2, 2, 1, 2, 2, 2, 1]
self.steps['natural minor'] = [2, 1, 2, 2, 1, 2, 2]
self.steps['harmonic minor'] = [2, 1, 2, 2, 1, 3]
self.steps['melodic minor up'] = [2, 1, 2, 2, 2, 2, 1]
self.steps['melodic minor down'] = [2, 2, 1, 2, 2, 1, 2]
self.steps['dorian'] = [2, 1, 2, 2, 2, 1, 2]
self.steps['mixolydian'] = [2, 2, 1, 2, 2, 1, 2]
self.steps['ahava raba'] = [1, 3, 1, 2, 1, 2, 2]
self.steps['minor penatonic blues'] = [3, 2, 2, 3, 2]
self.list = []
def scale(self, note, name): # Function to generate a scale from the required base note.
if re.sub('[^0-9]', '', note) == '': # Checks for nonexistent octave number
octave = 5 # Defaults to 5
else: # If octave number exists
octave = int(re.sub('[^0-9]', '', note)) # Extracts octave number from note
note = re.sub('[0-9]', '', note) # Strips all numbers from note
scale = [] # Initializes the scale to be empty
for i in rlen(self.chromatic): # Loops through all elements of the Chromatic scale
if self.chromatic[i] is not list: # If the note is just a natural
if note == self.chromatic[i]: scale = [i + 1] # Check if the note is in the chromatic. If it is, add it.
else:
if note in self.chromatic[i]: scale = [i + 1] # If the note is in a key of the chromatic, add it too. It is a sharp/flat.
for i in rlen(self.steps[name]): # Loops through desired scale
scale.append(self.steps[name][i] + scale[i]) # Adds string notes following the algorithm of the scale
scale[i + 1] = scale[i + 1] % len(self.chromatic) # Modulo length of Chromatic scale
Il est un début (et un exemple avec le code cruddy), mais il me aide du code de débogage vraiment rapidement.
Je préfère créer plus de commentaires sémantiquement pertinents dans le code. IOW, il est évident que if() vérifie si foo est 2. Mais il n'est pas évident que (par exemple) il vérifie si "les deux boîtes ont été vérifiées à l'écran", ou quel que soit foo représente. – Dogweather
Oui, c'est juste un exemple rapide. Laissez-moi poster un vrai code ... – Blender
ne l'écoutez pas, il écrit python! ;) – iain