さくらレンタルサーバーでのgit管理

バージョン管理したいフォルダに移動

% git init
% git status
$ git add .  (全てのファイルをインデックスへ追加)
$ git commit -m "2016/12/11 commit"  (コメントは任意)

リモートリポジトリの作成

% mkdir git
% cd git
% ls
% mkdir remote.git
% git init --bare

リモートリポジトリへpush

% git remote add origin ssh://hoge@siriuz.sakura.ne.jp/home/hoge/git/remote.git
% git remote -v
origin  ssh://hoge@siriuz.sakura.ne.jp/home/hoge/git/remote.git (fetch)
origin  ssh://hoge@siriuz.sakura.ne.jp/home/hoge/git/remote.git (push)
% git push origin master:master

ログ

% git log

git diff

% git diff

commit logの閲覧終了:q
git checkout

hidden file you can find
ls -a

git diff
git diff –staged
git diff commit1 commit2

git branch
-git branch
-git branch easy-mode

[vagrant@localhost reflections]$ git branch
* master
[vagrant@localhost reflections]$ git branch easy-mode
[vagrant@localhost reflections]$ git branch
  easy-mode
* master

checkout

[vagrant@localhost reflections]$ git checkout easy-mode
Switched to branch 'easy-mode'
[vagrant@localhost reflections]$ git branch
* easy-mode
  master

git log –graph –oneline

git merge

[vagrant@localhost reflections]$ git merge master easy-mode
Updating d878411..bdd001b
Fast-forward
 asteroids/game.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

remote branchへのpush
git push origin master

diff command

mac os use this command


[vagrant@localhost git]$ diff -u game_new.js game_old.js
--- game_new.js 2016-12-11 08:17:10.604013030 +0900
+++ game_old.js 2016-12-11 08:17:10.605013028 +0900
@@ -4,9 +4,9 @@
 //

 KEY_CODES = {
-  13: 'enter',
   32: 'space',
   37: 'left',
+  38: 'up',
   39: 'right',
   40: 'down',
   70: 'f',
@@ -392,7 +392,7 @@
       this.vel.rot = 0;
     }

-    if (KEY_STATUS.spacr) {
+    if (KEY_STATUS.up) {
       var rad = ((this.rot-90) * Math.PI)/180;
       this.acc.x = 0.5 * Math.cos(rad);
       this.acc.y = 0.5 * Math.sin(rad);
@@ -406,7 +406,7 @@
     if (this.delayBeforeBullet > 0) {
       this.delayBeforeBullet -= delta;
     }
-    if (KEY_STATUS.enter) {
+    if (KEY_STATUS.space) {
       if (this.delayBeforeBullet <= 0) {
         this.delayBeforeBullet = 10;
         for (var i = 0; i < this.bullets.length; i++) {
@@ -919,7 +919,7 @@
     waiting: function () {
       Text.renderText(ipad ? 'Touch Sreen to Start' : 'Press Space to Start', 36, Game.canvasWidth/2 - 270, Game.canvasHeight/2);
       if (KEY_STATUS.space || window.gameStart) {
-        KEY_STATUS.space = false; // hack so we don't move right away
+        KEY_STATUS.space = false; // hack so we don't shoot right away
         window.gameStart = false;
         this.state = 'start';
       }

Port connection

22 for ssh
80 for http

The port range that a normal (non-root) user can listen on is 1024 through 65535.

Hosts:a machine on the internet that might host services

vagrant@vagrant-ubuntu-trusty-64:~$ ping -c 1 google.com
PING google.com (172.217.24.142) 56(84) bytes of data.
64 bytes from nrt20s01-in-f14.1e100.net (172.217.24.142): icmp_seq=1 ttl=50 time=87.3 ms
vagrant@vagrant-ubuntu-trusty-64:~$ host google.com
google.com has address 172.217.25.238
google.com has IPv6 address 2404:6800:4004:817::200e
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
vagrant@vagrant-ubuntu-trusty-64:~$ host -t a google.com
google.com has address 172.217.25.238
vagrant@vagrant-ubuntu-trusty-64:~$ dig www.udacity.com

; <<>> DiG 9.9.5-3ubuntu0.10-Ubuntu <<>> www.udacity.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42131
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.udacity.com.               IN      A

;; ANSWER SECTION:
www.udacity.com.        3081    IN      CNAME   apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com.
apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. 33 IN A 54.186.129.122
apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. 33 IN A 52.26.227.216
apollo-mesos-elb-berlioz2-prod-885022263.us-west-2.elb.amazonaws.com. 33 IN A 35.163.216.178

;; Query time: 86 msec
;; SERVER: 10.0.2.3#53(10.0.2.3)
;; WHEN: Sat Dec 10 14:32:18 UTC 2016
;; MSG SIZE  rcvd: 160

CNAMEレコード 【 Canonical NAME record 】 CNAMEレコードとは、DNSで定義されるそのドメインについての情報の種類の一つで、あるドメイン名やホスト名の別名を定義するもの。
ドメイン名に対応するIPv6形式のIPアドレスが書いてある行が「AAAAレコード」
NSレコードは、ゾーン情報を管理するネームサーバー(DNSサーバー)名を定義するレコード

network address

vagrant@vagrant-ubuntu-trusty-64:~$ ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:6c:cb:39 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe6c:cb39/64 scope link
       valid_lft forever preferred_lft forever

my ip address
http://test-ipv6.com/

what server they use

nc send string over network.

vagrant@vagrant-ubuntu-trusty-64:~$ printf ‘HEAD / HTTP/1.1\r\nHost: www.google.co.jp\r\n\r\n’ | nc www.google.co.jp 80

HTTP/1.1 200 OK
Date: Sat, 10 Dec 2016 13:39:24 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Shift_JIS
P3P: CP=”This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.”
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=91=vO7aa7YjWxJPGDWffZj–p855KgcdJdpvH9dCUAVgWBX7f8JZdeweK_wY3msj09u8Osyz2tK4H4X3cjnZX1QK45cCxRB910amTq7Kmllk6TS3iH9HMIVw73LAaOW4Swq; expires=Sun, 11-Jun-2017 13:39:24 GMT; path=/; domain=.google.co.jp; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding

vagrant@vagrant-ubuntu-trusty-64:~$ printf ‘HEAD / HTTP/1.1\r\nHost: www.amazon.com\r\n\r\n’ | nc www.amazon.com 80
HTTP/1.1 301 Moved Permanently
Date: Sat, 10 Dec 2016 13:44:34 GMT
Server: Server
Location: https://www.amazon.com/
Content-Type: text/html; charset=iso-8859-1

vagrant@vagrant-ubuntu-trusty-64:~$ man nc

connecting by nc command.

vagrant@vagrant-ubuntu-trusty-64:~$ nc localhost 3456
vagrant@vagrant-ubuntu-trusty-64:~$ nc -l 3456

getting started Ubuntu

After install vitrualbox and vagrant,

vagrant init ubuntu/trusty64
vagrant up
vagrant ssh
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install netcat-openbsd tcpdump traceroute mtr

command

vagrant@vagrant-ubuntu-trusty-64:~$ ip addr show eth0
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:6c:cb:39 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe6c:cb39/64 scope link
       valid_lft forever preferred_lft forever

vagrant@vagrant-ubuntu-trusty-64:~$ ip route show
default via 10.0.2.2 dev eth0
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15

vagrant@vagrant-ubuntu-trusty-64:~$ ping -c3 8.8.8.8

vagrant@vagrant-ubuntu-trusty-64:~$ host -t aaaa google.com
google.com has IPv6 address 2404:6800:4004:817::200e

vagrant@vagrant-ubuntu-trusty-64:~$ printf 'HEAD / HTTP/1.1\r\nHost: www.udacity.com \r\n\r\n' \ |nc www.udacit.com 80
HTTP/1.1 200 OK
Content-Length: 0
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Sat, 10 Dec 2016 13:16:14 GMT

vagrant@vagrant-ubuntu-trusty-64:~$ sudo tcpdump -n -c5 -i eth0

vagrant@vagrant-ubuntu-trusty-64:~$ mtr www.udacity.com

ping request do not need server, just confirm computer system existing. Ping is simpler than HTTP, http is not ping.

make new instance

your-name

import media

toy_story = media.Movie("Toy Story",
                        "A story of a boy and toys that come to life",
                        "http://upload.wikimedia.org/wikipedia/en/1/13/Toy_Story.jpg",
                        "https://www.youtube.com/watch?v=vwyZH85NQC4")
# print(toy_story.storyline)

avatar = media.Movie("Avatar",
                     "A marine on an alien planet",
                     "http://upload.wikimedia.org/wikipedia/id/b/b0/Avatar-Teaser-Poster.jpg",
                     "http://www.youtube.com/watch?v=-9ceBgWV8io")

print(avatar.storyline)
# avatar.show_trailer()

kiminonawa = media.Movie("Kimino na wa",
                         "Your Name is a 2016 Japanese anime fantasy film written and directed by Makoto Shinkai",
                         "https://myanimelist.cdn-dena.com/images/anime/7/79999.webp",
                         "https://www.youtube.com/watch?v=k4xGqY5IDBE")
kiminonawa.show_trailer()

media.py

import webbrowser

class Movie():
    def __init__(self, movie_title, movie_storyline, poster_image, trailer_youtube):
        self.title = movie_title
        self.storyline = movie_storyline
        self.poster_image_url = poster_image
        self.trailer_youtube_url = trailer_youtube

    def show_trailer(self):
        webbrowser.open(self.trailer_youtube_url)

%e7%84%a1%e9%a1%8c

import fresh_tomatoes
import media

brother = media.Movie("brother",
                        "Takeshi Kitano plays Yamamoto, a lone yakuza officer. Defeated in a war with a rival family, his boss killed, he heads to Los Angeles, California.",
                        "https://upload.wikimedia.org/wikipedia/en/a/a1/BrotherKitano.jpg",
                        "https://www.youtube.com/watch?v=AgjDYR6-DTU")
# print(toy_story.storyline)
outrage = media.Movie("Outrage",
                     "The film begins with a sumptuous banquet at the opulent estate of the Grand Yakuza leader Sekiuchi (Soichiro Kitamura), boss of the Sanno-kai, a huge organized crime syndicate controlling the entire Kanto region, and he has invited the many Yakuza leaders under his control.",
                     "https://upload.wikimedia.org/wikipedia/en/f/f4/Outrage-2010-poster.png",
                     "https://www.youtube.com/watch?v=EXLuvZY8bMU")

#print(avatar.storyline)
# avatar.show_trailer()

#kiminonawa = media.Movie("Kimino na wa",
#                         "Your Name is a 2016 Japanese anime fantasy film written and directed by Makoto Shinkai",
#                         "https://myanimelist.cdn-dena.com/images/anime/7/79999.webp",
#                         "https://www.youtube.com/watch?v=k4xGqY5IDBE")
#kiminonawa.show_trailer()

kids = media.Movie("Kids Return",
                             "The movie is about two high school dropouts, Masaru (Ken Kaneko) and Shinji (Masanobu Ando)",
                             "https://upload.wikimedia.org/wikipedia/en/a/a7/KidsReturnPoster.jpg",
                             "https://www.youtube.com/watch?v=MvY6JCFCrQk")

ratatouille = media.Movie("Ratatouille",
                             "storyline",
                             "http://upload.wikimedia.org/wikipedia/en/5/50/RatatouillePoster.jpg",
                             "https://www.youtube.com/watch?v=3YG4h5GbTqU")

midnight_in_paris = media.Movie("Midnight in Paris",
                             "storyline",
                             "http://upload.wikimedia.org/wikipedia/en/9/9f/Midnight_in_Paris_Poster.jpg",
                             "https://www.youtube.com/watch?v=JABOZpoBYQE")

hunger_games = media.Movie("Hunger Games",
                             "storyline",
                             "http://upload.wikimedia.org/wikipedia/en/4/42/HungerGamesPoster.jpg",
                             "https://www.youtube.com/watch?v=PbA63a7H0bo")

movies = [brother, outrage, kids, ratatouille, midnight_in_paris, hunger_games]
fresh_tomatoes.open_movies_page(movies)

documentation

>>> import turtle
>>> turtle.Turtle.__doc__
'RawTurtle auto-creating (scrolled) canvas.\n\n    When a Turtle object is created or a function derived from some\n    Turtle method is called a TurtleScreen object is automatically created.\n 

inheritance

class Parent():
    def __init__(self, last_name, eye_color):
        print("parent constructor called")
        self.last_name = last_name
        self.ey_color = eye_color

class Child(Parent):
    def __init__(self, last_name, eye_color, number_of_toys):
        print("child constructor called")
        Parent.__init__(self, last_name, eye_color)
        self.number_of_toys = number_of_toys
        

#billy_cyrus = Parent("Cyrus","blue")
#print(billy_cyrus.last_name)

myley_crus = Child("cryus", "blue", 5)
print(myley_crus.last_name)
print(myley_crus.number_of_toys)

curse word search

http://www.wdylike.appspot.com/ is a site judge it contain curse word in it.

import urllib

def read_text():
   quotes = open(r"C:\Users\hoge\Desktop\controller\movie_quote.txt")
   contents_of_file = quotes.read()
   # print(contents_of_file)
   quotes.close()
   check_profanity(contents_of_file)

def check_profanity(text_to_check):
    connection = urllib.urlopen("http://www.wdylike.appspot.com/?q="+text_to_check)
    output = connection.read()
    # print(output)
    connection.close()
    if "true" in output:
        print("Profanity Alert!")
    elif "false" in output:
        print("This document has no curse words")
    else:
        print("Could not scan the document properly.")
    
read_text()

when you write python, google style guide would help coding.
https://google.github.io/styleguide/pyguide.html

draw star

star

import turtle

def draw_star(some_turtle):
    for i in range(1, 6):
        some_turtle.forward(100)
        some_turtle.right(144)

def draw_art():
    window = turtle.Screen()
    window.bgcolor("red")
    
    brad = turtle.Turtle()
    brad.shape("arrow")
    brad.color("blue")    
    brad.speed(5)
    for i in range(1, 11):
        draw_star(brad)
        brad.right(36)

    # angie = turtle.Turtle()
    # angie.shape("arrow")
    # angie.color("blue")
    # angie.circle(100)

    # triangle = turtle.Turtle()
    # angie.shape("arrow")
    # angie.color("blue")
    
    # for x in range(0, 3):
    #    brad.forward(100)
    #    brad.right(240)

    window.exitonclick()

draw_art()

open file and read the content from the file.
python library: https://docs.python.org/2/library/functions.html#open

below code “quotes” indicate the object another word instance.

def read_text():
   quotes = open(r"C:\Users\hoge\Desktop\controller\movie_quote.txt")
   contents_of_file = quotes.read()
   print(contents_of_file)
   quotes.close()
   
read_text()

using python build-in function

def multi_func(a, b):
    return a*b

items = [1,3, 5, 7]
def reduce_value():
    value = reduce(multi_func, items)
    print(value)

reduce_value()

105
>>>

python time alert

time alert program which call you tube video.

import time
import webbrowser

total_breaks = 3
break_count = 0

print("this program start on"+time.ctime())
while(break_count < total_breaks):
    time.sleep(2*60*60)
    webbrowser.open("https://www.youtube.com/watch?v=PDSkFeMVNFs")
    break_count = break_count + 1

get a file from computer and rename file name.In this case, remove the number of file name.

import os
def rename_files():
    #(1)get file names from a folder
    file_list = os.listdir(r"C:\Users\prank")
    #print(file_list)
    save_path = os.getcwd()
    print(save_path)
    os.chdir(r"C:\Users\prank")
    #(2)for each file, rename filename
    for file_name in file_list:
       os.rename(file_name, file_name.translate(None, "0123456789"))
    os.chdir(save_path)

rename_files()

crawling page rank

def crawl_web(seed): # returns index, graph of outlinks
    tocrawl = [seed]
    crawled = []
    graph = {}  # <url>:[list of pages it links to]
    index = {} 
    while tocrawl: 
        page = tocrawl.pop()
        if page not in crawled:
            content = get_page(page)
            add_page_to_index(index, page, content)
            outlinks = get_all_links(content)
            graph[page] = outlinks
            union(tocrawl, outlinks)
            crawled.append(page)
    return index, graph
def ancestors(genealogy, person):
  if person in genealogy:
    parents = genealogy[person]
    result = parents
    for parent in parents:
      result = result + ancestors(genealogy, parent)
    return result
    
  return []

Blaise Pascal Triangle

def make_next_row(row):
  result = []
  prev = 0
  for e in row:
    result.append(e + prev)
    prev = e
  result.append(prev)
  return result

def triangle(n):
  result = []
  current = [1]
  for unused in range(0, n):
    result.append(current)
    current = make_next_row(current)
    
  return result