What is a generative method?

Go To StackoverFlow.com

19

I'm familiar with Python generators, however I've just come across the term "generative method" which I am not familiar with and cannot find a satisfactory definition.

To put it in context, I found the term in SQLAlchemy's narrative documentation:

Full control of the “autocommit” behavior is available using the generative Connection.execution_options() method provided on Connection, Engine, Executable, using the “autocommit” flag which will turn on or off the autocommit for the selected scope.

What is a generative method? Trying to iterate the object returned by Connection.execution_options() doesn't work so I'm figuring it's something other than a standard generator.

2012-04-05 02:29
by Matty
Some googling suggests it could be talking about code generation, but that seems unlikely... http://www.program-transformation.org/Transform/GenerativeProgrammingWik - jimw 2012-04-05 02:34
It does not mean a python generator, that's for sure. This method is for modifying the connection state, so it makes no sense for it to produce a sequence - alexis 2012-04-07 10:05


30

It doesn't appear to be a common database concept, but SQLAlchemy uses the term generative in the sense "generated by your program iteratively at runtime". (So, no connection to python generators). An example from the tutorial:

The Query object is fully generative, meaning that most method calls return a new Query object upon which further criteria may be added. For example, to query for users named “ed” with a full name of “Ed Jones”, you can call filter() twice, which joins criteria using AND:

>>> for user in session.query(User).\
...   filter(User.name=='ed').\
...   filter(User.fullname=='Ed Jones'):
...     print user

This call syntax is more commonly known as "method chaining", and the design that allows it as a "fluent interface".

So, in the case of Connection.execution_options(), "generative" means that it returns the modified connection object, so that you can chain the calls as above.

2012-04-07 10:13
by alexis
It also "generates" SQL statements piecemeal - Keith 2012-04-08 13:23
Yes, and all sorts of other objects. SQL statements are what Query encapsulates, in fact - alexis 2012-04-08 13:25
good call I might want to add that term to the doc - zzzeek 2012-04-15 21:53
From the horse's mouth, then! :-) Thanks - alexis 2012-04-16 13:09


4

Looking at the source code of Connection.execution_options (lib/sqlalchemy/engine/base.py), all that method does is add options to the connection.

The idea is that those options influence the future behaviour of e.g. queries.

As an example:

        result = connection.execution_options(stream_results=True).\
                            execute(stmt)

Here, the behaviour was changed in the middle of the connection for just this query. In a way, it "generates" or clones itself as an object that has a slightly different behaviour.

Here you can also set autocommit to True. Example

# obtain a connection
connection = ...
# do some stuff
# for the next section we want autocommit on
autocommitting_connection = connection.execution_options(autocommit=True)
autocommitting_connection.execute(some_insert)
result = autocommitting_connection.execute(some_query)
# done with this section. Continue using connection (no autocommit)

This is what is meant with that section of the documentation. "generative method" refers to a method that returns a modified copy of the same instance that you can continue working with. This is applicable to the classes Connection, Engine, Executable.

2012-04-08 10:48
by j13r
Don't be too sure the change is "for just this query". Some of the options affect the underlying connection object, and so they persist - alexis 2012-04-08 21:55
@alexis in the example I give ("Here") it is just for this query. The connection is never modified by the generative method, but a modified copy returned. Unless you care to give an example where the underlying connection object is truly affected - j13r 2012-04-09 04:05
According to http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.base.Connection.execution_options, changes in isolation_level persist until the underlying connection is closed - alexis 2012-04-09 12:06


3

You would have to consult the specific documentation or source code of that project to really make sure, but I would guess that it returns a modified version of some object adapted to the requirements/behaviour defined by the arguments.

The documentation states:

The method returns a copy of this Connection which references the same underlying DBAPI connection, but also defines the given execution options which will take effect for a call to execute().

2012-04-05 02:44
by Preet Kukreti


0

As @zzzeek comments above, this is now documented in the SQLAlchemy glossary.

generative means:

A term that SQLAlchemy uses to refer what’s normally known as method chaining; see that term for details.

And method chaining is:

An object-oriented technique whereby the state of an object is constructed by calling methods on the object. The object features any number of methods, each of which return a new object (or in some cases the same object) with additional state added to the object.

2016-09-13 10:19
by lingfish
Ads