[sqlalchemy] Re: ORM ramblings 2 - and announcing some o2r wrapper

2007-01-17 Thread svil


mmmh.
This is about underlying base-framework of a system, equivalent in
complexity to ERP.

So i have unknown hierarchy of classes - be them documents, entities,
whatever u fancy. And they can point to each other in an unknown way.

if u want some example: DocumentA1 has some data and references other
such documents (e.g. parent - in a reason-consequence chain); then
DocumentA2 inherits DocumentA1 and adds some more fields to fill.

More, i'm dealing with this O2R stuff over 10 years and i'm sick of
specificaly write something over and over if it can be automated.
But just recently i saw some light in the tunnel - SAlchemy.

Therefore, all this about automating the O2R mapping. Major step? can
be. i will do it.

On Jan 17, 11:29 am, Paul Johnston [EMAIL PROTECTED] wrote:

Hi Svil,

 here the case where B inherits A, and A references either A or B. i
 cannot get this to work.What may help everyone here, is if you can give a 
concrete usage example
of why you want to do this. It's likely there's a way to achieve what
you want to do without such major steps.

Paul



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: ORM ramblings 2 - and announcing some o2r wrapper

2007-01-17 Thread svil




 So i have unknown hierarchy of classes - be them documents, entities,
 whatever u fancy. And they can point to each other in an unknown way.
so, you are looking to create an application that generates python
applications, basically.  


hm, can be interpreted this way. just add 'and interpret them on the
fly'
i want to use and extend the python's self-reflection - and
self-modificiation - to get a sort-of self-interpretable thing, but
without generating any source. Haveing proper self-reflection can
auto-generate me the source - or multiple sources for multiple target
environments, e.g. idl-python-python+C+html+binary. i've done that
multiple times - but i want the thing to interpret itself. i know it's
slower, but more flexible.


if you want to use SA for that, you have to
completely master SA's relationships using simple, non-dynamically
generated examples first, so you can get a feel for what it does.  


i've done tons of this, to get where i am now. And i keep doing it. My
approach is like start always a new and try with minimal possible
stuff, so any thing i am using (e.g. those primary-joins) has been
required in a way, (e.g. when u have multiple references, SA wants u to
explicitly specify which is what).


For example, i notice in your example youre trying to send in primaryjoin
conditions which are already expressed in the Table objects - that
already is overly complex since SA can divine simple join conditions
from the tables themselves.  

is it errorneus to explicitly put the (correct) joins even if they
could be figured out by SA?


for each pattern you are trying to create
dynamically, create a non-dynamic version first, get that to work, then
figure out what about your dynamic thing is different from the simple
version.

yes, yes. so far so good, see all the sa_*py files; and when i started
combining the different viewpoints - pure inheritance, pure (cyclical)
references, and pure types, the thing broke.


i hear theres a hot new template language
called Mako that does this kind of thing.*


*hehe*. good. i have done many languages before, interpretable and
compileable, but now i am basing all my language creations stuff onto
python - that is, using the python as syntax interpreter, and eventualy
completely replacing the actual semantix; be it on-the-fly or via the
objects themselves.

Two examples are the StaticType declarative structure language
(creatings python objects of unchangeable structure; very usable for
DBs, protocols and similar must-not-change stuff), and the expr.py,
which has a way to interpret python functions into Expr-trees, which
then can be translated (read code-generation) into whatever, like text
or sql-alchemy column expressions.

It's all like using the python to build _your_ syntax tree made of your
objects, and then use the objects - or some visitor - to do
code-generation/ interpretation.
But this is all offtopic; i could explain much-more if anyone is
interested.

i really _really_ want to extend the SA to be able to automaticaly do
table-decomposition and mapping of an object-hierarchy.
As i said, think of activemapper that also does inheritance and
queries, provided just a few hints of how user exactly wants it done.

So, back to the question: inheritance and referencing together?
This is what stops me now; otherwise i have about 10 diff.cases that SA
goes wrong, but i've found a way around so don't bother about them
_now_.

Also, if u can fix that repeating return obj.__dict__[key] in
orm/attributes.py InstrumentedAttribute.get()...
to be just return value,
will be great. line 214 or so.
see, i am replacing it myself, runtime (see sahack4repeatability.py
;-), but it's not nice, changing library's source on the fly ;-).

Tomorrow i'll put a version stripped of all my statictype stuff, and
all of the testcases (if u want so), which may seem more nice to
you... all is about 1500 lines so far.

have fun.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: ORM ramblings 2 - and announcing some o2r wrapper

2007-01-17 Thread svil

  is it errorneus to explicitly put the (correct) joins even if they
  could be figured out by SA?
 its not.  but made your code that much harder to read
well, this case does require them :-(

  which has a way to interpret python functions into Expr-trees, which
  then can be translated (read code-generation) into whatever, like text
  or sql-alchemy column expressions.
 are you using AST for this at least ?  no need to reinvent the
 wheel
AST... i don't parse it, python parses it.  i just replace .__eq__()
with something else.
The funny thing is, the python compiled code of some expression can be
reinterpreted in diff.ways,
e.g. def myfunc(x,y): return x100  y.startswith('big')
..
myfunc( 23, big_balls_of_fire) - will eval the func yielding
true/false, while
myfunc( Var('x'), Var('y'))  - will build you an expression (procided
Var()s have operators etc defined). And even give errors - not any
syntax will fit your new semantix.

of course, theoretically, there is some AST, and some grammar, but it's
sort-a virtual...

  It's all like using the python to build _your_ syntax tree made of your
  objects, and then use the objects - or some visitor - to do
  code-generation/ interpretation.
 sounds awfully generic, when i could just write a little AST visitor...
Each time a new grammar and AST and new visitor? and teach users to
that new grammar? and testsuite preventing the huns into rome? mmh. i
did that. couple of languages per year...
Users (be them end-customers or the other-team) know their semantix
pretty well and they can cope with just about any _sensible_ easy
grammar as far as it speaks in their lingo.
oh well. my choice is - no more grammars  (lowlevel) - if i can avoid
them.

  As i said, think of activemapper that also does inheritance and
  queries, provided just a few hints of how user exactly wants it done.
  as far as querying, im guessing you
 are looking for an object-query-language (which is what all the pure
 ORM types seem to be desiring...but strangely they never get)
not at all. SQL, OQL and probably all them *QL frighten me. Too
extreme. Looks like academical creation with little touch to mortal
people. i am targeting what u have with columns, but applied directly
to object attributes, and used more for filtering, than real
calculations.
Practical stuff, as u said - whatever we can do, now.

  So, back to the question: inheritance and referencing together?
 im in the business of maintaining and developing
 SQLAlchemy, and if theres a bug which you can illustrate with a
 *simple* test case of around 50 lines or less, ill fix the bug,
110. can't make it less ;-)
can?
and it is about A=Employee and B=Manager, and all Employees having a
manager.
http://linuxteam.sistechnology.com/orm/sa_B_inh_A_A_ref_AB2.py

usage:
1) $ python sa_B_inh_A_A_ref_AB2.py Alink=Manager
or
2) $ python sa_B_inh_A_A_ref_AB2.py Alink=Empluyee
same 2 errors.
if of any help, i've found that if no manager (reference) is being ever
assigned, then case 1 does not crash.

-
from sqlalchemy import *

#table_inheritance, polymorphic
def case( Alink='Manager' ):

class Employee( object):
name = 'notset'
def __str__(me):
return ' '.join( [me.__class__.__name__, str(me.id),
str(me.name), getattr( me.manager, 'name', 'none') ])
class Manager( Employee ):
bonus = 'notset'
def __str__(me):
return Employee.__str__(me) + ' ' + str(me.bonus)

db = create_engine( 'sqlite:///:memory:')
meta = BoundMetaData( db)
meta.engine.echo = 0

class tables: pass

tables.Employee = Table('Employee', meta,
Column('id', Integer, primary_key=True),
Column('name', String, ),
Column('atype', String),
Column('manager_id', Integer,
ForeignKey( Alink+'.id',
   use_alter=True, name='whatever1'
)
)
)

tables.Manager = Table('Manager', meta,
Column('bonus', String, ),
Column('id', Integer,
ForeignKey( 'Employee.id'),
primary_key=True,
),
)
meta.create_all()


ajoin = {
'Employee': tables.Employee.select( tables.Employee.c.atype ==
'Employee'),
'Manager': join( tables.Employee, tables.Manager,
tables.Manager.c.id ==tables.Employee.c.id),
}
Ajoin = polymorphic_union( ajoin, None )
mA = mapper( Employee, tables.Employee,
select_table=Ajoin, polymorphic_on=Ajoin.c.atype,
polymorphic_identity='Employee'
)

mA.add_property( 'manager',
relation( Alink == 'Employee' and Employee or Manager,
primaryjoin=
tables.Employee.c.manager_id==(Alink=='Employee' and tables.Employee or
tables.Manager).c.id,
lazy=True, uselist=False, post_update=True
)
)

mB = mapper( Manager,