Use a python file from a separate maven module - python

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?

Related

How do I get the list of imports and dependant files from python script?

I have a .py file that imports from other python modules that import from config files, other modules, etc.
I am to move the code needed to run that .py file, but only whatever the py file is reading from (I am not talking about packages installed by pip install, it's more about other python files in the project directory, mostly classes, functions and ini files).
Is there a way to find out only the external files used by that particular python script? Is it something that can be found using PyCharm for example?
Thanks!
Static analysis tools (such as PyCharm's refactoring tools) can (mostly) figure out the module import tree for a program (unless you do dynamic imports using e.g. importlib.import_module()).
However, it's not quite possible to statically definitively know what other files are required for your program to function. You could use Python's audit events (or strace/ptrace or similar OS-level functions) to look at what files are being opened by your program (e.g. during your tests being run (you do have tests, right?), or during regular program use), but it's likely not going to be exhaustive.

Not able to access the keywords in .robot file in one directory from other directory after creating python package

Not able to access the keywords in .robot file in one directory from other directory after creating python package. Although I am able to access methods of .py but not .robot file.
I have created Python package(which contains .robot test suite) so that I can reuse the keywords in that particular package in any other project by simply importing the Library anywhere
I have successfully created and installed package(named as Resources which contains some .robot test suites and .py files). Now I want to use the keywords from 'Resources' package in some other Robot project. Here, problem is when I write below statement:
Library Resources
I am not able to use the keyword given in Resources package. But If I import that particular robot file, them I am able to access the keyword: e.g
Resource ../../../../Documents/MyWorkspace/RobotFramework/Resources/PostLoginSearch.robot
Here I do not want to import using above absolute path since it can be done without creating package also. So is it possible to use keywords by only importing Resources package.
Also I have successfully created and installed "Resources" package and I could see the version also. I am able to call the python methods but unable to access the keyword in Resources .robot file after importing "Resources" package
And this "Resources" is in some other project
enter image description here
creating test libraries
Library search path
The most common way to specify a test library to import is using its name. The Robot Framework tries to find the class or module implementing the library from the library search path. Basically, this means that the library code and all its possible dependencies must be in PYTHONPATH. If your library is in PYTHONPATH environment variable then use
Library <<name of your custom library>>
Updated after introspection
i do not think, this is allowed in robot, since, by definition , when one creates python user-defined library, it would be containing only .py files, since, they are custom-library.
On the other hand, resource files provide a mechanism to User keywords and variables created in the robot which contains .robot extension.
These are 2 different aspects, I feel that you are combining 2 different things which should not be.
Maybe an alternative approach is to define all the common keywords(not custom *.py) files as part of the testsuite itself, so that, when any test case(s)/ modules are calling they would get inherited at once.
Better Approach
Group all your common keywords into one file(s), make it available for all the testcases to consume i.e using Resource under settings section.
Create Custom-test Library and put it under PYTHONPATH and access those methods as keywords as well.

Fully embedded SymPy+Matplotlib+others within a C/C++ application

I've read the Python documentation chapter explaining how to embed the Python interpreter in a C/C++ application. Also, I've read that you can install Python modules either in a system-wide fashion, or locally to a given user.
But let's suppose my C/C++ application will use some Python modules such as SymPy, Matplotlib, and other related modules. And let's suppose end users of my application won't have any kind of Python installation in their machines.
This means that my application needs to ship with "pseudo-installed" modules, inside its data directories (just like the application has a folder for icons and other resources, it will need to have a directory for Python modules).
Another requirement is that the absolute path of my application installation isn't fixed: the user can "drag" the application bundle to another directory and it will run fine there (it already works this way but that's prior to embedding Python in it, and I wish it continues being this way after embedding Python).
I guess my question could be expressed more concisely as "how can I use Python without installing Python, neither system-wide, nor user-wide?"
There are various ways you could attempt to do this, but none of them are general solutions. From the (docs):
5.5. Embedding Python in C++
It is also possible to embed Python in a C++ program; precisely how this is done will depend on the details of the C++ system used; in general you will need to write the main program in C++, and use the C++ compiler to compile and link your program. There is no need to recompile Python itself using C++.
This is the shortest section in the document, and is roughly equivalent to: 'left as an exercise for the reader`. I do not believe you will find any straight forward solutions.
Use pyinstaller to gather the pieces:
This means that my application needs to ship with "pseudo-installed" modules, inside its data directories (just like the application has a folder for icons and other resources, it will need to have a directory for Python modules).
If I needed to tackle this problem, I would use pyinstaller as a base. (Disclosure: I am an occasional contributer). One of the major functions of pyinstaller is to gather up all of the needed resources for a python program. In onedir mode, all of the things needed to let the program run are gathered into one directory.
You could include this tool into your make system, and have it place all of the needed pieces into your python data directory in your build tree.

Python init file name convention

I started a new Python project and I want to have a good structure from the beginning. I'm reading some convention Python guides but I don't find any info about how the main script must be named. Is there any rules for this? Is there any other kind of convention for folders or text files inside the project (like readme files)?
By the way, I'm programming a client-server app so there is no way for this to become a package (at least in the way a think a package is).
If you want to package your application to allow a ZIP file containing it or its directory to be passed as an argument to the python interpreter to run the application, name your main script __main__.py. If you don't care about being able to do this (and most python applications do not), name it whatever you want.
No such rule exists for python main script which starts your application. There are coding guidelines (PEP8) which you can follow to keep your code clean though.
You can check existing python applications which are easily available. May be open source/free software projects e.g yum (on rpm based distros) command, lots of python apps (you can checkout them from publicly available source code management systems e.g git repo) etc. You can check basic principles they follow. But there are no constraints as such.

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