groovy & grails - lecture 6
Upcoming SlideShare
Loading in...5
×
 

groovy & grails - lecture 6

on

  • 403 views

Eclipse tips

Eclipse tips
Productive tip
Two thoughts on code design
Running a script standalone
test case: build all isoforms & extract proteotypic peptides

Statistics

Views

Total Views
403
Views on SlideShare
403
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • today end of a cycle\nnext week: genetic algorithm\nthen web programming\nend of the year exam: bring in your ideas\nplay customer + coder\ncustomer phase with me, then iterative development.\n
  • parsing command line arguments, packaging a project into a library\nIn practice, we will often use web application for end usage\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • Agile: refactor your code show you do not fear it! think of how to deal with a wild animal\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • "You ain't gonna need it" (or YAGNI for short) is the principle in extreme programming that programmers should not add functionality until it is necessary.[1] Ron Jeffries writes, "Always implement things when you actually need them, never when you just foresee that you need them."[2]\n\n
  • not only for fun...\nit is also possible to run a script/project packaging a jar and only with java\n
  • not only for fun...\nit is also possible to run a script/project packaging a jar and only with java\n
  • not only for fun...\nit is also possible to run a script/project packaging a jar and only with java\n
  • not only for fun...\nit is also possible to run a script/project packaging a jar and only with java\n
  • not only for fun...\nit is also possible to run a script/project packaging a jar and only with java\n
  • in real life, you don’t edit a script, save and rerun\nan option is to parse argument from a property file\n
  • in real life, you don’t edit a script, save and rerun\nan option is to parse argument from a property file\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • classes code for action, for re-use, test etc....\n
  • classes code for action, for re-use, test etc....\n
  • classes code for action, for re-use, test etc....\n
  • classes code for action, for re-use, test etc....\n
  • classes code for action, for re-use, test etc....\n
  • can also contains source code (more voluminous but easier for third parties to debug), javadoc...\n
  • can also contains source code (more voluminous but easier for third parties to debug), javadoc...\n
  • can also contains source code (more voluminous but easier for third parties to debug), javadoc...\n
  • can also contains source code (more voluminous but easier for third parties to debug), javadoc...\n
  • can also contains source code (more voluminous but easier for third parties to debug), javadoc...\n
  • in fact, ant can be used for countless tasks, as soon as you needed for automated, dependency oriented process\nan ant file can contained multiple targets (as possibly a default one)\n
  • in fact, ant can be used for countless tasks, as soon as you needed for automated, dependency oriented process\nan ant file can contained multiple targets (as possibly a default one)\n
  • in fact, ant can be used for countless tasks, as soon as you needed for automated, dependency oriented process\nan ant file can contained multiple targets (as possibly a default one)\n
  • in fact, ant can be used for countless tasks, as soon as you needed for automated, dependency oriented process\nan ant file can contained multiple targets (as possibly a default one)\n
  • in fact, ant can be used for countless tasks, as soon as you needed for automated, dependency oriented process\nan ant file can contained multiple targets (as possibly a default one)\n
  • \n
  • \n
  • \n
  • first questions : you build be some example to validate the program\n
  • first questions : you build be some example to validate the program\n
  • first questions : you build be some example to validate the program\n
  • first questions : you build be some example to validate the program\n

groovy & grails - lecture 6 groovy & grails - lecture 6 Presentation Transcript

  • Groovy: Efficiency Oriented ProgrammingLecture 6Master Proteomics & Bioinformatics - University of GenevaAlexandre Masselot - summer 2011
  • Contents‣ Eclipse tips‣ Productive tip‣ Two thoughts on code design‣ Running a script standalone‣ test case: build all isoforms & extract proteotypic peptides
  • The book of the day ‣ by Neal Ford ‣ Do’s and Don’t for programmer, from the rookies to ol’timers...
  • Go productive: clipboard history‣ Working with a computer induces a lot of cut/past (<ctrl>-C / <ctrl>-V)
  • Go productive: clipboard history‣ Working with a computer induces a lot of cut/past (<ctrl>-C / <ctrl>-V)‣ In practice, you often refer (or intent to refer) to a piece of text you cut a few minutes ago
  • Go productive: clipboard history‣ Working with a computer induces a lot of cut/past (<ctrl>-C / <ctrl>-V)‣ In practice, you often refer (or intent to refer) to a piece of text you cut a few minutes ago‣ It is possible to use clipboard history (and <ctrl>-V’ offers to visit history) - mac OS: jumpcut - ubuntu: parcellite - windows: CLCL
  • Go productive: clipboard history‣ Working with a computer induces a lot of cut/past (<ctrl>-C / <ctrl>-V)‣ In practice, you often refer (or intent to refer) to a piece of text you cut a few minutes ago‣ It is possible to use clipboard history (and <ctrl>-V’ offers to visit history) - mac OS: jumpcut - ubuntu: parcellite - windows: CLCL‣ What a big deal? - such functionalities has been present in editors such as vi or x?emacs for decades...
  • Eclipse tips
  • Eclipse tips‣ create documentation for a method (filled with method parameters) - place the cursor on the method name - right button > source > generate comment - or <ctrl>-<alt>-J - documentation is ready to be exported in a html javadoc
  • Eclipse tips
  • Eclipse tips‣ How to know where a method is called? - place the cursor on the method declaration - right button > open call hierarchy - or <ctrl>-<alt>-H
  • DRY: Don’t Repeat Yourself 7
  • Code design: DRY‣ <ctrl>-C / <ctrl>-V is the most common shortcut appealing to duplicate a piece of “working” code into another part of the software
  • Code design: DRY‣ Problems:
  • Code design: DRY‣ Problems: - harder to understand where a given action is coded,
  • Code design: DRY‣ Problems: - harder to understand where a given action is coded, - hard to maintain bug.
  • Code design: DRY‣ Fight against DRY:
  • Code design: DRY‣ Fight against DRY: - refactor your code continuously (test are there to ensure stability).
  • Code design: DRY‣ Fight against DRY: - refactor your code continuously (test are there to ensure stability). - tackle duplication in your process at any level
  • Code design: DRY‣ Fight against DRY: - refactor your code continuously (test are there to ensure stability). - tackle duplication in your process at any level - time spent for factorizing process or code is almost never a waste
  • YAGNI: You Ain’t Gonna Need It 11
  • Code design: YAGNI‣ A very common pitfall when writing code it to follow some enthusiasm and imagine features that are (on the instant you imagine them):
  • Code design: YAGNI‣ A very common pitfall when writing code it to follow some enthusiasm and imagine features that are (on the instant you imagine them): - very cool,
  • Code design: YAGNI‣ A very common pitfall when writing code it to follow some enthusiasm and imagine features that are (on the instant you imagine them): - very cool, - you have a bright idea on how to implement them,
  • Code design: YAGNI‣ A very common pitfall when writing code it to follow some enthusiasm and imagine features that are (on the instant you imagine them): - very cool, - you have a bright idea on how to implement them, - even though they don’t answer a particular need at that time, you “feel” they would be “cool to have in the future”
  • Code design: YAGNI‣ Problems:
  • Code design: YAGNI‣ Problems: - the code get more complex, harder to maintain,
  • Code design: YAGNI‣ Problems: - the code get more complex, harder to maintain, - in a few weeks of time, this feature won’t be of any use (but other will!)
  • Code design: YAGNI‣ Fight YAGNI:
  • Code design: YAGNI‣ Fight YAGNI: - stick to your customer practical needs
  • Code design: YAGNI‣ Fight YAGNI: - stick to your customer practical needs - pair programming is the most efficient way to tackle such situations
  • Groovy: running a script (outside the IDE)‣ Programming goal is (often) to have a runnable program, outside the IDE
  • Groovy: running a script (outside the IDE)‣ Programming goal is (often) to have a runnable program, outside the IDE‣ Be able to deploy the program on another machine
  • Groovy: running a script (outside the IDE)‣ Programming goal is (often) to have a runnable program, outside the IDE‣ Be able to deploy the program on another machine‣ First: install Groovy on the machine (apt-get install groovy etc.)
  • Groovy: running a script (outside the IDE)‣ Programming goal is (often) to have a runnable program, outside the IDE‣ Be able to deploy the program on another machine‣ First: install Groovy on the machine (apt-get install groovy etc.)‣ Let’s consider a simple script hw.groovy, stored in directory /some/path println “hello, world”
  • Groovy: running a script (outside the IDE)‣ Programming goal is (often) to have a runnable program, outside the IDE‣ Be able to deploy the program on another machine‣ First: install Groovy on the machine (apt-get install groovy etc.)‣ Let’s consider a simple script hw.groovy, stored in directory /some/path println “hello, world”‣ launch the script from command line groovy hw.groovy
  • Parsing command line arguments‣ Runtime arguments are passed on the command line
  • Parsing command line arguments‣ Runtime arguments are passed on the command line‣ Defining the possible arguments def cli = new CliBuilder(usage:myscript.groovy [options]) //binary argument -h or --help and usage text cli.h(longOpt:help, usage information) //compulsory argument for -n or --name=<user name> cli.n(longOpt:name, argName:user name, args:1, required: true, some user to salute) //facultative argument --int-value=<int> cli.i(longOpt:int-value, argName:int, args:1, some integer value)
  • Parsing command line arguments (cont’d)‣ Parsing the command line def options = cli.parse(args) if (!options) return
  • Parsing command line arguments (cont’d)‣ Parsing the command line def options = cli.parse(args) if (!options) return‣ Check --help if (options.help) cli.usage()
  • Parsing command line arguments (cont’d)‣ Parsing the command line def options = cli.parse(args) if (!options) return‣ Check --help if (options.help) cli.usage()‣ Arguments usage: println "Hello $options.name (${options[int-value]})"
  • Parsing command line arguments (cont’d)‣ groovy myscript.groovy --helpusage: myscript.groovy [options] -h,--help usage information -i,--int-value <int> some integer value -n,--name <user name> some user to salute
  • Parsing command line arguments (cont’d)‣ groovy myscript.groovy --helpusage: myscript.groovy [options] -h,--help usage information -i,--int-value <int> some integer value -n,--name <user name> some user to salute‣ groovy myscript.groovy --name=alice --int-value=234Hello alice (234)
  • Parsing command line arguments (cont’d)‣ groovy myscript.groovy --help usage: myscript.groovy [options] -h,--help usage information -i,--int-value <int> some integer value -n,--name <user name> some user to salute‣ groovy myscript.groovy --name=alice --int-value=234 Hello alice (234)‣ More doc on http://groovy.codehaus.org/gapi/groovy/util/CliBuilder.html
  • Parsing command line arguments (cont’d)‣ The whole script: def cli = new CliBuilder(usage:myscript.groovy [options]) cli.h(longOpt:help, usage information) cli.n(longOpt:name, argName:user name, args:1, required: true, some user to salute) cli.i(longOpt:int-value, argName:int, args:1, some integer value) def options = cli.parse(args) if (!options) return if (options.help) cli.usage() println "Hello $options.name (${options[int-value]}) "
  • Running a script with a packaged library‣ A script by itself is soon meaningless, it is often backed by a library of packaged classes for doing the actual business
  • Running a script with a packaged library‣ A script by itself is soon meaningless, it is often backed by a library of packaged classes for doing the actual business‣ The script is often the last assembly of coded (and tested) functionalities
  • Running a script with a packaged library‣ A script by itself is soon meaningless, it is often backed by a library of packaged classes for doing the actual business‣ The script is often the last assembly of coded (and tested) functionalities‣ Library is packaged as a jar (Java Archive) and the script is written on the side, making calls to the archive
  • Running a script with a packaged library‣ A script by itself is soon meaningless, it is often backed by a library of packaged classes for doing the actual business‣ The script is often the last assembly of coded (and tested) functionalities‣ Library is packaged as a jar (Java Archive) and the script is written on the side, making calls to the archive‣ The jar if often the distributed part of a code
  • Running a script with a packaged library‣ A script by itself is soon meaningless, it is often backed by a library of packaged classes for doing the actual business‣ The script is often the last assembly of coded (and tested) functionalities‣ Library is packaged as a jar (Java Archive) and the script is written on the side, making calls to the archive‣ The jar if often the distributed part of a code‣ The script varsplic.groovy uses the jar import unige.mpb.eop.proteomics.uniprot.UniprotEntrySplicer ... def xml=new XmlSlurper() .parseText("http://pir.uniprot.org/uniprot/${ac}.xml" .toURL().text) .entry UniprotEntrySplicer splicer=[entryXml:xml] splicer.buildAllIsoforms().values().each{print it}
  • Eclipse: exporting a jar from the IDE‣ A jar is an archive that contains at least the compiled classes
  • Eclipse: exporting a jar from the IDE‣ A jar is an archive that contains at least the compiled classes‣ Test classes are usually not exported
  • Eclipse: exporting a jar from the IDE‣ A jar is an archive that contains at least the compiled classes‣ Test classes are usually not exported‣ Exporting from eclipse - project > right button > export > java > jar
  • Eclipse: exporting a jar from the IDE‣ A jar is an archive that contains at least the compiled classes‣ Test classes are usually not exported‣ Exporting from eclipse - project > right button > export > java > jar‣ A myfile.jar file is created
  • Eclipse: exporting a jar from the IDE‣ A jar is an archive that contains at least the compiled classes‣ Test classes are usually not exported‣ Exporting from eclipse - project > right button > export > java > jar‣ A myfile.jar file is created
  • Eclipse: exporting a jar from ant‣ Apache ant is a versatile tool for automatic software build process
  • Eclipse: exporting a jar from ant‣ Apache ant is a versatile tool for automatic software build process‣ It can be used to build a task: the build.xml (at project home directory) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <project default="create_run_jar" name="Create Runnable Jar for Project eop.lec6"> <targetname="create_run_jar"> <jar destfile="eop6.jar"> <fileset dir="bin"> <exclude name="**/*Test.class"/> <exclude name="src/practicals/groovy/scripts/*"/> </fileset> </jar> </target> </project>
  • Eclipse: exporting a jar from ant‣ Apache ant is a versatile tool for automatic software build process‣ It can be used to build a task: the build.xml (at project home directory) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <project default="create_run_jar" name="Create Runnable Jar for Project eop.lec6"> <targetname="create_run_jar"> <jar destfile="eop6.jar"> <fileset dir="bin"> <exclude name="**/*Test.class"/> <exclude name="src/practicals/groovy/scripts/*"/> </fileset> </jar> </target> </project>‣ right button > run as > ant build
  • Eclipse: exporting a jar from ant‣ Apache ant is a versatile tool for automatic software build process‣ It can be used to build a task: the build.xml (at project home directory) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <project default="create_run_jar" name="Create Runnable Jar for Project eop.lec6"> <targetname="create_run_jar"> <jar destfile="eop6.jar"> <fileset dir="bin"> <exclude name="**/*Test.class"/> <exclude name="src/practicals/groovy/scripts/*"/> </fileset> </jar> </target> </project>‣ right button > run as > ant build‣ <ctrl>-<alt>-X-Q
  • Eclipse: exporting a jar from ant‣ Apache ant is a versatile tool for automatic software build process‣ It can be used to build a task: the build.xml (at project home directory) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <project default="create_run_jar" name="Create Runnable Jar for Project eop.lec6"> <targetname="create_run_jar"> <jar destfile="eop6.jar"> <fileset dir="bin"> <exclude name="**/*Test.class"/> <exclude name="src/practicals/groovy/scripts/*"/> </fileset> </jar> </target> </project>‣ right button > run as > ant build‣ <ctrl>-<alt>-X-Q‣ http://ant.apache.org/manual/tasksoverview.html
  • Executing a script with a jar library‣ The script varsplic.groovy can reside anywhere groovy -cp eop6.jar /path/to/varsplic.groovy --ac=Q70Z44
  • Executing a script with a jar library‣ The script varsplic.groovy can reside anywhere groovy -cp eop6.jar /path/to/varsplic.groovy --ac=Q70Z44‣ Arguments following the script will be passed to the script args
  • Executing a script with a jar library‣ The script varsplic.groovy can reside anywhere groovy -cp eop6.jar /path/to/varsplic.groovy --ac=Q70Z44‣ Arguments following the script will be passed to the script args‣ Remember: put the least possible “business intelligence” into the script
  • Test case: isoforms & proteotypic peptides‣ Proteotypic peptide: given a list of proteins and cleavage enzyme, a proteotypic peptide is a peptide that can be produced only by one of those proteins
  • Test case: isoforms & proteotypic peptides‣ Proteotypic peptide: given a list of proteins and cleavage enzyme, a proteotypic peptide is a peptide that can be produced only by one of those proteins‣ Customer of the day: “I want to give you one uniprot AC, you print me all the isoforms in a fasta format and the list list of proteotypic peptide (+ the isoform they are referring to). Please.”
  • Test case: isoforms & proteotypic peptides‣ Proteotypic peptide: given a list of proteins and cleavage enzyme, a proteotypic peptide is a peptide that can be produced only by one of those proteins‣ Customer of the day: “I want to give you one uniprot AC, you print me all the isoforms in a fasta format and the list list of proteotypic peptide (+ the isoform they are referring to). Please.”‣ Hopefully enough, you already have a toolkit to build, from a uniprot entry, a map isoform name -> isoform Protein
  • Test case: isoforms & proteotypic peptides‣ Proteotypic peptide: given a list of proteins and cleavage enzyme, a proteotypic peptide is a peptide that can be produced only by one of those proteins‣ Customer of the day: “I want to give you one uniprot AC, you print me all the isoforms in a fasta format and the list list of proteotypic peptide (+ the isoform they are referring to). Please.”‣ Hopefully enough, you already have a toolkit to build, from a uniprot entry, a map isoform name -> isoform Protein‣ Your turn to play....