Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
d28ddc7e
Commit
d28ddc7e
authored
Aug 01, 2008
by
Dag Sverre Seljebotn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
StringIOTree.insert
parent
eaa6d477
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
7 deletions
+34
-7
Cython/StringIOTree.py
Cython/StringIOTree.py
+34
-7
No files found.
Cython/StringIOTree.py
View file @
d28ddc7e
...
...
@@ -7,8 +7,7 @@ class StringIOTree(object):
def
__init__
(
self
,
stream
=
None
):
self
.
prepended_children
=
[]
if
stream
is
None
:
stream
=
StringIO
()
self
.
stream
=
stream
self
.
stream
=
stream
# if set to None, it will be constructed on first write
def
getvalue
(
self
):
return
(
""
.
join
([
x
.
getvalue
()
for
x
in
self
.
prepended_children
])
+
...
...
@@ -19,20 +18,44 @@ class StringIOTree(object):
needs to happen."""
for
child
in
self
.
prepended_children
:
child
.
copyto
(
target
)
if
self
.
stream
:
target
.
write
(
self
.
stream
.
getvalue
())
def
write
(
self
,
what
):
if
not
self
.
stream
:
self
.
stream
=
StringIO
()
self
.
stream
.
write
(
what
)
def
commit
(
self
):
# Save what we have written until now so that the buffer
# itself is empty -- this makes it ready for insertion
if
self
.
stream
:
self
.
prepended_children
.
append
(
StringIOTree
(
self
.
stream
))
self
.
stream
=
None
def
insert
(
self
,
iotree
):
"""
Insert a StringIOTree (and all of its contents) at this location.
Further writing to self appears after what is inserted.
"""
self
.
commit
()
self
.
prepended_children
.
append
(
iotree
)
def
insertion_point
(
self
):
"""
Returns a new StringIOTree, which is left behind at the current position
(it what is written to the result will appear right before whatever is
next written to self).
Calling getvalue() or copyto() on the result will only return the
contents written to it.
"""
# Save what we have written until now
# (would it be more efficient to check with len(self.stream.getvalue())?
# leaving it out for now)
self
.
prepended_children
.
append
(
StringIOTree
(
self
.
stream
))
# This is so that getvalue on the result doesn't include it.
self
.
commit
()
# Construct the new forked object to return
other
=
StringIOTree
()
self
.
prepended_children
.
append
(
other
)
self
.
stream
=
StringIO
()
return
other
__doc__
=
r"""
...
...
@@ -65,12 +88,16 @@ beta
gamma
<BLANKLINE>
>>> i = StringIOTree()
>>> d.insert(i)
>>> i.write('inserted\n')
>>> out = StringIO()
>>> a.copyto(out)
>>> print out.getvalue()
first
second
alpha
inserted
beta
gamma
third
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment