Comments (6)
Feel free to add a comment to the code:
Find the file that created the Handout object, to include its source code in the output document. The loop walks through the call stack, skips entries that are in
handout.py
which are internal, and breaks at the first external Python file.
from handout.
@danijar - it is more clear from the comment, but is there a possiblity to move dealing with source file into own class?
Handout class is busy constructing the html - I think there can be a separate class that provides access to source file:
class Source:
def __init__(self, info: inspect.FrameInfo):
self._info = info
def filename(self) -> str:
"""Return path to a file where Handout object was created."""
return self._info.filename
def text(self) -> str:
"""Return contents of a file where Handout object was created."""
module = inspect.getmodule(self._info.frame)
return inspect.getsource(module)
def current_line(self) -> int:
"""Return current line in a file where Handout object was created."""
for info in inspect.stack():
if info.filename == self.filename():
return info.lineno
message = (
"Handout object was created in '{}' and accessed in '{}'. The file in "
"which you create the handout will be rendered. Thus, it only makes "
"sense to add to the handout from this file or functions called from "
"this file. You should not pass the handout object to a parent file.")
raise RuntimeError(message.format(self.filename(), info.filename))
class Handout:
def __init__(self, ...):
# ...
# The loop walks through the call stack, skips
# internal entries in handout.py, and breaks at
# the first external Python file. We assume this
# is the file is where a Handout instance was created.
for info in inspect.stack():
if info.filename == __file__:
continue
break
self.source = Source(info)
#later:
self.source.text()
self.source.current_line()
As a benefit I see:
_get_current_line()
moves away from Handout class- the constructor in Handout gets cleaner, for example there is no need to save
self._source_name
from handout.
I'm not sure whether this restructuring is desirable. I think it would make more sense to move everything HTML related into a separate HTMLBackend
and also contains the Block
classes as separate functions. That would open the door to add a LaTeXBackend
in the future. That being said, I'd say it's a bit early to do this refactoring since we don't know if it will be necessary.
from handout.
Handout
minus HTMLBackend
probably equals Source
or similar, imo, but sure differentiating backends is a bigger task than style preferences for Handout
alone.
from handout.
Exactly, that's what I was thinking. On a philosophical level, both could be separated out to separate different sources and targets. But for now I don't see why this would be necessary and I like to defer refactorings until they become necessary, because the "optimal solution" may change until then.
from handout.
Right, refactoring options may be wider than we think now when new concerns emerge. Lets try tackle making a pdf and latex outputs in #18 and #9.
from handout.
Related Issues (20)
- Opt-out of Markdown rendering for some multi-line comments HOT 2
- Support interactive environment in PyCharm where there is no source file HOT 6
- Comparison to Jupyter notebooks HOT 5
- Add contributing instructions HOT 1
- Pandas Dataframe Display Support HOT 4
- Making handout work with Jupyter HOT 3
- Convert jupyter notebook to handout HOT 5
- Use AST module to parse script rather than processing text line by line HOT 7
- Insert page breaks for print layout
- Generate handout every time the Python script is saved HOT 5
- Pathlib error when running setup.py on Linux HOT 8
- steps for porting to Julia HOT 6
- conda package HOT 3
- Possible to hide some code? HOT 1
- doc.show() will execute _generate method and rewrites index.html? HOT 1
- Add collapsible sections
- How to change the HTML page width? HOT 1
- image misplaced to end of file HOT 2
- Atom / VS Code / Sublime Text Extension HOT 1
- Need to install dependencies separately HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from handout.