Python

From Leo's Notes
Last edited on 22 January 2020, at 21:15.

Random notes related to Python.

pip[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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.