Re: [Tutor] Query about python recipies for practices

2019-05-27 Thread Alan Gauld via Tutor
On 27/05/2019 23:44, Mats Wichmann wrote:

> and, if you meant problems for practicing... there are a lot of those
> around.  Most courses have problems for you to solve, naturally, and a
> brief hunt around turned up some sites like these that are not courseware:

And don't forget the wonderful Python Challenge

http://www.pythonchallenge.com/

Now up to 33 levels!


-- 
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


Re: [Tutor] Query about python recipies for practices

2019-05-27 Thread Mats Wichmann
On 5/27/19 11:43 AM, boB Stepp wrote:
> On Sun, May 19, 2019 at 12:55 PM bijaya dalei <2212bij...@gmail.com> wrote:
>>
>> Hii, Good morning. I am a new user of python programming language. I have a
>> small query on "where to get python recepies for practices".plz
>> suggest.Thanks.
> 
> It is not very clear to me what you are asking for, which may be why
> you have not gotten any responses so far.
> 
> ActiveState used to have a Python "recipe" section.  Apparently it has
> been moved to a GitHub repository at
> https://github.com/ActiveState/code

and, if you meant problems for practicing... there are a lot of those
around.  Most courses have problems for you to solve, naturally, and a
brief hunt around turned up some sites like these that are not courseware:

http://www.practicepython.org/
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
https://w3resource.com/python-exercises/

No personal experience of the quality of any of these

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


Re: [Tutor] Query about python recipies for practices

2019-05-27 Thread boB Stepp
On Sun, May 19, 2019 at 12:55 PM bijaya dalei <2212bij...@gmail.com> wrote:
>
> Hii, Good morning. I am a new user of python programming language. I have a
> small query on "where to get python recepies for practices".plz
> suggest.Thanks.

It is not very clear to me what you are asking for, which may be why
you have not gotten any responses so far.

ActiveState used to have a Python "recipe" section.  Apparently it has
been moved to a GitHub repository at
https://github.com/ActiveState/code

I own a book, "Python Cookbook, 3rd ed." by Beazley and Jones.
Perhaps that might help?

Some cautionary words of warning:  If this is an obscure way of
getting a pre-packaged homework solution, then you are doing yourself
a grave disservice!

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


Re: [Tutor] Query: lists

2018-08-14 Thread Alan Gauld via Tutor
On 14/08/18 22:38, Cameron Simpson wrote:

> If you're trying to partition words into values starting with "x" and values 
> not starting with "x", you're better off making a separate collection for the 
> "not starting with x" values. And that has me wondering what the list "b" in 
> your code was for originally.

And further to Cameron's point this demonstrates why choosing
meaningful variable names (rather than single letters) is so
important. If a,b and z had been names expressing their purpose
we would be better able to guess at your intentions and
ultimate goal. But with single letters we have no real clue.

> As a matter of principle, functions that "compute a value" (in your case, a 
> list of the values starting with "x") should try not to modify what they are 
> given as parameters. When you pass values to Python functions, you are 
> passing 
> a reference, not a new copy. If a function modifies that reference's 
> _content_, 
> as you do when you go "words.move(z)", you're modifying the original.

It's also good if functions that compute a value *return* that
value rather than (or as well as) print it. The act of returning
something also helps to clarify the functions objective. With
multiple print statements we are not quite sure which line of
output is the most important.


-- 
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


Re: [Tutor] Query: lists

2018-08-14 Thread Alan Gauld via Tutor
On 14/08/18 23:16, Peter Otten wrote:

> For a simple solution you do need a and b: leave words unchanged, append 
> words starting with "x" to a and words not starting with "x" to b.
> 
> Someone familiar with Python might do it with a sort key instead:

Or, for one definition of simple, a list comprehension?

filtered_list = [word for word in words if not word.startswith('x')]

Of course it doesn't retain the deleted words if that is important.
Or if you only want the deleted words simply remove the 'not'.

If you need all three results (original, filtered and removed)
then you need the original 'a' and 'b' lists as well as the
original 'words'.

It all depends on what exactly you need as an end result.

-- 
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


Re: [Tutor] Query: lists

2018-08-14 Thread Nitin Madhok
Deepti,

What you’re seeing happens because you are making changes (words.remove(z)) to 
the list while you are iterating over it (for z in words). If your goal is to 
print the original words, removed words and original words without removed 
words, you could do something like this using sets:

words = ['bbb', 'ccc', 'axx', 'xzz', 'xaa']

def front_x(words):
 # +++your code here+++
 removed_words = []
 for word in words:
   if word.startswith('x'):
 removed_words.append(word)
 print 'word is', word
 print 'original', sorted(words)
 print 'new', sorted(removed_words)
 print sorted(list(set(removed_words + words)))

If you also wanted to get the original words after removing the removed_words, 
you could print them like this:
print 'original - new', list(set(words) - set(removed_words))

Or you could even use list comprehension like this:
print 'original - new', [word for word in words if word not in removed_words]

--

Thanks,
Nitin Madhok
Clemson University

CONFIDENTIALITY NOTICE
This e-mail, and any attachments thereto, is intended only for use by the 
addressee(s) named herein and may contain privileged and/or confidential 
information. If you are not the intended recipient of this e-mail, any 
dissemination, distribution or copying of this e-mail, and any attachments 
thereto, is strictly prohibited. If you have received this e-mail in error, 
please immediately notify the sender by e-mail or telephone and permanently 
delete all copies of this e-mail and any attachments.

> On Aug 14, 2018, at 4:11 AM, Deepti K  wrote:
> 
> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
> function, it picks up only 'xzz' and not 'xaa'
> 
> def front_x(words):
>  # +++your code here+++
>  a = []
>  b = []
>  for z in words:
>if z.startswith('x'):
>  words.remove(z)
>  b.append(z)
>  print 'z is', z
>  print 'original', sorted(words)
>  print 'new', sorted(b)
>  print sorted(b) + sorted(words)
> 
> Thanks,
> Deepti
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Query: lists

2018-08-14 Thread Deepti K
Thanks all. This is very helpful. I am new to Python :)

Sent from my iPhone

> On 15 Aug 2018, at 8:16 am, Peter Otten <__pete...@web.de> wrote:
> 
> Alan Gauld via Tutor wrote:
> 
>>> On 14/08/18 09:11, Deepti K wrote:
>>> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
>>> function, it picks up only 'xzz' and not 'xaa'
>> 
>> Correct because
>> 
>>> def front_x(words):
>>>  # +++your code here+++
>>>  a = []
>>>  b = []
>>>  for z in words:
>>>if z.startswith('x'):
>>>  words.remove(z)
>> 
>> You just changed the thing you are iterating over.
>> By removing an elem,ent the list got shorter so the
>> internal counter inside the for loop now points at
>> the next item - ie it skipped one.
>> 
>> As a general rule never modify the thing you are
>> iterating over with a for loop - use a copy or
>> change to a while loop instead.
>> 
>> 
>>>  b.append(z)
>>>  print 'z is', z
>>>  print 'original', sorted(words)
>> 
>> But it's not the original because you've removed
>> some items.
>> 
>>>  print 'new', sorted(b)
>>>  print sorted(b) + sorted(words)
>> 
>> But this should be the same as the original
>> (albeit almost sorted).
>> 
>> PS. Since you only modify 'b' and 'words' you
>> don't really need 'a'
> 
> For a simple solution you do need a and b: leave words unchanged, append 
> words starting with "x" to a and words not starting with "x" to b.
> 
> Someone familiar with Python might do it with a sort key instead:
> 
 sorted(['bbb', 'ccc', 'axx', 'xzz', 'xaa'],
> ... key=lambda s: not s.startswith("x"))
> ['xzz', 'xaa', 'bbb', 'ccc', 'axx']
> 
> If you want ['xaa', 'xzz', 'axx', 'bbb', 'ccc'] as the result
> you can achieve that by sorting twice (Python's sorting is "stable") or by 
> tweaking the key function.
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query: lists

2018-08-14 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 14/08/18 09:11, Deepti K wrote:
>>  when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
>> function, it picks up only 'xzz' and not 'xaa'
> 
> Correct because
> 
>> def front_x(words):
>>   # +++your code here+++
>>   a = []
>>   b = []
>>   for z in words:
>> if z.startswith('x'):
>>   words.remove(z)
> 
> You just changed the thing you are iterating over.
> By removing an elem,ent the list got shorter so the
> internal counter inside the for loop now points at
> the next item - ie it skipped one.
> 
> As a general rule never modify the thing you are
> iterating over with a for loop - use a copy or
> change to a while loop instead.
> 
> 
>>   b.append(z)
>>   print 'z is', z
>>   print 'original', sorted(words)
> 
> But it's not the original because you've removed
> some items.
> 
>>   print 'new', sorted(b)
>>   print sorted(b) + sorted(words)
> 
> But this should be the same as the original
> (albeit almost sorted).
> 
> PS. Since you only modify 'b' and 'words' you
> don't really need 'a'

For a simple solution you do need a and b: leave words unchanged, append 
words starting with "x" to a and words not starting with "x" to b.

Someone familiar with Python might do it with a sort key instead:

>>> sorted(['bbb', 'ccc', 'axx', 'xzz', 'xaa'],
... key=lambda s: not s.startswith("x"))
['xzz', 'xaa', 'bbb', 'ccc', 'axx']

If you want ['xaa', 'xzz', 'axx', 'bbb', 'ccc'] as the result
you can achieve that by sorting twice (Python's sorting is "stable") or by 
tweaking the key function.

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


Re: [Tutor] Query: lists

2018-08-14 Thread Cameron Simpson

On 14Aug2018 18:11, Deepti K  wrote:

when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
function, it picks up only 'xzz' and not 'xaa'

def front_x(words):
 # +++your code here+++
 a = []
 b = []
 for z in words:
   if z.startswith('x'):
 words.remove(z)
 b.append(z)
 print 'z is', z
 print 'original', sorted(words)
 print 'new', sorted(b)
 print sorted(b) + sorted(words)


That is because you are making a common mistake which applies to almost any 
data structure, but is particularly easy with lists and loops: you are 
modifying the list _while_ iterating over it.


After you go:

 words.remove(z)

all the elements _after_ z (i.e. those after 'xzz' i.e. ['xaa']) are moved down 
the list.


In your particular case, that means that 'xaa' is now at index 3, and the next 
iteration of the loop would have picked up position 4. Therefore the loop 
doesn't get to see the value 'xaa'.


A "for" loop and almost anything that "iterates" over a data structure does not 
work by taking a copy of that structure ahead of time, and looping over the 
values. This is normal, because a data structure may be of any size - you do 
not want to "make a copy of all the values" by default - that can be 
arbitrarily expensive.


Instead, a for loop obtains an "iterator" of what you ask it to loop over. The 
iterator for a list effectively has a reference to the list (in order to obtain 
the values) and a notion of where in the list it is up to (i.e. a list index, a 
counter starting at 0 for the first element and incrementing until it exceeds 
the length of the list).


So when you run "for z in words", the iterator is up to index 3 when you reach 
"xzz". So z[3] == "xzz". After you remove "xzz", z[3] == "xaa" and in this case 
there is no longer a z[4] at all because the list is shortened. So the next 
loop iteration never inspects that value. Even if the list had more value, the 
loop would still skip the "xaa" value.


You should perhaps ask yourself: why am I removing values from "words"?

If you're just trying to obtain the values starting with "x" you do not need to 
modify words because you're already collecting the values you want in "b".


If you're trying to partition words into values starting with "x" and values 
not starting with "x", you're better off making a separate collection for the 
"not starting with x" values. And that has me wondering what the list "b" in 
your code was for originally.


As a matter of principle, functions that "compute a value" (in your case, a 
list of the values starting with "x") should try not to modify what they are 
given as parameters. When you pass values to Python functions, you are passing 
a reference, not a new copy. If a function modifies that reference's _content_, 
as you do when you go "words.move(z)", you're modifying the original.


Try running this code:

 my_words = ['bbb', 'ccc', 'axx', 'xzz', 'xaa']
 print 'words before =", my_words
 front_x(my_words)
 print 'words after =", my_words

You will find that "my_words" has been modified. This is called a "side 
effect", where calling a function affects something outside it. It is usually 
undesirable.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query: lists

2018-08-14 Thread Alan Gauld via Tutor
On 14/08/18 09:11, Deepti K wrote:
>  when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
> function, it picks up only 'xzz' and not 'xaa'

Correct because

> def front_x(words):
>   # +++your code here+++
>   a = []
>   b = []
>   for z in words:
> if z.startswith('x'):
>   words.remove(z)

You just changed the thing you are iterating over.
By removing an elem,ent the list got shorter so the
internal counter inside the for loop now points at
the next item - ie it skipped one.

As a general rule never modify the thing you are
iterating over with a for loop - use a copy or
change to a while loop instead.


>   b.append(z)
>   print 'z is', z
>   print 'original', sorted(words)

But it's not the original because you've removed
some items.

>   print 'new', sorted(b)
>   print sorted(b) + sorted(words)

But this should be the same as the original
(albeit almost sorted).

PS. Since you only modify 'b' and 'words' you
don't really need 'a'

-- 
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


Re: [Tutor] Query: How to fetch data to Python from InfluxDB database?

2018-04-23 Thread Mats Wichmann
On 04/23/2018 01:00 AM, Hemendra Singh Rathod wrote:
> Hello Tutor Members,
> 
> 
> I want to import the big Data’s from InfluxDB database. Could anyone please
> guide me on how can I do it in Python 3.x?
> 
> Please help me on this. Thank you in advance.

There is a project listed on PyPi which might help:

https://pypi.org/project/influxdb/


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


Re: [Tutor] Query regarding output

2017-07-02 Thread George Fischhof
2017-06-29 15:55 GMT+02:00 shubham goyal :

> Thanks all
> Great place to learn Python.
>
> On Jun 29, 2017 7:24 PM, "shubham goyal"  wrote:
>
> Thankyou all.
>
> Great place to learn.
>
> On Jun 29, 2017 5:55 PM, "Mats Wichmann"  wrote:
>
> > On 06/29/2017 03:02 AM, Alan Gauld via Tutor wrote:
> > > On 29/06/17 03:14, shubham goyal wrote:
> > >
> > >> This Question is asked in some exam. i am not able to figure it out.
> > >>
> > >> a = [0, 1, 2, 3]
> > >> for a[-1] in a:
> > >> print(a[-1])
> > >>
> > >> its giving output 0 1 2 2
> > >>
> > >> it should be 3 3 3 3 as a[-1] belongs to 3.
> > >> can anyone help me figuring it out.
> > >
> > > This is quite subtle and it took me a few minutes to figure
> > > it out myself.
> > >
> > > It might be clearer if we print all of 'a' instead
> > > of a[-1]:
> > >
> >  for a[-1] in a:
> > > ...print(a)
> > > ...
> > > [0, 1, 2, 0]
> > > [0, 1, 2, 1]
> > > [0, 1, 2, 2]
> > > [0, 1, 2, 2]
> > >
> > > What is happening is that a[-1] is being assigned the value
> > > of each item in a in turn. The final iteration assigns a[-1]
> > > to itself, thus we repeat the 2.
> >
> >
> > Ugh.  I guess on an exam where they're trying to see if you can tease it
> > out, as you may one day have to debug such a sequence... but don't write
> > code like that.  One of the reasons people like "functional programming"
> > is it avoids these kind of side effects.
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



Hi All,

just a comment ;-)
Actually I do not know why it is good to ask similar things on a Python
exam... Yes, I know that if one can answer these questions they show that
they understand it.  (Just a side question: Really understand? ;-) )

But a Pythonista never will write a code like this.

I think this behaviour is just to "show" to the student that this is very
strong school... :-(

I think it would be better to teach how to write Pythonic code...

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


Re: [Tutor] Query regarding output

2017-06-29 Thread shubham goyal
Thanks all
Great place to learn Python.

On Jun 29, 2017 7:24 PM, "shubham goyal"  wrote:

Thankyou all.

Great place to learn.

On Jun 29, 2017 5:55 PM, "Mats Wichmann"  wrote:

> On 06/29/2017 03:02 AM, Alan Gauld via Tutor wrote:
> > On 29/06/17 03:14, shubham goyal wrote:
> >
> >> This Question is asked in some exam. i am not able to figure it out.
> >>
> >> a = [0, 1, 2, 3]
> >> for a[-1] in a:
> >> print(a[-1])
> >>
> >> its giving output 0 1 2 2
> >>
> >> it should be 3 3 3 3 as a[-1] belongs to 3.
> >> can anyone help me figuring it out.
> >
> > This is quite subtle and it took me a few minutes to figure
> > it out myself.
> >
> > It might be clearer if we print all of 'a' instead
> > of a[-1]:
> >
>  for a[-1] in a:
> > ...print(a)
> > ...
> > [0, 1, 2, 0]
> > [0, 1, 2, 1]
> > [0, 1, 2, 2]
> > [0, 1, 2, 2]
> >
> > What is happening is that a[-1] is being assigned the value
> > of each item in a in turn. The final iteration assigns a[-1]
> > to itself, thus we repeat the 2.
>
>
> Ugh.  I guess on an exam where they're trying to see if you can tease it
> out, as you may one day have to debug such a sequence... but don't write
> code like that.  One of the reasons people like "functional programming"
> is it avoids these kind of side effects.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query regarding output

2017-06-29 Thread Mats Wichmann
On 06/29/2017 03:02 AM, Alan Gauld via Tutor wrote:
> On 29/06/17 03:14, shubham goyal wrote:
> 
>> This Question is asked in some exam. i am not able to figure it out.
>>
>> a = [0, 1, 2, 3]
>> for a[-1] in a:
>> print(a[-1])
>>
>> its giving output 0 1 2 2
>>
>> it should be 3 3 3 3 as a[-1] belongs to 3.
>> can anyone help me figuring it out.
> 
> This is quite subtle and it took me a few minutes to figure
> it out myself.
> 
> It might be clearer if we print all of 'a' instead
> of a[-1]:
> 
 for a[-1] in a:
> ...print(a)
> ...
> [0, 1, 2, 0]
> [0, 1, 2, 1]
> [0, 1, 2, 2]
> [0, 1, 2, 2]
> 
> What is happening is that a[-1] is being assigned the value
> of each item in a in turn. The final iteration assigns a[-1]
> to itself, thus we repeat the 2.


Ugh.  I guess on an exam where they're trying to see if you can tease it
out, as you may one day have to debug such a sequence... but don't write
code like that.  One of the reasons people like "functional programming"
is it avoids these kind of side effects.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query regarding output

2017-06-29 Thread anupama srinivas murthy
for a[x] in a:
   print(a)

Except for when x = 0, a[x] never takes on its original value throughout
the iterations

So for x = 0, we get:

[0, 1, 2, 3]
[1, 1, 2, 3]
[2, 1, 2, 3]
[3, 1, 2, 3]

For x = 1:
[0, 0, 2, 3]
[0, 0, 2, 3]
[0, 2, 2, 3]
[0, 3, 2, 3]

For x = 2:
[0, 1, 0, 3]
[0, 1, 1, 3]
[0, 1, 1, 3]
[0, 1, 3, 3]

How is the array being transformed?

On 29 June 2017 at 14:32, Alan Gauld via Tutor  wrote:

> On 29/06/17 03:14, shubham goyal wrote:
>
> > This Question is asked in some exam. i am not able to figure it out.
> >
> > a = [0, 1, 2, 3]
> > for a[-1] in a:
> > print(a[-1])
> >
> > its giving output 0 1 2 2
> >
> > it should be 3 3 3 3 as a[-1] belongs to 3.
> > can anyone help me figuring it out.
>
> This is quite subtle and it took me a few minutes to figure
> it out myself.
>
> It might be clearer if we print all of 'a' instead
> of a[-1]:
>
> >>> for a[-1] in a:
> ...print(a)
> ...
> [0, 1, 2, 0]
> [0, 1, 2, 1]
> [0, 1, 2, 2]
> [0, 1, 2, 2]
>
> What is happening is that a[-1] is being assigned the value
> of each item in a in turn. The final iteration assigns a[-1]
> to itself, thus we repeat the 2.
>
> Another way to see it is to convert the for loop to
> a while loop:
>
> for variable in collection:
> process(variable)
>
> becomes
>
> collection = [some sequence]
> index = 0
> while index < len(collection):
>  variable = collection[index]
>  process(variable)
>  index += 1
>
> Now substitute your values:
>
> collection = [0,1,2,3]
> index = 0
> while index < len(collection):
>  collection[-1] = collection[index]
>  print(collection[-1])
>  index += 1
>
> Does that help?
>
> --
> 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
>



-- 

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


Re: [Tutor] Query regarding output

2017-06-29 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 29/06/17 03:14, shubham goyal wrote:
> 
>> This Question is asked in some exam. i am not able to figure it out.
>> 
>> a = [0, 1, 2, 3]
>> for a[-1] in a:
>> print(a[-1])
>> 
>> its giving output 0 1 2 2
>> 
>> it should be 3 3 3 3 as a[-1] belongs to 3.
>> can anyone help me figuring it out.
> 
> This is quite subtle and it took me a few minutes to figure
> it out myself.
> 
> It might be clearer if we print all of 'a' instead
> of a[-1]:
> 
 for a[-1] in a:
> ...print(a)
> ...
> [0, 1, 2, 0]
> [0, 1, 2, 1]
> [0, 1, 2, 2]
> [0, 1, 2, 2]
> 
> What is happening is that a[-1] is being assigned the value
> of each item in a in turn. The final iteration assigns a[-1]
> to itself, thus we repeat the 2.
> 
> Another way to see it is to convert the for loop to
> a while loop:
> 
> for variable in collection:
> process(variable)
> 
> becomes
> 
> collection = [some sequence]
> index = 0
> while index < len(collection):
>  variable = collection[index]
>  process(variable)
>  index += 1
> 
> Now substitute your values:
> 
> collection = [0,1,2,3]
> index = 0
> while index < len(collection):
>  collection[-1] = collection[index]
>  print(collection[-1])
>  index += 1
> 
> Does that help?

The simplest rewrite is probably

>>> a = [0, 1, 2, 3]
>>> for tmp in a:
... a[-1] = tmp
... print(tmp)
... 
0
1
2
2

which should clarify how the list is modified while iterating over it.
The takeaway is that

for ... in [value]:
pass

is equivalent to

... = value

There are a few other implicit ways to assign a value, with varying 
generality:

>>> with open("tmp.txt") as a[0]: pass
... 

works, but

>>> import os as a[0]
  File "", line 1
import os as a[0]
  ^
SyntaxError: invalid syntax

and

>>> try: 1/0
... except Exception as a[0]: pass
  File "", line 2
except Exception as a[0]: pass
 ^
SyntaxError: invalid syntax

are forbidden by the language. 

The only case -- other than standard name binding -- I find useful is 
unpacking in a for loop:

>>> for hi, lo in ["Aa", "Zz"]:
...   print(hi, "-", lo)
... 
A - a
Z - z


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


Re: [Tutor] Query regarding output

2017-06-29 Thread Alan Gauld via Tutor
On 29/06/17 03:14, shubham goyal wrote:

> This Question is asked in some exam. i am not able to figure it out.
> 
> a = [0, 1, 2, 3]
> for a[-1] in a:
> print(a[-1])
> 
> its giving output 0 1 2 2
> 
> it should be 3 3 3 3 as a[-1] belongs to 3.
> can anyone help me figuring it out.

This is quite subtle and it took me a few minutes to figure
it out myself.

It might be clearer if we print all of 'a' instead
of a[-1]:

>>> for a[-1] in a:
...print(a)
...
[0, 1, 2, 0]
[0, 1, 2, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]

What is happening is that a[-1] is being assigned the value
of each item in a in turn. The final iteration assigns a[-1]
to itself, thus we repeat the 2.

Another way to see it is to convert the for loop to
a while loop:

for variable in collection:
process(variable)

becomes

collection = [some sequence]
index = 0
while index < len(collection):
 variable = collection[index]
 process(variable)
 index += 1

Now substitute your values:

collection = [0,1,2,3]
index = 0
while index < len(collection):
 collection[-1] = collection[index]
 print(collection[-1])
 index += 1

Does that help?

-- 
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


Re: [Tutor] Query regarding Regular Expression

2017-06-28 Thread Alan Gauld via Tutor
On 28/06/17 21:27, cookiestar227 - Cookie Productions wrote:

>  So far have understood everything except for the following example:
> 
  t = "A fat cat doesn't eat oat but a rat eats bats."
  mo = re.findall("[force]at", t)

> What I don't understand is the [force] part of the Regular Expression.  

A sequence of characters inside square brackets means match any one of
the characters. So [force]at matches:

fat, oat, rat, cat, eat

It does not ,atch bat because there is no b inside the brackets.

The fact that force spells a real word is misleading, it could just as
well be written

[ocfre]at

and it would do the same.

> I would prefer to use the following RE as it achieves my desired result:
> 
 mo = re.findall("[A-Za-z]at", t)
 print(mo)
> ['fat', 'cat', 'eat', 'oat', 'rat', 'eat',  'bat']
Fine, but it does a different job, as you discovered.

The problem with regex is that very minor changes in
pattern can have big differences in output. Or, as
you've shown a big difference in pattern can make
a very subtle difference in output.

That's what makes regex so powerful and so very difficult
to get right.

-- 
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


Re: [Tutor] Query

2017-06-13 Thread Alan Gauld via Tutor
On 13/06/17 10:09, Muddunuri Mahesh wrote:
> Where can i get the perfect tutorials for black scripting using python

I'm not sure what you mean by black scripting - and
neither does google apparently... Other than that
it is a gothic style of typescript font...

But the perfect tutorial for anything does not exist
so you are going to have to be more specific about
what you want.

Can you already program in any language?
Can you already program in Python?
What do you want to achieve?
What kind of teaching style do you prefer - theory or hands-on?
What kind of OS do you use?

-- 
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


Re: [Tutor] Query regarding loop problem

2015-08-31 Thread Laura Creighton
I have been told that in gmail it is simple, but far from obvious
to get your mail to open so you add to the bottom.   There are three
tiny dots which conceal  the previous text.  Click on that, and then
add below.  I am also told that if you open your mail message with
'control A' (hold the control key down and press A) this will also
work.

So we can see if that works ...

(Thanks to Rustom Mody, who as a gmail user ought to know such things. :) )

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


Re: [Tutor] Query regarding loop problem

2015-08-30 Thread Laura Creighton
Alan has pointed out the string/int problem with your code.
Your code has a different problem, as well.


># User enters a number between 1 - 5
># Computer generates random number until an equivalent number is achieved
>
>import random
>computerNumber = 0

Ok, now you have set computerNumber to 0

>computerNumber = input("Press enter to prompt the computer to enter a
>number: ")

As an aside -- what happens now it I type 0 ?  
as your code stands now, nothing, since computerNumber would
then be set to the string "0", which isn't the same as the
integer 0.  But once you get your types to match, this could
be a problem for you.

>while computerNumber != 0:

This is the line that is dodgy.   computerNumber isn't 0 to
start with (good, we will run this loop, as intended) but 
where does computerNumber get changed?

>if myNumber != computerNumber:
>computerNumber = random.randint(1,5)

Here.  Where it will become something in [1, 2, 3, 4, 5]
But none of these are 0s.  So your while loop will never terminate.

You continue 

>prompt = input("Press enter to prompt the computer to enter a number: 
> ")

You don't use this anywhere, so this is just so you can single step
through your loop, I guess.  If that is the idea, then it is better
to write it like this:  (I shortened it to fit on one line):

prompt = input("Press enter to prompt the computer: ")
computerNumber = random.randint(1,5)

Hit return, and get the assignment.  Not 'hit return and the next
time the loop is executed, if this happens, then sometime a
new number will be assiged'.

But back to the larger issue.

How are you going to get out of your while loop?

There are 3 different approaches you can use here.

The first way is to assign computerNumber to 0 when you have
finally found your match.  The next time around the loop, the
while condition will fail, and the loop will end.

The second way is to explicitly break out of your loop when
you have found what you are looking for with a break statement.

The second way suggests a way to improve your code.
Instead of messing around with assigning values that can never
be True,  which puts a conceptual load on the person reading your
code to trace every possible way that you could get computerNumber
to become 0 -- which, admittedly isn't a huge load in such a
tiny program.

If instead you write this as:

while True:
  if :
 do stuff
  else:
 announce_victory
 break

People who see 'while True' at the top know, for certain, that you
don't want this loop to ever terminate.  Thus you are relying on
break statements to leave, rather than 'I want to leave via the
break statement or if computerNumber becomes 0'.  Much easier to
read and understand.

The final way to get out of the loop is to replace the condition,
not with the somewhat confusing 'something that is never going
to happen normally', not with 'somethting that is very clear about
that it is never going to happen, ever' but with a test that you
are really interested in.  In this case, it is

while myNumber != computerNumber:

Hope this helps,
Laura


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


Re: [Tutor] Query regarding loop problem

2015-08-29 Thread Cameron Simpson

On 29Aug2015 22:37, Martin Mwaka  wrote:

I would be grateful for some help please.  I have recently started learning
python and I am attemping to write a programme where a user enters a number
between 1 and 5, and the computer generates random numbers until an
equivalent number is achieved.  The code (see below) runs, but the else
part of the loop does not run when the computer generates an equivalent
number. I have tried to resolve this a number of ways and have run the code
under a debugger, but cannot work out why the else part is not running.  I
would be grateful for your help / guidance with this.

[...]

import random
computerNumber = 0
myNumber = input("Input a number between 1 and 5: ")
print ("Your chosen number is: ", myNumber)
computerNumber = input("Press enter to prompt the computer to enter a
number: ")

while computerNumber != 0:
   if myNumber != computerNumber:
   computerNumber = random.randint(1,5)
   print ("Your chosen number is ", myNumber,": Computer number is: ",
computerNumber)
   print ("Numbers do not match.")
   prompt = input("Press enter to prompt the computer to enter a
number: ")
   else:
   print ("MyNumber is ", str(myNumber),": Computer number is: ",
str(computerNumber))
   print ("We have a match.")


Looks like you assign the new number to "prompt" instead of to "myNumber".

Cheers,
Cameron Simpson 

In theory, there is no difference between theory and practice.
In practice, there is. - Yogi Berra
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query regarding loop problem

2015-08-29 Thread Alan Gauld

On 29/08/15 22:37, Martin Mwaka wrote:


myNumber = input("Input a number between 1 and 5: ")


myNumber is now a *string* representing a number from 1-5.


while computerNumber != 0:
 if myNumber != computerNumber:
 computerNumber = random.randint(1,5)


computerNumber is now a random *integer* between 1-5

A string can never equal an integer. "5" is not the same as 5
Types are important in programming.


 print ("Your chosen number is ", myNumber,": Computer number is: ",
computerNumber)
 else:


So the else will never get called

You need to convert your string into an integer using int()

myNumber = int(input("Input a number between 1 and 5: "))


HTH
--
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


Re: [Tutor] query

2015-05-01 Thread Peter Otten
Vishakh Rameshan wrote:

> i have downloaded and installed python 3.4.3
> and when i just type print "with message to display" it shows missing
> paranthesis error
> but what i have learnt is that inorder to display message onle print
> command and message in "msg"

In Python 2 you would write

print "hello"

but in Python 3 print is no longer a statement, it has become a function. As 
with every other function parentheses are required to invoke it:

print("hello")

There are other subtle changes in Python 3, so you might want to read the 
tutorial at  or any other 
introductory text specifically targeting Python 3.

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


Re: [Tutor] query from sqlalchemy returns AttributeError: 'NoneType' object

2013-05-03 Thread Alan Gauld

On 02/05/13 23:13, Karthik Sharma wrote:

This doesn't have much to do with learning Python and a lot to do with 
SqlAlchemy so you'd be better off asking on a SqlAlchemy forum I suspect.


However, some basic debugging investigation first may help your cause.
For example...


 Traceback (most recent call last):

...

   File "/home/karthik/pox/tutorial.py", line 118, in _handle_PacketIn
 self.act_like_switch(packet, packet_in)
   File "/home/karthik/pox/tutorial.py", line 86, in act_like_switch
 self.send_packet(packet_in.buffer_id,
packet_in.data,q_res.port_no, packet_in.in_port)
 AttributeError: 'NoneType' object has no attribute 'port_no'


Have you checked to see what q_res is supposed to be?
And where it comes from? And why it's apparently a NoneType?

Some print statements might be in order?

That will help anyone who can help you to so do.

--
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] query from sqlalchemy returns AttributeError: 'NoneType' object :p:

2013-05-02 Thread Paradox


On 05/03/2013 06:13 AM, Karthik Sharma wrote:


from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String

This list is mostly for standard library questions (though there may be 
some that can help here too).  Did you know there is an sqlalchemy 
google group (


This list is really for standard library questions (though there may be some 
that can help here too).  Did you know there is an sqlalchemy google group?  
The site is here: http://groups.google.com/group/sqlalchemy?hl=en.  Michael 
Bayer frequently answers questions there too, though there are lots of 
knowledgeable people on that list that can handle simpler problems.

thomas

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


Re: [Tutor] Query String

2013-02-19 Thread Alan Gauld

On 19/02/13 11:35, eryksun wrote:

On Tue, Feb 19, 2013 at 4:14 AM, Alan Gauld  wrote:


https://www.google.co.uk/#hl=en


Just to clarify, using a fragment (#) like that isn't a standard
query.


Yes I noticed the anomaly but I chose that particular query for a reason :-)


--
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] Query String

2013-02-19 Thread eryksun
On Tue, Feb 19, 2013 at 4:14 AM, Alan Gauld  wrote:
>
> https://www.google.co.uk/#hl=en

Just to clarify, using a fragment (#) like that isn't a standard
query. It's a JavaScript trick for manipulating browser history based
on location.hash, so one can do AJAX page refreshes without breaking
the back button. You can see the actual query URL ('.../search?hl=en')
if you view the background GET (e.g. in the Firefox web console).

urlparse.urlsplit parses 'hl=en' as the fragment in this case:

>>> urlparse.urlsplit('https://www.google.co.uk/#hl=en')
SplitResult(scheme='https', netloc='www.google.co.uk', path='/', query='',
fragment='hl=en')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query String

2013-02-19 Thread Sunil Tech
Thank you Alan.





On Tue, Feb 19, 2013 at 2:44 PM, Alan Gauld wrote:

> On 19/02/13 07:30, Sunil Tech wrote:
>
>>
>> Here i request, can you tell me what is Query String with some examples?
>>
>
> It depends on the context but I suspect you mean in the context of a web
> app? In that case a query string is the bit at the end of a URL that
> includes the search parameters etc. Thus if I do a search on Google for
> "query string" the url used is:
>
> https://www.google.co.uk/#hl=**en&sugexp=les%3B&gs_rn=3&gs_**ri=psy-
> ab&tok=XeR8I7yoZ93k_zpsfFWQeg&**cp=8&gs_id=i7&xhr=t&q=query+**string&
> es_nrs=true&pf=p&tbo=d&biw=**1375&bih=897&sclient=psy-ab&**oq=query+st&
> gs_l=&pbx=1&bav=on.2,or.r_gc.**r_pw.r_cp.r_qf.&bvm=bv.**42553238,d.d2k&
> fp=e58955d3a8f3f3a1
>
> And everything after the uk/ is the query string.
>
> That particular search brings back lots of links about query strings.
>
> --
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query String

2013-02-19 Thread Alan Gauld

On 19/02/13 07:30, Sunil Tech wrote:


Here i request, can you tell me what is Query String with some examples?


It depends on the context but I suspect you mean in the context of a web 
app? In that case a query string is the bit at the end of a URL that 
includes the search parameters etc. Thus if I do a search on Google for 
"query string" the url used is:


https://www.google.co.uk/#hl=en&sugexp=les%3B&gs_rn=3&gs_ri=psy-
ab&tok=XeR8I7yoZ93k_zpsfFWQeg&cp=8&gs_id=i7&xhr=t&q=query+string&
es_nrs=true&pf=p&tbo=d&biw=1375&bih=897&sclient=psy-ab&oq=query+st&
gs_l=&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&bvm=bv.42553238,d.d2k&
fp=e58955d3a8f3f3a1

And everything after the uk/ is the query string.

That particular search brings back lots of links about query strings.

--
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] Query - Where to put in global variables, if needed, as a good programming practice

2012-06-15 Thread Steven D'Aprano

spa...@gmail.com wrote:

Hello,

The point of good-bad-ness of global variables aside, if I needed to use
them, which is a better place to put them.
1. In the __init__ function of a class? So they are available at the time
an object is initialized or
2. In the actual function of the class where the variables are needed?
Pros and Cons of either approach?


Neither of those are *global* variables.

In Python, global variables are those at the top level of the module, and are 
only global to a single module, not your entire program. If your program is a 
single module, there is no difference.


This confusion is one of the reasons that I hate the Java-ism of calling 
things-attached-to-classes-or-attributes as "variables" instead of members or 
attributes. In Python, the usual term for them is "attributes", and you can 
have class attributes shared between all instances of a class, and instance 
attributes that are specific to the instance.



class K:
shared = "this attribute is shared"

def __init__(self):
self.attribute = "this one is specific to the instance"


Pros for class attributes:

+ they are shared, so all your instances see the same value

+ you can reach them directly from the class, without creating an
  instance first: K.shared works

Cons for class attributes:

- they are shared, so all your instances see the same value


Pros for instance attributes:

+ they are not shared, so all your instances don't see the same value

Cons for class attributes:

- they are not shared, so all your instances don't see the same value

- every time you create an instance, the attribute has to be created

- you can't reach them directly from the class without creating an
  instance first: K.attribute does not work





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


Re: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice

2012-06-15 Thread Walter Prins
Hi Spawgi,

On 15 June 2012 12:44,   wrote:
> Hello,
>
> The point of good-bad-ness of global variables aside, if I needed to use
> them, which is a better place to put them.
> 1. In the __init__ function of a class? So they are available at the time an
> object is initialized or

Firstly note as a bit of an aside that member variables of an object
(or perhaps of the class), are not considered global, at least not in
the sense most people mean when they talk about "global" variables.

I'm however assuming you're NOT referring to a variable that's
actually either a class or object member variable in your question,
but rather something akin to a true global.

I say "akin", because Python does not really have true global
variables in the sense that one might expect coming from some other
langauges.  The closest is probably the contents of the '__builtin__'
module, which is the last searched when resolving names and hence
practically speaking probably the closest to containing what one might
describe as global variables, but I'd hazard to submit there will
virtually without exception always be a better location for your
variables than to plug them into the __builtin__ module.

> 2. In the actual function of the class where the variables are needed?
> Pros and Cons of either approach?

OK, to state the obvious again, but if a variable is only needed in
one method (function) of a class, then by definition you don't
actually need a global.  If it's needed in more than one method in an
object, then you should probably consider an object member variable or
suitably parameterizethe methods.

That aside, to answer your question in a general fashion,
initialisation should always happen in the initialization section (as
appropriate) of the namespace that a variable is being
declared/contained in.  So package level varibles should be
initialized in __init__.py of the package, module variables should be
initialized in the module itself, prior to any other code that may
refer to them obviously, and presumably towards the top of the module
in most cases, class level variables should be initialized directly in
the class prior to use, object level variables in the object
initializer (__init__ method) and so on.

So, your (presumably module) global variables should therefore *not*
be initialized as part of the object initializer (what happens when
you create multiple instances of your object?), object methods (what
happens when you call the method multiple times?) or even plain
functions (again, what happens when you call the method multiple
times?) as you suggested, and assuming you're talking about module
globals, they should in fact simply be initialized in the module,
prior to use elsewhere in the module.  (Use from other modules are
possible by importing the module containing the module global variable
from such other modules etc.)

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


Re: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice

2012-06-15 Thread Mark Lawrence

On 15/06/2012 12:44, spa...@gmail.com wrote:

Hello,

The point of good-bad-ness of global variables aside, if I needed to use
them, which is a better place to put them.
1. In the __init__ function of a class? So they are available at the time
an object is initialized or
2. In the actual function of the class where the variables are needed?
Pros and Cons of either approach?

Thanks and Regards,
Sumod




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


I don't understand why you would need global variables, and then 
promptly start discussing them wrt classes.  Please explain what you are 
trying to achieve and I'm certain that we'll come up with the best 
solution for your use case.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Query with SOAP request generation, which other modules can be used.

2012-05-23 Thread Mark Lawrence

On 23/05/2012 08:17, Ataulla S H wrote:

We can try suds its very lightweight soap client.

Thanks
Ataulla SH

On Wed, May 23, 2012 at 9:43 AM, ankur ~ अंकुर  wrote:


Dear Pythoneers,

We want to butile the SOAP request request in below manner. -

In header we want to pass the wsse auth part and custom transref section
and both has different xmlns.


http://schemas.xmlsoap.org/soap/envelope/
"
xmlns:com="http://some.xmlns.org/.1.0/Common.xsd"; xmlns:bil="
http://some.xmlns.org/Schema/Billing/1.0/Billing.xsd";>

   

  
 PORTAL
 123456
 123456
  

http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
">
 http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd
">
user
pass
 
  

   

   
  
 187000
  
   




Currently we are using pysimplesoap (
http://code.google.com/p/pysimplesoap/) module for this. The current
python code is attached.

But we are not able to pass the custom xmlns ( for bil and for com - the
very first line )  with pysimplesoap module.

*Any idea which other module we have to explore for generating the SOAP
request like above.*

Thank You,
Ankur.

___
BangPypers mailing list
bangpyp...@python.org
http://mail.python.org/mailman/listinfo/bangpypers



___
BangPypers mailing list
bangpyp...@python.org
http://mail.python.org/mailman/listinfo/bangpypers


What is the unladen airspeed velocity of a swallow in flight?

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] query result set

2010-11-09 Thread bob gailer
When starting a new subject, please start with a new email. Do not 
"hijack" an existing one, as that causes your query to appear as part of 
the hijacked thread.



--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] query result set

2010-11-08 Thread Steven D'Aprano
On Mon, Nov 08, 2010 at 05:58:36PM -0800, Shawn Matlock wrote:
> I am able to successfully query a MySQL database using zxJDBC but the result 
> set seems odd to me. It returns the following:
> 
> [(u'envDB', u'systest2'), (u'envDir', u'st2'), (u'envCellName', 
> u'Systest2Cell'), (u'envFrontEnd', u'systest2FrontEnd'), (u'envTag', 
> u'system_test2')]
> 
> Why is there a "u" before every entry? Can someone point me to an example 
> that puts the results into a List (or Dictionary) without the "u"?


No, you have misunderstood what you are asking for. That is like asking
for lists without [] or dicts without {}.

In Python 2.x, there are two types of strings: byte strings, which 
have delimiters " ", and unicode (text) strings, which have delimiters
u" and ". Notice that the u is not part of the string contents, but is
part of the delimiter, just like the " in byte strings, or [ and ] for 
lists.

Coming from a database, the strings are Unicode, which means that they could
contain characters that can't be stored in byte-strings. So you have to use
Unicode, which means the object repr() looks like:

u"text inside the quotation marks"

But if you print them, you get:

text inside the quotation marks

*without* the quotation marks included. The slight complication is that 
if you put the Unicode string inside a list, dict or tuple, Python prints
the repr() which means you see the u" ". If you don't like it, write a 
helper function that prints the list the way you want, or try the pprint 
module.

-- 
Steven














> 
> Thank you,
> Shawn
> 
> 
> 
> csdbConn = zxJDBC.connect("jdbc:mysql://myhost:3306/mydb", "User", 
> "Password", "com.mysql.jdbc.Driver")
> 
> csdbCursor = csdbConn.cursor(1)
> 
> envsql = "select envVariable, envValue from ODS_ENV_DICT where envName = 
> 'ST2'"
> 
> csdbCursor.execute(envsql)
> 
> print csdbCursor.fetchall()
> 
> csdbCursor.close()
> csdbConn.close()
> 
> ___
> 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


Re: [Tutor] query

2010-02-01 Thread Grigor Kolev
В 22:09 + на 31.01.2010 (нд), invincible patriot написа:
> i just don wana index all the characters rather i wana double it too
> like 
> ['d','a','v','i','d']
> would b 
> ['d','dd','a','aa','v','vv','i','ii','d','dd']
> and then i wana replace all non 'd' characters with '.' a dot
> 
> i know how replace a specific character, but i don know how to replace
> all characters other than a specific character
> 
> 
> thanks
> 
> 
> 
> > Subject: Re: [Tutor] query
> > From: grigor.ko...@gmail.com
> > To: invincible_patr...@hotmail.com
> > CC: anand.shash...@gmail.com; da...@pythontoo.com; tutor@python.org
> > Date: Sun, 31 Jan 2010 23:49:58 +0200
> > 
> > В 21:21 + на 31.01.2010 (нд), invincible patriot написа:
> > > Hi
> > > can any one tel me how can i do indexing of individual characters
> in
> > > python
> > > like if i hav a word eg david
> > > a='david'
> > > b=list(a)
> > > # this will give ['d','a','v','i','d']
> > > not i want to print the index of each character
> > > how can i do that
> > > please tel me
> > > 
> > > thanks
> > > 
> > > 
> > > 
> > >
> __
> > > Hotmail: Free, trusted and rich email service. Get it now.
> > > ___
> > > Tutor maillist - Tutor@python.org
> > > To unsubscribe or change subscription options:
> > > http://mail.python.org/mailman/listinfo/tutor
> > List indexed by position
> > ['d','a','v','i','d']
> > 0,1,2,3,4,5
> > Can index like this
> > a[1]
> > You can index and string too not need to be a list
> > a[1]
> > if you want find position of 'a' use this
> > a.find('a')
> > -- 
> > Grigor Kolev 
> > 
> 
> 
> __
> Hotmail: Free, trusted and rich email service. Get it now.
Read this 
http://docs.python.org/tutorial/controlflow.html#for-statements
-- 
Grigor Kolev 

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


Re: [Tutor] query

2010-01-31 Thread R. Alan Monroe

> i just don wana index all the characters rather i wana double it too like 
>  ['d','a','v','i','d']
> would b 
>  ['d','dd','a','aa','v','vv','i','ii','d','dd']
> and then i wana replace all non 'd' characters with '.' a dot

> i know how replace a specific character, but i don know how to
> replace all characters other than a specific character

Hint 1: a FOR loop will help you count your way through lists one
at a time. Let the computer do the counting for you.

Hint 2: learn about IF statements to replace characters IF they're not
a d, for example.

Alan

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


Re: [Tutor] query

2010-01-31 Thread invincible patriot

i just don wana index all the characters rather i wana double it too like 
 ['d','a','v','i','d']
would b 
 ['d','dd','a','aa','v','vv','i','ii','d','dd']
and then i wana replace all non 'd' characters with '.' a dot

i know how replace a specific character, but i don know how to replace all 
characters other than a specific character


thanks



> Subject: Re: [Tutor] query
> From: grigor.ko...@gmail.com
> To: invincible_patr...@hotmail.com
> CC: anand.shash...@gmail.com; da...@pythontoo.com; tutor@python.org
> Date: Sun, 31 Jan 2010 23:49:58 +0200
> 
> В 21:21 + на 31.01.2010 (нд), invincible patriot написа:
> > Hi
> > can any one tel me how can i do indexing of individual characters in
> > python
> > like if i hav a word eg david
> > a='david'
> > b=list(a)
> > # this will give ['d','a','v','i','d']
> > not i want to print the index of each character
> > how can i do that
> > please tel me
> > 
> > thanks
> > 
> > 
> > 
> > __
> > Hotmail: Free, trusted and rich email service. Get it now.
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> List indexed by position
> ['d','a','v','i','d']
> 0,1,2,3,4,5
> Can index like this
> a[1]
> You can index and string too not need to be a list
> a[1]
> if you want find position of 'a' use this
> a.find('a')
> -- 
> Grigor Kolev 
> 
  
_
Hotmail: Free, trusted and rich email service.
http://clk.atdmt.com/GBL/go/196390708/direct/01/___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] query

2010-01-31 Thread Grigor Kolev
В 21:21 + на 31.01.2010 (нд), invincible patriot написа:
> Hi
> can any one tel me how can i do indexing of individual characters in
> python
> like if i hav a word eg david
> a='david'
> b=list(a)
> # this will give ['d','a','v','i','d']
> not i want to print the index of each character
> how can i do that
> please tel me
> 
> thanks
> 
> 
> 
> __
> Hotmail: Free, trusted and rich email service. Get it now.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
List indexed by position
['d','a','v','i','d']
0,1,2,3,4,5
Can index like this
a[1]
You can index and string too not need to be a list
a[1]
if you want find position of 'a' use this
a.find('a')
-- 
Grigor Kolev 

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


Re: [Tutor] query

2010-01-31 Thread Sander Sweers
On 31 January 2010 22:21, invincible patriot
 wrote:
> can any one tel me how can i do indexing of individual characters in python
> like if i hav a word eg david
> a='david'
> b=list(a)
> # this will give ['d','a','v','i','d']
> not i want to print the index of each character
> how can i do that
> please tel me

Look at http://diveintopython.org/getting_to_know_python/lists.html.

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


Re: [Tutor] Query about getattr used as a dispatcher

2007-01-17 Thread Kent Johnson
raghu raghu wrote:
> i am following 'dive into python' for learning.  i dont know whether i am 
> following the right book. as i 
> am a beginner is it right  to follow this book?or is there any other 
> book which is best for beginners?

I don't think Dive Into Python is a great book for beginners. In my 
opinion the book is too focused on advanced features.

You might want to try one of these tutorials:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

or one of these books:
http://effbot.org/pyfaq/tutor-what-are-some-good-books-on-python.htm

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about getattr used as a dispatcher

2007-01-17 Thread Chris Calloway
raghu raghu wrote:
> Actually i installed python 2.5 i ran this script and its showing error 
> it could not import statsout. why is it so?

statsout is a *hypothetical* module used for an example only. The 
statsout module does not actually exist. When Dive Into Python wants you 
to type in an example, it will shown either as lines at the Python 
interpreter prompt (>>>), or it will be an example in a Python source 
file included with the Dive Into Python examples bundle. If you look at 
the example using the imaginary statsout module in section 4.12, which 
is not included in any Python source file in the Dive Into Python 
examples bundle, you will see the example is not referenced in a file, 
nor is it shown as being typed at the Python interpreter prompt.

If, however, you had an actual statsout module in your sys.path, you 
could import it. And it that module had top level functions functions 
that took one argument and had function names like "output_text" and 
"output_pdf" and "output_html," then the example would work if you typed 
it in. The example is just showing a hypothetical case of a very simple 
dispatcher. The example is asking you to imagine *if* you had a statsout 
module, and *if* that module had functions by those names in it.

Dive Into Python uses the getattr function in the apihelper.py example 
you are currently reading about. In the next chapter, a more complicated 
example is shown where getattr is used in a dispatcher which finds, not 
just a function by name, but a class by name and dispatches that class 
object to create a new object of that class. So the statsout *imaginary* 
example is just preparing you for a more complicated *real life* 
dispatcher example in the next chapter. Hypothetical examples are often 
shown in programming books to prepare you for more complicated real life 
examples.

-- 
Sincerely,

Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall   phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-07 Thread Alan Gauld

"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote

> The dialog box, what I think is better than a 'visual warning' ; 
> because it
> forces the user to correct the input error that he has made. A 
> visual
> warning might be omitted by the user.

I disagree. A dialog box in this situation has two bad effects:

1) It forces the users attention away from where the error occurs
(ie in the entry box) and in an extreme case could even hide the
error by popping up on top of the box beintg edited!

2) It is not specific enough - the dialog could be from another
application in the background or about some other error
- a network lost for example. You have to read it to interpret
it. Whereas if the entry box you are typing into beeps and
goes red, say, it is obvious where the error is and it's very
easy to fix.

As to forcing the user to correct the error a doalog is no
better than a coloured entry widget since the user can just
OK it and carry on filling I the other fields. In that situation
you have to do a submit time check on the fields too or else
refuse to allow access to any other field while the error
persists.

Designing a good UI is frought with difficulty.

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-07 Thread Asrarahmed Kadri

Hey Luke,
The dialog box, what I think is better than a 'visual warning' ; because it
forces the user to correct the input error that he has made. A visual
warning might be omitted by the user.

REgards,
Asrarahmed

On 1/7/07, Luke Paireepinart <[EMAIL PROTECTED]> wrote:


Alan Gauld wrote:
> "Luke Paireepinart" <[EMAIL PROTECTED]> wrote
>
> OP>> Can some one help me how to add validation : only integers are
> allowed
> OP>> and minimum value should be 1.
> OP>>
> OP>> timeInterval = Pmw.EntryField(label_text='Time Interval(in
> OP>> sec.):',labelpos='w',validate = 'numeric')
>
> I don;t have PMW installed. I tend to use Tix now that its part
> of standard Python. However Grayson says this:
>
> "validation is [performed by a function which takes
> as its first argument the entered text and returns
> one of three standard values...
> Pmw.OK, Pmw.ERROR, Pmw.PARTIAL"
>
> So it seems you need
>
> def numeric(val):
> try: float(val)
> except ValueError: return Pmw.ERROR
> else: return Pmw.OK
>
> timeinterval = Pmw.EntryField(.validate=numeric)
>
> At least that's how I read it, obviously without Pmw I can't try it...
>
>
>>> Also I want to validate date and time? I know that there is a way
>>> to
>>> do it but I dont know the exact syntax..
>>>
>
> I'd use a similar technique as above. parse the string and try
> to create a datetime object then handle the exceptions.
>
> Luke> it's a lot easier to validate the input when you're using it
> instead of
> Luke> trying to restrict their input.
>
> Its easier for the programmer but much worse for the user.
> We should always catch erroneous input as early as possible.
> Early CGI Web pages were the classic example of late error
> handling and there is nothing more annoying than filling in a form,
> submitting it and then being told you filled in a field wrong right
> at the start!
>

The difference between a webpage and an application here is that if the
user enters something incorrectly,
he'll know as soon as he tries to submit it,
without having to wait for the page to reload,
and he won't lose all of his information just to go back and change one
field.
But I guess I don't know anything about UI design.
That's why I just purchased 'The Essential Guide To User Interface
Design' from Half-Price Books!
Yes, a bit of a strange coincidence that I bought it just today.

Anyway,
I know it's frustrating to have to go back to fix entries,
but it's also annoying to have something restrict your input.
That seems sort of like the programmer didn't want to take the time to
have a robust user input parser :D
I think the ideal situation for me would be that whenever an entry
widget loses focus, the validate() function is called,
and I get some kind of visual warning that my entry is incorrect.
NOT a dialog box, I hope, but some kind of red warning label next to the
entry widget or something.
However, this doesn't work for the last entry on the page, because most
likely focus will switch straight from the entry to the submit button,
but then the whole 'check on submit' thing would catch the error.
Basically I don't want to see an entry box that behaves differently than
a normal entry box (only accepts integers, for example).
-Luke

> HTH,
>
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor





--
To HIM you shall return.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-07 Thread John Fouhy
On 07/01/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> I don;t have PMW installed. I tend to use Tix now that its part
> of standard Python. However Grayson says this:

Does Tix work with py2exe?  That's why I gave up on it, a while ago..

> So it seems you need
>
> def numeric(val):
> try: float(val)
> except ValueError: return Pmw.ERROR
> else: return Pmw.OK
>
> timeinterval = Pmw.EntryField(.validate=numeric)

>From memory, you have to do something like
"validate={'validator':numeric}" if you're not using one of the
built-in validators.

(which Asrarahmed would need to do, since he wants to restrict the
range of numbers available)

Also, this will not work quite right; you need something like:

def numeric(val):
try:
float(val)
except ValueError:
if val == '':
return Pmw.PARTIAL
return Pmw.ERROR
return Pmw.OK

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-07 Thread Alan Gauld
"Luke Paireepinart" <[EMAIL PROTECTED]> wrote

>> Its easier for the programmer but much worse for the user.
>> We should always catch erroneous input as early as possible.
>
> The difference between a webpage and an application here is that if 
> the
> user enters something incorrectly, he'll know as soon as he tries to
> submit it, without having to wait for the page to reload,

Only a few seconds difference, he's still done all the work
of filling in 10 or more fields and now has to back to look
for the error.

> That's why I just purchased 'The Essential Guide To User Interface
> Design' from Half-Price Books!

I haven't seen that one, but I do like Abbout Face by Cooper - the
guy who wrote the first version of Visual Basic before MS bought it.

> but it's also annoying to have something restrict your input.

But if the input is wronmg better to get it right first time
rather than have the same error flagged to you after you've
stopped thinking about that bit of data.

> and I get some kind of visual warning that my entry is incorrect.
> NOT a dialog box, I hope, but some kind of red warning label next to 
> the
> entry widget or something.

I agree, dialogs are a pain for this.
But as I understand it (without having used it!) PMW actually
does give a visual indication by changing the colour of the
field to indicate an error rather than a dialog..

Also the validator is called on each keystroke *as well as*
when it loses focus.

> Basically I don't want to see an entry box that behaves differently 
> than
> a normal entry box (only accepts integers, for example).

But there are quite a few like that in Windows. Several of the
config tools regulate input. Think if the entry fields for inputting
an IP address.They autommatically jump to the next box
after 3 digits, and they won't let you enter a number above 255.

Similarly password entries are diffeerent to normal because
they show up asstars or blobs or sometimes as nothing at
all (I don't like those even though they are more secure!)


Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-07 Thread Luke Paireepinart
Alan Gauld wrote:
> "Luke Paireepinart" <[EMAIL PROTECTED]> wrote
>
> OP>> Can some one help me how to add validation : only integers are 
> allowed
> OP>> and minimum value should be 1.
> OP>>
> OP>> timeInterval = Pmw.EntryField(label_text='Time Interval(in
> OP>> sec.):',labelpos='w',validate = 'numeric')
>
> I don;t have PMW installed. I tend to use Tix now that its part
> of standard Python. However Grayson says this:
>
> "validation is [performed by a function which takes
> as its first argument the entered text and returns
> one of three standard values...
> Pmw.OK, Pmw.ERROR, Pmw.PARTIAL"
>
> So it seems you need
>
> def numeric(val):
> try: float(val)
> except ValueError: return Pmw.ERROR
> else: return Pmw.OK
>
> timeinterval = Pmw.EntryField(.validate=numeric)
>
> At least that's how I read it, obviously without Pmw I can't try it...
>
>   
>>> Also I want to validate date and time? I know that there is a way 
>>> to
>>> do it but I dont know the exact syntax..
>>>   
>
> I'd use a similar technique as above. parse the string and try
> to create a datetime object then handle the exceptions.
>
> Luke> it's a lot easier to validate the input when you're using it 
> instead of
> Luke> trying to restrict their input.
>
> Its easier for the programmer but much worse for the user.
> We should always catch erroneous input as early as possible.
> Early CGI Web pages were the classic example of late error
> handling and there is nothing more annoying than filling in a form,
> submitting it and then being told you filled in a field wrong right
> at the start!
>   

The difference between a webpage and an application here is that if the 
user enters something incorrectly,
he'll know as soon as he tries to submit it,
without having to wait for the page to reload,
and he won't lose all of his information just to go back and change one 
field.
But I guess I don't know anything about UI design.
That's why I just purchased 'The Essential Guide To User Interface 
Design' from Half-Price Books!
Yes, a bit of a strange coincidence that I bought it just today.

Anyway,
I know it's frustrating to have to go back to fix entries,
but it's also annoying to have something restrict your input.
That seems sort of like the programmer didn't want to take the time to 
have a robust user input parser :D
I think the ideal situation for me would be that whenever an entry 
widget loses focus, the validate() function is called,
and I get some kind of visual warning that my entry is incorrect.
NOT a dialog box, I hope, but some kind of red warning label next to the 
entry widget or something.
However, this doesn't work for the last entry on the page, because most 
likely focus will switch straight from the entry to the submit button,
but then the whole 'check on submit' thing would catch the error.
Basically I don't want to see an entry box that behaves differently than 
a normal entry box (only accepts integers, for example).
-Luke

> HTH,
>
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-07 Thread Alan Gauld

"Luke Paireepinart" <[EMAIL PROTECTED]> wrote

OP>> Can some one help me how to add validation : only integers are 
allowed
OP>> and minimum value should be 1.
OP>>
OP>> timeInterval = Pmw.EntryField(label_text='Time Interval(in
OP>> sec.):',labelpos='w',validate = 'numeric')

I don;t have PMW installed. I tend to use Tix now that its part
of standard Python. However Grayson says this:

"validation is [performed by a function which takes
as its first argument the entered text and returns
one of three standard values...
Pmw.OK, Pmw.ERROR, Pmw.PARTIAL"

So it seems you need

def numeric(val):
try: float(val)
except ValueError: return Pmw.ERROR
else: return Pmw.OK

timeinterval = Pmw.EntryField(.validate=numeric)

At least that's how I read it, obviously without Pmw I can't try it...

>> Also I want to validate date and time? I know that there is a way 
>> to
>> do it but I dont know the exact syntax..

I'd use a similar technique as above. parse the string and try
to create a datetime object then handle the exceptions.

Luke> it's a lot easier to validate the input when you're using it 
instead of
Luke> trying to restrict their input.

Its easier for the programmer but much worse for the user.
We should always catch erroneous input as early as possible.
Early CGI Web pages were the classic example of late error
handling and there is nothing more annoying than filling in a form,
submitting it and then being told you filled in a field wrong right
at the start!

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query about using Pmw entry widget...

2007-01-06 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
> Hello Folks,
>  
> I am using Pmw mdoule to develop a User interface and in that I am 
> using entry widget.
>  
> Can some one help me how to add validation : only integers are allowed 
> and minimum value should be 1.
>  
> timeInterval = Pmw.EntryField(label_text='Time Interval(in 
> sec.):',labelpos='w',validate = 'numeric')
>  
> Also I want to validate date and time? I know that there is a way to 
> do it but I dont know the exact syntax..
Asrarahmed -
 From what I remember (PMW is just an extension of TKinter so this 
should apply here)
it's a lot easier to validate the input when you're using it instead of 
trying to restrict their input.
I.E. when they hit 'submit' give them a little warning saying 'sorry, 
you can't submit unless you have an integer in field "fieldname" .'
That's most likely the easiest way to do this.
If you just google for tkinter validate entry  you should come up with 
the other way, IIRC (where they can't input incorrect values).
HTH,
-Luke.
>
> -- 
> To HIM you shall return.
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-26 Thread Alan Gauld
> newbie book, there certainly wouldn't be as many.  but speaking of
> learn how to program... is there a 2nd edition coming soon?  :-)

Unlikely. The sales have been steady (surprisingly so given its age 
now)
but not exactly stellar so Addison Wesley aren't too excited about
it I suspect. To be honest I'm not overly excited myself, the real 
focus
has always been the web site, I only wrote the book because so many
folks emailed me and asked for a paper version.

If AW ask me to do a revision I wouldn't say no, but I'm not chasing
them to do so... Any 2nd edition would reflect the changes on the
web site: extra topics, expanded coverage, and language updates
since version 1.5.2.

>> It's back!
>> http://www.uselesspython.com/
>
> this is great news.  i wish there were more sites like this and the
> Challenge out there... they really make you think in "real-time."

I agree. Useless is a unique resource so far as I can tell.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-26 Thread wesley chun
On 8/25/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
>> > i put a significant number of exercises in "Core Python," some which
>> > ...
>> > there are about 260 exercises in the 1st ed., and the upcoming 2nd
>> > ed. will have well over 300.  i believe this is more than any other
>> > Python book out there.
>
> Certainly more than mine Wes!

ah, but your book is target towards a different audience, the newbie.
'core python' is directed towards existing programmers new to python.
the fastest and most effective way to learn a new language is to
exercise the concepts and newfound knowledge. if i were to write a
newbie book, there certainly wouldn't be as many.  but speaking of
learn how to program... is there a 2nd edition coming soon?  :-)


>> I got a lot of flack on amazon reviews for not putting exercises
>> at the end of each chapter - although in fact there are over 50
>> "challenges" throughout the text, but you have to read the text
>> to find them! ( That's because I don't like text book style
>> exercises personally...)

i'm not a fan of dry textbooks, but i do believe in exercises.
sometimes i put what i call "mental challenges" in chapter reading,
but they are not phrased as problems to be solved, just something to
think about for the reader.

>> However one badly missed Python resource that used to help
>> a lot was the Useless Python web site. Alas it seems to have
>> dissappeared fromthe web.
>
> It's back!
> http://www.uselesspython.com/

this is great news.  i wish there were more sites like this and the
Challenge out there... they really make you think in "real-time."

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-25 Thread Kent Johnson
Alan Gauld wrote:
> However one badly missed Python resource that used to help
> a lot was the Useless Python web site. Alas it seems to have
> dissappeared fromthe web. 

It's back!
http://www.uselesspython.com/

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-25 Thread Alan Gauld
> i put a significant number of exercises in "Core Python," some which
> ...
> there are about 260 exercises in the 1st ed., and the upcoming 2nd 
> ed.
> will have well over 300.  i believe this is more than any other 
> Python
> book out there.

Certainly more than mine Wes!
I got a lot of flack on amazon reviews for not putting exercises
at the end of each chapter - although in fact there are over 50
"challenges" throughout the text, but you have to read the text
to find them! ( That's because I don't like text book style
exercises personally...)

However one badly missed Python resource that used to help
a lot was the Useless Python web site. Alas it seems to have
dissappeared fromthe web. The author tried to do a revamp and
for some reason version 2 never quite got the support the original
site did. But it was a great resource of little mini-projects and
challenges for beginners.

And of course the Python Challenge game is still on the web,
but the challenge there tends to be in figuring out what to do,
once yopu get past that you can write the code in about
5 minutes flat! The other problem is you usually need to paste
the result back into a webn browser to assess the result!

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-24 Thread R. Alan Monroe
> On 8/24/06, John Fouhy <[EMAIL PROTECTED]> wrote:
>> On 25/08/06, Joe Gamman <[EMAIL PROTECTED]> wrote:
>> > Anyway, would appreciate any comments on the idea.
>>
>> It seems like the sort of thing you find in textbooks or "learn to
>> program" books -- most books of this nature have suitable exercises at
>> the end of each chapter,

> joe,

> as john has suggested, exercises in Python books are another great way
> to get in the "practice" that you desire.

> i put a significant number of exercises in "Core Python," some which
> take 20-minutes as you had mentioned, but others may take up to 20
> hours.  they range from easy all the way to difficult, and some are
> game-oriented while most are not.

> there are about 260 exercises in the 1st ed., and the upcoming 2nd ed.
> will have well over 300.  i believe this is more than any other Python
> book out there. hope to be challenging you someday!

Sometimes you can find old books of very simplistic games written in
BASIC at your local library, and try converting them. Some of these
old books are archived at http://www.atariarchives.org/ .

Alan

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-24 Thread wesley chun
On 8/24/06, John Fouhy <[EMAIL PROTECTED]> wrote:
> On 25/08/06, Joe Gamman <[EMAIL PROTECTED]> wrote:
> > Anyway, would appreciate any comments on the idea.
>
> It seems like the sort of thing you find in textbooks or "learn to
> program" books -- most books of this nature have suitable exercises at
> the end of each chapter,

joe,

as john has suggested, exercises in Python books are another great way
to get in the "practice" that you desire.

i put a significant number of exercises in "Core Python," some which
take 20-minutes as you had mentioned, but others may take up to 20
hours.  they range from easy all the way to difficult, and some are
game-oriented while most are not.

there are about 260 exercises in the 1st ed., and the upcoming 2nd ed.
will have well over 300.  i believe this is more than any other Python
book out there. hope to be challenging you someday!

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query to the tutor mailing list

2006-08-24 Thread John Fouhy
On 25/08/06, Joe Gamman <[EMAIL PROTECTED]> wrote:
> Anyway, would appreciate any comments on the idea.

It seems like the sort of thing you find in textbooks or "learn to
program" books -- most books of this nature have suitable exercises at
the end of each chapter, or spread throughout each chapter.  Also,
creating good problems seems to me like it would be a difficult thing
to do, without plagiarising existing works.

Have you tried looking at programming books, or online tutorials (eg,
Alan Gauld's?  Or I think "How to think like a computer scientist"
uses python.)?

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query regarding Unittest module behaviour

2006-07-04 Thread Luke Paireepinart
[snip]
> Script abc.py imports xyz.py.
> Now when I execute abc.py from commandlline all unittestcases of 
> xyz.py are also executed.
> Why is this happening and what can be the solution to this.
anything that's in the global scope  of an imported module gets executed.
For example...

-- a.py
print "hello"


 >>> import a
This will cause the "hello" to be printed from 'a.py' because it's in 
the global scope.
so what you want to do is something like this:

-a.py

def main():
print "hello"

if __name__ == "__main__":
main()
---

Now if you do
 >>> import a
nothing will be printed.

But, if you do "python a.py" on command line, you'll see "hello" be printed.
I think this is the problem you're having.
HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] query python mastery via programming exercises

2006-05-08 Thread Kermit Rose






 
 

From: Alan Gauld
Date: 05/08/06 06:20:42
To: Kermit Rose; tutor@python.org
Subject: Re: [Tutor] query python mastery via programming exercises
 
 
The nearest to that is the Python Challenge 'game' web site.
It presents a series of challenges to be solved in Python. Each
challenge uses a specific feature of Python and the challenges
get harder as you go. If you complete all challenges you will
have a very good knowledge of pyuthon and some of its more
interesting modules.
 
You will need a basic knowledge of pytho0n to start with, the
official tutor is the best place to go if you altready know other
programming languages.
 
*
 
Thank you very much.  I have bookmarked the game site, and plan to explore it as soon as possible.
 
***
 
There is a job listing page on the Python web site. Also some
jobs get posted on Python mailing lists (esp regional ones)
and on the comp.lanmg.python newsgroup.
 
 

 
Thanks.  I've now bookmarked the http://www.python.org/ web page for future referrence.
 
Kermit   <    [EMAIL PROTECTED]  >
 
 
* 
 







___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] query python mastery via programming exercises

2006-05-08 Thread Alan Gauld
Hello again,

> Has someone organized a set of exercises that when solved in the 
> order
> presented teaches mastery of Python programming.

The nearest to that is the Python Challenge 'game' web site.
It presents a series of challenges to be solved in Python. Each
challenge uses a specific feature of Python and the challenges
get harder as you go. If you complete all challenges you will
have a very good knowledge of pyuthon and some of its more
interesting modules.

You will need a basic knowledge of pytho0n to start with, the
official tutor is the best place to go if you altready know other
programming languages.

> Second,   if or when I master Python,   how may I contact folks who 
> wish to
> pay for programs written in Python.

There is a job listing page on the Python web site. Also some
jobs get posted on Python mailing lists (esp regional ones)
and on the comp.lanmg.python newsgroup.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query

2006-03-06 Thread Kent Johnson
Rob Lane wrote:
> Hey,
> 
> I am currently working on a final year project in college building a 3-D 
> virtual model of a building in a python based program called Vizard. I 
> have prepared a set of tutorials on powerpoint which i would like to 
> access by clicking on various objects in the building. e.g. Click on a 
> Window/Air Duct and then open a powerpoint presentation on the topic.
> 
> I was wondering if there is python code which will let me launch my 
> powerpoint presentations from with my virtual world.

os.system() will let you execute a command line, for example

os.system(r'"C:\Program Files\Microsoft Office\Office\POWERPNT.EXE" 
F:\Personal\TEACHI~1\TDDINT~1.PPT')

Note the use of a raw string to allow \ path separators and the 
double-quotes around the path to the exe because it has spaces in it.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] query regarding mysql database backup?? please Help

2006-02-03 Thread Danny Yoo


On Fri, 3 Feb 2006, deepak.gupta wrote:

> I did my script like this for backing my max_everest_2006 mysql database
>
> error: 1044: Access denied for user 'MAX_USER'@'%' to database
> 'MAX_EVEREST_2006' when using LOCK TABLES


Your question doesn't have to do with Python.  Ask your MySQL
administrator to help you: what you're seeing is a MySQL error message.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] query regarding mysql database backup?? please Help

2006-01-04 Thread Alan Gauld
> > os.system("mysqldump --add-drop-table -c -u root  -pmysql 
> > max_everest_2006 > "+target_dir+"/table.bak.sql")
>
> root is my user name,mysql is my password

You shouldn't use root to adnin a database, it leaves you open to
all sorts of security holes.

And you shouldn't post your root password on a public list, I hope it
wasn't really your password or that you changed it immediately!

> but it tolds me that does not recognise mysqldump as an internal
> or external command

I suspect the path needs to be set or more likely you provide the full
path of the mysqldump command to os.system...

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query dictionaries with sql-like syntax

2005-07-13 Thread Alan G
> I'm trying to migrate a lot of data from an accounting 
> system software to another using Python. 

he easiest way to do this is usually to create a set of 
views using SQL in one database that map to the schema 
in the other. You can then read the table data out of 
the views and export it into the other DB.

This has the advantage of full SQL access to all tables
in the source DB and use SQL to join and manipulate field
data in populating the views. Default values can be added too.

> required, my question is: does a module/package/... exist 
> to query a set of dictionaries using sql syntax 

I don't know of one, thats where the Python DBI comes in 
to access sql tables directly from Python, but perrsonally 
for migrations, whoich I sadly seem to spend a lot of time 
doing! - I prefer the view approach - provided you
a) have access tothe underlying databases and
b) they support views!

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query dictionaries with sql-like syntax

2005-07-13 Thread Kent Johnson
Negroup - wrote:
> Anyway, I'm learning Python and just for exercise I'll try to
> implement a simple sql-like interface for querying dictionaries!

It might be a better use of your time to learn about Python's built-in 
abilities to manipulate lists and dictionaries, which are powerful and simple. 
You don't say what kind of queries you need to do but they may be very simple 
to express directly in Python. My guess is that simple loops and list 
comprehensions would do a lot of the work for you.

I'm going to guess that your data is expressed as lists of dictionaries. For 
example here is a list with two records:

 >>> r1 = { 'fname' : 'Kent', 'lname' : 'Johnson' }
 >>> r2 = { 'fname' : 'Negroup', 'lname' : None }
 >>> data = [ r1, r2 ]
 >>> data
[{'lname': 'Johnson', 'fname': 'Kent'}, {'lname': None, 'fname': 'Negroup'}]

Suppose I want to find everyone whose first name is 'Kent'. This is easy to do 
with a list comprehension:

 >>> [ r for r in data if r['fname'] == 'Kent' ]
[{'lname': 'Johnson', 'fname': 'Kent'}]

What if I want to compute a new field in the form lastname, firstname but 
handle the case of lastname == None? A helper function to compute the new field 
looks like this:

 >>> def makeLnameFname(fname, lname):
 ...   if not lname: return fname
 ...   return '%s, %s' % (lname, fname)
 ...

Let's try it out to make sure it works:

 >>> makeLnameFname('Kent', 'Johnson')
'Johnson, Kent'
 >>> makeLnameFname('Negroup', None)
'Negroup'

Now we go through the data adding a 'fullname' attribute to each record. A for 
loop makes short work of this:

 >>> for r in data:
 ...   r['fullname'] = makeLnameFname(r['fname'], r['lname'])
 ...
 >>> data
[{'lname': 'Johnson', 'fullname': 'Johnson, Kent', 'fname': 'Kent'}, {'lname': 
None, 'fullname': 'Negroup', 'fname': 'Negroup'}]

I hope you can see that this is vastly easier than implementing an SQL engine. 
Python is very powerful for manipulating data structures and learning how to 
use that power is well worth the time.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query dictionaries with sql-like syntax

2005-07-13 Thread Negroup -
2005/7/13, Sandip Bhattacharya <[EMAIL PROTECTED]>:
> Negroup - wrote:
[cut]
> 
> Why dont you use python-sqlite to dump your data? You might need to
> modify your sql slightly(depending on your current db), but rest is all sql.

Thanks for the suggestion, I didn't even know about python-sqlite
existence... Probably, in another occasion I'll need to develop a such
migra-tool I will at least consider to use python-sqlite. Now it would
be quite hard (a lot of work has already been done).

Anyway, I'm learning Python and just for exercise I'll try to
implement a simple sql-like interface for querying dictionaries!

Thank you.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Query dictionaries with sql-like syntax

2005-07-13 Thread Sandip Bhattacharya
Negroup - wrote:
> Hi all, I hope my questions makes sense..
> I'm trying to migrate a lot of data from an accounting system software
> to another using Python. Software A stores datas inside sql tables,
> software B too, with a different db structure.
> 
> My idea has been to export sensible tables from db A, store them in
> python dictionaries, process/elaborate these dictionaries, and
> generate files containing data in a format that is importable from
> system B. Yes, it could seem a good idea.. however now I must handle a
> lot of dictionaries with a lot of related data.
> 

Why dont you use python-sqlite to dump your data? You might need to
modify your sql slightly(depending on your current db), but rest is all sql.

- Sandip

-- 
Sandip Bhattacharya  *Puroga Technologies   * [EMAIL PROTECTED]
Work: http://www.puroga.com  *   Home/Blog: http://www.sandipb.net/blog

PGP/GPG Signature: 51A4 6C57 4BC6 8C82 6A65 AE78 B1A1 2280 A129 0FF3

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor