frescobaldi / frescobaldi

Frescobaldi LilyPond Editor
http://www.frescobaldi.org/
GNU General Public License v2.0
744 stars 154 forks source link

Suggestion: Roman numerals support #138

Open uliska opened 11 years ago

uliska commented 11 years ago

I would like to add support for roman numerals because that's a common idiom for indexing variable names without arabic numbers.

I have added a basic module with the conversion routines int_to_roman() and roman_to_int() to my fork (https://github.com/uliska/frescobaldi), and invite anybody who is interested in this to pull the romannumerals branch to check it out.

Create a new snippet with this text (updated incorporating the later commits):

-*- python; selection: yes, keep;

from romannumerals import int_to_roman

number = int(text)
newtext = []

for i in range(1, number + 1):
  newtext.append(int2roman(i) + ' = \\relative c\' {\n\n}\n\n')

text = newtext

enter and select a (preferrably small) positive integer in an empty document and run the snippet.

This is only a demonstration to get the idea, there is no error checking etc.


Before continuing I would like to get some feedback if it's interesting and worth the effort. And I'd like to get some ideas/suggestions for functions to be implemented.

As a start here are a few ideas:

wbsoft commented 11 years ago

A good idea indeed. As of the implementation, there is already frescobaldi_app/ly/util.py which has int2text, int2roman and int2letter. The score wizard also uses this functionality. Maybe it is nice to add the conversion routine roman2int there.

uliska commented 11 years ago

Oops. Good that I didn't implement this myself :-) I'm afraid such things will happen more often because of course I don't know the sources intimately ...

For now I'll keep the stuff in my additional module to see where it leads to (and how much it will become). In the end I can incorporate it in an existing file or not.

Have removed int_to_roman and renamed roman_to_int into roman2int. Unfortunately there are now two parallel structures _roman_numerals (in util.py) and _roman_letters (in romannumerals), but I have the impression that merging them would make one of the two algorithms much more complicated.

Also added a few helper routines.

uliska commented 11 years ago

Example snippet (working with the latest commits from romannumerals):

-*- python; selection: yes;

from romannumerals import *

base, index = romansuffix2int(text)
if index >= 0:
    text = base + int2roman(index+1)

select a variable name like melodyII and run the snippet. Also test with melody and melodyXD (invalid numeral).

This isn't a real-world application but an example where this could be going.