-1

I have the below python file "source.py", which is having dictionary(default_args) in it. I would like to extract only default_args dictionary from another python file "destination.py"

source.py:

from datetime import timedelta
import os

default_args = {
    'owner': 'kumar',
    'team': 'data_engineering',
    'start_date': timezone.datetime(2018, 4, 9, 0, 0, 0, tzinfo=LOCAL_TZ),
    'provide_context': True,
    'environment': ENV,
    'email': 'venkatsiva789@gmail.com',
    'email_on_failure': True,
}

destination.py:

 text = open("source.py").read()
print(text)

I want to extract only the below code snippet.

default_args = {
    'owner': 'kumar',
    'team': 'data_engineering',
    'start_date': timezone.datetime(2018, 4, 9, 0, 0, 0, tzinfo=LOCAL_TZ),
    'provide_context': True,
    'environment': ENV,
    'email': 'venkatsiva789@gmail.com',
    'email_on_failure': True,
}

Please help to extract the above code snippet or let me know is there any built in module to extract specific dictionary.

shiva
  • 1
  • 2
  • Is there any reason you cannot import the module and access the dictionary like `from source import default_args`? – FallenWarrior Jan 16 '20 at 14:45
  • because, I get this file name as an argument with absolute path like "c://python/test/source.py" – shiva Jan 16 '20 at 15:08
  • You _can_ [import a module given its full path](https://stackoverflow.com/q/67631/1782792). If you still do not want to import it (e.g. you do not want to run the code in it), you can grab the value of that assignment easily with the [`ast`](https://docs.python.org/3/library/ast.html) module. However, since the value is not made of just literals (has a function call and and `ENV` variable, which I'm not sure where is defined), you will not be able to get the value without actually running its code. – jdehesa Jan 16 '20 at 15:35

2 Answers2

1

Let's say you have another file main.py where you want this dictionary, so:

# main.py

import source  # full path if needed

default_args = source.default_args
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • thanks for reply. I do not want to import the file, I need to open the file from the system and extract the specific part. – shiva Jan 16 '20 at 14:48
  • Why? You can import at any point fo your code, so why read it in instead of just importing? – LeoE Jan 16 '20 at 14:52
  • @shiva if you read it as text, that's an unuseful piece of text, that's not a valid json, and you will have to use `eval` to use it as `dict`, so why? – Gocht Jan 16 '20 at 14:56
  • I want dict object or else if i extract complete dictionary as a string then I can convert it to dict. – shiva Jan 16 '20 at 15:02
  • @shiva if you import and do `default_args = source.default_args` you have the complete dict – Gocht Jan 16 '20 at 15:03
  • because, I get this file name as an argument with absolute path like "c://python/test/source.py" – shiva Jan 16 '20 at 15:09
  • 2
    you can still [import a module given the full path](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – Pablo Jan 16 '20 at 15:15
  • Does this help? https://stackoverflow.com/questions/8718885/import-module-from-string-variable – LeoE Jan 16 '20 at 15:15
  • @shiva import_module will do the trick https://docs.python.org/3/library/importlib.html#importlib.import_module – Gocht Jan 16 '20 at 15:22
0

If you want to use it as a dict see @Gocht's answer. If you want it as a string you could use regex:

import re
re.findall(r'default_args(?:.|\s)+}', text)
LeoE
  • 2,054
  • 1
  • 11
  • 29