Search code examples
pythonclasssharing

How do I go make my Python code more efficient and dynamic?


I'm a trainee novice programmer.

I have / am creating simple programs, generally around screen scraping, data caption (postrgres), various processing methods and now a GUI via wxpython

I find a lot of these programs overlap - ie use same techniques, and get some very long copied and pasted programs!

Overtime I improve these techniques and find myself having to backtrack over multiple programs to update these.

How? Can I? Create a more dynamic, systematic process.

One where all programs / procedures / classes are shared?

Has it got a name?

My logical thought is that like 'procedures' and 'classes' I have would have smaller low level programs and mid level programs that called upon these - the GUI being the top program! but this would mean passing data to and from! Can Classes and Procedures be separate programs?

Many thanks Cameron


Solution

  • There is a range of techniques you can use :

    1. Shared modules - A set of modules with well defined interfaces - functions, classes etc. These modules sit in a folders which every application you use can get to - i.e. the path to the folder is added to PYTHON_PATH environment variable. These interfaces should be engineered so when you add functionality to them they don't break the old applications.
    2. Design patterns - Design your applications with a good design pattern - MVC (Master View Controller) is a useful one for GUI programs. V is your GUI - and exposes only a few methods, which aren't actually dependent on the GUI itself (for instances methods such as display_foo). The Master is your data access functionality - again with well defined interfaces. Controller interfaces between View and Master. There may be other patterns which apply to your application too.