Re: [sqlalchemy] On new native PG Range in SA 2.0

2022-10-22 Thread Mike Bayer


On Sat, Oct 22, 2022, at 6:57 AM, Lele Gaifax wrote:
> Hi,
> 
> as stated in 
> https://docs.sqlalchemy.org/en/20/dialects/postgresql.html#range-and-multirange-types,
>  
> in SA 2.0 the PG "range" (and "multirange") types are wrapped in a new
> Range class, while the underlying driver-specific class is "hidden"
> within the dialect implementation.
> 
> This means that many operations that were previously exposed to the
> Python logic are not available anymore, and I'm not sure if that is
> intentional, not-yet-implemented functionalities or instead just me
> missing something.

we were going for the lowest common denominator of functionality, which was to 
be able to persist and load Range objects.   If you look at asyncpg's Range 
type, it's very basic: 
https://github.com/MagicStack/asyncpg/blob/eccdf61afb0116f9500f6fb2f832058ba8eb463e/asyncpg/types.py#L42

We looked at the methods that psycopg2's Range has and they seemed to be 
related related to psycopg2's own internal needs, such as the comparison 
operator implementations, which don't make any real sense and are documented as 
"arbitrary": 
https://github.com/psycopg/psycopg2/blob/20bb48666312e6f4747d1f6187f520c812f29591/lib/_range.py#L159

More obvious methods like issubset and issuperset 
(https://github.com/MagicStack/asyncpg/blob/master/asyncpg/types.py#L110) are 
easy to add and are just a pull request / issue away.   as for psycopg2's 
__contains__, that also looks very simple but I'd rather have it as an explicit 
name like "contains_value()".

As far as the Multirange type in psycopg3, I looked at that and the features it 
had seemed to be redundant vs. what you can do with a plain list, and indeed 
asyncpg implements multirange using lists of Ranges which IMO is much more 
natural and easy to work with.


> 
> For example, one of the trickiest things I would need to reimplement is
> the Python-side equivalent of the "<@" operator, that checks whether an
> element is contained in a range: in SA 1.x this was readily exposed by
> the driver implementation, usually by a `__contains__` method
> (see https://github.com/psycopg/psycopg2/blob/master/lib/_range.py#L121
> for psycopg2).

Similar methods are a PR (with tests) away from being included.


> 
> What do you foresee, that the Range class should/will "proxies" those
> capabilities, or instead that I should change (pseudo)code like::

Range is a really simple dataclass so adding a few simple value methods 
contains_value(), issubset(), issuperset() is a PR (with tests) away.   there's 
no need to "proxy" things, these are pretty obvious methods to just implement 
directly.   I would just disagree with psycopg2's more nonsensical methods like 
`__lt__`, `__gt__`, etc. 

> 

-- 
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/65c1d0aa-bf26-459c-8cc2-60f9083292fc%40app.fastmail.com.


Re: [sqlalchemy] iterate sqlalchemy query over for loop in my template python-flask

2022-10-22 Thread Abdellah ALAOUI ISMAILI
wheel ... in my template, I get just the first tag color, returned from the 
function.

this is the result of my HTML file source code :


 LOG  

  EXADATA  

  DMZ_PRIVE  

 

I hope it's clear, 
thank you .

Le vendredi 21 octobre 2022 à 09:23:48 UTC+1, Simon King a écrit :

> I don't understand the question. Are you saying that only one tag is 
> displayed? If so, that's not a problem with SQLAlchemy, it's a problem with 
> your template logic.
>
> If that's not what you mean, you need to give us more information. What is 
> the value of "server.tags", and what is the output from the template?
>
> Simon
>
> On Thu, Oct 20, 2022 at 9:05 AM Abdellah ALAOUI ISMAILI <
> my.ala...@gmail.com> wrote:
>
>> Hello,
>> I call a function in my template that returns sqlalchemy query result, 
>> (color value from the name of the tag). this is the query function :
>>
>> *def get_tag_color(name): *
>> *return db.session.query(Tag.tag_color).filter(Tag.tag_name == 
>> name).scalar() *
>>
>> and I call it in my template file:
>> * {% if server.tags %}*
>> * {% for tag in server.tags.split(",") %} *
>> * > class="label"> {{tag}} *
>> * {% endfor %} {% endif %} * 
>>
>> the problem is that I get just one result of the first tag. do you have 
>> any idea what I miss?
>>
>> Thank you 
>>
>> -- 
>> 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/4f9d1bab-479d-47d5-89cf-c305026ec3d7n%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/28c544b5-5445-45f9-ac8e-1dc786984d82n%40googlegroups.com.


[sqlalchemy] On new native PG Range in SA 2.0

2022-10-22 Thread Lele Gaifax
Hi,

as stated in 
https://docs.sqlalchemy.org/en/20/dialects/postgresql.html#range-and-multirange-types,
 
in SA 2.0 the PG "range" (and "multirange") types are wrapped in a new
Range class, while the underlying driver-specific class is "hidden"
within the dialect implementation.

This means that many operations that were previously exposed to the
Python logic are not available anymore, and I'm not sure if that is
intentional, not-yet-implemented functionalities or instead just me
missing something.

For example, one of the trickiest things I would need to reimplement is
the Python-side equivalent of the "<@" operator, that checks whether an
element is contained in a range: in SA 1.x this was readily exposed by
the driver implementation, usually by a `__contains__` method
(see https://github.com/psycopg/psycopg2/blob/master/lib/_range.py#L121
for psycopg2).

What do you foresee, that the Range class should/will "proxies" those
capabilities, or instead that I should change (pseudo)code like::

  class Contract:
 active = Column(bool)
 validity = Column(daterange)
 
 def is_currently_active(self):
return self.active and date.today() in self.validity

to something different, that in some way "reaches" the underlying
psycopg (say) implementation and uses its logic?

Thanks in advance,
ciao, lele.
-- 
nickname: Lele Gaifax | Dire che Emacs è "conveniente" è come
real: Emanuele Gaifas | etichettare l'ossigeno come "utile"
l...@etour.tn.it  |   -- Rens Troost

-- 
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/87o7u4f7me.fsf%40metapensiero.it.