-
Notifications
You must be signed in to change notification settings - Fork 62
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
copy to returning a stream #103
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6fb4738
create utils and ResponseStream
simon-contreras-deel 43533fc
new copyto_stream method
simon-contreras-deel 3ac03e7
test_copyto_stream
simon-contreras-deel a97fbbc
fix import and const
simon-contreras-deel dbb452d
hound
simon-contreras-deel d9df842
NEWS and v1.5.0
simon-contreras-deel f17129c
example with copy and pandas
simon-contreras-deel a555004
some hound happiness
simon-contreras-deel d1de7b0
undo version upgrade
simon-contreras-deel 773e9cd
Apply suggestions from code review
alrocar bf8a63a
examples requirements
simon-contreras-deel 106d7a3
Merge branch 'master' into copy-stream
simon-contreras-deel d782b79
honor CR
simon-contreras-deel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from io import RawIOBase | ||
|
||
|
||
class ResponseStream(RawIOBase): | ||
simon-contreras-deel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self, response): | ||
self.it = response.iter_content(8 * 1024) | ||
self.leftover = None | ||
|
||
def readable(self): | ||
return True | ||
|
||
def readinto(self, b): | ||
try: | ||
length = len(b) | ||
chunk = self.leftover or next(self.it) | ||
output, self.leftover = chunk[:length], chunk[length:] | ||
b[:len(output)] = output | ||
return len(output) | ||
except StopIteration: | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import argparse | ||
import os | ||
import sys | ||
import logging | ||
import pandas as pd | ||
|
||
from carto.auth import APIKeyAuthClient | ||
from carto.sql import SQLClient | ||
from carto.sql import CopySQLClient | ||
|
||
# Logger (better than print) | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format=' %(asctime)s - %(levelname)s - %(message)s', | ||
datefmt='%I:%M:%S %p') | ||
logger = logging.getLogger() | ||
|
||
|
||
# set input arguments | ||
parser = argparse.ArgumentParser(description='Example of CopySQLClient usage with COPY feature and pandas (file-based interface)') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (130 > 79 characters) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (130 > 120 characters) |
||
|
||
parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL', | ||
default=os.environ.get('CARTO_API_URL', ''), | ||
help='Set the base URL. For example:' + | ||
' https://username.carto.com/ ' + | ||
'(defaults to env variable CARTO_API_URL)') | ||
|
||
parser.add_argument('--api_key', dest='CARTO_API_KEY', | ||
default=os.environ.get('CARTO_API_KEY', ''), | ||
help='Api key of the account' + | ||
' (defaults to env variable CARTO_API_KEY)') | ||
|
||
|
||
args = parser.parse_args() | ||
|
||
# Set authentification to CARTO | ||
if args.CARTO_BASE_URL and args.CARTO_API_KEY: | ||
auth_client = APIKeyAuthClient( | ||
args.CARTO_BASE_URL, args.CARTO_API_KEY) | ||
else: | ||
logger.error('You need to provide valid credentials, run with ' | ||
'-h parameter for details') | ||
sys.exit(1) | ||
|
||
# Create and cartodbfy a table | ||
sqlClient = SQLClient(auth_client) | ||
sqlClient.send(""" | ||
CREATE TABLE IF NOT EXISTS copy_example ( | ||
the_geom geometry(Geometry,4326), | ||
name text, | ||
age integer | ||
) | ||
""") | ||
sqlClient.send("SELECT CDB_CartodbfyTable(current_schema, 'copy_example')") | ||
|
||
copyClient = CopySQLClient(auth_client) | ||
|
||
# COPY FROM example | ||
logger.info("COPY'ing FROM file...") | ||
query = ('COPY copy_example (the_geom, name, age) ' | ||
'FROM stdin WITH (FORMAT csv, HEADER true)') | ||
result = copyClient.copyfrom_file_path(query, 'files/copy_from.csv') | ||
logger.info('result = %s' % result) | ||
|
||
# COPY TO example with pandas DataFrame | ||
logger.info("COPY'ing TO pandas DataFrame...") | ||
query = 'COPY copy_example TO stdout WITH (FORMAT csv, HEADER true)' | ||
result = copyClient.copyto_stream(query) | ||
df = pd.read_csv(result) | ||
logger.info(df.head()) | ||
|
||
# Truncate the table to make this example repeatable | ||
sqlClient.send('TRUNCATE TABLE copy_example RESTART IDENTITY') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
requests>=2.7.0 | ||
pyrestcli>=0.6.4 | ||
configparser>=3.5.0 | ||
pandas>=0.24.2 | ||
../ | ||
prettytable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We need to document the return type. Something like:
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.
👍