Skip to content

Commit

Permalink
Adding new attributes:
Browse files Browse the repository at this point in the history
* UUID
* Integer
* Float
* Decimal
  • Loading branch information
gabrielfalcao committed Aug 31, 2015
1 parent 0be197f commit dd5ae81
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions repocket/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from __future__ import unicode_literals
import json
import importlib

import dateutil.parser

from uuid import UUID as PythonsUUID
from datetime import datetime
from uuid import UUID
from repocket._cache import MODELS

from repocket.connections import configure
from decimal import Decimal as PythonsDecimal

from repocket._cache import MODELS


class Attribute(object):
Expand All @@ -20,8 +22,9 @@ class Attribute(object):
__base_type__ = bytes
__empty_value__ = b''

def __init__(self, null=False):
def __init__(self, null=False, default=None):
self.can_be_null = null
self.default = default

def to_string(self, value):
"""Utility method that knows how to safely convert the value into a string"""
Expand Down Expand Up @@ -80,16 +83,22 @@ class AutoUUID(Attribute):
"""Automatically assigns a uuid1 as the value.
``__base_type__ = uuid.UUID``
"""
__base_type__ = UUID
__base_type__ = PythonsUUID

@classmethod
def cast(cls, value):
if isinstance(value, UUID):
if isinstance(value, PythonsUUID):
return value

return super(AutoUUID, cls).cast(value)


class UUID(AutoUUID):
"""Holds a valid ``uuid.UUID`` value
``__base_type__ = uuid.UUID``
"""


class Unicode(Attribute):
"""Handles unicode-safe values
``__base_type__ = unicode``
Expand All @@ -106,6 +115,30 @@ class Bytes(Attribute):
__empty_value__ = b''


class Integer(Attribute):
"""Handles int
``__base_type__ = int``
"""
__base_type__ = int
__empty_value__ = 0


class Float(Attribute):
"""Handles float
``__base_type__ = float``
"""
__base_type__ = float
__empty_value__ = 0.0


class Decimal(Attribute):
"""Handles Decimal
``__base_type__ = Decimal``
"""
__base_type__ = PythonsDecimal
__empty_value__ = PythonsDecimal('0')


class JSON(Unicode):
"""This special attribute automatically stores python data as JSON
string inside of redis. ANd automatically deserializes it when
Expand Down

0 comments on commit dd5ae81

Please sign in to comment.