我正在读取XML文件并将所需数据重组为Python数据结构(列表,元组等)
例如,我的一个XML解析器模块生成以下数据:
# data_miner.py
animals = ['Chicken', 'Sheep', 'Cattle', 'Horse']
population = [150, 200, 50, 30]
Run Code Online (Sandbox Code Playgroud)
然后我有一个绘图仪模块粗略地说,例如:
# plotter.py
from data_miner import animals, population
plot(animals, population)
Run Code Online (Sandbox Code Playgroud)
使用这种方法,我每次执行绘图时都必须解析XML文件.我还在测试我的程序的其他方面,XML文件现在不会经常更改.避免解析阶段会大大缩短我的测试时间.
这是我想要的结果:
在data_miner.py
和之间plotter.py
,我想要一个包含animals
并且population
可以通过plotter.py
本地访问它们的文件(例如,无需更改绘图代码),而无需data_miner.py
每次都运行.如果可能,它不应该是csv
ASCII格式或任何ASCII格式,只能是本机可访问的格式.plotter.py
现在看起来应该大致如下:
# plotter.py
# This line may not necessarily be a one-liner.
from data_file import animals, population
# But I want this portion to stay the same
plot(animals, population)
Run Code Online (Sandbox Code Playgroud)
类比:
这大致相当于MATLAB的save
命令,它将活动工作区的变量保存到.mat
文件中.我正在寻找类似于.mat …
我有以下字典:
history = {
"2008-11-17": 41,
"2010-05-28": 82,
"2008-11-14": 47,
"2008-11-13": 60,
"2008-11-12": 56,
"2008-11-11": 55,
"2008-11-10": 98,
"2008-11-19": 94,
"2008-11-18": 94,
"2004-05-27": 82,
"2004-05-26": 45,
"2004-05-25": 70,
# there's more ...
}
Run Code Online (Sandbox Code Playgroud)
如何定义生成器函数get_records(dict_history, str_from_date, str_to_date)
以生成date: record
条目?
我知道如何将datetime
对象转换为我想要的任何字符串格式.但是,我在这个障碍中的主要痛点是:
dict
s没有订购.dict
键是字符串.到目前为止,这是我能想到的:
from datetime import datetime, timedelta
def get_records(history, start_date, end_date):
fmt = "%Y-%m-%d"
dt = timedelta(days=1)
present_date = datetime.strptime(start_date, fmt)
end_date = datetime.strptime(end_date, fmt)
while present_date <= end_date:
present_string = present_date.strftime(fmt) …
Run Code Online (Sandbox Code Playgroud) 我从一个2x4矩阵A开始
import numpy as np
A = np.matrix([[1, 2, 3, 4], [5, 6, 7, 8]])
Run Code Online (Sandbox Code Playgroud)
我还有另一个1x4矩阵B.
B = np.matrix([9, 10, 11, 12])
Run Code Online (Sandbox Code Playgroud)
如何连接A和B,以便获得3x4矩阵C.
C = [[9 10 11 12]
[1 2 3 4]
[5 6 7 8]]
Run Code Online (Sandbox Code Playgroud)
注意,B在矩阵A的第0行之前被预先添加.
我总是按对键入配对的字符:
'' "" () [] {} <>
Run Code Online (Sandbox Code Playgroud)
当我在插入模式下键入Vim时,如何映射Vim会立即跳入其中?我尝试了一些谷歌搜索,但我似乎无法得到正确的搜索词.
我成功地为我的应用添加了Heroku自定义域名.我想知道如何捕获以开头的请求www
并将它们重定向到裸域.例如,我将以下自定义域映射到我的Heroku应用程序:
www.myapp.com
myapp.com
Run Code Online (Sandbox Code Playgroud)
请求http://myapp.com
和http://www.myapp.com
成功,但www
保持.
我希望将所有请求http://www.myapp.com
重定向到http://myapp.com
.这也适用于其他路径,例如http://www.myapp.com/some/foo/bar/path
重定向到http://myapp.com/some/foo/bar/path
.我想要这样的东西:http://www.stef.io,看看它www.
是如何离开地址栏的.
到目前为止,我在Google上找到的说明是关于编辑我的.htaccess
文件,但我正在使用Flask框架上的Python应用程序在Heroku上运行.
考虑以下Python logging
YAML配置文件片段:
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
logfile:
class: logging.handlers.TimedRotatingFileHandler
level: DEBUG
filename: some_fancy_import_name.generate_filename_called_error
backupCount: 5
formatter: simple
Run Code Online (Sandbox Code Playgroud)
我想以这种方式加载此YAML配置文件:
with open('logging.yaml', 'r') as fd:
config = yaml.safe_load(fd.read())
logging.config.dictConfig(config)
Run Code Online (Sandbox Code Playgroud)
采取的特别通知filename
到的handler
应该写日志。在普通的Python代码中,我希望some_fancy_import_name.generate_filename_called_errorlog
生成字符串'error.log'
。总而言之,我想说这个日志处理程序应该写入'error.log'
当前目录中的文件。
但是,事实证明并非如此。当我查看当前目录时,我看到一个名为'some_fancy_import_name.generate_filename_called_errorlog'
。
我想filename
以编程方式确定。我已成功尝试使用常规Python脚本通过以下方式配置日志记录:
# fancy import name
from os import environ as env
# Programmatically determine filename path
log_location = env.get('OPENSHIFT_LOG_DIR', '.')
log_filename = os.path.join(log_location, 'error')
handler = …
Run Code Online (Sandbox Code Playgroud) 我有以下脚本定义一个tzinfo
对象:
import time
from datetime import datetime, timedelta, tzinfo
class ManilaTime(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=8)
def tzname(self, dt):
return "Manila"
manila = ManilaTime()
Run Code Online (Sandbox Code Playgroud)
现在,我要说
t = datetime(tzinfo=manila, *time.gmtime()[:-3])
print t
Run Code Online (Sandbox Code Playgroud)
这给了我
2011-07-24 12:52:06+08:00
Run Code Online (Sandbox Code Playgroud)
问题:什么12:52:06+08:00
意思?我想学习如何根据标准读取包含UTC偏移的时间信息.请忽略我使用过的time.gmtime()
.假设我只有时间字符串本身.我该怎么看?
答:我需要执行添加以获得马尼拉时间.读完这篇文章后,我应该做一个计算,我会说
它
12:52:06
在格林威治,我应该抵消+08:00
.意思是,它20:52:06
在马尼拉.
B.我会从表面上看它并说出来
它
12:52:06
在马尼拉,它与UTC的偏差+08:00
.意思是,它04:52:06
在格林威治.
哪个是对的?A还是B?
我有一个http://myapp.com/get_data
返回的URL application/json
Content-Type
.当我浏览到该URL时,我会在浏览器窗口中获得纯文本JSON数组
[[key, value],
[key, value],
[key, value],
...]
Run Code Online (Sandbox Code Playgroud)
我还有一个JavaScript函数,期望数据采用JSON数组格式
function process_data() {
var data = // give me more data in JSON array format...
}
Run Code Online (Sandbox Code Playgroud)
如何让我的JavaScript浏览http://myapp.com/get_data
并将生成的JSON数组分配到data
变量里面process_data()
?
我是JavaScript新手(来自Python背景),如果你能推荐使用核心JavaScript库的解决方案,我将不胜感激.也欢迎使用其他库的解决方案,最好是那些被认为是最佳实践的解决方案.
看来我对我的问题并不清楚.让我举一个Python的例子.做完必要的进口后,我可以做点什么
url = "http://myapp.com/get_data"
page = urllib2.urlopen(url)
page_source = page.read()
Run Code Online (Sandbox Code Playgroud)
这一次,page_source
已经是str
我可以轻松玩的Python 对象,分配给其他变量等.如果我可以将Python和JavaScript混合在一起,对于这个问题的上下文,我想做类似的事情
function process_data() {
url = "http://myapp.com/get_data"
page = urllib2.urlopen(url)
page_source = page.read()
var data = convert_str_to_JSON(page_source)
}
Run Code Online (Sandbox Code Playgroud)
当然,这只是代码的一个丑陋的混乱,但我希望它传达了我想要的东西:
GET
是一个URL.我有一个服务器端Python脚本,它返回一个包含客户端JavaScript参数的JSON字符串.
# Python
import simplejson as json
def server_script()
params = {'formatting_function': 'foobarfun'}
return json.dumps(params)
Run Code Online (Sandbox Code Playgroud)
这foobarfun
应该引用JavaScript函数.这是我的主要客户端脚本
// JavaScript
function client_script() {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, async=true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
options = JSON.parse(xhr.responseText);
options.formatting_function();
}
};
xhr.send(null);
}
function foobarfun() {
//do_something_funny_here...
}
Run Code Online (Sandbox Code Playgroud)
当然,options.formatting_function()
会抱怨"字符串不可调用"或类似的东西.
在使用Chrome的Inspect Element后,在Resources选项卡下,并导航左侧边栏以进行XHR>查询,我发现client_script
解释options
如下.foobarfun
被视为一个字符串.
// JavaScript
options = {"formatting_function": "foobarfun"}
Run Code Online (Sandbox Code Playgroud)
我本来希望client_script
看到options
的
// …
Run Code Online (Sandbox Code Playgroud) 我在这里通过Mac OS X链接安装了Git http://git-scm.com/download
安装后,我在终端中尝试以下操作:
$ git help fetch
$ git help remote
$ man git
$ man git-fetch
Run Code Online (Sandbox Code Playgroud)
但是,我收到了消息No manual entry for git-<subcommand>
.如何安装Git的手册页?我遇到了与此处解释的相同的问题,但是kernel.org已经关闭,所以它没有多大帮助.