Django or Rails?


Today I got into a conversation on Twitter about Rails versus Django.  I know that I have blogged about this before but I thought that I should maybe clarify my reasons for preferring one over the other.

Before I start, I must confess that I did not use Django that much and it was also in my beginning stages of web development so bear that in mind as you read this.

I came from Python where I first learned it for geoprocessing to use with ArcGIS.  Now using Python (which is installed with ArcGIS) for geoprocessing inside of ArcGIS is easier than natively.  For example, there is a UI for forms to define what fields or data types to use.  Python for geoprocessing is very well documented and is very easy to use.

I soon found out that I was very limited are far as functionality goes using Python “inside” of ArcGIS.  I looked at options for creating desktop GUI‘s and found that wxPython was the easiest for me to use to accomplish what I wanted to do.  I have several posts regarding this stuff in my archives so I will not repeat myself.

On to web development…

At my old “job” we had an app written in Ruby on Rails and that was my starting transition to web applications.  I started by creating Python interfaces for desktop applications to interact with our RoR web application and then started to assist on the web application itself.  This piqued my interest.  I had taught myself Python so why not learn Django?

I other posts I have commented that the best place to find help learning a new programming language is to go to user groups.  In Arizona there was not a Python/Django user group.  I found via Twitter a couple of local guys that were Django developers and met with them at coffee shops to try to learn from them and I greatly appreciated their assistance.

It was the learning at home, alone, that was not successful.  I found that there was not a lot of training “materials” online for Django.  Do not get me wrong, there is a lot of stuff on the web to help with learning Django but a lot of it is just as confusing as Django is.  I even went as far as trying to start-up/re-incarnate a local Python group which was not very successful.

Enter Ruby user group…

I found out that there was a local Ruby group and a local Rails group.  I went to my first Ruby::AZ meeting.  I was at a transitional stage where the separation between the Ruby group and the Rails group was diminishing and morphed into what it is today; Ruby and all things Ruby.

I found the support group I was looking for.  There were so many Rubyists that were happy to point me in the right direction and to help me when I needed it.  The resources for learning RoR are phenomenal.  My still all time favorite and a major reference guide for me is Railscasts.com.

I then met @m3talsmith with RebelHold and he took me on as an apprentice.  The rest is history.  I now have my own company Rebel Outpost which is a member of the RebelHold family.

Django or Rails?

 In all fairness, I prefer Rails as I was able to learn it.  That does not mean that Python or Django are bad languages at all.  I also cannot say that Rails is better than Django but I do love Rails.

Ruby is so clean and elegant to read and write but lacks the whitespace-ness (is that a word?) that Python has.

So I hope that clarifies my love for Rails over Django………

Python or Ruby….


I see that a lot of my hits come from the question of Python vs. Ruby.  I think that the two are absolutely two different species.

If you are going to do anything in GIS or ArcView then Python is the way to go.  ESRi has embedded Python into its applications and I assume that Python will be there for a long time to come.

Primarily the two languages are very similar.  I think that the choice must be decided on what to use them for.  I feel that there are a lot of aspects of Python that are better than Ruby and vice-versa.

The line between the two becomes more distinct when you look at web applications and the programming community.  Here is where I start to piss people off……..

I (and again it is my blog and my opinion) feel that Ruby on Rails is EASIER to learn and use than Django.  For those that do not know, Django is written in Python such as Rails is written in Ruby.  You cannot compare Python and Django any more than you can compare monkeys and elephants.  They are two absolute different things.  You can however compare Rails and Django.

Now this is what I have discovered as far as the community goes between Django and Rails.  I have already posted that I (again my blog) feel that Python/Django users are a complete different species than Ruby/Rails users.  To me the Python/Django users are those that prefer to work alone and dislike the mob mentality as opposed to the Ruby/Rails users that are a very close knit family.  In my attempts to learn both Django and Rails, I have found a lot more support and people willing to help learn which leads me to yet another tirade of mine….

As there are so many places to learn and get help learning different languages (I have often blogged that if you have any questions, feel free to contact me) I am totally against ANYBODY charging to teach either of these languages.  Sure everybody deserves to make money but how many of those out there learned from somebody else and are now charging to teach others.  Maybe my attitude is skewed as programming in general is a hobby to me although I do get paid for it sometimes.

Take for an example one such person out there that spends who knows how much time creating Railscasts (http://railscasts.com/) also known on Twitter as @rbates the world famous Ryan Bates.  Now I am sure that somewhere he is making money to do what he does but I do not care if it is FREE for me!  Take for example my routing articles or posts; many people/companies charge for the information that I give away for free just like Ryan Bates.

Now for those of you that I have pissed off……..If you make money charging people to take your programming classes  I do not fault you.  I myself will NEVER pay to learn programming especially Ruby on Rails as there are way too many tutorials out there and too many people willing to help.

Keep open source open source and help others as others have helped you!

Next Steps….


All I can say is that keeping up a blog is a lot more work than I ever thought it would be. With that being said, email me if you would like to add an article to my blog as a guest author.

I have gotten quite a few requests for video tutorials and I am planning on creating some for geoprocessing.  I am thinking very short and sweet “how to’s” for the various basic geoprocessing functions.  I have seen a little interest in Phoenix for Python geoprocessing classes for which I would love to do if there is enough interest.  I am hoping that fellow members of the DesertPy.org group will also be interested in sharing their knowledge/experiences for some more basic Python training.

As usual, if you have any questions, comments, or bitches, feel free to contact me.

BR

Python Geoprocessing


Alright everybody, I get a lot of searches everyday for Python geoprocessing related questions.  I would love to add more content about geoprocessing but not sure what it is that all of you would like to learn.  I was going to do a “Geoprocessing tip of the week” but I felt that it would be pretty much the same as looking at the ESRI help docs.

The simplest way is to email me via the about/contact page and I will attempt to answer what I can.  I am also looking at putting on a Python Geoprocessing class(s) here in Phoenix at the DesertPy.org group meetups.  Would there be any interest in a Python Geoprocessing class if I were to have one?  Maybe if there is enough interest we can do a webinar so that those outside of the Phoenix area could also participate.

As always, feel free to drop me a line…

BR

What programming language do I learn?


If you search the web there are a million posts on a million forums about what is the best language.  These are all OPINIONS and that is that.  So time for my opinion…

I think that the biggest thing in determining what programming language you should learn is deciding what it is that you intend to use it for.  I would never spend the time and effort to learn Chinese if I never have any intention of going to China.  Just remember that programming languages are just that..languages.  So before you decide what language you want to learn, you need to ask yourself what you want to use it for.

I learned Python as I work in GIS and as I have said before, ESRi’s ArcView comes with Python.  If you are in GIS and do geoprocessing, then there is not really and questions as to which language to learn.

For general (non web application) use, I think that there are a lot of things that are better with Python than Ruby but I also feel that there are parts of Ruby formatting that I like better than Python.  Ruby has its gems and Python has its eggs or modules. Truthfully for non web development it is really a matter of personal preference between Ruby and Python.

Now, for web development, I am really torn between the two.  I have tried both Django and Rails.  I have tried many of the other smaller web frameworks as well. There are parts of each that I think are better than the other.  Rails generates more upfront files and html but Django creates a pretty awesome administrative interface.  I still have not decided which one I prefer.  I will mess around with Django for a few days and then jump to Rails for a few days.  I will say that there is a lot more documentation on Rails than there is for Django.  That brings me to my last opinion on the difference.

Remember that this is my blog and these are my opinions.  I also know that the name of my blog is “Python and then some..”  That being said, there is a major difference between Python/Django and Ruby/Rails in the users and the communities.  There is also a different mindset and (in my opinion) personality between the two types.

I feel that in general, Python users are more independent than Ruby users.  Every major (and a lot of minor) cities in America have Ruby or Rails user groups and they are very popular.  There are not as many Python user groups.  I started a Python user group in Phoenix AZ and I am struggling to get people show up.  Comparatively, the Ruby and Rails user groups have excellent turn outs.  You can say what you want but the Ruby/Rails community is a lot closer and tighter than any Python groups.

Again, this is just my opinion but I have noticed a major difference between the two.  For example, Rails is big on TDD (Test Driven Development) where you create a test that you know will fail before you create the process to make it pass. That is what I call proactive troubleshooting, whereas you test for failure prior to creating the process.  In Python/Django I see more of a reactive troubleshooting, whereas you create the process and if it doesn’t work you test to see why not.

Now this does NOT mean that this is always the case.  There are extremes in either group.  I just feel that the Ruby/Rails community is more close knit than the Python/Django community.

So, what language should you learn?  Decide what it is that you want to use if for in the first place and go with that.

And to the Python world, this is not intended to be insulting but rather a call out for more unity.  The Rails community can do it than why the hell can’t the Python community be more connected?

BR

Whats going on…


Just an update to what is up lately.  I have been busy with work releasing our new web-based route optimization application so I have been lacking on new material on the blog.  I have also been messing with:

  • Django
  • RefineryCMS (a Ruby on Rails CMS)
  • RoR
  • a Python interface for our desktop application
  • DesertPy.org

I will add again that I am somewhat torn between RoR and Django.  The RefineryCMS is simply put…AWESOME!  I need to look at Django – CMS to compare just to be fair.

I was hoping to make it to Cartwheels Python courses but again it seems that money is the deterrent.

I also added a cool QR code to the bottom of the right hand section that has my contact info.  I keep seeing these codes for downloading apps to my Android phone and think they are pretty cool.

If you are here for route optimization, I have added my Solid Waste routing paper to the downloads section.

As always, if you have any questions, feel free to contact me and stay tuned for new content.

Python and then some….


Wow, I am just past 6 months of my blog being up.  I have has a lot more hits than I ever expected to.  I know that the blog started out “Everything Python” and then transitioned as I decided to add new topics.

Well, guess what, I think I am going to be adding more AGAIN.

My work has me dabbling in Ruby on Rails and from that I am looking at Django.  As time goes by I will list my likes and dislikes of both of these web frameworks but not in this article.  I will however at this point say that the Ruby and/or Rails community is a lot more “together” than anything that I can see to get going in Phoenix for Python.  That is disappointing to say the least.

I also recently got turned on the jQuery.  One word…AWESOME.  I will put my savings calculator (again a hobby turned to work use) in another post but needless to say is that I needed a savings calculator that would have required fields and with a little adaptation, jQuery was the answer.  More so than this is the fact that the calculator can be used on our RoR site as well as our Joomla based website.

So stay tuned for more categories and more topics and thanks for reading….

Export Shapefile as .csv tutorial


I recently had issues with a simple export to .csv. What I wanted to do is pull in a shapefile and delete a bunch of fields and then export it out to a .csv (this works the same for saving to a text file). It actually turned out easier than I thought as I did not need to delete all the fields but rather I just wrote out the fields that I wanted to keep:


# Import system modules
import sys, string, os, glob, csv
import arcgisscripting
# Create the Geoprocessor object
gp = arcgisscripting.create()

# System arguments
input = sys.argv[1]
output = sys.argv[2]

# Set our new outputnew to be opened output with write privileges.
outputnew=open(output,'wb')

# Set linewriter as csv.writer written out to outputnew
# The delimiter can be set as comma, pipe etc.
linewriter=csv.writer(outputnew, delimiter=',')

# If you want to pass all fields thru then disregard this.  I use this as I only want to pass some of the fields out and these are listed in Python list
good_fields = ['field 1', 'field 2', 'field 3', 'field 4', 'field 5']

# Now we go thru all of the fields and keep just those that are in the list of good fields
flds=gp.ListFields(stops_in)
fld = flds.Next()
header=[]
while fld:
    if fld.Name in good_fields:
        value=fld.Name
        header.append(value)
    fld = flds.Next()
linewriter.writerow(header)

# And we run thru each row populating the kept fields
row=cursor.Next()
row_num = 1
while row:
    line=[]
    if row_is_valid(row_num, row):
        for fld in header:
            value=row.GetValue(fld)
            line.append(value)
        linewriter.writerow(line)
    row=cursor.Next()
    row_num += 1

del cursor
gp.AddMessage("Finished")
output.close()


I originally attempted to make a copy of the original shapefile and then a table view from the shapefile. In the end all I needed to do was simply read the input shapefile and then write it out to the .csv file.

Adding a Python Script to a new Toolbar in ArcView 9.3.1. tutorial


So you finished your Python ArcToolbox tool and now you want to add a shortcut to the toolbar. This tutorial is based on the assumption that you have already added your script to the ArcToolbox tools. If not I will cover that in another tutorial. I will show how to add a button in addition to how to add a menu to ArcView toolbar. Both are convenient ways to launch your Python script tools without having to open ArcToolbox.

First we will create a button to launch your tool.

  • Go to Tools > Customize


    • This will open up to the Toolbars tab. If you do not want to add your own Toolbar, skip this section.
  • Click New for a new toolbar:


  • Name your toolbar
  • Save it to either the current project (the .mxd) or to the Normal.mxt for use in all projects.
  • This will create an empty toolbar. Drag the toolbar to the top of ArcView.


Now that you have a new and empty toolbar we need to create the button for the Python tool.

  • Click on the Commands tab under Customize:


  • Scroll to the bottom of the Categories window:


  • Highlight [UIControls] and at the bottom click New UIControl


  • Select UIButtonControl and hit Create
  • Click in the box for the name if you wish to rename the tool


  • Next, click and drag the new UIButtonControl1 to your toolbar that was just created.


Now that the button is one the toolbar, we need to set it up to launch your Python tool.

  • Right click on the new button and you should see several options here.


  • You can change the button image to whatever you would like or you can just use “Text Only” for the name of the tool.


Now that we have the button on the toolbar and changed the way that it displays, it is time to add the Visual Basic snippet to launch the tool.

  • Click on “View Source”. This should open up the Microsoft Visual Basic editor:


 

 

  • Copy and Paste the following script between the Private Sub UIButtonControl1_Click() and End Sub:

Dim pUID As New UID
pUID = "esriGeoprocessingUI.ArcToolboxExtension"
Dim pGP As IGeoProcessor
Set pGP = New GeoProcessor
Dim pATBExt As IArcToolboxExtension
Set pATBExt = Application.FindExtensionByCLSID(pUID)
Dim pAtb As IArcToolbox
Set pAtb = pATBExt.ArcToolbox
Dim pTool As IGPTool
Set pTool = pAtb.GetToolbyNameString("mypythontoolname")
Dim pCommand As IGPToolCommandHelper
Set pCommand = New GPToolCommandHelper
pCommand.SetTool pTool
pCommand.Invoke Nothing</span>

  • You will need to replace “mypythontoolname” with the name of your Python tool.
  • Click the save button at the top of the VB editor.
  • Click on your button and it should open.

There are a few things that can cause the button not to open your Python tool. The most common that I have seen is that the Visual Basic reference is not selected as available for using the ESRI GeoprocessingUI Object Library. This can be added by going into the Visual Basic editor again. At the top there is an option for Tools and then select references. Make sure that the ESRI GeoprocessingUI Object Library is checked. If not, check it to make the library available.

If you want to add a drop down menu for listing multiple Python tools the steps are mostly the same. After creating a new toolbar, you can create a new menu before you create the tool buttons.

On the Customize > Commands tab in the left hand pane there is an option for [New Menu]. Create the new menu in the same manner as creating the UIControls button. Add the menu to the toolbar and then you can add tool buttons to the drop down menu by clicking and dragging them to the drop down.

Hope that helps!

So Why Use Python?


I guess this is a situation of putting the horse after the cart but I realized the other day that I never really listed why I choose Python as my language of choice. Being in the GIS related field I use ESRI ArcView a lot. I found myself performing some tasks repeatedly or rather a series of steps. Maybe I would use one toolbox tool to change a shapefiles projection and then after that I would add the x and y coordinates to the shapefile. I started reading about creating models using ESRI ModelBuilder. No offense to the ModelBuilder but I never really “got it” and therefor never really used it.

What I did find out though is that after I made a Model I could export out the resulting Python code. Hmm, this seemed an interesting way to go. I decided to learn Python programming and more specifically I learned to use it for geoprocessing. I soon learned that ALL of the geoprocessing tools and all of the ArcToolBox tools had an example of the Python code for them. I won’t say that it was as easy and cut-and-paste but using Python for geoprocessing just made sense to me.

I was asked the other day how to choose which language to learn if somebody is interested in learning a programming language. I think that most programmers will use the language that they first learn provided that it serves their needs. That isn’t to say that all programmers will just learn one language or use just one language. I recently started to look at creating web applications and I saw that there is Django which is written in Python to do such a thing. I really struggled with getting Django installed on my Win7 machine!! I know a lot of people that love Ruby on Rails so I started to look at that. I learned enough Ruby to get by and started doing quite a few tutorials creating web applications in Rails.

That sense of failure with the installation of Django just kept bugging me. I recently refurbished a pc from my daughter that didn’t have a hard drive. After buying a new hard drive I decided to go Linux. I installed the new Ubuntu 10.10 and lo and behold, installing Django was a piece of cake. I am now working on the Django tutorials. I will probably add a new section about Django as I go along.

The purpose of all of this is that I now am familiar with Ruby on Rails and that is making it easier for me to learn Django and to improve on my Python. I think I will stick with Django as there is a GeoDjango which is right up my alley. You might need to try out a language or two to decide which the right one for you is. Do some research on the different languages and you will see that there are some that are very similar and others that are very specific. Find the one that suites your needs and have fun programming.