Populating Factory using Metaclasses in Python -


obviously, registering classes in python major use-case metaclasses. in case, i've got serialization module uses dynamic imports create classes , i'd prefer replace factory pattern.

so basically, this:

data = #(generic class based on serial data) modulename = data.getmodule() classname = data.getclass() amodule = __import__(modulename) aclass = getattr(amodule, classname) 

but want this:

data = #(generic class based on serial data) classkey = data.getfactorykey() aclass = factory.getclass(classkey) 

however, there's hitch: if make factory rely on metaclasses, factory learns existence of classes after modules imported (e.g., they're registered @ module import time). populate factory, i'd have either:

  1. manually import related modules (which defeat purpose of having metaclasses automatically register things...) or
  2. automatically import in whole project (which strikes me incredibly clunky , ham-fisted).

out of these options, registering classes directly factory seems best option. has found better solution i'm not seeing? 1 option might automatically generate imports required in factory module traversing project files, unless commit-hook, run risk of factory getting out of date.

update:

i have posted self-answer, close off. if knows way traverse python modules across nested subpackages in way never hit cycle, gladly accept answer rather one. main problem see happening is:

\a.py (import sub.s2) \sub\s1.py (import a) \sub\s2.py \sub\s3.py (import sub.s2) 

when try import s3, first needs import main (otherwise won't know sub is). @ point, tries import a. while there, __init__.py called, , tries register a. @ point, tries import s1. since __init__.py in sub hit, tries import s1, s2, , s3. however, s1 wants import (which not yet exist, in process of being imported)! import fails. can switch how traversal occurs (i.e., depth first rather breadth first), hit same issues. insight on traversal approach helpful. two-stage approach can solve (i.e., traverse module references, import flat batch). however, not quite sure of best way handle final stage (i.e., know when done traversing , import everything). big restriction not want have super-package deal (i.e., directory under sub , a). if had that, kick off traversal, need import relative no reason (i.e., imports longer directory). thusfar, adding special function call sitecustomize.py seems option (i set root directory package development in file anyway).

the solution found imports on package based off of particular base directory , have special __init__.py functions of ones might have modules classes i'd want have registered. basically, if import module, first has import base directory , proceeds walk every package (i.e., folder) similar __init__.py file.

the downside of approach same modules imported multiple times, annoying if leaves code side effects in module import. however, that's bad either way. unfortunately, major packages (cough, cough: flask) have serious complaints idle if (idle restarts, rather doing anything). other downside because modules import each other, attempts import module in process of importing (an caught error, 1 i'm still trying stamp out). it's not ideal, job done. additional details on more specific issue attached, , if can offer better answer, gladly accept it.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -