Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Henry Versemann
ok.

On Wednesday, April 15, 2015 at 3:39:26 PM UTC-5, Vijay Khemlani wrote:
>
> tblEntryObj seems to be an instance of your model, it is not a dictionary, 
> so normally it wouldn't have a "keys" method.
>
> On Wed, Apr 15, 2015 at 5:32 PM, Henry Versemann  > wrote:
>
>> Vijay,
>>
>> I tried your method, and while it got me the table class that I wanted, 
>> for some reason when I try to get into the actual list of entries in the 
>> desired table, I can't seem to get to either the keys or values of any of 
>> the list objects.
>> So I'm not sure what's going on.
>> Here's some condensed code of what I've done so far:
>>
>> for entry in tblsLst:
>> entryKeys = entry.keys()
>> table_name = str(entryKeys[0])
>> table_class = get_model('canvas_app',table_name)
>> tblEntryLst = table_class.objects.all()
>> lstLgth = len(tblEntryLst)
>> for loopindx in range(lstLgth):
>> tblEntryObj = tblEntryLst[loopindx]
>> tblEntryObjKeys = tblEntryObj.keys()
>>
>> And I seem to be getting an AttributeError exception on the last line 
>> above.
>> When I print the size or contents (table objects format (the objects are 
>> listed like this:
>>
>> 
>>
>> ) no internal value to see) of the tblEntryLst everything looks right.
>> Is there anything you can suggest for determining where the problem might 
>> be?
>>
>> Thanks for the help.
>>
>> Henry
>>
>>
>> On Wednesday, April 15, 2015 at 12:43:28 PM UTC-5, Vijay Khemlani wrote:
>>
>>> You can use get_model
>>>
>>> from django.db.models.loading import get_model
>>>
>>> YourModel = get_model('your_app', tableName)
>>>
>>> On Wed, Apr 15, 2015 at 2:01 PM, Tim Chase  
>>> wrote:
>>>
 On 2015-04-15 09:45, Henry Versemann wrote:
 > My problem is since the logic won't know which tables will be in
 > the incoming list I need to try to reference the entries in each
 > table using some kind of evaluated version of a variable containing
 > the name of each table, as I iterate through the list.
 >
 > I'm sure I could do this using an "eval" statement like this
 >
 > tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
 >
 > eval(tblComannd)

 Since it's just Python, you can create mappings of the table-objects,
 and then get attributes on them.

   table_names = {
 "tblA": tblA,
 "tblB": tblB,
 "tblRenamed": tblXYZ,
 }

   for name, cls in table_names.items():
 log.info("Dumping %s", name)
 for item in cls.objects.all():
   write_to_file(item)

 Presumably, these classes come from some namespace like a module, so
 you can even do something like

   import models
   table_names = ["tblA", "tblB"]
   for name in table_names:
 log.info("Dumping %s", name)
 cls = getattr(models, name)
 for item in cls.objects.all():
   write_to_file(item)

 -tim



 --
 You received this message because you are subscribed to the Google 
 Groups "Django users" group.
 To unsubscribe from this group and stop receiving emails from it, send 
 an email to django-users...@googlegroups.com.
 To post to this group, send email to django...@googlegroups.com.
 Visit this group at http://groups.google.com/group/django-users.
 To view this discussion on the web visit https://groups.google.com/d/
 msgid/django-users/20150415120110.77c4ea6f%40bigbox.christie.dr.
 For more options, visit https://groups.google.com/d/optout.

>>>
>>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com .
>> To post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/3d659059-6219-4266-93f9-4e56dc348e49%40googlegroups.com
>>  
>> 
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0b22355b-91b4-4f10-92df-83cbaa83a64a%40googlegroups.com.

Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Vijay Khemlani
tblEntryObj seems to be an instance of your model, it is not a dictionary,
so normally it wouldn't have a "keys" method.

On Wed, Apr 15, 2015 at 5:32 PM, Henry Versemann 
wrote:

> Vijay,
>
> I tried your method, and while it got me the table class that I wanted,
> for some reason when I try to get into the actual list of entries in the
> desired table, I can't seem to get to either the keys or values of any of
> the list objects.
> So I'm not sure what's going on.
> Here's some condensed code of what I've done so far:
>
> for entry in tblsLst:
> entryKeys = entry.keys()
> table_name = str(entryKeys[0])
> table_class = get_model('canvas_app',table_name)
> tblEntryLst = table_class.objects.all()
> lstLgth = len(tblEntryLst)
> for loopindx in range(lstLgth):
> tblEntryObj = tblEntryLst[loopindx]
> tblEntryObjKeys = tblEntryObj.keys()
>
> And I seem to be getting an AttributeError exception on the last line
> above.
> When I print the size or contents (table objects format (the objects are
> listed like this:
>
> 
>
> ) no internal value to see) of the tblEntryLst everything looks right.
> Is there anything you can suggest for determining where the problem might
> be?
>
> Thanks for the help.
>
> Henry
>
>
> On Wednesday, April 15, 2015 at 12:43:28 PM UTC-5, Vijay Khemlani wrote:
>
>> You can use get_model
>>
>> from django.db.models.loading import get_model
>>
>> YourModel = get_model('your_app', tableName)
>>
>> On Wed, Apr 15, 2015 at 2:01 PM, Tim Chase 
>> wrote:
>>
>>> On 2015-04-15 09:45, Henry Versemann wrote:
>>> > My problem is since the logic won't know which tables will be in
>>> > the incoming list I need to try to reference the entries in each
>>> > table using some kind of evaluated version of a variable containing
>>> > the name of each table, as I iterate through the list.
>>> >
>>> > I'm sure I could do this using an "eval" statement like this
>>> >
>>> > tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
>>> >
>>> > eval(tblComannd)
>>>
>>> Since it's just Python, you can create mappings of the table-objects,
>>> and then get attributes on them.
>>>
>>>   table_names = {
>>> "tblA": tblA,
>>> "tblB": tblB,
>>> "tblRenamed": tblXYZ,
>>> }
>>>
>>>   for name, cls in table_names.items():
>>> log.info("Dumping %s", name)
>>> for item in cls.objects.all():
>>>   write_to_file(item)
>>>
>>> Presumably, these classes come from some namespace like a module, so
>>> you can even do something like
>>>
>>>   import models
>>>   table_names = ["tblA", "tblB"]
>>>   for name in table_names:
>>> log.info("Dumping %s", name)
>>> cls = getattr(models, name)
>>> for item in cls.objects.all():
>>>   write_to_file(item)
>>>
>>> -tim
>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/django-users/20150415120110.77c4ea6f%40bigbox.christie.dr.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3d659059-6219-4266-93f9-4e56dc348e49%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei1mbXGN3PNWpnqg0Cjg-rvOP738U5dqDEAY_0%2B%3DAy-Zpw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Henry Versemann
Vijay,

I tried your method, and while it got me the table class that I wanted, for 
some reason when I try to get into the actual list of entries in the 
desired table, I can't seem to get to either the keys or values of any of 
the list objects.
So I'm not sure what's going on.
Here's some condensed code of what I've done so far:

for entry in tblsLst:
entryKeys = entry.keys()
table_name = str(entryKeys[0])
table_class = get_model('canvas_app',table_name)
tblEntryLst = table_class.objects.all()
lstLgth = len(tblEntryLst)
for loopindx in range(lstLgth):
tblEntryObj = tblEntryLst[loopindx]
tblEntryObjKeys = tblEntryObj.keys()

And I seem to be getting an AttributeError exception on the last line above.
When I print the size or contents (table objects format (the objects are 
listed like this:



) no internal value to see) of the tblEntryLst everything looks right.
Is there anything you can suggest for determining where the problem might 
be?

Thanks for the help.

Henry


On Wednesday, April 15, 2015 at 12:43:28 PM UTC-5, Vijay Khemlani wrote:

> You can use get_model
>
> from django.db.models.loading import get_model
>
> YourModel = get_model('your_app', tableName)
>
> On Wed, Apr 15, 2015 at 2:01 PM, Tim Chase  > wrote:
>
>> On 2015-04-15 09:45, Henry Versemann wrote:
>> > My problem is since the logic won't know which tables will be in
>> > the incoming list I need to try to reference the entries in each
>> > table using some kind of evaluated version of a variable containing
>> > the name of each table, as I iterate through the list.
>> >
>> > I'm sure I could do this using an "eval" statement like this
>> >
>> > tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
>> >
>> > eval(tblComannd)
>>
>> Since it's just Python, you can create mappings of the table-objects,
>> and then get attributes on them.
>>
>>   table_names = {
>> "tblA": tblA,
>> "tblB": tblB,
>> "tblRenamed": tblXYZ,
>> }
>>
>>   for name, cls in table_names.items():
>> log.info("Dumping %s", name)
>> for item in cls.objects.all():
>>   write_to_file(item)
>>
>> Presumably, these classes come from some namespace like a module, so
>> you can even do something like
>>
>>   import models
>>   table_names = ["tblA", "tblB"]
>>   for name in table_names:
>> log.info("Dumping %s", name)
>> cls = getattr(models, name)
>> for item in cls.objects.all():
>>   write_to_file(item)
>>
>> -tim
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com .
>> To post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/20150415120110.77c4ea6f%40bigbox.christie.dr
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3d659059-6219-4266-93f9-4e56dc348e49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Henry Versemann
Thanks for the help Stephen.

On Wednesday, April 15, 2015 at 11:53:09 AM UTC-5, Stephen Butler wrote:
>
> Or, if you know all the Model classes are in the same module that 
> you've imported, it should be as easy as: 
>
> from myapp import models 
>
> model_class = getattr(models, tableName) 
>
> On Wed, Apr 15, 2015 at 11:50 AM, Stephen J. Butler 
>  wrote: 
> > Classes (MyModel) are attributes of the module (myapp.models). No 
> > reason to use eval. You can get the module from importlib and then the 
> > class from using getattr() on the module. 
> > 
> > 
> http://stackoverflow.com/questions/10773348/get-python-class-object-from-string
>  
> > 
> > 
> > 
> > On Wed, Apr 15, 2015 at 11:45 AM, Henry Versemann  > wrote: 
> >> I have an incoming list of DB table names associated with my 
> application. I 
> >> want to iterate through this list of table names getting all of the 
> entries 
> >> for each table and then print some data from each tables' entry to a 
> file. 
> >> I've tried to write this so I can use it for as many app-associated 
> tables 
> >> as possible. 
> >> 
> >> My problem is since the logic won't know which tables will be in the 
> >> incoming list I need to try to reference the entries in each table 
> using 
> >> some kind of evaluated version of a variable containing the name of 
> each 
> >> table, as I iterate through the list. 
> >> 
> >> I'm sure I could do this using an "eval" statement like this 
> >> 
> >> tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()" 
> >> 
> >> eval(tblComannd) 
> >> 
> >> but I also know that the "eval" statement is not the safest thing to 
> use. 
> >> 
> >> So far I've tried to reference the table name in the following ways: 
> >> 
> >> tblEntryLst = str(tableName).objects.all() 
> >> tblEntryLst = tableName.objects.all() 
> >> 
> >> with no success at all and only an AttributeError exception being the 
> >> result. 
> >> 
> >> I think its possible since I've successfully done some similar things 
> with 
> >> referencing and saving the values of unknown keys for dictionaries 
> similar 
> >> to this: 
> >> 
> >> new_dictionary[str(extracted_key)] = old_dictionary[str(extracted_key)] 
> >> 
> >> but don't know this for sure. 
> >> 
> >> Can someone please confirm if this is indeed possible to do or not, and 
> if 
> >> so give a general format or example for how to do it? 
> >> 
> >> Any help would be much appreciated. 
> >> 
> >> Thanks. 
> >> 
> >> Henry 
> >> 
> >> 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> Groups 
> >> "Django users" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an 
> >> email to django-users...@googlegroups.com . 
> >> To post to this group, send email to django...@googlegroups.com 
> . 
> >> Visit this group at http://groups.google.com/group/django-users. 
> >> To view this discussion on the web visit 
> >> 
> https://groups.google.com/d/msgid/django-users/e3430efb-7fc2-4baf-931b-6ec8d8580315%40googlegroups.com.
>  
>
> >> For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/83866de5-07b8-4b1a-85ef-3b1993539494%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Vijay Khemlani
You can use get_model

from django.db.models.loading import get_model

YourModel = get_model('your_app', tableName)

On Wed, Apr 15, 2015 at 2:01 PM, Tim Chase 
wrote:

> On 2015-04-15 09:45, Henry Versemann wrote:
> > My problem is since the logic won't know which tables will be in
> > the incoming list I need to try to reference the entries in each
> > table using some kind of evaluated version of a variable containing
> > the name of each table, as I iterate through the list.
> >
> > I'm sure I could do this using an "eval" statement like this
> >
> > tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
> >
> > eval(tblComannd)
>
> Since it's just Python, you can create mappings of the table-objects,
> and then get attributes on them.
>
>   table_names = {
> "tblA": tblA,
> "tblB": tblB,
> "tblRenamed": tblXYZ,
> }
>
>   for name, cls in table_names.items():
> log.info("Dumping %s", name)
> for item in cls.objects.all():
>   write_to_file(item)
>
> Presumably, these classes come from some namespace like a module, so
> you can even do something like
>
>   import models
>   table_names = ["tblA", "tblB"]
>   for name in table_names:
> log.info("Dumping %s", name)
> cls = getattr(models, name)
> for item in cls.objects.all():
>   write_to_file(item)
>
> -tim
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/20150415120110.77c4ea6f%40bigbox.christie.dr
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei33E%2BkH63sCXWx_Y7xPSa91Kv_JB4kYHZt_djvF46LUyQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Tim Chase
On 2015-04-15 09:45, Henry Versemann wrote:
> My problem is since the logic won't know which tables will be in
> the incoming list I need to try to reference the entries in each
> table using some kind of evaluated version of a variable containing
> the name of each table, as I iterate through the list.
> 
> I'm sure I could do this using an "eval" statement like this
> 
> tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
> 
> eval(tblComannd)

Since it's just Python, you can create mappings of the table-objects,
and then get attributes on them.

  table_names = {
"tblA": tblA,
"tblB": tblB,
"tblRenamed": tblXYZ,
}

  for name, cls in table_names.items():
log.info("Dumping %s", name)
for item in cls.objects.all():
  write_to_file(item)

Presumably, these classes come from some namespace like a module, so
you can even do something like

  import models
  table_names = ["tblA", "tblB"]
  for name in table_names:
log.info("Dumping %s", name)
cls = getattr(models, name)
for item in cls.objects.all():
  write_to_file(item)

-tim



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20150415120110.77c4ea6f%40bigbox.christie.dr.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Stephen J. Butler
Or, if you know all the Model classes are in the same module that
you've imported, it should be as easy as:

from myapp import models

model_class = getattr(models, tableName)

On Wed, Apr 15, 2015 at 11:50 AM, Stephen J. Butler
 wrote:
> Classes (MyModel) are attributes of the module (myapp.models). No
> reason to use eval. You can get the module from importlib and then the
> class from using getattr() on the module.
>
> http://stackoverflow.com/questions/10773348/get-python-class-object-from-string
>
>
>
> On Wed, Apr 15, 2015 at 11:45 AM, Henry Versemann  
> wrote:
>> I have an incoming list of DB table names associated with my application. I
>> want to iterate through this list of table names getting all of the entries
>> for each table and then print some data from each tables' entry to a file.
>> I've tried to write this so I can use it for as many app-associated tables
>> as possible.
>>
>> My problem is since the logic won't know which tables will be in the
>> incoming list I need to try to reference the entries in each table using
>> some kind of evaluated version of a variable containing the name of each
>> table, as I iterate through the list.
>>
>> I'm sure I could do this using an "eval" statement like this
>>
>> tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
>>
>> eval(tblComannd)
>>
>> but I also know that the "eval" statement is not the safest thing to use.
>>
>> So far I've tried to reference the table name in the following ways:
>>
>> tblEntryLst = str(tableName).objects.all()
>> tblEntryLst = tableName.objects.all()
>>
>> with no success at all and only an AttributeError exception being the
>> result.
>>
>> I think its possible since I've successfully done some similar things with
>> referencing and saving the values of unknown keys for dictionaries similar
>> to this:
>>
>> new_dictionary[str(extracted_key)] = old_dictionary[str(extracted_key)]
>>
>> but don't know this for sure.
>>
>> Can someone please confirm if this is indeed possible to do or not, and if
>> so give a general format or example for how to do it?
>>
>> Any help would be much appreciated.
>>
>> Thanks.
>>
>> Henry
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/e3430efb-7fc2-4baf-931b-6ec8d8580315%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxUqGKChACN9ecPMVoUKEfWz7yO_GUHc%3DqTQQVJ2-JMM_w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Trying to avoid using the "eval" command/statement

2015-04-15 Thread Stephen J. Butler
Classes (MyModel) are attributes of the module (myapp.models). No
reason to use eval. You can get the module from importlib and then the
class from using getattr() on the module.

http://stackoverflow.com/questions/10773348/get-python-class-object-from-string



On Wed, Apr 15, 2015 at 11:45 AM, Henry Versemann  wrote:
> I have an incoming list of DB table names associated with my application. I
> want to iterate through this list of table names getting all of the entries
> for each table and then print some data from each tables' entry to a file.
> I've tried to write this so I can use it for as many app-associated tables
> as possible.
>
> My problem is since the logic won't know which tables will be in the
> incoming list I need to try to reference the entries in each table using
> some kind of evaluated version of a variable containing the name of each
> table, as I iterate through the list.
>
> I'm sure I could do this using an "eval" statement like this
>
> tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"
>
> eval(tblComannd)
>
> but I also know that the "eval" statement is not the safest thing to use.
>
> So far I've tried to reference the table name in the following ways:
>
> tblEntryLst = str(tableName).objects.all()
> tblEntryLst = tableName.objects.all()
>
> with no success at all and only an AttributeError exception being the
> result.
>
> I think its possible since I've successfully done some similar things with
> referencing and saving the values of unknown keys for dictionaries similar
> to this:
>
> new_dictionary[str(extracted_key)] = old_dictionary[str(extracted_key)]
>
> but don't know this for sure.
>
> Can someone please confirm if this is indeed possible to do or not, and if
> so give a general format or example for how to do it?
>
> Any help would be much appreciated.
>
> Thanks.
>
> Henry
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e3430efb-7fc2-4baf-931b-6ec8d8580315%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxUUpEdBcyDv8vJFWsJGdCMuXkmXs9SBf_STz5%3D9-Jw78w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Trying to avoid using the "eval" command/statement

2015-04-15 Thread Henry Versemann
I have an incoming list of DB table names associated with my application. I 
want to iterate through this list of table names getting all of the 
entries for each table and then print some data from each tables' entry to 
a file. I've tried to write this so I can use it for as many 
app-associated tables as possible.

My problem is since the logic won't know which tables will be in the 
incoming list I need to try to reference the entries in each table 
using some kind of evaluated version of a variable containing the name of 
each table, as I iterate through the list.

I'm sure I could do this using an "eval" statement like this

tblComannd = "tblEntryLst = " + str(tableName) + ".objects.all()"

eval(tblComannd)

but I also know that the "eval" statement is not the safest thing to use.

So far I've tried to reference the table name in the following ways:

tblEntryLst = str(tableName).objects.all()
tblEntryLst = tableName.objects.all()

with no success at all and only an AttributeError exception being the 
result.

I think its possible since I've successfully done some similar things with 
referencing and saving the values of unknown keys for dictionaries similar 
to this:

new_dictionary[str(extracted_key)] = old_dictionary[str(extracted_key)]

but don't know this for sure.

Can someone please confirm if this is indeed possible to do or not, and if 
so give a general format or example for how to do it?

Any help would be much appreciated.

Thanks.

Henry
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e3430efb-7fc2-4baf-931b-6ec8d8580315%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using the eval() command

2015-03-12 Thread Carl Meyer
Hi Henry,

On 03/10/2015 07:37 PM, Henry Versemann wrote:
> So how does an expression like you suggested above (
> innerDict['+newinnrkey+'] = newinnrval ) work then?
> It seems like it wouldn't work without enclosing the expression
> with quotes or double-quotes, and even then it seems like it would only
> become some kind of string instead of a statement which would be
> automatically executed to produce a result. Please explain or point me
> to some documentation explaining this type of code or coding.
> Thanks.

I didn't suggest that line of code, Bill did. As Tom said, it was
probably a typo. The line of code I suggested, which is what you should
use, is simply:

innerDict[newinnrkey] = newinnrval

Carl

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/550067A9.6030903%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature


Re: Using the eval() command

2015-03-12 Thread Tom Evans
On Wed, Mar 11, 2015 at 1:24 AM, Henry Versemann  wrote:
> so its valid python code to write the expression
>
> innerDict['+newinnrkey+']
>
> without enclosing the parts outside of the plus-signs surrounding the
> "newinnrkey" variable within quotes or double quotes?
> I'm not sure I've ever heard or seen such python code before anywhere.
> Can you explain or point me to some docs on this type of code expression?
> And are there any gotchas or problems coding expressions like this?
> Thanks for the help.
>

No, it isn't valid, Bill mis-typed his reply. I don't want to speak
for him, but I think he meant to write:

>  Why wouldn't you just say:
>
>innerDict[newinnrkey] = newinnrval

and, indeed, why wouldn't you?

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1Jh-C0Rns0nxZD2m%3DME3PW-iwY0dF7XTJ_y9s7iRmtOsA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using the eval() command

2015-03-11 Thread Henry Versemann
Carl,

Thanks for the advice and information. I'm certainly going to try it.
Thanks again for the help.

Henry

On Wednesday, March 11, 2015 at 11:05:38 AM UTC-5, Carl Meyer wrote:

> Hi Henry, 
>
> On 03/10/2015 07:37 PM, Henry Versemann wrote: 
> > So how does an expression like you suggested above ( 
> > innerDict['+newinnrkey+'] = newinnrval ) work then? 
> > It seems like it wouldn't work without enclosing the expression 
> > with quotes or double-quotes, and even then it seems like it would only 
> > become some kind of string instead of a statement which would be 
> > automatically executed to produce a result. Please explain or point me 
> > to some documentation explaining this type of code or coding. 
> > Thanks. 
>
> I didn't suggest that line of code, Bill did. As Tom said, it was 
> probably a typo. The line of code I suggested, which is what you should 
> use, is simply: 
>
> innerDict[newinnrkey] = newinnrval 
>
> Carl 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9b1a8a7a-a5e0-47f7-b71f-ab388d88290b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using the eval() command

2015-03-10 Thread Henry Versemann
So how does an expression like you suggested above ( 
innerDict['+newinnrkey+'] = newinnrval ) work then?
It seems like it wouldn't work without enclosing the expression with quotes 
or double-quotes, and even then it seems like it would only become some 
kind of string instead of a statement which would be automatically executed 
to produce a result. Please explain or point me to some 
documentation explaining this type of code or coding.
Thanks.

Henry

On Tuesday, March 10, 2015 at 4:49:48 PM UTC-5, Carl Meyer wrote:

> Hi Henry, 
>
> On 03/10/2015 03:25 PM, Henry Versemann wrote: 
> > I have a new dictionary that I want to build, using data from another 
> > dictionary. I have a view which is receiving a single key/value pair 
> > from the original dictionary. Then in the view I've defined the new 
> > dictionary like this: 
> > 
> > innerDict = {}   
> > 
> > Now I want to make this as dynamic as possible so I'm trying to use the 
> > "eval()" statement below to add the new key/value pair to the new 
> > dictionary, which is declared above. Will the following code work to 
> > actually add the new key/value pair to the new dictionary? 
> > 
> > innrDictCmnd = "innerDict['"+newinnrkey+"'] = newinnrval" 
> > eval(innrDictCmnd) 
> > 
> > If not why not, and in lieu of the statements above not working, then 
> > how would I do it? 
>
> It doesn't work, because eval() only accepts expressions; assignment is 
> a statement. Using exec() instead of eval() will work (though the way 
> you have it written, it will always assign the string "newinnrval" -- 
> perhaps you meant to end innrDictCmnd with '... = ' + newinnrval). 
>
> But regardless, you should not use either eval() or exec(). 
>
> Since you say this code is in a view, I assume that newinnrkey comes 
> from request data (user input). Imagine what happens if I am a malicious 
> user and I call this view with newinnrkey set to: 
>
> '] = 0; import os; os.rm('/'); d = {}; d[' 
>
> Oops. 
>
> Both exec() and eval() should be avoided. They are very rarely 
> necessary, they usually make code less readable and maintainable, and if 
> you ever accidentally pass user input to them, you've opened up a 
> security hole in your application that someone could drive a truck 
> through. 
>
> For your case, what's wrong with just writing `innerDict[newinnerkey] = 
> newinnerval`? It's every bit as dynamic as the version using eval or 
> exec - the eval/exec gains you nothing. 
>
> Carl 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/36ef505c-9957-4b68-964e-b8861e7fec14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using the eval() command

2015-03-10 Thread Henry Versemann
so its valid python code to write the expression

innerDict['+newinnrkey+'] 

without enclosing the parts outside of the plus-signs surrounding the 
"newinnrkey" variable within quotes or double quotes?
I'm not sure I've ever heard or seen such python code before anywhere.
Can you explain or point me to some docs on this type of code expression?
And are there any gotchas or problems coding expressions like this?
Thanks for the help.

Henry 
 

On Tuesday, March 10, 2015 at 4:34:34 PM UTC-5, ke1g wrote:

> eval() operates on an expression, not a statement.  Assignment makes it a 
> statement.
>
> Why wouldn't you just say:
>
>   innerDict['+newinnrkey+'] = newinnrval
>
>
> On Tue, Mar 10, 2015 at 5:25 PM, Henry Versemann  > wrote:
>
>> I have a new dictionary that I want to build, using data from another 
>> dictionary. I have a view which is receiving a single key/value pair 
>> from the original dictionary. Then in the view I've defined the new 
>> dictionary like this:
>>
>> innerDict = {}  
>>
>> Now I want to make this as dynamic as possible so I'm trying to use the 
>> "eval()" statement below to add the new key/value pair to the new 
>> dictionary, which is declared above. Will the following code work to 
>> actually add the new key/value pair to the new dictionary?
>>
>> innrDictCmnd = "innerDict['"+newinnrkey+"'] = newinnrval"
>> eval(innrDictCmnd)
>>
>> If not why not, and in lieu of the statements above not working, then how 
>> would I do it?
>>
>> Thanks for the help.
>>
>> Henry
>>
>>
>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com .
>> To post to this group, send email to django...@googlegroups.com 
>> .
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/e6c61ca1-efba-4965-a5ec-f10b55927b15%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a6113acb-e437-42c6-88d9-f19476905dbd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using the eval() command

2015-03-10 Thread Carl Meyer
Hi Henry,

On 03/10/2015 03:25 PM, Henry Versemann wrote:
> I have a new dictionary that I want to build, using data from another
> dictionary. I have a view which is receiving a single key/value pair
> from the original dictionary. Then in the view I've defined the new
> dictionary like this:
> 
> innerDict = {}  
> 
> Now I want to make this as dynamic as possible so I'm trying to use the
> "eval()" statement below to add the new key/value pair to the new
> dictionary, which is declared above. Will the following code work to
> actually add the new key/value pair to the new dictionary?
> 
> innrDictCmnd = "innerDict['"+newinnrkey+"'] = newinnrval"
> eval(innrDictCmnd)
> 
> If not why not, and in lieu of the statements above not working, then
> how would I do it?

It doesn't work, because eval() only accepts expressions; assignment is
a statement. Using exec() instead of eval() will work (though the way
you have it written, it will always assign the string "newinnrval" --
perhaps you meant to end innrDictCmnd with '... = ' + newinnrval).

But regardless, you should not use either eval() or exec().

Since you say this code is in a view, I assume that newinnrkey comes
from request data (user input). Imagine what happens if I am a malicious
user and I call this view with newinnrkey set to:

'] = 0; import os; os.rm('/'); d = {}; d['

Oops.

Both exec() and eval() should be avoided. They are very rarely
necessary, they usually make code less readable and maintainable, and if
you ever accidentally pass user input to them, you've opened up a
security hole in your application that someone could drive a truck through.

For your case, what's wrong with just writing `innerDict[newinnerkey] =
newinnerval`? It's every bit as dynamic as the version using eval or
exec - the eval/exec gains you nothing.

Carl


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/54FF66E3.5050408%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature


Re: Using the eval() command

2015-03-10 Thread Bill Freeman
eval() operates on an expression, not a statement.  Assignment makes it a
statement.

Why wouldn't you just say:

  innerDict['+newinnrkey+'] = newinnrval


On Tue, Mar 10, 2015 at 5:25 PM, Henry Versemann 
wrote:

> I have a new dictionary that I want to build, using data from another
> dictionary. I have a view which is receiving a single key/value pair
> from the original dictionary. Then in the view I've defined the new
> dictionary like this:
>
> innerDict = {}
>
> Now I want to make this as dynamic as possible so I'm trying to use the
> "eval()" statement below to add the new key/value pair to the new
> dictionary, which is declared above. Will the following code work to
> actually add the new key/value pair to the new dictionary?
>
> innrDictCmnd = "innerDict['"+newinnrkey+"'] = newinnrval"
> eval(innrDictCmnd)
>
> If not why not, and in lieu of the statements above not working, then how
> would I do it?
>
> Thanks for the help.
>
> Henry
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e6c61ca1-efba-4965-a5ec-f10b55927b15%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAB%2BAj0v6ZY-d2z7QOrg9XnKb11kWoR6uBov_9ehohAAF5v7nYg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Using the eval() command

2015-03-10 Thread Henry Versemann
I have a new dictionary that I want to build, using data from another 
dictionary. I have a view which is receiving a single key/value pair 
from the original dictionary. Then in the view I've defined the new 
dictionary like this:

innerDict = {}  

Now I want to make this as dynamic as possible so I'm trying to use the 
"eval()" statement below to add the new key/value pair to the new 
dictionary, which is declared above. Will the following code work to 
actually add the new key/value pair to the new dictionary?

innrDictCmnd = "innerDict['"+newinnrkey+"'] = newinnrval"
eval(innrDictCmnd)

If not why not, and in lieu of the statements above not working, then how 
would I do it?

Thanks for the help.

Henry


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e6c61ca1-efba-4965-a5ec-f10b55927b15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.