Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python support #1

Merged
merged 8 commits into from Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flake8
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 88
38 changes: 16 additions & 22 deletions .travis.yml
@@ -1,28 +1,22 @@
language: python
cache: pip

python:
- 2.7
- 3.6
- 3.5
- 3.4
- "pypy"
- "pypy3"
matrix:
include:
- python: 3.7
dist: xenial
- python: 3.6
fast_finish: true

sudo: false
install:
- pip install --upgrade pip
- pip install --upgrade black flake8 pytest
- pip install --upgrade -r requirements.txt

script:
- python test_fng.py
- python test_artists.py

after_success:
- pip install pep8 pyflakes
- pep8 *.py
- pyflakes *.py
# Unit tests
- pytest

matrix:
allow_failures:
- python: 3.6
- python: 3.5
- python: 3.4
- python: "pypy"
- python: "pypy3"
# Static analysis
- flake8
- black . --check --diff
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -2,7 +2,8 @@ fng-tools
=========

[![Build Status](https://travis-ci.org/hugovk/fng-tools.svg)](https://travis-ci.org/hugovk/fng-tools)
[![Python: 2.7, 3.4+](https://img.shields.io/badge/python-2.7,_3.4+-blue.svg)](https://www.python.org/downloads/)
[![Python: 3.6+](https://img.shields.io/badge/python-3.6+-blue.svg)](https://www.python.org/downloads/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)

Some tools to do stuff with Finnish National Gallery data.

Expand Down
63 changes: 30 additions & 33 deletions artists.py
Expand Up @@ -7,21 +7,18 @@

try:
import timing
except:
pass


# Windows cmd.exe cannot do Unicode so encode first
def print_it(text):
print(text.encode('utf-8'))
assert timing # silence warnings
except ImportError:
pass


def year_from_date(date):
year = None
if date is None:
return None

match = re.search("\d\d\d\d", date)
match = re.search(r"\d\d\d\d", date)
if match:
year = int(match.group(0))

Expand Down Expand Up @@ -68,43 +65,44 @@ def artist_data_from_xml():
elif grandchild.tag == "{http://purl.org/dc/elements/1.1/}title":
# print(1, grandchild.tag)
# print(2, grandchild.attrib)
# print_it("3 " + grandchild.text)
# print("3 " + grandchild.text)
name = grandchild.text

elif grandchild.tag == "{http://purl.org/dc/elements/1.1/}date":
if grandchild.attrib['type'] == 'birth':
if grandchild.attrib["type"] == "birth":
birthdate = grandchild.text
if 'loc' in grandchild.attrib:
birthplace = grandchild.attrib['loc']
if grandchild.attrib['type'] == 'death':
if "loc" in grandchild.attrib:
birthplace = grandchild.attrib["loc"]
if grandchild.attrib["type"] == "death":
deathdate = grandchild.text
if 'loc' in grandchild.attrib:
deathplace = grandchild.attrib['loc']
if "loc" in grandchild.attrib:
deathplace = grandchild.attrib["loc"]

# {http://purl.org/dc/elements/1.1/}date
# {'loc': 'Tampere', 'type': 'birth'}
# 1907-03-08
# {http://purl.org/dc/elements/1.1/}date
# {'loc': 'Tampere', 'type': 'birth'}
# 1907-03-08

# {http://purl.org/dc/elements/1.1/}date
# {'loc': 'Tampere', 'type': 'death'}
# 1999-11-18
# {http://purl.org/dc/elements/1.1/}date
# {'loc': 'Tampere', 'type': 'death'}
# 1999-11-18

if artist:

birth_year = year_from_date(birthdate)
death_year = year_from_date(deathdate)

# Skip bad data
if ((name == "Tampere") or
(name == "Milano") or
(name == "Moskova, Venäjä") or
(death_year < birth_year) or
(birthdate == deathdate) or
(birth_year == death_year) or
(birth_year == 180 and death_year == 1682) or
(birthdate == "(1100 - 1874)" and
deathdate == "(1100 - 1989)") or
(birthdate == "(1000 - 1916)")):
if (
(name == "Tampere")
or (name == "Milano")
or (name == "Moskova, Venäjä")
or (birth_year and death_year and death_year < birth_year)
or (birthdate == deathdate)
or (birth_year == death_year)
or (birth_year == 180 and death_year == 1682)
or (birthdate == "(1100 - 1874)" and deathdate == "(1100 - 1989)")
or (birthdate == "(1000 - 1916)")
):
continue
if death_year and birth_year:
if death_year - birth_year > 150:
Expand All @@ -121,8 +119,7 @@ def artist_data_from_xml():
if death_year > max_death_year:
max_death_year = death_year

artists.append(
[name, birthdate, birthplace, deathdate, deathplace])
artists.append([name, birthdate, birthplace, deathdate, deathplace])

print(min_birth_year)
print(max_birth_year)
Expand All @@ -132,7 +129,7 @@ def artist_data_from_xml():
return artists


if __name__ == '__main__':
if __name__ == "__main__":

artists = artist_data_from_xml()

Expand Down
16 changes: 5 additions & 11 deletions fng-artist-counts.py
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Show top 25 artists represented in the FNG collection.
Expand Down Expand Up @@ -34,23 +34,17 @@
25. Schjerfbeck, Helene (201)

"""
from __future__ import print_function
from __future__ import unicode_literals
from collections import Counter
from xml.etree.cElementTree import parse

try:
import timing

assert timing # silence warnings
except:
except ImportError:
pass


# Windows cmd.exe cannot do Unicode so encode first
def print_it(text):
print(text.encode('utf-8'))


def get_artists_from_xml():
"""Return list of all artists"""
filename = "fng-data-dc.xml"
Expand Down Expand Up @@ -98,12 +92,12 @@ def most_frequent_with_counts(some_list, number=None):
return most_common


if __name__ == '__main__':
if __name__ == "__main__":

artists = get_artists_from_xml()
print("Found", len(artists), "artworks with artists")
top = most_frequent_with_counts(artists, 25)
for i, (artist, count) in enumerate(top):
print_it(str(i+1) + ". " + artist + " (" + str(count) + ")")
print(str(i + 1) + ". " + artist + " (" + str(count) + ")")

# End of file
20 changes: 11 additions & 9 deletions fng.py
@@ -1,12 +1,13 @@
#!/usr/bin/env python
from __future__ import print_function
#!/usr/bin/env python3
import re
from PIL import Image, ImageDraw
from xml.etree.cElementTree import parse

try:
import timing
except:

assert timing # silence warnings
except ImportError:
pass


Expand All @@ -25,17 +26,17 @@ def get_cm(text):
text = text.replace(",", ".")

# Try cm
found = re.findall("(\d+\.\d+) cm", text)
found = re.findall(r"(\d+\.\d+) cm", text)
if len(found):
cm = float(found[0])
else:
# Try mm
found = re.findall("(\d+\.\d+) mm", text)
found = re.findall(r"(\d+\.\d+) mm", text)
if len(found):
cm = float(found[0]) / 10
else:
# Try m
found = re.findall("(\d+\.\d+) m", text)
found = re.findall(r"(\d+\.\d+) m", text)
if len(found):
cm = float(found[0]) * 100
# Avoid float('4.1') * 100 == 409.99999999999994
Expand Down Expand Up @@ -77,7 +78,7 @@ def get_sizes_from_xml():
# print grandchild.tag
# print grandchild.attrib
# print grandchild.text
if grandchild.attrib == {'type': 'dimension'}:
if grandchild.attrib == {"type": "dimension"}:
if grandchild.text.startswith("leveys"):
width = grandchild.text
elif grandchild.text.startswith("korkeus"):
Expand Down Expand Up @@ -108,6 +109,7 @@ def stats(sizes):

try:
from collections import Counter

data = Counter(widths)
w_mode = data.most_common(1)[0][0]
data = Counter(heights)
Expand All @@ -131,7 +133,7 @@ def centred(w, h, big_size):
def plot_sizes(sizes, max_w, max_h):
"""sizes is a list of (width, height)"""
print("Plot sizes")
im = Image.new('RGB', (int(max_w * 1.1), int(max_h * 1.1)), "white")
im = Image.new("RGB", (int(max_w * 1.1), int(max_h * 1.1)), "white")
draw = ImageDraw.Draw(im)

for (w, h) in sizes:
Expand All @@ -140,7 +142,7 @@ def plot_sizes(sizes, max_w, max_h):
im.save("out.png")


if __name__ == '__main__':
if __name__ == "__main__":

sizes, max_w, max_h = get_sizes_from_xml()

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
@@ -0,0 +1,2 @@
[tool.black]
py36 = true
12 changes: 5 additions & 7 deletions test_artists.py
@@ -1,29 +1,27 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit tests for artists.py
"""
from __future__ import unicode_literals
import unittest
import artists


class TestIt(unittest.TestCase):

def test_1(self):
date = '1895-04-02'
date = "1895-04-02"
year = artists.year_from_date(date)
self.assertEqual(year, 1895)

def test_2(self):
date = '1699'
date = "1699"
year = artists.year_from_date(date)
self.assertEqual(year, 1699)

def test_3(self):
date = None
year = artists.year_from_date(date)
self.assertEqual(year, None)
self.assertIsNone(year)

def test_4(self):
date = "1741 (jälkeen)"
Expand All @@ -46,7 +44,7 @@ def test_7(self):
self.assertEqual(year, 1630)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

# End of file
17 changes: 8 additions & 9 deletions test_fng.py
@@ -1,26 +1,24 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Unit tests for fng.py
"""
from __future__ import unicode_literals
import unittest
import fng


class TestIt(unittest.TestCase):

def test_1(self):
text = 'leveys 40,00 cm'
text = "leveys 40,00 cm"
cm = fng.get_cm(text)
self.assertEqual(cm, 40.0)

def test_2(self):
text = 'korkeus 41,50 cm'
text = "korkeus 41,50 cm"
cm = fng.get_cm(text)
self.assertEqual(cm, 41.5)

def test_3(self):
text = 'leveys p\xe4iv\xe4mitta 30,00 cm'
text = "leveys p\xe4iv\xe4mitta 30,00 cm"
cm = fng.get_cm(text)
self.assertEqual(cm, 30.0)

Expand All @@ -35,7 +33,7 @@ def test_5(self):
self.assertEqual(cm, 7.5)

def test_6(self):
text = 'korkeus 72,50 cm'
text = "korkeus 72,50 cm"
cm = fng.get_cm(text)
self.assertEqual(cm, 72.5)

Expand All @@ -59,5 +57,6 @@ def test_10(self):
cm = fng.get_cm(text)
self.assertEqual(cm, 410.0)

if __name__ == '__main__':
unittest.main()

if __name__ == "__main__":
unittest.main()