How to read $xyz value from .properties file in python? - python

I have a build.properties file like
[directory]
src = src
srcdir = ${src}/fix
srcext = ${srcdir}/extensions
srct = ${srcext}/xyz
my aim is to get the full value of srct i mean it is to be src/fix/extensions/xyz
Is there any way of getting the value of cde? I tried configparser and jproperties but i couldn't get the required output.

You can use ExtendedInterpolation class which implements more advanced syntax.
>>> from configparser import ConfigParser, ExtendedInterpolation
>>>
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
>>> parser.read("build.properties")
['build.properties']
>>> print(parser["directory"]["srct"])
src/fix/extensions/xyz

Related

Is it possible to find out from which module a class instance come from?

I would like to know if it is possible to obtain the module name and directory path from a class instance of that module.
I need that because I save a class instance with pickle and I need to know from where this class come from when I load it from another software (on the same computer).
update
So I tried the aswer of #albar
>>> from Models import Model_physio_moyen
>>> b = Model_physio_moyen.pop_Sigmoid_outside_noise
>>> module_name = sys.modules[b.__module__].__name__
>>> module_name
'Models.Model_physio_moyen'
So that working except if the class has been compiled with jitclassof numba:
>>> from Models.Pops_Stim import ATN
>>> a = ATN.pop_ATN()
>>> module_name = sys.modules[a.__module__].__name__
>>> module_name
'numba.jitclass.boxing'
In this condition, I wish I could find Models.Pops_Stim but I'm getting 'numba.jitclass.boxing'instead.
Assuming your instance is called a:
import sys
module_name = sys.modules[a.__module__].__name__
module_file = sys.modules[a.__module__].__file__

Extended interpolation not working in configparser

I tried to use configparser module from standard library in python 3.6 or python 3.5.1
My ini file looks like this:
[common]
domain = http://some_domain_name:8888
about = about/
loginPath = /accounts/login/?next=/home
fileBrowserLink = /filebrowser
partNewDirName = some_dir
[HUE_310]
partNewFilePath = ${common:domain}
My "main" program looks like this:
from configparser import ConfigParser
parser = ConfigParser()
parser.read('configfile.ini')
lll = parser.get('HUE_310', 'partNewFilePath')
print(lll)
Instead http://some_domain_name:8888 I got ${common:domain}
I use Pycharm Community as my IDE. I use virtualenv.
I have no idea what is wrong with my code...
If you want extended interpolation, you have to create an instance of the configparser.ExtendedInterpolation class, by calling it, and then using that with the interpolation= keyword argument when you create the ConfigParser instance as shown below:
from configparser import ConfigParser, ExtendedInterpolation
parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read('configfile.ini')
lll = parser.get('HUE_310', 'partNewFilePath')
print(lll) # -> http://some_domain_name:8888

Find Python module filename

I got module name which contains declaration of os.path.isfile. Jedi lib gave me genericpath (without file path). Now i want to get full filename of PY file with this genericpath module. E.g. "C:\Py27\Lib\genericpath.py". How can I do it? Jedi cannot do it?
You could check the value of __file__:
>>> import genericpath
>>> genericpath.__file__
'/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/genericpath.pyc'
When __file__ doesn't work, do it the proper way—use inspect:
>>> import somemodule
>>> import inspect
>>> inspect.getfile(somemodule)
'/usr/lib64/python2.7/somemodule.pyc'
Like this:
>>> import re
>>> re.__file__
'/usr/lib/python2.7/re.pyc'
For packages that are not part of the Python core, you can also use __path__:
>>> import requests
>>> requests.__file__
'/usr/local/lib/python2.7/dist-packages/requests-1.1.0-py2.7.egg/requests/__init__.pyc'
>>> requests.__path__
['/usr/local/lib/python2.7/dist-packages/requests-1.1.0-py2.7.egg/requests']
Get module filename without loading it:
print(importlib.util.find_spec("urllib.request", None).origin)

Dynamically import and create class instance

With my python application, I have 40 modules (classes) which contain a parser for some text. In my function I only want to instanciate and use a particular module. These are all sorted in the database.
I am at the point now where I know my parser, and have both the python file name and class I want to import and create
However.... How do you actually do this in python?
eg;
file_name = 'lex_parser'
class_name = 'LexParser'
how can I do....
from {file_name} import {class_name}
Parser = {class_name}()
Follow what I mean?
Try this:
file_name = 'lex_parser'
class_name = 'LexParser'
Parser = getattr(__import__(file_name), class_name)
Note that file_name must not contain .py.
This won't work if the module is within a package because __import__ would return the top level package. In that case you can do this:
import sys
file_name = 'parsers.lex_parser'
class_name = 'LexParser'
__import__(file_name)
Parser = getattr(sys.modules[file_name], class_name)
This will work in both cases and is recommeded by the __import__ function documentation.
In both examples Parser is a class which you have to instantiate as normally:
parser = Parser()
How about something like this:
module = __import__('my_module')
if hasattr(module, 'ClassName'):
ClassName = module.ClassName
my_object = ClassName()

Python : How to import a module if I have its path as a string?

Lets say I have path to a module in a string module_to_be_imported = 'a.b.module'
How can I import it ?
>>> m = __import__('xml.sax')
>>> m.__name__
'xml'
>>> m = __import__('xml.sax', fromlist=[''])
>>> m.__name__
'xml.sax'
You can use the build-in __import__ function. For example:
import sys
myconfigfile = sys.argv[1]
try:
config = __import__(myconfigfile)
for i in config.__dict__:
print i
except ImportError:
print "Unable to import configuration file %s" % (myconfigfile,)
For more information, see:
Python Documentation
Python.org - [Python Wpg] Import a module using a string
x = __import__('a.b.module', fromlist=[''])
Reference

Categories

Resources