Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2006-07-12 00:19:47
Size: 196
Editor: 12-214-79-52
Comment:
Revision 11 as of 2008-11-15 09:16:03
Size: 9456
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
||<tablestyle="width: 100%" rowstyle="background-color: #FFFFE0" style="width: 80%"> '''''Jython Monthly''''' ||<:>http://www.jython.org/css/jython.png || ||<tablestyle="width: 100%" rowstyle="background-color: #FFFFE0" style="width: 80%"> '''''Jython Monthly''''' ||<:>{{http://www.jython.org/css/jython.png}} ||
Line 3: Line 3:

Welcome to the another issue of Jython Monthly. The content of this newsletter will focus on using and developing the Jython languge. I want to encourage the readers of Jython Monthly to send any articles, tips, tricks, or any other Jython related material to me if you think it should be distributed with this newsletter.

- Josh Juneau

Questions, comments, or suggestions???

Please send email to:

jython-monthly@mchsi.com or jython-users@lists.sourceforge.net for discussion.


= Articles =

== Add Logging to Jython Using Log4J ==
http://wiki.python.org/jython/JythonMonthly/Articles/August2006/1

'' Submitted by: Josh Juneau ''

Are you still using the Jython ''print'' command to show your errors? How about in a production environment, are you using any formal logging? If not, you should be doing so...and the Apache log4j API makes it easy to do so. Many Java developers have grown to love the log4j API and it is utilized throughout much of the community. That is great news for Jython developers since we've got direct access to Java libraries!

=== Setting Up Your Environment ===

The most difficult part about using log4j with Jython is the setup. You must ensure that the log4j.jar archive resides somewhere within your Jython PATH (usually this entails setting the CLASSPATH to include necessary files in Windows environment). You then set up a properties file for use with log4j. Within the properties file, you can include appender information, where logs should reside, and much more.

Example properties file:
{{{
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:\\Jython\\testlog4j.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
}}}

You are now ready to use log4j in your Jython application. As you can see, if you've ever used log4j with Java, it is pretty much the same.

=== Using log4j in a Jython Application ===

Once again, using log4j within a Jython application is very similar to it's usage in the Java world.

First, you must import the log4j packages:

{{{
from org.apache.log4j import *
}}}

Second, you obtain a new logger for your class or module and set up a PropertyConfigurator:

{{{
 Logger logger = Logger.getLogger("myClass")
  # Assume that the log4j properties resides within a folder named "utilities"
  PropertyConfigurator.configure(sys.path[0] + "/utilities/log4j.properties")
}}}

Lastly, use log4j:

{{{
  # Example module within the class:
   def submitDocument(self, event):
       try:
           # Assume we perform some SQL here
       except SQLException, ex:
           self.logger.error("docPanel#submitDocument ERROR: %s" % (ex))
}}}

Your logging will now take place within the file you specified in the properties file for ''log4j.appender.R.File''.

=== Using log4j in Jython Scripts ===

Many may ask, why in the world would you be interested in logging information about your scripts? Most of the time a script is executed interactively via the command line. However, there are plenty of instances where it makes sense to have the system invoke a script for you. As you probably know, this technique is used quite often within an environment to run nightly tasks, or even daily tasks which are automatically invoked on a scheduled basis. For these cases, it can be extremely useful to log errors or information using log4j. Some may even wish to create a separate automated task to email these log files after the tasks complete.

The overall implementation is the same as above, the most important thing to remember is that you must have the log4j.jar archive and properties file within your Jython path. Once this is ready to go you can use log4j in your script.

{{{
 from org.apache.log4j import *
 logger = Logger.getLogger("scriptname")
 PropertyConfigurator.configure("C:\path_to_properties\log4j.properties")
 logger.info("Test the logging")
}}}

=== Future Possibilities ===

Log4J is nice, but there are other logging frameworks which could be extremely useful in Jython as well. As a matter of fact, there is a new logging "anti-framework" which is called SimpleLog 2.0 and it looks helpful. I haven't yet reviewed SimpleLog 2.0, but I plan to do so in the near future!

Check it out at: [[https://simple-log.dev.java.net/]]


= Tips and Tricks =

== Create a Jython Time Profiler ==

A module that helps to inject time profiling code
in other modules to measure actual execution times
of blocks of code. This has been tested and verified with Jython!

[[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496938|Python Cookbook Recipe]]

= BECOME THE FIRST JYTHON DEVELOPER ON SUN GRID AND WIN YOUR SHARE OF $50,000 IN PRIZES! =

                               {{https://coolapps.developer.network.com/banner.JPG}}


Test your skills with the Coolaps Developer Challenge:
There are two ways to compete - the best application that runs on Sun Grid Compute Utility,
and the best application built with the Compute Server Plugin for NetBeans.
We designed things this way to allow as many people as possible to participate, including International participants,
who cannot access the Sun Grid Compute Utility today.

Learn more about the Coolapps Developer Challenge: https://coolapps.developer.network.com/

= Off The Lists =

Question:
''I would like to know what you think is the best way to recognize -
right at the beginning of a script -, whether the code is run by
Jython or pure Python interpreter, because I will get ImportError when
Python sees the "import java" lines etc. (also if the script is part
of a package and another module is imported)''

Answers:

{{{import sys
if sys.platform.startswith('java'):
    ...}}}
OR
{{{import sys
isJython = hasattr(sys,'classLoader')
}}}
OR
{{{import sys

if sys.platform.startswith('java'):
   JAVA = 1
else:
   JAVA=0
}}}

Pydev and Pydev Extensions 1.2.2 have been released

Details on Pydev Extensions: http://www.fabioz.com/pydev
Details on Pydev: http://pydev.sf.net
Details on its development: http://pydev.blogspot.com



= Mentionable Books =

Check out this new book by ''Kuassi Mensah'' entitled [[http://www.amazon.com/gp/product/1555583296/102-2444634-2799355?redirect=true|Oracle Database Programming Using Java and Web Services]]. It includes some experimental examples of running Jython in the database using Java runtime.

Check out this blog for more details:
[[http://db360.blogspot.com/2006/08/oracle-database-programming-using-java_01.html|Kuassi Mensah Blog]]



= Interested in Developing Jython? =

If you are interested in developing Jython, please take a look at the [[http://sourceforge.net/tracker/?func=browse&group_id=12867&atid=112867|current bug listing]] and submit patches for items which you can repair.

[[http://www.informit.com/articles/article.asp?p=26127&rl=1|Oldie but goodie for those interested in helping to develop Jython]]


= Who's Using Jython? =

[[http://www.pythonthreads.com/news/latest/activegrid-chooses-jython-to-bring-lamp-development-closer-to-java..html|ActiveGrid chooses Jython to bring LAMP closer to Java]]

[[http://sourceforge.net/projects/protocard|ProtoCard]]

[[http://grinder.sourceforge.net/index.html|Grinder - Java Load Testing Framework]]

= IDE Information =

[[http://sourceforge.net/projects/jyconsole|JyConsole]]

JyConsole is an advanced Java graphic console for Jython. JyConsole provides an object-oriented completion on Java and Python objects and allows the direct manipulation of objects. JyConsole includes command history, script loading and GUI preference.

How To Get Cluster Name via Jython in wsadmin?

http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?forum=266&thread=123951&cat=9

= Jython Blogs =

Jython Considered Dangerous? - http://dev2dev.bea.com/blog/simonvc/archive/2006/07/jython_consider.html

Scripting Oracle with Jython - http://prpi.blogspot.com/2006/05/scripting-oracle-with-jython.html

Jython, Swing & Curry - http://www.smallshire.org.uk/sufficientlysmall/2005/12/03/jython-swing-curry/

= Interesting Facts =

Jython - Average Job Salary & Stats in UK http://www.itjobswatch.co.uk/jobs/uk/jython.do

= Useful Links =

||<tablestyle="width: 80%"> '''Articles ''' ||
||||
|| [[http://jroller.com/page/techkriti?entry=using_jython_to_test_hibernate|Using Jython to Test Hibernate Criteria Queries]] ||

||<tablestyle="width: 80%"> '''Links ''' ||
||||
|| [[http://www.jython.org|Jython Home]] ||
|| [[http://www.python.org|Python Home]] ||
|| [[http://en.wikipedia.org/wiki/Jython|Jython WikiPedia]] ||
|| [[http://freshmeat.net/projects/jython/|Freshmeat.net]] ||

Jython Monthly

http://www.jython.org/css/jython.png

August 2006 -- Issue #2

Welcome to the another issue of Jython Monthly. The content of this newsletter will focus on using and developing the Jython languge. I want to encourage the readers of Jython Monthly to send any articles, tips, tricks, or any other Jython related material to me if you think it should be distributed with this newsletter.

- Josh Juneau

Questions, comments, or suggestions???

Please send email to:

jython-monthly@mchsi.com or jython-users@lists.sourceforge.net for discussion.

Articles

Add Logging to Jython Using Log4J

http://wiki.python.org/jython/JythonMonthly/Articles/August2006/1

Submitted by: Josh Juneau

Are you still using the Jython print command to show your errors? How about in a production environment, are you using any formal logging? If not, you should be doing so...and the Apache log4j API makes it easy to do so. Many Java developers have grown to love the log4j API and it is utilized throughout much of the community. That is great news for Jython developers since we've got direct access to Java libraries!

Setting Up Your Environment

The most difficult part about using log4j with Jython is the setup. You must ensure that the log4j.jar archive resides somewhere within your Jython PATH (usually this entails setting the CLASSPATH to include necessary files in Windows environment). You then set up a properties file for use with log4j. Within the properties file, you can include appender information, where logs should reside, and much more.

Example properties file:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:\\Jython\\testlog4j.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

You are now ready to use log4j in your Jython application. As you can see, if you've ever used log4j with Java, it is pretty much the same.

Using log4j in a Jython Application

Once again, using log4j within a Jython application is very similar to it's usage in the Java world.

First, you must import the log4j packages:

from org.apache.log4j import * 

Second, you obtain a new logger for your class or module and set up a PropertyConfigurator:

 Logger logger = Logger.getLogger("myClass")
  # Assume that the log4j properties resides within a folder named "utilities"
  PropertyConfigurator.configure(sys.path[0] + "/utilities/log4j.properties")

Lastly, use log4j:

  # Example module within the class:
   def submitDocument(self, event):
       try:
           # Assume we perform some SQL here              
       except SQLException, ex:
           self.logger.error("docPanel#submitDocument ERROR: %s" % (ex)) 

Your logging will now take place within the file you specified in the properties file for log4j.appender.R.File.

Using log4j in Jython Scripts

Many may ask, why in the world would you be interested in logging information about your scripts? Most of the time a script is executed interactively via the command line. However, there are plenty of instances where it makes sense to have the system invoke a script for you. As you probably know, this technique is used quite often within an environment to run nightly tasks, or even daily tasks which are automatically invoked on a scheduled basis. For these cases, it can be extremely useful to log errors or information using log4j. Some may even wish to create a separate automated task to email these log files after the tasks complete.

The overall implementation is the same as above, the most important thing to remember is that you must have the log4j.jar archive and properties file within your Jython path. Once this is ready to go you can use log4j in your script.

 from org.apache.log4j import *
 logger = Logger.getLogger("scriptname")
 PropertyConfigurator.configure("C:\path_to_properties\log4j.properties")
 logger.info("Test the logging")

Future Possibilities

Log4J is nice, but there are other logging frameworks which could be extremely useful in Jython as well. As a matter of fact, there is a new logging "anti-framework" which is called SimpleLog 2.0 and it looks helpful. I haven't yet reviewed SimpleLog 2.0, but I plan to do so in the near future!

Check it out at: https://simple-log.dev.java.net/

Tips and Tricks

Create a Jython Time Profiler

A module that helps to inject time profiling code in other modules to measure actual execution times of blocks of code. This has been tested and verified with Jython!

Python Cookbook Recipe

BECOME THE FIRST JYTHON DEVELOPER ON SUN GRID AND WIN YOUR SHARE OF $50,000 IN PRIZES!

  • https://coolapps.developer.network.com/banner.JPG

Test your skills with the Coolaps Developer Challenge: There are two ways to compete - the best application that runs on Sun Grid Compute Utility, and the best application built with the Compute Server Plugin for NetBeans. We designed things this way to allow as many people as possible to participate, including International participants, who cannot access the Sun Grid Compute Utility today.

Learn more about the Coolapps Developer Challenge: https://coolapps.developer.network.com/

Off The Lists

Question: I would like to know what you think is the best way to recognize - right at the beginning of a script -, whether the code is run by Jython or pure Python interpreter, because I will get ImportError when Python sees the "import java" lines etc. (also if the script is part of a package and another module is imported)

Answers:

{{{import sys if sys.platform.startswith('java'):

  • ..}}}

OR {{{import sys isJython = hasattr(sys,'classLoader') }}} OR {{{import sys

if sys.platform.startswith('java'):

  • JAVA = 1

else:

  • JAVA=0

}}}

Pydev and Pydev Extensions 1.2.2 have been released

Details on Pydev Extensions: http://www.fabioz.com/pydev Details on Pydev: http://pydev.sf.net Details on its development: http://pydev.blogspot.com

Mentionable Books

Check out this new book by Kuassi Mensah entitled Oracle Database Programming Using Java and Web Services. It includes some experimental examples of running Jython in the database using Java runtime.

Check out this blog for more details: Kuassi Mensah Blog

Interested in Developing Jython?

If you are interested in developing Jython, please take a look at the current bug listing and submit patches for items which you can repair.

Oldie but goodie for those interested in helping to develop Jython

Who's Using Jython?

ActiveGrid chooses Jython to bring LAMP closer to Java

ProtoCard

Grinder - Java Load Testing Framework

IDE Information

JyConsole

JyConsole is an advanced Java graphic console for Jython. JyConsole provides an object-oriented completion on Java and Python objects and allows the direct manipulation of objects. JyConsole includes command history, script loading and GUI preference.

How To Get Cluster Name via Jython in wsadmin?

http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?forum=266&thread=123951&cat=9

Jython Blogs

Jython Considered Dangerous? - http://dev2dev.bea.com/blog/simonvc/archive/2006/07/jython_consider.html

Scripting Oracle with Jython - http://prpi.blogspot.com/2006/05/scripting-oracle-with-jython.html

Jython, Swing & Curry - http://www.smallshire.org.uk/sufficientlysmall/2005/12/03/jython-swing-curry/

Interesting Facts

Jython - Average Job Salary & Stats in UK http://www.itjobswatch.co.uk/jobs/uk/jython.do

Useful Links

Articles

Using Jython to Test Hibernate Criteria Queries

Links

Jython Home

Python Home

Jython WikiPedia

Freshmeat.net

JythonMonthly/Newsletters/August2006 (last edited 2008-11-15 09:16:03 by localhost)