text stringlengths 0 828 |
|---|
name |
))" |
862,"def find_one(self, cls, id): |
""""""Find single keyed row - as per the gludb spec."""""" |
found = self.find_by_index(cls, 'id', id) |
return found[0] if found else None" |
863,"def find_by_index(self, cls, index_name, value): |
""""""Find all rows matching index query - as per the gludb spec."""""" |
cur = self._conn().cursor() |
# psycopg2 supports using Python formatters for queries |
# we also request our JSON as a string for the from_data calls |
query = 'select id, value::text from {0} where {1} = %s;'.format( |
cls.get_table_name(), |
index_name |
) |
found = [] |
with self._conn() as conn: |
with conn.cursor() as cur: |
cur.execute(query, (value,)) |
for row in cur: |
id, data = str(row[0]).strip(), row[1] |
obj = cls.from_data(data) |
assert id == obj.id |
found.append(obj) |
return found" |
864,"def save(self, obj): |
""""""Save current instance - as per the gludb spec."""""" |
cur = self._conn().cursor() |
tabname = obj.__class__.get_table_name() |
index_names = obj.__class__.index_names() or [] |
col_names = ['id', 'value'] + index_names |
value_holders = ['%s'] * len(col_names) |
updates = ['%s = EXCLUDED.%s' % (cn, cn) for cn in col_names[1:]] |
if not obj.id: |
id = uuid() |
obj.id = id |
query = 'insert into {0} ({1}) values ({2}) on conflict(id) do update set {3};'.format( |
tabname, |
','.join(col_names), |
','.join(value_holders), |
','.join(updates), |
) |
values = [obj.id, obj.to_data()] |
index_vals = obj.indexes() or {} |
values += [index_vals.get(name, 'NULL') for name in index_names] |
with self._conn() as conn: |
with conn.cursor() as cur: |
cur.execute(query, tuple(values))" |
865,"def delete(self, obj): |
""""""Required functionality."""""" |
del_id = obj.get_id() |
if not del_id: |
return |
cur = self._conn().cursor() |
tabname = obj.__class__.get_table_name() |
query = 'delete from {0} where id = %s;'.format(tabname) |
with self._conn() as conn: |
with conn.cursor() as cur: |
cur.execute(query, (del_id,))" |
866,"def authenticated_get(username, password, url, verify=True): |
"""""" |
Perform an authorized query to the url, and return the result |
"""""" |
try: |
response = requests.get(url, auth=(username, password), verify=verify) |
if response.status_code == 401: |
raise BadCredentialsException( |
""Unable to authenticate user %s to %s with password provided!"" |
% (username, url)) |
except requests.exceptions.SSLError: |
raise CertificateException(""Unable to verify certificate at %s!"" % url) |
return response.content" |
867,"def cleaned_request(request_type, *args, **kwargs): |
"""""" Perform a cleaned requests request """""" |
s = requests.Session() |
# this removes netrc checking |
s.trust_env = False |
return s.request(request_type, *args, **kwargs)" |
868,"def download_to_bytesio(url): |
"""""" Return a bytesio object with a download bar """""" |
logger.info(""Downloading url: {0}"".format(url)) |
r = cleaned_request('get', url, stream=True) |
stream = io.BytesIO() |
total_length = int(r.headers.get('content-length')) |
for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1): |
if chunk: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.