Re: newbe's re question
[EMAIL PROTECTED] wrote: > Frederic Rentsch wrote: > >> [EMAIL PROTECTED] 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 (' "~;.*~==(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 ma
Re: newbe's re question
Frederic Rentsch wrote: > [EMAIL PROTECTED] 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: > > > >> [EMAIL PROTECTED] wrote: > >> > >>> Frederic Rentsch wrote: > >>> > >>> > [EMAIL PROTECTED] wrote: > > > > Frederic Rentsch wrote: > > > > > > > >> [EMAIL PROTECTED] 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.. > >>> > >>> > >>> > >>> > >>> > >>> > >>> [code] > >>> def extractCsdInstrument (input_File_Name, output_File_Name, > >>> instr_number): > >>> > >>> "takes an .csd input file and grabs instr_number instrument and > >>> creates output_File_Name" > >>> f = open (input_File_Name , 'r')#opens file passed > >>> in to read > >>> f2 = open (output_File_Name, 'w') #opens file passed > >>> in to write > >>> instr_yes = 'false' #set flag to false > >>> > >>> for line in f: #for through all > >>> the lines > >>> if "instr" in line: #look for instr in > >>> the file > >>>if instr_yes == 'true':#check to see if > >>> this ends the instr block > >>>break#exit the block > >>> > >>>reline = re.line.split('instr', '/d$') #error probily > >>> split instr and /d (decimal number into parts) $ for end of line > >>>number = int(reline[1]) #convert to a > >>> number maybe not important > >>> if number == instr_number:#check to see if > >>> it is the instr passed to function > >>> instr_yes = "true": #change flag to > >>> true because this is the instr we want > >>> if instr_yes = "true":#start of code to > >>> copy to another file > >>>f2.write(f.line) #write line to > >>> output file > >>> > >>> f.close #close input file > >>> f2.close > >>> > >>> [/code] > >>> > >>> > >>> > >>> > >>> > >> 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 = ''' > >>; > >> ; test.csd - a Csound structured data file > >> > >> > >> -W -d -o tone.wav > >> > >> > >>;optional section > >> Before 4.10 ;these two statements check for > >> After 4.08 ; Csound version 4.09 > >> > >> > >> > >> ; originally tone.orc > >> sr = 44100 > >> kr = 4410 > >> ksmps = 10 > >> nchnls = 1 > >> instr 1 > >> a1 oscil p4, p5, 1 ; simple oscillator > >> out a1 > >>endin > >> > >> > >> > >> ; originally tone.sco > >> f1 0 8192 10 1 > >> i1 0 1 2 1000 ;play one second of one kHz tone > >> e > >> > >> > >> > >> > >> 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 f
Re: newbe's re question
[EMAIL PROTECTED] 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: > >> [EMAIL PROTECTED] wrote: >> >>> Frederic Rentsch wrote: >>> >>> [EMAIL PROTECTED] wrote: > Frederic Rentsch wrote: > > > >> [EMAIL PROTECTED] 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.. >>> >>> >>> >>> >>> >>> >>> [code] >>> def extractCsdInstrument (input_File_Name, output_File_Name, >>> instr_number): >>> >>> "takes an .csd input file and grabs instr_number instrument and >>> creates output_File_Name" >>> f = open (input_File_Name , 'r')#opens file passed >>> in to read >>> f2 = open (output_File_Name, 'w') #opens file passed >>> in to write >>> instr_yes = 'false' #set flag to false >>> >>> for line in f: #for through all >>> the lines >>> if "instr" in line: #look for instr in >>> the file >>>if instr_yes == 'true':#check to see if >>> this ends the instr block >>>break#exit the block >>> >>>reline = re.line.split('instr', '/d$') #error probily >>> split instr and /d (decimal number into parts) $ for end of line >>>number = int(reline[1]) #convert to a >>> number maybe not important >>> if number == instr_number:#check to see if >>> it is the instr passed to function >>> instr_yes = "true": #change flag to >>> true because this is the instr we want >>> if instr_yes = "true":#start of code to >>> copy to another file >>>f2.write(f.line) #write line to >>> output file >>> >>> f.close #close input file >>> f2.close >>> >>> [/code] >>> >>> >>> >>> >>> >> 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 = ''' >>; >> ; test.csd - a Csound structured data file >> >> >> -W -d -o tone.wav >> >> >>;optional section >> Before 4.10 ;these two statements check for >> After 4.08 ; Csound version 4.09 >> >> >> >> ; originally tone.orc >> sr = 44100 >> kr = 4410 >> ksmps = 10 >> nchnls = 1 >> instr 1 >> a1 oscil p4, p5, 1 ; simple oscillator >> out a1 >>endin >> >> >> >> ; originally tone.sco >> f1 0 8192 10 1 >> i1 0 1 2 1000 ;play one second of one kHz tone >> e >> >> >> >> >> 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?) >> >>
Re: newbe's re question
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: > [EMAIL PROTECTED] wrote: > > Frederic Rentsch wrote: > > > >> [EMAIL PROTECTED] wrote: > >> > >>> Frederic Rentsch wrote: > >>> > >>> > [EMAIL PROTECTED] 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.. > > > > > > > > > > > > > > [code] > > def extractCsdInstrument (input_File_Name, output_File_Name, > > instr_number): > > > > "takes an .csd input file and grabs instr_number instrument and > > creates output_File_Name" > > f = open (input_File_Name , 'r')#opens file passed > > in to read > > f2 = open (output_File_Name, 'w') #opens file passed > > in to write > > instr_yes = 'false' #set flag to false > > > > for line in f: #for through all > > the lines > > if "instr" in line: #look for instr in > > the file > >if instr_yes == 'true':#check to see if > > this ends the instr block > >break#exit the block > > > >reline = re.line.split('instr', '/d$') #error probily > > split instr and /d (decimal number into parts) $ for end of line > >number = int(reline[1]) #convert to a > > number maybe not important > > if number == instr_number:#check to see if > > it is the instr passed to function > > instr_yes = "true": #change flag to > > true because this is the instr we want > > if instr_yes = "true":#start of code to > > copy to another file > >f2.write(f.line) #write line to > > output file > > > > f.close #close input file > > f2.close > > > > [/code] > > > > > > > > > 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 = ''' > ; > ; test.csd - a Csound structured data file > > > -W -d -o tone.wav > > > ;optional section > Before 4.10 ;these two statements check for > After 4.08 ; Csound version 4.09 > > > > ; originally tone.orc > sr = 44100 > kr = 4410 > ksmps = 10 > nchnls = 1 > instr 1 > a1 oscil p4, p5, 1 ; simple oscillator > out a1 > endin > > > > ; originally tone.sco > f1 0 8192 10 1 > i1 0 1 2 1000 ;play one second of one kHz tone > e > > > > > 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
Re: newbe's re question
[EMAIL PROTECTED] wrote: > Frederic Rentsch wrote: > >> [EMAIL PROTECTED] wrote: >> >>> Frederic Rentsch wrote: >>> >>> [EMAIL PROTECTED] 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.. > > > > > > > [code] > def extractCsdInstrument (input_File_Name, output_File_Name, > instr_number): > > "takes an .csd input file and grabs instr_number instrument and > creates output_File_Name" > f = open (input_File_Name , 'r')#opens file passed > in to read > f2 = open (output_File_Name, 'w') #opens file passed > in to write > instr_yes = 'false' #set flag to false > > for line in f: #for through all > the lines > if "instr" in line: #look for instr in > the file >if instr_yes == 'true':#check to see if > this ends the instr block >break#exit the block > >reline = re.line.split('instr', '/d$') #error probily > split instr and /d (decimal number into parts) $ for end of line >number = int(reline[1]) #convert to a > number maybe not important > if number == instr_number:#check to see if > it is the instr passed to function > instr_yes = "true": #change flag to > true because this is the instr we want > if instr_yes = "true":#start of code to > copy to another file >f2.write(f.line) #write line to > output file > > f.close #close input file > f2.close > > [/code] > > > > 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 = ''' ; ; test.csd - a Csound structured data file -W -d -o tone.wav ;optional section Before 4.10 ;these two statements check for After 4.08 ; Csound version 4.09 ; originally tone.orc sr = 44100 kr = 4410 ksmps = 10 nchnls = 1 instr 1 a1 oscil p4, p5, 1 ; simple oscillator out a1 endin ; originally tone.sco f1 0 8192 10 1 i1 0 1 2 1000 ;play one second of one kHz tone e 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
Re: newbe's re question
Frederic Rentsch wrote: > [EMAIL PROTECTED] wrote: > > Frederic Rentsch wrote: > > > >> [EMAIL PROTECTED] 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.. > >>> > >>> > >>> > >>> > >>> > >>> > >>> [code] > >>> def extractCsdInstrument (input_File_Name, output_File_Name, > >>> instr_number): > >>> > >>> "takes an .csd input file and grabs instr_number instrument and > >>> creates output_File_Name" > >>> f = open (input_File_Name , 'r')#opens file passed > >>> in to read > >>> f2 = open (output_File_Name, 'w') #opens file passed > >>> in to write > >>> instr_yes = 'false' #set flag to false > >>> > >>> for line in f: #for through all > >>> the lines > >>> if "instr" in line: #look for instr in > >>> the file > >>>if instr_yes == 'true':#check to see if > >>> this ends the instr block > >>>break#exit the block > >>> > >>>reline = re.line.split('instr', '/d$') #error probily > >>> split instr and /d (decimal number into parts) $ for end of line > >>>number = int(reline[1]) #convert to a > >>> number maybe not important > >>> if number == instr_number:#check to see if > >>> it is the instr passed to function > >>> instr_yes = "true": #change flag to > >>> true because this is the instr we want > >>> if instr_yes = "true":#start of code to > >>> copy to another file > >>>f2.write(f.line) #write line to > >>> output file > >>> > >>> f.close #close input file > >>> f2.close > >>> > >>> [/code] > >>> > >>> > >>> > >> 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 = ''' > >>; > >> ; test.csd - a Csound structured data file > >> > >> > >> -W -d -o tone.wav > >> > >> > >>;optional section > >> Before 4.10 ;these two statements check for > >> After 4.08 ; Csound version 4.09 > >> > >> > >> > >> ; originally tone.orc > >> sr = 44100 > >> kr = 4410 > >> ksmps = 10 > >> nchnls = 1 > >> instr 1 > >> a1 oscil p4, p5, 1 ; simple oscillator > >> out a1 > >>endin > >> > >> > >> > >> ; originally tone.sco > >> f1 0 8192 10 1 > >> i1 0 1 2 1000 ;play one second of one kHz tone > >> e > >> > >> > >> > >> > >> 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
Re: newbe's re question
[EMAIL PROTECTED] wrote: > Frederic Rentsch wrote: > >> [EMAIL PROTECTED] 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.. >>> >>> >>> >>> >>> >>> >>> [code] >>> def extractCsdInstrument (input_File_Name, output_File_Name, >>> instr_number): >>> >>> "takes an .csd input file and grabs instr_number instrument and >>> creates output_File_Name" >>> f = open (input_File_Name , 'r')#opens file passed >>> in to read >>> f2 = open (output_File_Name, 'w') #opens file passed >>> in to write >>> instr_yes = 'false' #set flag to false >>> >>> for line in f: #for through all >>> the lines >>> if "instr" in line: #look for instr in >>> the file >>>if instr_yes == 'true':#check to see if >>> this ends the instr block >>>break#exit the block >>> >>>reline = re.line.split('instr', '/d$') #error probily >>> split instr and /d (decimal number into parts) $ for end of line >>>number = int(reline[1]) #convert to a >>> number maybe not important >>> if number == instr_number:#check to see if >>> it is the instr passed to function >>> instr_yes = "true": #change flag to >>> true because this is the instr we want >>> if instr_yes = "true":#start of code to >>> copy to another file >>>f2.write(f.line) #write line to >>> output file >>> >>> f.close #close input file >>> f2.close >>> >>> [/code] >>> >>> >>> >> 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 = ''' >>; >> ; test.csd - a Csound structured data file >> >> >> -W -d -o tone.wav >> >> >>;optional section >> Before 4.10 ;these two statements check for >> After 4.08 ; Csound version 4.09 >> >> >> >> ; originally tone.orc >> sr = 44100 >> kr = 4410 >> ksmps = 10 >> nchnls = 1 >> instr 1 >> a1 oscil p4, p5, 1 ; simple oscillator >> out a1 >>endin >> >> >> >> ; originally tone.sco >> f1 0 8192 10 1 >> i1 0 1 2 1000 ;play one second of one kHz tone >> e >> >> >> >> >> 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbe's re question
Frederic Rentsch wrote: > [EMAIL PROTECTED] 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.. > > > > > > > > > > > > > > [code] > > def extractCsdInstrument (input_File_Name, output_File_Name, > > instr_number): > > > > "takes an .csd input file and grabs instr_number instrument and > > creates output_File_Name" > > f = open (input_File_Name , 'r')#opens file passed > > in to read > > f2 = open (output_File_Name, 'w') #opens file passed > > in to write > > instr_yes = 'false' #set flag to false > > > > for line in f: #for through all > > the lines > > if "instr" in line: #look for instr in > > the file > >if instr_yes == 'true':#check to see if > > this ends the instr block > >break#exit the block > > > >reline = re.line.split('instr', '/d$') #error probily > > split instr and /d (decimal number into parts) $ for end of line > >number = int(reline[1]) #convert to a > > number maybe not important > > if number == instr_number:#check to see if > > it is the instr passed to function > > instr_yes = "true": #change flag to > > true because this is the instr we want > > if instr_yes = "true":#start of code to > > copy to another file > >f2.write(f.line) #write line to > > output file > > > > f.close #close input file > > f2.close > > > > [/code] > > > > > 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 = ''' >; > ; test.csd - a Csound structured data file > > > -W -d -o tone.wav > > >;optional section > Before 4.10 ;these two statements check for > After 4.08 ; Csound version 4.09 > > > > ; originally tone.orc > sr = 44100 > kr = 4410 > ksmps = 10 > nchnls = 1 > instr 1 > a1 oscil p4, p5, 1 ; simple oscillator > out a1 >endin > > > > ; originally tone.sco > f1 0 8192 10 1 > i1 0 1 2 1000 ;play one second of one kHz tone > e > > > > > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbe's re question
[EMAIL PROTECTED] 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.. > > > > > > > [code] > def extractCsdInstrument (input_File_Name, output_File_Name, > instr_number): > > "takes an .csd input file and grabs instr_number instrument and > creates output_File_Name" > f = open (input_File_Name , 'r')#opens file passed > in to read > f2 = open (output_File_Name, 'w') #opens file passed > in to write > instr_yes = 'false' #set flag to false > > for line in f: #for through all > the lines > if "instr" in line: #look for instr in > the file >if instr_yes == 'true':#check to see if > this ends the instr block >break#exit the block > >reline = re.line.split('instr', '/d$') #error probily > split instr and /d (decimal number into parts) $ for end of line >number = int(reline[1]) #convert to a > number maybe not important > if number == instr_number:#check to see if > it is the instr passed to function > instr_yes = "true": #change flag to > true because this is the instr we want > if instr_yes = "true":#start of code to > copy to another file >f2.write(f.line) #write line to > output file > > f.close #close input file > f2.close > > [/code] > > 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 = ''' ; ; test.csd - a Csound structured data file -W -d -o tone.wav ;optional section Before 4.10 ;these two statements check for After 4.08 ; Csound version 4.09 ; originally tone.orc sr = 44100 kr = 4410 ksmps = 10 nchnls = 1 instr 1 a1 oscil p4, p5, 1 ; simple oscillator out a1 endin ; originally tone.sco f1 0 8192 10 1 i1 0 1 2 1000 ;play one second of one kHz tone e 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbe's re question
[EMAIL PROTECTED] 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. > > [code] > def extractCsdInstrument (input_File_Name, output_File_Name, > instr_number): > "takes an .csd input file and grabs instr_number instrument and > creates output_File_Name" > f = open (input_File_Name , 'r') > f2 = open (output_File_Name, 'w') > instr_yes = 'false' # Python has built-in booleans ># Also, use sane naming: isInstr or hasInstr > > for line in f: > if "instr" in line: >if instr_yes == 'true': # Use the bool -- `if hasInstr:` >break > >reline = re.line.split('instr', '/d$') # ??? >number = int(reline[1]) > if number == instr_number: # Doesn't need to be 2 lines > instr_yes = "true": # the `:` is a syntax > error > if instr_yes = "true": >f2.write(f.line) # odd way of doing it... > > f.close() > f2.close() > > [/code] 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: [code] def extractCsdInstrument (inputPath, outputPath, instrId): """ Takes a .csd file, grabs info for instrument ID and writes to file. """ src = open(inputPath, 'r') dst = open(outputPath, 'w') for line in src: if "instr" in line: info_comment = line.split(";", 1) # [info, comment] # Extract integers instrInfo = re.split(r"[^0-9]*", info_comment[0]) # Check the second number in the line if instrInfo[1] == str(instrId): dst.write(instrInfo.append(info_comment[1])) src.close() dst.close() [/code] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbe's re question
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbe's re question
George Sakkis wrote: > [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newbe's re question
[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list