class Employee ( HasTraits ): name = Str( '<unknown>' ) title = Str phone = Regex( regex = r'\d\d\d-\d\d\d\d' ) def default_title ( self ): self.title = 'Senior Engineer'
class Employee(HasTraits): """ Defines a company employee. """ name = Str('<unknown>') title = Str() phone = Regex(regex=r'\d\d\d-\d\d\d\d') def default_title(self): self.title = 'Senior Engineer'
class Person(HasStrictTraits): # Trait definitions: name = Str age = Int phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') # Traits view definition: traits_view = View('name', 'age', 'phone', width=0.18, buttons=['OK', 'Cancel'])
class Employee(HasTraits): name = Str age = Int gender = Enum('Male', 'Female') phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') traits_view = View('name', 'age', 'phone', title='Create new employee', width=0.18, buttons=['OK', 'Cancel'])
class Employee(HasTraits): first_name = Str last_name = Str age = Int phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') traits_view = View( 'first_name', 'last_name', 'age', 'phone', title='Create new employee', width=0.18, buttons=['OK', 'Cancel'] )
class Person(HasStrictTraits): # ------------------------------------------------------------------------- # Trait definitions: # ------------------------------------------------------------------------- name = Str() age = Int() phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') # ------------------------------------------------------------------------- # Traits view definition: # ------------------------------------------------------------------------- traits_view = View('name', 'age', 'phone')
class Person(HasStrictTraits): #------------------------------------------------------------------------- # Trait definitions: #------------------------------------------------------------------------- name = Str() age = Int() phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') #------------------------------------------------------------------------- # Traits view definition: #------------------------------------------------------------------------- traits_view = View('name', 'age', 'phone', title='Create new person', width=0.18, buttons=['OK', 'Cancel'])
class Person(Handler): # ------------------------------------------------------------------------- # Trait definitions: # ------------------------------------------------------------------------- name = Str() age = Int() phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') notes = Str() # ------------------------------------------------------------------------- # Handles the 'Annoy' button being clicked: # ------------------------------------------------------------------------- def _annoy_clicked(self, info): self.edit_traits( view=View(title='Annoying', kind='modal', buttons=['OK']))
class DirectoryNode(FileSpaceRootNode): #--------------------------------------------------------------------------- # Trait definitions: #--------------------------------------------------------------------------- # The user specified name of the directory: dir_name = Regex('New Folder', r'[A-Za-z.#@$%-_~]+') #--------------------------------------------------------------------------- # Traits view definitions: #--------------------------------------------------------------------------- view = View('dir_name{Name}', title='Create New Folder', buttons=['OK', 'Cancel'], width=300) #--------------------------------------------------------------------------- # Gets the label to display for a specified object: #--------------------------------------------------------------------------- def tno_get_label(self, node): """ Gets the label to display for a specified object. """ return basename(self.path) #--------------------------------------------------------------------------- # Sets the label for a specified node: #--------------------------------------------------------------------------- def tno_set_label(self, node, label): """ Sets the label for a specified object. """ old_name = self.path dir_name = dirname(old_name) new_name = join(dir_name, label) try: rename(old_name, new_name) self.path = new_name except: error(self.handler.parent, ("An error occurred while trying to " "rename '%s' to '%s'") % (basename(old_name), label)) #--------------------------------------------------------------------------- # Confirms that a specified object can be deleted or not: # Result = True: Delete object with no further prompting # = False: Do not delete object # = other: Take default action (may prompt user to confirm delete) #--------------------------------------------------------------------------- def tno_confirm_delete(self, node=None): """ Confirms that a specified object can be deleted or not. """ return (confirm(self.handler.parent, "Delete '%s' and all of its contents?" % self.name, 'Delete Directory') == YES) #--------------------------------------------------------------------------- # Deletes the associated directory from the file system: #--------------------------------------------------------------------------- def delete(self, path=None): """ Deletes the associated directory from the file system. """ if path is None: path = self.path not_deleted = 0 try: for name in listdir(path): fn = join(path, name) if isfile(fn): if self.include_file(fn): remove(fn) else: not_deleted += 1 elif isdir(fn) and (not self.delete(fn)): not_deleted += 1 if not_deleted == 0: rmdir(path) return True except: error(self.handler.parent, "Could not delete '%s'" % fn) # Indicate that the directory was not deleted: return False
# (C) Copyright 2010-2020 Enthought, Inc., Austin, TX # All rights reserved. from traits.api import Regex, String, BaseInt #: Used for variable names, but allow also empty string as it's the default #: case and it will be present if the workflow is saved before actually #: specifying the value. Identifier = Regex(regex=r"(^[^\d\W]\w*\Z|^\Z)") #: Identifies a CUBA type with its key. At the moment a String with #: no validation, but will come later. CUBAType = String() class PositiveInt(BaseInt): """A positive integer trait.""" info_text = 'a positive integer' default_value = 1 def validate(self, object, name, value): int_value = super(PositiveInt, self).validate(object, name, value) if int_value > 0: return int_value self.error(object, name, value)
class Person(HasStrictTraits): name = Str age = Int phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d') traits_view = View('name', 'age', 'phone')
# table_4.py # # 1. Add ability to create/delete employees through the table. ############################################################################# # enthought imports from traitsui.api import View, Item, Group, TableEditor from traitsui.table_column import ObjectColumn from traitsui.menu import OKButton, CancelButton, HelpButton from traits.api import HasTraits, Str, Int, Enum, List, Bool, Regex # Local imports from square_checkbox_column import SquareCheckboxColumn ssn_trait = Regex('000-00-0000', regex='\d\d\d[-]\d\d[-]\d\d\d\d') class Employee(HasTraits): first_name = Str(desc='First name of the Employee') last_name = Str gender = Enum('male', 'female') ssn = ssn_trait retired = Bool class Company(HasTraits): employees = List(Employee)