473,503 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newbe's re question

All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from

http://python-forum.org/py/viewtopic.php?t=1703

thanks in advance the hole routine is down below..


Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3.  
  4. "takes an .csd input file and grabs instr_number instrument and
  5. creates output_File_Name"
  6. f = open (input_File_Name , 'r')                #opens file passed
  7. in to read
  8. f2 = open (output_File_Name, 'w')               #opens file passed
  9. in to write
  10. instr_yes = 'false'                             #set flag to false
  11.  
  12. for line in f:                                  #for through all
  13. the lines
  14. if "instr" in line:                           #look for instr in
  15. the file
  16. if instr_yes == 'true':                    #check to see if
  17. this ends the instr block
  18. break                                #exit the block
  19.  
  20. reline = re.line.split('instr', '/d$')     #error probily
  21. split instr and /d (decimal number into parts) $ for end of line
  22. number = int(reline[1])                  #convert to a
  23. number maybe not important
  24. if number == instr_number:            #check to see if
  25. it is the instr passed to function
  26. instr_yes = "true":                 #change flag to
  27. true because this is the instr we want
  28. if instr_yes = "true":                        #start of code to
  29. copy to another file
  30. f2.write(f.line)                         #write line to
  31. output file
  32.  
  33. f.close                                         #close input file
  34. f2.close
  35.  
  36.  
Sep 20 '06 #1
13 1700
Er*********@msn.com wrote:
All I am after realy is to change this

reline = re.line.split('instr', '/d$')
If you think this is the only problem in your code, think again; almost
every other line has an error or an unpythonic idiom. Have you read any
tutorial or sample code before typing this python-like pseudocode ?

George

Sep 20 '06 #2

George Sakkis wrote:
Er*********@msn.com wrote:
All I am after realy is to change this

reline = re.line.split('instr', '/d$')

If you think this is the only problem in your code, think again; almost
every other line has an error or an unpythonic idiom. Have you read any
tutorial or sample code before typing this python-like pseudocode ?

George
better to offend a python than a rattlesnake (you have to see the cwa
rattlesnake shirts shirts compared to the python shirts) :)

As long as it will run but I feel like I am just converting it to
something that feels like c somewhat but I am learning the commands
(hopefully).. hard to get a good chapters with questions at the end
without cash... I am happy even if the code isn't pythonic if it runs
with this.

import csoundroutines

extractCsdInstrument("bay-at-night.csd", "test.orc", 1)

http://www.dexrow.com

Sep 20 '06 #3
Hi Eric,

Don't let people offput you from learning python. It's a great
language, and is fun to use. But really, George does have a point -- if
you want to learn python, then the best way is to swim with the tide
rather than against it. But still, don't worry too much about being
pythonic and whatnot (many people would disagree with me here), just
write the code that _you_ (and your team) feel the most comfortable
with, and helps you be the most productive. But again, many of the
conventional pythonic ways to do things are such because people have
tested various ways and found those to be the best. So don't just blow
off convention, either. Find a happy medium between your coding style
and the conventions, and then you'll really become as productive as you
can be. That's my opinion. And only my opinion. But if it helps you
too, that's good. :)

Ps. Do have a look at the tutorials and other guides available, they
should help you out alot (e.g., the regexp tutorial [1]).

[1] http://www.amk.ca/python/howto/regex/

Regards,
Jordan

Sep 20 '06 #4
Er*********@msn.com wrote:
All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.

Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3.     "takes an .csd input file and grabs instr_number instrument and
  4. creates output_File_Name"
  5.     f = open (input_File_Name , 'r')
  6.     f2 = open (output_File_Name, 'w')
  7.     instr_yes = 'false' # Python has built-in booleans
  8.                                # Also, use sane naming: isInstr or hasInstr
  9.     for line in f:
  10.       if "instr" in line:
  11.            if instr_yes == 'true': # Use the bool -- `if hasInstr:`
  12.                break
  13.            reline = re.line.split('instr', '/d$')     # ???
  14.            number = int(reline[1])
  15.                 if number == instr_number:      # Doesn't need to be 2 lines
  16.                 instr_yes = "true":                 # the `:` is a syntax error
  17.       if instr_yes = "true":
  18.            f2.write(f.line) # odd way of doing it...
  19.     f.close()
  20.     f2.close()
  21.  
Did you read the HOWTO on regular expressions yet? It's included with
the standard documentation, but here's a link:

http://www.amk.ca/python/howto/regex/

Now, onto the issues:
1) The escape character is \, not /. Use "\d" to match a number.

2) I'm not sure I understand what you're trying to extract with your
regex. As it is, the expression will return a list containing one item,
a string identical to the line being matched up to, but not including,
the first number. The rest of the line will be discarded. Try Kiki, a
tool for testing regular expressions, before burying the expression in
your code.

3) Python has built-in booleans: True and False, no quotes. Use them so
that you don't get flamed on forums.

4) I don't claim to be a Python guru, but here's a massaged version of
the function you gave:

Expand|Select|Wrap|Line Numbers
  1.  
  2. def extractCsdInstrument (inputPath, outputPath, instrId):
  3. """ Takes a .csd file, grabs info for instrument ID and writes to
  4. file. """
  5. src = open(inputPath, 'r')
  6. dst = open(outputPath, 'w')
  7. for line in src:
  8. if "instr" in line:
  9. info_comment = line.split(";", 1) # [info, comment]
  10. # Extract integers
  11. instrInfo = re.split(r"[^0-9]*", info_comment[0])
  12. # Check the second number in the line
  13. if instrInfo[1] == str(instrId):
  14. dst.write(instrInfo.append(info_comment[1]))
  15. src.close()
  16. dst.close()
  17.  
  18.  
I didn't check if this actually works, since I don't know what a .csd
file looks like. There are of course other, better ways to do it, but
hopefully this leads you in the right direction.

5) Please, read the documentation that came with your Python
installation. Reading up on string methods etc. ahead of time will save
you much more time than trying to slug through it. Also consider using
a text editor with syntax highlighting; that will help catch most
obvious syntax errors during coding.

Sep 20 '06 #5
Er*********@msn.com wrote:
All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from

http://python-forum.org/py/viewtopic.php?t=1703

thanks in advance the hole routine is down below..


Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3.     "takes an .csd input file and grabs instr_number instrument and
  4. creates output_File_Name"
  5.     f = open (input_File_Name , 'r')                #opens file passed
  6. in to read
  7.     f2 = open (output_File_Name, 'w')               #opens file passed
  8. in to write
  9.     instr_yes = 'false'                             #set flag to false
  10.     for line in f:                                  #for through all
  11. the lines
  12.       if "instr" in line:                           #look for instr in
  13. the file
  14.            if instr_yes == 'true':                    #check to see if
  15. this ends the instr block
  16.                break                                #exit the block
  17.            reline = re.line.split('instr', '/d$')     #error probily
  18. split instr and /d (decimal number into parts) $ for end of line
  19.            number = int(reline[1])                  #convert to a
  20. number maybe not important
  21.                 if number == instr_number:            #check to see if
  22. it is the instr passed to function
  23.                 instr_yes = "true":                 #change flag to
  24. true because this is the instr we want
  25.       if instr_yes = "true":                        #start of code to
  26. copy to another file
  27.            f2.write(f.line)                         #write line to
  28. output file
  29.     f.close                                         #close input file
  30.     f2.close
  31.  

Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this

data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
Regards

Frederic

Sep 20 '06 #6

Frederic Rentsch wrote:
Er*********@msn.com wrote:
All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from

http://python-forum.org/py/viewtopic.php?t=1703

thanks in advance the hole routine is down below..


Expand|Select|Wrap|Line Numbers
  1.  def extractCsdInstrument (input_File_Name, output_File_Name,
  2.  instr_number):
  3.  
  4.      "takes an .csd input file and grabs instr_number instrument and
  5.  creates output_File_Name"
  6.      f = open (input_File_Name , 'r')                #opens file passed
  7.  in to read
  8.      f2 = open (output_File_Name, 'w')               #opens file passed
  9.  in to write
  10.      instr_yes = 'false'                             #set flag to false
  11.  
  12.      for line in f:                                  #for through all
  13.  the lines
  14.        if "instr" in line:                           #look for instr in
  15.  the file
  16.             if instr_yes == 'true':                    #check to see if
  17.  this ends the instr block
  18.                 break                                #exit the block
  19.  
  20.             reline = re.line.split('instr', '/d$')     #error probily
  21.  split instr and /d (decimal number into parts) $ for end of line
  22.             number = int(reline[1])                  #convert to a
  23.  number maybe not important
  24.                  if number == instr_number:            #check to see if
  25.  it is the instr passed to function
  26.                  instr_yes = "true":                 #change flag to
  27.  true because this is the instr we want
  28.        if instr_yes = "true":                        #start of code to
  29.  copy to another file
  30.             f2.write(f.line)                         #write line to
  31.  output file
  32.  
  33.      f.close                                         #close input file
  34.      f2.close
  35.  
  36.  
Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this

data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
Regards

Frederic
I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..

http://www.dexrow.com

Sep 20 '06 #7
Er*********@msn.com wrote:
Frederic Rentsch wrote:
>Er*********@msn.com wrote:
>>All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from

http://python-forum.org/py/viewtopic.php?t=1703

thanks in advance the hole routine is down below..


Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3.     "takes an .csd input file and grabs instr_number instrument and
  4. creates output_File_Name"
  5.     f = open (input_File_Name , 'r')                #opens file passed
  6. in to read
  7.     f2 = open (output_File_Name, 'w')               #opens file passed
  8. in to write
  9.     instr_yes = 'false'                             #set flag to false
  10.     for line in f:                                  #for through all
  11. the lines
  12.       if "instr" in line:                           #look for instr in
  13. the file
  14.            if instr_yes == 'true':                    #check to see if
  15. this ends the instr block
  16.                break                                #exit the block
  17.            reline = re.line.split('instr', '/d$')     #error probily
  18. split instr and /d (decimal number into parts) $ for end of line
  19.            number = int(reline[1])                  #convert to a
  20. number maybe not important
  21.                 if number == instr_number:            #check to see if
  22. it is the instr passed to function
  23.                 instr_yes = "true":                 #change flag to
  24. true because this is the instr we want
  25.       if instr_yes = "true":                        #start of code to
  26. copy to another file
  27.            f2.write(f.line)                         #write line to
  28. output file
  29.     f.close                                         #close input file
  30.     f2.close


Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this

data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
Regards

Frederic

I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..

http://www.dexrow.com

Eric,
I'm beginning to enjoy this. I'm sure we'll sort this out in no
time if we proceed methodically. Imagine you are a teacher and I am your
student. This is a quiz. I have to take it and you need to explain to me
the problem you want me to solve. If you don't explain it clearly, I
will not know what I have to do and cannot do the quiz. If you answer my
questions above, your description of the problem will be clear and I can
take the quiz. Okay?

Frederic


Sep 20 '06 #8

Frederic Rentsch wrote:
Er*********@msn.com wrote:
Frederic Rentsch wrote:
Er*********@msn.com wrote:

All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from

http://python-forum.org/py/viewtopic.php?t=1703

thanks in advance the hole routine is down below..


Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3.     "takes an .csd input file and grabs instr_number instrument and
  4. creates output_File_Name"
  5.     f = open (input_File_Name , 'r')                #opens file passed
  6. in to read
  7.     f2 = open (output_File_Name, 'w')               #opens file passed
  8. in to write
  9.     instr_yes = 'false'                             #set flag to false
  10.     for line in f:                                  #for through all
  11. the lines
  12.       if "instr" in line:                           #look for instr in
  13. the file
  14.            if instr_yes == 'true':                    #check to see if
  15. this ends the instr block
  16.                break                                #exit the block
  17.            reline = re.line.split('instr', '/d$')     #error probily
  18. split instr and /d (decimal number into parts) $ for end of line
  19.            number = int(reline[1])                  #convert to a
  20. number maybe not important
  21.                 if number == instr_number:            #check to see if
  22. it is the instr passed to function
  23.                 instr_yes = "true":                 #change flag to
  24. true because this is the instr we want
  25.       if instr_yes = "true":                        #start of code to
  26. copy to another file
  27.            f2.write(f.line)                         #write line to
  28. output file
  29.     f.close                                         #close input file
  30.     f2.close

Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this

data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
Regards

Frederic
I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..

http://www.dexrow.com

Eric,
I'm beginning to enjoy this. I'm sure we'll sort this out in no
time if we proceed methodically. Imagine you are a teacher and I am your
student. This is a quiz. I have to take it and you need to explain to me
the problem you want me to solve. If you don't explain it clearly, I
will not know what I have to do and cannot do the quiz. If you answer my
questions above, your description of the problem will be clear and I can
take the quiz. Okay?

Frederic

instr 1
a1 oscil p4, p5, 1 ; simple oscillator ; comment is
sometimes here
out a1
endin
I need to know the file I wan't to grab this from I need to grab this
out of the larger file and put it into it's own file, I need to know
what instr the user wants. I need to know what file to put it into and
it would be usefull to have the comment line returned (if any).

I did just get python essential reference 3rd edition.. If there is a
better reference on just the subject I am after I would be glad to grab
it when I get cash again.

Sep 20 '06 #9
Er*********@msn.com wrote:
Frederic Rentsch wrote:
>Er*********@msn.com wrote:
>>Frederic Rentsch wrote:
Er*********@msn.com wrote:
All I am after realy is to change this
>
reline = re.line.split('instr', '/d$')
>
into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from
>
http://python-forum.org/py/viewtopic.php?t=1703
>
thanks in advance the hole routine is down below..
>
>
>
>
>
>
Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3. >
  4.     "takes an .csd input file and grabs instr_number instrument and
  5. creates output_File_Name"
  6.     f = open (input_File_Name , 'r')                #opens file passed
  7. in to read
  8.     f2 = open (output_File_Name, 'w')               #opens file passed
  9. in to write
  10.     instr_yes = 'false'                             #set flag to false
  11. >
  12.     for line in f:                                  #for through all
  13. the lines
  14.       if "instr" in line:                           #look for instr in
  15. the file
  16.            if instr_yes == 'true':                    #check to see if
  17. this ends the instr block
  18.                break                                #exit the block
  19. >
  20.            reline = re.line.split('instr', '/d$')     #error probily
  21. split instr and /d (decimal number into parts) $ for end of line
  22.            number = int(reline[1])                  #convert to a
  23. number maybe not important
  24.                 if number == instr_number:            #check to see if
  25. it is the instr passed to function
  26.                 instr_yes = "true":                 #change flag to
  27. true because this is the instr we want
  28.       if instr_yes = "true":                        #start of code to
  29. copy to another file
  30.            f2.write(f.line)                         #write line to
  31. output file
  32. >
  33.     f.close                                         #close input file
  34.     f2.close
  35. >
>
>
>
>
Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this

data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
Regards

Frederic
I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..

http://www.dexrow.com
Eric,
I'm beginning to enjoy this. I'm sure we'll sort this out in no
time if we proceed methodically. Imagine you are a teacher and I am your
student. This is a quiz. I have to take it and you need to explain to me
the problem you want me to solve. If you don't explain it clearly, I
will not know what I have to do and cannot do the quiz. If you answer my
questions above, your description of the problem will be clear and I can
take the quiz. Okay?

Frederic


instr 1
a1 oscil p4, p5, 1 ; simple oscillator ; comment is
sometimes here
out a1
endin
I need to know the file I wan't to grab this from I need to grab this
out of the larger file and put it into it's own file, I need to know
what instr the user wants. I need to know what file to put it into and
it would be usefull to have the comment line returned (if any).

I did just get python essential reference 3rd edition.. If there is a
better reference on just the subject I am after I would be glad to grab
it when I get cash again.

Eric,

Tell us the story. Are you a student? A musician? What are you doing
with these files? Where do you get them from? What for?

Frederic

Sep 21 '06 #10
These are csound files. Csound recently added python as a scripting
language and is allowing also allowing csound calls from outside of
csound. The nice thing about csound is that instead of worrying about
virus and large files it is an interpiter and all the files look
somewhat like html. 4,000 virus free instruments for $20 is available
at
http://www.csounds.com and the csound programming book is also
available. The downside is that csound is can be realy ugly looking
(that is what I am trying to change) and it lets you write ugly looking
song code that is almost unreadable at times (would look nice in a
grid)

http://www.msn.com
...
Frederic Rentsch wrote:
Er*********@msn.com wrote:
Frederic Rentsch wrote:
Er*********@msn.com wrote:

Frederic Rentsch wrote:
Er*********@msn.com wrote:
All I am after realy is to change this

reline = re.line.split('instr', '/d$')

into something that grabs any line with instr in it take all the
numbers and then grab any comment that may or may not be at the end of
the line starting with ; until the end of the line including white
spaces.. this is a corrected version from

http://python-forum.org/py/viewtopic.php?t=1703

thanks in advance the hole routine is down below..


Expand|Select|Wrap|Line Numbers
  1. def extractCsdInstrument (input_File_Name, output_File_Name,
  2. instr_number):
  3.     "takes an .csd input file and grabs instr_number instrument and
  4. creates output_File_Name"
  5.     f = open (input_File_Name , 'r')                #opens file passed
  6. in to read
  7.     f2 = open (output_File_Name, 'w')               #opens file passed
  8. in to write
  9.     instr_yes = 'false'                             #set flag to false
  10.     for line in f:                                  #for through all
  11. the lines
  12.       if "instr" in line:                           #look for instr in
  13. the file
  14.            if instr_yes == 'true':                    #check to see if
  15. this ends the instr block
  16.                break                                #exit the block
  17.            reline = re.line.split('instr', '/d$')     #error probily
  18. split instr and /d (decimal number into parts) $ for end of line
  19.            number = int(reline[1])                  #convert to a
  20. number maybe not important
  21.                 if number == instr_number:            #check to see if
  22. it is the instr passed to function
  23.                 instr_yes = "true":                 #change flag to
  24. true because this is the instr we want
  25.       if instr_yes = "true":                        #start of code to
  26. copy to another file
  27.            f2.write(f.line)                         #write line to
  28. output file
  29.     f.close                                         #close input file
  30.     f2.close


Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this

data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file

<CsOptions>
-W -d -o tone.wav
</CsOptions>

<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>

<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>

<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>

</CsoundSynthesizer>

Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
Regards

Frederic
I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..

http://www.dexrow.com

Eric,
I'm beginning to enjoy this. I'm sure we'll sort this out in no
time if we proceed methodically. Imagine you are a teacher and I am your
student. This is a quiz. I have to take it and you need to explain to me
the problem you want me to solve. If you don't explain it clearly, I
will not know what I have to do and cannot do the quiz. If you answer my
questions above, your description of the problem will be clear and I can
take the quiz. Okay?

Frederic

instr 1
a1 oscil p4, p5, 1 ; simple oscillator ; comment is
sometimes here
out a1
endin
I need to know the file I wan't to grab this from I need to grab this
out of the larger file and put it into it's own file, I need to know
what instr the user wants. I need to know what file to put it into and
it would be usefull to have the comment line returned (if any).

I did just get python essential reference 3rd edition.. If there is a
better reference on just the subject I am after I would be glad to grab
it when I get cash again.

Eric,

Tell us the story. Are you a student? A musician? What are you doing
with these files? Where do you get them from? What for?

Frederic
Sep 23 '06 #11
Er*********@msn.com wrote:
These are csound files. Csound recently added python as a scripting
language and is allowing also allowing csound calls from outside of
csound. The nice thing about csound is that instead of worrying about
virus and large files it is an interpiter and all the files look
somewhat like html. 4,000 virus free instruments for $20 is available
at
http://www.csounds.com and the csound programming book is also
available. The downside is that csound is can be realy ugly looking
(that is what I am trying to change) and it lets you write ugly looking
song code that is almost unreadable at times (would look nice in a
grid)

http://www.msn.com
..
Frederic Rentsch wrote:
>Er*********@msn.com wrote:
>>Frederic Rentsch wrote:
Er*********@msn.com wrote:
Frederic Rentsch wrote:
>
>
>
>Er*********@msn.com wrote:
>>
>>
>>
>>All I am after realy is to change this
>>>
>> reline = re.line.split('instr', '/d$')
>>>
>>into something that grabs any line with instr in it take all the
>>numbers and then grab any comment that may or may not be at the end of
>>the line starting with ; until the end of the line including white
>>spaces.. this is a corrected version from
>>>
>>http://python-forum.org/py/viewtopic.php?t=1703
>>>
>>thanks in advance the hole routine is down below..
>>>
>>>
>>>
>>>
>>>
>>>
>>
Expand|Select|Wrap|Line Numbers
  1. >>def extractCsdInstrument (input_File_Name, output_File_Name,
  2. >>instr_number):
  3. >>>
  4. >>    "takes an .csd input file and grabs instr_number instrument and
  5. >>creates output_File_Name"
  6. >>    f = open (input_File_Name , 'r')                #opens file passed
  7. >>in to read
  8. >>    f2 = open (output_File_Name, 'w')               #opens file passed
  9. >>in to write
  10. >>    instr_yes = 'false'                             #set flag to false
  11. >>>
  12. >>    for line in f:                                  #for through all
  13. >>the lines
  14. >>      if "instr" in line:                           #look for instr in
  15. >>the file
  16. >>           if instr_yes == 'true':                    #check to see if
  17. >>this ends the instr block
  18. >>               break                                #exit the block
  19. >>>
  20. >>           reline = re.line.split('instr', '/d$')     #error probily
  21. >>split instr and /d (decimal number into parts) $ for end of line
  22. >>           number = int(reline[1])                  #convert to a
  23. >>number maybe not important
  24. >>                if number == instr_number:            #check to see if
  25. >>it is the instr passed to function
  26. >>                instr_yes = "true":                 #change flag to
  27. >>true because this is the instr we want
  28. >>      if instr_yes = "true":                        #start of code to
  29. >>copy to another file
  30. >>           f2.write(f.line)                         #write line to
  31. >>output file
  32. >>>
  33. >>    f.close                                         #close input file
  34. >>    f2.close
  35. >>>
  36. >>
>>>
>>>
>>>
>>>
>>>
>Eric,
> From your problem description and your code it is unclear what
>exactly it is you want. The task appears to be rather simple, though,
>and if you don't get much useful help I'd say it is because you don't
>explain it very well.
> I believe we've been through this before and your input data is
>like this
>>
> data = '''
> <CsoundSynthesizer>;
> ; test.csd - a Csound structured data file
>>
> <CsOptions>
> -W -d -o tone.wav
> </CsOptions>
>>
> <CsVersion ;optional section
> Before 4.10 ;these two statements check for
> After 4.08 ; Csound version 4.09
> </CsVersion>
>>
> <CsInstruments>
> ; originally tone.orc
> sr = 44100
> kr = 4410
> ksmps = 10
> nchnls = 1
> instr 1
> a1 oscil p4, p5, 1 ; simple oscillator
> out a1
> endin
> </CsInstruments>
>>
> <CsScore>
> ; originally tone.sco
> f1 0 8192 10 1
> i1 0 1 20000 1000 ;play one second of one kHz tone
> e
> </CsScore>
>>
> </CsoundSynthesizer>
>>
>Question 1: Is this your input?
>if yes:
> Question 1.1: What do you want to extract from it? In what format?
>if no:
> Question 1.1: What is your input?
> Question 1.2: What do you want to extract from it? In what format?
>Question 2: Do you need to generate output file names from the data?
>(One file per instrument?)
>if yes:
> Question 2.1: What do you want to make your file name from?
>(Instrument number?)
>>
>>
>Regards
>>
>Frederic
>>
>>
>>
I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..
>
http://www.dexrow.com
>
>
>
>
Eric,
I'm beginning to enjoy this. I'm sure we'll sort this out in no
time if we proceed methodically. Imagine you are a teacher and I am your
student. This is a quiz. I have to take it and you need to explain to me
the problem you want me to solve. If you don't explain it clearly, I
will not know what I have to do and cannot do the quiz. If you answer my
questions above, your description of the problem will be clear and I can
take the quiz. Okay?

Frederic
instr 1
a1 oscil p4, p5, 1 ; simple oscillator ; comment is
sometimes here
out a1
endin
I need to know the file I wan't to grab this from I need to grab this
out of the larger file and put it into it's own file, I need to know
what instr the user wants. I need to know what file to put it into and
it would be usefull to have the comment line returned (if any).

I did just get python essential reference 3rd edition.. If there is a
better reference on just the subject I am after I would be glad to grab
it when I get cash again.
Eric,

Tell us the story. Are you a student? A musician? What are you doing
with these files? Where do you get them from? What for?

Frederic

Eric,
I found an excellent CSound doc at:
ems.music.utexas.edu/program/mus329j/CSPrimer.pdf. I am myself
interested in music and will definitely look into this more closely.
For the time being I get the impression that you are dealing with
CSound instrument definition files which you can buy cheaply in great
quantity but unfortunately are illegibly formatted. If this is so, what
you would need is a formatter. I found references to CSound editors
(e.g. http://flavio.tordini.org/csound-editor/). Those should be capable
of formatting.
Writing your own formatter shouldn't be all that hard. Here's a
quick try:
def csound_formatter (in_file, out_file_file = sys.stdout):

INDENT = 10
CODE_LENGTH = 50

for l in in_file:
line = l.strip ()
if line == '':
out_file.write ('\n')
else:
if line [0] == ';': # Comment line
out_file.write ('%s\n' % line)
else:
code = comment = ''
if line [-1] == ':': # Label
out_file.write ('%s\n' % line)
else:
if ';' in line:
code, comment = line.split (';')
out_file.write ('%*s%-*s;%s\n' % (INDENT, '',
CODE_LENGTH, code, comment))
else:
out_file.write ('%*s%s\n' % (INDENT, '', line))
And here is a section of a csound file with white space squeezed out as
would make the file smaller:
>>csound = StringIO.StringIO ('''; Basic FM Instrument with Variable
Vibrato
; p4=amp p5=pch(fund) p6=vibdel p7=vibrate p8=vibwth
; p9=rise p10=decay p11=max index p12=car fac p13=modfac
; p14=index rise p15=index decay p16=left channel factor p17=original p3

instr 9,10,11,12
;--------------------------------------------------------------------------------
;initialization block:
kpitch init cpspch (p5)
itempo = p3/p17 ;ratio seconds/beats
idelay = p6 * itempo ;convert beats to secs
irise = p9 * itempo
idecay = p10 * itempo
indxris = (p14 == 0 ? irise : p14 * itempo)
indxdec = (p15 == 0 ? idecay : p15 * itempo)
if (p16 != 0) igoto panning ;if a panfac, use it, else
ilfac = .707 ;default is mono (sqrt(.5))
irfac = .707
igoto perform
panning:
ilfac = sqrt(p16)
irfac = sqrt(1-p16)
;--------------------------------------------------------------------------------
;performance block:
perform:
if (p7 == 0 || p8 == 0) goto continue
kontrol oscil1 idelay,1,.5,2 ;vib control
kvib oscili p8*kontrol,p7*kontrol,1 ;vibrato unit
kpitch = cpsoct(octpch(p5)+kvib) ;varying fund pitch in hz
continue:
kamp linen p4,irise,p3,idecay
kindex linen p11,indxris,p3,indxdec
asig foscili kamp,kpitch,p12,p13,kindex,1 ;p12,p13=carfac,mod fac
outs asig*ilfac,asig*irfac
endin
''')

Now we run this through the function defined above:
>>csound_formatter (s)
; Basic FM Instrument with Variable Vibrato
; p4=amp p5=pch(fund) p6=vibdel p7=vibrate p8=vibwth
; p9=rise p10=decay p11=max index p12=car fac p13=modfac
; p14=index rise p15=index decay p16=left channel factor p17=original p3

instr 9,10,11,12
;--------------------------------------------------------------------------------
;initialization block:
kpitch init cpspch (p5)
itempo = p3/p17 ;ratio
seconds/beats
idelay = p6 * itempo ;convert
beats to secs
irise = p9 * itempo
idecay = p10 * itempo
indxris = (p14 == 0 ? irise : p14 * itempo)
indxdec = (p15 == 0 ? idecay : p15 * itempo)
if (p16 != 0) igoto panning ;if a
panfac, use it, else
ilfac = .707 ;default is
mono (sqrt(.5))
irfac = .707
igoto perform
panning:
ilfac = sqrt(p16)
irfac = sqrt(1-p16)
;--------------------------------------------------------------------------------
;performance block:
perform:
if (p7 == 0 || p8 == 0) goto continue
kontrol oscil1 idelay,1,.5,2 ;vib control
kvib oscili p8*kontrol,p7*kontrol,1 ;vibrato unit
kpitch = cpsoct(octpch(p5)+kvib) ;varying
fund pitch in hz
continue:
kamp linen p4,irise,p3,idecay
kindex linen p11,indxris,p3,indxdec
asig foscili kamp,kpitch,p12,p13,kindex,1
;p12,p13=carfac,mod fac
outs asig*ilfac,asig*irfac
endin
It works on a couple of presumptions (e.g. no code following labels). So
it may not function perfectly but shouldn't be hard to tweak.

Hope this helps

Frederic

Sep 24 '06 #12

Frederic Rentsch wrote:
Er*********@msn.com wrote:
These are csound files. Csound recently added python as a scripting
language and is allowing also allowing csound calls from outside of
csound. The nice thing about csound is that instead of worrying about
virus and large files it is an interpiter and all the files look
somewhat like html. 4,000 virus free instruments for $20 is available
at
http://www.csounds.com and the csound programming book is also
available. The downside is that csound is can be realy ugly looking
(that is what I am trying to change) and it lets you write ugly looking
song code that is almost unreadable at times (would look nice in a
grid)

http://www.msn.com
..
Frederic Rentsch wrote:
Er*********@msn.com wrote:

Frederic Rentsch wrote:
Er*********@msn.com wrote:
Frederic Rentsch wrote:

Er*********@msn.com wrote:
>
>
>
>All I am after realy is to change this
>>
> reline = re.line.split('instr', '/d$')
>>
>into something that grabs any line with instr in it take all the
>numbers and then grab any comment that may or may not be at the end of
>the line starting with ; until the end of the line including white
>spaces.. this is a corrected version from
>>
>http://python-forum.org/py/viewtopic.php?t=1703
>>
>thanks in advance the hole routine is down below..
>>
>>
>>
>>
>>
>>
>
Expand|Select|Wrap|Line Numbers
  1. >def extractCsdInstrument (input_File_Name, output_File_Name,
  2. >instr_number):
  3. >>
  4. >    "takes an .csd input file and grabs instr_number instrument and
  5. >creates output_File_Name"
  6. >    f = open (input_File_Name , 'r')                #opens file passed
  7. >in to read
  8. >    f2 = open (output_File_Name, 'w')               #opens file passed
  9. >in to write
  10. >    instr_yes = 'false'                             #set flag to false
  11. >>
  12. >    for line in f:                                  #for through all
  13. >the lines
  14. >      if "instr" in line:                           #look for instr in
  15. >the file
  16. >           if instr_yes == 'true':                    #check to see if
  17. >this ends the instr block
  18. >               break                                #exit the block
  19. >>
  20. >           reline = re.line.split('instr', '/d$')     #error probily
  21. >split instr and /d (decimal number into parts) $ for end of line
  22. >           number = int(reline[1])                  #convert to a
  23. >number maybe not important
  24. >                if number == instr_number:            #check to see if
  25. >it is the instr passed to function
  26. >                instr_yes = "true":                 #change flag to
  27. >true because this is the instr we want
  28. >      if instr_yes = "true":                        #start of code to
  29. >copy to another file
  30. >           f2.write(f.line)                         #write line to
  31. >output file
  32. >>
  33. >    f.close                                         #close input file
  34. >    f2.close
  35. >>
  36. >
>>
>>
>>
>>
>>
Eric,
From your problem description and your code it is unclear what
exactly it is you want. The task appears to be rather simple, though,
and if you don't get much useful help I'd say it is because you don't
explain it very well.
I believe we've been through this before and your input data is
like this
>
data = '''
<CsoundSynthesizer>;
; test.csd - a Csound structured data file
>
<CsOptions>
-W -d -o tone.wav
</CsOptions>
>
<CsVersion ;optional section
Before 4.10 ;these two statements check for
After 4.08 ; Csound version 4.09
</CsVersion>
>
<CsInstruments>
; originally tone.orc
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
instr 1
a1 oscil p4, p5, 1 ; simple oscillator
out a1
endin
</CsInstruments>
>
<CsScore>
; originally tone.sco
f1 0 8192 10 1
i1 0 1 20000 1000 ;play one second of one kHz tone
e
</CsScore>
>
</CsoundSynthesizer>
>
Question 1: Is this your input?
if yes:
Question 1.1: What do you want to extract from it? In what format?
if no:
Question 1.1: What is your input?
Question 1.2: What do you want to extract from it? In what format?
Question 2: Do you need to generate output file names from the data?
(One file per instrument?)
if yes:
Question 2.1: What do you want to make your file name from?
(Instrument number?)
>
>
Regards
>
Frederic
>
>
>
I want to pass the file name to the subroutine and return a comment
string if it is there maybe it should be simplier. I probily should
have the option of grabbing the comment in other related routines. I
am pretty ambitious with the main program. I did notice some code in
tcl that would be usefull to the app If I compile it.. I am probily
not ready for that though..

http://www.dexrow.com


Eric,
I'm beginning to enjoy this. I'm sure we'll sort this out in no
time if we proceed methodically. Imagine you are a teacher and I am your
student. This is a quiz. I have to take it and you need to explain to me
the problem you want me to solve. If you don't explain it clearly, I
will not know what I have to do and cannot do the quiz. If you answer my
questions above, your description of the problem will be clear and I can
take the quiz. Okay?

Frederic
instr 1
a1 oscil p4, p5, 1 ; simple oscillator ; comment is
sometimes here
out a1
endin
I need to know the file I wan't to grab this from I need to grab this
out of the larger file and put it into it's own file, I need to know
what instr the user wants. I need to know what file to put it into and
it would be usefull to have the comment line returned (if any).

I did just get python essential reference 3rd edition.. If there is a
better reference on just the subject I am after I would be glad to grab
it when I get cash again.

Eric,

Tell us the story. Are you a student? A musician? What are you doing
with these files? Where do you get them from? What for?

Frederic
Eric,
I found an excellent CSound doc at:
ems.music.utexas.edu/program/mus329j/CSPrimer.pdf. I am myself
interested in music and will definitely look into this more closely.
For the time being I get the impression that you are dealing with
CSound instrument definition files which you can buy cheaply in great
quantity but unfortunately are illegibly formatted. If this is so, what
you would need is a formatter. I found references to CSound editors
(e.g. http://flavio.tordini.org/csound-editor/). Those should be capable
of formatting.
Writing your own formatter shouldn't be all that hard. Here's a
quick try:
def csound_formatter (in_file, out_file_file = sys.stdout):

INDENT = 10
CODE_LENGTH = 50

for l in in_file:
line = l.strip ()
if line == '':
out_file.write ('\n')
else:
if line [0] == ';': # Comment line
out_file.write ('%s\n' % line)
else:
code = comment = ''
if line [-1] == ':': # Label
out_file.write ('%s\n' % line)
else:
if ';' in line:
code, comment = line.split (';')
out_file.write ('%*s%-*s;%s\n' % (INDENT, '',
CODE_LENGTH, code, comment))
else:
out_file.write ('%*s%s\n' % (INDENT, '', line))
And here is a section of a csound file with white space squeezed out as
would make the file smaller:
>>csound = StringIO.StringIO ('''; Basic FM Instrument with Variable
Vibrato
; p4=amp p5=pch(fund) p6=vibdel p7=vibrate p8=vibwth
; p9=rise p10=decay p11=max index p12=car fac p13=modfac
; p14=index rise p15=index decay p16=left channel factor p17=original p3

instr 9,10,11,12
;--------------------------------------------------------------------------------
;initialization block:
kpitch init cpspch (p5)
itempo = p3/p17 ;ratio seconds/beats
idelay = p6 * itempo ;convert beats to secs
irise = p9 * itempo
idecay = p10 * itempo
indxris = (p14 == 0 ? irise : p14 * itempo)
indxdec = (p15 == 0 ? idecay : p15 * itempo)
if (p16 != 0) igoto panning ;if a panfac, use it, else
ilfac = .707 ;default is mono (sqrt(.5))
irfac = .707
igoto perform
panning:
ilfac = sqrt(p16)
irfac = sqrt(1-p16)
;--------------------------------------------------------------------------------
;performance block:
perform:
if (p7 == 0 || p8 == 0) goto continue
kontrol oscil1 idelay,1,.5,2 ;vib control
kvib oscili p8*kontrol,p7*kontrol,1 ;vibrato unit
kpitch = cpsoct(octpch(p5)+kvib) ;varying fund pitch in hz
continue:
kamp linen p4,irise,p3,idecay
kindex linen p11,indxris,p3,indxdec
asig foscili kamp,kpitch,p12,p13,kindex,1 ;p12,p13=carfac,mod fac
outs asig*ilfac,asig*irfac
endin
''')

Now we run this through the function defined above:
>>csound_formatter (s)

; Basic FM Instrument with Variable Vibrato
; p4=amp p5=pch(fund) p6=vibdel p7=vibrate p8=vibwth
; p9=rise p10=decay p11=max index p12=car fac p13=modfac
; p14=index rise p15=index decay p16=left channel factor p17=original p3

instr 9,10,11,12
;--------------------------------------------------------------------------------
;initialization block:
kpitch init cpspch (p5)
itempo = p3/p17 ;ratio
seconds/beats
idelay = p6 * itempo ;convert
beats to secs
irise = p9 * itempo
idecay = p10 * itempo
indxris = (p14 == 0 ? irise : p14 * itempo)
indxdec = (p15 == 0 ? idecay : p15 * itempo)
if (p16 != 0) igoto panning ;if a
panfac, use it, else
ilfac = .707 ;default is
mono (sqrt(.5))
irfac = .707
igoto perform
panning:
ilfac = sqrt(p16)
irfac = sqrt(1-p16)
;--------------------------------------------------------------------------------
;performance block:
perform:
if (p7 == 0 || p8 == 0) goto continue
kontrol oscil1 idelay,1,.5,2 ;vib control
kvib oscili p8*kontrol,p7*kontrol,1 ;vibrato unit
kpitch = cpsoct(octpch(p5)+kvib) ;varying
fund pitch in hz
continue:
kamp linen p4,irise,p3,idecay
kindex linen p11,indxris,p3,indxdec
asig foscili kamp,kpitch,p12,p13,kindex,1
;p12,p13=carfac,mod fac
outs asig*ilfac,asig*irfac
endin
It works on a couple of presumptions (e.g. no code following labels). So
it may not function perfectly but shouldn't be hard to tweak.

Hope this helps

Frederic
I was hoping to add and remove instruments.. although the other should
go into my example file because it will come in handy at some point.
It would also be cool to get a list of instruments along with any
comment line if there is one into a grid but that is two different
functions.

http://www.dexrow.com

Sep 24 '06 #13
Er*********@msn.com wrote:
Frederic Rentsch wrote:
>Er*********@msn.com wrote:
>>These are csound files. Csound recently added python as a scripting
language and is allowing also allowing csound calls from outside of
csound. The nice thing about csound is that instead of worrying about
virus and large files it is an interpiter and all the files look
somewhat like html. 4,000 virus free instruments for $20 is available
at
http://www.csounds.com and the csound programming book is also
available. The downside is that csound is can be realy ugly looking
(that is what I am trying to change) and it lets you write ugly looking
song code that is almost unreadable at times (would look nice in a
grid)
snip snip snip snip snip .........
I was hoping to add and remove instruments.. although the other should
go into my example file because it will come in handy at some point.
It would also be cool to get a list of instruments along with any
comment line if there is one into a grid but that is two different
functions.

http://www.dexrow.com

Eric,

Below the dotted line there are two functions.
"csound_filter ()" extracts and formats instrument blocks from
csound files and writes the output to another file. (If called without
an output file name, the output displays on the screen).
The second function, "make_instrument_dictionaries ()", takes the
file generated by the first function and makes two dictionaries. One of
them lists instrument ids keyed on instrument description, the other one
does it the other way around, listing descriptions by instrument id.
Instrument ids are file name plus instrument number.
You cannot depend on this system to function reliably, because it
extracts information from comments. I took my data from
"ems.music.utexas.edu/program/mus329j/CSPrimer.pdf" the author of which
happens to lead his instrument blocks with a header made up of comment
lines, the first of which characterizes the block. That first line I use
to make the dictionaries. If your data doesn't follow this practice,
then you may not get meaningful dictionaries and are in for some
hacking. In any case, this doesn't look like a job that can be fully
automated. But if you can build your data base with a manageable amount
of manual work you should be okay.
The SE filter likewise works depending on formal consistency with
my sample. If it fails, you may have to either tweak it or move up to a
parser.
I'll be glad to provide further assistance to the best of my
knowledge. But you will have to make an effort to express yourself
intelligibly. As a matter of fact the hardest part of proposing
solutions to your problem is guessing what your problem is. I suggest
you do this: before you post, show the message to a good friend and edit
it until he understands it.

Regards

Frederic
..

------------------------------------------------------------------------------------------------------------------------

def csound_filter (csound_file_name,
name_of_formatted_instrument_blocks_file = sys.stdout):

"""
This function filters and formats instrument blocks out of a
csound file.

csound_formatter (csound_file_name,
name_of_formatted_instrument_blocks_file)
csound_formatter (csound_file_name) # Single argument: screen output

"""

import SE
Instruments_Filter = SE.SE ('<EAT"~;.*~==(10)"
"~instr(.|\n)*?endin~==(10)(10)"')

INDENT = 10
CODE_LENGTH = 50

def format ():
for l in instruments_file:
line = l.strip ()
if line == '':
out_file.write ('\n')
else:
if line [0] == ';': # Comment line
out_file.write ('%s\n' % line)
else:
code = comment = ''
if line [-1] == ':': # Label
out_file.write ('%s\n' % line)
else:
if ';' in line:
code, comment = line.split (';')
out_file.write ('%*s%-*s;%s\n' % (INDENT, '',
CODE_LENGTH, code, comment))
else:
out_file.write ('%*s%s\n' % (INDENT, '', line))

instruments_file_name = Instruments_Filter (csound_file_name)
instruments_file = file (instruments_file_name, 'w+a')
if name_of_formatted_instrument_blocks_file != sys.stdout:
out_file = file (name_of_formatted_instrument_blocks_file, 'wa')
owns_out_file = True
else:
out_file = name_of_formatted_instrument_blocks_file
owns_out_file = False

format ()

if owns_out_file: out_file.close ()

def make_instrument_dictionaries (name_of_formatted_instrument_blocks_file):

"""
This function takes a file made by the previous function and
generates two dictionaries.
One records instrument ids by description. The other one
instrument descriptions by id.
Instrument ids are made up of file name and instrument number.
If these two dictionaries are pickled they can be used like data
bases and added to
incrementally.

"""

import re
instrument_numbers_re = re.compile ('instr *([0-9,]+)')

in_file = file (name_of_formatted_instrument_blocks_file, 'ra')

instrument_descriptions_by_id = {}
instrument_ids_by_description = {}

inside = False
for line in in_file:
line = line.strip ()
if not inside:
if line:
instrument_description = line [1:].rstrip ()
inside = True
else:
if line == '':
inside = False
if inside:
if line.startswith ('instr'):
instrument_numbers = instrument_numbers_re.match
(line).group (1).split (',')
instrument_ids = ['%s: %s' %
(name_of_formatted_instrument_blocks_file, i_n) for i_n in
instrument_numbers]
instrument_ids_by_description [instrument_description] =
instrument_ids
for instrument_id in instrument_ids:
instrument_descriptions_by_id [instrument_id] =
instrument_description

in_file.close ()

return instrument_descriptions_by_id, instrument_ids_by_description

---------------------------------------------------------------------------------------------
>>for instrument_id in instrument_descriptions_by_id:
print '%s - %s' % (instrument_id,
instrument_descriptions_by_id [instrument_id])

T:\instruments: 5 - Simple Gating Instrument with Chorus
T:\instruments: 4 - Portamento/Panning Instrument
T:\instruments: 12 - Basic FM Instrument with Variable Vibrato
T:\instruments: 11 - Basic FM Instrument with Variable Vibrato
T:\instruments: 10 - Basic FM Instrument with Variable Vibrato

---------------------------------------------------------------------------------------------
>>for description in instrument_ids_by_description:
print description
for instrument in instrument_ids_by_description [description]:
print ' ', instrument

Basic FM Instrument with Variable Vibrato
T:\instruments: 9
T:\instruments: 10
T:\instruments: 11
T:\instruments: 12
Simple Gating Instrument with Chorus
T:\instruments: 5
T:\instruments: 6
T:\instruments: 7
T:\instruments: 8
Portamento/Panning Instrument
T:\instruments: 1
T:\instruments: 2
T:\instruments: 3
T:\instruments: 4

---------------------------------------------------------------------------------------------

Sep 26 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1808
by: WindAndWaves | last post by:
Hi Gurus I hope I am going to make sense with this question: I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a...
1
1643
by: Sebastien GIRAUD | last post by:
Hello, First let say that am french and that i'll try to write the best english i can... I'm a python newbe and have the following problem : I try to create 2 threads in a server program and they...
1
1762
by: Ron | last post by:
Is this built into any of the python versions? Need it! Using 2.3.5 and doesn't seem to have it.Newbe needs help!email ron@nac.net Thanks Ron
7
1876
by: Jean Pierre Daviau | last post by:
Hi, <script language="javascript" type="text/javascript"> if(navigator.appName.indexOf("Netscape") != -1){ document.writeln('<link rel="stylesheet" href="~styles/aquarelle_ns.css"...
9
6661
by: Yaro | last post by:
Hello DB2/NT 8.1.3 Sorry for stupid questions. I am newbe in DB2. 1. How can I read *.sql script (with table and function definitions) into a database? Tool, command... 2. In Project Center...
1
1995
by: Jim | last post by:
I have created a windows form that contains several tab pages which contain a panels. On a tab page I am trying to dynamically create a series of buttons in that pages panel. I am failing because...
6
1475
by: ken | last post by:
Hi all, I copied this code from the examples. "How to: Receive Strings From Serial Ports in Visual Basic" When I call the function and using the single step method it hangs at Dim Incoming As...
19
1532
by: AMP | last post by:
I have a simple question. If i have a button on form1 that creates : Form2 newform = new Form2(); newform.Show(); As I click the button a new form shows,but acording to my code each one has...
17
2685
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.