Giter Site home page Giter Site logo

Comments (6)

siegfriedpammer avatar siegfriedpammer commented on May 20, 2024 2

This was not a question, but rather a feature request. Also, why is the idea misguided? If the idea of automatic conversions of built-in types is so wrong, why do they exist for strings, integers, floats and all other built-in types EXCEPT decimal? Also note that the range of the type should not be a problem, because Python can use arbitrarily sized numeric types. The conversion from Python to .NET could fail with an OverflowException IMHO.

In my case I cannot even circumvent the problem by using the Equals method. x.Decimal.Equals(42) would work, because System.Decimal stays System.Decimal in Python. However, x.Int.Equals(42) does not work, because System.Int32 is marshalled to Python int and therefore has no Equals method. This causes interesting surprises when working with boxed primitive types. x.Boxed.Equals works with decimal, but fails with int.

Would be nice if guidance could be provided somehow. Thanks!

If you don't want to answer the questions here, I will be glad to open a discussion in a few days.

from pythonnet.

lostmsu avatar lostmsu commented on May 20, 2024

Generally misguided idea. You should use https://docs.python.org/3/library/decimal.html

Also not a bug. For questions please use discussions.

from pythonnet.

lostmsu avatar lostmsu commented on May 20, 2024

The particular idea is misguided because System.Decimal does not map neither to int - because Decimal has fractions, nor to float - because floats can not precisely represent decimals, and precise calculations are the whole point of decimals.

You need a compelling use case. Your last example is not very compelling either because x.Int == 42 is more idiomatic in Python, and if you want C# semantics you can use System.Object.Equals(a, b).

from pythonnet.

siegfriedpammer avatar siegfriedpammer commented on May 20, 2024

Thanks I already implemented an encoder that maps System.Decimal to decimal.Decimal, which solved most of the issues. However, I would welcome a change that adds a value parameter to the CanEncode/CanDecode methods, so the callee has all information available.

from pythonnet.

lostmsu avatar lostmsu commented on May 20, 2024

What scenario do you need this for? CanEncode/CanDecode are only there to quickly filter out types the codec does not support. Without them Python.NET would have to go through all codecs for every single attempt to convert anything, which would kill performance.

from pythonnet.

siegfriedpammer avatar siegfriedpammer commented on May 20, 2024

Sorry for taking so long to reply. I have missed the notification.

A value parameter would enable the following:

public bool CanEncode(Type type, object value)
{
    return type == typeof(decimal)
        || (type == typeof(object) && Type.GetTypeCode(value?.GetType()) == TypeCode.Decimal);
}

Then it would be possible to handle boxed decimals without calling TryEncode for all objects.

from pythonnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.