Python for each module and package

I am trying to structure my application in Python. Coming back from C # / Java background, I like the approach of one class to the file. I would like my project tree to look like this:

[Service]
    [Database]
        DbClass1.py
        DbClass2.py
    [Model]
        DbModel1.py
        DbModel2.py
    TheService.py
[ServiceTests]
    [Database]
        DbClass1Tests.py
        DbClass2Tests.py
    [Model]
        DbModel1Tests.py
        DbModel2Tests.py
    TheServiceTests.py
  • Is a single class approach to an OK file in Python?
  • Is it possible to create packages / modules in such a way that the packages work like Java packages or .NET namespaces, i.e. in DbModel1Tests.py:

    import Service.Model
    
    def test():
       m = DbModel1()
    
+5
source share
3 answers

-, 1: , . , , , , , . , Python , .

2: . , , , .

, import Service.Model, m = Service.Model.DBModel1().

, from Service.Model import * ( from Service.Model import DBModel1, ). , : m = DBModel1().

+4

Q1. 1 Python, .

Q2. from Service.Model import * - Service/Model/__init__.py, , , . import * Python

: Python - #/Java. , , $other_language .

, :

  • , Python (, )
  • , . , DbModel1, /, .
  • from Service.Model.DbModel1 import DbModel1 . from service.model import DbModel1: / ( , 1 .)
+13

, . , ( - ).

:

  • . __init__.py .
  • , : from Service.Model import DbModel , : m = DbModel1.DbModel1Class()

:

  • Pythonic ( ). , service.model.dbModel1.DbModel1().
  • you can do from Service.Model import DbModel1to directly import a class from a module DbModel1. This can be done by properly setting the content __init__.py(I don’t know how to fine-tune it). More info here
+3
source

All Articles