Importing python modules and packages - python-3.x

I have following project structure,
|
├── database
│   ├── db_adapter.py
│   ├── db_layer
│   │   ├── __init__.py
│   │   └── mysql_adapter.py
│   ├── __init__.py
│   └── scripts
│   └── schema.sql
|
└── workflow
├── dags
│   ├── dss_controller.py
│   ├── __init__.py
|
├── __init__.py
├── plugins
I want to import db_adapter.py module inside dss_controller module, when I tried do it following error shown,
ModuleNotFoundError: No module named 'database'
How can I do the correct importing of the modules?

Related

importing folder that shadows same name as third party dependency. What to do?

say I have a folder structure that looks like this:
.
├── CODEOWNERS
├── Makefile
├── README.rst
├── __pycache__
│   └── conftest.cpython-37-pytest-6.2.2.pyc
├── airflow
│   ├── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── dev
│   │   └── dags
│   └── <some_file_I_need>
how do I import the file I need from the airflow local package (not the third party dependency named airflow). I have a dependency called airflow unfortunately and that gets imported when I do: import airflow.dev... and that errors out.

Create localized django app and use the localization from other app

I have the following problem:
I created a Django app (app1) and then installed it in other one (app2). Now I'm trying to make the internationalization of the site, but I want to be able to use the installed app translations and I cannot even compile them.
Some useful information:
APP 1
.
├── MANIFEST.in
├── app1
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── locale/
│   │   ├── en-us
│   │   │   └── LC_MESSAGES
│   │   │   └── django.po
│   │   ├── es
│   │   │   └── LC_MESSAGES
│   │   │   └── django.po
│   │   └── pr
│   │   └── LC_MESSAGES
│   │   └── django.po
│   ├── migrations/
│   ├── models.py
│   ├── settings.py
│   ├── static/
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── utils.py
│   └── views.py
└── setup.py
APP 2 (the one that has APP 1 installed)
├── app2/
│   ├── locale/
│   │   ├── en-us/
│   │   │   └── LC_MESSAGES
│   │   │   ├── django.mo
│   │   │   └── django.po
│   │   ├── es/
│   │   │   └── LC_MESSAGES
│   │   │   ├── django.mo
│   │   │   └── django.po
│   │   └── pr/
│   │   └── LC_MESSAGES
│   │   ├── django.mo
│   │   └── django.po
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1.apps.App1SiteConfig',
'app2.apps.App2SiteConfig',
]
LANGUAGE_CODE = 'es'
LANGUAGES = (
('en-us', _('English')),
('es', _('Spanish')),
('pt', _('Portuguese'))
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, "app2", "locale"),
)
Basically, the desired TODOs are:
compile the .mo's from App1
use the App1 transalations (it has its own templates and models so ideally they would be used there)
What I don't want:
compile the App1 .mo's from django-admin of App2 and then translate it there.
Thanks in advance!
Well.. finally I managed to solve this by my way..
With the virtualenv activated I moved to App1 dir and executed django-admin compilemessages, the .mo's has been created in the right path (app1/locale/<lang>/LC_MESSAGES/django.mo) and they're able to be used from the site.

How to make mtd-utils 2.0 for specified deployment path

I downloaded mtd-utils 2.0 and I want to built it for specified deployment path. If I launch:
./configure --bindir .../mtd-utils-81049e5/deploy/usr/sbin
and then I do:
make
I will get output into folder, where I launched make. I want to have executable files somewhere like: bla/mtd-utils-2.0.../deploy/usr/sbin...
IIUC, you can do this like that:
./configure --prefix=/tmp/mtd-utils
make
make install
Finally, you get this:
$ tree /tmp/mtd-utils
/tmp/mtd-utils
├── sbin
│   ├── doc_loadbios
│   ├── docfdisk
│   ├── flash_erase
│   ├── flash_eraseall
│   ├── flash_lock
│   ├── flash_otp_dump
│   ├── flash_otp_info
│   ├── flash_otp_lock
│   ├── flash_otp_write
│   ├── flash_unlock
│   ├── flashcp
│   ├── ftl_check
│   ├── ftl_format
│   ├── jffs2dump
│   ├── jffs2reader
│   ├── mkfs.jffs2
│   ├── mkfs.ubifs
│   ├── mtd_debug
│   ├── mtdinfo
│   ├── mtdpart
│   ├── nanddump
│   ├── nandtest
│   ├── nandwrite
│   ├── nftl_format
│   ├── nftldump
│   ├── recv_image
│   ├── rfddump
│   ├── rfdformat
│   ├── serve_image
│   ├── sumtool
│   ├── ubiattach
│   ├── ubiblock
│   ├── ubicrc32
│   ├── ubidetach
│   ├── ubiformat
│   ├── ubimkvol
│   ├── ubinfo
│   ├── ubinize
│   ├── ubirename
│   ├── ubirmvol
│   ├── ubirsvol
│   └── ubiupdatevol
└── share
└── man
├── man1
│   └── mkfs.jffs2.1
└── man8
└── ubinize.8
5 directories, 44 files

Python3.5 recursive glob not showing matching files

With Pyton >= 3.5 glob supports recursive searching. I am attempting to do this, but with no success.
Here you can see my directory structure:
/Volumes/user/TEST/
└── [NAME]john\ doe,\ peter\ griffin\ -\ sample\ video[29.06.17][1080]{user}
├── [NAME]john\ doe,\ perter\ griffin\ -\ sample\ video[29.06.17][1080]{user}.mp4
├── [NAME]john\ doe,\ peter\ griffin\ -\ sample\ video[29.06.17][Hi-Res]
│   ├── 583a6ea574035-img-8399.jpg
│   ├── 583a6eb9681c0-img-8401.jpg
│   ├── 583a6eb9e53e1-img-8402.jpg
│   ├── 583a6ecad078d-img-8404.jpg
│   ├── 583a6ecd207ed-img-8403.jpg
│   ├── 583a6edb7348c-img-8405.jpg
│   ├── 583a6ede1cbfa-img-8406.jpg
│   ├── 583a6eeb294a9-img-8407.jpg
│   ├── 583a6eee94a45-img-8408.jpg
│   ├── 583a6efb2389a-img-8409.jpg
│   ├── 583a6f01a78ae-img-8410.jpg
│   ├── 583a6f0daea30-img-8411.jpg
│   ├── 583a6f1301e3c-img-8412.jpg
│   ├── 583a6f1ec07d8-img-8413.jpg
│   ├── 583a6f240d4ce-img-8414.jpg
│   ├── 583a6f306f517-img-8415.jpg
│   ├── 583a6f36def7a-img-8416.jpg
│   ├── 583a6f4280706-img-8417.jpg
│   ├── 583a6f48b8b11-img-8418.jpg
│   ├── 583a6f55160cc-img-8419.jpg
│   ├── 583a6f5b7fb35-img-8420.jpg
│   ├── 583a6f68aad40-img-8423.jpg
│   ├── 583a6f6da8c0b-img-8424.jpg
│   ├── 583a6f7b9e1d6-img-8425.jpg
│   ├── 583a6f8069e59-img-8426.jpg
│   ├── 583a6f8e9891e-img-8427.jpg
│   ├── 583a6f9427910-img-8428.jpg
│   ├── 583a6fa45e3f4-img-8429.jpg
│   ├── 583a6fa96b182-img-8430.jpg
│   ├── 583a6fb935bf2-img-8431.jpg
│   ├── 583a6fca7a0c9-img-8433.jpg
│   ├── 583a6fcbab549-img-8434.jpg
│   ├── 583a6fdcf2812-img-8435.jpg
│   ├── 583a6fde5ee86-img-8436.jpg
│   ├── 583a6fee54d9b-img-8437.jpg
│   ├── 583a6ff0acb76-img-8438.jpg
│   ├── 583a7000ecd80-img-8439.jpg
│   ├── 583a70026c325-img-8440.jpg
│   ├── 583a701319c22-img-8441.jpg
│   ├── 583a701457221-img-8443.jpg
│   ├── 583a7027e8531-img-8444.jpg
│   ├── 583a7029f0e7c-img-8445.jpg
│   ├── 583a703d6c3c9-img-8446.jpg
│   ├── 583a703f879f5-img-8447.jpg
│   ├── 583a7051ae77a-img-8448.jpg
│   ├── 583a705b588c3-img-8449.jpg
│   ├── 583a706d40811-img-8450.jpg
│   ├── 583a7076cb3e9-img-8451.jpg
│   ├── 583a70894ec35-img-8452.jpg
│   ├── 583a70964fbcb-img-8453.jpg
│   ├── cai-8498-pa.jpg
│   └── cai-8498-pt.jpg
├── [NAME]john\ doe,\ peter\ griffin\ -\ sample\ video[29.06.17][Hi-Res].jpg
└── cover.png
So the images are definitely there.
Now here is my method, that for some reason, is not working:
def count_images(folder):
"""
Count the number of images in a folder.
:param folder: Folder to count the images
:return: Number of photos
"""
print(folder)
files = [file for file in glob.glob(folder + '/**/*.jpg', recursive=True)]
print(files)
return len([file for file in glob.glob(folder + '/**/*.jpg', recursive=True)])
But it just outputs the following:
/Volumes/user/TEST/[NAME]john doe, peter griffin - sample video[29.06.17][1080]{user}/
[]
It is becoming extremely frustrating, as I do not see what I am doing wrong.
So I found the problem.
The glob module has issues with certain characters in the filename - [ and ] in this case. You can use glob.escape() to escape these characters in the filename.
So the final function will look like this:
def count_images(folder):
return len([file for file in glob.glob(glob.escape(folder) + '/**/*.jpg', recursive=True)])

Correct way of starting a NodeJs, React project with Express

I'm beginning with React, NodeJs and ExpressJs. I have seen many tutorials but I'm not sure of the correct way to start a project?
I have seen two ways. The first being express <project_name> and the second being npm init.
Which is correct and if there isn't a correct way then why would you initialize them differently when npm init includes express eventually (In the tutorials).
Thanks
npm init is good way to start, as you know it creates a package.json file in your project directory where you can store your project dependencies.
After this you must run the following commands:
npm install --save-dev webpack
npm install --save-dev babel
npm install --save-dev babel-loader
npm install babel-core
npm install babel-preset-env
npm install babel-preset-react
or as a single line command use this:
npm install --save-dev webpack babel babel-loader babel-core babel-preset-env babel-preset-react
first command will create a webpack.config.js file.
Second command will ready the babel to use in your project and the third to use babel-loader.
Now it's time to create project structure which looks like :
projectFolder/
├── package.json
├── public
│   ├── favicon.ico
│   └── index.html
├── README.md
└── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
└── logo.png
This is the very basic project structure. It doesn't have server side structure.
full structure looks like:
react/
├── CHANGELOG.md
├── CONTRIBUTING.md
├── docs
│   ├── data-fetching.md
│   ├── getting-started.md
│   ├── how-to-configure-text-editors.md
│   ├── react-style-guide.md
│   ├── README.md
│   └── recipes/
├── LICENSE.txt
├── node_modules/
├── package.json
├── README.md
├── src
│   ├── actions
│   ├── client.js
│   ├── components
│   │   ├── App
│   │   │   ├── App.js
│   │   │   ├── App.scss
│   │   │   ├── package.json
│   │   │   └── __tests__
│   │   │   └── App-test.js
│   │   ├── ContentPage
│   │   │   ├── ContentPage.js
│   │   │   ├── ContentPage.scss
│   │   │   └── package.json
│   │   ├── ErrorPage
│   │   │   ├── ErrorPage.js
│   │   │   ├── ErrorPage.scss
│   │   │   └── package.json
│   │   ├── Feedback
│   │   │   ├── Feedback.js
│   │   │   ├── Feedback.scss
│   │   │   └── package.json
│   │   ├── Footer
│   │   │   ├── Footer.js
│   │   │   ├── Footer.scss
│   │   │   └── package.json
│   │   ├── Header
│   │   │   ├── Header.js
│   │   │   ├── Header.scss
│   │   │   ├── logo-small#2x.png
│   │   │   ├── logo-small.png
│   │   │   └── package.json
│   │   ├── Link
│   │   │   ├── Link.js
│   │   │   └── package.json
│   │   ├── Navigation
│   │   │   ├── Navigation.js
│   │   │   ├── Navigation.scss
│   │   │   └── package.json
│   │   ├── NotFoundPage
│   │   │   ├── NotFoundPage.js
│   │   │   ├── NotFoundPage.scss
│   │   │   └── package.json
│   │   ├── TextBox
│   │   │   ├── package.json
│   │   │   ├── TextBox.js
│   │   │   └── TextBox.scss
│   │   ├── variables.scss
│   │   └── withViewport.js
│   ├── config.js
│   ├── constants
│   │   └── ActionTypes.js
│   ├── content
│   │   ├── about.jade
│   │   ├── index.jade
│   │   └── privacy.jade
│   ├── core
│   │   ├── db.js
│   │   ├── DOMUtils.js
│   │   ├── fetch
│   │   │   ├── fetch.client.js
│   │   │   ├── fetch.server.js
│   │   │   └── package.json
│   │   ├── Location.js
│   │   └── passport.js
│   ├── data
│   │   ├── queries
│   │   │   ├── content.js
│   │   │   ├── me.js
│   │   │   └── news.js
│   │   ├── schema.js
│   │   └── types
│   │   ├── ContentType.js
│   │   ├── NewsItemType.js
│   │   └── UserType.js
│   ├── public
│   │   ├── apple-touch-icon.png
│   │   ├── browserconfig.xml
│   │   ├── crossdomain.xml
│   │   ├── favicon.ico
│   │   ├── humans.txt
│   │   ├── robots.txt
│   │   ├── tile.png
│   │   └── tile-wide.png
│   ├── routes
│   │   ├── contact
│   │   │   ├── Contact.js
│   │   │   ├── Contact.scss
│   │   │   └── index.js
│   │   ├── home
│   │   │   ├── Home.js
│   │   │   ├── Home.scss
│   │   │   └── index.js
│   │   ├── login
│   │   │   ├── index.js
│   │   │   ├── Login.js
│   │   │   └── Login.scss
│   │   └── register
│   │   ├── index.js
│   │   ├── Register.js
│   │   └── Register.scss
│   ├── routes.js
│   ├── server.js
│   ├── stores
│   └── views
│   ├── error.jade
│   └── index.jade
├── test
│   └── stubs
│   └── SCSSStub.js
└── tools
├── build.js
├── bundle.js
├── clean.js
├── copy.js
├── deploy.js
├── lib
│   ├── fetch.js
│   └── fs.js
├── README.md
├── run.js
├── runServer.js
├── start.js
└── webpack.config.js
Created using yeoman generator-react-fullstack
Alternatively you can do all this stuff by simply using yeoman react generator but be careful with generators, they sometimes toughen your deployment process.

Resources