2015年12月9日水曜日

GAEp クックパッド (2018/2/23)

いま流行の Flask で。
Windows で Flask を使うおまじない。appengine_config.py
# coding: utf-8

# https://issuetracker.google.com/issues/72043776#comment7
import os
import sys
on_appengine = os.environ.get('SERVER_SOFTWARE','').startswith('Development')

if on_appengine and os.name == 'nt':
  sys.platform = "Not Windows"

from google.appengine.ext import vendor

# Add any libraries installed in the "lib" folder.
vendor.add('lib') 

エラー発生時に、コンソール画面へスタックトレースを表示したい
# https://stackoverflow.com/a/46914462
app = Flask(__name__)

import os
production_environment = os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')

if not production_environment:
  app.debug = True 
空のレスポンス
    # https://stackoverflow.com/a/24295616
    return ('', 201)

Key のパースエラーを包み隠す
  try:
    key = ndb.Key(urlsafe = request.args.get('k'))
  except Exception, e:
    # https://github.com/googlecloudplatform/datastore-ndb-python/issues/143#issuecomment-110869342
    if e.__class__.__name__ == 'ProtocolBufferDecodeError':
      return ('', 404)
    raise

Content-Type を設定
  resp = Response("\n".join(lines))
  resp.headers["content-type"] = 'text/plain'
  return resp

Jinja2 で三項演算子。loop.first ? 'true' : 'false'参考
{{ 'true' if loop.first else 'false' }}
?? 演算子も。
{{ value or 'fallback' }}

JSON を返す。参考
from flask import jsonify

return jsonify(root)
自分の URL 参考
from flask import request

request.base_url

a
a
a

2015年12月4日金曜日

Cygwin gcj 4.9.3 で NoClassDefFoundError GCCMain エラー

gcj エラー出力

/usr/bin/gcj -O2 --encoding=UTF-8 --classpath="/usr/share/java/libgcj-4.9.3.jar:/home/ku_000/pdftk-2.02-dist/java:." -C pdftk/com/lowagie/text/ElementTags.java
Exception in thread "main" java.lang.NoClassDefFoundError: org.eclipse.jdt.internal.compiler.batch.GCCMain
   at gnu.java.lang.MainThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jdt.internal.compiler.batch.GCCMain not found in gnu.gcj.runtime.SystemClassLoader{urls=[], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
   at java.net.URLClassLoader.findClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at gnu.java.lang.MainThread.run(Unknown Source)
Makefile:44: recipe for target 'pdftk/com/lowagie/text/ElementTags.class' failed
make[1]: [pdftk/com/lowagie/text/ElementTags.class] Error 1 (ignored)

こういう事らしいです…

Specifically, it says to download

  ftp://sourceware.org/pub/java/ecj-latest.jar

and rename the file to

  /usr/share/java/ecj.jar.

https://cygwin.com/ml/cygwin/2012-04/msg00103.html