Re: [sqlalchemy] row.Row and dict behavior in 2.0

2023-01-12 Thread Mike Bayer


On Thu, Jan 12, 2023, at 1:46 PM, mkmo...@gmail.com wrote:
> Hi Mike,
> 
> Thanks. I have a few cases where it is easiest if I have a plain dict instead 
> of a RowMapping. For example RowMapping is immutable, and isn't playing 
> nicely with my json encoder.
> 
> What is your preferred method to convert to a plain dict?

for row in result.mappings():
d = dict(row)

should do it

the row was not mutable in 1.4, 1.3, etc. either, what did you do then ?

> 
> Row._asdict()
> dict(RowMapping.items())
> something else? 
> 
> Thanks and best regards,
> 
> Matthew
> 
> 
> On Monday, January 9, 2023 at 6:00:08 PM UTC-8 Mike Bayer wrote:
>> 
>> 
>> On Mon, Jan 9, 2023, at 8:50 PM, mkmo...@gmail.com wrote:
>>> Hello,
>>> 
>>> It looks like in 2.0 we can no longer treat a row.Row as a dict. I have a 
>>> few cases where I want to do this, such as when I need to get a list of 
>>> columns, or when I don't know the column name in advance.
>>> 
>>> rows = conn.execute(select(t.c.foo)).fetchall()
>>> 
>>> rows[0].keys()  # Not Allowed
>>> 
>>> rows[0][some_unknown_column]  # not allowed
>>> 
>>> If we need to treat it as a dict, are we supposed to be calling:
>>> 
>>> rows[0]._asdict()
>>> 
>>> This works, but the only issue is that our IDEs flag this as accessing a 
>>> protected member of a class. Is there any alternative?
>>> 
>>> Thanks and best regards,
>> 
>> you have more options here than previously on how to treat rows, as tuples 
>> or mappings, either up front or on a per-row basis.  the new API has been  
>> available as of version 1.4.  Relevant links include:
>> 
>> 1. announcement of change and rationale
>> 
>> https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#rowproxy-is-no-longer-a-proxy-is-now-called-row-and-behaves-like-an-enhanced-named-tuple
>> 
>> 2. migration guide
>> 
>> https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#result-rows-act-like-named-tuples
>> 
>> 3. new tutorial coverage
>> 
>> https://docs.sqlalchemy.org/en/14/tutorial/dbapi_transactions.html#fetching-rows
>> 
>> included is background on how to get mappings from a Row or how to get 
>> RowMapping objects from a result up front using result.mappings().
>> 
>> 
>> 
>> 
>> 
>> 
>>> 
>>> Matthew
>>> 
>>> 
>>> -- 
>>> SQLAlchemy - 
>>> The Python SQL Toolkit and Object Relational Mapper
>>>  
>>> http://www.sqlalchemy.org/
>>>  
>>> To post example code, please provide an MCVE: Minimal, Complete, and 
>>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
>>> description.
>>> --- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to sqlalchemy+...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/sqlalchemy/10721070-ecf9-4438-87dc-a9ff6c13c0dan%40googlegroups.com
>>>  
>>> .
>> 
> 
> 
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/624b2fdc-1c9d-4c3b-9424-f091b4be529an%40googlegroups.com
>  
> .

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/4f191969-46b7-4f14-afd1-3cfb26002d24%40app.fastmail.com.


Re: [sqlalchemy] row.Row and dict behavior in 2.0

2023-01-12 Thread mkmo...@gmail.com
Hi Mike,

Thanks. I have a few cases where it is easiest if I have a plain dict 
instead of a RowMapping. For example RowMapping is immutable, and isn't 
playing nicely with my json encoder.

What is your preferred method to convert to a plain dict?

Row._asdict()
dict(RowMapping.items())
something else? 

Thanks and best regards,

Matthew


On Monday, January 9, 2023 at 6:00:08 PM UTC-8 Mike Bayer wrote:

>
>
> On Mon, Jan 9, 2023, at 8:50 PM, mkmo...@gmail.com wrote:
>
> Hello,
>
> It looks like in 2.0 we can no longer treat a row.Row as a dict. I have a 
> few cases where I want to do this, such as when I need to get a list of 
> columns, or when I don't know the column name in advance.
>
> rows = conn.execute(select(t.c.foo)).fetchall()
>
> rows[0].keys()  # Not Allowed
>
> rows[0][some_unknown_column]  # not allowed
>
> If we need to treat it as a dict, are we supposed to be calling:
>
> rows[0]._asdict()
>
> This works, but the only issue is that our IDEs flag this as accessing a 
> protected member of a class. Is there any alternative?
>
> Thanks and best regards,
>
>
> you have more options here than previously on how to treat rows, as tuples 
> or mappings, either up front or on a per-row basis.  the new API has been  
> available as of version 1.4.  Relevant links include:
>
> 1. announcement of change and rationale
>
>
> https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#rowproxy-is-no-longer-a-proxy-is-now-called-row-and-behaves-like-an-enhanced-named-tuple
>
> 2. migration guide
>
>
> https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#result-rows-act-like-named-tuples
>
> 3. new tutorial coverage
>
>
> https://docs.sqlalchemy.org/en/14/tutorial/dbapi_transactions.html#fetching-rows
>
> included is background on how to get mappings from a Row or how to get 
> RowMapping objects from a result up front using result.mappings().
>
>
>
>
>
>
>
> Matthew
>
>
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/10721070-ecf9-4438-87dc-a9ff6c13c0dan%40googlegroups.com
>  
> 
> .
>
>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/624b2fdc-1c9d-4c3b-9424-f091b4be529an%40googlegroups.com.


[sqlalchemy]

2023-01-12 Thread Kurt Forrester


-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CABkCPzYXv_rcc%3D%2BZvro%3DrU1ZYirN701UMrsRH22dD_FK%3DdvnAQ%40mail.gmail.com.


Re: [sqlalchemy] Adding OR () with do_orm_execute

2023-01-12 Thread Mike Bayer
if you have a select() construct, you can add new AND things just calling 
.where() again.  e.g. existing_statement.where(or_(f1, f2)) .

On Wed, Jan 11, 2023, at 11:26 PM, Val Huber wrote:
> I am using this and it is working well to AND a series of filters to a 
> statement.
> 
> But I do not see how to OR these into the original statement.
> 
> For example, I have filters f1 and f2.  I want the result to be:
> 
> where  AND (f1 OR f2).
> 
> Thanks in advance,
> Val
> 
> 
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/69c0c26f-812c-4c0f-b435-c4903ffdf57dn%40googlegroups.com
>  
> .

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/f85048a8-51db-4eb4-9781-b40d1e0a6dee%40app.fastmail.com.


Re: [sqlalchemy] Determine transaction after_flush state

2023-01-12 Thread Mike Bayer
it means the Session is in the middle of the "flush()" method.  it's a boolean 
state.  when flush() is over, it turns back off.

On Wed, Jan 11, 2023, at 9:27 PM, Val Huber wrote:
> Sounds like *session._flushing* is exactly what I want… presuming it means “I 
> have started flushing, in fact may have flushed any number of times”.
> 
> Thanks for the again-quick response, if my presumption is correct, I am all 
> set.
> 
> Regards,
> Val
> 
>> On Jan 11, 2023, at 6:22 PM, Mike Bayer 
>>  wrote:
>> 
>> 
>> a session can flush any number of times, so what here determines "after" 
>> flush state as far as do_orm_execute()? do_orm_execute is not called within 
>> the flush itself (well, maybe it is...)
>> 
>> If you see do_orm_executes inside of the flush, I guess you could look at 
>> session._flushing, is that what you are looking for ?
>> 
>> On Wed, Jan 11, 2023, at 7:44 PM, Val Huber wrote:
>>> In API Logic Server, clients can make RESTful requests that cause 
>>> SQLAlchemy reads.  These are subjected to authorization security using 
>>> do_orm_execute (which is amazing, by the way).  These add filters to ensure 
>>> proper access.  This is all fine.
>>> 
>>> But, when the client makes changes, these activate business logic, which 
>>> needs to run *without* auth security.
>>> 
>>> My question is: how can my do_orm_execute event handler determine we are 
>>> in/after after-flush state?  Sorry if I missed them, but I did not find 
>>> variables method on session or transaction for this.
>>> 
>>> Thanks in advance,
>>> Val
>>> 
>>> 
>>> -- 
>>> SQLAlchemy - 
>>> The Python SQL Toolkit and Object Relational Mapper
>>>  
>>> http://www.sqlalchemy.org/
>>>  
>>> To post example code, please provide an MCVE: Minimal, Complete, and 
>>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
>>> description.
>>> --- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to sqlalchemy+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/sqlalchemy/59922a81-8e2c-4575-87c8-e178a99c7a96n%40googlegroups.com
>>>  
>>> .
>> 
>> 
>> -- 
>> SQLAlchemy - 
>> The Python SQL Toolkit and Object Relational Mapper
>>  
>> http://www.sqlalchemy.org/
>>  
>> To post example code, please provide an MCVE: Minimal, Complete, and 
>> Verifiable Example. See  http://stackoverflow.com/help/mcve for a full 
>> description.
>> --- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "sqlalchemy" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/sqlalchemy/kO4zI2r6k0c/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> sqlalchemy+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/sqlalchemy/d864d888-a058-403b-b25b-315cb16d3158%40app.fastmail.com
>>  
>> .
> 
> 
> 
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/24E9CFA0-2309-4BB6-AA42-B1DBC5833FCF%40gmail.com
>  
> .

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/cfd89872-d738-40aa-bffc-6b698c28e7f0%40app.fastmail.com.