Code Monkey home page Code Monkey logo

Comments (8)

AlexandreDecan avatar AlexandreDecan commented on May 26, 2024 2

1.7.0 will be released in a few minutes, with from_data and to_data.

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 26, 2024 1

Hi!

I will have a look at what is required to support json serialization. That shouldn't be difficult to implement, but maybe there's a more generic way to support different (other) kinds of serialization.

I'll keep you in touch.

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 26, 2024 1

The problem is to encode/decode arbitrary objects that could be used as bounds. Integers are supported out of the box by json, but not, e.g., Timestamp.

Notice that your example is already "supported" with a bit of Python:

>>> i = I.closed(3, 4) | I.closed(7, 9)
>>> [(x.left, x.lower, x.upper, x.right) for x in i]
[(True, 3, 4, True), (True, 7, 9, True)]

>>> v = [(True, 3, 4, True), (True, 7, 9, True)]
>>> I.Interval(*[I.AtomicInterval(*x) for x in v])
[3,4] | [7,9]

I can add functions that do this job. They should also accept a parameter to specify how the bounds (arbitrary objects) can be converted from/to built-in data types. For example:

>>> i = I.closed(Timestamp(3), Timestamp(4)) | I.closed(Timestamp(7), Timestamp(9))
>>> I.to_data(i, conv=lambda t: t.as_int())
[(True, 3, 4, True), (True, 7, 9, True)]

>>> v = [(True, 3, 4, True), (True, 7, 9, True)]
>>> I.from_data(v, conv=Timestamp)
>>> [Timestamp(3),Timestamp(4)] | [Timestamp(7),Timestamp(9)]

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 26, 2024

It seems there is no "hook" that can be written on Interval or AtomicInterval to seamlessly support (de)serialization. However, I already see two workarounds:

  • The first one is to implement new methods/functions that convert Interval instances from/to a Python built-in data type (e.g. tuples). The question then is: how to convert the bounds of an atomic interval, as they can be any custom objects;
  • The second one, which is already available, is to rely on the from_string and to_string functions so that you can convert Interval instances from/to strings, and store them as strings in JSON. In that case, custom objects are explicitly supported through the conv attribute. However, this approach could be less straightforward for custom types (but very convenient, e.g., for integers).

Could you give a try to the latter solution?

from portion.

gfitas avatar gfitas commented on May 26, 2024

In my case, I use your library in an python Api that communicate in json. So if I use your last solution, I would have to parse the string intervals from JavaScript. Maybe you could generate array of custom object like this :
[{lowerV: 2, lowerT:'close',upperV: 4, upperT:'open'},
{lowerV: 5, lowerT:'open',upperV: 6, upperT:'close'},...]

Easy to read, easy to parse, easy to apply from any languages

from portion.

gfitas avatar gfitas commented on May 26, 2024

Yes that will be super great

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 26, 2024

I opened a PR (#7). I still need to find why doctest is failing in Python 2.7.
I will probably bundle another new feature with release 1.7.0, I hope to find some time to work on it today. If so, you can expect 1.7.0 to be released by the end of the day.

from portion.

AlexandreDecan avatar AlexandreDecan commented on May 26, 2024

Closed by #7

from portion.

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.