Skip to main content
Notice removed Draw attention by Conor Mancone
Bounty Ended with RootTwo's answer chosen by Conor Mancone
Python question should be tagged with [python] - https://codereview.meta.stackexchange.com/a/5458
Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158
Tweeted twitter.com/StackCodeReview/status/1381306049102286848
Notice added Draw attention by Conor Mancone
Bounty Started worth 150 reputation by Conor Mancone
added 89 characters in body
Source Link
Conor Mancone
  • 1.3k
  • 7
  • 19

Here is an example of how you would use this in practice. I've "mocked" an actual model (which mayis not actually required for this example use case but is implicitly required with the current class behavior):

You would adjust the dependency injection rules for your test to swap out the cursor_backend for this memory backend. This change is transparent to the model, sowhich you would use the model (and therefore memory backend) like this:

Note thatAnd it would pass the above MemoryTable class isn't used directly bycreate request down to the model. Rather, there is a MemoryBackend and, from there, to the above MemoryTable class that manages one of these "tables" for each model. The

There is a query builder automatically connects models and the backend, as well as generate the configuration for the eventual backend call. Therefore, if you used a query builder like this:

I can't decide if the lambda builder makes perfect sense, is impossible to read, or both. I also can't decide if there is a better approach. Also available The code lives here and there is further documentation under construction here.

I'm especially curious if there is a better, more readable approach (that doesn't make this take up a hundred extra lines of code or entail andan endless if/elif block), but of course I'm always up for any and all other suggestions!

Here is an example of how you would use this in practice. I've "mocked" an actual model (which may not actually required for this example use case):

You would adjust the dependency injection rules for your test to swap out the cursor_backend for this memory backend. This change is transparent to the model, so you would use the model (and therefore memory backend) like this:

Note that the above MemoryTable class isn't used directly by the model. Rather, there is a MemoryBackend class that manages one of these "tables" for each model. The query builder automatically connects models and the backend, as well as generate the configuration for the eventual backend call. Therefore, if you used a query builder like this:

I can't decide if the lambda builder makes perfect sense, is impossible to read, or both. I also can't decide if there is a better approach. Also available here

I'm especially curious if there is a better, more readable approach (that doesn't make this take up a hundred extra lines of code or entail and endless if/elif block), but of course I'm always up for any and all other suggestions!

Here is an example of how you would use this in practice. I've "mocked" an actual model (which is not actually required for this example use case but is implicitly required with the current class behavior):

You would adjust the dependency injection rules for your test to swap out the cursor_backend for this memory backend. This change is transparent to the model, which you use like this:

And it would pass the create request down to the MemoryBackend and, from there, to the above MemoryTable class.

There is a query builder automatically connects models and the backend, as well as generate the configuration for the eventual backend call. Therefore, if you used a query builder like this:

I can't decide if the lambda builder makes perfect sense, is impossible to read, or both. I also can't decide if there is a better approach. The code lives here and there is further documentation under construction here.

I'm especially curious if there is a better, more readable approach (that doesn't make this take up a hundred extra lines of code or entail an endless if/elif block), but of course I'm always up for any and all other suggestions!

added 2192 characters in body
Source Link
Conor Mancone
  • 1.3k
  • 7
  • 19

Here is an example of how you would use this in practice. I've "mocked" an actual model (which may not actually required for this example use case):

from clearskies.backends.memory_backend import MemoryTable
from types import SimpleNamespace


model = SimpleNamespace(table_name='people', columns_configuration=lambda: {'name': ''})
table = MemoryTable(model=model)
table.create({'name': 'alice'})
table.create({'name': 'bob'})
table.create({'name': 'jane'})
table.rows({'wheres': [{'column': 'name', 'operator': '=', 'values': ['bob']}]})

# returns [{'id': 2, 'name': 'bob'}]

For more context, you would normally declare a model class like this:

from collections import OrderedDict
from clearskies import Model
from clearskies.column_types import string, email, integer


class User(Model):
    def __init__(self, cursor_backend, columns):
        super().__init__(cursor_backend, columns)

    def columns_configuration(self):
        return OrderedDict([
            string('name'),
            email('email'),
            integer('age'),
        ])

You would adjust the dependency injection rules for your test to swap out the cursor_backend for this memory backend. This change is transparent to the model, so you would use the model (and therefore memory backend) like this:

user.create({'name': 'Bob', 'email': '[email protected]', 'age': 10})

Note that the above MemoryTable class isn't used directly by the model. Rather, there is a MemoryBackend class that manages one of these "tables" for each model. The query builder automatically connects models and the backend, as well as generate the configuration for the eventual backend call. Therefore, if you used a query builder like this:

preschoolers = users.where('age<6').where('age>2')
for preschooler in preschooler:
  print(preschooler.name)

Then the MemoryTable in question would see this happen:

table = MemoryTable(model=user)
# records are added
table.rows({'wheres': [
    {'column': 'age', 'operator': '<', 'values': [6]},
    {'column': 'age', 'operator': '>', 'values': [2]}
]})

I can't decide if thisthe lambda builder makes perfect sense, is impossible to read, or both. I also can't decide if there is a better approach. Also available here

I can't decide if this makes perfect sense, is impossible to read, or both. I also can't decide if there is a better approach. Also available here

Here is an example of how you would use this in practice. I've "mocked" an actual model (which may not actually required for this example use case):

from clearskies.backends.memory_backend import MemoryTable
from types import SimpleNamespace


model = SimpleNamespace(table_name='people', columns_configuration=lambda: {'name': ''})
table = MemoryTable(model=model)
table.create({'name': 'alice'})
table.create({'name': 'bob'})
table.create({'name': 'jane'})
table.rows({'wheres': [{'column': 'name', 'operator': '=', 'values': ['bob']}]})

# returns [{'id': 2, 'name': 'bob'}]

For more context, you would normally declare a model class like this:

from collections import OrderedDict
from clearskies import Model
from clearskies.column_types import string, email, integer


class User(Model):
    def __init__(self, cursor_backend, columns):
        super().__init__(cursor_backend, columns)

    def columns_configuration(self):
        return OrderedDict([
            string('name'),
            email('email'),
            integer('age'),
        ])

You would adjust the dependency injection rules for your test to swap out the cursor_backend for this memory backend. This change is transparent to the model, so you would use the model (and therefore memory backend) like this:

user.create({'name': 'Bob', 'email': '[email protected]', 'age': 10})

Note that the above MemoryTable class isn't used directly by the model. Rather, there is a MemoryBackend class that manages one of these "tables" for each model. The query builder automatically connects models and the backend, as well as generate the configuration for the eventual backend call. Therefore, if you used a query builder like this:

preschoolers = users.where('age<6').where('age>2')
for preschooler in preschooler:
  print(preschooler.name)

Then the MemoryTable in question would see this happen:

table = MemoryTable(model=user)
# records are added
table.rows({'wheres': [
    {'column': 'age', 'operator': '<', 'values': [6]},
    {'column': 'age', 'operator': '>', 'values': [2]}
]})

I can't decide if the lambda builder makes perfect sense, is impossible to read, or both. I also can't decide if there is a better approach. Also available here

added 2388 characters in body
Source Link
Conor Mancone
  • 1.3k
  • 7
  • 19
Loading
deleted 212 characters in body; edited tags
Source Link
Conor Mancone
  • 1.3k
  • 7
  • 19
Loading
Source Link
Conor Mancone
  • 1.3k
  • 7
  • 19
Loading