[Tutor] instantiate and name a class from / with a string

bob gailer bgailer at gmail.com
Fri Aug 9 19:40:18 EDT 2019


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



More information about the Tutor mailing list