Simpycity: A Quick Tutorial
Using Simpycity is as easy as its name suggests - quick, easy. Simple. In keeping with that philosophy, Simpycity offers 3 constructs for database access: Function, Query, and Raw. At a glance, each name describes the type of access it offers: Function providing access through stored procedures, Query constructing a simple SQL query from your arguments, and Raw, which allows you to write your own SQL query directly. We will first describe the Function construct, which, if you recall from the introductory article,
>>> f = Function("get_rows")
is the most basic construct that can be created with Simpycity. When this is converted to SQL, it becomes
SELECT * FROM get_rows();
Now, any time that f() is called now, Simpycity will connect to your database, execute the query and return the results to you. Simple. But a bit limited. To make this function more versatile, we can do the following:
>>> f = Function("get_row",['id'])
The second argument to Function tells Simpycity that our function requires an argument, which we've named 'id'. This argument name isn't required to match the PostgreSQL function definition, it's purely for Simpycity. Now, we can call the function using normal positional arguments:
>>> result = f(1)
Or, using keyword arguments:
>>> result = f(id=1)
Simple and easy, and totally consistent with every other Python function you've ever used. Additionally, Simpycity supports returning only a subset of columns from your query:
>>> results = f(id=1, options=dict(columns=["id"]))
Which Simpycity will turn into:
SELECT id FROM get_row(1);
This works in more advanced cases as well, such as:
>>> results = f(id=1, options=dict(columns=["id as identifier","col1"]))
Which Simpycity will interpret as:
SELECT id as identifier, col1 FROM get_row(1);
Easy to use, and easy to remember. Next time, we'll take a look at the Query and Raw constructs, as well as the basis for collective Simpycity constructs. As always, Simpycity is a Commandprompt Open Source project, licensed under the terms of the Lesser GPL and you can get it from https://public.commandprompt.com/projects/simpycity/