Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

using python.ly in a python script

14 views
Skip to first unread message

Stefan Thomas

unread,
Dec 12, 2023, 8:50:08 AM12/12/23
to fresc...@googlegroups.com
Dear community,
I'm trying to use the python.ly package in a python package, but without success.
I' tried it with:
#!/usr/bin/env python3
import ly
Music = "c d e f g"
Music = ly.pitch.transpose.Transposer("c","des",Music)
print(Music)
I get:
AttributeError: module 'ly' has no attribute 'pitch'

What's going wrong in my mind?
Thanks for your help!
Stefan

Jean Abou Samra

unread,
Dec 12, 2023, 8:54:58 AM12/12/23
to fresc...@googlegroups.com
You need

import ly.pitch

Importing a Python package doesn't automatically import all submodules (otherwise it could be very slow for huge packages with many modules).



Stefan Thomas

unread,
Dec 12, 2023, 9:20:04 AM12/12/23
to fresc...@googlegroups.com
Dear Jean,
thanks for your reply.
Unfortunately it still doesn't work.
I get now:
AttributeError: module 'ly.pitch' has no attribute 'transpose'




--
Frescobaldi homepage: http://www.frescobaldi.org/
Mailing list: http://groups.google.com/group/frescobaldi
Issue tracker: https://github.com/frescobaldi/frescobaldi/issues
---
You received this message because you are subscribed to the Google Groups "Frescobaldi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to frescobaldi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/frescobaldi/13E53C34-BAE8-4874-A084-623FAA9296B8%40abou-samra.fr.

Peter Bjuhr

unread,
Dec 13, 2023, 1:52:20 PM12/13/23
to fresc...@googlegroups.com
This is something I've been using:

import ly.document
from ly.pitch import transpose

def transposeMusic(music, fromstr, tostr, lang='english'):
def pitchFromStr(pstr):
readp = ly.pitch.pitchReader(lang)(pstr.replace("'",
'').replace(',', ''))
return ly.pitch.Pitch(*readp, octave=ly.pitch.octaveToNum(pstr))
fromPitch = pitchFromStr(fromstr.lower())
toPitch = pitchFromStr(tostr.lower())
transposer = transpose.Transposer(fromPitch, toPitch)
doc = ly.document.Document("{" + music + "}")
cursor = ly.document.Cursor(doc)
transpose.transpose(cursor, transposer, language=lang)
transposed = doc.plaintext()
return transposed[1:-1]

Best Peter
> To view this discussion on the web visit https://groups.google.com/d/msgid/frescobaldi/CAH-WrMHx%2BOHT_ZTNNN2pjWrKORFzfxYG-xK9mA3EhR4nzE%3D5QQ%40mail.gmail.com.

Stefan Thomas

unread,
Dec 14, 2023, 1:44:00 AM12/14/23
to fresc...@googlegroups.com
Dear Peter,
thanks for Your reply. Your code works nicely!

Peter Bjuhr

unread,
Dec 14, 2023, 3:53:08 PM12/14/23
to fresc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages