Python Google App Engine: ImportError: cannot import name check_call

I was getting this error recently with Python Google App Engine.

  File "/home/txzhang/projects/foo/lib/dateutil/tz.py", line 968, in gettz
    from dateutil.zoneinfo import gettz
  File "/home/txzhang/projects/foo/lib/dateutil/zoneinfo/__init__.py", line 7, in <module>
    from subprocess import check_call
ImportError: cannot import name check_call

Reason:

You probably will see this if you are using dateutil lib for python.

https://pypi.python.org/pypi/python-dateutil

More specifically, if you are using the latest version. which is 2.4.2 as of now.

It’s kind of strange why you cannot import from subprocess which seems to be a standard module of python 2. 7.

According to Google’s official doc for Python Runtime Environment on App Engine,

https://cloud.google.com/appengine/docs/python/?csw=1#Pure_Python

“Some modules have been disabled because their core functions are not supported by App Engine, such as networking or writing to the filesystem…”

Here is the list of supported modules

https://cloud.google.com/appengine/kb/libraries

there is no subprocess,

so subprocess is not supported by Google App Engine Python environment, that’s why you cannot import it.

Solution:

If you need to use dateutil, I would suggest you use the 1.5 version of dateutil instead of 2.4.2. Because the 1.5 version doesn’t need subprocess module. I guess version later than 1.5 might still work as far as it doesn’t need subprocess which is not supported by Google App Engine.

Python: PdfFileReader PdfReadError: EOF marker not found

I had this error when I was trying to read the content of a PDF file using python lib pyPDF.

PdfFileReader(StringIO.StringIO(result.content))

it turns out to be that the PDF file I was trying to read is not a standard PDF file.

Standard PDF ends with “%%EOF” marker within the last 1024 bytes of the file.

But in order to see the content of a PDF, it does not have to be a standard one. So for this “broken” PDF, I could still view it correctly using most PDF reader but throws this EOF marker not found exception when I am using pyPDF library.

The solution it to change the pyPDF/pdf.py files:

def read(self, stream):
    # start at the end:
    stream.seek(-1, 2)
    line = ''
    while not line:
        line = self.readNextEndLine(stream)
    if line[:5] != "%%EOF":
        raise utils.PdfReadError, "EOF marker not found"

To:


def read(self, stream):
    # start at the end:
    stream.seek(-1, 2)
    line = ''
    while line[:5] != "%%EOF":
        line = self.readNextEndLine(stream)

Javascript Base64 Encoding Decoding

I am having this painful thing because I was using Google’s GMail API.

I was trying to retrieve the body of the Email which turns out to be Base64 data.

The very first approach I could find is to use atob() function.

This works for some of the cases.

But not all of them.

When I decode using this atob() function, I sometimes see this error.

Uncaught InvalidCharacterError: Failed to execute ‘atob’ on ‘Window': The string to be decoded is not correctly encoded.

Test data is:

PHA-Tm8gcHJvYmxlbSBhdCBhbGwsIEkgaHVnZWx5IGFwcHJlY2lhdGUgeW91ciBxdWljayByZXNwb25zZSwgZm9yIG1ha2luZyBhbiBhd2Vzb21lIHBsdWdpbiBmb3IgdXMgYWxsIHRvIGVuam95LCBhbmQgZm9yIGNvbnRpbnVpbmcgdG8gcHJvdmlkZSBzdXBwb3J0IDwvcD4NCg0KPHAgc3R5bGU9ImZvbnQtc2l6ZTpzbWFsbDstd2Via2l0LXRleHQtc2l6ZS1hZGp1c3Q6bm9uZTtjb2xvcjojNjY2OyI-Jm1kYXNoOzxicj5SZXBseSB0byB0aGlzIGVtYWlsIGRpcmVjdGx5IG9yIDxhIGhyZWY9Imh0dHBzOi8vZ2l0aHViLmNvbS9tYmVuZm9yZC9uZ1RhZ3NJbnB1dC9pc3N1ZXMvNDA2I2lzc3VlY29tbWVudC04NzI1MzgyMCI-dmlldyBpdCBvbiBHaXRIdWI8L2E-LjxpbWcgYWx0PSIiIGhlaWdodD0iMSIgc3JjPSJodHRwczovL2dpdGh1Yi5jb20vbm90aWZpY2F0aW9ucy9iZWFjb24vQUQ1N1U0eERaY3pvXzNITnZBYnpOMm9udmlBZ3lWdFJrczVuNXNjb2dhSnBaTTREeGs3Ty5naWYiIHdpZHRoPSIxIiAvPjwvcD4NCjxkaXYgaXRlbXNjb3BlIGl0ZW10eXBlPSJodHRwOi8vc2NoZW1hLm9yZy9FbWFpbE1lc3NhZ2UiPg0KICA8ZGl2IGl0ZW1wcm9wPSJhY3Rpb24iIGl0ZW1zY29wZSBpdGVtdHlwZT0iaHR0cDovL3NjaGVtYS5vcmcvVmlld0FjdGlvbiI-DQogICAgPGxpbmsgaXRlbXByb3A9InVybCIgaHJlZj0iaHR0cHM6Ly9naXRodWIuY29tL21iZW5mb3JkL25nVGFnc0lucHV0L2lzc3Vlcy80MDYjaXNzdWVjb21tZW50LTg3MjUzODIwIj48L2xpbms-DQogICAgPG1ldGEgaXRlbXByb3A9Im5hbWUiIGNvbnRlbnQ9IlZpZXcgSXNzdWUiPjwvbWV0YT4NCiAgPC9kaXY-DQogIDxtZXRhIGl0ZW1wcm9wPSJkZXNjcmlwdGlvbiIgY29udGVudD0iVmlldyB0aGlzIElzc3VlIG9uIEdpdEh1YiI-PC9tZXRhPg0KPC9kaXY-DQo=

You can try this.  I do know this is valid base64 data because I could decode this correctly using python.

import base64

base64.urlsafe_b64decode(data)

After searching quite a lot, I found this which works great for me.

https://github.com/dankogai/js-base64

I also heard that in Google Closure Library, goog.crypt.base64 is also a great one for encoding and decoding stuff. Cause I haven’t found out how to quick use this in my js, so I didn’t try this out.

Error: wiredep:app running grunt serve after yoeman generates angular project

yoeman is a scaffolding tool helpful for developers.


$ yo angular

creates an angular folder for you.

But it surprises me that after doing this, when I run


$ grunt serve

To start the localhost server, it gives me back error.

Running “wiredep:app” (wiredep) task
Warning: ENOENT, no such file or directory ‘/Users/txzhang/git/mm-motohire/app/bower.json’ Use –force to continue.

Aborted due to warnings.

To fix this, change the file Gruntfile.js in the root folder of your project.


wiredep: {
options: {
cwd: '<%= yeoman.app %>'
},
app: {
src: ['<%= yeoman.app %>/index.html'],
ignorePath: /\.\.\//
}
},

remove the line cwd:'<%= yeoman.app %>.

Then you would be good to go.

How to use custom font in web HTML

Sometimes you might want to use your own custom font for your web application.

Here is the quick way to do this.

Important: Make sure the file font-file.ttf stays in the same folder where the css file stays. I haven’t tried with relative position of the font-file in argument for url(). Maybe it would work.

In css file, you define the font-familiy


@font-face { font-family: font-name; src: url('font-file.ttf'); } 

#element_id{

     font-familiy: font-name;      

}

So then in your html, you could see your words in custom font


<p id="element_id">The words to show</p>