HttpReponse is the class that represents an HTTP response back to the client. The way we used it in this video is the absolute simplest way of generating a response. It's not the most useful tool in the shed, but it's the gateway to all of the other HTTP tools we have.
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (50) NOT NULL,
email VARCHAR (355) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);
CREATE TABLE role(
role_id serial PRIMARY KEY,
role_name VARCHAR (255) UNIQUE NOT NULL
例如CEO 清潔工);
Don't worry about fully understanding this last example yet!
CREATE TABLE account_role
(
user_id integer NOT NULL,
role_id integer NOT NULL,
grant_date timestamp without time zone,
PRIMARY KEY (user_id, role_id),
CONSTRAINT account_role_role_id_fkey FOREIGN KEY (role_id)
REFERENCES role (role_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT account_role_user_id_fkey FOREIGN KEY (user_id)
REFERENCES account (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
# ### Retrieve the Cursor
#
# * A cursor is a control structure that enables traversal over the records in a database. You can think of it as an iterator or pointer for Sql data retrieval.
# In[3]:
cur = conn.cursor()
cursor了之後就可以用
cur.execute(’SELECT * FROM payment’)
之後就可以使用fetch
# ### One line at a time
# In[61]:
cur.fetchone()
# ### Many lines at a time
# In[62]:
cur.fetchmany(5)
這樣可以顯示5 row(以tuple返還)
# ### Or everything at once
# In[63]:
cur.fetchall()
# ## Disconnect from the cursor and database (again)
CharField - A Peewee field that holds onto characters. It's a varchar in SQL terms varchar 的意思是variable character 有所長度,所以必須告訴他我們要儲存多長的東西
unique - Whether the value in the field can be repeated in the table 要看名字是否有重複
IntegerField - A Peewee field that holds an integer
class Meta:
database = db
告訴python目標database是我們一開始設定的db
if __name__ == '__main__':
db.connect()
db.create_tables([Student], safe=True)
.connect() - A database method that connects to the database
.create_tables() - A database method to create the tables for the specified models.
safe - Whether or not to throw errors if the table(s) you're attempting to create already exist
Tries to match a pattern against the beginning of the text.
matching at the beginning of a string, use match.
re.search(pattern, text, flags) -
不只是開頭,而是整個文本第一次出現的地方
Tries to match a pattern anywhere in the text. Returns the first match.
matching somewhere in the string, use search.
re.search(r'string', text )
r'string' - A raw string (尚未說明)
錯誤
What method creates a pointer to a file in Python?
open()
escape
\w matches a Unicode word character. 小寫\w任何字母、不管大小寫、數字還有底線都算
\W 大寫則相反,指不是這些的
\s 空白 tabs, and newlines 也都算
\S 不是空白的東西
\d 0~9的數字
\D 不是數字的東西
\b - matches word boundaries. What's a word boundary? It's the edges of word, defined by white space or the edges of the string.
\B - matches anything that isn't the edges of a word.
[^abc] - a set that will not match, and, in fact, exclude, the letters 'a', 'b', and 'c'.
re.IGNORECASE or re.I - flag to make a search case-insensitive. re.match('A', 'apple', re.I) would find the 'a' in 'apple'.
re.VERBOSE or re.X - flag that allows regular expressions to span multiple lines and contain (ignored) whitespace and comments.
re.MULTILINE or re.M - flag to make a pattern regard lines in your text as the beginning or end of a string.
^ - specifies, in a pattern, the beginning of the string.
$ - specifies, in a pattern, the end of the string.
之所以用re.M是因為我希望一個人的檔案可以作為一行
名字、信箱、電話、工作、推特,這樣作為一行,就要標記
這樣就不會是全部一整行,可以一行一行的出現
line = re.search(r'''
^(?P<name>[-\w ]*,\s[-\w ]+)\t # Last and first names
(?P<email>[-\w\d.+]+@[-\w\d.]+)\t # Email
(?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # Phone
(?P<job>[\w\s]+,\s[\w\s.]+)\t? # Job and company
(?P<twitter>@[\w\d]+)?$ # Twitter
''', data, re.X|re.M)
print(line)
print(line.groupdict())
(?P<name>[abc]) - creates a named group that contains a set for the letters 'a', 'b', and 'c'. This could later be accessed from the Match object as .group('name').
.groups() - method to show all of the groups on a Match object.
re.compile(pattern, flags) - method to pre-compile and save a regular expression pattern, and any associated flags, for later use.
.groupdict() - method to generate a dictionary from a Match object's groups. The keys will be the group names. The values will be the results of the patterns in the group.
re.finditer() - method to generate an iterable from the non-overlapping matches of a regular expression. Very handy for for loops.
.group() - method to access the content of a group. 0 or none is the entire match. 1 through how ever many groups you have will get that group. Or use a group's name to get it if you're using named groups.
line = re.compile(r'''
^(?P<name>(?P<last>[-\w ]*),\s(?P<first>[-\w ]+))\t # Last and first names
(?P<email>[-\w\d.+]+@[-\w\d.]+)\t # Email
(?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # Phone
(?P<job>[\w\s]+,\s[\w\s.]+)\t? # Job and company
(?P<twitter>@[\w\d]+)?$ # Twitter
''', re.X|re.M)
#print(line.search(data).groupdict())
(這樣只會有一行,所以我們如果要全部的都出來,就可以用finditer(),And it gives us back an iterable of each non-overlapping match. We get back a match object, like when we use re.match or re.search.)
print(match.group('name'))
意思是:when you have a match object says, show me whatever is inside of the group.
目標:Create a function named find_words that takes a count and a string. Return a list of all of the words in the string that are count word characters long or longer.