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

defaultdict and ordered defaultdict #2

Closed
chebee7i opened this issue Nov 10, 2014 · 7 comments
Closed

defaultdict and ordered defaultdict #2

chebee7i opened this issue Nov 10, 2014 · 7 comments

Comments

@chebee7i
Copy link

Could we also get a defaultdict and an ordered defaultdict? The latter would be especially helpful snce the typical recommendation is to subclass: http://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python

Might require a package rename?

@shoyer
Copy link
Owner

shoyer commented Nov 10, 2014

It does seem like a natural fit to add defaultdict in the same model as OrderedDict and rename the module to "cycollections". A pull request would be greatfully accepted!

I'm not so sure about ordered defaultdict, because that is not actually in the standard lib. But that subclass should still work -- if not, it is a bug.

On Mon, Nov 10, 2014 at 9:14 AM, chebee7i notifications@github.com
wrote:

Could we also get a defaultdict and an ordered defaultdict? The latter would be especially helpful snce the typical recommendation is to subclass: http://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python

Might require a package rename?

Reply to this email directly or view it on GitHub:
#2

@shoyer
Copy link
Owner

shoyer commented Nov 10, 2014

Actually, defaultdict in the Python standard library is already written in C. So there's no point in doing it in Cython.

@chebee7i
Copy link
Author

Right, so I guess only the ordered variant would benefit.

@shoyer
Copy link
Owner

shoyer commented Nov 10, 2014

@chebee7i Looks like github mangled my earlier emailed reply about ordered/default dict:

I'm not so sure about ordered defaultdict, because that is not actually in the standard lib. But that subclass should still work -- if not, it is a bug.

Please do give that subclass a try and let me know if for any reason it does not work.

@chebee7i
Copy link
Author

It works nicely. https://gist.github.com/chebee7i/49ebf6af69c329f8ffcb

In the gist, the DefaultOrderedDict derives from cyordereddict.OrderedDict, but the default functionality is still pure Python. Since defaultdict is implemented in C, I'm assuming there was some benefit over a pure Python implementation. So perhaps there might be some benefit to a C/Cython implementation of DefaultOrderedDict. But the gist (which would result in compiled code) is probably good for most purposes.

@shoyer
Copy link
Owner

shoyer commented Nov 10, 2014

If you're really concerned about speed, I would consider is making it an extension type instead of a normal class. I found that made a big difference for the speed of OrderedDict.

@shoyer
Copy link
Owner

shoyer commented Nov 13, 2014

I don't see a cython ordered defaultdict fitting in with this project, because it's not part of the standard library. But if you need it, I encourage you to go ahead and do it in your own project!

@shoyer shoyer closed this as completed Nov 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants