Beispiel #1
0
def database():
    db.connect()
    db.create_tables([Account, Transaction])
    yield db

    db.close()
    os.remove('test.sqlite3')
Beispiel #2
0
def analysis_new_mission_commonparameters_tagged_contains(city):
    lines = commonparameters(city)
    db = pymysql.connect(host=HOST,
                         user=USER,
                         password=PASSWD,
                         db=DB,
                         charset='utf8')
    cursor = db.cursor()
    for l in range(0, len(lines)):
        cgi = lines[l][4]
        district = lines[l][3]
        shapes = gaodemapscene(district, city, 120000)
        Path = mpath.Path
        for s in range(0, len(shapes)):
            shape = shapes[s][-1]
            shape_array = np.array([
                float(i)
                for i in shape.replace('|', ',').replace(';', ',').split(',')
            ]).reshape(-1, 2)
            point = (float(lines[l][15]), float(lines[l][14]))
            p = Path(shape_array)
            if p.contains_points([point]):
                query_commonparameters_tagged = """UPDATE {} set residential_flag='1' where cgi='{}'""".format(
                    TABLE_NAME_ANALYSIS_Commonparameters, cgi)
                cursor.execute(query_commonparameters_tagged)
                break
        print('%.2f%%' % (l / len(lines) * 100))
    cursor.close()
    db.commit()
    db.close()
Beispiel #3
0
def analysis_new_mission_commonparameters_tagged_100_200(city):
    lines = commonparameters(city)
    db = pymysql.connect(host=HOST,
                         user=USER,
                         password=PASSWD,
                         db=DB,
                         charset='utf8')
    cursor = db.cursor()
    for l in range(0, len(lines)):
        cgi = lines[l][4]
        district = lines[l][3]
        shapes = gaodemapscene(district, city, 120000)
        Path = mpath.Path
        for s in range(0, len(shapes)):
            shape = shapes[s][-1]
            shape_array = np.array([
                float(i)
                for i in shape.replace('|', ',').replace(';', ',').split(',')
            ]).reshape(-1, 2)
            angle = float(lines[l][18])
            point = (float(lines[l][15]), float(lines[l][14]))
            Path(shape_array)
            try:
                a = mpatches.Wedge(point,
                                   0.0018,
                                   angle - 65,
                                   angle + 65,
                                   width=0.0009)._path.vertices
                a = Polygon(a).buffer(0)
                b = Polygon(shape_array)
                c = a.intersection(b)
                polygon = Polygon(c)
                f = polygon.area / b.area
            except NotImplementedError as e:
                f = 0
            if f > 0.5:
                query_commonparameters_tagged = """UPDATE {} set residential_flag='3' where cgi='{}'""".format(
                    TABLE_NAME_ANALYSIS_Commonparameters, cgi)
                cursor.execute(query_commonparameters_tagged)
                break
        print('%.2f%%' % (l / len(lines) * 100))
    cursor.close()
    db.commit()
    db.close()
Beispiel #4
0
def main():
    logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
    db.connect()
    bot = init_detwtr_bot()
    for job in Job.select():
        logging.info("Processing job: {id}".format(id=job.id))

        if (datetime.datetime.now() - job.tweet.created_at) < datetime.timedelta(minutes=5):
            logging.info("Tweet is not old enough, wait a few more minutes")
            continue

        is_duplicate = False
        for tweet in Tweet.select().where((Tweet.user == job.tweet.user) & (
                    Tweet.created_at > job.tweet.created_at) & ~(Tweet.is_deleted) & ~(Tweet.is_withheld)):
            levdist = editdistance.eval(tweet.text, job.tweet.text)
            if levdist <= max(3, int(math.ceil(14 / 140 * len(job.tweet.text)))) and job.tweet.media == tweet.media:
                is_duplicate = True
                logging.info("Duplicate found:\n{tweet_1}\n---\n{tweet_2}".format(tweet_1=job.tweet.text,
                                                                                  tweet_2=tweet.text))
                break

        if is_duplicate:
            logging.info("Tweet is very similar to other tweets, won't restore")
            job.delete_instance()
        else:
            logging.info("Found no similar tweets, going to restore! :3")
            text = job.tweet.text.replace("@", "&")
            try:
                if job.tweet.media:
                    media = io.BytesIO(job.tweet.media)
                    resp = bot.upload_media(media=media)
                    bot.update_status(status=text, media_ids=[resp["media_id"]])
                else:
                    bot.update_status(status=text)
                logging.info("Tweet restored, all is well...")
                job.delete_instance()
            except TwythonError as e:
                logging.error("TwythonError: {error}".format(error=repr(e)))
                if "Status is a duplicate" in e.msg:
                    job.delete_instance()
    db.close()
    def test_destroy(self):
        db = self.db
        db.open('test_wipe')
        db.put('doggy', (1,2,3))
        self.assertTupleEqual(db.get('doggy'), (1,2,3))

        db.close()
        db.open('test_wipe')
        self.assertTupleEqual(db.get('doggy'), (1,2,3))

        db.wipe()
        db.open('test_wipe')
        self.assertEqual(db.get('doggy'), None)

        db.put('doggy', (1,2,3))
        db.close()
        db.open('test_wipe')
        self.assertTupleEqual(db.get('doggy'), (1,2,3))

        db.start = Mock(side_effect=db.start)
        db.wipe(True)

        db.start.assert_called()
        self.assertEqual(db.get('doggy'), None)
Beispiel #6
0
def subscribe():
    db.close()  # avoid unnecessary open db connections
    return relay.subscribe('everybody').response_stream()
Beispiel #7
0
def _db_close(exc):
    if not db.is_closed():
        db.close()
Beispiel #8
0
 def on_finish(self):
     db.close()
     db.rollback()
Beispiel #9
0
def closeDB(db):
    log.logger.debug("close database")
    db.close()
Beispiel #10
0
def _verify_identity(username, password):
    """
    Verifies the identity's username/password credentials.
    Params:
        username (string): The username of the identity to authenticate.
        password (string): The password of the identity to authenticate.
    Returns:
        (dictionary): Representation of an identity.
    Raises:
        AuthFailed - No identity exists for that username.
    """
    # Get the current bcrypt cost from config.
    current_cost = app.config['BCRYPT_LOG_ROUNDS']

    try:
        connection = db.connection()
        result = db.call(connection, 'fetch_identity_credentials', [username])
        password_hash = result['password_hash']
        identity_exists = True

        db.close(connection)

    except DBKeyDoesNotExistException:  # The username does not exist.
        # We could bail here, but not running the bcrypt
        # function will leak the presence/non-presense of identity
        # via timing attack.

        # Use this sample hash so we have something to feed into
        # bcrypt function. Make sure our bogus attempt uses the
        # same cost as a stored hashes.
        password_hash = (
            '$2b$' + str(current_cost) +
            # Rymths with assword
            '$eCc5pM5y.eOhNPandfz8GuQry6cYz6vJW.QjqUAgw0C7YdaqrYyzS')
        identity_exists = False
        # We don't want to abort here.
        pass  # We want to run the hash function to avoid timing attack.

    bcrypt = Bcrypt(app)
    did_pass = bcrypt.check_password_hash(password_hash, password)

    if identity_exists:
        # If identity and password is correct,
        # let's see if we should update the bcrypt cost.
        if did_pass:
            hash_array = password_hash.split('$')
            hash_cost = int(hash_array[2])  # The hash cost

            # If we have changed the bcrypt cost since this password
            # was created, let's update it with the new cost.
            if hash_cost != current_cost:
                identity_id = result['identity_id']
                bcrypt = Bcrypt(app)
                password_hash = bcrypt.generate_password_hash(password)

                connection = db.connection()
                db.call(connection, 'update_identity_password_hash',
                        [identity_id, password_hash])
                db.commit(connection)
        else:
            did_pass = _check_temp_password(result, password)
            result['temp_session'] = True
    else:
        # Now we can bail if no identity exists.
        raise AuthFailed

    result['did_pass'] = did_pass
    return result
Beispiel #11
0
def teardown_request(exc):
    db.close()
    g.redis.connection_pool.disconnect()
Beispiel #12
0
def closeDB(db):
    log.logger.debug("close database")
    db.close()
Beispiel #13
0
#!/usr/bin/python
from database import db

if db: print db.cleanTable('person')

dic = {'name': 'maybe', 'address': 'Shanxi', 'sex': 'female'}
if db: print db.insertDict('person', dic)

dic = {'name': 'lixue', 'address': 'Shanxi', 'sex': 'female'}
if db: print db.insertDict('person', dic)

dic = {'name': 'maybe', 'address': 'ShanDong', 'sex': 'male'}
if db: print 'insertUpdateDict: ', db.insertUpdateDict('person', 'name', dic)

dic = {'name': 'quqinglei', 'address': 'ShanDong', 'sex': 'male'}
if db: print 'insertUpdateDict: ', db.insertUpdateDict('person', 'name', dic)

if db: print 'fetchAllDict', db.fetchAllDict("select * from person")
if db: print 'countRows ', db.countRows('select * from person')

if db: print 'deleteRowDict ', db.deleteRowDict('person', {'name': 'quqinglei'})

if db: db.close()
Beispiel #14
0
def app_close(exception):
    db.close()  # Disconnect database
async def shutdown_storage(dispatcher: Dispatcher):
    db.close()
    await dispatcher.storage.close()
    await dispatcher.storage.wait_closed()
Beispiel #16
0
def teardown_request(exc):
    db.close()
    g.redis.connection_pool.disconnect()
Beispiel #17
0
def analysis_new_mission_gaodemapscene_tagged(city):
    db = pymysql.connect(host=HOST,
                         user=USER,
                         password=PASSWD,
                         db=DB,
                         charset='utf8')
    cursor = db.cursor()
    _region = findRegion(city, 120000)
    print(city)
    print('**********')
    print(_region)
    for region in _region:

        region = region[0]
        print(region)
        shapes = gaodemapscene1(city, region, 120000)
        print(len(shapes))
        region = REGION[region]
        print(region)
        lines = commonparameters1(city, region)
        print(len(lines))
        for s in range(0, len(shapes)):
            gaodemapscene_id = shapes[s][0]
            shape = shapes[s][16]
            shape_array = np.array([
                float(i)
                for i in shape.replace('|', ',').replace(';', ',').split(',')
            ]).reshape(-1, 2)
            Path = mpath.Path
            p = Path(shape_array)
            i = 0
            for l in range(0, len(lines)):

                angle = float(lines[l][18])

                point = (float(lines[l][15]), float(lines[l][14]))

                cgi = lines[l][4]

                chinesename = lines[l][6]
                pci = lines[l][12]

                if p.contains_points([point]):
                    i += 1
                    if i == 58:
                        print('超出范围')
                        break

                    cursor.execute(
                        query_gaodemapscene(cgi, chinesename, pci,
                                            gaodemapscene_id, i))

                else:
                    try:
                        a = mpatches.Wedge(point, 0.0009, angle - 65,
                                           angle + 65)._path.vertices
                        a = Polygon(a).buffer(0)
                        b = Polygon(shape_array)
                        if not b.is_valid:
                            break
                        c = a.intersection(b)
                        Polygon(c)
                        overlap = 1
                    except AssertionError as e:
                        print(cgi, shape, gaodemapscene_id)
                        break
                    except ValueError as e:
                        print(shape)
                        break
                    except NotImplementedError as e:
                        overlap = 0
                    finally:
                        cursor.close()

                        db.close()
                    if overlap > 0:
                        i += 1
                        if i == 58:
                            print('超出范围')
                            break
                        cursor.execute(
                            query_gaodemapscene(cgi, chinesename, pci,
                                                gaodemapscene_id, i))

                    else:
                        try:
                            a = mpatches.Wedge(point,
                                               0.0018,
                                               angle - 65,
                                               angle + 65,
                                               width=0.0009)._path.vertices
                            a = Polygon(a).buffer(0)
                            b = Polygon(shape_array)

                            c = a.intersection(b)
                            polygon = Polygon(c)
                            f = polygon.area / b.area
                        except AssertionError as e:
                            print(cgi, shape, gaodemapscene_id)
                            break
                        except ValueError as e:
                            print(shape)
                            break
                        except NotImplementedError as e:
                            f = 0
                        finally:
                            cursor.close()

                            db.close()
                        if f > 0.5:
                            i += 1
                            if i == 58:
                                print('超出范围')
                                break

                            cursor.execute(
                                query_gaodemapscene(cgi, chinesename, pci,
                                                    gaodemapscene_id, i))

            print('%.2f%%' % (s / len(shapes) * 100))
    cursor.close()
    db.commit()
    db.close()
Beispiel #18
0
def after_req(response):
    db.close()
    return response
Beispiel #19
0
 def on_finish(self):
     db.close()
     db.rollback()
Beispiel #20
0
#!/usr/bin/python

from database import db
if db: print db.cleanTable('person')
dic = {'name': 'maybe', 'address': 'Shanxi', 'sex': 'female'}
if db: print db.insertDict('person', dic)

dic = {'name': 'lixue', 'address': 'Shanxi', 'sex': 'female'}
if db: print db.insertDict('person', dic)

ret = db.fetchAllDict("select * from person")
if ret['count'] == 2: print "Result: Test ok"
else: print "Result: Test Failure"
db.close()
Beispiel #21
0
def on_done():
    db.close()