def test_decimal(self):
        # Default behavior: Decimal is preserved
        obj = decimal.Decimal(0.5)
        as_json = jsonpickle.dumps(obj)
        clone = jsonpickle.loads(as_json)
        self.assertTrue(isinstance(clone, decimal.Decimal))
        self.assertEqual(obj, clone)

        # Custom behavior: we want to use simplejson's Decimal support.
        jsonpickle.set_encoder_options('simplejson',
                                       use_decimal=True, sort_keys=True)

        jsonpickle.set_decoder_options('simplejson',
                                       use_decimal=True)

        # use_decimal mode allows Decimal objects to pass-through to simplejson.
        # The end result is we get a simple '0.5' value as our json string.
        as_json = jsonpickle.dumps(obj, unpicklable=True, use_decimal=True)
        self.assertEqual(as_json, '0.5')
        # But when loading we get back a Decimal.
        clone = jsonpickle.loads(as_json)
        self.assertTrue(isinstance(clone, decimal.Decimal))

        # side-effect: floats become decimals too!
        obj = 0.5
        as_json = jsonpickle.dumps(obj)
        clone = jsonpickle.loads(as_json)
        self.assertTrue(isinstance(clone, decimal.Decimal))
Beispiel #2
0
        [random.choice(symbols) for i in range(random.randrange(maxlen))])


def rand_dig(maxlen):
    dig = string.digits
    return "".join(
        [random.choice(dig) for i in range(random.randrange(maxlen))])


testdata = [
    Contact(firstname=random_string("firstname", 10),
            middlename=random_string("middlename", 10),
            lastname=random_string("lastname", 10),
            nick=random_string("nick", 10),
            email1="*****@*****.**",
            address=random_string("address", 10),
            homephone=rand_dig(10),
            mobilephone=rand_dig(10),
            workphone=rand_dig(10),
            fax=rand_dig(10),
            month=2,
            day="10",
            year="1984") for i in range(5)
]

file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f)

with open(file, "w") as out:
    jsonpickle.set_decoder_options("json", indent=2)
    out.write(jsonpickle.encode(testdata))
Beispiel #3
0
import redis
from elasticsearch import Elasticsearch
from tweepy import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API
from tweepy import Cursor
from tweepy import TweepError

import warnings

import config
from config import elasticsearch_host, elasticsearch_user, elasticsearch_password, elasticsearch_port

warnings.filterwarnings("ignore")
jsonpickle.set_decoder_options('simplejson', encoding='utf8', cls=JSONDecoder)

# Variables that contains the user credentials to access Twitter API
# # keys from  "Twitter Tweet Summarization" app
CONSUMER_KEY = 'p9smvhLm9SXh9z2EUNYyakFe7'
CONSUMER_SECRET = 'Fu6f5YWPyEfrUwpGS02Ojm2eMSxanySYzvkhP7ypVLtlCvIvgy'
ACCESS_TOKEN = '809287937351249920-2S2imOpJVEy0oxDlGwVADh3wM2WPVpY'
ACCESS_TOKEN_SECRET = 'EkF2JQx9jcsBOyyMU4UsZYgFqLWJ1UH0W8NCDPL7Axnbs'
redis_conn = redis.Redis(host='localhost', port=6379, db=0)
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
DATA_FOLDER = './'
es = Elasticsearch(hosts=[{
    'host': elasticsearch_host,
    'port': elasticsearch_port
}],
Beispiel #4
0
    opts, args = getopt.getopt(sys.argv[1:], "n:f:", ["number of groups", "file"])
except getopt.GetoptError as err:
    getopt.usage()
    sys.exit(2)

n = 5
f = "data/groups.json"

for o, a in opts:
    if o == "-n":
        n = int(a)
    elif o == "-f":
        f = a


def random_string(prefix, maxlen):
    symbols = string.ascii_letters + string.digits + string.punctuation + " "*10
    return prefix + "".join([random.choice(symbols) for i in range(random.randrange(maxlen))])


testdata = [Group(name="", header="", footer="")] + [
    Group(name=random_string("name", 10), header=random_string("header", 20), footer=random_string("footer", 20))
    for i in range(n)
    ]

file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f)

with open(file, "w") as out:
    jsonpickle.set_decoder_options("json", indent=2)
    out.write(jsonpickle.encode(testdata))
Beispiel #5
0
 def decode(json: str) -> Any:
     jsonpickle.set_preferred_backend('simplejson')
     jsonpickle.set_decoder_options('simplejson', use_decimal=True)
     return jsonpickle.decode(json)
Beispiel #6
0
import jsonpickle

jsonpickle.set_encoder_options('simplejson',
                               sort_keys=True,
                               indent=4,
                               encoding='utf-8',
                               ensure_ascii=False,
                               unpicklable=False,
                               use_decimal=True)

jsonpickle.set_decoder_options('simplejson',
                               encoding='utf-8',
                               use_decimal=True)

# jsonpickle.set_preferred_backend('simplejson')
Beispiel #7
0
def get_response(response_obj):
    jsonpickle.set_decoder_options('simplejson', sort_keys=True, indent=4)
    res = jsonpickle.decode(response_obj.__dict__['_content'])
    if isinstance(res, json_response.Response):
        return res
    raise TypeError("Unexpected Response object type")
                it[i] = formatAll(it[i],formatDic,{},formatter)
    elif isinstance(it,str):
        if formatter is not None:
            it = formatter(it).evaluate(formatDic)
        else:
            it = it.format(**formatDic)
        
    return it
   
# register common extension and backend demjson
import jsonpickle.ext.numpy
import demjson
jsonBackend = "demjson"
jsonpickle.ext.numpy.register_handlers()
jsonpickle.load_backend(jsonBackend,"encode","decode",CE.MyValueError)
jsonpickle.set_preferred_backend(jsonBackend)
jsonpickle.set_decoder_options(jsonBackend,decode_float=float)

def jsonDump(obj,file,compact=False,*args,**kargs):
    global jsonBackend
    jsonpickle.set_encoder_options(jsonBackend,compactly=compact,*args,**kargs)
    file.write(jsonpickle.encode(obj))
    
def jsonParse(s):
    return jsonpickle.decode(s)
    
def jsonLoad(filePath):
    f = open(filePath)
    return jsonParse(f.read())
        
Beispiel #9
0
def loads(data, **kwargs):
    jsonpickle.set_decoder_options("json", **kwargs)
    return jsonpickle.decode(data, keys=False)
import configparser


import jsonpickle
import jsonpickle.ext.numpy
import demjson

jsonBackend = "demjson"
jsonpickle.ext.numpy.register_handlers()
jsonpickle.load_backend(jsonBackend,"encode","decode", ValueError)
jsonpickle.set_preferred_backend(jsonBackend)
jsonpickle.set_decoder_options(jsonBackend,decode_float=float)

def jsonDump(obj,file,compact=False,*args,**kargs):
    global jsonBackend
    jsonpickle.set_encoder_options(jsonBackend,compactly=compact,*args,**kargs)
    file.write(jsonpickle.encode(obj))
    
def jsonParse(s):
    return jsonpickle.decode(s)
    
def jsonLoad(filePath):
    f = open(filePath)
    return jsonParse(f.read())
    


def loadIniFile(filePath):
    config = configparser.ConfigParser()
    config.read(filePath)
    return config
Beispiel #11
0
from .datapoint import DataPoint
from .outputfactoryinterface import OutputFactoryInterface

import numpy as np
import jsonpickle

jsonpickle.set_encoder_options('simplejson', use_decimal=True)
jsonpickle.set_decoder_options('simplejson', use_decimal=True)


class NumpyFloatHandler(jsonpickle.handlers.BaseHandler):
    """
    Automatic conversion of numpy float to python floats
    Required for jsonpickle to work correctly
    """
    def flatten(self, obj, data):
        """
        Converts and rounds a Numpy.float* to Python float
        """
        return round(obj, 6)


class NumpyIntHandler(jsonpickle.handlers.BaseHandler):
    """
    Automatic conversion of numpy int to python int.
    Required for jsonpickle to work correctly
    """
    def flatten(self, obj, data):
        return int(obj)