To merge two lists l1
, l2
in python, one can use the following
p1
, p2
l1[p1]
to result
and increment p1
and p2
by 1l1[p1] < l2[p2]
, then add l1[p1]
to result
, then increment p1
l1[p1] > l2[p2]
, then add l2[p2]
to result
, then increment p2
result
Just a quick reminder for the syntax of the crontab for cronjobs:
# 1. Entry: Minute when the process will be started [0-60]
# 2. Entry: Hour when the process will be started [0-23]
# 3. Entry: Day of the month when the process will be started [1-28/29/30/31]
# 4. Entry: Month of the year when the process will be started [1-12]
# 5. Entry: Weekday when the process will be started [0-6] [0 is Sunday]
#
# all x min = */x
I wanted to notifiy the user, whenever I am doing an AJAX request in Angular2. To achieve this in an existing project, the goal was, to edit as little files as possible. The solution to this problem is very straightforward: The class Http
needs to be extended which allows own code to be used.
First, I needed to create the EventHttpService
with the following code:
import { Http } from '@angular/http';
export class EventHttpService extends Http{}
Http
class, providing the same functionality. Therefore I needed to override all the methods which should perform a different action. Below is an example for the get
method, with all the required imports. Of course, the whole class needs to be decorated with Injectable()
so that we can inject it via Dependency Injection.
import { Injectable } from '@angular/core';
import { Http, RequestOptions, RequestOptionsArgs, Response, ConnectionBackend } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class EventHttpService extends Http {
public get(url: string, options?: RequestOptionsArgs) : Observable<Response> {
// Own code before the request is performed
var response = super.get(url, options);
response.subscribe(null, error => {
// Own code on error
}, () => {
// Own code on success
});
return response;
}
}
app.module.ts
:
@NgModule({
imports: [],
declarations: [],
providers: [
HTTP_PROVIDERS,
provide(Http, {
useFactory: (xhrBackend: XHRBackend, requestOptions: Request) => new EventHttpService(xhrBackend, requestOptions),
deps: [XHRBackend, RequestOptions]})
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Http.get
is used, the EventHttpService is used automatically, executing my custom code. If you want to inject other services into Http
, this is also straightforward, just add these to the constructor of the EventHttpService
so that it looks as follows. In my case, I am using the awesome ng2-slim-loading-bar to show a loading bar, when the first AJAX request is performed.
public constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private loadingBar: SlimLoadingBarService) {
super(_backend, _defaultOptions);
}
event-http.service.ts
:
import { Injectable } from '@angular/core';
import { Http, RequestOptions, RequestOptionsArgs, Response, ConnectionBackend } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { SlimLoadingBarService } from 'ng2-slim-loading-bar';
@Injectable()
export class EventHttpService extends Http {
private currentRequests: number = 0;
public constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private loadingBar: SlimLoadingBarService) {
super(_backend, _defaultOptions);
}
public get(url: string, options?: RequestOptionsArgs) : Observable<Response> {
this.incrementRequestCount();
var response = super.get(url, options);
response.subscribe(null, error => {
this.decrementRequestCount();
}, () => {
this.decrementRequestCount();
});
return response;
}
private decrementRequestCount() {
if (--this.currentRequests == 0) {
this.loadingBar.complete();
}
}
private incrementRequestCount() {
if (this.currentRequests++ == 0) {
this.loadingBar.start();
}
}
}
A cheatsheet for vom can be found here. I find this quite useful to begin with this powerful editor. The most important keys, if you just want to edit a file are:
An interesting site which shows the Big O notation for all many commonly used datatypes is the Big O Cheatsheet.