Map all files and objects of a django Project - python

Currently, I'm starting to work on a Django project which it's already begun.
Don't misunderstand me, I already know how basic files and dependencies work and relate to each other on this framework (forms, views, models, urls, templates, etc)
But my problem is, that this is a project with lots of files, classes, models, constants, etc... And has lots of code not written by myself.
I'm not very familiar to these internal custom objects, so, I need a module or script on python which can read all folders and map all relationships and dependencies (internal project objects, not modules or requirements), so I can better understand what's already done and work more efficiently on these modifications I have to do.
Currently I'm studying it manually, but it's very unefficient, and a slow process, so, if anyone knows about a tool or maybe a script, of these characteristics, please let me know where I can find it, download it, etc...
Thanks in advance!

Python has a builtin module called modulefinder that can be run from the command line to trace dependencies. It can be invoked like
python -m modulefinder mymodule.py
to print module dependencies of mymodule.py

Related

Use a python file from a separate maven module

I have a Maven project with multiple modules. Many of the modules contain the code and config for a separate application. I have a python script with each application that will take care of launching the application. I also have a separate "helper" python file that has functionality intended to be used by each of the application launcher scripts.
I'd like to avoid having to have a copy of the helper in each application module, so I was hoping I could figure out a way to either make the python file a "dependency", or to copy it into the built distribution (zip file containing the needed jar files and other configuration) during the maven build process.
I already have a "common" module that contains Java classes commonly used by code in many of the other modules, so this module is already a dependency required by other module pom files. Seems like the perfect place to put the helper python file. The thing is: maven seems to very good at handling maven artifact dependencies, but I'm not sure how (or if) it can handle this type of situation.
Has anyone done something like this?

Defining and using a Python class within a ROS catkin workspace

I am running ROS Indigo. I have what should be a simple problem: I have a utility class in my package that I want to be callable from our scripts. It only needs to be called within our own package; I don't need it to be available to other ROS packages.
I defined a class named HandControl in a file HandControl.py. All my attempts to import it, or use it without importing, fail. Where in the catkin workspace do I put it -- the root of the package, or in scripts? Do I need __init.py__ anywhere (I have tried several places)?
It is a good practice to follow the standards of Python and ROS here. Scripts are typically placed in /script directory and they should not be imported into other python scripts. Reusable python code is an indication of a python module. Python modules should be placed in /src/package_name and there you should create __init__.py as well. This module will be available everywhere in your catkin workspace. There is a good chance this structure will help you in the future to structure things, even though you may not seem to need it at the moment. Project typically grow and following guidelines helps to maintain good code. For more specific details checkout this python doc.
Erica,
please see this school project, which was written in Python and run on ROS Indigo. If you'd look in the /scripts folder, you can see an example of a custom class that is being called from other scripts. If you'd look into the launch files in /launch you can see an example of configuring the ROS nodes - maybe that is your problem.

python 3 import from subdir

My project has to be extensible, i have a lot of scripts with the same interface that lookup things online. Before i was using __import__ but that does not let me put my 'plugins' on a dedicated directory:
root/
main.py
plugins/
[...]
So my question is: Is there a way to individually import modules from that subdirectory? I'm guessing importlib, but i'm so lost in how the python module loading process works... What i want to do is something like this:
for pluginname in plugins:
plugin = somekindofimport("plugins/{name}".format(name=pluginname))
plugin.unififedinterface()
Also, as a side question, the way am i trying to achieve extensibility is a good way?
I'm on python3.3
Stop thinking in terms of pathnames and start thinking in terms of packages. Read Packages in the tutorial, and if you want more detail see The import system.
But the basic idea is this:
Create a file name plugins/__init__.py. It can be empty; that's enough to turn plugins into a package. Which means you can import modules from that package with:
import plugins.plugin
So, how do you do this dynamically? That's what importlib is for. (You can also use __import__ here, but it's less flexible, and less readable in non-trivial cases, so unless you need pre-3.3 compatibility, don't.)
plugin = importlib.import_module('plugins.{name}'.format(name=pluginname))
It would probably be cleaner to import plugins to get the package, and then use relative imports from within that package, as shown in the examples in the import_module docs.
This also means Python takes care of the .pyc creation and caching, etc.
And it means that you can later expand plugins to be a "namespace package", which can be split across multiple directories like /usr/share/myapp/plugins for stock plugins, /etc/myapp/plugins for site plugins and ~/myapp/plugins for user-specific plugins.
If you really, really want to import from a directory that isn't a package, you can create a module loader and use it, but that's a whole lot of work for no actual benefit. (It's actually not that hard in 3.3 (SourceLoader and friends will do most of the work for you), but you will find almost no examples out there to guide you; instead, you'll find examples of the 2.6-3.2 way, or the 2.0-2.5 way, both of which are hard.) Plus, it means that if someone creates a plugin named, say, gzip, you can end up blocking the stdlib gzip module with the plugin. (That's especially fun if the gzip plugin tries to use the gzip stdlib module, as it likely will…) If the plugin ends up being named plugins.gzip, there's no problem.
Also, as a side question, the way am i trying to achieve extensibility is a good way?
As long as you only want to support 3.3+, yes, I think this is a great solution.
Before 3.3, using a package for plugins was a lot more problematic. People have come up with a variety of different plugin systems—in one case going so far as to dynamically create module objects and execfile into them. If you need to deal with that, I would suggest looking at existing Python apps with plugins (e.g., MusicBrainz Picard) to get different ideas.

Where to put standalone-django scripts without breaking imports?

I'm still pretty new to Python.
I have a Django website with several apps and a /libs directory. I need to add a couple cron jobs that will use my Django models. I've already worked all that out, no big deal.
I have a problem with my imports, though.
I would like to include these scripts in the App they generally belong-to. But when I execute the script from the commandline, it's unable to load modules from the /libs directory.
To illustrate specifically the dir structure, imagine this pseudo-code:
import ./../libs/mysharedlib.py
In other words the scripts and sharedlib are:
~/project/myapp/myscript.py
~/project/libs/mysharedlib.py
I get that I could just add ~/project to my PYTHONPATH but then I have to worry about doing this during deployment and it just feels broken.
Should I move my scripts out of my apps and put them in the ~/project? Is there a better way?
There is a better way, custom management commands solve this.
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
These let you write stand alone utility scripts. You can run these as a cron or just as utilities. They use the exact same paths as any other module in your django app.
While these solve import problems does your libs directory have a __init__.py file? Can you import your lib directory in your views? Or is your import just not working in your cron scripts? Either way custom managment commands address this.

Python project deployment design

Here is the situation: the company that I'm working in right now gave me the freedom to work with either java or python to develop my applications. The company has mainly experience in java.
I have decided to go with python, so they where very happy to ask me to give maintenance to all the python projects/scripts related to the database maintenance that they have.
Its not that bad to handle all that stuff and its kind of fun to see how much free time I have compared to java programmers. There is just one but, the projects layout is a mess.
There are many scripts that simply lay in virtual machines all over the company. Some of them have complex functionality that is spread across a few modules(4 at maximum.)
While thinking about it about it, I realized that I don't know how to address that, so here are 3 questions.
Where do I put standalone scripts? We use git as our versioning system.
How do structure the project's layout in a way that the user do not need to dig deep into the folders to run the programs(in java I created a jar or a jar and a shell script to handle some bootstrap operations.)
What is a standard way to create modules that allow easy reusability(mycompany.myapp.mymodule?)
Where do I put standalone scripts?
You organize them "functionally" -- based on what they do and why people use them.
The language (Python vs. Java) is irrelevant.
You have to think of scripts as small applications focused on some need and create appropriate directory structures for that application.
We use /opt/thisapp and /opt/thatapp. If you want a shared mount-point, you might use a different path.
How do structure the project's layout in a way that the user do not need to dig deep into the folders to run the programs
You organize them "functionally" -- based on what they do and why people use them. At the top level of a /opt/thisapp directory, you might have an __init__.py (because it's a package) and perhaps a main.py script which starts the real work.
In Python 2.7 and Python 3, you have the runpy module. With this you would name your
top-level main script __main__.py
http://docs.python.org/library/runpy.html#module-runpy
What is a standard way to create modules that allow easy reusability(mycompany.myapp.mymodule?)
Read about packages. http://docs.python.org/tutorial/modules.html#packages
A package is a way of creating a module hierarchy: if you make a file called __init__.py in a directory, Python will treat that directory as a package and allow you to import its contents using dotted imports:
spam \
__init__.py
ham.py
eggs.py
import spam.ham
The modules inside a package can reference each other -- see the docs.
If these are all DB maintenance scripts, I would make a package called DB or something, and place them all in it. You can have subpackages for the more complicated ones. So if you had a script for, I don't know, cleaning up the transaction logs, you could put it in ourDB.clean and do
import ourDB.clean
ourDB.clean.transaction_logs( )

Categories