Showing posts with label RDBMS. Show all posts
Showing posts with label RDBMS. Show all posts

Friday, January 18, 2019

Announcing PIaaS - Python Interviewing as a Service



Hello, readers,

Announcing Python Interviewing as a Service:

I'm now officially offering PIaaS - Python Interviewing as a Service. I have done it some earlier, informally, for clients. Recently a couple of companies asked me for help on this again, so I am now adding it to my list of offered services, the others being consulting (software design and development, code review, technology evaluation and recommendation) and software training.

I can help your organization interview and hire Python developer candidates, offloading (some of) that work from your core technical and HR / recruitment staff.

I can also interview on related areas like SQL and RDBMS, and Unix and Linux commands and shell scripting.

I have long-term experience in all the above areas.

To hire me for PIaaS or to learn more about it, contact me via the Gmail address on my site's contact page.

- Vasudev Ram

My Codementor profile: Vasudev Ram on Codementor


Sunday, July 24, 2016

Control break report to PDF with xtopdf

By Vasudev Ram

Hi readers,

Control break reports are very common in data processing, from the earliest days of computing until today. This is because they are a fundamental kind of report, the need for which is ubiquitous across many kinds of organizations.

Here is an example program that generates a control break report and writes it to PDF, using xtopdf, my Python toolkit for PDF creation.

The program is named ControlBreakToPDF.py. It uses xtopdf to generate the PDF output, and the groupby function from the itertools module to handle the control break logic easily.

I've written multiple control-break report generation programs before, including implementing the logic manually, and it can get a little fiddly to get everything just right, particularly when there is more than one level of nesting (i.e. no off-by-one errors, etc.); you have to check for various conditions, set flags, etc.

So it's nice to have Python's itertools.groupby functionality handle it, at least for basic cases. Note that the data needs to be sorted on the grouping key, in order for groupby to work. Here is the code for ControlBreakToPDF.py:
from __future__ import print_function

# ControlBreakToPDF.py
# A program to show how to write simple control break reports
# and send the output to PDF, using itertools.groupby and xtopdf.
# Author: Vasudev Ram
# Copyright 2016 Vasudev Ram
# http://jugad2.blogspot.com
# https://gumroad.com/vasudevram

from itertools import groupby
from PDFWriter import PDFWriter

# I hard-code the data here to make the example shorter.
# More commonly, it would be fetched at run-time from a 
# database query or CSV file or similar source.

data = \
[
    ['North', 'Desktop #1', 1000],
    ['South', 'Desktop #3', 1100],
    ['North', 'Laptop #7', 1200],
    ['South', 'Keyboard #4', 200],
    ['North', 'Mouse #2', 50],
    ['East', 'Tablet #5', 200],
    ['West', 'Hard disk #8', 500],
    ['West', 'CD-ROM #6', 150],
    ['South', 'DVD Drive', 150],
    ['East', 'Offline UPS', 250],
]

pw = PDFWriter('SalesReport.pdf')
pw.setFont('Courier', 12)
pw.setHeader('Sales by Region')
pw.setFooter('Using itertools.groupby and xtopdf')

# Convenience function to both print to screen and write to PDF.
def print_and_write(s, pw):
    print(s)
    pw.writeLine(s)

# Set column headers.
headers = ['Region', 'Item', 'Sale Value']
# Set column widths.
widths = [ 10, 15, 10 ]
# Build header string for report.
header_str = ''.join([hdr.center(widths[ind]) \
    for ind, hdr in enumerate(headers)])
print_and_write(header_str, pw)

# Function to base the sorting and grouping on.
def key_func(rec):
    return rec[0]

data.sort(key=key_func)

for region, group in groupby(data, key=key_func):
    print_and_write('', pw)
    # Write group header, i.e. region name.
    print_and_write(region.center(widths[0]), pw)
    # Write group's rows, i.e. sales data for the region.
    for row in group:
        # Build formatted row string.
        row_str = ''.join(str(col).rjust(widths[ind + 1]) \
            for ind, col in enumerate(row[1:]))
        print_and_write(' ' * widths[0] + row_str, pw)
pw.close()
Running it gives this output on the screen:
$ python ControlBreakToPDF.py
  Region        Item     Sale Value

   East
                Tablet #5       200
              Offline UPS       250

  North
               Desktop #1      1000
                Laptop #7      1200
                 Mouse #2        50

  South
               Desktop #3      1100
              Keyboard #4       200
                DVD Drive       150

   West
             Hard disk #8       500
                CD-ROM #6       150

$
And this is a screenshot of the PDF output, viewed in Foxit PDF Reader:


So the itertools.groupby function basically provides roughly the same sort of functionality that SQL's GROUP BY clause provides (of course, when included in a complete SELECT statement). The difference is that with Python's groupby, you do the grouping and related processing in your program code, on data which is in memory, while if using SQL via a client-server RDBMS from your program, the grouping and processing will happen on the database server and only the aggregate results will be sent to your program to process further. Both methods can have pros and cons, depending on the needs of the application.

In my next post about Python, I'll use this program as one vehicle to demonstrate some uses of randomness in testing, continuing the series titled "The many uses of randomness", the earlier two parts of which are here and here.

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Follow me on Gumroad to be notified about my new products:




My Python posts     Subscribe to my blog by email

My ActiveState recipes



Thursday, January 9, 2014

Generate PDF output from Firebird SQL data

By Vasudev Ram



PDF


Firebird is a cross-platform, lightweight, open source, relational database that was derived from Borland's Interbase, a database that was bundled with some versions of Borland's development tools like Delphi and Borland C++.

Firebird on Wikipedia.

I've used it some in the past. It is fairly easy to setup and use.

Here is a demo program that shows how to generate PDF output from a Firebird database, using my xtopdf PDF creation toolkit and the fbd Python driver for Firebird.
# FirebirdToPDF.py
# Author: Vasudev Ram - http://www.dancingbison.com
# Demo program to show how to convert Firebird RDBMS data to PDF.
# Uses xtopdf, Reportlab, Firebird RDBMS and fdb Python driver for Firebird.

import fdb
from PDFWriter import PDFWriter

con = fdb.connect(dsn='localhost:/temp/firebird/test.fdb', 
    user='dummy',  password='dummy')

cur = con.cursor()
select_stmt = 'select * from contacts order by name desc'
cur.execute(select_stmt)
pw = PDFWriter("contacts.pdf")
pw.setFont("Courier", 12)
pw.setHeader("Firebird contacts.fdb to PDF")
pw.setFooter("Generated by xtopdf using fdb Firebird driver for Python")
for (id, name, address) in cur:
    print id, name, address
    pw.writeLine(str(id) + " " + name + " " + address)
pw.close()

# EOF
You can run it with the command: python FirebirdToPDF.py

This is a screenshot of the output PDF created (click to enlarge):


The status page about fbd on the Firebird site is interesting. Excerpts from it (emphasis mine):

[ FDB is a Python library package that implements Python Database API 2.0-compliant support for the open source relational database Firebird®. In addition to the minimal feature set of the standard Python DB API, FDB also exposes the entire native client API of the database engine. Notably:
Automatic data conversion from strings on input.
Automatic input/output conversions of textual data between UNICODE and database character sets.
Support for prepared SQL statements.
Multiple independent transactions per single connection.
All transaction parameters that Firebird supports, including table access specifications.
Distributed transactions.
Firebird BLOB support, including support for stream BLOBs.
Support for Firebird Events.
Support for Firebird ARRAY data type.
Support for all Firebird Services
FDB is implemented in Python on top of Firebird client library using ctypes.

FDB works with Firebird 2.0 and newer, Python 2.7+ and 3.0+. ]

- Vasudev Ram - Dancing Bison Enterprises

Contact Page




Friday, December 20, 2013

OrientDB, a distributed transactional graph database

By Vasudev Ram

SQL



OrientDB from Orient Technologies (in London, UK :-) is a distributed transactional graph database.

I saw it via this post on Hacker News:

OrientDB, the most versatile database I’ve run across

The article linked in that post is here:

OrientDB. Thanks!!!

An interesting point in that article is that the author, Petter Graff, thinks that OrientDB is versatile, and somewhat unusual in that it seems to be good for graph, document and object-oriented database applications - which may not be common in a single database product.

I scanned some pages on the OrientDB / Orient Technologies sites and got an overview of the product. It has a native Java and a JDBC interface. It also seems to have interfaces, i.e. bindings, to some other popular programming languages or technologies, including JavaScript, Scala, C, PHP. .NET, Python, Node.js, Clojure and Android.


They have both a free Community edition and an Enterprise edition, with professional services. OrientDB customers includes Lufthansa, Pitney Bowes and Cisco, according to their site.

I'm checking out OrientDB (with the Python binding) and will blog again later if I find it interesting.

- Vasudev Ram - Python, Linux and open source consulting

Contact Page


O'Reilly 50% Ebook Deal of the Day


Monday, July 30, 2012

Firebird, free and open source database - ex-Borland

By Vasudev Ram




Firebird is a free and open source relational database based on source code from Borland.

About Firebird

I had first come across Firebird some years earlier. It was good back then: easy to install, easy to use, and fast.

Came across it on the Net again recently.

It can be a good alternative to other free databases like MySQL and PostgreSQL, for smaller applications, particularly where low administration overhead is needed. Also, it is cross-platform to some extent - works on Windows and Linux.

- Vasudev Ram - Dancing Bison Enterprises