[Tutor] question about listing variables defined since session started

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] question about listing variables defined since session started

Comer Duncan
Hi,

I have a newbie type question.  Say I have started a python (or
ipython) session and have done some imports and have also defined some
new variables since the session started.  So, I have in my current
namespace a bunch of things. Suppose I  want to list just those
variable  names which have been defined since the session started but
not include the names of the objects that who and whos will return.
How to do that?  In matlab, this is what the who returns, but in
python I seem to always get a raft of things since I typically do
import a bunch of things.

Thanks for your suggestions.

Comer
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Robert Sjoblom
On 30 April 2012 23:25, Comer Duncan <[hidden email]> wrote:
> Hi,
>
> I have a newbie type question.  Say I have started a python (or
> ipython) session and have done some imports and have also defined some
> new variables since the session started.  So, I have in my current
> namespace a bunch of things. Suppose I  want to list just those
> variable  names which have been defined since the session started but
> not include the names of the objects that who and whos will return.
> How to do that?

Not entirely sure, but something like this might work (untested):
for name in dir():
    myvalue = eval(name)
    print name, "is", type(name), "and is equal to ", myvalue

There's also global(), local() and vars().
--
best regards,
Robert S.
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Alan Gauld
In reply to this post by Comer Duncan
On 30/04/12 22:25, Comer Duncan wrote:

> I have a newbie type question.  Say I have started a python (or
> ipython) session and have done some imports and have also defined some
> new variables since the session started.  So, I have in my current
> namespace a bunch of things. Suppose I  want to list just those
> variable  names which have been defined since the session started

You could save the initial startup state then later do a delta.
Saving startup state only needs doing once since it should be
the same each time - unless you define local startup commands - in
whioch case you will need to regenerate the startup state..

> not include the names of the objects that who and whos will return.

What are who and whos?
They are not defined in my version of Python...

> How to do that?  In matlab, this is what the who returns,

No idea what Matlab does, sorry.

> python I seem to always get a raft of things since I typically do
> import a bunch of things.

So I'm guessing you don't want any of the imported stuff?
What if you define a variable in an imported module?
Should that be listed or not?

But basically I think you want locals() - startup()
[where you define startup as described above]


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Steven D'Aprano-8
In reply to this post by Comer Duncan
Comer Duncan wrote:
> Hi,
>
> I have a newbie type question.  Say I have started a python (or
> ipython) session and have done some imports and have also defined some
> new variables since the session started.  So, I have in my current
> namespace a bunch of things. Suppose I  want to list just those
> variable  names which have been defined since the session started but
> not include the names of the objects that who and whos will return.

What's "who and whos"?


> How to do that?  In matlab, this is what the who returns, but in
> python I seem to always get a raft of things since I typically do
> import a bunch of things.

That depends on what you are doing.

If you are using dir(), then you will get a list of all the currently existing
objects in your session. There's no way to show only "names defined since the
session started". Maybe iPython does something like that, but I doubt it.

Taken literally, I don't think you want is possible in Python. When objects
are created, they aren't timestamped with the moment of when they were
created, or who created them, or anything else. So there's no way to tell the
difference between "x = 1" done during system startup and "x = 1" done after
system startup.

But why do you care? If you explain in more detail what you are hoping to
accomplish, perhaps we can think of an alternative way to do so.


--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Robert Sjoblom
> What's "who and whos"?
They're matlab functions:
who lists the variables currently in the workspace.
whos lists the current variables and their sizes and types. It also
reports the totals for sizes.

--
best regards,
Robert S.
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Steven D'Aprano-8
In reply to this post by Robert Sjoblom
Robert Sjoblom wrote:

> On 30 April 2012 23:25, Comer Duncan <[hidden email]> wrote:
>> Hi,
>>
>> I have a newbie type question.  Say I have started a python (or
>> ipython) session and have done some imports and have also defined some
>> new variables since the session started.  So, I have in my current
>> namespace a bunch of things. Suppose I  want to list just those
>> variable  names which have been defined since the session started but
>> not include the names of the objects that who and whos will return.
>> How to do that?
>
> Not entirely sure, but something like this might work (untested):
> for name in dir():
>     myvalue = eval(name)
>     print name, "is", type(name), "and is equal to ", myvalue

Please do not use eval unless you know what you are doing, and certainly don't
encourage newbies to use it without a word about the risks.

(I really wish eval and exec were hidden inside a module that you had to
import, to discourage people from using them unnecessarily.)

My advice is:

Never use eval.
For experts only: hardly ever use eval.

eval is slow. eval is tricky to use correctly for all but the simplest uses.
eval is dangerous.

In this *specific* case, using eval is probably safe. But as a matter of best
practice, you should not use eval when there is a simpler and safer alternative:

for name in dir():
     print name, "is", vars()[name]


You can replace vars() with globals() if you prefer.

Possibly better still:

from pprint import pprint
pprint(vars())



Why is eval so dangerous?

Because it executes code.

The risk with eval is not using it at the interactive interpreter. If you want
to destroy your own data, there are easier ways than using eval. But the risk
is that you write a function that uses eval, and then some day that function
gets used in your web application, and you collect text from users on the
Internet who feed your application something that causes eval to execute code.
Suddenly, your web server is under their control and they can do *anything*.

Sound far-fetched? But it happens, and very frequently. Code injection attacks
are now the *most* common security vulnerability, more common than even buffer
overflows. Whenever you hear about some website being compromised, or a virus
or trojan horse taking over people's desktops, there is a high probability
that it is because some coder used the equivalent of "eval" incorrectly.

Here is a humorous look at the issue of code injection:

http://xkcd.com/327/


and a more serious discussion:

http://en.wikipedia.org/wiki/Code_injection



--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Prasad, Ramit-2
> Steven D'Aprano wrote:
> Robert Sjoblom wrote:
> > On 30 April 2012 23:25, Comer Duncan <[hidden email]> wrote:
> >> Hi,
> >>
> >> I have a newbie type question.  Say I have started a python (or
> >> ipython) session and have done some imports and have also defined some
> >> new variables since the session started.  So, I have in my current
> >> namespace a bunch of things. Suppose I  want to list just those
> >> variable  names which have been defined since the session started but
> >> not include the names of the objects that who and whos will return.
> >> How to do that?
> >
> > Not entirely sure, but something like this might work (untested):
> > for name in dir():
> >     myvalue = eval(name)
> >     print name, "is", type(name), "and is equal to ", myvalue
>
> Please do not use eval unless you know what you are doing, and certainly
> don't
> encourage newbies to use it without a word about the risks.
>

ast.literal_eval(name) is probably safer.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Steven D'Aprano-8
Prasad, Ramit wrote:

>> Steven D'Aprano wrote:
>> Robert Sjoblom wrote:
>>> On 30 April 2012 23:25, Comer Duncan <[hidden email]> wrote:
>>>> Hi,
>>>>
>>>> I have a newbie type question.  Say I have started a python (or
>>>> ipython) session and have done some imports and have also defined some
>>>> new variables since the session started.  So, I have in my current
>>>> namespace a bunch of things. Suppose I  want to list just those
>>>> variable  names which have been defined since the session started but
>>>> not include the names of the objects that who and whos will return.
>>>> How to do that?
>>> Not entirely sure, but something like this might work (untested):
>>> for name in dir():
>>>     myvalue = eval(name)
>>>     print name, "is", type(name), "and is equal to ", myvalue
>> Please do not use eval unless you know what you are doing, and certainly
>> don't
>> encourage newbies to use it without a word about the risks.
>>
>
> ast.literal_eval(name) is probably safer.

Safer, but doesn't work:


py> import ast
py> name = 25
py> ast.literal_eval('name')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "ast.py", line 87, in literal_eval
     return _convert(node_or_string)
   File "ast.py", line 86, in _convert
     raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0xb7a9560c>


literal_eval is for evaluating literals, not names.

py> ast.literal_eval('[123, "ABC", None, {}]')
[123, 'ABC', None, {}]


It apparently can also do simply arithmetic, but that's *possibly* an
implementation detail due to the keyhole optimizer in CPython's compiler.


--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] question about listing variables defined since session started

Prasad, Ramit-2
Steven D'Aprano wrote:

> Prasad, Ramit wrote:
> >> Steven D'Aprano wrote:
> >> Robert Sjoblom wrote:
> >>> On 30 April 2012 23:25, Comer Duncan <[hidden email]> wrote:
> >>>> Hi,
> >>>>
> >>>> I have a newbie type question.  Say I have started a python (or
> >>>> ipython) session and have done some imports and have also defined
> some
> >>>> new variables since the session started.  So, I have in my current
> >>>> namespace a bunch of things. Suppose I  want to list just those
> >>>> variable  names which have been defined since the session started but
> >>>> not include the names of the objects that who and whos will return.
> >>>> How to do that?
> >>> Not entirely sure, but something like this might work (untested):
> >>> for name in dir():
> >>>     myvalue = eval(name)
> >>>     print name, "is", type(name), "and is equal to ", myvalue
> >> Please do not use eval unless you know what you are doing, and
> certainly
> >> don't
> >> encourage newbies to use it without a word about the risks.
> >>
> >
> > ast.literal_eval(name) is probably safer.
>
> Safer, but doesn't work:
>
>
> py> import ast
> py> name = 25
> py> ast.literal_eval('name')
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "ast.py", line 87, in literal_eval
>      return _convert(node_or_string)
>    File "ast.py", line 86, in _convert
>      raise ValueError('malformed node or string: ' + repr(node))
> ValueError: malformed node or string: <_ast.Name object at 0xb7a9560c>
>
>
> literal_eval is for evaluating literals, not names.
>
> py> ast.literal_eval('[123, "ABC", None, {}]')
> [123, 'ABC', None, {}]
>
>
> It apparently can also do simply arithmetic, but that's *possibly* an
> implementation detail due to the keyhole optimizer in CPython's compiler.
>

What about just using dir / globals / locals?

global_variables = globals()
for name in dir():
   value = globals_variables[ name ] if name in global_variables else locals[ name ]
   print '{0} is {1} and is equal to {2}'.format( name, type(value), value )  

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor