-
Notifications
You must be signed in to change notification settings - Fork 752
implement list codec #1084
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
Merged
Merged
implement list codec #1084
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b500aa1
begin to implement list codec
koubaa f80ae87
add a test
koubaa 765b6a2
improve CanDecode
koubaa 98ce49c
improve list codec
koubaa b827f5a
full set of list codecs
koubaa 224df0e
abandon rank and use three codecs
koubaa 1600fdc
update
koubaa c43446e
respond to PR comments
koubaa 32fa32d
make decoders public
koubaa dfc814b
Make reset public
koubaa f025cd1
add codec tests
koubaa 24d78c3
respond to comments
koubaa ebcfa30
fix brace
koubaa 5cc575d
use unix line endings to avoid large diff
koubaa 17c47a7
fix compiler error
koubaa e8cc3d8
don't rethrow exception
koubaa 080dbc3
cleanup
koubaa 059ab08
fixes
koubaa 432c09e
clean up sequence wrapper
koubaa 3043201
respond to comments
koubaa 57d7779
fix potential double free
koubaa 7fb5915
fix exception
koubaa 085c665
fix all exceptions
koubaa bf2d038
respond to PR feedback
koubaa 07ee9fb
use hasattr("__iter__")
koubaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Python.Runtime.Codecs | ||
{ | ||
public class IterableDecoder : IPyObjectDecoder | ||
{ | ||
internal static bool IsIterable(Type targetType) | ||
{ | ||
//if it is a plain IEnumerable, we can decode it using sequence protocol. | ||
if (targetType == typeof(System.Collections.IEnumerable)) | ||
return true; | ||
|
||
if (!targetType.IsGenericType) | ||
return false; | ||
|
||
return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>); | ||
} | ||
|
||
internal static bool IsIterable(PyObject objectType) | ||
{ | ||
return objectType.HasAttr("__iter__"); | ||
} | ||
|
||
public bool CanDecode(PyObject objectType, Type targetType) | ||
{ | ||
return IsIterable(objectType) && IsIterable(targetType); | ||
} | ||
|
||
public bool TryDecode<T>(PyObject pyObj, out T value) | ||
{ | ||
//first see if T is a plan IEnumerable | ||
if (typeof(T) == typeof(System.Collections.IEnumerable)) | ||
{ | ||
object enumerable = new CollectionWrappers.IterableWrapper<object>(pyObj); | ||
value = (T)enumerable; | ||
return true; | ||
} | ||
|
||
var elementType = typeof(T).GetGenericArguments()[0]; | ||
var collectionType = typeof(CollectionWrappers.IterableWrapper<>).MakeGenericType(elementType); | ||
|
||
var instance = Activator.CreateInstance(collectionType, new[] { pyObj }); | ||
value = (T)instance; | ||
return true; | ||
} | ||
|
||
public static IterableDecoder Instance { get; } = new IterableDecoder(); | ||
|
||
public static void Register() | ||
{ | ||
PyObjectConversions.RegisterDecoder(Instance); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.