Checking for membership inside nested dict

前端 未结 2 1945
刺人心
刺人心 2021-01-16 12:41

This is a followup questions to this one:

Python DictReader - Skipping rows with missing columns?

Turns out I was being silly, and using the wrong ID field.<

2条回答
  •  天命终不由人
    2021-01-16 13:04

    My python skills are poor, so I am far too ignorant to write out what I have in mind in any kind of reasonable time. But I do know how to do OO decomposition.

    Why does the Employees class to do all the work? There are several types of things that your monolithic Employees class does:

    • Read and write data from a file - aka serialization
    • Manage and access data from individual employees
    • Manage relationships between exmployees.

    I suggest that you create a class to handle each task group listed.

    Define an Employee class to keep track or employee data and handle field processing/tidying tasks.

    Use the Employees class as a container for employee objects. It can handle tasks like tracking down an Employee's supervisor.

    Define a virtual base class EmployeeLoader to define an interface (load, store, ?? ). Then implement a subclass for CSV file serialization. (The virtual base class is optional--I'm not sure how Python handles virtual classes, so this may not even make sense.)

    So:

    • create an instance of EmployeeCSVLoader with a file name to work with.
    • The loader can then build an Employees object and parse the file.
    • As each record is read, a new Employee object will be created and stored in the Employees object.
    • Now ask the Employees object to populate supervisor links.
    • Iterate over the Employees object's collection of employees and ask each one to tidy itself.
    • Finally, let the serialization object handle updating the data file.

    Why is this design worth the effort?

    It makes things easier to understand. Smaller, task focused objects are easier to create clean, consistent APIs for.

    If you find that you need an XML serialization format, it becomes trivial to add the new format. Subclass your virtual loader class to handle the XML parsing/generation. Now you can seamlessly move between CSV and XML formats.

    In summary, use objects to simplify and structure your data. Section off common data and behaviors into separate classes. Keep each class tightly focused on a single type of ability. If your class is a collection, accessor, factory, kitchen sink, the API can never be usable: it will be too big and loaded with dissimilar groups of methods. But if your classes stay on topic, they will be easy to test, maintain, use, reuse, and extend.

提交回复
热议问题