Вопрос

I'm trying to organize some hierarchical classes in python with an eye to reusing the subclasses elsewhere---but still with one canonical representation. I understand this is part of what subclassing and inheritance are for, but I'm getting tripped up in actual file and directory organization as much as anything.

Here's my case. I'd like to define three entities:

  1. People
  2. Organizations
  3. Addresses

Both people and organizations can have addresses, but addresses are always the same, whether they belong to people or organizations. Therefore, I'd like the same validation logic (eg, "is that a valid zip code?") to be located in a single Address class stored in a single place in my project, and for People and Organizations to draw from that class. That is, People and Organizations contain Addresses.

I think I will be instantiating them from JSON documents.

@ensure("zip", is_valid_zip)
class Address:
   def __init__(self,s):
      self.street = s.get('street')
      self.city = s.get('city')
      self.state = s.get('state')
      self.zip = s.get('zip')

>>> addr = Address({"street":"123 main st", "city": "Kalamazoo", "state": "MI","zip":"49001"})

Now, when I create, say, a person, I'd like to include the address. If the person looks like this, and the string that creates it looks like what follows:

class Person:
   def __init__(self,s):
      self.fname = s.get('fname')
      self.lname = s.get('lname')
      self.address = Address(s.get('address'))
>>> bill = Person({'fname': "Billbert", "lname": "Bronson", "address": {"street":"123 main st", "city": "Kalamazoo", "state": "MI","zip":"49001"}})

>>> bill.address.street
>>> '123 main st'

...where should I store these modules, and how? Where is the correct place for an import in my project? I have something like this...

schemas
├── schemas
│   ├── __init__.py
│   ├── people.py
│   ├── addresses.py
│   └── organizations.py
├── README.md
└── setup.py

where __init__.py contains:

from schemas.addresses import Address
from schemas.people import Person
from schemas.organizations import Organization

But there's something circular here. Does the Person class itself need an import statement for Address in it?

Now in my new script, I would like to be able to say...

 >>> from schemas import Person
 >>> bill = Person({'fname': "Billbert", "lname": "Bronson", "address": {"street":"123 main st", "city": "Kalamazoo", "state": "MI","zip":"49001"}})

Among other problems, when I try this approach, I get an error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stuff/schemas/schemas/people.py", line 5, in __init__
    self.address = Address(s.get('address'))
NameError: global name 'Address' is not defined

...which makes me concerned I'm barking up the wrong inheritance tree entirely.

Any tips on how to do this /correctly/ would be greatly appreciated---even things such as "here's an alternative to loading in from JSON" or "do a fancy abstract class factory crazy thing like so."

Это было полезно?

Решение

This actually worked fine once I got the import statement correct. (Yes, I did have to import sub classes in each, and it worked fine.) I'm accepting this as an answer, but if anyone has better recommendations, you're welcome to post 'em.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top