def encode_play(): # None of this works because all encode uses await, and you cannot call # any await method unless every def is async def # So we do have to split our database query build + ORM into both sync # and async from uvicore import db import sqlalchemy as sa from databases import Database connection = 'wiki' database = Database('mysql://*****:*****@127.0.0.1:3306/uvicore_wiki') database.connect() table = sa.Table("users", db.metadata.get(connection), sa.Column("id", sa.Integer, primary_key=True), sa.Column("name", sa.String(length=50))) #engine = db.engine('wiki') #con = db.connect() query = table.select() users = database.fetch_all(query) # for user in users: # table = User.Db.table # query = table.select() # users = con.execute(query) dd('DONE encode_play()')
def decorator(cls): # cls = the decorated class = app1.http.controllers.home2.Home2 #dd(inspect.signature(cls.__init__)) dd(get_type_hints(cls)) return cls
async def cli(): """Play""" #await mail_play() #await cache_play() #await orm_insert_play() #await poly_play() from app1.models import Post post = await Post.query().find(1) dd(post) dd(Post.__dict__)
async def poly_play(): from app1.models import User, Post, Comment, Tag # # posts = await Post.query().include('attributes').get() # # dd(posts) # # user = await User.query().include('posts.attributes').find(1) # # dd(user) # comment = await Comment.query().include('post.attributes').find(1) # dd(comment) tags = await Tag.query().key_by('name').get() post = { 'slug': 'test-post11', 'title': 'Test Post11', 'body': 'This is the body for test post11. I like the taste of water.', 'creator_id': 1, 'owner_id': 2, 'tags': [ #{'id': 1, 'name': 'linux', 'creator_id': 1}, #{'id': 3, 'name': 'bsd', 'creator_id': 2}, tags['linux'], tags['bsd'], ], # 'comments': [ # { # 'title': 'Post11 Comment1', # 'body': 'Body for post11 comment1', # #'post_id': 1, # No id needed, thats what post.create() does # 'creator_id': 1, # } # ], } x = await Post.insert_with_relations(post) dd(x)
def test_dd(): with pytest.raises(SystemExit): dumper.dd('hi')
async def other_play(): user = await models.User.query().include( 'groups.roles.permissions', #'roles', 'roles.permissions', 'posts.creator.info', 'posts.creator.creator.info', #'creator.info', ).find(2) dd(user) dd('doneo play') # Redis # from uvicore.redis import Redis # redis = await Redis.connect() # cache = await Redis.connect('cache') # dump(await redis.get('name')) # dump(await cache.get('name')) # dump(await redis.keys('*')) # Cache #from uvicore.cache import Cache cache = uvicore.cache #cache = uvicore.cache.connect('app1') #cache = Cache.connect('app1') #dump( await Cache.connect('app1').get('name') ) await cache.put('k0', 'k0 value') await cache.put({ 'k1': 'k1 value', 'k2': 'k2 value', }) dump(await cache.get('k0')) x = await cache.get(['k1', 'k2']) dump(x) #dump(x.k1) #dump( await cache.pull('k0')) #x = await cache.pull(['k1', 'k2']) #dump(x) #dump( await cache.get('name') ) #dump( await cache.store('redis').get('name') ) #dump( await cache.store('app1').get('name') ) #dump( await cache.get('name2') ) #dump( await cache.store('redis').get('name') ) #dump( await cache.get('name') ) #dump( await cache.store('redis2').get('name') ) #dump( await cache.store('redis').get('name') ) #await cache.increment('inc1') #dump( await cache.get('inc1') ) #await cache.put('one', 'one here') #dump( await cache.get('one') ) #await cache.put('two', 'two here', 5) #dump( await cache.has('two') ) async def method0(): #return 'method0 here' dump('query here') return await models.Post.query().find(1) async def method1(): return 'method1 here' async def method2(): return { 'asdf': 'asdfasdf', 'xafd': 'casdfasdf', } # await cache.remember('method0', method0, seconds=5) # await cache.remember({ # 'method1': method1, # 'method2': method2, # }, seconds=5) # #dump( await cache.get('method1') ) # dump( await cache.get(['method0', 'method1', 'method2']) ) #post = await uvicore.db.query().table('posts').cache(seconds=5).find(unique_slug='test-post3') #dump(post) #post = await models.Post.query().include().cache(seconds=10).find(1) #dump(post) user = await models.User.query().include( 'contact', 'info', 'groups', 'groups.roles', 'groups.roles.permissions').cache(seconds=10).find(2) dd(user) #await cache.forget(['k0', 'k1', 'k2']) #await cache.flush() #( await cache.add('add1', 'add1 here') ) #dump( await cache.has('add1') ) #x = await cache.pull('add1') #dump(x) #dump( await cache.has('add1') ) #await cache.put({ #'k1': 'value of k1', #'k2': 'value of k2', #}) #dump( await cache.get('k1') ) #dump( await cache.get('k2') ) #x = await cache.get(['k1', 'k2']) #dump(x) #dump(x.k1) #from uvicore.cache.cache import Cache #cache = uvicore.ioc.make('cache') #dump(uvicore.ioc.binding('uvicore.cache.cache.Cache')) #redis = await uvicore.ioc.make('redis').connect() #cache = await uvicore.ioc.make('redis').connect('cache') #dump(redis, cache) #x = await redis.connection('app1').get('key') #dump(x) # Swap connections #await redis.connection('cache').set('name', 'matthew2') #dump(await redis.connection('cache').get('name')) #await redis.set('name', 'matthew') #dump(await redis.get('name')) #dump(redis.connections) #dump(redis.engines) #dump(uvicore.cache.get('key')) #dump(uvicore.ioc.bindings) # # cache as a singleton in Ioc # cache = uvicore.ioc.make('cache') # # Or on global # cache = uvicore.cache # cache.get('key') # cache.get('key', 'default') # does NOT set default back to redis # cache.put('key', 'value') # def users_db_query(): # pass # users = cache.get('mreschke', users_db_query) # does NOT set results back to redis # users = cache.remember('mreschke', users_db_query) # DOES set back to redis, true caching # cache.has('key') # cache.increment('key') # cache.increment('key', 4) # cache.decrement('key') # cache.decrement('key', 5) # cache.pull('key') # DELETES from redis once pulled # # Integrate with ORM # users = User.query().cache().get() # cache key will be complex parameters, or hash of entire SQL itself? # # In package.py config # config = { # 'default': 'array', # 'stores': { # 'array': { # 'driver': 'array' # }, # 'file': { # 'driver': 'file', # 'path': '/tmp/cache' # } # 'redis': { # 'driver': 'redis', # 'connection': 'cache', # From redis db connections config # } # } # } # # In package.py config # redis = { # 'default': 'app1', # 'connections': { # 'app1': { # 'host': '127.0.0.1', # 'post': 6379, # 'database': 0, # 'password': None # }, # 'cache': { # 'host': '127.0.0.1', # 'post': 6379, # 'database': 99, # 'password': None # } # } # } # Keystone dd('DONE PLAY!') #class argon2.PasswordHasher(time_cost=2, memory_cost=102400, parallelism=8, hash_len=16, salt_len=16, encoding='utf-8', type=<Type.ID: 2>) #dd('DONE') #import sys #from uvicore.auth.database.tables.users import Users #from uvicore.auth.models.user import User #from app1.models import User, Post #dump(User) # from app1.models.user import User as User2 # print(sys.modules['uvicore.auth.models.user']) # #dump(uvicore.app.providers) # dump(id(User)) # dump(User) # dump(id(User2)) # dump(User2) # dump(id(sys.modules.get('uvicore.auth.models.user'))) # dump((sys.modules.get('uvicore.auth.models.user'))) # dump(id(sys.modules.get('app1.models.user'))) # dump((sys.modules.get('app1.models.user'))) # #dump(sys.modules) #posts = await Post.query().include('creator.contact', 'attributes').get() #posts = await User.query().include('info').get() #dump(posts) #dump("Hi there, my name is matthew reschke, what is your name? Again, my name is Matthew Reschke, what is your name? Again, my name is Matthew Reschke, what is your name? Hi there, my name is matthew reschke, what is your name? Again, my name is Matthew Reschke, what is your name? Again, my name is Matthew Reschke, what is your name?") #dump(123.32) #db = await uvicore.db.database() #await db.disconnect() #dump(db) #dump(uvicore.ioc.bindings) #dump(uvicore.ioc.bindings['uvicore.auth.database.tables.users.Users']) #dump(uvicore.ioc.bindings) #dump(User.modelfields) # from uvicore.orm import Field # x = Field('id', name='user_id') # dump(x) #await uvicore.db.disconnect_all() dump('Play Done')
async def misc(): """Play asdfasdfasdfasdf""" # The reverse # from mreschke.wiki.models import SpaceSectionTopic # topics = await SpaceSectionTopic.query().include('section.space').get() # dd(topics) # from uvicore.auth.models import User # user = await (User.query() # .where('disabled', '1') # .include('groups') # #.find(username='******') # .find(1) # ) # dd(user) # #['roles', 'roles.permissions', 'groups', 'groups.roles', 'groups.roles.permissions'] dd('DONE') from uvicore.typing import Dict, OrderedDict, Optional # from dataclasses import dataclass # @dataclass # class Config: # name: Optional[str] = None # registers: Optional[Dict] = None # x = Config() # x.name = 'matthew' # x.registers = { # 'models': True # } # x.asdf = 'hi' # x.name = 1 # dump(x.asdf) # dd(x) dump(uvicore.config) #dump(uvicore.config('mreschke.wiki.database')) dd('DONE') #dd(uvicore.config) #dd(uvicore.app.packages.mreschke.wiki) #dd(uvicore.app.package('mreschke.wiki').name) a = Dict({ 'name': 'Mreschke Wiki', 'registers': { 'modules': True, 'tables': True, }, 'database': { 'default': 'wiki', 'connections': { 'wiki': { 'server': 'localhost', 'port': 3308 } } } }) b = { 'name': 'NEW name', 'database': { 'connections': { 'wiki': { 'port': 99999 } } } } a.merge(b) #a.dotset('database.connections.wiki.asdf.asdf.asdf', 1234) #dump(a.dotget('database.connections.wiki.port')) dump(a.dotget('database.connections.wiki.port')) dd(a) dd('done') #dd(a) # a.defaults(b) # a.freeze() # a.unfreeze() # a.asdfasdfasdf = 'asdfasdf' # dd(a) #x = config('app.name') x = config.app.name config('app.name', 'newname') config.app.name = 'newname' x = Config(name='matthew', registers={'modules': True}) dd(x) dump(config['name']) dump(config.name) if 'name' in config: dump('name found') if config.name: dump('name found again') if config.asdfasdfasdfasdfasdf: dump('asdfasdfasdfasdfasdfasdfasdfasdf') dump(config.database.connections.wiki) if config.database.connections: dump('connections found') # Add something DEEP without defining parents config.frameworks.laravel.color = ['red', 'blue'] dump(config) dd('DONE PLAYING') from mreschke.wiki.models.post import Post posts = (await Post.query().include('creator').get()) dump(posts)