6/07/2020
Python - Coding Problem: Creating Your Own Iterators
** Coding Problem: Creating Your Own Iterators
==========================================
my_sentence = Sentence('This is a test')
for word in my_sentence:
print(word)
# This should have the following output:
# This
# is
# a
# test
---------------------------------
class Sentence:
def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]
my_sentence = Sentence('This is a test')
for word in my_sentence:
print(word)
---------------------------------
This
is
a
test
==========================================
==========================================
class Sentence:
def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]
my_sentence = Sentence('This is a test')
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
---------------------------------
is
a
test
print(next(my_sentence))
File "C:\Users\purunet\Documents\py9\ii.py", line 14, in __next__
raise StopIteration
StopIteration
---------------------------------
class Sentence:
def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()
def __iter__(self):
return self
def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]
def sentence(sentence):
for word in sentence.split():
yield word
my_sentence = sentence('This is a test')
for word in my_sentence:
print(word)
---------------------------------
This
is
a
test
==========================================
By LinuxerHAN , Date : 6/07/2020
Label : 파이썬, iterations, python
6/05/2020
Python - Iterators and Iterables : What Are They and How Do They Work?
By LinuxerHAN , Date : 6/05/2020
Python - 참고
** 참고
==========================================
C:\Users\purunet\Documents\Flask_Blog>pip install flask-wtf
Collecting flask-wtf
Downloading Flask_WTF-0.14.3-py2.py3-none-any.whl (13 kB)
Collecting WTForms
Downloading WTForms-2.3.1-py2.py3-none-any.whl (169 kB)
|████████████████████████████████| 169 kB 327 kB/s
Requirement already satisfied: itsdangerous in c:\users\purunet\appdata\local\pr
ograms\python\python38-32\lib\site-packages (from flask-wtf) (1.1.0)
Requirement already satisfied: Flask in c:\users\purunet\appdata\local\programs\
python\python38-32\lib\site-packages (from flask-wtf) (1.1.2)
Requirement already satisfied: MarkupSafe in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from WTForms->flask-wtf) (1.1.1)
Requirement already satisfied: Jinja2>=2.10.1 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-wtf) (2.11.2)
Requirement already satisfied: click>=5.1 in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from Flask->flask-wtf) (7.1.2)
Requirement already satisfied: Werkzeug>=0.15 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-wtf) (1.0.1)
Installing collected packages: WTForms, flask-wtf
Successfully installed WTForms-2.3.1 flask-wtf-0.14.3
==========================================
C:\Users\purunet\Documents\Flask_Blog>pip install email_validator
Collecting email_validator
Downloading email_validator-1.1.1-py2.py3-none-any.whl (17 kB)
Requirement already satisfied: idna>=2.0.0 in c:\users\purunet\appdata\local\pro
grams\python\python38-32\lib\site-packages (from email_validator) (2.9)
Collecting dnspython>=1.15.0
Downloading dnspython-1.16.0-py2.py3-none-any.whl (188 kB)
|████████████████████████████████| 188 kB 327 kB/s
Installing collected packages: dnspython, email-validator
Successfully installed dnspython-1.16.0 email-validator-1.1.1
==========================================
C:\Users\purunet\Documents\Flask_Blog>pip install flask_sqlalchemy
Collecting flask_sqlalchemy
Downloading Flask_SQLAlchemy-2.4.1-py2.py3-none-any.whl (17 kB)
Collecting SQLAlchemy>=0.8.0
Downloading SQLAlchemy-1.3.17-cp38-cp38-win32.whl (1.2 MB)
|████████████████████████████████| 1.2 MB 364 kB/s
Requirement already satisfied: Flask>=0.10 in c:\users\purunet\appdata\local\pro
grams\python\python38-32\lib\site-packages (from flask_sqlalchemy) (1.1.2)
Requirement already satisfied: Werkzeug>=0.15 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask>=0.10->flask_sqlalchem
y) (1.0.1)
Requirement already satisfied: itsdangerous>=0.24 in c:\users\purunet\appdata\lo
cal\programs\python\python38-32\lib\site-packages (from Flask>=0.10->flask_sqlal
chemy) (1.1.0)
Requirement already satisfied: click>=5.1 in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from Flask>=0.10->flask_sqlalchemy) (
7.1.2)
Requirement already satisfied: Jinja2>=2.10.1 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask>=0.10->flask_sqlalchem
y) (2.11.2)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\purunet\appdata\loca
l\programs\python\python38-32\lib\site-packages (from Jinja2>=2.10.1->Flask>=0.1
0->flask_sqlalchemy) (1.1.1)
Installing collected packages: SQLAlchemy, flask-sqlalchemy
Successfully installed SQLAlchemy-1.3.17 flask-sqlalchemy-2.4.1
==========================================
C:\Users\purunet\Documents\Flask_Blog>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from flaskblog import db
C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fla
sk_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICAT
IONS adds significant overhead and will be disabled by default in the future. S
et it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
>>> db.create_all()
>>> from flaskblog import User, Post
>>> user_1 = User(username='LinuxerHAN', email='LinuxerHAN@gmail.com', password=
'password')
>>> db.session.add(user_1)
>>> user_2 = User(username='SeokDu', email='seokdu@demo.com', password='password
')
>>> db.session.add(user_2)
>>> db.session.commit()
>>> User.query.all()
[User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg'), User('SeokDu', 'seok
du@demo.com', 'default.jpg')]
>>> User.query.first()
User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')
>>> User.query.filter_by(username='LinuxerHAN').all()
[User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')]
>>> User.query.filter_by(username='LinuxerHAN').first()
User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')
>>> user = User.query.filter_by(username='LinuxerHAN').first()
>>> user
User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')
>>> user.id
1
>>> user = User.query.get(1)
>>> user
User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')
>>> user.posts
[]
>>> user.id
1
>>> post_1 = Post(title='Blog 1', content='First Post Content!', user_id=user.id
)
>>> post_2 = Post(title='Blog 2', content='Second Post Content!', user_id=user.i
d)
>>> db.session.add(post_1)
>>> db.session.add(post_2)
>>> db.session.commit()
>>> user.posts
[Post('Blog 1', '2020-05-24 13:22:56.712866'), Post('Blog 2', '2020-05-24 13:22:
56.713867')]
>>> for post in user.posts:
... print(post.title)
...
Blog 1
Blog 2
>>> post = Post.query.first()
>>>
>>> post
Post('Blog 1', '2020-05-24 13:22:56.712866')
>>> post.user_id
1
>>> post.author
User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')
>>> db.drop_all()
>>> db.create_all()
>>> User.query.all()
[]
>>> Post.query.all()
[]
==========================================
C:\Users\purunet\Documents\LinuxerHAN>tree/f
폴더 PATH의 목록입니다.
볼륨 일련 번호가 B273F7ED 840E:6930입니다.
C:.
│ run.py
│
└─flaskblog
│ forms.py
│ models.py
│ routes.py
│ site.db
│ __init__.py
│
├─snippets
│ article.html
│ main.html
│ navigation.html
│
├─static
│ main.css
│
├─templates
│ about.html
│ home.html
│ layout.html
│ login.html
│ register.html
│
└─__pycache__
forms.cpython-38.pyc
models.cpython-38.pyc
routes.cpython-38.pyc
__init__.cpython-38.pyc
==========================================
C:\Users\purunet>pip install flask-bcrypt
Collecting flask-bcrypt
Downloading Flask-Bcrypt-0.7.1.tar.gz (5.1 kB)
Requirement already satisfied: Flask in c:\users\purunet\appdata\local\programs\
python\python38-32\lib\site-packages (from flask-bcrypt) (1.1.2)
Collecting bcrypt
Downloading bcrypt-3.1.7-cp38-cp38-win32.whl (26 kB)
Requirement already satisfied: Jinja2>=2.10.1 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-bcrypt) (2.11.2
)
Requirement already satisfied: click>=5.1 in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from Flask->flask-bcrypt) (7.1.2)
Requirement already satisfied: itsdangerous>=0.24 in c:\users\purunet\appdata\lo
cal\programs\python\python38-32\lib\site-packages (from Flask->flask-bcrypt) (1.
1.0)
Requirement already satisfied: Werkzeug>=0.15 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-bcrypt) (1.0.1)
Collecting cffi>=1.1
Downloading cffi-1.14.0-cp38-cp38-win32.whl (165 kB)
|████████████████████████████████| 165 kB 363 kB/s
Requirement already satisfied: six>=1.4.1 in c:\users\purunet\appdata\roaming\py
thon\python38\site-packages (from bcrypt->flask-bcrypt) (1.14.0)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\purunet\appdata\loca
l\programs\python\python38-32\lib\site-packages (from Jinja2>=2.10.1->Flask->fla
sk-bcrypt) (1.1.1)
Collecting pycparser
Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
|████████████████████████████████| 112 kB 2.2 MB/s
Building wheels for collected packages: flask-bcrypt
Building wheel for flask-bcrypt (setup.py) ... done
Created wheel for flask-bcrypt: filename=Flask_Bcrypt-0.7.1-py3-none-any.whl s
ize=5017 sha256=ca1e80cdcec68bef7a093c1a06048973aa8866a5cf060e97cd27e9b8cd191001
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\8a\d9\0e\
dc762c4ebc76f581397a2e25991db6efd148640b5616ab9210
Successfully built flask-bcrypt
Installing collected packages: pycparser, cffi, bcrypt, flask-bcrypt
Successfully installed bcrypt-3.1.7 cffi-1.14.0 flask-bcrypt-0.7.1 pycparser-2.2
0
==========================================
C:\Users\purunet>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask_bcrypt import Bcrypt
>>> bcrypt = Bcrypt()
>>> bcrypt.generate_password_hash('testing')
b'$2b$12$mFAQ6BDudqfCpZPhMckeROvqlnaEsM5NgfwSeASq8bphdpZcCcpc.'
>>> bcrypt.generate_password_hash('testing').decode('utf-8')
'$2b$12$avJZl7.zrIANX69rakHJTO/rJ8602zoq7VXQVEb3lV8HOPFoaCCba'
>>> bcrypt.generate_password_hash('testing').decode('utf-8')
'$2b$12$pht9HoEEBCpPZdHz35wNpeGuc.27zH2lfJc.bzctjGJs2bqpoITf2'
>>> bcrypt.generate_password_hash('testing').decode('utf-8')
'$2b$12$B2euNgvatxQUY04ewJ5Jt.uk5SixLUoy28nEWnlWxNC67tTWmaVvS'
==========================================
>>> hashed_pw = bcrypt.generate_password_hash('testing').decode('utf-8')
>>> bcrypt.check_password_hash(hashed_pw, 'password')
False
>>> bcrypt.check_password_hash(hashed_pw, 'testing')
True
==========================================
C:\Users\purunet\Documents\LinuxerHAN>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from flaskblog import db
C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fla
sk_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICAT
IONS adds significant overhead and will be disabled by default in the future. S
et it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
>>> from flaskblog.models import User
>>> user = User.query.first()
>>> user
User('LinuxerHAN', 'LinuxerHAN@gmail.com', 'default.jpg')
>>> user.password
'$2b$12$V2KdD62Q6WJ7JBry4xTQveOjgEZjIjtlrj7KKfiwhVhh2nR5RR8CW'
>>>
==========================================
C:\Users\purunet\Documents\LinuxerHAN>pip install flask-login
Collecting flask-login
Downloading Flask_Login-0.5.0-py2.py3-none-any.whl (16 kB)
Requirement already satisfied: Flask in c:\users\purunet\appdata\local\programs\
python\python38-32\lib\site-packages (from flask-login) (1.1.2)
Requirement already satisfied: Werkzeug>=0.15 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-login) (1.0.1)
Requirement already satisfied: click>=5.1 in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from Flask->flask-login) (7.1.2)
Requirement already satisfied: itsdangerous>=0.24 in c:\users\purunet\appdata\lo
cal\programs\python\python38-32\lib\site-packages (from Flask->flask-login) (1.1
.0)
Requirement already satisfied: Jinja2>=2.10.1 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-login) (2.11.2)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\purunet\appdata\loca
l\programs\python\python38-32\lib\site-packages (from Jinja2>=2.10.1->Flask->fla
sk-login) (1.1.1)
Installing collected packages: flask-login
Successfully installed flask-login-0.5.0
==========================================
C:\Users\purunet\Documents\LinuxerHAN>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from flaskblog.models import Post
C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\lib\site-packages\fla
sk_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICAT
IONS adds significant overhead and will be disabled by default in the future. S
et it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
>>> posts = Post.query.all()
>>> for post in posts:
... print(post)
...
Post('My first Post', '2020-05-25 12:42:07.786133')
Post('A Second Post', '2020-05-25 14:07:08.138319')
>>>
==========================================
C:\Users\purunet\Documents\LinuxerHAN>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> s = Serializer('secret', 30)
>>> token = s.dumps({'user_id': 1}).decode('utf-8')
>>> token
'eyJhbGciOiJIUzUxMiIsImlhdCI6MTU5MDQ2MDI1NCwiZXhwIjoxNTkwNDYwMjg0fQ.eyJ1c2VyX2lk
IjoxfQ.jtMEIcCLnCQ42jUPm-AUXl0VR_jds6cdQotZ5Aifk67GuiZmaLnrQtWrpnM6QiqZUxLFeJNjk
9od8VYMk8XCog'
>>> s.loads(token)
{'user_id': 1}
>>> s.loads(token)
Traceback (most recent call last):
File "
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\itsdangerous\jws.py", line 202, in loads
raise SignatureExpired(
itsdangerous.exc.SignatureExpired: Signature expired
>>>
==========================================
C:\Users\purunet\Documents\LinuxerHAN>pip install flask-mail
Collecting flask-mail
Downloading Flask-Mail-0.9.1.tar.gz (45 kB)
|████████████████████████████████| 45 kB 68 kB/s
Requirement already satisfied: Flask in c:\users\purunet\appdata\local\programs\
python\python38-32\lib\site-packages (from flask-mail) (1.1.2)
Collecting blinker
Downloading blinker-1.4.tar.gz (111 kB)
|████████████████████████████████| 111 kB 547 kB/s
Requirement already satisfied: Jinja2>=2.10.1 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-mail) (2.11.2)
Requirement already satisfied: Werkzeug>=0.15 in c:\users\purunet\appdata\local\
programs\python\python38-32\lib\site-packages (from Flask->flask-mail) (1.0.1)
Requirement already satisfied: itsdangerous>=0.24 in c:\users\purunet\appdata\lo
cal\programs\python\python38-32\lib\site-packages (from Flask->flask-mail) (1.1.
0)
Requirement already satisfied: click>=5.1 in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from Flask->flask-mail) (7.1.2)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\purunet\appdata\loca
l\programs\python\python38-32\lib\site-packages (from Jinja2>=2.10.1->Flask->fla
sk-mail) (1.1.1)
Building wheels for collected packages: flask-mail, blinker
Building wheel for flask-mail (setup.py) ... done
Created wheel for flask-mail: filename=Flask_Mail-0.9.1-py3-none-any.whl size=
7573 sha256=1c196c8f11ccb7fab4fb811cebc030397e8c4e28f068f8d8f19785b0f2bfa156
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\98\bc\8c\
34c329e4d7efeaf7b9886db0c76d0b23170e54de443f688e3c
Building wheel for blinker (setup.py) ... done
Created wheel for blinker: filename=blinker-1.4-py3-none-any.whl size=13455 sh
a256=42f9c6ac0dcfe0462b0d67224988cbf450eeb3c70539a14b0182a2697f174104
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\b7\a5\68\
fe632054a5eadd531c7a49d740c50eb6adfbeca822b4eab8d4
Successfully built flask-mail blinker
Installing collected packages: blinker, flask-mail
Successfully installed blinker-1.4 flask-mail-0.9.1
==========================================
C:\Users\purunet\Documents\LinuxerHAN>pip install django
Requirement already satisfied: django in c:\users\purunet\appdata\local\programs
\python\python38-32\lib\site-packages (3.0.6)
Requirement already satisfied: asgiref~=3.2 in c:\users\purunet\appdata\local\pr
ograms\python\python38-32\lib\site-packages (from django) (3.2.7)
Requirement already satisfied: pytz in c:\users\purunet\appdata\local\programs\p
ython\python38-32\lib\site-packages (from django) (2020.1)
Requirement already satisfied: sqlparse>=0.2.2 in c:\users\purunet\appdata\local
\programs\python\python38-32\lib\site-packages (from django) (0.3.1)
C:\Users\purunet\Documents\LinuxerHAN>python -m django --version
3.0.6
==========================================
C:\Users\purunet\Documents\LinuxerHAN>django-admin
Type 'django-admin help
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly conf
igured (error: Requested setting INSTALLED_APPS, but settings are not configured
. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.).
==========================================
C:\Users\purunet\Documents\LinuxerHAN>django-admin startproject django_project
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 28, 2020 - 16:35:27
Django version 3.0.6, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py startapp blog
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>tree/f
폴더 PATH의 목록입니다.
볼륨 일련 번호가 7995090C 840E:6930입니다.
C:.
│ db.sqlite3
│ manage.py
│
├─blog
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ └─migrations
│ __init__.py
│
└─django_project
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
│
└─__pycache__
settings.cpython-38.pyc
urls.cpython-38.pyc
wsgi.cpython-38.pyc
__init__.cpython-38.pyc
==========================================
https://getbootstrap.com/docs/4.0/getting-started/introduction/#starter-template
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py makemigrat
ions
No changes detected
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py createsupe
ruser
Username (leave blank to use 'specialist'): LinuxerHAN
Email address: linuxerhan@gmail.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py makemigrat
ions
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Post
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py sqlmigrate
blog 0001
BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "titl
e" varchar(100) NOT NULL, "content" text NOT NULL, "date_posted" datetime NOT NU
LL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIA
LLY DEFERRED);
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
COMMIT;
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying blog.0001_initial... OK
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py shell
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from blog.models import Post
>>> from django.contrib.auth.models import User
>>> User.objects.all()
>>> User.objects.first()
>>> User.objects.filter(username='LinuxerHAN')
>>> User.objects.filter(username='LinuxerHAN').first()
>>> user = User.objects.filter(username='LinuxerHAN').first()
>>> user
>>> user.id
1
>>> user.pk
1
>>> user = User.objects.get(id=1)
>>> user
>>> Post.objects.all()
>>> post_1 = Post(title='Blog 1', content='First Post Content!', author=user)
>>> Post.objects.all()
>>> post_1.save()
>>> Post.objects.all()
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py shell
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from blog.models import Post
>>> from django.contrib.auth.models import User
>>> Post.objects.all()
>>> user = User.objects.filter(username='LinuxerHAN').first()
>>> user
>>> post_2 = Post(title='Blog 2', content='Second Post Content!', author_id=user
.id)
>>> post_2.save()
>>> Post.objects.all()
>>> post = Post.objects.first()
>>> post.content
'First Post Content!'
>>> post.date_posted
datetime.datetime(2020, 6, 1, 10, 4, 19, 148035, tzinfo=
>>> post.author
>>> post.author.email
'linuxerhan@gmail.com'
>>> user
>>> user.post_set
>>> user.post_set.all()
>>> user.post_set
>>> user.post_set.all()
>>> user.post_set.create(title='Blog 3', content='Third Post Content!')
>>> Post.objects.all()
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project> pip install django-crispy-
forms
Requirement already satisfied: django-crispy-forms in c:\users\purunet\appdata\l
ocal\programs\python\python38-32\lib\site-packages (1.9.1)
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py makemigrat
ions
No changes detected
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions, users
Running migrations:
Applying users.0001_initial... OK
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py shell
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> user = User.objects.filter(username='LinuxerHAN').first()
>>> user
>>> user.profile
>>> user.profile.image
>>> user.profile.image.width
802
>>> user.profile.image.url
'profile_pics/default2.jpg'
>>> user = User.objects.filter(username='SeokDu').first()
>>> user
>>> user.profile.image
==========================================
>>> import json
>>> from blog.models import Post
>>> with open('posts.json') as f:
... posts_json = json.load(f)
...
>>> for post in posts_json:
... post = Post(title=post['title'], content=post['content'], author_id=post
['user_id'])
... post.save()
...
==========================================
C:\Users\purunet\Documents\LinuxerHAN\django_project>python manage.py shell
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.paginator import Paginator
>>> posts = ['1', '2', '3', '4', '5']
>>> p = Paginator(posts, 2)
>>> p.num_pages
3
>>> for page in p.page_range:
... print(page)
...
1
2
3
>>> p1 = p.page(1)
>>> p1
>>> p1.number
1
>>> p1.object_list
['1', '2']
>>> p1.has_previous()
False
>>> p1.has_next()
True
>>> p1.next_page_number()
2
>>> exit()
==========================================
5/27/2020
Python - Set Methods and Operations to Solve Common Problems
** Set Methods and Operations to Solve Common Problems
==========================================
s1 = set([1, 2, 3, 4, 5])
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = set()
print(s1)
---------------------------------
set()
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5, 1, 2, 3}
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.add(6)
print(s1)
---------------------------------
{1, 2, 3, 4, 5, 6}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.update([6, 7, 8])
print(s1)
---------------------------------
{1, 2, 3, 4, 5, 6, 7, 8}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s2 = {7, 8, 9}
s1.update([6, 7, 8], s2)
print(s1)
---------------------------------
{1, 2, 3, 4, 5, 6, 7, 8, 9}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.remove(5)
print(s1)
---------------------------------
{1, 2, 3, 4}
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.remove(6)
print(s1)
---------------------------------
s1.remove(6)
KeyError: 6
---------------------------------
==========================================
s1 = {1, 2, 3, 4, 5}
s1.discard(6)
print(s1)
---------------------------------
{1, 2, 3, 4, 5}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.intersection(s2)
print(s4)
---------------------------------
{2, 3}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.intersection(s2, s3)
print(s4)
---------------------------------
{3}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.difference(s2)
print(s4)
---------------------------------
{1}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s2.difference(s1)
print(s4)
---------------------------------
{4}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s2.difference(s1, s3)
print(s4)
---------------------------------
set()
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s3.difference(s1, s2)
print(s4)
---------------------------------
{5}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s1.symmetric_difference(s2)
print(s4)
---------------------------------
{1, 4}
---------------------------------
==========================================
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {3, 4, 5}
s4 = s2.symmetric_difference(s1)
print(s4)
---------------------------------
{1, 4}
---------------------------------
==========================================
l1 = [1, 2, 3, 1, 2, 3]
l2 = list(set(l1))
print(l2)
---------------------------------
[1, 2, 3]
---------------------------------
==========================================
employees = ['Corey', 'Jim', 'Steven', 'April', 'Judy', 'Jenn', 'John', 'Jane']
gym_members = ['April', 'John', 'Corey']
developers = ['Judy', 'Corey', 'Steven', 'Jane', 'April']
result = set(gym_members).intersection(developers)
print(result)
---------------------------------
{'Corey', 'April'}
---------------------------------
==========================================
employees = ['Corey', 'Jim', 'Steven', 'April', 'Judy', 'Jenn', 'John', 'Jane']
gym_members = ['April', 'John', 'Corey']
developers = ['Judy', 'Corey', 'Steven', 'Jane', 'April']
result = set(employees).difference(gym_members, developers)
print(result)
---------------------------------
{'Jenn', 'Jim'}
---------------------------------
==========================================
employees = ['Corey', 'Jim', 'Steven', 'April', 'Judy', 'Jenn', 'John', 'Jane']
gym_members = ['April', 'John', 'Corey']
developers = ['Judy', 'Corey', 'Steven', 'Jane', 'April']
if 'Corey' in developers:
print('Found!')
# O(n) for list
# O(1) for a set
---------------------------------
Found!
---------------------------------
5/23/2020
Python - Full-Featured Web App Part 1 - Getting Started
** Full-Featured Web App Part 1 - Getting Started
==========================================
C:\Users\purunet>pip install flask
Collecting flask
Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
|████████████████████████████████| 94 kB 154 kB/s
Collecting Jinja2>=2.10.1
Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
|████████████████████████████████| 125 kB 656 kB/s
Collecting Werkzeug>=0.15
Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
|████████████████████████████████| 298 kB 2.2 MB/s
Collecting itsdangerous>=0.24
Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting click>=5.1
Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
|████████████████████████████████| 82 kB 188 kB/s
Collecting MarkupSafe>=0.23
Downloading MarkupSafe-1.1.1-cp38-cp38-win32.whl (16 kB)
Installing collected packages: MarkupSafe, Jinja2, Werkzeug, itsdangerous, click
, flask
Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2
flask-1.1.2 itsdangerous-1.1.0
==========================================
C:\Users\purunet>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> exit()
==========================================
C:\Users\purunet\Documents\Flask_Blog>set FLASK_APP = flaskblog.py
==========================================
C:\Users\purunet\Documents\Flask_Blog>python flaskblog.py
* Serving Flask app "flaskblog" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployme
nt.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 234-451-768
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/May/2020 12:48:56] " [37mGET / HTTP/1.1 [0m" 200 -
127.0.0.1 - - [23/May/2020 12:48:56] " [33mGET /favicon.ico HTTP/1.1 [0m" 404 -
* Detected change in 'C:\\Users\\purunet\\Documents\\Flask_Blog\\flaskblog.py',
reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 234-451-768
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/May/2020 12:49:33] " [37mGET / HTTP/1.1 [0m" 200 -
==========================================
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
---------------------------------
---------------------------------
==========================================
C:\Users\purunet\Documents\Flask_Blog>set FLASK_DEBUG=1
C:\Users\purunet\Documents\Flask_Blog>flask run
* Environment: production
WARNING: This is a development server. Do not use it in a production deployme
nt.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 556-684-567
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/May/2020 14:14:14] " [35m [1mGET / HTTP/1.1 [0m" 500 -
Traceback (most recent call last):
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\Lib\site-pack
ages\flask\cli.py", line 338, in __call__
self._flush_bg_loading_exception()
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\Lib\site-pack
ages\flask\cli.py", line 326, in _flush_bg_loading_exception
reraise(*exc_info)
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\Lib\site-pack
ages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\Lib\site-pack
ages\flask\cli.py", line 314, in _load_app
self._load_unlocked()
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\Lib\site-pack
ages\flask\cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\Lib\site-pack
ages\flask\cli.py", line 398, in load_app
raise NoAppException(
flask.cli.NoAppException: Could not locate a Flask application. You did not prov
ide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was
not found in the current directory.
127.0.0.1 - - [23/May/2020 14:14:14] " [37mGET /?__debugger__=yes&cmd=resource&f
=style.css HTTP/1.1 [0m" 200 -
127.0.0.1 - - [23/May/2020 14:14:14] " [37mGET /?__debugger__=yes&cmd=resource&f
=debugger.js HTTP/1.1 [0m" 200 -
127.0.0.1 - - [23/May/2020 14:14:16] " [37mGET /?__debugger__=yes&cmd=resource&f
=jquery.js HTTP/1.1 [0m" 200 -
127.0.0.1 - - [23/May/2020 14:14:17] " [37mGET /?__debugger__=yes&cmd=resource&f
=console.png HTTP/1.1 [0m" 200 -
127.0.0.1 - - [23/May/2020 14:14:18] " [37mGET /?__debugger__=yes&cmd=resource&f
=ubuntu.ttf HTTP/1.1 [0m" 200 -
127.0.0.1 - - [23/May/2020 14:14:18] " [37mGET /?__debugger__=yes&cmd=resource&f
=console.png HTTP/1.1 [0m" 200 -
---------------------------------
---------------------------------
==========================================
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "
Hello World!
"if __name__ == '__main__':
app.run(debug=True)
---------------------------------
---------------------------------
==========================================
from flask import Flask
app = Flask(__name__)
@app.route("/")
@app.route("/home")
def home():
return "
Home Page
"@app.route("/about")
def about():
return "
About Page
"if __name__ == '__main__':
app.run(debug=True)
---------------------------------