Python: Difference between revisions
From Leo's Notes
This page was last edited on 21 January 2020, at 04:34.
m Text replacement - "</code>" to "}}" |
mNo edit summary |
||
| Line 1: | Line 1: | ||
Random notes related to Python. | |||
== spaCy == | |||
{{highlight|lang=terminal|code= | |||
# pip install spacy | |||
}} | |||
Example code: | |||
{{highlight|lang=python|code= | |||
import spacy | |||
# Load English tokenizer, tagger, parser, NER and word vectors | |||
nlp = spacy.load("en_core_web_sm") | |||
# Process whole documents | |||
text = ("When Sebastian Thrun started working on self-driving cars at " | |||
"Google in 2007, few people outside of the company took him " | |||
"seriously. “I can tell you very senior CEOs of major American " | |||
"car companies would shake my hand and turn away because I wasn’t " | |||
"worth talking to,” said Thrun, in an interview with Recode earlier " | |||
"this week.") | |||
doc = nlp(text) | |||
# Analyze syntax | |||
print("Noun phrases:", [chunk.text for chunk in doc.noun_chunks]) | |||
print("Verbs:", [token.lemma_ for token in doc if token.pos_ == "VERB"]) | |||
# Find named entities, phrases and concepts | |||
for entity in doc.ents: | |||
print(entity.text, entity.label_) | |||
}} | |||
== No module named ez_setup == | == No module named ez_setup == | ||
While trying to install a package, you get: | While trying to install a package, you get: | ||
{{highlight|lang=text|code= | |||
Traceback (most recent call last): | |||
File "setup.py", line 12, in <module> | |||
from ez_setup import use_setuptools | |||
ImportError: No module named ez_setup | |||
}} | |||
On CentOS (or other RHEL derivatives), run: | On CentOS (or other RHEL derivatives), run: | ||
{{highlight|lang=terminal|code | |||
# yum install python-setuptools | |||
}} | |||
On other platforms, it's probably named {{code|python-distutils}} | On other platforms, it's probably named {{code|python-distutils}} | ||
== MySQL == | == MySQL == | ||
To install the MySQL connector: | To install the MySQL connector: | ||
{{highlight|lang=terminal|code= | |||
# yum install MySQL-python | |||
}} | |||
To use it, import {{code|_mysql}}, or you can use the {{code|MySQLdb}} wrapper. Basically, to fetch something, we can now do: | To use it, import {{code|_mysql}}, or you can use the {{code|MySQLdb}} wrapper. Basically, to fetch something, we can now do: | ||
{{highlight|lang=python|code= | |||
import MySQLdb as mdb # Wraps _mysql | |||
import sys | |||
con = mdb.connect("search", "lyricslive", "lyricslive", "lyricslive") | |||
with con: | |||
cur = con.cursor(mdb.cursors.DictCursor) | |||
cur.execute("SELECT * FROM lyric LIMIT 1") | |||
results = cur.fetchall() | |||
for record in results: | |||
print record["lyric"] | |||
}} | |||
The above is analogous to {{code|<?php $result = mysql_fetch_array($handle); ?>}}. If we take out {{code|mdb.cursors.DictCursor}}, the records will not be in a dictionary but rather fields. | The above is analogous to {{code|1=<?php $result = mysql_fetch_array($handle); ?>}}. If we take out {{code|mdb.cursors.DictCursor}}, the records will not be in a dictionary but rather fields. | ||
[[Category:Code]] | [[Category:Code]] | ||
Revision as of 04:34, 21 January 2020
Random notes related to Python.
spaCy
# pip install spacy
Example code:
import spacy
# Load English tokenizer, tagger, parser, NER and word vectors
nlp = spacy.load("en_core_web_sm")
# Process whole documents
text = ("When Sebastian Thrun started working on self-driving cars at "
"Google in 2007, few people outside of the company took him "
"seriously. “I can tell you very senior CEOs of major American "
"car companies would shake my hand and turn away because I wasn’t "
"worth talking to,” said Thrun, in an interview with Recode earlier "
"this week.")
doc = nlp(text)
# Analyze syntax
print("Noun phrases:", [chunk.text for chunk in doc.noun_chunks])
print("Verbs:", [token.lemma_ for token in doc if token.pos_ == "VERB"])
# Find named entities, phrases and concepts
for entity in doc.ents:
print(entity.text, entity.label_)
No module named ez_setup
While trying to install a package, you get:
Traceback (most recent call last):
File "setup.py", line 12, in <module>
from ez_setup import use_setuptools
ImportError: No module named ez_setup
On CentOS (or other RHEL derivatives), run:
code
# yum install python-setuptools
On other platforms, it's probably named python-distutils
MySQL
To install the MySQL connector:
# yum install MySQL-python
To use it, import _mysql, or you can use the MySQLdb wrapper. Basically, to fetch something, we can now do:
import MySQLdb as mdb # Wraps _mysql
import sys
con = mdb.connect("search", "lyricslive", "lyricslive", "lyricslive")
with con:
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM lyric LIMIT 1")
results = cur.fetchall()
for record in results:
print record["lyric"]
The above is analogous to <?php $result = mysql_fetch_array($handle); ?>. If we take out mdb.cursors.DictCursor, the records will not be in a dictionary but rather fields.