#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Deron Meranda <http://deron.meranda.us/>"
__homepage__ = "http://deron.meranda.us/python/demjson/"
__date__ = "2015-12-22"
__version__ = "2.2.4"
__version_info__ = (2, 2, 4) # Will be converted into a namedtuple below
__credits__ = "Short of demjson"
# Set demjson version
try:
from collections import namedtuple as _namedtuple
__version_info__ = _namedtuple('version_info', ['major', 'minor', 'micro'])(*__version_info__)
except ImportError:
raise ImportError("demjson %s requires a Python 2.6 or later" % __version__)
version, version_info = __version__, __version_info__
# Determine Python version
_py_major, _py_minor = None, None
def _get_pyver():
global _py_major, _py_minor
import sys
vi = sys.version_info
try:
_py_major, _py_minor = vi.major, vi.minor
except AttributeError:
_py_major, _py_minor = vi[0], vi[1]
_get_pyver()
# ----------------------------------------------------------------------
# Useful global constants
content_type = 'application/json'
file_ext = 'json'
class _dummy_context_manager(object):
"""A context manager that does nothing on entry or exit."""
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
return False
_dummy_context_manager = _dummy_context_manager()
# ----------------------------------------------------------------------
# Decimal and float types.
#
# If a JSON number can not be stored in a Python float without loosing
# precision and the Python has the decimal type, then we will try to
# use decimal instead of float. To make this determination we need to
# know the limits of the float type, but Python doesn't have an easy
# way to tell what the largest floating-point number it supports. So,
# we detemine the precision and scale of the float type by testing it.
try:
# decimal module was introduced in Python 2.4
import decimal
except ImportError:
decimal = None
def determine_float_limits(number_type=float):
"""Determines the precision and range of the given float type.
The passed in 'number_type' argument should refer to the type of
floating-point number. It should either be the built-in 'float',
or decimal context or constructor; i.e., one of:
# 1. FLOAT TYPE
determine_float_limits( float )
# 2. DEFAULT DECIMAL CONTEXT
determine_float_limits( decimal.Decimal )
# 3. CUSTOM DECIMAL CONTEXT
ctx = decimal.Context( prec=75 )
determine_float_limits( ctx )
Returns a named tuple with components:
( significant_digits,
max_exponent,
min_exponent )
Where:
* significant_digits -- maximum number of *decimal* digits
that can be represented without any loss of precision.
This is conservative, so if there are 16 1/2 digits, it
will return 16, not 17.
* max_exponent -- The maximum exponent (power of 10) that can
be represented before an overflow (or rounding to
infinity) occurs.
* min_exponent -- The minimum exponent (negative power of 10)
that can be represented before either an underflow
(rounding to zero) or a subnormal result (loss of
precision) occurs. Note this is conservative, as
subnormal numbers are excluded.
"""
if decimal:
numeric_exceptions = (ValueError, decimal.Overflow, decimal.Underflow)
else:
numeric_exceptions = (ValueError,)
if decimal and number_type == decimal.Decimal:
number_type = decimal.DefaultContext
if decimal and isinstance(number_type, decimal.Context):
# Passed a decimal Context, extract the bound creator function.
create_num = number_type.create_decimal
decimal_ctx = decimal.localcontext(number_type)
is_zero_or_subnormal = lambda n: n.is_zero() or n.is_subnormal()
elif number_type == float:
create_num = number_type
decimal_ctx = _dummy_context_manager
is_zero_or_subnormal = lambda n: n == 0
else:
raise TypeError("Expected a float type, e.g., float or decimal context")
with decimal_ctx:
zero = create_num('0.0')
# Find signifianct digits by comparing floats of increasing
# number of digits, differing in the last digit only, until
# they numerically compare as being equal.
sigdigits = None
n = 0
while True:
n = n + 1
pfx = '0.' + '1' * n
a = create_num(pfx + '0')
for sfx in '123456789': # Check all possible last digits to
# avoid any partial-decimal.
b = create_num(pfx + sfx)
if (a + zero) == (b + zero):
sigdigits = n
break
if sigdigits:
break
# Find exponent limits. First find order of magnitude and
# then use a binary search to find the exact exponent.
base = '1.' + '1' * (sigdigits - 1)
base0 = '1.' + '1' * (sigdigits - 2)
minexp, maxexp = None, None
for expsign in ('+', '-'):
minv = 0;
maxv = 10
# First find order of magnitude of exponent limit
while True:
try:
s = base + 'e' + expsign + str(maxv)
s0 = base0 + 'e' + expsign + str(maxv)
f = create_num(s) + zero
f0 = create_num(s0) + zero
except numeric_exceptions:
f = None
if not f or not str(f)[0].isdigit() or is_zero_or_subnormal(f) or f == f0:
break
else:
minv = maxv
maxv = maxv * 10
# Now do a binary search to find exact limit
while True:
if minv + 1 == maxv:
if expsign == '+':
maxexp = minv
else:
minexp = minv
break
elif maxv < minv:
if expsign == '+':
maxexp = None
else:
minexp = None
break
m = (minv + maxv) // 2
try:
s = base + 'e' + expsign + str(m)
s0 = base0 + 'e' + expsign + str(m)
f = create_num(s) + zero
f0 = create_num(s0) + zero
except numeric_exceptions:
f = None
else:
if not f or not str(f)[0].isdigit():
f = None
elif is_zero_or_subnormal(f) or f == f0:
f = None
if not f:
# infinite
maxv = m
else:
minv = m
return _namedtuple('float_limits', ['significant_digits', 'max_exponent', 'min_exponent'])(sigdigits, maxexp,
-minexp)
float_sigdigits, float_maxexp, float_minexp = determine_float_limits(float)
# For backwards compatibility with older demjson versions:
def determine_float_precision():
v = determine_float_limits(float)
return (v.significant_digits, v.max_exponent)
# ----------------------------------------------------------------------
# The undefined value.
#
# ECMAScript has an undefined value (similar to yet distinct from null).
# Neither Python or strict JSON have support undefined, but to allow
# JavaScript behavior we must simulate it.
class _undefined_class(object):
"""Represents the ECMAScript 'undefined' value."""
__slots__ = []
def __repr__(self):
return self.__module__ + '.undefined'
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一款基于Python的股票金融数据处理工具源码,包含325个文件,涵盖244个Python脚本、54个Markdown文档、7个ReStructuredText文件、4个JavaScript脚本、3个YAML文件、2个文本文件、2个SVG文件、1个Git忽略文件、1个Dockerfile、1个LICENSE文件。该项目使用akshare库进行股票数据的抓取和分析,适用于金融数据分析与处理。
资源推荐
资源详情
资源评论
收起资源包目录
基于Python的akshare股票金融数据处理源码 (442个子文件)
Dockerfile 748B
Dockerfile-Jupyter 351B
.flake8 263B
.gitignore 227B
layout.html 1KB
AKShare_logo.jpg 1.55MB
covid.js 204KB
cninfo.js 203KB
outcrypto.js 139KB
jm.js 114KB
ths.js 39KB
ths.js 39KB
crypto.js 8KB
calendar.json 110KB
LICENSE 1KB
stock.md 854KB
macro.md 352KB
futures.md 215KB
index.md 172KB
fund_public.md 153KB
pandas-05.md 126KB
pandas-01.md 123KB
changelog.md 111KB
others.md 111KB
option.md 109KB
event.md 99KB
bond.md 93KB
tutorial.md 68KB
fund_private.md 39KB
pandas-04.md 38KB
interest_rate.md 28KB
pandas-02.md 27KB
commodity.md 25KB
broker.md 23KB
fx.md 19KB
installation.md 16KB
energy.md 15KB
index_data.md 14KB
fundamental.md 13KB
dc.md 13KB
demo.md 12KB
article.md 12KB
currency.md 12KB
pandas-03.md 10KB
pandas-00.md 8KB
anaconda-01.md 8KB
anaconda-00.md 7KB
fund.md 6KB
introduction.md 6KB
special.md 5KB
contributor.md 5KB
articles.md 4KB
answer.md 4KB
spot.md 4KB
anaconda.md 3KB
CODE_OF_CONDUCT.md 3KB
nlp.md 3KB
akdocker.md 3KB
tools.md 3KB
bank.md 2KB
dependency.md 2KB
hf.md 2KB
tool.md 1KB
platform.md 955B
bug_report.md 834B
deploy_http.md 761B
feature_request.md 595B
akshare-----.md 457B
CONTRIBUTING.md 154B
custom.md 126B
ds.png 116KB
demjson.py 236KB
macro_china.py 180KB
__init__.py 156KB
macro_usa.py 121KB
stock_hist_em.py 66KB
stock_hsgt_em.py 64KB
index_baidu.py 53KB
cot.py 50KB
stock_fund_em.py 42KB
macro_euro.py 40KB
fund_em.py 40KB
covid.py 37KB
stock_gdfx_em.py 36KB
option_finance_sina.py 36KB
cons.py 35KB
stock_lhb_em.py 32KB
macro_bank.py 31KB
fund_amac.py 30KB
stock_technology_ths.py 29KB
stock_finance.py 27KB
futures_daily_bar.py 26KB
futures_zh_sina.py 24KB
stock_board_industry_ths.py 23KB
bond_zh_cov_sina.py 23KB
request.py 22KB
stock_summary.py 22KB
receipt.py 21KB
bond_issue_cninfo.py 21KB
stock_dzjy_em.py 20KB
共 442 条
- 1
- 2
- 3
- 4
- 5
资源评论
lly202406
- 粉丝: 3036
- 资源: 5531
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功