Reputation: 1512
How to read data from the end of the source file?
The only way that I have thought of to do this would be to reopen the source from within the program and reading the source as text. There has to be code to deal with .pyc
files.
Is there an easier, more pythonic way to do this? I suppose it might involve some way to make a forward reference in a python source file, like this (which doesn't work).
test_data = gettestdata()
for d in data:
# do all kinds of stuff in may lines of code
def gettestdata():
return \
(many lines of data)
Upvotes: 0
Views: 40
Reputation: 20495
You have one or more "large" strings to fetch, such as SQL CREATE TABLE statements. Bury each one in a getter, using triple-quote syntax. For example:
def get_create_foo():
return """
create table foo (
id integer primary key,
...
);
"""
Then a call like
session.execute(get_create_foo())
is a nice compact one-liner.
For test data, you might want to .split()
or otherwise massage your data so it's easy to iterate over.
For large test data, it probably belongs in a separate file, such as a CSV.
Consider pulling it in with .from_csv()
, which is quite convenient.
Remember that __file__
can tell you where your script is running.
(There are some edge cases, such as when the script is in a .zip on your $PYTHONPATH.)
Here is a standard idiom for finding a CSV in the same folder as your source code:
from pathlib import Path
import os
folder = Path(os.path.abspath(os.path.dirname(__file__)))
with open(folder / 'foo.csv') as file_in:
...
Upvotes: 1
Reputation: 917
Assuming that everything should be inside functions/classes, so the main function exists only for parsing parameters, initiating loggers and other non functional stuff.
Code
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def run(input_data):
return input_data
if __name__ == '__main__':
# initiate loggers
# make other things
data = "qqqqqqqqqq" \
"wwwwwwwwwww" \
"eeeeeeeeeee"
result = run(data)
print(result)
Output
qqqqqqqqqqwwwwwwwwwwweeeeeeeeeee
Is it what you want? If not, sorry, I do not know other 'pythonic' ways.
Upvotes: 1