Python: Difference between revisions
mNo edit summary |
|||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Random notes related to Python. | |||
== pip == | |||
If you use pip as root, you should set the umask to {{code|022}} so that the packages that are installed {{code|/usr/lib64/python2.7/site-packages}} for instance are usable by other users on the system. | |||
To reinstall a package with pip: | |||
{{highlight|lang=terminal|code= | |||
# pip install --upgrade --force-reinstall <package> | |||
}} | |||
== spaCy == | |||
To install SpaCy and the English model: | |||
{{highlight|lang=terminal|code= | |||
# pip install spacy | |||
# python -m spacy download en_core_web_sm | |||
}} | |||
Additional models can be found at: https://github.com/explosion/spacy-models/releases. | |||
Example code: | |||
{{highlight|lang=python|code= | |||
#!/usr/bin/python | |||
# -*- coding: latin-1 -*- | |||
# If using python 2, the following is required. | |||
from __future__ import unicode_literals | |||
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_) | |||
}} | |||
== Dates and Times == | |||
{{highlight|lang=python|code= | |||
import datetime | |||
today = datetime.date.today() | |||
# returns datetime.date(2020, 1, 22) | |||
datetime.date.today() | |||
# returns 2020-01-22 | |||
today.strftime("%Y-%m-%d") | |||
# returns tomorrow's date | |||
# datetime.timedelta(day, seconds, ...) | |||
(today + datetime.timedelta(1)).strftime("%Y-%m-%d") | |||
}} | |||
== 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 | On other platforms, it's probably named {{code|python-distutils}} | ||
== MySQL == | |||
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: | ||
{{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 | 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]] | ||
Latest revision as of 21:15, 22 January 2020
Random notes related to Python.
pip
If you use pip as root, you should set the umask to 022 so that the packages that are installed /usr/lib64/python2.7/site-packages for instance are usable by other users on the system.
To reinstall a package with pip:
# pip install --upgrade --force-reinstall <package>
spaCy
To install SpaCy and the English model:
# pip install spacy
# python -m spacy download en_core_web_sm
Additional models can be found at: https://github.com/explosion/spacy-models/releases.
Example code:
#!/usr/bin/python
# -*- coding: latin-1 -*-
# If using python 2, the following is required.
from __future__ import unicode_literals
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_)
Dates and Times
import datetime
today = datetime.date.today()
# returns datetime.date(2020, 1, 22)
datetime.date.today()
# returns 2020-01-22
today.strftime("%Y-%m-%d")
# returns tomorrow's date
# datetime.timedelta(day, seconds, ...)
(today + datetime.timedelta(1)).strftime("%Y-%m-%d")
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.