I'd like to have different database files for each Peewee ORM instance. Peewee assigns the database engine to an instance using a nested "Meta" class.
My issue seems to come down to accessing a class instance attribute from an inner class. Using the Peewee quickstart example, this is what I'm trying to achieve in (broken) Python:
from peewee import *
class Person(Model):
def __init__(self, database):
self.database = database
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
# The following is incorrect; I'm trying to access the instance
# variable for the database filename string
database = SqliteDatabase(Person.database)
# Create two instances with different databases:
john = Person('john-database.db')
jane = Person('jane-database.db')
I've found a few general answers regarding nested classes, but struggle to translate their lessons to this specific application. I appreciate your help!
I think the short answer is "peewee isn't really designed for your use case". But I played around with it a bit, and while there has to be a better solution out there, here's something worked. But it's not a good idea, and you shouldn't do it.
First, we use the standard peewee example model, except we use the Proxy class for the database connection:
from peewee import *
from playhouse import *
db = Proxy()
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db
Assume we have this in model.py.
Now, to make this work, we're going to need two instances of the model module, which we can get by (ab)using the importlib module:
import importlib.util
import peewee
import sys
def load_module_as(modname, alias):
mod_spec = importlib.util.find_spec(modname)
mod = importlib.util.module_from_spec(mod_spec)
mod_spec.loader.exec_module(mod)
sys.modules[alias] = mod
return mod
This allows us to load in two separate instances of the model:
model1 = load_module_as('model', 'model1')
model2 = load_module_as('model', 'model2')
And we can then initialize two different databases:
model1.db.intitialize(pwee.SqliteDatabase('db1.db'))
model2.db.intitialize(pwee.SqliteDatabase('db2.db'))
While this sort of gets you what you want, you will always need to qualify your classes (model1.Person, model2.Person).
Here's a complete example, with unit tests:
import datetime
import importlib.util
import os
import peewee
import shutil
import sys
import tempfile
import unittest
def load_module_as(modname, alias):
mod_spec = importlib.util.find_spec(modname)
mod = importlib.util.module_from_spec(mod_spec)
mod_spec.loader.exec_module(mod)
sys.modules[alias] = mod
return mod
model1 = load_module_as('model', 'model1')
model2 = load_module_as('model', 'model2')
class TestDatabase(unittest.TestCase):
def setUp(self):
self.workdir = tempfile.mkdtemp('testXXXXXX')
self.db1_path = os.path.join(self.workdir, 'db1.db')
self.db1 = peewee.SqliteDatabase(self.db1_path)
self.db1.connect()
self.db2_path = os.path.join(self.workdir, 'db2.db')
self.db2 = peewee.SqliteDatabase(self.db2_path)
self.db2.connect()
model1.db.initialize(self.db1)
model2.db.initialize(self.db2)
self.db1.create_tables([model1.Person])
self.db2.create_tables([model2.Person])
def test_different_instances(self):
assert model1.db != model2.db
def test_create_model1_person(self):
p = model1.Person(name='testperson',
birthday=datetime.datetime.now().date(),
is_relative=True)
p.save()
def test_create_model2_person(self):
p = model2.Person(name='testperson',
birthday=datetime.datetime.now().date(),
is_relative=True)
p.save()
def test_create_both(self):
p1 = model1.Person(name='testperson',
birthday=datetime.datetime.now().date(),
is_relative=True)
p2 = model2.Person(name='testperson',
birthday=datetime.datetime.now().date(),
is_relative=False)
p1.save()
p2.save()
p1 = model1.Person.select().where(model1.Person.name == 'testperson').get()
p2 = model2.Person.select().where(model2.Person.name == 'testperson').get()
assert p1.is_relative
assert not p2.is_relative
def tearDown(self):
self.db1.close()
self.db2.close()
shutil.rmtree(self.workdir)
if __name__ == '__main__':
unittest.main(verbosity=2)
I also located this thread with some possible answers.
Related
I'm having trouble while dealing with patching. I'm using mock from unittest library. While testing check_codes() view I would like to set another values to db.find_one()
api.utils.py
from pymongo import MongoClient
import os
def get_share_code_collection():
client = MongoClient(os.getenv("DB_HOST"))
db_handle = client[os.getenv("DB_NAME")]
return db_handle["share_codes"]
views.py
def check_codes(self, request):
db = get_share_code_collection()
data = db.find_one({"specialist_id": {"$exists": True}})
test_views.py
from unittest import mock
#mock.patch("api.utils.get_share_code_collection")
def test_share_code_correct_no_share_types(
self, mocked_collection, mocked_share_code, user_model
):
mocked_collection().find_one.return_value = True
...
#mock.patch("api.utils.get_share_code_collection")
def test_share_code_no_start_time(
self, mocked_collection, user_model
):
mocked_collection().find_one.return_value = False
...
The only workaround I found is setting
mocked_collection().find_one.side_effect = [True,False]
but once it is initialized I can't add values. How can I deal with the problem?
I have been researching for ages and cannot find this specific question being asked (so perhaps I am missing something simple!) but I have had trouble separating classes into different .py files.
Scenario:
Main class imports a Settings class file and a Work class file..Settings class populates a list with objects instantiated from an Object class file...
Work class wants to cycle through that list and change values within each of those objects. <-- here is where I come unstuck.
I have tried it by making the values class variables rather than instance. Still I have to import the settings class in the work class in order to write the code to access the value to change. But it wont change the instance of that class within the main class where all these classes are called!
I read an article on Properties. The examples they gave were still examples of different classes within the same file.
Any advice as to what I should be looking at would be greatly appreciated!
This is what I was doing to test it out:
Main File where all will be run from:
import Set_Test
import Test_Code
sting = Set_Test.Settings()
tc = Test_Code.Testy()
ID = sting._settingsID
print(f'Settings ID is: {ID}')
tc.changeVal()
ID = sting._settingsID
print(f'Settings ID is: {ID}')
Set_Test.py:
class Settings:
def __init__(self):
self._settingsID = 1
#property
def settingsID(self):
return self._settingsID
#settingsID.setter
def settingsID(self, value):
self.settingsID = value
Test_Code.py:
import Set_Test
class Testy:
def changeVal(self):
Set_Test.Settings.settingsID = 8
Thanks to stovfl who provided the answer in comments. I managed to decipher what stovfl meant eventually :D
I think!
Well the below code works for anyone who wants to know:
Main:
import Set_Test
import Test_Code
sting = Set_Test.Settings()
tc = Test_Code.Testy()
ID = sting._settingsID
print(f'Settings ID is: {ID}')
tc.changeVal(sting)
ID = sting._settingsID
print(f'Settings ID is: {ID}')
Set_Test.py:
class Settings:
def __init__(self):
self._settingsID = 1
#property
def settingsID(self):
return self._settingsID
#settingsID.setter
def settingsID(self, value):
self._settingsID = value
Test_Code.py
import Set_Test
sting = Set_Test.Settings()
class Testy():
def changeVal(self, sting):
print(sting.settingsID)
sting.settingsID = 8
print(sting.settingsID)
So I am not even sure if what I want to do is possible but I thought I would ask and find out.
I want to build a chef "databag" via python. This is pretty much just a python dictionary. There are other things that need to happen with this databag that are encapsulated in the Databag class.
Now for the meat of the question...
I want to add key/values to this dictionary but need to build it in a way that is easily extensible. NOTE: the autodict is a class that makes it so you can build a dictionary using dot notation.
Here is what I am trying to do:
databag = Databag(
LogGroup=Sub("xva-${environment}-${uniqueid}-mygroup"),
RunList=[
"mysetup::default",
"consul::client"
]
)
databag.Consul() <-- Trying to add consul key/values to databag
print(databag.to_dict())
print(databag.to_string_list())
So you can see how I add the "consul" key values to the already existing databag object.
Here are the class definitions. I know this is wrong which is why I am here to see if this is even possible.
Databag Class
class Databag(object):
def __init__(self,uniqueid=Ref("uniqueid"),environment=Ref("environment"),LogGroup=None,RunList=[]):
self.databag = autodict()
self.databag.uniqueid = uniqueid
self.databag.environment = environment
self.databag.log.group = LogGroup
self.runlist=RunList
def to_string_list(self):
return self.convert_databag_to_string(self.databag)
def to_dict(self):
return self.databag
def get_runlist(self):
return self.convert_to_runlist_string(self.runlist)
Consul Class
class Consul(Databag):
def __init__(self, LogGroup=None):
if LogGroup == None:
Databag.consul.log.group = Databag.log.group
else:
Databag.consul.log.group = LogGroup
As you can see the Consul class is supposed to access the databag dictionary of the Databag class and add the "consul" variables, almost like an attribute. However, I don't want to add a new function to the databag class every time otherwise that class will end up being very, very large.
I was able to get something like this to work with the following method. Although I am up for an suggestions to get this to work. I just read the help posted on this link:
http://www.qtrac.eu/pyclassmulti.html
EDIT: This method is a lot easier:
Note: This uses the exact same implementation of the old method.
consul.py
from classes.databag.utils import *
class Consul:
def Consul(self, LogGroup=None):
if LogGroup == None:
self.databag.consul.log.group = self.databag.log.group
else:
self.databag.consul.log.group = LogGroup
databag.py
from classes.databag.utils import autodict
from classes.databag import consul
class Databag(consul.Consul):
def __init__(self,uniqueid=Ref("uniqueid"),environment=Ref("environment"),LogGroup=None,RunList=[]):
self.databag = autodict()
self.databag.uniqueid = uniqueid
...
...
Folder Structure
/classes/
databag/
utils.py
databag.py
consul.py
testing.py
---- OLD METHOD -----
How I implemented it
from classes.databag.databag import *
databag = Databag(
LogGroup=Sub("xva-${environment}-${uniqueid}-traefik"),
RunList=[
"mysetup::default",
"consul::client"
]
)
databag.Consul()
print(databag.to_dict())
print(databag.to_string_list())
lib.py
def add_methods_from(*modules):
def decorator(Class):
for module in modules:
for method in getattr(module, "__methods__"):
setattr(Class, method.__name__, method)
return Class
return decorator
def register_method(methods):
def register_method(method):
methods.append(method)
return method
return register_method
databay.py
from classes.databag import lib, consul
#lib.add_methods_from(consul)
class Databag(object):
def __init__(self,uniqueid=Ref("uniqueid"),environment=Ref("environment"),LogGroup=None,RunList=[]):
self.databag = autodict()
self.databag.uniqueid = uniqueid
....
....
consul.py
from classes.databag import lib
__methods__ = []
register_method = lib.register_method(__methods__)
#register_method
def Consul(self, LogGroup=None):
if LogGroup == None:
self.databag.consul.log.group = self.databag.log.group
else:
self.databag.consul.log.group = LogGroup
Folder Structure
/classes/
/databag
lib.py
databag.py
consul.py
utils.py
/testing.py
Suppose here is my models.py
models.py
from django.db import models
from django.contrib.auth.models import *
# Create your models here.
class A(models.Model):
p = models.CharField(max_length=200)
class B(models.Model):
d = models.OneToOneField(User)
e = models.ForeignKey(A)
class C(models.Model):
f = models.CharField(max_length=200)
g = models.ForeignKey(A,related_name="c")
i wanto import these models inside my views like this.
from app import models
def import():
list=['A','B','C']
for x in list:
from model import x
import()
Please suggest me a better solution ,I am new to python django.Thanks in advance.
edit
i want to use this loop for some reason.
Better to just import the models file itself and reference them from there.
from my_app import models
models.A.objects.all()
models.B.objects.all()
Avoid from my_app import *: it can lead to confusion and namespace pollution, plus explicit is better than implicit.
But of course if you already know the list of models, you can simply import those directly:
from my_app.models import A, B, C
you can also do something like this answer suggested
Dynamic module import in Python
import imp
import os
def load_from_file(file path, expected_class):
class_inst = None
mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
if file_ext.lower() == '.py':
py_mod = imp.load_source(mod_name, filepath)
elif file_ext.lower() == '.pyc':
py_mod = imp.load_compiled(mod_name, filepath)
if hasattr(py_mod, expected_class):
class_inst = getattr(py_mod, expected_class)()
return class_inst
i.e
module = load_from_file(file_path, expected_class)
My understanding is that you are trying to import all classes of models in your views file
simple way is:
from app.models import *
factory_boy defaults to 1 for sequences. How can I pass in a number to use as a different starting number instead? I can subclass the _setup_next_sequence() method, but how can I give it a variable to use?
# File: models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
# File: factories.py
from .models import Book
import factory
class BookFactory(factory.Factory):
FACTORY_FOR = BookModel
title = factory.Sequence(lambda n: u'Title #{}'.format(n))
#classmethod
def _setup_next_sequence(cls):
# Instead of defaulting to starting with number 1, start with starting_seq_num.
# But how do I set starting_seq_num?
return starting_seq_num
# File: make_data.py
from factories import BookFactory
# somehow set starting sequence number here?
BookFactory().create()
I'm using factory_boy 1.2.0 (via pip install factory_boy)
factory_boy code: https://github.com/dnerdy/factory_boy
In addition to the answer of Rob Bednark
We can use reset_sequence() function, which will reset the counter to a specific value.
# File: make_data.py
import factories
factories.BookFactory.reset_sequence(100)
my_book = factories.BookFactory().create()
print(my_book.title) # Title #100
I found two ways of solving this:
Use a module variable
Use a class attribute set outside of the class definition
Use a module variable:
# File: factories.py
from .models import Book
import factory
starting_seq_num = 0
class BookFactory(factory.Factory):
FACTORY_FOR = BookModel
title = factory.Sequence(lambda n: u'Title #{}'.format(n))
#classmethod
def _setup_next_sequence(cls):
# Instead of defaulting to starting with 0, start with starting_seq_num.
return starting_seq_num
# File: make_data.py
import factories
factories.starting_seq_num = 100
factories.BookFactory().create()
Use a class attribute set outside of the class definition:
# File: factories.py
from .models import Book
import factory
class BookFactory(factory.Factory):
# Note that starting_seq_num cannot be set here in the class definition,
# because Factory will then pass it as a kwarg to the model's create() method
# and cause an exception. It must be set outside the class definition.
FACTORY_FOR = BookModel
title = factory.Sequence(lambda n: u'Title #{}'.format(n))
#classmethod
def _setup_next_sequence(cls):
return getattr(cls, 'starting_seq_num', 0)
# File: make_data.py
from factories import BookFactory
BookFactory.starting_seq_num = 100
BookFactory().create()
Update: factory_boy now handles it!
In the latest version of factory_boy (2.8.1 to this day) it is now possible to force the sequence counter into a define value:
Forcing the value on a per-call basis
In order to force the counter for a specific Factory instantiation, just pass the value in the
__sequence=42 parameter:
class AccountFactory(factory.Factory):
class Meta:
model = Account
uid = factory.Sequence(lambda n: n)
name = "Test"
Then in the console:
>>> obj1 = AccountFactory(name="John Doe", __sequence=10)
>>> obj1.uid # Taken from the __sequence counter
10
>>> obj2 = AccountFactory(name="Jane Doe")
>>> obj2.uid # The base sequence counter hasn't changed
1
And it is also possible to reset the counter to a specific value:
>>> AccountFactory.reset_sequence(42)
>>> AccountFactory().uid
42
>>> AccountFactory().uid
43
The third, and simplest way:
# File: factories.py
from .models import BookModel
import factory
class BookFactory(factory.Factory, starting_seq_num):
FACTORY_FOR = BookModel
title = factory.Sequence(lambda n: u'Title #{}'.format(n + starting_seq_num))
# File: make_data.py
import factories
book = factories.BookFactory(512).create() #Start with 512
I'm only starting with Factory Boy myself, and not too experienced in Python either, so I may be missing something, but you see where I'm going here. To make it clearer, I think I'd actually prefer it to be keyworded:
class BookFactory(factory.Factory, title_seq_start=-1):
...
book = factories.BookFactory(title_seq_start=512).create()