Comments (17)
Perhaps implement __subclasshook__
.
from bidict.
Thanks for raising this issue, @pacholik. The thing is, bidict
actually isn't a subclass of dict
, it's an implementation of collections.Mapping. By checking for that your code will be more polymorphic:
>>> from collections import Mapping
>>> from bidict import bidict
>>> b = bidict()
>>> isinstance(b, Mapping)
True
I could understand the argument that being able to check isinstance(b, dict)
would be more convenient, but if it's not actually necessary or true, would this be compromising purity for the sake of convenience, or is this actually an omission?
Let me know what you think, and meanwhile I'll give the collections.abc docs another read, and if it's still not clear, appeal to Python-dev etc. for more expert guidance.
from bidict.
I'd say, then, no need to allege via __subclasshook
that issubclass(bidict, dict)
because I think it's probably better for user code to check issubclass(bidict, Mapping)
(or, more likely, isinstance(thing, Mapping)
as opposed to isinstance(thing, dict)
) -- and evidently bidict
already does the right thing.
from bidict.
Cool, in that case just closed this with a documentation clarification. @pacholik, please let me know if this doesn't adequately address your use case.
In general, I always appreciate meeting new bidict users (thanks for stopping by:), hearing about your use cases, and any other ways bidict could better meet them. Please let me know anything you'd like to share!
from bidict.
You're welcome, and thanks for a good package. I've used it most recently to biject between database column names which aren't valid Python identifiers, and SQLAlchemy names for the same columns which are (valid Python identifiers).
from bidict.
Interesting, hadn't encountered this use case, thanks for sharing! I wonder if this is common. If so maybe I'll ask SQLAlchemy if they'd accept a PR adding a tip to their docs about this? /cc @zzzeek (ps hey @zzzeek!)
from bidict.
I suspect the use case is fairly common.
from bidict.
um. we're looking for what, some kind of recipe w/ bidict? would need a lot of specifics for that use case. SQLA lets you put a .key on a Column if you want to deal with some friendly python name, you never need to know the real col names at all, so a use case here is not apparent to me.
from bidict.
Thanks for the info @zzzeek! I don't have experience with SQLA so will let @Twangist respond but am interested in following along.
from bidict.
In my use case, I have a table T in a Postgres db which inherits from a base table containing common fields such as creator, updated-by, timestamps. It was convenient to have a class attribute holding the collection of fields unique to T (in fact, most but not all of those, even). In the db, the fieldnames use "kebob-case", and in a few places I need to translate those names to SQLA colnames and vice-versa. A bidict provides both the collection and the translation capability.
from bidict.
Well, I know I can check for issubclass(bidict, Mapping)
, thing is, I'm not the one who is checking.
In particular df.rename checks if columns
arg is instance of dict
and if not, it expect a function. If I pass bidict
, it fails with mybidict is not callable
.
Anyway, thank you for your time.
from bidict.
Aha. OK then maybe df.rename
can be improved. Note that Its doc is ambiguous: first it says that it expects a "function / dict", but then it says in the parameters section "index, columns : dict-like or function, optional". Evidently "dict-LIKE" is false. So there's either a df.rename
bug or a documentation bug -- consider reporting it. You're usng latest/greatest Pandas? (I assume)
For now, you could convert your bidict
to a dict
when calling df.rename
-- df.rename(dict(my_bidict))
or somesuch accommodation -- but would get onerous if you do it alot.
from bidict.
@pacholik @Twangist +1 on this being an issue on Pandas's side.
@zzzeek What did you think of @Twangist's reply to you 3 comments back?
from bidict.
@jab it sounds vague and very specific to some part of someone's application without any context, so far not much material for the stated goal of a "documentation tip".
from bidict.
@pacholik @Twangist Since it looks like this wasn't reported upstream yet, I just submitted pandas-dev/pandas#11461 in case you're interested in following there.
from bidict.
Also, I ended up asking about this on the python-dev list, and https://mail.python.org/pipermail/python-dev/2015-October/142058.html suggests that bidict's current approach is sound.
from bidict.
One other reason to not subclass dict is we would inherit the fromkeys
classmethod, which makes no sense for a bidict, and so would have to go out of our way to remove it.
from bidict.
Related Issues (20)
- Add more properties based tests HOT 1
- global name 'BidirectionalMapping' is not defined HOT 21
- github git tags out of sync since 0.18.4 HOT 3
- Missing __all__ in bidict/__init__.py leads to implicit reexport error with mypy in strict mode. HOT 9
- logo HOT 5
- Dependency Dashboard
- Type hints for Bidict type HOT 2
- Try slipcover HOT 1
- Maybe use nix for devcontainer and GHA “tests” workflow
- Why it returns None when I use bidict?
- Will you add a persistence method? HOT 1
- Improve OpenSSF Scorecard HOT 6
- Swap syntax does not work HOT 4
- Automate upgrading dev dependencies HOT 1
- Type alias definition for `MISSING` results in type violations HOT 1
- test fails with python3.12 HOT 3
- Remove dead batteries
- 0.22.1: temp directory in source tree trashes build HOT 6
- 0.22.1: sphinx warnings `reference target not found` HOT 6
- mypy type inference change in 0.23.0 HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bidict.