turicas / turicas/rows

Add FractionField

Open
#280 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rows.fields
Dominant language
Python
Stars
886
Forks
137
PR merge metrics
No merged PRs in 30d

Description

Sometimes it's useful to represent fractions, instead of using float or decimal.Decimal. Python has the fractions module in the stdlib (both Python 2 and 3) and we could use it.

A draft implementation (needs to check some corner cases, add tests):

from fractions import Fraction

class FractionField(rows.fields.Field):

    TYPE = (Fraction, )

    @classmethod
    def deserialize(cls, value):
        value = str(value or '').strip()
        if not value:
            return None

        try:
            return Fraction(value)
        except ValueError:
            raise

    @classmethod
    def serialize(cls, value):
        if value is None:
            return ''
        else:
            return str(value)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing rows.fields.Field implementation and the field tests, then compare their conventions with Python's fractions module and the draft FractionField shown here. Add coverage for normal values and corner cases, with completion shown by passing the field tests and correct Fraction serialization and deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.