{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Python Basic**\n",
"지난 수업 이후로 **Python 에서 기본 모듈로** 구현가능한 내용들 정리하기"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **1 math 계산하기**\n",
"목적 : **생성한 Dict 객체 호출시 기본값을 특정하여** 오류처리 없이도 전체 과정이 진행"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"math.ceil(3.14) # 올림 : 결과는 4"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.floor(3.14) # 내림 : 결과는 3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.trunc(-3.14) # trunc() 내림을 해도 0을 향한다"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(3.1415) # 반올림 : Python 내장함수를 사용"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"# **Dict 객체 다루기**\n",
"**default Dict** 과 **Ordered Dict** 기초 [블로그](https://dongdongfather.tistory.com/71?category=680339) 정리\n",
"1. 자료형 작업을 하다보면 **dict 객체를** 다양하게 정의할 필요가 있다\n",
"\n",
"## **1 defaultDict**\n",
"목적 : **생성한 Dict 객체 호출시 기본값을 특정하여** 오류처리 없이도 전체 과정이 진행"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defaultdict(int, {})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import defaultdict\n",
"int_dict = defaultdict(int)\n",
"int_dict"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
},
{
"data": {
"text/plain": [
"defaultdict(int, {'key1': 0})"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 입력값이 없어도, 호출시 \"int\" 를 자동생성\n",
"# 작업 과정에 오류가 있어도 Dict 객체를 생성\n",
"print(int_dict['key1'])\n",
"int_dict"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defaultdict(int, {'key1': 0, 'key2': 'test'})"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int_dict['key2'] = 'test'\n",
"int_dict"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defaultdict(int, {'key1': 0, 'key2': 'test', 'key3': 0})"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int_dict['key3']\n",
"int_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **2 OrderedDict**\n",
"dict 객체를 순서에 따라 정렬할 필요가 있을때\n",
"1. **\"Json\" 의 Header 안내를** 컬럼으로 활용하는 방법을 알아보자\n",
"1. dict 객체를 [list 내부 (tuple) 묶음으로 관리 및 활용]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"OrderedDict([('NUM', '번호'),\n",
" ('FOOD_CD', '식품코드'),\n",
" ('DESC_KOR', '식품이름'),\n",
" ('SERVING_WT', '1회제공량(g)')])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"col_to_kor = {\n",
" \"NUM\":\"번호\",\n",
" \"FOOD_CD\":\"식품코드\",\n",
" \"DESC_KOR\":\"식품이름\",\n",
" \"SERVING_WT\":\"1회제공량(g)\",\n",
"}\n",
"\n",
"from collections import OrderedDict\n",
"json_keys = OrderedDict(col_to_kor)\n",
"json_keys"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['NUM', 'FOOD_CD', 'DESC_KOR', 'SERVING_WT']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(OrderedDict(col_to_kor).keys())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"# **시계열 데이터 다루기**\n",
"**python** 의 기본 **datetime** 객체 다루기\n",
"1. 요즘들어 Python 기본 모듈로 인코딩 변환이 취미가 됨...\n",
"1. 가능하면 외부 모듈이 아닌, **Python 기본 모듈을** 로 최대한 구현해보기"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **1 Datetime 모듈**\n",
"Python 에서 기본 Datetime 객체 다루기"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Monday 21. October 2019\n",
"2019-10-21\n"
]
},
{
"data": {
"text/plain": [
"datetime.datetime(2019, 10, 10, 12, 12, 12, 1000)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import datetime, timedelta\n",
"print(datetime.today().strftime(\"%A %d. %B %Y\"))\n",
"print(datetime.today().strftime('%Y-%m-%d'))\n",
"# 년, 월, 일, 시간, 분, 초\n",
"datetime(2019,10,10,12,12,12,1000)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2019, 9, 30, 0, 0)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 수치를 활용한 보간\n",
"# 날짜를 연산으로 변환된 값을 추출 가능\n",
"datetime(2019,10,10) - timedelta(10)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(2019, 1, 1, 0, 0)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"datetime.strptime('20190101', '%Y%d%m')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **2 Pandas**\n",
"datetime 객체를 활용하는 Pandas 모듈"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',\n",
" '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',\n",
" '2019-01-09', '2019-01-10'],\n",
" dtype='datetime64[ns]', freq='D')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 10일의 기간 출력\n",
"import pandas as pd\n",
"pd.date_range('2019/01/01', periods=10)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',\n",
" '2019-01-07', '2019-01-08', '2019-01-09', '2019-01-10',\n",
" '2019-01-11', '2019-01-14'],\n",
" dtype='datetime64[ns]', freq='B')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 10일의 기간 출력\n",
"pd.date_range('2019/01/01', periods=10, freq='B')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-01-01 00:00:00', '2019-01-01 12:30:00',\n",
" '2019-01-02 01:00:00', '2019-01-02 13:30:00',\n",
" '2019-01-03 02:00:00', '2019-01-03 14:30:00',\n",
" '2019-01-04 03:00:00', '2019-01-04 15:30:00',\n",
" '2019-01-05 04:00:00', '2019-01-05 16:30:00'],\n",
" dtype='datetime64[ns]', freq='750T')"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 10일의 기간 출력\n",
"pd.date_range('2019/01/01', periods=10, freq='12h30min', normalize=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatetimeIndex(['2019-01-01 00:00:00', '2019-02-03 18:40:00',\n",
" '2019-03-09 13:20:00', '2019-04-12 08:00:00',\n",
" '2019-05-16 02:40:00', '2019-06-18 21:20:00',\n",
" '2019-07-22 16:00:00', '2019-08-25 10:40:00',\n",
" '2019-09-28 05:20:00', '2019-11-01 00:00:00'],\n",
" dtype='datetime64[ns]', freq=None)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 기간 구간을 10개로 나누기\n",
"pd.date_range('2019/01/01', '2019/11/01', periods=10)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2019-10-10', '2019-10-11', '2019-10-12', '2019-10-13', '2019-10-14']"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# String Format time data\n",
"# 개별 객체에 datetime 메소드를 적용\n",
"[_.strftime('%Y-%m-%d') for _ in pd.date_range('2019/10/10', periods=5)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"# **Decorator**\n",
"슬기로운 파이썬 트릭 (2019 | 프로그래밍 인사이트)\n",
"## **1 데코레이터의 힘**"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already up-to-date: instabot in /home/momukji/Python/Python/lib/python3.6/site-packages (0.89.0)\n",
"Requirement already satisfied, skipping upgrade: schedule>=0.6.0 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (0.6.0)\n",
"Requirement already satisfied, skipping upgrade: huepy>=0.9.8.1 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (1.2.1)\n",
"Requirement already satisfied, skipping upgrade: pysocks>=1.6.8 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (1.7.1)\n",
"Requirement already satisfied, skipping upgrade: responses>=0.10.5 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (0.10.9)\n",
"Requirement already satisfied, skipping upgrade: future>=0.17.1 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (0.18.2)\n",
"Requirement already satisfied, skipping upgrade: pytz>=2019.1 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (2019.3)\n",
"Requirement already satisfied, skipping upgrade: requests>=2.21.0 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (2.21.0)\n",
"Requirement already satisfied, skipping upgrade: tqdm>=4.30.0 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (4.36.1)\n",
"Requirement already satisfied, skipping upgrade: requests-toolbelt>=0.8.0 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (0.9.1)\n",
"Requirement already satisfied, skipping upgrade: six>=1.12.0 in /home/momukji/Python/Python/lib/python3.6/site-packages (from instabot) (1.12.0)\n",
"Requirement already satisfied, skipping upgrade: idna<2.9,>=2.5 in /home/momukji/Python/Python/lib/python3.6/site-packages (from requests>=2.21.0->instabot) (2.8)\n",
"Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in /home/momukji/Python/Python/lib/python3.6/site-packages (from requests>=2.21.0->instabot) (1.24.3)\n",
"Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /home/momukji/Python/Python/lib/python3.6/site-packages (from requests>=2.21.0->instabot) (2019.9.11)\n",
"Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /home/momukji/Python/Python/lib/python3.6/site-packages (from requests>=2.21.0->instabot) (3.0.4)\n"
]
}
],
"source": [
"! pip install -U instabot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2020-01-02 17:13:32,482 - INFO - Instabot Started\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"from instabot import Bot\n",
"\n",
"\n",
"bot = Bot()\n",
"bot.login(username=\"saltman21@naver.com\", password=\"inst8472\")\n",
"# bot.login(username=\"YOUR_LOGIN\", password=\"YOUR_PASSWORD\")\n",
"user_id = bot.get_user_id_from_username(\"erdoskim\")\n",
"user_info = bot.get_user_info(user_id)\n",
"print(user_info['biography'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"ename": "InstagramException",
"evalue": "Response code is 400. Body: {\"message\": \"useragent mismatch\", \"status\": \"fail\"} Something went wrong. Please report issue., Code:400",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mInstagramException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;31m#Getting an account by id\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0maccount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minstagram\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_account_by_id\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/Python/Python/lib/python3.6/site-packages/igramscraper/instagram.py\u001b[0m in \u001b[0;36mget_account_by_id\u001b[0;34m(self, id)\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAccount\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 109\u001b[0m \"\"\"\n\u001b[0;32m--> 110\u001b[0;31m \u001b[0musername\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_username_by_id\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 111\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_account\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0musername\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/Python/Python/lib/python3.6/site-packages/igramscraper/instagram.py\u001b[0m in \u001b[0;36mget_username_by_id\u001b[0;34m(self, id)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mInstagram\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mHTTP_OK\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 128\u001b[0m raise InstagramException.default(response.text,\n\u001b[0;32m--> 129\u001b[0;31m response.status_code)\n\u001b[0m\u001b[1;32m 130\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[0mjson_response\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mInstagramException\u001b[0m: Response code is 400. Body: {\"message\": \"useragent mismatch\", \"status\": \"fail\"} Something went wrong. Please report issue., Code:400"
]
}
],
"source": [
"from igramscraper.instagram import Instagram\n",
"\n",
"instagram = Instagram()\n",
"\n",
"# authentication supported\n",
"instagram.with_credentials('erdoskim', 'inst8472')\n",
"instagram.login()\n",
"\n",
"#Getting an account by id\n",
"account = instagram.get_account_by_id(3)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"ename": "InstagramException",
"evalue": "Response code is 400. Body: {\"message\": \"useragent mismatch\", \"status\": \"fail\"} Something went wrong. Please report issue., Code:400",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mInstagramException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;31m#Getting an account by id\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0maccount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minstagram\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_account_by_id\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;31m# Available fields\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/Python/Python/lib/python3.6/site-packages/igramscraper/instagram.py\u001b[0m in \u001b[0;36mget_account_by_id\u001b[0;34m(self, id)\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAccount\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 109\u001b[0m \"\"\"\n\u001b[0;32m--> 110\u001b[0;31m \u001b[0musername\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_username_by_id\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 111\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_account\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0musername\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/Python/Python/lib/python3.6/site-packages/igramscraper/instagram.py\u001b[0m in \u001b[0;36mget_username_by_id\u001b[0;34m(self, id)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mInstagram\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mHTTP_OK\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 128\u001b[0m raise InstagramException.default(response.text,\n\u001b[0;32m--> 129\u001b[0;31m response.status_code)\n\u001b[0m\u001b[1;32m 130\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[0mjson_response\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mInstagramException\u001b[0m: Response code is 400. Body: {\"message\": \"useragent mismatch\", \"status\": \"fail\"} Something went wrong. Please report issue., Code:400"
]
}
],
"source": [
"# Available fields\n",
"print('Account info:')\n",
"print('Id: ', account.identifier)\n",
"print('Username: ', account.username)\n",
"print('Full name: ', account.full_name)\n",
"print('Biography: ', account.biography)\n",
"print('Profile pic url: ', account.get_profile_pic_url_hd())\n",
"print('External Url: ', account.external_url)\n",
"print('Number of published posts: ', account.media_count)\n",
"print('Number of followers: ', account.followed_by_count)\n",
"print('Number of follows: ', account.follows_count)\n",
"print('Is private: ', account.is_private)\n",
"print('Is verified: ', account.is_verified)\n",
"\n",
"# or simply for printing use \n",
"print(account)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [1,2,3]\n",
"b = [1,2,3]\n",
"a is b"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a == b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n"
]
}
],
"source": [
"%reset"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"def uppercase(func):\n",
" def wrapper(): \n",
" return func().upper()\n",
" return wrapper"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"@uppercase\n",
"def greet():\n",
" return 'hello boys!!'"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "wrapper() takes 0 positional arguments but 1 was given",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgreet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hello boys!'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: wrapper() takes 0 positional arguments but 1 was given"
]
}
],
"source": [
"greet('hello boys!')"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"def decor1(func):\n",
" def wrap():\n",
" print(\"$$$$$$$$$$$$$$\")\n",
" func()\n",
" print(\"$$$$$$$$$$$$$$\")\n",
" return func.upper()\n",
" return wrap"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"$$$$$$$$$$$$$$\n",
"Hello\n",
"$$$$$$$$$$$$$$\n"
]
},
{
"ename": "AttributeError",
"evalue": "'function' object has no attribute 'upper'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msayhello\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0msayhello\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m\u001b[0m in \u001b[0;36mwrap\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"$$$$$$$$$$$$$$\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrap\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'function' object has no attribute 'upper'"
]
}
],
"source": [
"@decor1\n",
"def sayhello():\n",
" print(\"Hello\")\n",
"sayhello()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" >>> def decor2(func):\n",
" def wrap():\n",
" print(\"##############\")\n",
" func()\n",
" print(\"##############\")\n",
" return wrap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}