Re: [Tutor] Imaplib Select Fails while connecting folder(**Labelname) of gmail

2012-04-30 Thread Steven D'Aprano

Anurag Maherchandani wrote:

I am using imaplib module for connecting to Gmail Imap, and i am getting
the below mentioned error.
I am using select command to connect

Labelname is  **LabelName


I Get this Error:

resp, data = self._imap.select("**LabelName")
  File "/usr/lib/python2.6/imaplib.py", line 642, in select
typ, dat = self._simple_command(name, mailbox)
  File "/usr/lib/python2.6/imaplib.py", line 1060, in _simple_command
return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.6/imaplib.py", line 895, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SELECT command error: BAD ['Could not parse command']

whereas if the Labelname is
** LabelName

It successfully connects.



Do you have a question? Since it successfully connects when you give the name 
"** LabelName", I don't understand what your problem is.


Is there something that makes you think that select("**LabelName") should also 
work?




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-04-30 Thread Steven D'Aprano

Robert Sjoblom wrote:

On 30 April 2012 23:25, 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.
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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-04-30 Thread 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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-04-30 Thread Steven D'Aprano

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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-04-30 Thread Alan Gauld

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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Mark Lawrence

On 01/05/2012 00:35, Steven D'Aprano wrote:

Mark Lawrence wrote:

On 30/04/2012 19:40, Alan Gauld wrote:



But if the number of spaces is critical string formatting is better
still. And better than string addition.



True indeed, but which of the three versions of string formatting that
I'm aware of?


Any of them.




Alright you  antipodean :)

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Steven D'Aprano

Mark Lawrence wrote:

On 30/04/2012 19:40, Alan Gauld wrote:



But if the number of spaces is critical string formatting is better
still. And better than string addition.



True indeed, but which of the three versions of string formatting that 
I'm aware of?


Any of them.


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Mark Lawrence

On 30/04/2012 19:40, Alan Gauld wrote:

On 30/04/12 19:27, Mark Lawrence wrote:


print 'Addition of above two numbers are : ', z


Except that you'll get two spaces after the colon :)


OK thats true,
Try this:

print 'Addition of above two numbers are :', z

for one. :-)

But if the number of spaces is critical string formatting is better
still. And better than string addition.



True indeed, but which of the three versions of string formatting that 
I'm aware of?


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-04-30 Thread Robert Sjoblom
On 30 April 2012 23:25, 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.
> 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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] question about listing variables defined since session started

2012-04-30 Thread 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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Alan Gauld

On 30/04/12 19:27, Mark Lawrence wrote:


print 'Addition of above two numbers are : ', z


Except that you'll get two spaces after the colon :)


OK thats true,
Try this:

print 'Addition of above two numbers are :', z

for one. :-)

But if the number of spaces is critical string formatting is better 
still. And better than string addition.


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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Mark Lawrence

On 30/04/2012 18:36, Alan Gauld wrote:

On 30/04/12 11:00, Kapil Shukla कपिल शुक्‍ला wrote:

Viral
You should be doing this

"Addition of two numbers is" + str(x+y)


There is no need for str() since print implicitly calls string to
convert objects to string format. Also the addition is not needed since
print takes a comma separated list of arguments. So it would normally be:

print 'Addition of above two numbers are : ', z


Except that you'll get two spaces after the colon :)





+ operator works on same datatypes and


Using addition on strings is quite expensive and there are usually
better ways to do the same job.



int being one of the built in objects in python does not have a method z


This however is true(ish - int is a type not strictly an object, except
that everything in Python is an object, including types! :-) and it is
the source of the original error message.




--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Alan Gauld

On 30/04/12 11:00, Kapil Shukla कपिल शुक्‍ला wrote:

Viral
You should be doing this

"Addition of two numbers is" + str(x+y)


There is no need for str() since print implicitly calls string to 
convert objects to string format. Also the addition is not needed since 
print takes a comma separated list of arguments. So it would normally be:


print 'Addition of above two numbers are : ', z



+ operator works on same datatypes and


Using addition on strings is quite expensive and there are usually 
better ways to do the same job.




int being one of the built in objects in python does not have a method z


This however is true(ish - int is a type not strictly an object, except 
that everything in Python is an object, including types! :-) and it is 
the source of the original error message.


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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Imaplib Select Fails while connecting folder(**Labelname) of gmail

2012-04-30 Thread Anurag Maherchandani
I am using imaplib module for connecting to Gmail Imap, and i am getting
the below mentioned error.
I am using select command to connect

Labelname is  **LabelName


I Get this Error:

resp, data = self._imap.select("**LabelName")
  File "/usr/lib/python2.6/imaplib.py", line 642, in select
typ, dat = self._simple_command(name, mailbox)
  File "/usr/lib/python2.6/imaplib.py", line 1060, in _simple_command
return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.6/imaplib.py", line 895, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SELECT command error: BAD ['Could not parse command']

whereas if the Labelname is
** LabelName

It successfully connects.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Dave Angel
On 04/30/2012 05:50 AM, viral shah wrote:
> Hi
>
> I'm new in the learning of python
>
> I want to make simple code for two variable addition
>
> Please help me in the following code
>
> *x = 12
> print x
> y =20
> print y
> z = x+y
> print 'Addition of above two numbers are : ' + int.z
> *
> when I run this same I got an error message :
>
> *AttributeError: type object 'int' has no attribute 'z'
>
> Please help me to solve the same error
>
>

Welcome to Python tutor.

Normally, the print statement accepts a list of items, separated by
commas.  Each item is implicitly converted to a string, and the results
are sent to standard out, with a space between each item.

So the most straightforward way to print z would be something like:

print 'Addition of above two numbers are:',  z

Now, sometimes you want to combine two strings yourself, so you might use:

print 'Addition of above two numbers are:' + str(z)

Here we are using the str type as a conversion function.  Note that we
used parentheses, not the dot operator.  And notice that since print
only gets one item, it does not add a space.





-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Variable Addition

2012-04-30 Thread Kapil Shukla कपिल शुक्‍ला
Viral
You should be doing this 

"Addition of two numbers is" + str(x+y)

+ operator works on same datatypes and int being one of the built in objects in 
python does not have a method z

U shld use str() or int() to do type conversion 

-Kapil
कपिल शुक्‍ला

-Original Message-
From: viral shah 
Sender: tutor-bounces+shukla.kapil=gmail@python.org
Date: Mon, 30 Apr 2012 15:20:21 
To: 
Subject: [Tutor] Python Variable Addition

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Variable Addition

2012-04-30 Thread viral shah
Hi

I'm new in the learning of python

I want to make simple code for two variable addition

Please help me in the following code

*x = 12
print x
y =20
print y
z = x+y
print 'Addition of above two numbers are : ' + int.z
*
when I run this same I got an error message :

*AttributeError: type object 'int' has no attribute 'z'

Please help me to solve the same error

*--
Viral Shah
IT Department,
E-mail : shahviral...@gmail.com
Mobile : (+91) 9722312220
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: help

2012-04-30 Thread spawgi
What does your input look like?
Please provide that information.

Regards,
- SWP

On Mon, Apr 30, 2012 at 12:30 PM, viral shah  wrote:

>
>
> I want to print below matrix.
>
> can any one suggest me the method for the same
>
> 1 2   3
> 4 5   6
> 7 8   9
>
> Thanks
>
> --
> Viral Shah
> IT Department,
> E-mail : shahviral...@gmail.com
> Mobile : (+91) 9722312220
>
>
>
>
>
>
> --
> Viral Shah
> IT Department,
> E-mail : shahviral...@gmail.com
> Mobile : (+91) 9722312220
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: help

2012-04-30 Thread Mark Lawrence

On 30/04/2012 08:00, viral shah wrote:

I want to print below matrix.

can any one suggest me the method for the same

1 2   3
4 5   6
7 8   9

Thanks




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Quoting from c.l.p at 07:46

"In general, for homework questions, you should present your attempt at
a solution, with specific questions where you are running into
difficulty.  Are you able to output anything using a python program?
If not, you should take a look at one of the several excellent
tutorials easily found by a web search.  The official tutorial is at
http://docs.python.org/py3k/tutorial/ and it might be enough for you
to at least attempt a solution to your problem."

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: help

2012-04-30 Thread Alan Gauld

On 30/04/12 08:00, viral shah wrote:



I want to print below matrix.

can any one suggest me the method for the same

1 2   3
4 5   6
7 8   9



print '''
1 2   3
4 5   6
7 8   9
'''


But I suspect you wanted more than that?
But without some context I can't guess what.

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: help

2012-04-30 Thread viral shah
I want to print below matrix.

can any one suggest me the method for the same

1 2   3
4 5   6
7 8   9

Thanks

-- 
Viral Shah
IT Department,
E-mail : shahviral...@gmail.com
Mobile : (+91) 9722312220






-- 
Viral Shah
IT Department,
E-mail : shahviral...@gmail.com
Mobile : (+91) 9722312220
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor