flask sqlalchemy with circular imports with db models - flask-sqlalchemy

backend
├── erp
│   ├── blueprint_create_order_and_add_products
│   │   ├── __init__.py
│   │   └── resources
│   │   ├── create_order.py
│   │   ├── __init__.py
│   ├── blueprint_general_query
│   │   ├── __init__.py
│   │   └── resources
│   │   ├── general_query.py
│   │   └── __init__.py
│   ├── common
│   │   ├── __init__.py
│   │   └── models
│   │   ├── brand.py
│   │   ├── productwithspecs.py
│   ├── database_collection
│   │   ├── finance.db
│   │   ├── orders.db
│   │   └── vendors.db
│   └── __init__.py
├── __init__.py
└── run.py
Above is sort of my app structure
The models folder have db classes, the problem is that the class in brand module involves (through relationship) and imports the class in productwithspecs module, and there are lots of related db classes. How to import them in a CRUD resource of flask restful without circular imports.
The structure of app.py is:
from erp import app
if __name__ == '__main__':
app.run(debug=True)
the code of erp/ini.py is:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
db = SQLAlchemy(app)
ma = Marshmallow(app)
from erp.blueprint_create_order_and_add_products import bp as bp1 # api_createorders_addproducts
# REGISTER blueprint apis to app
app.register_blueprint(bp1)
Please let me know how to avoid circular imports, as I will be needing to import db classes into resources folder in blueprints e.g create_order module etc. Thanks

You should use an Application Factory App. You find the official recommendation here and this is a good tutorial.
After refactoring your erp/__init__.py should look similar to:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
db = SQLAlchemy(app)
ma = Marshmallow(app)
def create_app():
db.init_app(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
ma.init_app(app)
with app.app_context():
# Include your routes here
app.register_blueprint(erp.blueprint_create_order_and_add_products.bp)
return app
Your app.py:
from erp import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
And from all other modules you can import like this:
from erp import app, db

Related

webpack load amd path modules dependency

I am testing the loading of modules in webpack. How would you indicate the path of the dependency in an AMD module?
My project has something like this:
├── modules
│   ├── mod1.js
│   ├── mod2.js
│   └── others
│   └── mod3.js
├── public
│   └── bundle.js
├── src
│   └── app
│   └── app.js
└── webpack.config.js
in app.js I import only mod3.js therefore you must compile the three JS (mod1, mod2, mod3) since mod3.js depend on them.
I have a "others" route. Every time I create a folder I have to include the following line in webpack.config.js?
path.resolve(__dirname, 'modules/others'),
Is it not possible to indicate the path of the dependency in the module itself without webpack compiling go to the hard defined in the config?
Thank you

IntelliJ import subfolder as project

Having a code structure like below which contains documentation at the root level how can I TELL IDEA to import the Code/spark subfolder as a project?
.
├── Code
│   ├── foo
│   │   ├── bar
│   │   └── baz
│   └── spark
│   ├── build.sbt
│   ├── common
│   ├── job1
│   ├── project
│   ├── run_application
│   ├── sbt
│   ├── scalastyle-config.xml
│   └── version.sbt
├── Docs
You need to add Content Root, go to Project Structure settings (Ctrl + Alt + Shift + s), select your module, then on the right panel click Add Content Root and select Docs folder. Then you can select it and mark as part of the Module, for documentation I believe it should be Resources.
Even better: use a proper build tool like gradle and then apply https://docs.gradle.org/current/userguide/composite_builds.html

Which elm package versions are installed?

elm-package can manage dependencies for elm, but the only commands it supports (as of version 0.18.0) are install, publish, bump and diff according to running it without arguments. I was expecting something like elm-package list to show the installed packages.
Is there a command to list the currently installed elm package versions?
I think there is no one, but you can execute tree elm-stuff/packages -L 3 --noreport in your command line.
You will get a tree like this:
elm-stuff/packages
├── debois
│   ├── elm-dom
│   │   └── 1.2.3
│   └── elm-mdl
│   └── 8.1.0
├── elm-lang
│   ├── core
│   │   └── 5.1.1
│   ├── dom
│   │   └── 1.1.1
│   ├── html
│   │   └── 2.0.0
│   ├── http
│   │   └── 1.0.0
│   ├── mouse
│   │   └── 1.0.1
│   ├── virtual-dom
│   │   └── 2.0.4
│   └── window
│   └── 1.0.1
├── mgold
│   └── elm-date-format
│   └── 1.2.0
└── thaterikperson
└── elm-strftime
You can also just do cat elm-stuff/exact-dependencies.json, but there is no guarantee of have them installed:
{
"debois/elm-mdl": "8.1.0",
"elm-lang/virtual-dom": "2.0.4",
"elm-lang/mouse": "1.0.1",
"mgold/elm-date-format": "1.2.0",
"elm-lang/dom": "1.1.1",
"elm-lang/html": "2.0.0",
"elm-lang/http": "1.0.0",
"debois/elm-dom": "1.2.3",
"elm-lang/window": "1.0.1",
"elm-lang/core": "5.1.1"
}
If you use the Lighttable editor with the elm-light plug in then you would have a command to show (and add) packages.

Change modules loading priority

I would like to know the right way to change modules loading priorities in Linux. I want to have hdmi and LCD output the most quickly.
For now it take 3 seconds to came, I know it's not delay due to hdmi or TV because the first stuff I see on screen is some lines about mali init (mali is the name of the GPU here).
I use a A10-Olinuxino-Lime board with an homemade rootfs generated using buildroot and a custom Linux made for this kind of processor (linux-sunxi).
The tree of /etc/:
etc/
├── dhcp
│   ├── dhclient.conf
│   └── dhcpd.conf
├── dropbear
├── fstab
├── group
├── hostname
├── hosts
├── init.d
│   ├── rcK
│   ├── rcS
│   ├── S01logging
│   ├── S20urandom
│   ├── S40network
│   ├── S50dropbear
│   ├── S80dhcp-relay
│   ├── S80dhcp-server
│   ├── S80mali
│   └── S99TVOS
├── inittab
├── inittab~
├── inputrc
├── issue
├── ld.so.cache
├── ld.so.conf
├── ld.so.conf.d
├── mtab -> /proc/mounts
├── network
│   ├── if-down.d
│   ├── if-post-down.d
│   ├── if-post-up.d
│   ├── if-pre-down.d
│   ├── if-pre-up.d
│   ├── if-up.d
│   └── interfaces
├── nsswitch.conf
├── os-release
├── passwd
├── profile
├── protocols
├── random-seed
├── resolv.conf -> ../tmp/resolv.conf
├── securetty
├── services
├── shadow
├── ts.conf
└── wpa_supplicant.conf
Do you have any ideas ?
I'd create an /etc/init.d/S00modules script containing a sequence of insmod (or modprobe if your env supports it) lines.
If that doesn't help, then your modules are loaded even earlier,and you'll have to find how and where that happens. I'd first look at /sbin/init or what is used instead.

rails3 asset pipeline and file collisions

I'm updating an existing rails 2 app to rails 3, and having some trouble understanding the asset pipeline. I have read through the guide and as I understand it, files in any of the following directories will resolve to /assets:
app/assets
lib/assets
vendor/assets
and you could access them using helpers...i.e.
image_tag('logo.png')
But what I don't understand is how collisions are handled? For example, what if there are the following files:
app/assets/images/logo.png
lib/assets/images/logo.png
If I go to myapp.com/assets/images/logo.png, which file will be returned? I could check for collisions manually within my app, but this becomes a pain point when using gems that rely on the asset pipeline.
Based on what I've found, you can't have duplicate files, as rails will just return the first one found.
This seems like a bit of a design flaw, as a gem may not namespace their own assets
Why not taking advantage of the index manifest and organize your app/assets into decoupled modules? You can then reference to a particular image, image_tag('admin/logo.png'), and get for free your UI codebase organised in a more meaningful way. You could even promote a complex component, such as Single Page Application into it's own module and reuse it from different parts of the app.
Let's say you app is composed out of three modules: the public side, an admin UI and, e.g., a CRM to let your agents track the selling process at your company:
app/assets/
├── coffeescripts
│   ├── admin
│   │   ├── components
│   │   ├── index.coffee
│   │   └── initializers
│   ├── application
│   │   ├── components
│   │   ├── index.sass
│   │   └── initializers
│   └── crm
│   ├── components
│   ├── index.sass
│   └── initializers
├── images
│   ├── admin
│   ├── application
│   └── crm
└── stylesheets
├── admin
│   ├── components
│   └── index.sass
├── application
│   ├── components
│   └── index.sass
└── crm
├── components
└── index.sass
21 directories, 6 files
Don't forget to update your application.rb so they will be precompiled properly:
config.assets.precompile = %w(admin.js application.js crm.js
admin.css application.css crm.css)