Skip to content

Instantly share code, notes, and snippets.

@suganoo
suganoo / empyfile.py
Created September 7, 2017 02:17
pythonで空ファイルだけを作る
with open("test.txt","w"):pass
@suganoo
suganoo / extract_not_exist.py
Last active September 7, 2017 06:20
setでlistの差分を取る方法
listA = ["d","a","b","c"]
listB = ["e","b","c"]
def extract_not_exist(list_a, list_b):
"""
# forでなんとか抽出する方法
not_exist_items = []
for item in list_a:
if not (item in list_b):
not_exist_items.append(item)
@suganoo
suganoo / error_output.txt
Last active July 23, 2018 01:35
json パースでTypeError: string indices must be integers
self.filepath = self._get_value_from_messageattributes("filepath")
File "/xxxxxx/test.py", line 93, in _get_value_from_messageattributes
self.logger.debug("return value : %s", self.message["Message"][key]["Value"])
TypeError: string indices must be integers
@suganoo
suganoo / 01_list_set_list.py
Created September 12, 2017 08:25
リストで重複除く
list_a = ["a","a","a","a","b","b","c","d","d","d"]
print "before: %s" % list_a
print "after: %s" % list(set(list_a))
@suganoo
suganoo / 01_file_list
Last active October 15, 2017 15:12
【globals()】文字列からクラス生成する
# ファイルが下記のように配置されてるとする
include_script_1.py
include_script_2.py
test_1.py
test_2.py
@suganoo
suganoo / 01 logging_formatter.conf
Last active October 17, 2017 09:27
ユーザー定義のLogRecord属性
# もちろん必要部分は書いてください
# 説明上formatだけ記述します。
......................
[formatter_hogehogeFormatter]
format=%(asctime)s%(tab)s%(hostname)s%(tab)s%(user)s%(tab)s%(message)s
@suganoo
suganoo / 01 str_num_to_int_list.py
Last active October 22, 2017 14:50
文字列数字をint型リストにする
input_num="1,2,3,4"
print("### original ###")
print(input_num)
print("### result split ###")
print(input_num.split(","))
print("### result split map ###")
print(map(int,input_num.split(",")))
@suganoo
suganoo / 01 hostnamecheck.py
Last active October 26, 2017 09:57
ホスト名を取得
import socket
import os
import platform
print socket.getfqdn()
# 以降は同じみたい
print socket.gethostname()
print os.uname()[1]
print platform.uname()[1]
@suganoo
suganoo / ホスト名の確認.md
Last active October 27, 2017 09:45
ホスト名がどこから読んでいるか

01.何も変更しない状態(default)

/etc/hosts
127.0.0.1 samplehost localhost localhost.localdomain
# python -c 'import socket; print socket.getfqdn()'
localhost.localdomain
# python -c 'import socket; print socket.gethostname()'
samplehost
@suganoo
suganoo / 01_get_username.py
Last active November 10, 2017 06:52
ユーザー名の取得
import os
import getpass
import pwd
print(os.getlogin())
print(getpass.getuser())
print(os.environ.get("USER"))
print(pwd.getpwuid(os.getuid())[0])