-
Notifications
You must be signed in to change notification settings - Fork 31
output generator #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
output generator #13
Conversation
4371c7a to
1da434b
Compare
Codecov Report
@@ Coverage Diff @@
## master #13 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 10 10
Lines 268 276 +8
=====================================
+ Hits 268 276 +8
Continue to review full report at Codecov.
|
|
@meeuw 👍 I like this! I'm going to look at the format number updates that Irina made and then I'll take a look at this. |
| output.append('\n') | ||
|
|
||
| return ''.join(output) | ||
| yield _get_separator(i, sep_title, sep_character, sep_length) + result + '\n' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@meeuw I don't think we need the newline character any longer since we're returning each record separately. It's now the client code's job to insert a newline between each record. Then you can remove the extra blank line you had to add to the tests.
|
@meeuw Other than that comment I made, this looks good to me. Great job! So, what about the tabulate and delimited output adapters? Since this is a breaking change (mycli doesn't work without a change to support this), we're going to need to bump the version of CLI Helpers to 1.0. Plus, we need to consider pgcli as well, which will soon depend on CLI Helpers. I'm thinking we'll release v0.2 of CLI Helpers with the format_numbers and style_output change. Along with updating mycli to depend on |
1da434b to
48b816f
Compare
|
Hmm the changes I've made in mycli are made so it doesn't break with the current version of cli_helpers (but supports the changes in this branch). But maybe it's indeed better to only return generators/iterators from cli_helpers. I'm all in for doing this for the 1.0 release, that will give us some time to update the other output adapters to return generators. |
|
@meeuw Ok, that sounds good. So, we can push the mycli change out with the next release so it will be ready whenever cli_helpers 1.0 is released. |
|
@meeuw I do think we should return a consistent format from CLI Helpers. |
48b816f to
9824a19
Compare
9824a19 to
1bfd86b
Compare
9312a28 to
61800c7
Compare
| l.reset() | ||
| writer.writerow(headers) | ||
| for line in l.lines: | ||
| yield line[:-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than stripping the newline by index, we can tell the csv module not to add newlines at all.
On line 32:
ckwargs = {'delimiter': delimiter, 'lineterminator': ''}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, lineterminator needs to be removed from line 23 since client code should not be able to specify a line ending (since we return a generator with no newlines).
| writer = csv.writer(l, **ckwargs) | ||
| l.reset() | ||
| writer.writerow(headers) | ||
| for line in l.lines: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about putting the loop and yield in a linewriter.readlines() function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's possible without leaving a loop in adapter (yield is bound to the function/method as far as I know). I've removed the loop; I assume every writerow directly calls our write function (with all the data we've supplied).
|
|
||
| class linewriter(): | ||
| def reset(self): | ||
| self.lines = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since linewriter requires the lines attribute to work, it should create that attribute on instantiation. reset() can be used to clear the lines.
Also, in Python 3, classes are always new-style. Just to be safe we should probably make this a new-style class in Python 2: class linewriter(object):
|
@meeuw I'm thinking we should make all the adapters return generators instead of just lists (for tabulate/terminaltable). What do you think? |
5248c36 to
4578e32
Compare
|
Hmm it doesn't add anything but I think you're right that it's better to have consistent output. pgcli also needs changes before it can use cli_helpers, I'll try to create a PR for pgcli as well. |
| l = linewriter() | ||
| writer = csv.writer(l, **ckwargs) | ||
| writer.writerow(headers) | ||
| yield l.line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this 😄
| t.padding_left, | ||
| t.padding_right)[:3] | ||
| for row in t.gen_table(*dimensions): | ||
| yield ''.join(''.join(row)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an extra join here? From what I can tell, it looks like a single join is all that's needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, "row" is a bit confusing because it's not a row; I've copied this from:
https://github.com/Robpol86/terminaltables/blob/master/terminaltables/build.py#L151
It is called by https://github.com/Robpol86/terminaltables/blob/master/terminaltables/base_table.py#L217
I'll have a look if I can replace this by itertools.chain.from_iterable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they are using two joins because they join the pieces of the row together, then they join all the rows together.
We only need to join the pieces of the row together. If you put logging in you should see that the first join is returning a string, not a list. At least that's what I was seeing yesterday when I was testing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, you're right, thanks!
|
@meeuw Ok, this looks good other than that last comment I left about the |
4578e32 to
782a79d
Compare
782a79d to
310088e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks!
I'm marking it as approved, but let's not merge it until pgcli is ready.
|
pgcli is ready! |
Description
output generator
Checklist
CHANGELOG.