Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

astuple method #77

Closed
Insoleet opened this issue Sep 5, 2016 · 7 comments
Closed

astuple method #77

Insoleet opened this issue Sep 5, 2016 · 7 comments
Labels
Milestone

Comments

@Insoleet
Copy link
Contributor

Insoleet commented Sep 5, 2016

Hello,

For the usage of attr with DB API 2.0 libraries (like Sqlite3), it would be great to have a astuple method.

This method would return the object values in a tuple, ordered by the order of declaration of it's attributes.

For example we could do things like this :

import sqlite3
import attr

@attr.s()
class Foo:
    a = attr.ib()
    b = attr.ib()

foo = Foo(2, 3)
con = sqlite3.connect(":memory:")
with con:
    con.execute("INSERT INTO foo VALUES (?, ?)", attr.astuple(foo))

This would be useful for DB API libraries but also for other needs probably.

What is your opinion about it ?

@hynek
Copy link
Member

hynek commented Sep 6, 2016

Your use case is compelling indeed. attr.asdict(foo).values() probably gets old. Unless @glyph tells me a good reason against, I would allow it.

@hynek hynek added the Feature label Sep 6, 2016
@Tinche
Copy link
Member

Tinche commented Sep 6, 2016

attr.asdict(foo).values() wouldn't even work unless you used an ordered dict :)

@Tinche
Copy link
Member

Tinche commented Sep 6, 2016

I think this would be a nice mirror to asdict. Also a tuple of values will be more efficient than a dict if you can use this.

@Insoleet Would you like to implement this? It's relatively easy and more contributors is always good :) I can do a preliminary review before @hynek.

@Insoleet
Copy link
Contributor Author

Insoleet commented Sep 6, 2016

I'd be glad to add my commit to this wonderful library ;) I'll try to submit a pull request during the week.

@Insoleet
Copy link
Contributor Author

Insoleet commented Sep 6, 2016

How do you think that dict set list tuple should be handled by the astuple method ? Using deepcopy ? Using references ? Or something else ?

@hynek
Copy link
Member

hynek commented Sep 6, 2016

does recursing make even sense with tuples? oO

@Tinche
Copy link
Member

Tinche commented Sep 6, 2016

We should probably try to mirror asdict.

For nested attr classes, this would be:

@attr.s
class A:
    b = attr.ib()

@attr.s
class B:
    c = attr.ib()

>>> a = A(B(1))
>>> 
>>> attr.asdict(a)
{'b': {'c': 1}}
>>> attr.asdict(a, recurse=False)
{'b': B(c=1)}
>>> attr.astuple(a)
((1,),)
>>> attr.astuple(a, recurse=False)
(B(c=1),)

For built-in collections... Just do what asdict does?

>>> attr.asdict(A([B(1), B(2), B(3)]))
{'b': [{'c': 1}, {'c': 2}, {'c': 3}]}
>>> attr.astuple(A([B(1), B(2), B(3)]))
([(1,), (2,), (3,)],)

Another side of the mirroring: an non-nested attr class can be dumped and loaded like this:

b = B(1)
b == B(**attr.asdict(b))

so tuples work in a similar way:

b = B(1)
b == B(*attr.astuple(b))

Thoughts?

@hynek hynek modified the milestone: 16.2.0 Sep 8, 2016
@hynek hynek closed this as completed in 1b419e3 Sep 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants