Note that there are some explanatory texts on larger screens.

plurals
  1. POPython namespace elements visibility (vs proper package structure)
    text
    copied!<p><strong>Python3</strong></p> <p>Tried to found an answer but failed. First I'll present the snippet, then I'll explain why I wanted to do it this way and what I wanted to achieve. Maybe it'll look like this approach is "the bad one". Hence this semi-double topic, cause first I'd like to know why this snippet isn't working and second - I'd like to know if this approach is right.</p> <p>So:</p> <pre><code>class Namespace: def some_function(): pass class SomeClass: fcnt = some_function </code></pre> <p>This won't work due to:</p> <p><code>NameError: name 'some_function' is not defined</code></p> <p>What I want to achieve is code and file structure readability. </p> <p>Above example is a snippet which I use (not this one, but it looks like this) in Pyramid project.</p> <p>My project tree looks like this:</p> <pre><code>my_project ├── models │   ├── __init__.py │   └── some_model.py ├── schemas │   ├── __init__.py │   ├── some_schema.py │   └── some_other_schema.py ... ├── views │   ├── __init__.py │   └── some_view.py └── __init__.py </code></pre> <p>What I wanted to achieve is clean schema/model/view importing. </p> <p>In <strong>some_schema.py</strong> file resides <code>class SomeSchema</code>, in <strong>some_other_schema.py</strong> <code>class SomeOtherSchema</code>.</p> <p>With above snippet I can make: <code>from my_project.schemas.some_schema import Schema</code></p> <p>and use it like <code>Schema.SomeSchema()</code></p> <p>I've got a little bit lost with packages and <code>import</code>s. How one could make a clean structure (one schema per file) and still be able to use Schema namespace? (In C++ I'd just put each of those classes in <code>Schema</code> namespace, that's why I did this in snippet above. But! What works in C++ maybe shouldn't be used in python, right?).</p> <p>Thanks for answer in advance.</p> <p><strong>EDIT:</strong> Ok, I've done some testing (I thought that I've done it, but looks like not..).</p> <ol> <li>using <code>from my_project.schemas.some_schema import Schema</code> with another <code>from my_project.schemas.some_other_schema import Schema</code> causes in the second import shadowing first one. So if after first import I'd be able to use <code>x = Schema.SomeSchema()</code> than after second import I'd be unable to do this, because <code>class Schema</code> gets overriden. Right, so as Erik said - classes aren't namespaces. GOT IT!</li> <li><p>in my very first snippet yes, I should've used <code>fnct = Namespace.some_function</code>. What's wierd - it works. I have the same statement in my pyramid code, with one difference. <code>some_function</code> has <code>@colander.deferred</code> decorator. In fact it looks like this:</p> <pre><code>class Schema: @colander.deferred def deferred_some_function(node, kw): something = kw.get("something", []) return deform.widget.SelectWidget(values=something, multiple=True) class SomeSchema(colander.MappingSchema): somethings = colander.SchemaNode(colander.Set(), widget=Schema.deferred_some_function) </code></pre> <p>And I get <code>NameError: name 'Schema' is not defined</code></p></li> <li><p>Getting back to package format. With this:</p> <pre><code>### another/file.py from foo.bar.schema import SomeSchema # do something with SomeSchema: smth = SomeSchema() smth.fcnt() </code></pre> <p>I have to make one module <code>foo/bar/schema.py</code> in which I'd have to put all my <code>SomeXSchema</code> classes. An if I have lots of them, then there's the unreadabilty glitch which I wanted to get rid off by splitting SomeXSchema - one per file. Can I accomplish this somehow? I want to call this class for example: <code>User</code>. And here's the THING. Maybe I do it wrong? I'd like to have class named <code>User</code> in <code>schema</code> <em>namespace</em> and class named <code>User</code> in <code>model</code> <em>namespace</em>. Shouldn't I? Maybe I ought to use prefix? Like <code>class SchemaUser</code> and <code>class ModelUser</code> ? I wanted to avoid it by the use of modules/packages. If I'd use : <code>import foo.bar.schema</code> then I'd have to use it like <code>x = foo.bar.schema.User()</code> right? There is no way to use it like <code>x = schema.User()</code> ? Sorry, I just got stuck, my brain got fixed. Maybe I need a little break to take a fresh look?</p> <p><strong>ANOTHER EDIT (FOR POINT 3 ONLY)</strong></p> <p>I did some more research. The answer here would be to make it like this:</p> <pre><code>## file: myproject/schemas/__init__.py from .some_schema import SomeSchema from .some_other_schema import SomeOtherSchema </code></pre> <p>then usage would be like this:</p> <pre><code>## some file using it import myproject.schemas as schema s1 = schema.SomeSchema() s2 = schema.SomeOtherSchema() </code></pre> <p>Would it be <em>lege artis</em>?</p></li> <li><p>If anyone thinks that topic should be changed - go ahead, give me something more meaningful, I'd appreciate it.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload