Smarter Software

Using hotspins in your IPhone Application

Posted: June 20th, 2010 | Author: César Naranjo | Filed under: Uncategorized | No Comments »

The procedure is as follows:

  • Main Thread
    • First thing is to start a new Thread, the Background Loader Thread, as soon as the application starts.
    • Load the first screen and anything else you 100% need at startup to have a functional first screen.
    • Whenever something is referenced that may or may not have been loaded check the pointer/reference using a hotspin. If its loaded, great use it as is, otherwise you wait until the pointer is set.
  • Background Loader Thread
    • Load everything you need here: Databases, images, ViewControllers, whatever…

Starting a New Background Loader Thread, start Interface


[NSThread detachNewThreadSelector: @selector(backgroundLoad) toTarget:self withObject: nil];
[window makeKeyAndVisible];

Implementing the Background Loader Thread


- (void) backgroundLoad {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
self.takePicViewController = [[TakePhotoViewController alloc] init];
self.mainInterface = [[MainInterface alloc] initWithNibName: @"MainInterface" bundle:nil];
[pool release];
}

Hotspin on pointers/references


/* Will use the TakePicture View Controller */
- (void) takepicture {
if( takePicViewController == nil) {
//hot spin until value is set
while( takePicViewController == nil ) { [NSThread sleepForTimeInterval: 0.01]; }
}
[self.navigationController presentModalViewController: takePicViewController animated: YES];
}

Other References


A Mare Subiu: NYC Capoeira Women’s Event

Posted: February 2nd, 2010 | Author: César Naranjo | Filed under: Capoeira | Comments Off

OrangeMico is proud to sponsor A Mare Subiu: NYC Capoeira Women’s Event. The event is organized by Capoeira Brasil New York- Instructors Joy, Tuzinho and Esquilo, the event’s workshops will seeks to raise the level, technique, and confidence of female capoeiristas, bringing top female capoeiristas from around the world to the event.

Learn more about the event on the event blog: nycapoeirawomensevent.blogspot.com


Removing Accents and Tildes From Text String In Python

Posted: November 10th, 2009 | Author: César Naranjo | Filed under: Uncategorized | No Comments »


# -*- coding: utf-8 -*-
import unicodedata
wordwithaccents = "Asociación Española"
wordnoaccents = unicodedata.normalize('NFKD', wordwithaccents).encode('ASCII', 'ignore')


CSV Parsing

Posted: October 26th, 2009 | Author: César Naranjo | Filed under: Uncategorized | No Comments »

Removing a Project from Subversion

Posted: September 6th, 2009 | Author: César Naranjo | Filed under: Uncategorized | 1 Comment »

Whenever i have had to remove (uncheckout) a project from subversion control I use the following command. I assume there is a better way or more correct way. I would suggest you first run the find command within the back-ticks to make sure it is listing the correct files.


rm -Rf `find . | grep "\.svn$"`


Word Xml in Django

Posted: September 3rd, 2009 | Author: César Naranjo | Filed under: Uncategorized | No Comments »

If you would like your django web application to generate simple word documents this article will show you how using Microsoft’s Word XML format and django’s template system to do so.

Since Word 2003, Microsoft Word has had the ability to save documents in Word XML format.

The first step is to start an example project, an example application, and an example data model to use within that application. We will call the project wordxml , the application wxml and the data model People. We’ll skip quickly through the first couple steps because I’ll assume most of you already know the basics of django and that you already know python.

Let’s create the project and application:


bash$ django-admin.py startproject wordxml
bash$ cd wordxml
bash$ python manage.py startapp wxml

We’ll setup the database, for this example we’ll create a sqlite database. We will also install our application as well as tell django where to find the template will use. Open wordxml/settings.py and change the applicable database properties, the installed apps property, and template property as follows:


...
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'test.sqlite3'
...
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'wordxml.wxml',
)
...
TEMPLATE_DIRS = (
      "/Users/username/tmp/wordxml/templates",
)

Now open wordxml/wxml/models.py and add the following code to create the data model.


from django.db import models

class Person(models.Model):
  fname = models.CharField(max_length=255)
  lname = models.CharField(max_length=255)
  age = models.IntegerField(max_length=255)

Synchronize the database with your new model. We will say no to creating a superuser since in this example it won’t be necessary.


bash$ python manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table wxml_person

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
Installing index for auth.Permission model
Installing index for auth.Message model

We’ll create a few objects (Alex, Cluadia, and Mike) in our database before we’ll skip over to Word. We’ll do this using the django shell.


bash$ python manage.py shell
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from wordxml.wxml.models import *
>>> person_a = Person(fname="Alex", lname="Davis", age=18)
>>> person_a.save()
>>> person_b = Person(fname="Claudia", lname="Smith", age=21)
>>> person_b.save()
>>> person_c = Person(fname="Mike", lname="Ada", age=35)
>>> person_c.save()
>>> quit()

Now let’s jump to Word to create our document, actually in this case OpenOffice. We will create a document that just list the names down the sheet and save it in the Word XML format.


django_word_document


djang_word_document_save

* The xml file I will show the code for later contains less formatting; no different size fonts and colors.

Now we’ll create the view to read the xml file we just created as a template. Use the following as your wxml/views.py, notable lines are commented.


# used to generate the return object
from django.http import HttpResponse

# needed for templates
from django.template import Context, loader

# the database object we created earlier, make it visable
from wxml.models import Person

def example_view(request):
#get all person
people = Person.objects.all()

# place it into the context and pass it the template loader
c = Context( {‘people’: people} )
t = loader.get_template(“exmaple.xml”)

# render the template
render_xml = t.render(c)

#create and return the response object
response = HttpResponse(render_xml, mimetype=’application/ms-word’)
response['Content-Disposition'] = ‘attachment; filename=exmaple.xml’
return response

As you may have noticed we are passing people and not p in the context as it is not referenced in our xml document nothing will happen (of course the same would happen if it were using a html template). We will instead go straight to the xml document and add a for to loop over our people object and list each person one by one. If we only wanted one name to be listed we could have just created that singular object and passed it in the context, for example we could chosen the first person returned by django p = people[0] and placed it in the context as follows c = Context( {'p': p} ).

Both OpenOffice and Word will create the Word XML file but with no more or less no line-breaks, its much easier to dig into the xml after some preprocessing. We will pass it through xmllint to make it file more human friendly. All OS X user will have xmllint installed by default and so will most Linux user. The --format flag in xmllint does the trick, you must output it a temporary file and then change its file name to the original file.


bash$ xmllint --format exmaple.xml > exmaple.xml.tmp
bash$ mv exmaple.xml.tmp exmaple.xml

The file is no nicely formatted. Open it in the your favorite text editor. Once opened search for double brackets {{, these start the section where the variable is outputted in the template.


...
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
</w:pPr>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
</w:pPr>
<w:r>
<w:t>{{ p.fname }} {{ p.lname }} {{ p.age }}</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="next-page"/>
<w:pgSz w:w="12241.5302" w:h="15841.9803" w:orient="portrait"/>
<w:pgMar w:top="1133.9978" w:bottom="1133.9978" w:left="1133.9978" w:gutter="0" w:right="1133.9978"/>
<w:pgBorders w:offset-from="text"/>
</w:sectPr>
...

You have to play around with Word or OpenOffice to know how this xml format works, I am not sure to find good documentation. I’ll tell you right now the p element denotes a paragraph, we’ll create a for/endfor loop over that p xml element that is encapsulating our person object output.


...
</w:p>
{% for p in people %}
<w:p>
<w:pPr>
<w:pStyle w:val="Standard"/>
</w:pPr>
<w:r>
<w:t>{{ p.fname }} {{ p.lname }} {{ p.age }}</w:t>
</w:r>
</w:p>
{% endfor %}
<w:sectPr>
...

The next step is to setup django to serve the view we just created. Add the view to urlpatterns in urls.py.


from django.conf.urls.defaults import *

urlpatterns = patterns(”,
(r’^exmaple.xml’, ‘wordxml.wxml.views.example_view’),
)

Run the development server and then direct your browser to http://localhost:8000/exmaple.xml, your browser will request to the save the file to disk, do so and open it the  using Word or in my case OpenOffice.

Notes

Please keep in mind that the Word XML format is not same as the Office Open XML format, with a few changes, this simple technique I explain here will be applicable to that format. The Office Open XML format is essentiall a zipped file containing several xml files and other assets, using the technique above you can create the necessary xml files and assets and place then into a zip file and return that as your document; this tutorial explains only part of the process. The Word XML is currently retired but still useful and this technique can be used for other similar formats (eg: svg, Excel XML, etcetera).

Limitations

  • Word 2004 for Mac does not support the format
  • OpenOffice will render documents differently than Word, for example OpenOffice does not handle headers and footers.