Category Python

Flask Web Apps in OS X Server

So, you want to run your Python Flask web applications the native OS X Apache web server? Well that’s awesome, because so did I! This probably won’t work on 10.7.x and earlier without using Apache from brew/ports or using MAMP (which I used); I’m running OS X Yosemite 10.10.5 with the latest server application for this version of OS X.

vonavi:~ jamie$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.10.5
BuildVersion: 14F27

vonavi:~ jamie$ sudo serveradmin -v
Version 15S2259

Step 1: Choose where your app will live.
I personally chose “/Users/jamie/Sites/home” simply for ease of access but I’m not using the userdir functionality.

Here is how my app is laid out:
home\
|-- _venv\
|-- app\
|-- static\
|-- templates\
|-- __init__.py
|-- forms.py
...

Read More

SQLAlchemy: Connecting to pre-existing databases & table joins!

SqlAlchemy is an object-relational mapper (ORM), which means that it takes SQL constructs and makes them Python objects which can be manipulated natively. Most of the time the same code can be recycled to use multiple database back-ends (unless you use back-end specific features).

I will be using Microsoft SQL Server for this particular example and show you how to use a technique called database reflection to get table information from a database schema when the database and tables already exist. For this you will need SQLAlchemy and pymssql installed.

#!C:\python27\python.exe
# -*- coding: utf-8 -*-

__author__ = "Jamie Ivanov"

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, MetaData, Table

def main():
engine = create_engine('mssql+pymssql://server/Databa...

Read More

XML fun with Python and lxml’s objectify: Part 1.5: Advanced Parsing (XML, HTML, XHTML, oh my!)

There are dozens of scraping and parsing tools out there but sometimes they are too bloated or simply don’t do what you want them to do. Some may think this is the Rube Goldberg approach but this keeps you in absolute control and really isn’t as hard as it seems. This illustrates some of the features of lxml’s objectify which can be used to parse simple XML down to HTML/XHTML and broken variations.

For this example, I will be using the source code from google.com. You could use urllib or urllib2 to fetch the source and store it in a StringIO object. In this demonstration, I’ve loaded the source to google.com in a StringIO object.

NOTE: Looking at the google.com source, it kind-of makes my eyes want to bleed...

Read More

XML fun with Python and lxml’s objectify: Part 1: Parsing

I assume you’ve already read about lxml.objectify so I won’t bother being redundant but I am head-over-heels in love with it over lxml’s objectify. The goal of this is to be a supplement to lxml’s documentation and give real-world examples from my ETL experience using it. This is also assuming that you have some familiarity with Python and Python datatypes. With that quick blurb out of the way, let’s get to some fun!

For this work, I will be using a sample XML file (test.xml) consisting of:







The parser I will be using will require a file path or object but you can use a string parser if you are working with XML, XHTML, HTML, etc from other sources. First a parser needs to be created then objectify will parse the data against the parser...

Read More