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>

Build Angular Google App Engine Cloud Endpoints Application Notes Python

1 Build angular folders

Use Yeoman to help build Angular layout folder structure.

Running yo angular and select what library is needed for the probject.

2 Create app.yaml in the root folder of the project.


application: application-name
version: 0-1
runtime: python27
api_version: 1
threadsafe: true

skip_files:
- ^(.*/)?.*node_modules/.*$

handlers:
# Endpoints handler
- url: /_ah/spi/.*
script: endpoints.app
secure: always
- url: /bower_components
static_dir: bower_components

- url: (.*)/
static_files: app\1/index.html
upload: app
login: required
secure: always

- url: (.*)
static_files: app\1
upload: app
login: required
secure: always

libraries:
- name: endpoints
version: "1.0"

Google App Engine: PDF reportlab Import Error ImportError: Cannot re-init internal module __main__

When using python library reportlab in appengine, I have met this import error.

 File "/Users/bmh/GitRepos/MyProject/charges/views.py", line 15, in <module>
    from MyProject.invoices import generate_invoice, format_long_number
  File "/Users/bmh/GitRepos/MyProject/charges/invoices.py", line 23, in <module>
    from reportlab.lib import colors
  File "/Users/bmh/GitRepos/MyProject/contrib/reportlab/lib/colors.py", line 44, in <module>
    from reportlab.lib.rl_accel import fp_str
  File "/Users/bmh/GitRepos/MyProject/contrib/reportlab/lib/rl_accel.py", line 19, in <module>
    import __main__
ImportError: Cannot re-init internal module __main__

This import error will show up if you are running a old version of reportlab in appengine.

https://bitbucket.org/rptlab/reportlab/issue/27/cannot-re-init-internal-module-__main__-on

This is the issue people have submitted about this import error and it’s already fixed.

See this change.

https://bitbucket.org/rptlab/reportlab/commits/ca6c60fd1f627a0f9c040b370ef52f9f4496d6f5

So in order to make everything works, just go to the bitbucket repository and download the latest code(Version 3.1.10 or above should be fine).

https://bitbucket.org/rptlab/reportlab/downloads

Leetcode: Database Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

This question seems don’t have too much limitation about the order of the result after trying different solutions.


SELECT d.Name AS Department, e1.Name AS Employee, Salary
FROM Employee e1, Department d
WHERE (SELECT COUNT(DISTINCT(e2.Salary)) 
       FROM Employee e2 
       WHERE e2.DepartmentId = e1.DepartmentId 
             AND e2.Salary > e1.Salary
      ) < 3 AND e1.DepartmentId = d.Id

How to access Endpoints from Google App Script?

It’s easy to access Google’ s Endpoints from Javascript, but it might not be so easy to do the same thing in App Script. As far as I know for now, it is.

I am hoping in the future Google could come up with something that makes App Script easy to talk to a Endpoint. (Here Endpoints mean Use App Engine to expose data through endpoints).

Before writing any code, you need to create or reuse a project a Google Developer Console.

https://console.developers.google.com/project

Then in the APIs & auth -> Credentials  create or reuse a Client ID for web application.

Then  you will have your client id and client secret.

There is another thing to keep in mind that we need to modify REDIRECT URIS later. We will get there.

var tokenPropertyName = 'OAUTH_TOKEN';
var scriptProperties = PropertiesService.getScriptProperties();
var AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth'; 
var TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'; 

//Need to be changed
var CLIENT_ID = 'client_id'; 
var CLIENT_SECRET= 'client_secret';
var REDIRECT_URL= ScriptApp.getService().getUrl();

function doGet(e) { 
  if (e.parameters.code){
      //if we get "code" as a parameter in, then this is a callback. we can make this more explicit 
     getAndStoreAccessToken(e.parameters.code);
     var script_token= getAccessToken();
     
     return HtmlService.createHtmlOutput("Good to go");
  }
  else{
     return HtmlService.createHtmlOutput("<html><h1>Lets start with oAuth</h1><a href='"+getURLForAuthorization()+"'>click here to start</a></html>");
  } 
}

function getAccessToken() { 
 return scriptProperties.getProperty(tokenPropertyName);
}
function getUrlFetchOptions() {
 return {
 "contentType" : "application/json",
 "headers" : {
 "Authorization" : "Bearer " + getAccessToken(),
 "Accept" : "application/json"
 }
 };
}

//Google requires POST
function getAndStoreAccessToken(code){
 var parameters = {
 method : 'post',
 payload : 'client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&grant_type=authorization_code&redirect_uri='+REDIRECT_URL+'&code=' + code
 };
 try {
 var response = UrlFetchApp.fetch(TOKEN_URL,parameters).getContentText(); 
 }
 catch (err) { Logger.log(err); }
 var tokenResponse = JSON.parse(response);
 //store the token for later retrival
 scriptProperties.setProperty(tokenPropertyName, tokenResponse.access_token);
}

function getURLForAuthorization(){
       return AUTHORIZE_URL + '?response_type=code&client_id='+CLIENT_ID+'&redirect_uri='+REDIRECT_URL + '&scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile&state=/profile'; 
}
function testEndpoints(){
 var getDataURL = 'url_to_call_on_endpoints'
 var dataResponse = UrlFetchApp.fetch(getDataURL,getUrlFetchOptions()).getContentText();
 return JSON.parse(dataResponse);
}

When you publish this web app, you will get a URL for the web app. The url might look like as below

https://script.google.com/a/macros/motorola.com/s/script_id/exec

Remember to put this URL into the REDIRECT URI in the Google Developer Console. Otherwise, you will get a mismatch for redirect url when you did the oauth step.

LeetCode: Database Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

The idea is to get the highest salary for each department. This needs a GROUP BY to achieve. So we end up with something like this {(1200,1), (3000,2)} which means for Department 1, highest Salary is 1200, for Department 2, highest Salary is 3000. We can only get so little information is because the limit of use of GROUP BY. You can only select Aggregate value or the column in the Group By.

Then we need to join Employee, Department and the result we got in the previous step so that we can select all employee that has the highest Salary in each department.

Working code:


SELECT Department.Name, Employee.Name, Employee.Salary
FROM Employee, Department,
(SELECT MAX(Salary) as maxs, Employee.DepartmentId as did
FROM Employee, Department
WHERE Employee.DepartmentId = Department.Id
GROUP BY Employee.DepartmentId) As cand
WHERE Employee.Salary = cand.maxs and Employee.DepartmentId = cand.did and Department.Id = Employee.DepartmentId;