newbe's re question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric_Dexter@msn.com

    #1

    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

    Explore Australia’s vibrant culinary scene, featuring local ingredients and international influences. Discover diverse dining options, cultural fusion, and sustainable practices in Brisbane's dynamic food landscape.


    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
  • George Sakkis

    #2
    Re: newbe's re question

    Eric_Dexter@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

    Comment

    • Eric_Dexter@msn.com

      #3
      Re: newbe's re question


      George Sakkis wrote:
      Eric_Dexter@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

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



      Comment

      • MonkeeSage

        #4
        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

        Comment

        • Eric

          #5
          Re: newbe's re question

          Eric_Dexter@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.
          >
          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()
          >
          Did you read the HOWTO on regular expressions yet? It's included with
          the standard documentation, but here's a link:



          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()
          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.

          Comment

          • Frederic Rentsch

            #6
            Re: newbe's re question

            Eric_Dexter@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
            >
            Explore Australia’s vibrant culinary scene, featuring local ingredients and international influences. Discover diverse dining options, cultural fusion, and sustainable practices in Brisbane's dynamic food landscape.

            >
            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
            >
            >
            >
            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 = '''
            <CsoundSynthesi zer>;
            ; 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>

            <CsInstrument s>
            ; 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>

            </CsoundSynthesiz er>

            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

            Comment

            • Eric_Dexter@msn.com

              #7
              Re: newbe's re question


              Frederic Rentsch wrote:
              Eric_Dexter@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

              Explore Australia’s vibrant culinary scene, featuring local ingredients and international influences. Discover diverse dining options, cultural fusion, and sustainable practices in Brisbane's dynamic food landscape.


              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
              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 = '''
              <CsoundSynthesi zer>;
              ; 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>
              >
              <CsInstrument s>
              ; 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>
              >
              </CsoundSynthesiz er>
              >
              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..



              Comment

              • Frederic Rentsch

                #8
                Re: newbe's re question

                Eric_Dexter@msn .com wrote:
                Frederic Rentsch wrote:
                >
                >Eric_Dexter@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..
                >>>
                >>>
                >>>
                >>>
                >>>
                >>>
                >>
                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
                >>>
                >>
                >>>
                >>>
                >>>
                >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 = '''
                > <CsoundSynthesi zer>;
                > ; 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>
                >>
                > <CsInstrument s>
                > ; 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>
                >>
                > </CsoundSynthesiz er>
                >>
                >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..
                >

                >
                >
                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




                Comment

                • Eric_Dexter@msn.com

                  #9
                  Re: newbe's re question


                  Frederic Rentsch wrote:
                  Eric_Dexter@msn .com wrote:
                  Frederic Rentsch wrote:
                  Eric_Dexter@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..
                  >>
                  >>
                  >>
                  >>
                  >>
                  >>
                  >
                  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
                  >>
                  >
                  >>
                  >>
                  >>
                  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 = '''
                  <CsoundSynthesi zer>;
                  ; 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>
                  >
                  <CsInstrument s>
                  ; 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>
                  >
                  </CsoundSynthesiz er>
                  >
                  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..


                  >
                  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.

                  Comment

                  • Frederic Rentsch

                    #10
                    Re: newbe's re question

                    Eric_Dexter@msn .com wrote:
                    Frederic Rentsch wrote:
                    >
                    >Eric_Dexter@msn .com wrote:
                    >>
                    >>Frederic Rentsch wrote:
                    >>>
                    >>>
                    >>>Eric_Dexter@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..
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>>
                    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
                    >>>>>
                    >>>>
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>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 = '''
                    >>> <CsoundSynthesi zer>;
                    >>> ; 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>
                    >>>>
                    >>> <CsInstrument s>
                    >>> ; 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>
                    >>>>
                    >>> </CsoundSynthesiz er>
                    >>>>
                    >>>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?
                    >>>(Instrumen t 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

                    Comment

                    • Eric_Dexter@msn.com

                      #11
                      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)


                      ...


                      Frederic Rentsch wrote:
                      Eric_Dexter@msn .com wrote:
                      Frederic Rentsch wrote:
                      Eric_Dexter@msn .com wrote:
                      >
                      >Frederic Rentsch wrote:
                      >>
                      >>
                      >>Eric_Dexter@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..
                      >>>>
                      >>>>
                      >>>>
                      >>>>
                      >>>>
                      >>>>
                      >>>
                      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
                      >>>>
                      >>>
                      >>>>
                      >>>>
                      >>>>
                      >>>>
                      >>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 = '''
                      >> <CsoundSynthesi zer>;
                      >> ; 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>
                      >>>
                      >> <CsInstrument s>
                      >> ; 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>
                      >>>
                      >> </CsoundSynthesiz er>
                      >>>
                      >>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

                      Comment

                      • Frederic Rentsch

                        #12
                        Re: newbe's re question

                        Eric_Dexter@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)
                        >

                        ..
                        >
                        >
                        Frederic Rentsch wrote:
                        >
                        >Eric_Dexter@msn .com wrote:
                        >>
                        >>Frederic Rentsch wrote:
                        >>>
                        >>>
                        >>>Eric_Dexter@msn .com wrote:
                        >>>>
                        >>>>
                        >>>>Frederic Rentsch wrote:
                        >>>>>
                        >>>>>
                        >>>>>
                        >>>>>Eric_Dexter@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..
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>
                        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
                        >>>>>>>
                        >>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>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 = '''
                        >>>>> <CsoundSynthesi zer>;
                        >>>>> ; 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>
                        >>>>>>
                        >>>>> <CsInstrument s>
                        >>>>> ; 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>
                        >>>>>>
                        >>>>> </CsoundSynthesiz er>
                        >>>>>>
                        >>>>>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?
                        >>>>>(Instrumen t 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.utexa s.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_formatte r (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.String IO ('''; 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*k ontrol,1 ;vibrato unit
                        kpitch = cpsoct(octpch(p 5)+kvib) ;varying fund pitch in hz
                        continue:
                        kamp linen p4,irise,p3,ide cay
                        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_format ter (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*k ontrol,1 ;vibrato unit
                        kpitch = cpsoct(octpch(p 5)+kvib) ;varying
                        fund pitch in hz
                        continue:
                        kamp linen p4,irise,p3,ide cay
                        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

                        Comment

                        • Eric_Dexter@msn.com

                          #13
                          Re: newbe's re question


                          Frederic Rentsch wrote:
                          Eric_Dexter@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)


                          ..


                          Frederic Rentsch wrote:
                          Eric_Dexter@msn .com wrote:
                          >
                          >Frederic Rentsch wrote:
                          >>
                          >>
                          >>Eric_Dexter@msn .com wrote:
                          >>>
                          >>>
                          >>>Frederic Rentsch wrote:
                          >>>>
                          >>>>
                          >>>>
                          >>>>Eric_Dexter@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..
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>
                          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
                          >>>>>>
                          >>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>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 = '''
                          >>>> <CsoundSynthesi zer>;
                          >>>> ; 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>
                          >>>>>
                          >>>> <CsInstrument s>
                          >>>> ; 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>
                          >>>>>
                          >>>> </CsoundSynthesiz er>
                          >>>>>
                          >>>>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?
                          >>>>(Instrume nt 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.utexa s.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_formatte r (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.String IO ('''; 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*k ontrol,1 ;vibrato unit
                          kpitch = cpsoct(octpch(p 5)+kvib) ;varying fund pitch in hz
                          continue:
                          kamp linen p4,irise,p3,ide cay
                          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_format ter (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*k ontrol,1 ;vibrato unit
                          kpitch = cpsoct(octpch(p 5)+kvib) ;varying
                          fund pitch in hz
                          continue:
                          kamp linen p4,irise,p3,ide cay
                          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.



                          Comment

                          • Frederic Rentsch

                            #14
                            Re: newbe's re question

                            Eric_Dexter@msn .com wrote:
                            Frederic Rentsch wrote:
                            >
                            >Eric_Dexter@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.
                            >

                            >
                            >
                            Eric,

                            Below the dotted line there are two functions.
                            "csound_fil ter ()" 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_instrumen t_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.utex as.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_na me,
                            name_of_formatt ed_instrument_b locks_file = sys.stdout):

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

                            csound_formatte r (csound_file_na me,
                            name_of_formatt ed_instrument_b locks_file)
                            csound_formatte r (csound_file_na me) # Single argument: screen output

                            """

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

                            INDENT = 10
                            CODE_LENGTH = 50

                            def format ():
                            for l in instruments_fil e:
                            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_fil e_name = Instruments_Fil ter (csound_file_na me)
                            instruments_fil e = file (instruments_fi le_name, 'w+a')
                            if name_of_formatt ed_instrument_b locks_file != sys.stdout:
                            out_file = file (name_of_format ted_instrument_ blocks_file, 'wa')
                            owns_out_file = True
                            else:
                            out_file = name_of_formatt ed_instrument_b locks_file
                            owns_out_file = False

                            format ()

                            if owns_out_file: out_file.close ()



                            def make_instrument _dictionaries (name_of_format ted_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_numb ers_re = re.compile ('instr *([0-9,]+)')

                            in_file = file (name_of_format ted_instrument_ blocks_file, 'ra')

                            instrument_desc riptions_by_id = {}
                            instrument_ids_ by_description = {}

                            inside = False
                            for line in in_file:
                            line = line.strip ()
                            if not inside:
                            if line:
                            instrument_desc ription = line [1:].rstrip ()
                            inside = True
                            else:
                            if line == '':
                            inside = False
                            if inside:
                            if line.startswith ('instr'):
                            instrument_numb ers = instrument_numb ers_re.match
                            (line).group (1).split (',')
                            instrument_ids = ['%s: %s' %
                            (name_of_format ted_instrument_ blocks_file, i_n) for i_n in
                            instrument_numb ers]
                            instrument_ids_ by_description [instrument_desc ription] =
                            instrument_ids
                            for instrument_id in instrument_ids:
                            instrument_desc riptions_by_id [instrument_id] =
                            instrument_desc ription

                            in_file.close ()

                            return instrument_desc riptions_by_id, instrument_ids_ by_description

                            ---------------------------------------------------------------------------------------------
                            >>for instrument_id in instrument_desc riptions_by_id:
                            print '%s - %s' % (instrument_id,
                            instrument_desc riptions_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

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

                            Comment

                            Working...