Skip to content

Commit

Permalink
examples: fix employee example
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuDartiailh committed Nov 13, 2023
1 parent 223e291 commit e48c578
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions examples/tutorial/employee/employee.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2023, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
from __future__ import print_function

import datetime
Expand All @@ -14,12 +14,13 @@
from enaml.qt.qt_application import QtApplication

import sys, os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))


class Person(Atom):
""" A simple class representing a person object.
"""A simple class representing a person object."""

"""
last_name = Str()

first_name = Str()
Expand All @@ -30,48 +31,45 @@ class Person(Atom):

debug = Bool(False)

@observe('age')
@observe("age")
def debug_print(self, change):
""" Prints out a debug message whenever the person's age changes.
"""
"""Prints out a debug message whenever the person's age changes."""
if self.debug:
templ = "{first} {last} is {age} years old."
s = templ.format(
first=self.first_name, last=self.last_name, age=self.age,
first=self.first_name,
last=self.last_name,
age=self.age,
)
print(s)

@observe('dob')
@observe("dob")
def update_age(self, change):
""" Update the person's age whenever their date of birth changes
"""
"""Update the person's age whenever their date of birth changes"""
# grab the current date time
now = datetime.datetime.now(datetime.UTC)
now = datetime.datetime.now()
# estimate the person's age within one year accuracy
age = now.year - self.dob.year
# check to see if the current date is before their birthday and
# subtract a year from their age if it is
if ((now.month == self.dob.month and now.day < self.dob.day)
or now.month < self.dob.month):
age -= 1
if (
now.month == self.dob.month and now.day < self.dob.day
) or now.month < self.dob.month:
age -= 1
# set the persons age
self.age = age


class Employer(Person):
""" An employer is a person who runs a company.
"""An employer is a person who runs a company."""

"""
# The name of the company
company_name = Str()


class Employee(Person):
""" An employee is person with a boss and a phone number.
"""An employee is person with a boss and a phone number."""

"""
# The employee's boss
boss = Value(Employer)

Expand All @@ -81,16 +79,20 @@ class Employee(Person):
# This method will be called automatically by atom when the
# employee's phone number changes
def _observe_phone(self, val):
print('received new phone number for %s: %s' % (self.first_name, val))
print("received new phone number for %s: %s" % (self.first_name, val))


def main():
# Create an employee with a boss
boss_john = Employer(
first_name='John', last_name='Paw', company_name="Packrat's Cats",
first_name="John",
last_name="Paw",
company_name="Packrat's Cats",
)
employee_mary = Employee(
first_name='Mary', last_name='Sue', boss=boss_john,
first_name="Mary",
last_name="Sue",
boss=boss_john,
phone=(555, 555, 5555),
)

Expand All @@ -105,5 +107,6 @@ def main():

app.start()

if __name__ == '__main__':

if __name__ == "__main__":
main()

0 comments on commit e48c578

Please sign in to comment.