rabbitfoot530's diary

読んだ本と、プログラムに関することのメモです。好きな言語は、C++, Python, Golang, TypeScript。数学・物理・学習理論も好きです。

電話番号登録なしで、一時アカウントロックを回避する方法

 Twitterのアカウントを何故か、一時ロックされてしまうことがありました。Twitterからは、次のようなメールがすぐに送られてきました。

Your account appears to have exhibited automated behavior that violates the Twitter Rules. Inder to protect Twitter from unauthorized logins, we've temporarily locked @XXXXX. Click the button below to confirm that you are the valid account owner and unlock your account. Unlock my account Or use this link: https://twitter.com/unlock_account/sms_confirmation Thank you, The Twitter Security Team

 その際、電話番号の登録を強要されたのですが、電話番号をTwitterに登録するのはだったので、なんとかならないのかなぁと思って、PCからTwitterの設定画面を見ていました。(そもそもSMS出来ない人は、どうするのだろうか?

 そこで、下記のような項目が自分には不要だったので、削除しました。

Promoted Content : Tailor ads based on information shared by ad partners.

 そして保存すると、一時ロックも解除されていました。

 新規アカウント追加時に、電話番号が必要な人もいるというのは、知っていましたが、一時ロック解除にまで電話番号を強要してくるとか...Googleも昔、電話番号の強要をやっていましたが、辞めましたよね。本当に気持ち悪いから、こういう後付強要みたいなのはやめてもらいたいですね。やるならサービス開始時からやって欲しいです。

2014年に読んだ本

 2014年に、最初から最後まで読みきった本リスト。今年は、縦書の本をここ10年で1番読んだ年だったと思う。 幾何と物理部分にも徐々に進めているの、来年もこのまま数学と物理を頑張っていきたい。まだまだ基礎固め部分だが、自分の考えを数式にするには、僕はまだまだまだまだ、勉強不足。

PythonによるLinuxプログラミングインタフェース(5章 mkstemp)

 第5章のP115のテンポラリファイル作成のpythonコード。

#/usr/bin/env
#-*- encoding: utf-8 -*-
import tempfile
import os
import time

# don't use
# os.tmpnam (see: http://docs.python.org/2/library/os.html#os.tmpnam)
# os.tempnam (see: http://docs.python.org/2/library/os.html#os.tempnam)
# tempfile.mktemp (see: http://docs.python.org/2/library/tempfile.html#tempfile.mktemp)
# instead of tempfile.mkstemp
def pythonic():
    suffix = "XXXXXXX"
    prefix = "YYYY"
    # you can change temp path
    # dir = "/tmp"
    text = False
    # mkstemp return fd by os.open() and temp path
    fd, path = tempfile.mkstemp(
        suffix=suffix,
        prefix=prefix,
        text=text
    )

    if -1 == fd:
        exit()

    print("Generated filename was: {0}".format(path))
    # if you want to check temp file exist, try following sleep func!
    # time.sleep(10)
    os.unlink(path)
    os.close(fd)


if __name__ == '__main__':
    pythonic()

 テンポラリファイル作成には、os.tmpnam, os.tempnam, tempfile.mktempがあるが、tempfile.mkstempを使う。理由は上記コードのリンク先を参照。 テンポラリファイルの場所は、OSによって違うと思う。OSXで実行したら、/tmpではなかった。指定することも出来る。  実際に、ファイルがunlinkする前に作成されてるか確認するには、time.sleepをコメントアウトしてファイルの存在を確認すると、実際にテンポラリファイルが作成されていることがわかる。

PythonによるLinuxプログラミングインタフェース(5章 truncate)

第5章P109のtruncateについて。

#/usr/bin/env
#-*- encoding: utf-8 -*-
import os

def pythonic():
    path = "./p109_truncate.txt"
    file = open(path, 'w', encoding='utf-8')
    file.write("abc")
    # write "abc" and offset move to 3 is end of file.
    print(file.tell()) # 3
    # truncate padding 0. but offset won't move to end of file.
    file.truncate(100)
    # truncate don't move file offset
    print(file.tell()) # 3
    # seek to end of file
    file.seek(0, os.SEEK_END)
    print(file.tell()) # 100
    file.close()

def unix():
    path = "./p109_truncate.txt"
    os.truncate(path, 200)

if __name__ == '__main__':
    pythonic()
    unix()

 truncateは、ファイルを拡張してくれる。pythonicの方では、openで開いたファイルに対してtruncateを呼び出してるが、たぶんこっちはftruncateのファイルディスクリプタを使う方なんだと思う。Cのtruncateは、unixメソッドの方で示したos.truncateみたいにpathからファイルを拡張できるもの。os.ftruncateみたいにファイルディスクリプタを用いる方がpythonicメソッドで示したものだと思う。

PythonによるLinuxプログラミングインタフェース(5章 pread)

第5章のP105のpreadのサンプル。

#/usr/bin/env
#-*- encoding: utf-8 -*-
import os


def pythonic():
    file = open(__file__, encoding='utf-8')
    orig = file.tell()
    print("before offset: {0}".format(file.tell()))
    offset = 10
    file.seek(offset)
    lines = file.readlines()
    print(lines)
    file.seek(orig)
    print("after offset: {0}".format(file.tell()))
    file.close()

def pythonic2():
    file = open(__file__, encoding='utf-8')
    print("before offset: {0}".format(file.tell()))
    buffer_size = 100
    offset = 10
    s = os2.pread(file.fileno(), buffer_size, offset)
    print(s)
    print("after offset: {0}".format(file.tell()))
    file.close()

if __name__ == '__main__':
    pythonic()
    pythonic2()

 pythonicメソッドが、preadの内容をseekを使って書いたもの。自分でseekしなくても、preadがやってくれるので、preadの操作をしたいときは、os.preadを使ったほうがいい。 pread使えばreadした後、seekでreadした分戻らなくても、自動でread前の位置に戻ってる。

PythonによるLinuxプログラミングインタフェース(5章 dup)

 5章P103のduppythonコード。

#/usr/bin/env
#-*- encoding: utf-8 -*-
import os


def pythonic():
    def run():
        py_dup()
        py_dup2()

    def py_dup():
        newfd = os.dup(1)
        print("newfd: " + str(newfd))
        os.close(2)
        newfd = os.dup(1)
        print("newfd: " + str(newfd))

    def py_dup2():
        file = open(__file__)
        fd = file.fileno()
        newfd = 1000
        os.dup2(fd, newfd)
        print(fd)
        print(newfd)
        str = os.read(newfd, 100)
        print(str)

    return run

if __name__ == '__main__':
    pythonic()()
    #run = pythonic()
    #run()

os.dupにて、引数に与えたファイルディスクリプタをコピーする。 今回は書き込みのfdの1をコピー。 この時点でnewfdは、1のコピーとなる。

そして、os.dup2にてfdにopenしたファイルのファイルディスクリプタを与えて、newfdに適当な1000を入れておいて、os.dup2にてfdをnewfdにコピーすると、newfdはfdを指しているので、newfdをreadすると当然、fdでopenしたファイルの内容が表示される。

PythonによるLinuxプログラミングインタフェース(4章 read, write, lseek)

 4章のP90ページのread, write, lseekのプログラム。

#/usr/bin/env
#-*- encoding: utf-8 -*-
import sys


def pythonic():
    try:
        file = open(sys.argv[1], 'r+', encoding='utf-8')
    except IOError as e:
        print("err: " + e)
    else:
        try:
            ap = 2
            argc = len(sys.argv)
            while ap < argc:
                opt = sys.argv[ap][0]
                if opt in {'r', 'R'}:
                    length = int(sys.argv[ap][1:])
                    data = file.read(length)
                    i = 0

                    while i < length:
                        if opt == 'r':
                            print("{0}".format(data[i]))
                        elif opt == 'R':
                            print("{0}".format(str(ord(data[i]))))
                        i += 1

                elif opt == 'w':
                    print(sys.argv[ap][1:])
                    print(sys.argv[1])
                    file.write(sys.argv[ap][1:])
                elif opt == 's':
                    offset = int(sys.argv[ap][1:])
                    try:
                        file.seek(offset)
                    except Exception as e:
                        print(e)
                    else:
                        print("{0}: seek succeeded".format(offset))
                else:
                    pass
                ap += 1
        finally:
            file.close()

# touch tfile
# ./p90_seek_io.py tfile s100000 wabc
# ./p90_seek_io.py tfile s10000 R5
if __name__ == '__main__':
    pythonic()

 引数により、ファイルを作成したり、読み込んだりする。

  • ファイルホールの部分は、0が表示される。
  • ordにより文字列のバイトを表示する部分。