2

There are several questions related to reading python code from external files, including

Python: How to import other Python files

How to include external Python code to use in other files?

But it's not clear how to include snippets of text from an external file, as is standard practice with the CPP #include directive. For example, the following code:

def get_schema():
    schema1 = \
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "0",
    "type": "object",
    "properties": {
        "i": { "type": "integer" },
        "n": { "type": "string" }
    }
}
    return(schema1)

s = get_schema()
print(s)

returns the expected dict:

{'id': '0', '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'properties': {'i': {'type': 'integer'}, 'n': {'type': 'string'}}}

But what I want to do is write the code so that it imports variable definitions from external files that do not include any python code (function definitions or variable assignments):

def get_schema():
    schema1 = \
#include "schema1.json"
    return(schema1)

s = get_schema()
print(s)

I'm sure this could be done with a bunch of code to open the definition files, read the contents into a string, add pre- and post-amble lines of python, and then exec the string, but it seems that there should be a simpler way to just include text in a function definition. Is there?

Community
  • 1
  • 1
Dave
  • 3,834
  • 2
  • 29
  • 44
  • 1
    Python has no preprocessor so you cannot include text that is later compiled as you can in C. There is no `#ifdef` for instance. Although you can skip executing a block with `if 0` the block is compiled into byte code and takes up space in the executing image. – tdelaney Feb 12 '16 at 05:26
  • Not sure what you are after exactly. It seems to me that you have a problem, thought of a possible solution (using a c/c++ paradigm) and want to implement that in python. Maybe if you explain you original problem better, we can provide a pythonic solution. Could you provide a more general example of the kind of things you would put in the external file? I see from your comment below that you are interested in something more general than loading JSON data. – jorgeh Feb 12 '16 at 05:29
  • For the general case "it can't be done" is a reasonable answer. Separating the development and configuration management of json files from Python files seems like a good practice, and if that pattern ever comes up again for a different type of data (perhaps constant definitions?), knowing that the correct approach is to use or write an import routine is better than wondering if there is a built-in alternative. – Dave Feb 12 '16 at 12:15
  • @jorgeh, it struck me as remarkable that a subset of syntactically valid json strings are also syntactically valid Python rvalues. Two questions follow from that: 1) are there any valid json strings that are not valid directly-executable Python, and 2) are there any other independently-developed data formats that are subelements of Python syntax. But given that Python cannot import anything smaller than a module, no further thought needs to be given to those questions. – Dave Feb 12 '16 at 13:03
  • @Dave, there is a least a case where valid JSON is not valid python: `{"flag": true}`. JSON accepts lower-case `true`, but python will fail with that. There might be other cases, but at least this one answers your first question. I have no clue about your second question though. – jorgeh Feb 13 '16 at 22:30

1 Answers1

2

If you want to load json from a file you can use json library:

import json

def get_schema():
    with open('schema1.json', 'r') as f:    
        return json.load(f)
midori
  • 4,807
  • 5
  • 34
  • 62
  • You might want to change `'schema1'` to `'schema1.json'`. Otherwise, if this doesn't answer the question, clarification will be needed (imo). – Jared Goguen Feb 12 '16 at 04:57
  • i agree, but i don't know for sure if his file has json extension – midori Feb 12 '16 at 04:58
  • Yes this answers my immediate use case of importing json. But I'm still interested in the general question of whether it is possible to import units smaller than a complete module in python the way header files are imported into C programs. – Dave Feb 12 '16 at 05:08
  • i don't think it's possible in python @Dave – midori Feb 12 '16 at 05:11