Created
August 4, 2014 02:11
-
-
Save kasajei/4133500e3b290edd0f88 to your computer and use it in GitHub Desktop.
Python Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# 数字 | |
num = 1 | |
print num | |
num = 1.12 | |
print num | |
# 四則演算 | |
x = 12 | |
y = 4 | |
print x + y # 足し算 | |
print x - y # 引き算 | |
print x * y # 掛け算 | |
print x / y # 割り算 | |
# bool | |
boolean = True | |
print boolean | |
boolean = False | |
print boolean | |
# 文字 | |
string = "文字列" | |
print string | |
append = "ついか" | |
print string + append # 文字列の連結 | |
# 配列 | |
array = [1, 2, 3] | |
print array | |
print "最初の要素は" + `array[0]` | |
print "長さは" + `len(array)` | |
array[0] = 5 | |
array.append(4) | |
print "4を追加して : " + `array` | |
array.remove(4) | |
print "4を消去して : " + `array` | |
array.sort() | |
print "ソート" + `array` | |
array.sort(reverse=True) | |
print "反対にソート" + `array` | |
# タプル : 変更できない | |
tuple = (1, 2, 2) | |
print "タプル :" + `tuple` | |
try: | |
tuple[1] = 1 # error | |
except: | |
print "error" | |
# 辞書 | |
dict = { | |
"key1": "Value1", | |
"key2": "Value2" | |
} | |
print dict | |
print "keyの値は" + dict["key1"] | |
dict["key3"] = "Value3" | |
print "追加した : " + `dict` | |
# for文 | |
for num in array: | |
print "arrayの中身 : " + `num` | |
for key in dict: | |
print "dictのkey : " + key + "のValue : " + dict[key] | |
# if文 | |
if 1 == 1: | |
print "1は1です" | |
if 1 == 2: | |
print "1は2ではないので表示されない" | |
elif 1 == 3: | |
print "1は3でもないので表示されない" | |
elif 1 == 1: | |
print "1は1なので表示される" | |
# while文 | |
num = 0 | |
while num < 5: | |
print " while :" + `num` | |
num += 1 | |
# 関数 | |
def sum(x, y): | |
return x + y | |
print sum(1, 4) | |
def sum_and_multiple(x, y): | |
return x + y, x * y | |
print sum_and_multiple(1, 3) | |
def change_value(x, y): | |
z = x | |
x = y | |
y = z | |
return x, y | |
print change_value(1, 2) | |
# クラス | |
class Person(): | |
def __init__(self, name="名無しさん"): | |
self.name = name | |
self.age = 0 | |
def show_name(self): | |
return self.name | |
person = Person() | |
print person.show_name() | |
person.name = "kasajei" | |
print person.show_name() | |
print person.name | |
print person.age | |
person.age = 18 | |
print person.age | |
person2 = Person("名無しさん2") | |
print person2.show_name() | |
# クラスとクラス | |
class Company(): | |
def __init__(self, name="名無し会社", place="会社の場所"): | |
self.name = name | |
self.place = place | |
self.employees = [] | |
def add_employee(self, employee): | |
self.employees.append(employee) | |
employee.company = self | |
def remove_employee(self, employee): | |
self.employees.remove(employee) | |
employee.company = Company() | |
# クラスの継承 | |
class Employee(Person): | |
def __init__(self, name="名無しさん", company=Company()): | |
Person.__init__(self, name=name) | |
self.company = company | |
def __str__(self): | |
return self.name + "@" + self.company.name | |
employee = Employee() | |
print employee.show_name() | |
company = Company(name="R", place="銀座") | |
company.add_employee(employee) | |
for r_employee in company.employees: | |
print r_employee | |
print employee.company.name | |
print employee.company.place |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from twitter import * | |
from key import * | |
# https://github.com/sixohsix/twitter | |
class TwitterBot(): | |
def __init__(self): | |
auth = OAuth(ACCESS_TOKEN, ACCESS_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET) | |
self.twitter = Twitter(auth=auth) | |
self.twitter_stream = TwitterStream(auth=auth, domain="userstream.twitter.com") | |
def get_timeline(self): | |
time_lines = self.twitter.statuses.home_timeline() | |
for time_line in time_lines: | |
print time_line["text"] | |
def update(self, status): | |
self.twitter.statuses.update(status=status) | |
def stream(self): | |
for msg in self.twitter_stream.user(): | |
print msg | |
if "text" in msg: | |
self.bot(msg) | |
print msg["text"] | |
def bot(self, msg): | |
if u"テスト" in msg["text"]: | |
self.update("ほっ") | |
pass | |
if __name__ == "__main__": | |
twitterbot = TwitterBot() | |
# twitterbot.get_timeline() | |
# twitterbot.update("テスト") | |
twitterbot.stream() | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment