Comments (8)
1.7.0 will be released in a few minutes, with from_data
and to_data
.
from portion.
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.
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.
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
andto_string
functions so that you can convertInterval
instances from/to strings, and store them as strings in JSON. In that case, custom objects are explicitly supported through theconv
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.
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.
Yes that will be super great
from portion.
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.
Closed by #7
from portion.
Related Issues (20)
- Get number of times multiple intervals overlap HOT 3
- Iteration of empty intervals is inconsistent. in general "empty := (+inf,-inf)" is problematic HOT 15
- Is this library performant enough to work with (non-atomic) intervals which span integers between 1 and 1 billion? HOT 1
- Using an external comparator? HOT 3
- iterate is broken with subclasses of Interval HOT 12
- Add __format__ method to Interval (improvement) HOT 16
- Thoughts about text-annotation use case and Pandas Ext. API HOT 5
- interval diameter (length, width, measure, range, or size) HOT 8
- Add join / merge method HOT 2
- "compatible version" specifier in setup.py confuses poetry HOT 2
- AttributeError: module 'portion.interval' has no attribute 'empty' HOT 3
- IntervalMultiDict HOT 3
- Add example for pandas in README HOT 5
- importlib error with create_api HOT 2
- Error to import interval, inf, imath from interval HOT 1
- importlib.machinery error with create_api HOT 6
- Enclosure Calculation Bug HOT 1
- Empty Calculation Bug HOT 3
- The performance issues of interval calculations in large quantities. HOT 1
- mass/Lebesgue measure? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from portion.