How to import multiple class objects from seperate folders? - python-2.7

I built a python 2.7 application with the below directory structure for my relevant files. How do cal methods located in different folder locations?
Data-Wrangling-OpenStreetMap-data
|
+---- process_data
| |
| +---- __init__.py
| |
| +---- data_cleaner.py
|
+---- main_code.py
|
+---- audit _data
| |
| +---- __init__.py
| |
| +---- audit_file.py
I have succeeded in doing it correctly for one class referenced from main_code.py via the use of:
from process_data.data_cleaner import DataCleaner
However, if attempt a similar pattern for another class located in seperate folder referenced by main_code.py for via the use of the import statement
from audit_data.audit_file import AuditFile
I get the error:
ImportError: No module named audit_data.audit_file
Any ideas as to what I may be overlooking and/or guidance on what further details I need to post to help find the solution to my problem?

from process_data.data_cleaner import data_cleaner
as data_cleaner is the file name data_cleaner.py and the second one data_cleaner is a class defined in it.

The cause of my problem was a silly one;
The folder containing the class I was calling was named audit _file whilst the folder I was calling within my code was audit_file
What didnt work
from audit_data.audit_file import AuditFile
What worked
from audit _data.audit_file import AuditFile
For those reading this watch out for unintended spaces in your folder names

Related

Flask: Include common template to multiple blueprints

How could I implement a common template to be included in two Flask blueprints?
I am trying to create the following structure in my application:
app
+-- bp
| +-- hello
| | +-- templates
| | | +-- hello
| | | +-- hello.html
| | +-- routes.py
| +-- world
| + etc
+-- templates
+-- common_incl.html
in hello.html, I try to include another file by {% include '/templates/common_incl.html' %} but I always get a TemplateNotFound exception.
I would not like to expose the common_incl.html in static folder, as it isn't static but has Jinja variables expressions.
Re-reading https://flask.palletsprojects.com/en/2.0.x/blueprints/#templates might provide some guidance.
Depending on the nature of your application (and your blueprints) either put the base templates in the application's templates folder, or use a third blueprint that does nothing but hold common templates for your two templates.

Is it possible to include an installed_app's template in your app's template

I have a django app TestApp. In the templates directory of TestApp, I have an test_template.html. TestApp's settings.py has a reusable app (in the INSTALLED_APPS list) I created called reusable_app. There is a template called reusable_template.html in the templates directory of this reusable app.
Is it possible to include reusable_template.html in the test_template.html using the {% include} tag?
I tried {% include "reusable_app/templates/reusable_template.html" %} in test_template.html but got a TemplateDoesNotExist exception.
I also tried:
from django.template.loader import get_template
get_template('reusable_app/templates/reusable_template.html')
Django will look inside the app's templates directory, so you shouldn't specify this in the path. This should work:
{% include "reusable_template.html" %}
This assumes of course that the only app with a template called reusable_template.html is your reusable_app.
To minimise the risk of template collisions it is common to have a subdirectory in your app's templates directory, so that the path to the template is reusableapp/templates/reusableapp/reusable_template.html. You would then include it with:
{% include "reuseable_app/reusable_template.html" %}
If you have a project structure:
+app
|
+project/urls.py
| |
| +wsgi.py
| |
| +settings.py
|
+templates/base.html
|
+static/
then your template has to be in
+app+
| |
| +templates/your_app_base.html
| |
| +your_tempplate.html
+project/urls.py
| |
| +wsgi.py
| |
| +settings.py
|
+templates/base.html
|
+static/
Django first checks your apps's templates directory and if does not find templates, takes it from the main template directory. Make sure your template loaders are configured in settings.py

why Django put application at the same level of my project folder?

since Django 1.4 (I think) django create a folder for my project when I start a project. Django add a folder for any application I created (with python manage.py startapp) at the same level of my project folder.
Project_name
|---project_name_dir/
|---application_dir/
`---manage.py
I really like the following folder structure:
Project_name
|---project_name_dir/
| |---application_dir/
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | `-- views.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
| |---templates/
| | `---application_dir/
| `---static/
| |---css/
| |---font/
| |---img/
| `---js/
|---deployment/
|---documentation/
|---config/
`---manage.py
Because I have a folder with all my django files (project_name_dir/) and other directories for non django files.
So why Django put application at the same level of my project folder?
In Django, the position of the application directory is not considered. Django only uses the name of the application.
Thus, the position of the application is basically a matter of convenience of the programmer.
This is also the reason why two apps should not have the same name: even if they are imported in INSTALLED_APPS as
('app.app1', 'app1')
Django only concerns with the last part after the dot, i.e. app1.
So, in the end, you can use the directory structure you want, as long as the apps' names don't collide and you point to the app on INSTALLED_APPS. Because of this, if there isn't any special reason, you should put them on the project's root, like Django does.

Django ImportError on Heroku

I tried several things to get my app working on heroku, but now I'm out of ideas. I can install my project on heroku's rep, but I get a 500 error code. My application works very well using virtualenv on my machine after I followed the steps described on heroku documentation for django.
When I do my "git push heroku master" and try in browser, I get the following error:
2013-07-07T15:39:11.170514+00:00 app[web.1]: ImportError: No module named apps.base
2013-07-07T15:39:11.170059+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/dateformat.py", line 35, in format
2013-07-07T15:39:11.170202+00:00 app[web.1]: app = import_module(appname)
2013-07-07T15:39:11.170202+00:00 app[web.1]: default_translation = _fetch(settings.LANGUAGE_CODE)
2013-07-07T15:39:11.170202+00:00 app[web.1]: _default = translation(settings.LANGUAGE_CODE)
I tought it was caused by a directory structure that wasn't supported on heroku, so I adjusted it from the default one that was created with django startproject command.
Here is my new file structure. I adjusted the import reference everywhere and as I said, I works pefctly in local:
manage.py
Procfile
requirements.txt
vielfaltig
|____apps
| |____base
| | |____models.py
| | |____templates
| | |____tests.py
| | |____urls.py
| | |____views.py
| |____projects
| | |____admin.py
| | |____models.py
| | |____templates
| | |____templatetags
| | |____tests.py
| | |____translation.py
| | |____urls.py
| | |____views.py
|____locale
|____media
|____settings.py
|____static
|____urls.py
|____vielfaltig.db
|____wsgi.py
As you notice, I have 2 apps (base and projects). In the code, I import them using "vielfaltig.apps.base" for example. I changed this everywhere. I had this error before and I changed the directory structure according to what I read when I googled the error. I also tried to put everything in the root directory (along with the requirements.txt and procfile). I don't know why it keeps telling me an ImportError for "apps.base" while I reference the app using "vielfaltif.apps.base" everywhere... ?
Does anyone have an idea? I will paste my settings.py if needed. For now I think it would just take a lot of space.
Thank you very much for any help !
I guess it will work with import vielfaltig.apps.base instead of apps.base. It can be fixed if you insert path vielfaltig to python system path in your settings.py
import sys
sys.path.append('vielfaltig')

How to cross-reference already existing doxygen docs?

I have two C++ projects A and B; The dependency is only B to A.
B --> A
I would like to separately run Doxygen on A and B respectively but still let me possible cross reference on A from B doc. (That is, when I browse B doc, I can directly link to the A doc if there is any Class from A used in B).
--
[Replied the answer from 0x4b:]
if I set "CREATE_SUBDIRS" with YES and use relative path for tag files, Doxygen will somehow make wrong linking reference.
I did follow the example.
<root>
+- proj
| +- html HTML output directory for proj
| +- d1
| | +- d2
| | .... (*.html)
| |
| ...(*.html)
| +- src sources for proj
+- ext1
| +- html HTML output directory for ext1
| |- ext1.tag tag file for ext1
|- proj.cfg doxygen configuration file for proj
|- ext1.cfg doxygen configuration file for ext1
proj.cfg:
OUTPUT_DIRECTORY = proj
INPUT = proj/src
TAGFILES = ext1/ext1.tag=../../ext1/html
ext1.cfg:
OUTPUT_DIRECTORY = ext1
GENERATE_TAGFILE = ext1/ext1.tag
The documents under both html/ and html/d1/d2 would like to try linking external doc located in ../../ext1/html. Apparently one of them will fail.
You probably want to use the tagfile feature. When you generate the documentation for A, make sure the GENERATE_TAGFILE option is set. When you generate the documentation for B, set the value of TAGFILES to include the result from A.
[Update to address relative paths]
Doxygen is fairly fragile when it comes to [relative] paths. You clearly understand that using an absolute path will solve the problem. You could try taking a value from the environment, e.g. using
TAGFILES = ext1/ext1.tag=$(PWD)/../ext1/html
to create an absolute path. It's not ideal, but a lot of the values in the Doxyfile depend on where doxygen is run, and not where the configuration file lives.