Collections.Extensions.ToPyString
2.0.1
dotnet add package Collections.Extensions.ToPyString --version 2.0.1
NuGet\Install-Package Collections.Extensions.ToPyString -Version 2.0.1
<PackageReference Include="Collections.Extensions.ToPyString" Version="2.0.1" />
paket add Collections.Extensions.ToPyString --version 2.0.1
#r "nuget: Collections.Extensions.ToPyString, 2.0.1"
// Install Collections.Extensions.ToPyString as a Cake Addin #addin nuget:?package=Collections.Extensions.ToPyString&version=2.0.1 // Install Collections.Extensions.ToPyString as a Cake Tool #tool nuget:?package=Collections.Extensions.ToPyString&version=2.0.1
ToPyString
ToPyString is a .NET System.Collections extension for converting collections to a string in Python format.
The reason this small project exists is because it's a shame that C# doesn't have an in-built way of stringifying collections (like many other languages do). Although creating a ToString
method for your collections isn't difficult, you shouldn't be wasting time implementing rudimentary things on every project... especially on projects you're using to just quickly try something out and Console.WriteLine the output.
Summary
- Getting Started
- Prerequisites
- Installing
- Using ToPyString
- Runing the tests
- Break down of unit tests
- Contact
- Contributing
- Authors
- License
Getting Started
Follow these instructions to see how simple it is to use ToPyString
.
Prerequisites
This package supports every .NET version from the .NET Standard 2.0 and up!
You can find the .NET download page here.
Installing
You can get ToPyString
by installing the Collections.Extensions.ToPyString NuGet package:
Install-Package Collections.Extensions.ToPyString
Or via the NET command line interface:
dotnet add package Collections.Extensions.ToPyString
Using ToPyString
ToPyString
is an extension method that can be used on all .NET types.
- List:
var list = new List<object> { 11, "john", "doe" };
Console.WriteLine(list.ToPyString()); // Output: [11, 'john', 'doe']
-Self-containing List:
var list = new List<object> { 11 };
list.Add(list);
Console.WriteLine(list.ToPyString()); // Output: [11, [...]]
- Dictionary:
var dictionary = new Dictionary<object, object> { [1] = "key1", ["key2"] = 2, [new object()] = null };
Console.WriteLine(dictionary.ToPyString()); // Output: {1: 'key1', 'key2': 2, System.Object: null}
- Self-containing Dictionary:
var dictionary = new Dictionary<object, object> { [1] = "key1" };
dictionary.Add("self", dictionary);
Console.WriteLine(dictionary.ToPyString()); // Output: {1: 'key1', 'self': {...}}
- String and primitive types:
var str = "some string";
var intNum = 11;
var doubleNum = 1.012d;
Console.WriteLine(str.ToPyString()); // Output: some string
Console.WriteLine(intNum.ToPyString()); // Output: 11
Console.WriteLine(doubleNum.ToPyString()); // Output: 1.012
Be careful when using ToPyString with dynamic
type
Because of the way dynamic type is implemented the CLR will throw a RuntimeBinderException
if you try to call the ToPyString
extension method directly on a dynamic
object. To get around this issue simply use ToPyString
as a regular static method.
Wrong use with dynamic
:
dynamic dynObject = new { SomeField = 1 };
Console.WriteLine(dynObject.ToPyString()); // --> will throw a RuntimeBinderException
Correct use:
dynamic dynObject = new { SomeField = 1 };
Console.WriteLine(CollectionExtensions.ToPyString(dynObject)); // Output: { SomeField = 1 }
If you have a collection that contains a dynamic
object, you can use the ToPyString
as usual:
dynamic dynObject = new { SomeField = 1 };
var list = new List<object> { dynObject };
Console.WriteLine(list.ToPyString()); // Output: [{ SomeField = 1 }]
Running the tests
To run the tests using the command line use:
dotnet test
Alternatively, if you're using Visual Studio, you have a button that runs the tests for you, so you can also use that.
Break down of unit tests
The tests are mainly testing whether C# Collections types are converted to string in the expected Python format, and confirming that calling ToPyString
on non-collections
behaves the same way that calling ToString
would.
[Fact]
public void Prints_List_Of_Ints()
{
var list = new List<int> { 1, 2, -3, 100 };
var expectedResult = "[1, 2, -3, 100]";
var result = list.ToPyString();
Assert.Equal(expectedResult, result);
}
Contact
Have a question or an issue about ToPyString? Create an issue!
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to the project.
Authors
- Nenad - 0xNeshi
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Final thoughts
If you found this extension helpfull then please give it a star 🌟 and share it with others, help them so they also NOT waste time on stringifying collections.
Finally, if you have any suggestions on refactoring the codebase to make it simpler, more extensible, more elegant - please let me know, I LOVE refactoring!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.1 | 88 | 1/6/2025 |
2.0.0 | 86 | 1/5/2025 |
1.3.0 | 1,503 | 12/6/2020 |
1.2.0 | 1,357 | 6/21/2020 |
1.1.5.1 | 1,357 | 6/21/2020 |
1.1.5 | 1,333 | 6/21/2020 |
1.1.4 | 1,204 | 6/20/2020 |
1.1.3 | 1,330 | 6/20/2020 |
1.1.2 | 1,261 | 6/19/2020 |
1.1.1.1 | 1,248 | 6/19/2020 |
1.1.1 | 1,292 | 6/19/2020 |
1.1.0.3 | 1,258 | 6/18/2020 |
1.1.0.2 | 1,305 | 6/18/2020 |
1.1.0.1 | 1,314 | 6/18/2020 |
1.1.0 | 1,171 | 6/18/2020 |
1.0.4 | 1,256 | 6/17/2020 |
1.0.3.2 | 1,177 | 6/17/2020 |
1.0.2.2 | 1,299 | 6/17/2020 |
1.0.2.1 | 1,265 | 6/17/2020 |
1.0.2 | 1,281 | 6/17/2020 |
1.0.1 | 1,220 | 6/13/2020 |
1.0.0 | 1,187 | 6/13/2020 |