Esempio n. 1
0
    def test_credentials_baseurl(self):
        """credentials.Credentials carto.com base_url"""
        creds = Credentials(key=self.key, base_url=self.base_url)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), None)
        self.assertEqual(creds.base_url(), self.base_url)
Esempio n. 2
0
    def test_credentials_no_args(self):
        """credentials.Creentials load default cred file"""
        creds = Credentials()

        self.assertEqual(creds.key(), self.default['key'])
        self.assertEqual(creds.username(), self.default['username'])
        self.assertEqual(creds.base_url(), self.default['base_url'].strip('/'))
Esempio n. 3
0
    def test_credentials_delete(self):
        """credentials.Credentials.delete"""

        local_cred_file = './test_cred_file_{}.json'.format(
                str(time.time())[-4:])

        # create a credentials file
        with open(local_cred_file, 'w') as f:
            json.dump({'base_url': self.base_url,
                       'key': self.key},
                      f)

        # check if it's there
        self.assertTrue(os.path.isfile(local_cred_file))

        # load credentials file
        creds = Credentials(cred_file=local_cred_file)

        # delete credentials file
        creds.delete(local_cred_file)

        # ensure it's deleted
        self.assertTrue(not os.path.isfile(local_cred_file))

        self.assertIsNone(creds.delete('non_existent_file'))
Esempio n. 4
0
    def test_credentials(self):
        """credentials.Credentials common usage"""
        creds = Credentials(key=self.key, username=self.username)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), self.username)
        self.assertEqual(creds.base_url(), self.base_url)
Esempio n. 5
0
    def test_credentials_constructor(self):
        """credentials.Credentials object constructor"""
        creds1 = Credentials(key='abc123', username='******')
        creds2 = Credentials(creds=creds1)

        self.assertEqual(creds1.key(), creds2.key())
        self.assertEqual(creds1.username(), creds2.username())
        self.assertEqual(creds1.base_url(), creds2.base_url())
Esempio n. 6
0
    def test_credentials_onprem_baseurl(self):
        """credentials.Credentials on-prem-style base_url"""
        creds = Credentials(key=self.key,
                            username=self.username,
                            base_url=self.onprem_base_url)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), self.username)
        self.assertEqual(creds.base_url(), self.onprem_base_url.strip('/'))
Esempio n. 7
0
def predict(username,api_key, input_table, output_table):
    creds = Credentials(username=username, key=api_key)
    cc = CartoContext(creds=creds)
    data_obs_measures = [{'numer_id': 'us.census.acs.B25058001'},
			 {'numer_id': 'us.census.acs.B25071001'},
			 {'numer_id': 'us.census.acs.B25081002'},
			 ]
    locations = cc.data(input_table, data_obs_measures)

    features = locations[['minimum_nights', 'review_scores_accuracy', 'review_scores_location']]
    do_features = locations[['mortgaged_housing_units_2011_2015_by_owner_occupied_housing_uni', 'percent_income_spent_on_rent_2011_2015','median_rent_2011_2015']].fillna(0)

    normed_do = pd.DataFrame(augmented_data_min_max_scaler.transform(do_features), columns=do_features.columns)
    normed_features = pd.DataFrame(features_scaler.transform(features), columns=features.columns)


    do_and_bnb_features =  pd.merge(normed_features,normed_do, left_index=True, right_index=True)
    logger.info('Have data, running model')

    result = full_model.predict(do_and_bnb_features)
    logger.info('model run')

    denormed_results = target_scaler.inverse_transform(np.exp(result.reshape(-1,1)))
    logger.info('have results')
    locations = locations.assign(prediciton = denormed_results)
    logger.info('writing back to ', )
    cc.write(locations,output_table, overwrite=True, privacy='public')
    logger.info('all done')
    return "https://team.carto.com/u/{}/dataset/{}".format(username,output_table)
Esempio n. 8
0
 def setUp(self):
     # remove default credential file
     if os.path.exists(_DEFAULT_PATH):
         os.remove(_DEFAULT_PATH)
     # delete path for user config data
     if os.path.exists(_USER_CONFIG_DIR):
         os.rmdir(_USER_CONFIG_DIR)
     self.key = 'seaturtlexyz'
     self.username = '******'
     self.base_url = 'https://loggerhead.carto.com/'
     self.onprem_base_url = 'https://turtleland.com/user/{}'.format(
             self.username)
     self.default = {
         'key': 'default_key',
         'username': '******',
         'base_url': 'https://default_username.carto.com/'
     }
     self.default_cred = Credentials(**self.default)
     self.default_cred.save()
Esempio n. 9
0
    def test_credentials_baseurl(self):
        """credentials.Credentials carto.com base_url"""
        creds = Credentials(key=self.key,
                            base_url=self.base_url)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), None)
        self.assertEqual(creds.base_url(), self.base_url.strip('/'))

        with self.assertRaises(ValueError):
            creds = Credentials(
                key=self.key,
                base_url=self.base_url.replace('https', 'http')
            )
Esempio n. 10
0
    def test_credentials_cred_file(self):
        """credentials.Credentials cred_file"""
        local_cred_file = './test_cred_file_{}.json'.format(
            str(time.time())[-4:])

        # create a credential file with arbitrary name
        with open(local_cred_file, 'w') as f:
            json.dump({'base_url': self.base_url, 'key': self.key}, f)
        creds = Credentials(cred_file=local_cred_file)
        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.base_url(), self.base_url)
        creds.delete(local_cred_file)
Esempio n. 11
0
class TestCredentials(unittest.TestCase):
    """Tests for functions in keys module"""
    def setUp(self):
        # remove default credential file
        if os.path.exists(_DEFAULT_PATH):
            os.remove(_DEFAULT_PATH)
        # delete path for user config data
        if os.path.exists(_USER_CONFIG_DIR):
            os.rmdir(_USER_CONFIG_DIR)
        self.key = 'seaturtlexyz'
        self.username = '******'
        self.base_url = 'https://loggerhead.carto.com/'
        self.onprem_base_url = 'https://turtleland.com/user/{}'.format(
                self.username)
        self.default = {
            'key': 'default_key',
            'username': '******',
            'base_url': 'https://default_username.carto.com/'
        }
        self.default_cred = Credentials(**self.default)
        self.default_cred.save()

    def tearDown(self):
        self.default_cred.delete()
        if os.path.exists(_USER_CONFIG_DIR):
            os.rmdir(_USER_CONFIG_DIR)

    def test_credentials(self):
        """credentials.Credentials common usage"""
        creds = Credentials(key=self.key,
                            username=self.username)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), self.username)
        self.assertEqual(creds.base_url(), self.base_url.strip('/'))

    def test_credentials_constructor(self):
        """credentials.Credentials object constructor"""
        creds1 = Credentials(key='abc123', username='******')
        creds2 = Credentials(creds=creds1)

        self.assertEqual(creds1.key(), creds2.key())
        self.assertEqual(creds1.username(), creds2.username())
        self.assertEqual(creds1.base_url(), creds2.base_url())

    def test_credentials_baseurl(self):
        """credentials.Credentials carto.com base_url"""
        creds = Credentials(key=self.key,
                            base_url=self.base_url)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), None)
        self.assertEqual(creds.base_url(), self.base_url.strip('/'))

        with self.assertRaises(ValueError):
            creds = Credentials(
                key=self.key,
                base_url=self.base_url.replace('https', 'http')
            )

    def test_credentials_onprem_baseurl(self):
        """credentials.Credentials on-prem-style base_url"""
        creds = Credentials(key=self.key,
                            username=self.username,
                            base_url=self.onprem_base_url)

        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.username(), self.username)
        self.assertEqual(creds.base_url(), self.onprem_base_url.strip('/'))

    def test_credentials_no_args(self):
        """credentials.Creentials load default cred file"""
        creds = Credentials()

        self.assertEqual(creds.key(), self.default['key'])
        self.assertEqual(creds.username(), self.default['username'])
        self.assertEqual(creds.base_url(), self.default['base_url'].strip('/'))

    def test_credentials_invalid_key(self):
        """credentials.Credentials invalid key"""
        self.default_cred.delete()
        with self.assertRaises(RuntimeError,
                               msg='Did not specify key'):
            Credentials(username=self.username)

    def test_credentials_cred_file(self):
        """credentials.Credentials cred_file"""
        local_cred_file = './test_cred_file_{}.json'.format(
                str(time.time())[-4:])

        # create a credential file with arbitrary name
        with open(local_cred_file, 'w') as f:
            json.dump({'base_url': self.base_url,
                       'key': self.key},
                      f)
        creds = Credentials(cred_file=local_cred_file)
        self.assertEqual(creds.key(), self.key)
        self.assertEqual(creds.base_url(), self.base_url.strip('/'))
        creds.delete(local_cred_file)

    def test_credentials_set(self):
        """credentials.Credentials.set"""
        new_creds = {
                'username': '******',
                'key': 'abcdefg'
                }
        self.default_cred.set(**new_creds)

    def test_credentials_key(self):
        """credentials.Credentials.key"""

        creds = Credentials(key='abcdefg', username='******')
        creds.key('hijklmnop')
        self.assertEqual(creds.key(), 'hijklmnop')

    def test_credentials_retrieve(self):
        """credentials.Credentials.retrieve"""
        local_cred_file = './test_cred_file_{}.json'.format(
                str(time.time())[-4:])

        # create a credential file with arbitrary name
        with open(local_cred_file, 'w') as f:
            json.dump({'base_url': self.base_url,
                       'key': self.key},
                      f)
        self.default_cred._retrieve(local_cred_file)
        self.assertEqual(self.default_cred.key(), self.key)
        self.assertEqual(self.default_cred.username(), None)
        self.assertEqual(self.default_cred.base_url(), self.base_url)

    def test_credentials_delete(self):
        """credentials.Credentials.delete"""

        local_cred_file = './test_cred_file_{}.json'.format(
                str(time.time())[-4:])

        # create a credentials file
        with open(local_cred_file, 'w') as f:
            json.dump({'base_url': self.base_url,
                       'key': self.key},
                      f)

        # check if it's there
        self.assertTrue(os.path.isfile(local_cred_file))

        # load credentials file
        creds = Credentials(cred_file=local_cred_file)

        # delete credentials file
        creds.delete(local_cred_file)

        # ensure it's deleted
        self.assertTrue(not os.path.isfile(local_cred_file))

        self.assertIsNone(creds.delete('non_existent_file'))

    def test_credentials_username(self):
        """credentials.Credentials.username"""
        self.default_cred.username('updated_username')
        self.assertEqual(self.default_cred.username(),
                         'updated_username')

    def test_credentials_base_url(self):
        """credentials.Credentials.base_url"""
        new_base_url = 'https://updated_username.carto.com/'
        self.default_cred.base_url(new_base_url)
        self.assertEqual(self.default_cred.base_url(),
                         new_base_url)

    def test_credentials_repr(self):
        """credentials.Credentials.__repr__"""
        ans = ('Credentials(username=default_username, '
               'key=default_key, '
               'base_url=https://default_username.carto.com)')
        self.assertEqual(str(self.default_cred), ans)
Esempio n. 12
0
 def test_credentials_invalid_key(self):
     """credentials.Credentials invalid key"""
     self.default_cred.delete()
     with self.assertRaises(RuntimeError,
                            msg='Did not specify key'):
         Credentials(username=self.username)
Esempio n. 13
0
    def test_credentials_key(self):
        """credentials.Credentials.key"""

        creds = Credentials(key='abcdefg', username='******')
        creds.key('hijklmnop')
        self.assertEqual(creds.key(), 'hijklmnop')
Esempio n. 14
0
from cartoframes import Credentials
creds = Credentials(username='******', key='password')
creds.save() 
Esempio n. 15
0
get_ipython().magic('matplotlib inline')
import os
import json
import mapboxgl
import sqlalchemy
import pandas as pd
import geopandas as gpd
from cartoframes import CartoContext, Credentials
from cartoframes.contrib import vector

# In[4]:

F = open("/.carto_builder", "r")
carto_api_key = F.read().splitlines()[0]
F.close()
creds = Credentials(key=carto_api_key, username='******')
cc = CartoContext(creds=creds)

# In[5]:

F = open("/.password", "r")
password = F.read().splitlines()[0]
F.close()

engine = sqlalchemy.create_engine(
    "postgresql://*****:*****@{}:5432/{}".format(password,
                                                     DATABASE_ENDPOINT,
                                                     DATABASE_NAME))

# In[6]: