How to use the pydal._compat.to_bytes function in pydal

To help you get started, we’ve selected a few pydal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github web2py / pydal / tests / contribs.py View on Github external
def test_readline(self):
        f = LockedFile('test.txt', 'wb')
        f.write(to_bytes('abc\n'))
        f.write(to_bytes('123\n'))
        f.close()
        f = LockedFile('test.txt', 'rb')
        rl = f.readline()
        self.assertTrue(to_bytes('abc') in rl)
        rl = f.readline()
        self.assertTrue(to_bytes('123') in rl)
        f.close()
        f = LockedFile('test.txt', 'rb')
        rls = f.readlines()
        f.close()
        self.assertEqual(len(rls), 2)
github web2py / pydal / tests / nosql.py View on Github external
def testRun(self):
        db = DAL(DEFAULT_URI, check_reserved=['all'])
        rname = _quote(db, 'a very complicated fieldname')
        for ft in ['string', 'text', 'password', 'upload', 'blob']:
            db.define_table('tt', Field('aa', ft, default='', rname=rname))
            cv = 'x'
            self.assertEqual(isinstance(db.tt.insert(aa='x'), long), True)
            if IS_MONGODB and not PY2 and ft == 'blob':
                cv = to_bytes(cv)
            self.assertEqual(db().select(db.tt.aa)[0].aa, cv)
            drop(db.tt)
        db.define_table('tt', Field('aa', 'integer', default=1, rname=rname))
        self.assertEqual(isinstance(db.tt.insert(aa=3), long), True)
        self.assertEqual(db().select(db.tt.aa)[0].aa, 3)
        drop(db.tt)
        db.define_table('tt', Field('aa', 'double', default=1, rname=rname))
        self.assertEqual(isinstance(db.tt.insert(aa=3.1), long), True)
        self.assertEqual(db().select(db.tt.aa)[0].aa, 3.1)
        drop(db.tt)
        db.define_table('tt', Field('aa', 'boolean', default=True, rname=rname))
        self.assertEqual(isinstance(db.tt.insert(aa=True), long), True)
        self.assertEqual(db().select(db.tt.aa)[0].aa, True)
        drop(db.tt)
        db.define_table('tt', Field('aa', 'json', default={}, rname=rname))
        self.assertEqual(isinstance(db.tt.insert(aa={}), long), True)
github web2py / pydal / tests / contribs.py View on Github external
def test_readline(self):
        f = LockedFile('test.txt', 'wb')
        f.write(to_bytes('abc\n'))
        f.write(to_bytes('123\n'))
        f.close()
        f = LockedFile('test.txt', 'rb')
        rl = f.readline()
        self.assertTrue(to_bytes('abc') in rl)
        rl = f.readline()
        self.assertTrue(to_bytes('123') in rl)
        f.close()
        f = LockedFile('test.txt', 'rb')
        rls = f.readlines()
        f.close()
        self.assertEqual(len(rls), 2)
github web2py / pydal / tests / contribs.py View on Github external
def worker(fh):
            time.sleep(2)
            fh.close()

        f = LockedFile('test.txt', mode='wb')
        f.write(to_bytes('test ok'))
        t1 = threading.Thread(target=worker, args=(f, ))
        t1.start()
        start = int(time.time())
        content = read_locked('test.txt')
        end = int(time.time())
        t1.join()
        # it took at least 2 seconds to read
        self.assertTrue(end - start >= 2)
        self.assertEqual(content, to_bytes('test ok'))
github web2py / py4web / web3py / form.py View on Github external
self.formkey = None
        self.cached_helper = None

        if readonly or request.method=='GET':
            if self.record:
                self.vars = self.record
        else:
            post_vars = request.forms
            self.submitted = True
            process = False
            # we only a process a form if it is POST and the formkey matches (correct formname and crsf)
            # notice we never expose the crsf uuid, we only use to sign the form uuid
            if request.method == 'POST':                
                if csrf_uuid:
                    code, signature = post_vars['_formkey'].split('/')
                    expected = hmac.new(to_bytes(csrf_uuid), to_bytes(self.form_name+'/'+code)).hexdigest()
                    if signature == expected:
                        process = True
                elif post_vars.get('_formkey') == self.form_name:
                    process = True
            if process:
                if not post_vars.get('_delete'):
                    for field in self.table:
                        if field.writable:
                            value = post_vars.get(field.name)
                            # FIX THIS deal with set_self_id before validate
                            (value, error) = field.validate(value)
                            if field.type == 'upload':
                                delete = post_vars.get('_delete_'+field.name)
                                if value is not None and hasattr(value,'file'):
                                    value = field.store(value.file,
                                                        value.filename,
github web2py / pydal / pydal / migrator.py View on Github external
def log(self, message, table=None):
        isabs = None
        logfilename = self.adapter.adapter_args.get("logfile", "sql.log")
        writelog = bool(logfilename)
        if writelog:
            isabs = os.path.isabs(logfilename)
        if table and table._dbt and writelog and self.adapter.folder:
            if isabs:
                table._loggername = logfilename
            else:
                table._loggername = pjoin(self.adapter.folder, logfilename)
            logfile = self.file_open(table._loggername, "ab")
            logfile.write(to_bytes(message))
            self.file_close(logfile)
github web2py / pydal / pydal / representers / mongo.py View on Github external
def _blob(self, value):
        if isinstance(value, basestring) and value == '':
            value = None
        return MongoBlob(value) if PY2 else to_bytes(value)
github web2py / pydal / pydal / objects.py View on Github external
return self.custom_retrieve(name, path)
        if self.authorize or isinstance(self_uploadfield, str):
            row = self.db(self == name).select().first()
            if not row:
                raise NotFoundException
        if self.authorize and not self.authorize(row):
            raise NotAuthorizedException
        file_properties = self.retrieve_file_properties(name, path)
        filename = file_properties["filename"]
        if isinstance(self_uploadfield, str):  # ## if file is in DB
            stream = BytesIO(to_bytes(row[self_uploadfield] or ""))
        elif isinstance(self_uploadfield, Field):
            blob_uploadfield_name = self_uploadfield.uploadfield
            query = self_uploadfield == name
            data = self_uploadfield.table(query)[blob_uploadfield_name]
            stream = BytesIO(to_bytes(data))
        elif self.uploadfs:
            # ## if file is on pyfilesystem
            stream = self.uploadfs.open(text_type(name), "rb")
        else:
            # ## if file is on regular filesystem
            # this is intentionally a string with filename and not a stream
            # this propagates and allows stream_file_or_304_or_206 to be called
            fullname = pjoin(file_properties["path"], name)
            if nameonly:
                return (filename, fullname)
            stream = open(fullname, "rb")
        return (filename, stream)
github web2py / pydal / pydal / parsers / base.py View on Github external
def _blob(self, value):
        decoded = b64decode(to_bytes(value))
        try:
            decoded = to_native(decoded)
        except:
            pass
        return decoded
github web2py / pydal / pydal / representers / __init__.py View on Github external
def adapt(self, value):
        if PY2:
            if not isinstance(value, string_types):
                value = str(value)
            value = to_bytes(value)
            try:
                value.decode(self.adapter.db_codec)
            except:
                value = value.decode("latin1").encode(self.adapter.db_codec)
        else:
            value = to_unicode(value)
        return self.adapter.adapt(value)

pydal

pyDAL is a Database Abstraction Layer. It generates queries for SQlite, PotsgreSQL, MySQL, and other backends. It was originally part of the web2py frameworks but it is now an independent project. Example: db.define_table("thing",Field("name")) and db.thing.insert(name="Pizza")

BSD-3-Clause
Latest version published 17 days ago

Package Health Score

81 / 100
Full package analysis