[Tutor] Object creation query
All, I think I am asking for the impossible here. But I will ask anyway. I am using flask_sqlalchemy to build the tables and perform the queries, updates and insertions. I have multiple tables with the same structure with different names. A table called accounts which stores the name of the tables with the same structures. This is the important bits to know about. I have a page called transactions. When I call this page, I can append different names to the end. For example: Transactions/account1 Transactions/account2 Transactions/account3 . In the view for transactions I am doing the following (code extract) @app.route('/transactions/') def transactions(account): if accounts != "Transactions": Accounts.query.filter_by(account_name =account).first_or_404() tables = Accounts.query.all() if account == 'Account1': records = Account1 elif account == 'Account2': records = Account2 records = records.query.order_by(records.date.desc) as I am saving each model object into the same variable depending on the full URL name. I am wondering if there is a better way in doing this rather than using a list of if tests? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Object creation query
mhysnm1...@gmail.com wrote: > All, > > > > I think I am asking for the impossible here. But I will ask anyway. > > > > I am using flask_sqlalchemy to build the tables and perform the queries, > updates and insertions. I have multiple tables with the same structure > with different names. A table called accounts which stores the name of the > tables with the same structures. This is the important bits to know about. > > > > I have a page called transactions. When I call this page, I can append > different names to the end. For example: > > > > Transactions/account1 > > Transactions/account2 > > Transactions/account3 > > . > > > > In the view for transactions I am doing the following (code extract) > > > > @app.route('/transactions/') > > def transactions(account): > > if accounts != "Transactions": > > Accounts.query.filter_by(account_name =account).first_or_404() > > tables = Accounts.query.all() > > if account == 'Account1': > > records = Account1 > > elif account == 'Account2': > > records = Account2 > > records = records.query.order_by(records.date.desc) > > > > as I am saving each model object into the same variable depending on the > full URL name. I am wondering if there is a better way in doing this > rather than using a list of if tests? How about using only one table AccountEntries with an AccountId column which would also be the primary key of the Accounts table. Then # pseudo code # look up account wanted_accountId = select acountId from Accounts where name = account # find records for account records = select * from AccountEntries where accountId = wanted_accountId order by date desc That design would greatly simplify adding accounts 3 to, say, 30. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Object creation query
On 09/08/2019 09:54, mhysnm1...@gmail.com wrote: > updates and insertions. I have multiple tables with the same structure with > different names. Umm, why? Assuming by structure you mean they have the same fields then that's usually a bad design decision. Why not have one table with an attribute "name" or "Type" or similar? That should greatly simplify your code. If that's not what you mean by the same structure you need to give us a little more detail. Not necessarily the full descriptions but some simplified examples maybe? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] instantiate and name a class from / with a string
With the available classes Root, Channel and SSE I build the following (in CherryPy): root = Root() root.channel = Channel() root.channel.weather = SSE('weather') root.channel.energy = SSE('energy') root.channel.mail = SSE('mail') root.channel.rss = SSE('rss') ... http://example.com/channel/weather/ will then become the emitter of the weather event stream. I'd like create the instances of SSE programmatically by pulling the string 'weather', 'energy' etc. from a database. Ingo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] instantiate and name a class from / with a string
On 8/9/2019 1:55 PM, ingo wrote: With the available classes Root, Channel and SSE I build the following (in CherryPy): root = Root() root.channel = Channel() root.channel.weather = SSE('weather') root.channel.energy = SSE('energy') root.channel.mail = SSE('mail') root.channel.rss = SSE('rss') ... http://example.com/channel/weather/ will then become the emitter of the weather event stream. I'd like create the instances of SSE programmatically by pulling the string 'weather', 'energy' etc. from a database. Are you asking for help in obtaining a value from a database? Or how to dynamically create instances assigned to root.channel attributes? Assuming the latter: name = # get from data base setattr(root.channel, name, SSE(name)) -- Bob Gailer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Object creation query
On 8/9/2019 7:39 AM, Alan Gauld via Tutor wrote: On 09/08/2019 09:54, mhysnm1...@gmail.com wrote: updates and insertions. I have multiple tables with the same structure with differe I agree 100% with Peter and Alan's responses. -- Bob Gailer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] instantiate and name a class from / with a string
On 8/9/19 11:55 AM, ingo wrote: > With the available classes Root, Channel and SSE I build the following > (in CherryPy): > > root = Root() > root.channel = Channel() > > root.channel.weather = SSE('weather') > root.channel.energy = SSE('energy') > root.channel.mail = SSE('mail') > root.channel.rss = SSE('rss') > ... > > http://example.com/channel/weather/ will then become the emitter of the > weather event stream. > > I'd like create the instances of SSE programmatically by pulling the > string 'weather', 'energy' etc. from a database. To channel Bob: what are you actually asking for here? We can't figure it out. Is there some database that has these strings? If so, what is it? Or do you mean "some data structure in your program that maps those strings to something to do with them" (if so, where do you get that information from?) "With the available classes... in CherryPy" Available from where? Your code? Some external piece of code you're using? Does CherryPy actually have anything to do with the question you're not quite asking? Please be more precise. We're not trying to be argumentative; you have to help us know enough before we can help you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] instantiate and name a class from / with a string
On 10-8-2019 05:57, Mats Wichmann wrote: > Please be more precise. We're not trying to be argumentative; you have > to help us know enough before we can help you. > Mats, Bob, sorry for not being clear. I have the string and want to create the instances. Bob's name = # get from data base setattr(root.channel, name, SSE(name)) is what I was looking for, time to read up on attributes, Thanks, Ingo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor