Skip to content

Instantly share code, notes, and snippets.

@omaciel
Created May 11, 2017 21:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save omaciel/576850cd478229ac2d4d8bf098cfc181 to your computer and use it in GitHub Desktop.
Save omaciel/576850cd478229ac2d4d8bf098cfc181 to your computer and use it in GitHub Desktop.
Some simple code to show the differences between using namedtuple from the collections module and NamedTuple from the typing module. Extra points for using type hinting.

Playing with named tuples

Some simple code to show the differences between using namedtuple from the collections module and NamedTuple from the typing module. Extra points for using type hinting.

Using namedtuple from the collections module:

>>> from platforms_namedtuple_35 import *
>>> system_1 = RHEL7
>>> system_2 = RHEL5
>>> print(system_1)
OperatingSystem(distribution_id='Maipo', distribution_name='Red Hat Enterprise Linux Server', distribution_version='7.3', kernel_name='3.10.0-514
.el7', lsb_dist_id=' RedHatEnterpriseServer', manufacturer='Red Hat', operating_system='RedHat', os_family='Linux')
>>> print(system_2)
OperatingSystem(distribution_id='Tikanga', distribution_name='Red Hat Enterprise Linux Server', distribution_version='5.11', kernel_name='2.6.18-
398.el5', lsb_dist_id=' RedHatEnterpriseServer', manufacturer='Red Hat', operating_system='RedHat', os_family='Linux')
>>> print(system_1.kernel_name)
3.10.0-514.el7
>>> print(system_2.kernel_name)
2.6.18-398.el5
>>> print(system_1.to_json())
{
    "distribution_id": "Maipo",
    "distribution_name": "Red Hat Enterprise Linux Server",
    "distribution_version": "7.3",
    "kernel_name": "3.10.0-514.el7",
    "lsb_dist_id": " RedHatEnterpriseServer",
    "manufacturer": "Red Hat",
    "operating_system": "RedHat",
    "os_family": "Linux"
}

Using typing.NamedTuple module (requires Python >= 3.6.1 to take advantage of default values):

>>> from platforms_namedtuple_36 import *
>>> system_1 = RHEL7()
>>> system_2 = RHEL5()
>>> print(system_1)
<OperatingSystem: Red Hat Enterprise Linux Server release 7.3 (Maipo)>
>>> print(system_2)
<OperatingSystem: Red Hat Enterprise Linux Server release 5.11 (Tikanga)>
>>> print(system_1.kernel_name)
3.10.0-514.el7
>>> print(system_2.kernel_name)
2.6.18-398.el5
>>> print(system_1.to_json())
{
    "distribution_id": "Maipo",
    "distribution_name": "Red Hat Enterprise Linux Server",
    "distribution_version": "7.3",
    "kernel_name": "3.10.0-514.el7",
    "lsb_dist_id": "RedHatEnterpriseServer",
    "manufacturer": "Red Hat",
    "operating_system": "RedHat",
    "os_family": "Linux"
}
"""Objects representing operating systems."""
import json
from collections import namedtuple
PLATFORM_FIELDS = (
'distribution_id', # Pensacola
'distribution_name', # Red Hat Enterprise Linux Server
'distribution_version', # 2.7
'kernel_name', # 2.4.9-e.57.el2
'lsb_dist_id', # RedHatEnterpriseServer
'manufacturer', # Red Hat
'operating_system', # RedHat
'os_family', # Linux
)
class OperatingSystem(namedtuple('OperatingSystem', PLATFORM_FIELDS)):
"""Represents a basic operating system"""
# This helps keep memory requirements low by preventing the creation
# of instance dictionaries.
__slots__ = ()
def to_json(self):
"""Returns JSON representations for class."""
return json.dumps(self._asdict(), indent=4, sort_keys=True)
RHEL2 = OperatingSystem(
distribution_id='Pensacola',
distribution_name='Red Hat Enterprise Linux Server',
distribution_version='2.7',
kernel_name='2.4.9-e.57.el2',
lsb_dist_id=' RedHatEnterpriseServer',
manufacturer='Red Hat',
operating_system='RedHat',
os_family='Linux',
)
RHEL3 = OperatingSystem(
distribution_id='Taroon',
distribution_name='Red Hat Enterprise Linux Server',
distribution_version='3.9',
kernel_name='2.4.21-50.el3',
lsb_dist_id=' RedHatEnterpriseServer',
manufacturer='Red Hat',
operating_system='RedHat',
os_family='Linux',
)
RHEL4 = OperatingSystem(
distribution_id='Nahant',
distribution_name='Red Hat Enterprise Linux Server',
distribution_version='4.9',
kernel_name='2.6.9-100.el4',
lsb_dist_id=' RedHatEnterpriseServer',
manufacturer='Red Hat',
operating_system='RedHat',
os_family='Linux',
)
RHEL5 = OperatingSystem(
distribution_id='Tikanga',
distribution_name='Red Hat Enterprise Linux Server',
distribution_version='5.11',
kernel_name='2.6.18-398.el5',
lsb_dist_id=' RedHatEnterpriseServer',
manufacturer='Red Hat',
operating_system='RedHat',
os_family='Linux',
)
RHEL6 = OperatingSystem(
distribution_id='Santiago',
distribution_name='Red Hat Enterprise Linux Server',
distribution_version='6.9',
kernel_name='2.6.32-696.el6',
lsb_dist_id=' RedHatEnterpriseServer',
manufacturer='Red Hat',
operating_system='RedHat',
os_family='Linux',
)
RHEL7 = OperatingSystem(
distribution_id='Maipo',
distribution_name='Red Hat Enterprise Linux Server',
distribution_version='7.3',
kernel_name='3.10.0-514.el7',
lsb_dist_id=' RedHatEnterpriseServer',
manufacturer='Red Hat',
operating_system='RedHat',
os_family='Linux',
)
"""Objects representing operating systems."""
import json
from typing import NamedTuple
class RHEL(NamedTuple):
"""Represents a basic Red Hat Enterprise Server system"""
distribution_id: str = 'Maipo'
distribution_version: str = '7.3'
kernel_name: str = '3.10.0-514.el7'
distribution_name: str = 'Red Hat Enterprise Linux Server'
lsb_dist_id: str = 'RedHatEnterpriseServer'
manufacturer: str = 'Red Hat'
operating_system: str = 'RedHat'
os_family: str = 'Linux'
def __repr__(self) -> str:
return '<OperatingSystem: {name} release {version} ({id})>'.format(
name=self.distribution_name,
version=self.distribution_version,
id=self.distribution_id
)
def to_json(self) -> str:
"""Returns JSON representations for class."""
return json.dumps(self._asdict(), indent=4, sort_keys=True)
class RHEL2(RHEL):
"""Represents a RHEL 2 system."""
distribution_id: str = 'Pensacola'
distribution_version: str = '2.7'
kernel_name: str = '2.4.9-e.57.el2'
class RHEL3(RHEL):
"""Represents a RHEL 3 system."""
distribution_id: str = 'Taroon'
distribution_version: str = '3.9'
kernel_name: str = '2.4.21-50.el3'
class RHEL4(RHEL):
"""Represents a RHEL 4 system."""
distribution_id: str = 'Nahant'
distribution_version: str = '4.9'
kernel_name: str = '2.6.9-100.el4'
class RHEL5(RHEL):
"""Represents a RHEL 5 system."""
distribution_id: str = 'Tikanga'
distribution_version: str = '5.11'
kernel_name: str = '2.6.18-398.el5'
class RHEL6(RHEL):
"""Represents a RHEL 6 system."""
distribution_id: str = 'Santiago'
distribution_version: str = '6.9'
kernel_name: str = '2.6.32-696.el6'
class RHEL7(RHEL):
"""Represents a RHEL 7 system."""
distribution_id: str = 'Maipo'
distribution_version: str = '7.3'
kernel_name: str = '3.10.0-514.el7'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment