Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <uti7n1$2ef92$1@dont-email.me>
Deutsch   English   Français   Italiano  
<uti7n1$2ef92$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Mark Bourne <nntp.mbourne@spamgourmet.com>
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Thu, 21 Mar 2024 21:09:53 +0000
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <uti7n1$2ef92$1@dont-email.me>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Mar 2024 21:09:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e29d700c3c650c2f8b31878382de2e99";
	logging-data="2571554"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18bLhpF/rqU1jembenFCli+"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
 SeaMonkey/2.53.18.1
Cancel-Lock: sha1:fbz5PU1cclYuIrEzDvAkQND08SQ=
In-Reply-To: <87zfurgb6t.fsf@zedat.fu-berlin.de>
Bytes: 3240

Loris Bennett wrote:
> Hi,
> 
> I am using SQLAlchemy to extract some rows from a table of 'events'.
>  From the call to the DB I get a list of objects of the type
> 
>    sqlalchemy.orm.state.InstanceState
> 
> I would like to print these rows to the terminal using the 'tabulate'
> package, the documentation for which says
> 
>    The module provides just one function, tabulate, which takes a list of
>    lists or another tabular data type as the first argument, and outputs
>    a nicely formatted plain-text table
> 
> So as I understand it, I need to convert the InstanceState-objects to,
> say, dicts, in order to print them.  However I also want to remove one
> of the keys from the output and assumed I could just pop it off each
> event dict, thus:
>           
>      event_dicts = [vars(e) for e in events]
>      print(type(event_dicts[0]))
>      event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
>      print(type(event_dicts[0]))

vars() returns the __dict__ attribute of the object.  It may not be a 
good idea to modify that dictionary directly (it will also affect the 
object), although it might be OK if you're not going to do anything else 
with the original objects.  To be safer, you could copy the event objects:
     event_dicts = [dict(vars(e)) for e in events]
or:
     event_dicts = [vars(e).copy()]

> However, this prints
> 
>    <class 'dict'>
>    <class 'sqlalchemy.orm.state.InstanceState'>
> 
> If I comment out the third line, which pops the unwanted key, I get
> 
>    <class 'dict'>
>    <class 'dict'>
> 
> Why does popping one of the keys cause the elements of the list to
> revert back to their original class?

As Dieter pointed out, the main problem here is that pop() returns the 
value removed, not the dictionary with the rest of the values.  You 
probably want something more like:
     for e in event_dicts:
         del e['_sa_instance_state']
(There's not really any point popping the value if you're not going to 
do anything with it - just delete the key from the dictionary)

-- 
Mark.