<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jim Cassidy &#187; Databases</title>
	<atom:link href="http://jimcassidy.ca/category/best-practice/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://jimcassidy.ca</link>
	<description>Programming for fun and profit since 1989</description>
	<lastBuildDate>Mon, 02 Jan 2012 18:52:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Persisting Jython Objects Using DB4O</title>
		<link>http://jimcassidy.ca/2009/01/05/persisting-jython-objects-using-db4o/</link>
		<comments>http://jimcassidy.ca/2009/01/05/persisting-jython-objects-using-db4o/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 17:10:11 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Jython]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=472</guid>
		<description><![CDATA[If you have been following my blog, you will know that I have been playing with the following technologies: DB4O (an open source object database), Jython (Python 2.5, implemented on the JVM), and Django (A python based MVC-style web application framework in the same class as Ruby on Rails). So far, in tutorials I wrote [...]]]></description>
			<content:encoded><![CDATA[<p>If you have been following my blog, you will know that I have been playing with the following technologies: <a title="db4o" href="http://db4o.com" target="_blank">DB4O</a> (an open source object database), <a title="jython" href="http://www.jython.org/Project/" target="_blank">Jython</a> (<a title="python" href="http://python.org" target="_blank">Python</a> 2.5, implemented on the <a title="JVM" href="http://en.wikipedia.org/wiki/Java_Virtual_Machine" target="_blank">JVM</a>), and <a title="Django" href="http://www.djangoproject.com/" target="_blank">Django</a> (A python based <a title="MVC" href="http://en.wikipedia.org/wiki/Model-view-controller" target="_blank">MVC</a>-style web application framework in the same class as <a title="RoR" href="http://rubyonrails.org/" target="_blank">Ruby on Rails</a>).</p>
<p>So far, in <a title="Tutorials" href="http://www.jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/" target="_blank">tutorials</a> I wrote in November of last year, we have seen that Jython allows us to write Python code that uses Java libraries, such as DB40. I altered the standard <a title="Formula One Tutorial" href="http://developer.db4o.com/Resources/view.aspx/Formula_One_Tutorial" target="_blank">DB4O Formula One tutorial</a> using Jython instead of Java, and showed that we can persist and retrieve objects using this technology.</p>
<p>I also mentioned that Django, a Python based web application framework, also runs on Jython.</p>
<p>Which leads us to this entry, where I will start thinking and tinkering to find a way to use DB4O with Django/Jython rather than a relational database.</p>
<p>I am doing this for fun, not for profit. I am not an expert with any of the technologies I am using. I stand in awe of the very talented open source programmers who precede me. I hope to learn as much as I hope to teach and share what I know, or think I know.</p>
<p><strong>Persisting Objects:</strong></p>
<p>In my previous <a title="Formula One Tutorial" href="http://www.jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/" target="_blank">example</a>, I created Plain Old Java Objects (Pojos), and created a <a title="JAR" href="http://java.sun.com/developer/Books/javaprogramming/JAR/basics/" target="_blank">JAR</a>. I used the classes in the JAR to create the objects I persisted using DB4O. I have since decided that this is not desirable. I think this creates a barrier to Python programmers who want to use Django, Python and DB4O togther.</p>
<p>Therefore, I have resolved to persist Python classes by providing the means to automagically inspect these classes and generate the JAR files. I shared <a title="Plug into Django" href="http://www.jimcassidy.ca/2008/11/20/plug-db4o-into-django/" target="_blank">these thoughts</a> over a month ago.</p>
<p>Before writing any code, I just want to reflect on the task at hand. Let&#8217;s gather our assumptions.</p>
<ul style="list-style-type: square;padding-left: 30px">
<li>For every Python data type, there is a corresponding Java data type.</li>
<li>A &#8220;simple&#8221; object is based on a class that contains primitive types only.</li>
<li>A complex object contains other objects, or collections of objects.</li>
<li>We may need to make two passes to persist objects &#8211; first simple objects, and then complex.</li>
<li>A class contains an implementation as well as data, and we want to persist both.</li>
<li>If we change the definition of a class in our project, we need to think about what happens to our previously stored objects.</li>
<li>Our Jython classes must inherit from a single class only, or we will not be able to produce an equivalent Java class.</li>
</ul>
<p>In Django, we define models, and then use a command (python manage.py syncdb) to generate the corresponding database. The database is created automagically, based on the settings in the settings file (settings.py). The model is able to perform basic operations to create, read, update and delete objects (CRUD).</p>
<p>We want our DB4O objects to behave in the same way &#8211; as much as possible, we do not want to violate the expectations of Django programmers who have used Django with relational databases.</p>
<p>In my next entry, I will try to write code that creates a Java class based on a Jython class and saves it in a file. I will keep it simple to start, and the code will evolve as we go.</p>
<p>Our first model will be simple &#8211; a class with a few attributes of various types. The output will be a class files &#8211; we will not worry about creating the JAR just yet.</p>
<p>Once we have produced a simple class file, we will produce class that make two passes and resolve inheritance issues as well as contained objects that inherit from previously defined classes.</p>
<p>Code to come in a day or two.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2009/01/05/persisting-jython-objects-using-db4o/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Django/DB4O Continues</title>
		<link>http://jimcassidy.ca/2008/12/16/djangodb4o-continues/</link>
		<comments>http://jimcassidy.ca/2008/12/16/djangodb4o-continues/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 19:02:05 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Jython]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=468</guid>
		<description><![CDATA[Hi Folks &#8211; I have read that it is considered poor form to make a post on your blog that does nothing more than promise that you will post soon. It is also considered poor form to apologize for not posting. Still, that is why I am writing today. I live in Ottawa, Canada. We [...]]]></description>
			<content:encoded><![CDATA[<p>Hi Folks &#8211; I have read that it is considered poor form to make a post on your blog that does nothing more than promise that you will post soon. It is also considered poor form to apologize for not posting. Still, that is why I am writing today.</p>
<p>I live in Ottawa, Canada. We are currently experiencing a bus strike &#8211; traffic is a nightmare. Some people are reporting that it it is taking up to four times longer to get to work and home. My experience is similar. Combine that with the Christmas season, and my time for blogging and coding is drastically reduced.</p>
<p>Still, I can see that many of you continue to visit. You are eagarly waiting for continued information on my progress with DB4O and Django running in Jython. Last month, I received blog visits from 411 cities in 69 different countries.  I am proud to report that 57% of you are using the Firefox browser &#8211; only 21% are using Internet Explorer. As an aside, I was surprised to see that 6% of you are using Chrome.</p>
<p>So, my friends, if you can forgive me &#8211; I will contnue the work soon. And, I am sorry for not blogging. All the best. I am not writing, but I am developing and tinkering &#8211; the work continues.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/12/16/djangodb4o-continues/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Good Advice For Any Developer</title>
		<link>http://jimcassidy.ca/2008/11/24/good-advice-for-any-developer/</link>
		<comments>http://jimcassidy.ca/2008/11/24/good-advice-for-any-developer/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 16:10:09 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[Ruby/JRuby]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=404</guid>
		<description><![CDATA[I am working to use reflection to generate a POJO from data models defined in Jython &#8211; DB4O can only persist Java ojects. However, as I sort that out and  prepare for my next DB4O/Jython/Django post, let me share this good advice for any developer who builds any sort of web application in any language. [...]]]></description>
			<content:encoded><![CDATA[<p>I am working to use reflection to generate a POJO from data models defined in Jython &#8211; DB4O can only persist Java ojects. However, as I sort that out and  prepare for my next DB4O/Jython/Django post, let me share this good advice for any developer who builds any sort of web application in any language.</p>
<p>These <a title="seven good habits" href="http://www.ibm.com/developerworks/opensource/library/os-php-secure-apps/?S_TACT=105AGY46&amp;S_CMP=PCTAB" target="_blank">seven habits</a> for developing secure applications target PHP developers, but they constitute good advice for you, even if you are working in another language. Take heed.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/11/24/good-advice-for-any-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plug DB4O Into Django</title>
		<link>http://jimcassidy.ca/2008/11/20/plug-db4o-into-django/</link>
		<comments>http://jimcassidy.ca/2008/11/20/plug-db4o-into-django/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 03:03:10 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Jython]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=360</guid>
		<description><![CDATA[As I continue to work with Django, I am coming to appreciate the elegance and simplicity of its design. Less Ruby on Rails style &#8220;magic&#8221; gives us more flexibility. You can store your models any way you like. Last weekend, I played with the possibility of using Couchdb, and I found an example that seemed [...]]]></description>
			<content:encoded><![CDATA[<p>As I continue to work with Django, I am coming to appreciate the elegance and simplicity of its design. Less Ruby on Rails style &#8220;magic&#8221; gives us more flexibility. You can store your models any way you like. Last weekend, I played with the possibility of using <a title="Couchdb" href="http://incubator.apache.org/couchdb/" target="_blank">Couchdb</a>, and I found an<a title="couch on django" href="http://lethain.com/entry/2008/aug/18/an-introduction-to-using-couchdb-with-django/" target="_blank"> example</a> that seemed easy to understand. (Couchdb is another open source project that I may write about later.)</p>
<p>There should be two steps to plugging DB4O into Django. First, we should be able to store settings in the settings.py file to specify where and how we will be storing our objects. The second step should be to create and use our models almost exactly as we would in a regular Django application. It should be that easy.</p>
<p><strong>Editing Settings</strong></p>
<p>It is not my goal to duplicate information you can get elsewhere. To find out how to set up a Django project, and to understand the files that are created for you, see the following <a title="Django Tutorials" href="http://docs.djangoproject.com/en/dev/" target="_blank">tutorials</a>.</p>
<p>In this entry, we are going to look at how to use the <a title="settings" href="http://docs.djangoproject.com/en/dev/topics/settings/#topics-settings" target="_blank">setting file</a> in Django to store settings for DB4O. You can store anything in the settings file you like, and you can store these settings in separate settings files named prodsettings.py, devsettings.py etc. These settings are easy to retrieve from views and models.</p>
<p>We are going to make the following assumptions in our example: we only have one settings file, and it is called settings.py. Also, we are persisting objects to a file, and we are not using an object server.</p>
<p>A cllient/server mode is available in DB4O, and we will have to provide for it later. (DB4O client/server mode is covered in Chapter 8 of the book, <strong>The Definitive Guide to db4o</strong>. The chapter about client/server mode is <a title="chapter eight" href="http://www.springerlink.com/content/h204p31282862143/" target="_blank">available for free</a>, if you are curious.)</p>
<p>To create settings, find your settings file, called settings.py, and add values. To see what entries look like, see below:</p>
<pre>	# Django settings for mysite project.

	DEBUG = True
	TEMPLATE_DEBUG = DEBUG

	ADMINS = (
	    # ('Your Name', 'your_email@domain.com'),
	)

	MANAGERS = ADMINS

	DATABASE_ENGINE = ''
	DATABASE_NAME = '/path/to/file.db4o'
	DATABASE_USER = ''     # Not used with db4o in file mode.
	DATABASE_PASSWORD = '' # Not used with db4o in file mode.
	DATABASE_HOST = ''     # Not used with db4o in file mode.

	# Local time zone for this installation. Choices can be found here:
	# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
	# although not all choices may be available on all operating systems.
	# If running in a Windows environment this must be set to the same as your
	# system time zone.
	TIME_ZONE = 'America/Chicago'

	# Language code for this installation. All choices can be found here:
	# http://www.i18nguy.com/unicode/language-identifiers.html
	LANGUAGE_CODE = 'en-us'

        .  .  .  .  .  .  .</pre>
<p>For our purposes, DATABASE_NAME will be the name of the file used by DB4O to persist objects. If you have read the DB4O documentation, you will know that you also have the option of running a server, in which case we could also use the DATABASE_HOST and DATABASE_PORT settings.</p>
<p>We also have the option of creating any settings we want to help us manage our application&#8217;s behavior. Django&#8217;s convention is to use capital letters for settings and to separate words with underscores.</p>
<p>At this point, we have enough information to find and connect to our DB4O file and we have stored it in the settings file. These settings will be used by our models to create objects and manage them. Our models will be written in such a way as to hide DB4O specific details from programmers. In my next installment, I will think about how to create a model for us to use with DB4O running in Django/Jython.</p>
<p>My thinking is that I will create my models in Jython, but I will create a command line script to create plain old Java objects (POJOs) and place them in a jar for us. I am lazy, and I always use Eclipse to create my jars for me. I expect to face some challenges at the command line, but I am up for it.</p>
<p>Therefore, the steps will be:</p>
<ol>
<li>Create models in a file called models.py.</li>
<li> Run a command line command to generate a jar that contains classes for the the Java objects we want to persist. This jar will need to be in Jython&#8217;s classpath when the application runs. Corresponds to python manage.py syncdb for regular Django models.</li>
<li> Build views, and use Django just as you would to create any other Django application.</li>
</ol>
<p>The only difference will be in the behavior of the models, but our goal will be not to violate too many expectations. Regular Django models subclass <em>django.db.models.Model</em>, but ours will subclass <em>DB4OModel</em>, a class we will create. Our DB4O should resemble Django models as much as possible.</p>
<p>In my next installment, let&#8217;s look at <em>DB4OModel</em>. What does it need to look like?</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/11/20/plug-db4o-into-django/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pythonic DB4O</title>
		<link>http://jimcassidy.ca/2008/11/17/pythonic-db4o/</link>
		<comments>http://jimcassidy.ca/2008/11/17/pythonic-db4o/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 14:37:41 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=341</guid>
		<description><![CDATA[Jim Huginin, the founder the Jython project, is credited with having said: &#8220;The purpose of a programming language is to let software developers express their intentions as simply and directly as possible.&#8221; Unfortunately, we computer programmers are cursed by their own understanding of complexity. In fact, non-programmers often think complexity is a fundamental measure of [...]]]></description>
			<content:encoded><![CDATA[<p>Jim Huginin, the founder the Jython project, is credited with having said: &#8220;<em>The purpose of a programming language is to let software developers express their intentions as simply and directly as possible.&#8221;</em></p>
<p>Unfortunately, we computer programmers are cursed by their own understanding of complexity. In fact, non-programmers often think complexity is a fundamental measure of our skill. The truth is that complexity is easier to achieve than simplicity. Simplicity is the Holy Grail of any framework or programming language.</p>
<p>I aspire to developing code that clearly and simply expresses my intentions. Simplicity is in the eye of the beholder, of course. Often, one paints oneself into a corner and then simplicity goes out the window. However, we am starting from scratch here, so let&#8217;s be hopeful.</p>
<p>My site stats indicate that few people have paid attention to the articles I posted by <a title="Nene" href="http://blog.dhananjaynene.com/2008/09/commentary-on-python-from-a-java-programming-perspective/" target="_blank">Dhananjay Nene</a>. As I continue working with DB4O, I hope you get a chance to read these articles, especially <a title="Nene2" href="http://blog.dhananjaynene.com/2008/09/python-from-java-how-duck-typing-influences-class-design-and-design-principles/" target="_blank">the code examples</a>.</p>
<p><strong>The Project</strong></p>
<p>For anyone who is jumping in late, the latest version of <a title="Jython" href="http://www.jython.org/Project/" target="_blank">Jython</a>, a version of <a title="python" href="http://python.org" target="_blank">Python </a>that has been implement on the JVM, supports <a title="django" href="http://www.djangoproject.com/" target="_blank">Django</a>, a framework similar to Ruby on Rails. I want to demonstrate how to build a website, using Django to manage controllers and views, but I want to manage my models using DB4O.</p>
<p>Since I started writing about <a title="db4o" href="http://db4o.com" target="_blank">DB4O</a>, my site has been getting visitors from about 50 different countries. But, there are few visitors from my own country, Canada. I find that interesting.</p>
<p><strong>This Week</strong></p>
<p>Django lets you replace parts of the framework very easily. In fact, I am quickly coming to appreciate this flexibility when I compare Django to other frameworks. Django does not force you to use its templating system, nor does it for you to use its data models. This is the flexibility we want to exploit to get DB4O working happily with Dajngo running on Jython.</p>
<p>This week, I want to concentrate on hiding DB40 from programmers who want to use DB4O to persist objects while using Django/Jython. The objects should save themselves, and the details should be administered from a central file. This is where I hope to get the most help from Jython as opposed to Java.</p>
<p><strong>A Small Task</strong></p>
<p>While I do this work, if anybody is interested in helping out, here is a task. I have managed to install <a title="NGINX" href="http://wiki.codemongers.com/Main" target="_blank">Nginx</a> on a server, and I have Django under Nginx running using FastCGI and regular Python, not Jython. To do this, I had to install <a title="flup" href="http://trac.saddi.com/flup" target="_blank">Flup.</a> My question is: can we install Flup under Jython? I would be happy to chat with anybody who either knows the answer, or is willing to investigate.</p>
<p>Otherwise, we will have to find another way &#8211; we could run Django in a servlet, and we could perhaps use <a title="Jetty" href="http://www.mortbay.org/jetty/" target="_blank">Jetty</a>. Any other options?</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/11/17/pythonic-db4o/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jython: Formula One Tutorial: Part Three</title>
		<link>http://jimcassidy.ca/2008/11/05/jython-formula-one-tutorial-part-three/</link>
		<comments>http://jimcassidy.ca/2008/11/05/jython-formula-one-tutorial-part-three/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 23:59:58 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[Jython]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=296</guid>
		<description><![CDATA[In our first installment, we configured Eclipse to use Jython and DB4O, and we wrote some simple code to create Pilot objects and store them. In our second installment, we used Jython to retrieve the objects we save in the first part of the tutorial. Now, we need to update and delete these objects. Then [...]]]></description>
			<content:encoded><![CDATA[<p>In our <a title="First Installment" href="http://www.jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/" target="_blank">first installment</a>, we configured Eclipse to use Jython and DB4O, and we wrote some simple code to create Pilot objects and store them.</p>
<p>In our <a title="second installment" href="http://www.jimcassidy.ca/2008/10/31/jython-formula-one-tutorial-part-two/" target="_blank">second installment</a>, we used Jython to retrieve the objects we save in the first part of the tutorial. Now, we need to update and delete these objects.</p>
<p>Then we will have learned how to perform the four basic functions of persistent storage: create, retrieve, update and delete &#8211; often referred to as <a title="CRUD" href="http://en.wikipedia.org/wiki/CRUD_(acronym)" target="_blank">CRUD</a></p>
<p>This tutorial is based on the <a title="formula one tutorial" href="http://developer.db4o.com/Resources/view.aspx/Formula_One_Tutorial" target="_blank">Formula One DB4O tutorial</a>.</p>
<p><strong>Updating objects</strong></p>
<p>Updating objects is just as easy as storing them. In fact, you use the same set() method to update your objects: just call set() again after modifying any object.</p>
<p><em>Java Code:</em></p>
<pre>    // updatePilot
    ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
    Pilot found=(Pilot)result.next();
    found.addPoints(11);
    db.set(found);
    System.out.println("Added 11 points for "+found);
    retrieveAllPilots(db);

<strong>   OUTPUT:</strong>
    Added 11 points for Michael Schumacher/111
    2
    Michael Schumacher/111
    Rubens Barrichello/99</pre>
<p><em>Jython Code:</em></p>
<pre>    result=db.get(Pilot("Michael Schumacher",0))
    found=result.next()
    found.addPoints(11)
    db.set(found)
    print "Added 11 points for "+found
    retrieveAllPilots(db)</pre>
<p>Notice that we query for the object first. This is an important point. When you call set() to modify a stored object, if the object is not &#8216;known&#8217; (having been previously stored or retrieved during the current session), db4o will insert a new object. db4o does this because it does not automatically match up objects to be stored, with objects previously stored. It assumes you are inserting a second object which happens to have the same field values.</p>
<p>To make sure you&#8217;ve updated the pilot, please return to any of the retrieval examples above and run them again.</p>
<p><strong>Deleting objects</strong></p>
<p>Objects are removed from the database using the delete() method.</p>
<p><em>Java Code:</em></p>
<pre>   // deleteFirstPilotByName

   ObjectSet result=db.get(new Pilot("Michael Schumacher",0));
   Pilot found=(Pilot)result.next();
   db.delete(found);
   System.out.println("Deleted "+found);
   retrieveAllPilots(db);</pre>
<p><em>Jython Code:</em></p>
<pre>   result=db.get(Pilot("Michael Schumacher",0))
   found=result.next()
   db.delete(found)
   print "Deleted ", found
   proto=Pilot(None,0)
   result=db.get(proto)
   listResult(result)</pre>
<p>Let&#8217;s delete the other one, too.</p>
<pre>    result=db.get(Pilot("Rubens Barrichello",0))
    found=result.next()
    db.delete(found)
    print "Deleted "+found
    proto=Pilot(None,0)
    result=db.get(proto)
    listResult(result)</pre>
<p>Please check the deletion with the retrieval examples above.</p>
<p>As with updating objects, the object to be deleted has to be &#8216;known&#8217; to db4o. It is not sufficient to provide a prototype object with the same field values.</p>
<p><strong>Next Installment:</strong></p>
<p>In our next installment, we will provide the full source code for this tutorial. We will also reflect on the differences between the Jython code and the Java code.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/11/05/jython-formula-one-tutorial-part-three/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jython News!</title>
		<link>http://jimcassidy.ca/2008/11/04/jython-news/</link>
		<comments>http://jimcassidy.ca/2008/11/04/jython-news/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 14:43:11 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[Jython]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=300</guid>
		<description><![CDATA[Good news on the Jython front. For everyone who is following the DB4O with Jython series, the next version of Jython, Jython 2.5 Beta0, has been released. You may miss this because it is not posted on the Jython main page at the time of this writing. Also, the Jython road map has been updated. [...]]]></description>
			<content:encoded><![CDATA[<p>Good news on the <a title="Jython" href="http://www.jython.org/Project/" target="_blank">Jython </a>front. For everyone who is following the <a title="Jython with DB4O" href="http://www.jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/" target="_blank">DB4O with Jython series</a>, the next version of Jython, <a title="Next Jython release" href="http://fwierzbicki.blogspot.com/2008/10/jython-25-beta0-released.html" target="_blank">Jython 2.5 Beta0</a>, has been released. You may miss this because it is not posted on the Jython main page at the time of this writing.</p>
<p>Also, the <a title="Jython road map" href="http://fwierzbicki.blogspot.com/2008/10/updated-jython-roadmap.html" target="_blank">Jython road map</a> has been updated. It looks like Jython 2.5 Final will be out in January. This is great news. Kudos to the Jython team!</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/11/04/jython-news/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jython: Formula One Tutorial: Part Two</title>
		<link>http://jimcassidy.ca/2008/10/31/jython-formula-one-tutorial-part-two/</link>
		<comments>http://jimcassidy.ca/2008/10/31/jython-formula-one-tutorial-part-two/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 13:19:33 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=257</guid>
		<description><![CDATA[In Our last installment, we configured Eclipse to use Jython and DB4O, and we wrote some simple code to create Pilot objects and store them. In this installment, we are going to retrieve those objects. This tutorial is based on the Formula One DB4O tutorial. Retrieving objects db4o supplies three different quering systems, Query by [...]]]></description>
			<content:encoded><![CDATA[<p>In Our last installment, we configured Eclipse to use Jython and DB4O, and we wrote some simple code to create Pilot objects and store them. In this installment, we are going to retrieve those objects.</p>
<p>This tutorial is based on the <a href="http://developer.db4o.com/Resources/view.aspx/Formula_One_Tutorial">Formula One DB4O tutorial</a>.</p>
<p><strong>Retrieving objects</strong></p>
<p>db4o supplies three different quering systems, Query by Example (QBE), Native Queries (NQ) and the SODA Query API (SODA). In this first example we will introduce QBE. Once you are familiar with storing objects, we encourage you to use Native Queries, the main db4o querying interface. When using Jython, this may present some challenges &#8211; more on that later.</p>
<p>When using Query-By-Example, you create a prototypical object for db4o to use as an example of what you wish to retrieve. db4o will retrieve all objects of the given type that contain the same (non- default) field values as the example. The results will be returned as an ObjectSet instance. We will use a convenience method #listResult() to display the contents of our result ObjectSet :</p>
<p><em>Java Code:</em></p>
<pre>   public static void listResult (ObjectSet result){
      System.out.println(result.size());
      while(result.hasNext()) {
          System.out.println(result.next());
      }
   }</pre>
<p><em>Jython Equivilant:</em></p>
<pre>def listResult (result):
      print result.size()
      while result.hasNext():
          print result.next()</pre>
<p>To retrieve all pilots from our database, we provide an &#8216;empty&#8217; prototype:</p>
<p><em>Java Code:</em></p>
<pre>   // retrieveAllPilotQBE 

   Pilot proto=new Pilot(null,0);
   ObjectSet result=db.get(proto);
   listResult(result);</pre>
<p><em>Jython Equivilant:</em></p>
<pre>    proto=Pilot(None,0)
    result=db.get(proto)
    listResult(result)</pre>
<p>The output is:</p>
<pre>       2
       Michael Schumacher/100
       Rubens Barrichello/99</pre>
<p>Note that we specify 0 points, but our results were not constrained to only those Pilots with 0 points; 0 is the default value for int fields. Also note that where we employ <em>null</em> in Java, we use <em>None</em> in Jython.</p>
<p>db4o also supplies a shortcut to retrieve all instances of a class:</p>
<p><em>Java Code:</em></p>
<pre>   ObjectSet result=db.get(Pilot.class);
   listResult(result);</pre>
<p><em>Jython Equivilant:</em></p>
<pre>   result=db.get(Pilot)
   listResult(result)</pre>
<p><strong>Jython vs Java:</strong></p>
<p>You will notice that the Jython is nicer to read and write. (I think so, at any rate.) Functions return objects that are typed, but we do not have to declare the type in the code. Some people make a fuss about type safety in Java, C# and other languages. My own opinion is that while type declarations do help the compiler, most of the time they do not help the programmer. Type declarations are more work than I want to do most of the time &#8211; developer productivity is what I want.</p>
<p>(<strong>Note:</strong> Because my clients these days tend to be departments of the Canadian government, I am unlikely to have a chance to use Python or Jython in the near future for anything but my own interest. However, one must always be prepared to offer alternatives, and one must always be willing to advocate. )</p>
<p>In our next installment, we will look at updating and deleting DB4O objects. After that, perhaps we can start to think about the Active Record pattern.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/10/31/jython-formula-one-tutorial-part-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jython Goodness With DB4O</title>
		<link>http://jimcassidy.ca/2008/10/30/jython-goodness-with-db4o/</link>
		<comments>http://jimcassidy.ca/2008/10/30/jython-goodness-with-db4o/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 11:34:27 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>
		<category><![CDATA[Jython]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=247</guid>
		<description><![CDATA[I have watched Carl Rosenberger&#8217;s talk at ICOODB 2008 in Berlin. You can find it on German Viscuso&#8217;s blog. You will notice that he not only talks about LINQ, and LINQ for Java, but he also talks about the possible use of annotations for transactional demarkation. Separation of Concerns Using Decorators I was thinking that [...]]]></description>
			<content:encoded><![CDATA[<p>I have watched Carl Rosenberger&#8217;s talk at ICOODB 2008 in Berlin. You can find it on <a title="Carl's talk" href="http://planetgerman.blogspot.com/2008/05/just-objects-no-limits.html" target="_blank">German Viscuso&#8217;s blog</a>. You will notice that he not only talks about LINQ, and LINQ for Java, but he also talks about the possible use of annotations for transactional demarkation.</p>
<p><strong>Separation of Concerns Using Decorators</strong></p>
<p>I was thinking that I could have some fun with DB4O and Jython. If I create a generic object factory to instantiate the Java classes I want to use with DB4O, it would be possible for me to wrap the Java classes in a Python class. This would allow me to decorate method calls to indicate that a method is transactional &#8211; basically, by using a Python adapter on a Java class, I could use decorators to separate any one of a number of concerns.</p>
<p>I was thinking that when the object factory produces an object, it would use the generic reflector to populate a dictionary of methods or properties that can be called &#8211; basically, the factory would examine the Java object to identify its attributes. For the wrapper/mapper to work, I may have to impose some conventions &#8211; the basic idea could be useful and cool.</p>
<p>Remember, the far off goal is something that looks and works like Active Record, but for DB40 using Jython. The programmer would need to know very little about DB4O, but each object would know how to save, update and delete itself, and there would a simply way to dynamically query the repository.</p>
<p>Basically, I think Jython may provide the means to play with syntax, and to toy with various nifty ideas. I will post my next installment in the Formula One tutorial series soon.</p>
<p>Forgive me for blithering &#8211; I just want to use my blog entries to record my thoughts where I can.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/10/30/jython-goodness-with-db4o/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jython: Formula One Tutorial: Part One</title>
		<link>http://jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/</link>
		<comments>http://jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 21:30:32 +0000</pubDate>
		<dc:creator>Jim Cassidy</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Python/Jython]]></category>
		<category><![CDATA[DB4O]]></category>

		<guid isPermaLink="false">http://www.jimcassidy.ca/?p=191</guid>
		<description><![CDATA[Much has been written about NOSQL databases, but DB4O often goes umentioned. Since people also have a keen interest in Python, I thought I would look at ways to use use Python and DB4O together. This tutorial mirrors the DB4O tutorial many of us cut our teeth on: the DB4O Formula One Tutorial. Assumptions This [...]]]></description>
			<content:encoded><![CDATA[<p><!--Ads1-->Much has been written about NOSQL databases, but DB4O often goes umentioned. Since people also have a keen interest in Python, I thought I would look at ways to use use Python and DB4O together. This tutorial mirrors the DB4O tutorial many of us cut our teeth on: the <a title="The Formula One Tutorial" href="http://developer.db4o.com/Documentation/FormulaOneTutorial.aspx" target="_blank"><strong>DB4O Formula One Tutorial</strong></a>.</p>
<p><strong>Assumptions</strong></p>
<p>This tutorial makes a few assumptions. Let&#8217;s get them out there to help some people avoid frustration. I assume the following:</p>
<ul>
<li> You have <a title="Java" href="http://java.com/en/download/linux_manual.jsp?locale=en&amp;host=java.com:80" target="_blank">Java</a> installed and working &#8211; Java 5 or higher.</li>
<li> You have some knowledge of Java and the workings of the JVM.</li>
<li> You have Eclipse installed on your computer, and you know how to use it.</li>
<li> You have installed and configured <a title="pydev" href="http://pydev.sourceforge.net/" target="_blank">PyDev</a>, an Eclipse plugin.</li>
<li> You have installed <a title="jython" href="http://www.jython.org/Project/" target="_blank">Jython</a> on your computer. (I am using Jython 2.5a3 because it can run Django.)</li>
<li>You have some knowledge of <a title="Python" href="http://python.org" target="_blank">Python 2.5</a>.</li>
<li>You have installed <a title="DB4O" href="http://db4o.com" target="_blank">DB4O</a> on your machine.</li>
</ul>
<p>Everything used in this tutorial is free to download and use, and the code you write will run on a number of operating systems including Windows, Linux and Apple. I use Ubuntu Linux, and my examples refer to my setup not yours.</p>
<p>It would be a good idea to follow the DB4O Java tutorial first, and then repeat your work in Jython. However, feel to just dive in the Jython code, if you wish.</p>
<p><strong>First Steps:</strong></p>
<p>Let&#8217;s get started as simply as possible. We are going to learn how to open, store, retrieve, update and delete instances of a single class that only contains primitive and String members. In our example this will be a Formula One (F1) pilot whose attributes are his name and the F1 points he has already gained this season.</p>
<p>We are going to create a Java class to use in our example, and then we are going to put it in a jar. DB4O can persist Java objects, not Python objects. Other than the object itself, all of our other code with be in Jython.</p>
<p>First let&#8217;s set out classpath to refer to the DB4O jar, and let&#8217;s create our Pilot class. Most of you will have a strong understanding of classpaths. However, if you happen to have trouble defining a classpath environment variable, Eclipse provides the means to manage a classpath. A classpath tells Java where to find its libraries. Read the Eclipse documentation to learn how to configure a build path to the appropriate DB4O Jar. (I am using db4o-7.4.58.11547-java5.jar.)</p>
<p>Once you reference the DB4O Jar, you should see something like this:</p>
<p style="text-align: center;"><img src="http://www.jimcassidy.ca/wp-content/uploads/2008/10/jythondb4o.png" alt="Eclipse" /></p>
<p>First we create a native class. As was mentioned above, this is necessary because DB4O does not persist Python classes. Create a Java package such as:</p>
<pre>   package org.electricmousetrap.db4o.jython;
   public class Pilot {
       private String name;
       private int points;

       public Pilot(String name,int points) {
           this.name=name;
           this.points=points;
       }
       public int getPoints() {
           return points;
       }
       public void addPoints(int points) {
           this.points+=points;
       }
       public String getName() {
           return name;
       }
       public String toString() {
           return name+"/"+points;
       }
   }</pre>
<p><strong>Create A JAR Using Eclipse</strong></p>
<p>A JAR is a convenient way to package up class definitions in a single file. It is easy to create a JAR using Eclipse. If you need help, you will find a tutorial <a title="jar tutorial" href="http://www.eclipse-blog.org/eclipse-ide/exportingimporting-jar-files.html" target="_blank">here</a>. If you are desperate, and you are willing to run the risk of having things work the first time, you can always read the eclipse help file. I named my Jar DB4OJython.</p>
<p>Once you have created your Jar, create a new Jython project.</p>
<p><strong>Configure and Create A Jython Project</strong></p>
<p>The Pydev plugin will allow you to create a new Jython project if you have correctly configured Eclipse to run with Jython, but there is a dirty trick you will need to use if you want to use the latest version of Jython, which is currently in beta.</p>
<p><strong>Dirty Trick: </strong>To use Jython, you need to to tell Eclipse which JAR file to use. Pydev seems to expect a jar called jython.jar, but the beta ships with a jar called jython-complete.jar. Make a copy of this jar and rename it jython.jar. Other than that, follow the instructions for installing Jython, and you should have no problem. I would love to provide some other troubleshooting tips &#8211; perhaps later.</p>
<p>Then, using the Window-&gt;Preferences options, configure Pydev to use the new jar as the Jython interpreter. Having configured Jython, create a new Pydev project and specify the Jython 2.1 project type. This will work fine, although it is not accurate.</p>
<p>To keep things simple, create a new file and just call it DB4OExample.py, and then you are almost ready to go. The last thing you need is to put the DB4O jar in the classpath so that you use the DB4O library. You also need a reference to the jar we created earlier that contains the Pilot class. There are two ways to do this:</p>
<ol>
<li>Use the Jython <a title="Registry" href="http://www.jython.org/docs/registry.html" target="_blank">registry</a>.</li>
<li>Append a reference to the jars to the Python system path. See the code below.</li>
</ol>
<pre>import sys
sys.path.append('/path/to/your/db4o-7.4/lib/db4o-7.4.58.11547-java5.jar')
sys.path.append('/path/to/your/jython/Java/DB4OJython.jar')</pre>
<p>My code examples will employ the second approach, but there is good reason to separate configuration concerns from your code. Now we are ready to write Jython code that uses DB4O.</p>
<p><strong>Opening the database</strong></p>
<p>To access a db4o database file or create a new one, call Db4o.openFile() and provide the path to your database file as the parameter, to obtain an ObjectContainer instance. ObjectContainer represents &#8220;The Database&#8221;, and will be your primary interface to db4o. Closing the ObjectContainer with the #close() method will close the database file and release all resources associated with it.</p>
<p>In Java, the code looked like this:</p>
<pre>   // accessDb4o

   ObjectContainer db=Db4o.openFile(Util.DB4OFILENAME);
   try {
          // storeFirstPilot
          Pilot pilot1=new Pilot("Michael Schumacher",100);
          db.set(pilot1);
          System.out.println("Stored "+pilot1);

          Pilot pilot2=new Pilot("Rubens Barrichello",99);
          db.set(pilot2);
          System.out.println("Stored "+pilot2);
   }
  catch
  {
         // Handle exceptions
  }
  finally {
          db.close();
  }

}</pre>
<p>Because Jython is dynamically typed, you can end up typing less code. Here is a full example of how to save objects using DB4O &#8211; it coresponds to the DB4O formula one tutorial.</p>
<pre># Configure Jython environment
import sys
sys.path.append('/path/to/your/db4o/lib/db4o-7.4.58.11547-java5.jar')
sys.path.append('/path/to/your/jars/DB4OJython.jar')

#import java dependencies
from com.db4o import Db4o
from org.electricmousetrap.db4o.jython import Pilot

try:
    try:
        db=Db4o.openFile('/home/path/to/your/pilots.db4O')
        print "The following were saved"
        pilot1=Pilot("Michael Schumacher",1006)
        db.store(pilot1)
        print pilot1
        pilot2= Pilot("Rubens Barrichello",996)
        db.store(pilot2)
        print pilot2
    except:
        print sys.exc_info()
finally:
    db.commit()</pre>
<p>In my next installment, I will show you how to retrieve these objects.</p>
<p>Please note that I am using Jython without creating classes or objects of my own for now. That is the beauty of Python/Jython &#8211; it is great for prototyping and figuring things out. You can write a more robust Jython application later, or you can write the eventual code in pure Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimcassidy.ca/2008/10/28/jython-formula-one-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

