patternpythonMinor
Creating an app for selecting a folder
Viewed 0 times
appcreatingfolderforselecting
Problem
The script creates a simple app with a GUI containing an input field and a button to select a folder. On startup a default value is set to the input field. If the user types a value or selects a folder, the new value is stored and validated. If validation fails, the input field value is appended with the error message (the validation rule is that the value length must be greater than 0).
```
import Tkinter
import Tkconstants
import functools
import tkFileDialog
import configobj
import validate
from puremvc.patterns.proxy import Proxy
from puremvc.patterns.facade import Facade
from puremvc.patterns.command import SimpleCommand
from puremvc.patterns.mediator import Mediator
from puremvc.interfaces import ICommand, IMediator
class ConfigurationManager(object):
def __init__(self):
self.exec_spec = configobj.ConfigObj({'input_dir':'string(min=1)'}, list_values=False, _inspec=True, interpolation=False)
self.exec_params = configobj.ConfigObj(configspec=self.exec_spec)
def set(self, key, value):
self.exec_params[key] = value
validation_result = self.exec_params.validate(validate.Validator(self.exec_spec), preserve_errors=True)
for sections, key, exception in configobj.flatten_errors(self.exec_params, validation_result):
raise exception
def getValueFor(self, key):
return self.exec_params[key]
class ExecutionParamsProxy(Proxy):
NAME = "ExecutionParamsProxy"
def __init__(self):
super(ExecutionParamsProxy, self).__init__(ExecutionParamsProxy.NAME, [])
self.exec_params = ConfigurationManager()
def init(self):
self.exec_params.set('input_dir', '/path/to/nowhere')
self.sendNotification('defaults_loaded')
def set(self, key, value):
try:
self.exec_params.set(key, value)
except validate.ValidateError, ve:
self.sendNotification('validation_error', {'who':key, 'what':str(ve)})
def getValueFor(self, key):
re
```
import Tkinter
import Tkconstants
import functools
import tkFileDialog
import configobj
import validate
from puremvc.patterns.proxy import Proxy
from puremvc.patterns.facade import Facade
from puremvc.patterns.command import SimpleCommand
from puremvc.patterns.mediator import Mediator
from puremvc.interfaces import ICommand, IMediator
class ConfigurationManager(object):
def __init__(self):
self.exec_spec = configobj.ConfigObj({'input_dir':'string(min=1)'}, list_values=False, _inspec=True, interpolation=False)
self.exec_params = configobj.ConfigObj(configspec=self.exec_spec)
def set(self, key, value):
self.exec_params[key] = value
validation_result = self.exec_params.validate(validate.Validator(self.exec_spec), preserve_errors=True)
for sections, key, exception in configobj.flatten_errors(self.exec_params, validation_result):
raise exception
def getValueFor(self, key):
return self.exec_params[key]
class ExecutionParamsProxy(Proxy):
NAME = "ExecutionParamsProxy"
def __init__(self):
super(ExecutionParamsProxy, self).__init__(ExecutionParamsProxy.NAME, [])
self.exec_params = ConfigurationManager()
def init(self):
self.exec_params.set('input_dir', '/path/to/nowhere')
self.sendNotification('defaults_loaded')
def set(self, key, value):
try:
self.exec_params.set(key, value)
except validate.ValidateError, ve:
self.sendNotification('validation_error', {'who':key, 'what':str(ve)})
def getValueFor(self, key):
re
Solution
First off, the way you've named your functions is wrong, according to PEP8. Functions and variables should be in
Secondly, rather than creating a static attribute on your classes,
Finally, there are no comments here. At the minimum, you should add some docstrings to you functions and classes. Even if the code is fairly clear, you should at least add some docstrings. For example:
snake_case, and classes should be in CamelCase.Secondly, rather than creating a static attribute on your classes,
NAME, you could just do the following: MyClass.__name__.Finally, there are no comments here. At the minimum, you should add some docstrings to you functions and classes. Even if the code is fairly clear, you should at least add some docstrings. For example:
def my_func( ... ):
"""
Description of my_func and it's
arguments here.
"""
...Code Snippets
def my_func( ... ):
"""
Description of my_func and it's
arguments here.
"""
...Context
StackExchange Code Review Q#54361, answer score: 5
Revisions (0)
No revisions yet.