我最初使用App Engine网站上显示的示例在App Engine上实现了Jinja2:https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates jinja2直接导入的位置:
import jinja2
import os
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
greetings = 'somestring'
template_values = {
'greetings': greetings,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
Run Code Online (Sandbox Code Playgroud)
但我现在正在使用Simpleauth(https://github.com/crhym3/simpleauth),它遵循Nick Johnson在此描述的实现:http://blog.notdot.net/2011/11/Migrating-to-Python -2-7-part-2-Webapp-and-templates,其中jinja2是从webapp2_extras导入的:
import os
import webapp2
from webapp2_extras import jinja2
class BaseHandler(webapp2.RequestHandler):
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_template(self, filename, **template_args):
self.response.write(self.jinja2.render_template(filename, **template_args))
class IndexHandler(BaseHandler):
def get(self):
self.render_template('index.html', name=self.request.get('name'))
Run Code Online (Sandbox Code Playgroud)
以下哪一项是使用jinja2的首选方法?(他们似乎没有很好地合作,并且更愿意标准化最好的选择.)
我正在Mac OS X 10.8.1和Python 2.7.2上构建lxml并遇到问题.(我已经克隆了Github存储库,并按照Mac OS X的说明进行操作:http://lxml.de/build.html)
在构建libxml2时似乎有些东西悬而未决; 以下是终端输出的尾端:
configure: creating ./config.status
config.status: creating libxml2.spec
config.status: creating Makefile
config.status: creating include/Makefile
config.status: creating include/libxml/Makefile
config.status: creating doc/Makefile
config.status: creating doc/examples/Makefile
config.status: creating doc/devhelp/Makefile
config.status: creating example/Makefile
config.status: creating python/Makefile
config.status: creating python/tests/Makefile
config.status: creating xstc/Makefile
config.status: creating include/libxml/xmlversion.h
config.status: creating xml2-config
config.status: creating libxml-2.0.pc
config.status: creating libxml-2.0-uninstalled.pc
config.status: creating python/setup.py
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
rm: libtoolT: No such file …Run Code Online (Sandbox Code Playgroud) 我正在使用App Engine,SDK 1.6.3和Python 2.7.
我创建了这样的模型:
class MyModel(db.Model):
name = db.StringProperty()
website = db.StringProperty()
Run Code Online (Sandbox Code Playgroud)
我可以迭代并查看除Key id之外的所有内容.例如,在交互式shell中我可以运行:
from models import *
list = MyModel.all()
for p in list:
print(p.name)
Run Code Online (Sandbox Code Playgroud)
它会打印每个实体的名称.但当我运行这个:
from models import *
list = MyModel.all()
for p in list:
print(p.key.id) [or p.key.name or p.key.app]
Run Code Online (Sandbox Code Playgroud)
我得到一个AttributeError:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py", line 317, in post
exec(compiled_code, globals())
File "<string>", line 4, in <module>
AttributeError: 'function' object has no attribute 'id'
Run Code Online (Sandbox Code Playgroud)
谁能帮帮我吗??
我有一台MacBook Pro运行OS X 10.8,安装了Xcode 4.4.我最近安装了lxml,但是当我去导入它时,我得到了以下内容:
MacBook-Pro:~ jedc$ sudo pip install lxml==2.3.5
Password:
Downloading/unpacking lxml==2.3.5
Downloading lxml-2.3.5.tar.gz (3.2Mb): 3.2Mb downloaded
Running setup.py egg_info for package lxml
Building lxml version 2.3.5.
Building without Cython.
Using build configuration of libxslt 1.1.26
warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
Running setup.py install for lxml
Building lxml version 2.3.5.
Building without Cython.
Using build configuration of libxslt 1.1.26
building 'lxml.etree' extension
/usr/bin/cc -fno-strict-aliasing -O3 -w -pipe -DNDEBUG -g -fwrapv -O3 -Wall …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的Neo4j数据库中构建一个日期树,该数据库将与Nigel Small的py2neo库中的日历模块一起使用.
我从这里使用了Mark Needham的入门代码(http://java.dzone.com/articles/neo4j-cypher-creating-time),但是没有将所有Year节点连接到主日历节点,这是必需的对于py2neo库.(这里的文档:http://book.py2neo.org/en/latest/calendar/)
我修改了Mark的代码,尝试创建一个主节点来连接所有这些年,如下所示:
CREATE (x:Calendar {name:'master'})
WITH range(2005, 2014) AS years, range(1,12) as months
FOREACH(year IN years |
CREATE (y:Year {year: year})
MERGE (x)-[:YEAR]->(y)
FOREACH(month IN months |
CREATE (m:Month {month: month})
MERGE (y)-[:MONTH]->(m)
FOREACH(day IN (CASE
WHEN month IN [1,3,5,7,8,10,12] THEN range(1,31)
WHEN month = 2 THEN
CASE
WHEN year % 4 <> 0 THEN range(1,28)
WHEN year % 100 <> 0 THEN range(1,29)
WHEN year % 400 <> 0 THEN …Run Code Online (Sandbox Code Playgroud) 背景:我正在我的 Python/AppEngine 项目中获取数据并创建一个 .tsv 文件,以便我可以使用 d3.js 创建图表。现在我正在为每个页面加载编写 CSV;我想将文件存储在 Google Cloud Storage 中一次,然后从那里读取它。
每次加载页面时,我当前如何编写文件!:
def get(self): ## this gets called when loading myfile.tsv from d3.js
datalist = MyEntity.all()
self.response.headers['Content-Type'] = 'text/csv'
writer = csv.writer(self.response.out, delimiter='\t')
writer.writerow(['field1', 'field2'])
for eachco in datalist:
writer.writerow([eachco.variable1, eachco.variable2])
Run Code Online (Sandbox Code Playgroud)
虽然效率低下,但效果很好。
使用此 Google Cloud Storage 文档,我一直在尝试实现类似的功能:
def get(self):
filename = '/bucket/myfile.tsv'
datalist = MyEntity.all()
bucket_name = os.environ.get('BUCKET_NAME', app_identity.get_default_gcs_bucket_name())
write_retry_params = gcs.RetryParams(backoff_factor=1.1)
writer = csv.writer(self.response.out, delimiter='\t')
gcs_file = gcs.open(filename, 'w', content_type='text/csv', retry_params=write_retry_params)
gcs_file.write(writer.writerow(['field1', 'field2']))
for eachco in …Run Code Online (Sandbox Code Playgroud) 我似乎在Mac OS 10.6.7上破坏了我的Rails安装.这是状态:
MacBook-Pro$ which ruby
/usr/local/bin/ruby
MacBook-Pro$ which gem
/usr/local/bin/gem
MacBook-Pro$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]
MacBook-Pro$ gem -v
1.8.5
Run Code Online (Sandbox Code Playgroud)
我使用自制软件来管理这些包:
MacBook-Pro$ brew list
distribute libyaml ossp-uuid python sqlite
gdbm mongodb pip readline wget
git mysql postgresql ruby
Run Code Online (Sandbox Code Playgroud)
我的〜/ .profile有这个:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)
在这次考验开始之前,我曾经使用过
sudo gem install rails
Run Code Online (Sandbox Code Playgroud)
我刚刚再次运行它得到了这个:
MacBook-Pro$ sudo gem install rails
Password:
Successfully installed rails-3.0.9
1 gem installed
Installing ri documentation for rails-3.0.9...
file 'lib' not found
Installing RDoc documentation for rails-3.0.9...
file 'lib' …Run Code Online (Sandbox Code Playgroud)