컴퓨터/Python

생활코딩23~25강

풍경소리^^ 2019. 1. 27. 12:00

==========23.문법 함수 1/2

def average():
    a=1
    b=2
    c=3
    s=a+b+c
    r=s/3
    print(r)
average()

==========24.문법 함수 2/2

#매개변수parameter
def average(a,b,c):
    s=a+b+c
    r=s/3
    print(r)
#인자argument
average(10,20,30)

==========25.활용-함수

refactoring

----------index.py

def getList():
    files=os.listdir('data')
    #print(files)
    listStr=''
    for item in files:
        listStr=listStr+'<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item)
    #print(listStr)
    return listStr

-----

'''.format(title=pageId, desc=description,listStr=getList(),update_link=update_link,delete_action=delete_action))#query string
----------

index.py개선

update.py개선

create.py개선

----------코드정리indext.py

#!python
print("Content-Type: text/html\n")
import cgi, os

def getList():
    files=os.listdir('data')
    #print(files)
    listStr=''
    for item in files:
        listStr=listStr+'<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item)
    #print(listStr)
    return listStr
   
form = cgi.FieldStorage()
if 'id' in form:
    pageId=form["id"].value
    description=open('data/'+pageId,'r').read()
else:
    pageId='Welcome'
    description='Hello, WEB'
print('''
<!doctype html>
<html>
<head>
    <title>WEB1 - Welcome</title>
    <meta charset="utf-8">
</head>
<body>
    <h1><a href="index.py">WEB</a></h1>
    <ol>
        {listStr}
    </ol>
    <form action="process_create.py" method="post">
        <a href="create.py">creat</a>
        <p><input type="text" name="title" placeholder="title"></p>
        <p><textarea rows="4" name="description" placeholder="description"></textarea></p>
        <p><input type="submit"</p>
    </form>
</body>
</html>
'''.format(title=pageId, desc=description,listStr=getList()))#query string
----------코드정리create.py

#!python
print("Content-Type: text/html\n")
import cgi, os

def getList():
    files=os.listdir('data')
    #print(files)
    listStr=''
    for item in files:
        listStr=listStr+'<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item)
    #print(listStr)
    return listStr
   
form = cgi.FieldStorage()
if 'id' in form:
    pageId=form["id"].value
    description=open('data/'+pageId,'r').read()
else:
    pageId='Welcome'
    description='Hello, WEB'
print('''
<!doctype html>
<html>
<head>
    <title>WEB1 - Welcome</title>
    <meta charset="utf-8">
</head>
<body>
    <h1><a href="index.py">WEB</a></h1>
    <ol>
        {listStr}
    </ol>
    <form action="process_create.py" method="post">
        <a href="create.py">creat</a>
        <p><input type="text" name="title" placeholder="title"></p>
        <p><textarea rows="4" name="description" placeholder="description"></textarea></p>
        <p><input type="submit"</p>
    </form>
</body>
</html>
'''.format(title=pageId, desc=description,listStr=getList()))#query string
----------코드정리update.py

#!python
print("Content-Type: text/html\n")
import cgi, os

def getList():
    files=os.listdir('data')
    #print(files)
    listStr=''
    for item in files:
        listStr=listStr+'<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item)
    #print(listStr)
    return listStr

form = cgi.FieldStorage()
if 'id' in form:
    pageId=form["id"].value
    description=open('data/'+pageId,'r').read()
else:
    pageId='Welcome'
    description='Hello, WEB'
print('''
<!doctype html>
<html>
<head>
    <title>WEB1 - Welcome</title>
    <meta charset="utf-8">
</head>
<body>
    <h1><a href="index.py">WEB</a></h1>
    <ol>
        {listStr}
    </ol>
    <a href="create.py">creat</a>
    <form action="process_update.py" method="post">
        <input type="hidden" name="pageId" value="{form_default_title}">
        <p><input type="text" name="title" placeholder="title" value="{form_default_title}"></p>
        <p><textarea rows="4" name="description" placeholder="description">{form_default_description}</textarea></p>
        <p><input type="submit"</p>
    </form>
</body>
</html>
'''.format(title=pageId, desc=description,listStr=getList(),form_default_title=pageId,form_default_description=description))#query string

'컴퓨터 > Python' 카테고리의 다른 글

생활코딩28강   (0) 2019.01.27
생활코딩26~27강  (0) 2019.01.27
생활코딩19~22강  (0) 2019.01.27
생활코딩1~18강  (0) 2019.01.26
미운오리새끼-김왼손의 왼손코딩  (0) 2019.01.10