Skip to content

Instantly share code, notes, and snippets.

@gelendir
Created April 10, 2015 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gelendir/6959869ac6ad106921ba to your computer and use it in GitHub Desktop.
Save gelendir/6959869ac6ad106921ba to your computer and use it in GitHub Desktop.
Show time under musical notation form
from __future__ import unicode_literals
from datetime import datetime
WHOLE_NOTE = "\U0001D15D"
HALF_NOTE = "\U0001D15E"
QUARTER_NOTE = "\U0001D15F"
EIGHTH_NOTE = "\U0001D160"
WHOLE_REST = "\U0001D13B"
HALF_REST = "\U0001D13C"
QUARTER_REST = "\U0001D13D"
EIGHTH_REST = "\U0001D13E"
SIMPLE_BAR = "\U0001D100"
DOUBLE_BAR = "\U0001D101"
START_BAR = "\U0001D103"
END_BAR = "\U0001D102"
G_CLEF = "\U0001D11E"
DOT = "\U0001D16D"
FACTORS = {
12: (WHOLE_NOTE + DOT, WHOLE_REST + EIGHTH_REST),
6: (HALF_NOTE + DOT, HALF_REST + EIGHTH_REST),
3: (QUARTER_NOTE + DOT, QUARTER_REST + EIGHTH_REST),
2: (QUARTER_NOTE, QUARTER_REST),
1: (EIGHTH_NOTE, EIGHTH_REST),
}
CLOCK = {
0: QUARTER_REST,
1: QUARTER_NOTE,
2: HALF_NOTE,
3: HALF_NOTE + QUARTER_NOTE,
4: WHOLE_NOTE,
5: WHOLE_NOTE + QUARTER_NOTE,
6: WHOLE_NOTE + HALF_NOTE,
7: WHOLE_NOTE + HALF_NOTE + QUARTER_NOTE,
8: WHOLE_NOTE + WHOLE_NOTE,
9: WHOLE_NOTE + WHOLE_NOTE + QUARTER_NOTE,
10: WHOLE_NOTE + WHOLE_NOTE + HALF_NOTE,
11: WHOLE_NOTE + WHOLE_NOTE + HALF_NOTE + QUARTER_NOTE,
12: WHOLE_NOTE + WHOLE_NOTE + WHOLE_NOTE,
}
NUMBER_FACTORS = sorted(FACTORS.keys())
NOTE_FACTORS = {k: v[0] for k, v in FACTORS.items()}
REST_FACTORS = {k: v[1] for k, v in FACTORS.items()}
def measure(note_number):
rest_number = 12 - note_number
notes = "".join(NOTE_FACTORS[f] for f in decompose(note_number))
rests = "".join(REST_FACTORS[f] for f in decompose(rest_number))
return notes + rests
def decompose(number):
while number > 0:
higest = max(f for f in NUMBER_FACTORS if f <= number)
yield higest
number -= higest
def adjust_numbers(hours, minutes, seconds):
hours = hours - 12 if hours > 12 else hours
minutes = minutes // 5
seconds = seconds // 5
return hours, minutes, seconds
def additive_clock(hours, minutes, seconds):
hours, minutes, seconds = adjust_numbers(hours, minutes, seconds)
return CLOCK[hours], CLOCK[minutes], CLOCK[seconds]
def measure_clock(hours, minutes, seconds):
hours, minutes, seconds = adjust_numbers(hours, minutes, seconds)
return measure(hours), measure(minutes), measure(seconds)
if __name__ == "__main__":
now = datetime.now()
bars = SIMPLE_BAR.join(measure_clock(now.hour, now.minute, now.second))
print (G_CLEF + " " + START_BAR + bars + END_BAR).encode('utf8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment