通过Python查询treasury.io,返回Pandas数据框架
项目描述
pytreasuryio
======
从Python访问.. _treasury.io: http://treasury.io。
这是一个包含单个简单函数的包,用于从“python”向.. _treasury.io: http://treasury.io提交“SQL”查询。虽然您可以将函数从脚本复制到脚本,但这会使启动更快更简单!
它还提供了一些从treasury.io数据创建Twitter机器人的辅助工具。
安装
--------
使用pip安装。:
pip install treasuryio
示例
---------
基本查询
~~~~~~~~~
发送SQL查询并接收Pandas数据框架。:
2013年5月22日的运营现金余额
import treasuryio
sql = 'SELECT * FROM "t1" WHERE "date" = \'2013-05-22\';'
treasuryio.query(sql)
Twitter机器人
~~~~~~~~~
编写一个“~/.twitter.yml”文件。:
consumer_key: oeshaoduhsaousaoeuhts
consumer_secret: b233tsao-enuhsaoehsunoesudtuhoelaouhs2uo
access_token: 2349081293-astoehusatoehusaoeustahoeuhh2AOEUTAouhc
access_token_secret: 9023uonshesuaHONETuoeuoeouo0eOHNEuhOuoeu
定义一个产生推文文本的函数,并用
``@treasurio.tweet``装饰器。:
import treasuryio
import humanize
import math
MIL = 1e6
# 人类化数字/日期的辅助工具
def human_number(num)
return humanize.intword(int(math.ceil(num))).lower()
def human_date(date)
返回 humanize.naturalday(date).title()
@treasuryio.tweet
def total_debt_tweet()
df = treasuryio.query('SELECT date, close_today FROM t3c WHERE (item LIKE \'%subject to limit%\' AND year = 2013 AND month >=1) ORDER BY date DESC')
确定 DataFrame 的长度
end = len(df)-1
提取当前金额和年初金额
current_amt = df['close_today'][0]*MIL
previous_amt = df['close_today'][end]*MIL
计算变化
delta = abs(current_amt - previous_amt)
生成表示变化方向的单词
if current_amt > previous_amt
change = "increased"
elif current_amt < previous_amt
change = "decreased"
人性化值
注意包含的 ``human_date`` 和 ``human_number`` 函数,它们为您简化了这些值
current_date = human_date(df['date'][0])
amt = human_number(current_amt)
delta = human_number(delta)
previous_date = human_date(df['date'][end])
生成推文
vals = (current_date, amt, change, previous_date, 'http://treasury.io')
return "截至 %s,美国政府债务为 $%s。自 %s 以来,此金额已 %s - %s" % vals
然后只需运行它:
total_debt_tweet()
您可以通过切换使用的函数来变得复杂:
import treasuryio
import random
@treasurio.tweet
def tweet_a()
# ...
@treasurio.tweet
def tweet_b()
# ...
@treasurio.tweet
def tweet_c()
# ...
random.choice([tweet_a, tweet_b, tweet_c])()
======
从Python访问.. _treasury.io: http://treasury.io。
这是一个包含单个简单函数的包,用于从“python”向.. _treasury.io: http://treasury.io提交“SQL”查询。虽然您可以将函数从脚本复制到脚本,但这会使启动更快更简单!
它还提供了一些从treasury.io数据创建Twitter机器人的辅助工具。
安装
--------
使用pip安装。:
pip install treasuryio
示例
---------
基本查询
~~~~~~~~~
发送SQL查询并接收Pandas数据框架。:
2013年5月22日的运营现金余额
import treasuryio
sql = 'SELECT * FROM "t1" WHERE "date" = \'2013-05-22\';'
treasuryio.query(sql)
Twitter机器人
~~~~~~~~~
编写一个“~/.twitter.yml”文件。:
consumer_key: oeshaoduhsaousaoeuhts
consumer_secret: b233tsao-enuhsaoehsunoesudtuhoelaouhs2uo
access_token: 2349081293-astoehusatoehusaoeustahoeuhh2AOEUTAouhc
access_token_secret: 9023uonshesuaHONETuoeuoeouo0eOHNEuhOuoeu
定义一个产生推文文本的函数,并用
``@treasurio.tweet``装饰器。:
import treasuryio
import humanize
import math
MIL = 1e6
# 人类化数字/日期的辅助工具
def human_number(num)
return humanize.intword(int(math.ceil(num))).lower()
def human_date(date)
返回 humanize.naturalday(date).title()
@treasuryio.tweet
def total_debt_tweet()
df = treasuryio.query('SELECT date, close_today FROM t3c WHERE (item LIKE \'%subject to limit%\' AND year = 2013 AND month >=1) ORDER BY date DESC')
确定 DataFrame 的长度
end = len(df)-1
提取当前金额和年初金额
current_amt = df['close_today'][0]*MIL
previous_amt = df['close_today'][end]*MIL
计算变化
delta = abs(current_amt - previous_amt)
生成表示变化方向的单词
if current_amt > previous_amt
change = "increased"
elif current_amt < previous_amt
change = "decreased"
人性化值
注意包含的 ``human_date`` 和 ``human_number`` 函数,它们为您简化了这些值
current_date = human_date(df['date'][0])
amt = human_number(current_amt)
delta = human_number(delta)
previous_date = human_date(df['date'][end])
生成推文
vals = (current_date, amt, change, previous_date, 'http://treasury.io')
return "截至 %s,美国政府债务为 $%s。自 %s 以来,此金额已 %s - %s" % vals
然后只需运行它:
total_debt_tweet()
您可以通过切换使用的函数来变得复杂:
import treasuryio
import random
@treasurio.tweet
def tweet_a()
# ...
@treasurio.tweet
def tweet_b()
# ...
@treasurio.tweet
def tweet_c()
# ...
random.choice([tweet_a, tweet_b, tweet_c])()
项目详情
下载文件
下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。
源代码分发
treasuryio-0.0.4.tar.gz (4.4 kB 查看散列)
构建分发
treasuryio-0.0.4-py2-none-any.whl (7.0 kB 查看散列)