[Tutor] One basic question about the Python's PID of Process object.

2011-08-28 Thread Han Guanghua
Hello, 

 

1.   When I tried to run the following code (belonging to one Python
program), for the dynamically created object of CUSTOMER as Process type,
the printed PID as the following is always the same. 

 

Why?

 

2. Second question:  When I changed the os.getpid() to os.getppid() or self.
_pid,  the Python’s interpreter generates some error message like
“Customer object has no attribute ‘_pid’”.

 

Thanks for your help!

 

Partly code for the CUSTOMER object:

 

class Customer(Process):

def buyBaguette(self,cusType,bakery):

tIn=now()

print "PID is %d"%(os.getpid())

yield get,self,bakery.stock,r.choice(buy[cusType])

waits[cusType].append(now()-tIn)



def __del__(self):

print "distroy the customer"

 

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


[Tutor] program code for Python Programming for the Absolute Beginner, 3rd ed.?

2011-08-28 Thread Richard D. Moores
The book says the program code is in py3e_source.zip, at
www.courseptr.com/downloads , but that leads to the book at

with a "View Available Downloads" link that yields no downloads.

Does anyone know where  py3e_source.zip is? Or if you have it, could
you email it to me?

Thanks,

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


Re: [Tutor] Better way to compare values?

2011-08-28 Thread memilanuk




There's a few things that can make this a lot shorter. To address your
primary question first, the python idiom for this is like so:

if 10 in (a, b):

it constructs a tuple containing the variables a and b, then checks if
the number ten is inside that tuple. simple and straightforward. Now,
or's aren't limited to just two operands. you can chain multiple or's
together to make this function even shorter, like so:

if a == 10 or b == 10 or a + b == 10:


That's pretty much what the 'book' solution was (found the 'Show 
Solution' button staring me in the face shortly after posting my 
question here.


def makes10(a,b)
return(a == 10 or b == 10 or a + b == 10)




So, all said and done, the solution to your problem in idiomatic
python looks like this:

def makes10(a, b):
 return 10 in (a, b, a + b)



Something else I learned from this... for some reason I had the idea in 
my head that 'return' was a function like print() i.e. 'return()'. 
Obviously not, from your example and from others that I dug up in the 
Python docs.


Thanks for the detailed explanation - it was very clear and easy to 
follow!  What I had originally been thinking was something like 'a && b' 
or 'a || b'... but that was probably some cross-pollenation from earlier 
dabbling in PHP or something ;)


Thanks,

Monte

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


Re: [Tutor] Better way to compare values?

2011-08-28 Thread Hugo Arts
On Mon, Aug 29, 2011 at 3:19 AM, memilanuk  wrote:
> Hello,
>
> Recently picked back up the Python bug, and have started chipping away at
> the warm-up exercises over @ codingbat.com.
>
> I'm getting thru the basic stuff okay, but I'm getting a feeling that maybe
> I'm not doing things the 'right' way, even though the results are correct.
>
> Specifically, checking if value 'a' OR value 'b' equal a certain number...
>
> The example that I'm on at the moment (like I said, starting over at the
> basics)...
>
> http://codingbat.com/prob/p124984
>
> My solution:
>
> '''
> Given 2 ints, a and b, return True if one if them is 10 or if their sum is
> 10.
>
> makes10(9, 10) = True
> makes10(9, 9) = False
> makes10(1, 9) = True
> '''
>
> def makes10(a, b):
>    if (a == 10) or (b == 10):
>        #print('Yep!')
>        return(True)
>
>    elif a + b == 10:
>        #print('Si!')
>        return(True)
>
>    else:
>        #print('Nein!')
>        return(False)
>
> makes10(10,9)
> #makes10(9,9)
> #makes10(1,9)
>
>
>
> In particular, the 'if (a == 10) or (b == 10): line... is there a
> shorter/more compact/more correct (i.e. pythonic) way of testing to see if a
> OR b is equal to 10?
>

There's a few things that can make this a lot shorter. To address your
primary question first, the python idiom for this is like so:

if 10 in (a, b):

it constructs a tuple containing the variables a and b, then checks if
the number ten is inside that tuple. simple and straightforward. Now,
or's aren't limited to just two operands. you can chain multiple or's
together to make this function even shorter, like so:

if a == 10 or b == 10 or a + b == 10:

That's pretty nice. And now, for the grand trick, we can apply out
little idiom we just learned up there to this three part if statement:

if 10 in (a, b, a + b):

See what we did there? But we can do you one better. this construction
I see a lot from beginning programmers:

def function(x):
if x:
return True
else:
return False

But it is actually quite superfluous. if you check if a certain
expression is true, and if so, return True, we may just as well return
the expression itself to begin with! We only need to make sure the
function returns only True or False, and we can do that nicely by
converting our initial expression to a boolean:

def function(x):
return bool(x)

So, all said and done, the solution to your problem in idiomatic
python looks like this:

def makes10(a, b):
return 10 in (a, b, a + b)

Note: I didn't have to use the bool() function in our last example,
because I know that the 'in' operator always returns either True or
False.

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


Re: [Tutor] Better way to compare values?

2011-08-28 Thread bob gailer

On 8/28/2011 9:19 PM, memilanuk wrote:

Hello,

Recently picked back up the Python bug, and have started chipping away 
at the warm-up exercises over @ codingbat.com.


I'm getting thru the basic stuff okay, but I'm getting a feeling that 
maybe I'm not doing things the 'right' way, even though the results 
are correct.


Specifically, checking if value 'a' OR value 'b' equal a certain 
number...


The example that I'm on at the moment (like I said, starting over at 
the basics)...


http://codingbat.com/prob/p124984

My solution:

'''
Given 2 ints, a and b, return True if one if them is 10 or if their 
sum is 10.


makes10(9, 10) = True
makes10(9, 9) = False
makes10(1, 9) = True
'''

def makes10(a, b):
if (a == 10) or (b == 10):
#print('Yep!')
return(True)

elif a + b == 10:
#print('Si!')
return(True)

else:
#print('Nein!')
return(False)

makes10(10,9)
#makes10(9,9)
#makes10(1,9)



In particular, the 'if (a == 10) or (b == 10): line... is there a 
shorter/more compact/more correct (i.e. pythonic) way of testing to 
see if a OR b is equal to 10? stinfo/tutor



if 10 in (a,b) would do it.

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


[Tutor] Better way to compare values?

2011-08-28 Thread memilanuk

Hello,

Recently picked back up the Python bug, and have started chipping away 
at the warm-up exercises over @ codingbat.com.


I'm getting thru the basic stuff okay, but I'm getting a feeling that 
maybe I'm not doing things the 'right' way, even though the results are 
correct.


Specifically, checking if value 'a' OR value 'b' equal a certain number...

The example that I'm on at the moment (like I said, starting over at the 
basics)...


http://codingbat.com/prob/p124984

My solution:

'''
Given 2 ints, a and b, return True if one if them is 10 or if their sum 
is 10.


makes10(9, 10) = True
makes10(9, 9) = False
makes10(1, 9) = True
'''

def makes10(a, b):
if (a == 10) or (b == 10):
#print('Yep!')
return(True)

elif a + b == 10:
#print('Si!')
return(True)

else:
#print('Nein!')
return(False)

makes10(10,9)
#makes10(9,9)
#makes10(1,9)



In particular, the 'if (a == 10) or (b == 10): line... is there a 
shorter/more compact/more correct (i.e. pythonic) way of testing to see 
if a OR b is equal to 10?


Thanks,

Monte

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


Re: [Tutor] Sorting list of tuples in two passes

2011-08-28 Thread Hugo Arts
On Mon, Aug 29, 2011 at 2:19 AM, Dayo Adewunmi  wrote:
> On 08/28/2011 06:23 PM, Hugo Arts wrote:
>>
>> On Sun, Aug 28, 2011 at 6:43 PM, Dayo Adewunmi
>>  wrote:
>>>
>>> Hi
>>>
>>> I have a list of tuples that each have four elements:
>>>
>>> [(firstName,lastName,userName,gidNumber),(.)]
>>>
>>> I'm trying to sort this list in two passes. First by gidNumber and then
>>> the
>>> subgroups by lastName. So far i've only been able to sort by gidNumber.
>>> But
>>> I can't seem to wrap my mind around lambda, which is what my browsing
>>> around
>>> seems to indicate is needed to achieve this?
>>>
>>> Thanks
>>>
>>> Dayo
>>>
>> Python's builtin sort is stable, which means that ordering of items
>> with the same key is preserved. This property means that you can do
>> multiple pass sorting very easily and efficiently just by sorting
>> twice:
>>
> # we'll simplify the problem a bit and have tuples with just last name
> and id.
> l = [('aaa', 1), ('bbb', 1), ('ccc', 1), ('aaa', 2), ('bbb', 2),
> ('ccc', 2)]
> l.sort(key=itemgetter(0))
> l
>>
>> [('aaa', 1), ('aaa', 2), ('bbb', 1), ('bbb', 2), ('ccc', 1), ('ccc', 2)]
>
> l.sort(key=itemgetter(1))
> l
>>
>> [('aaa', 1), ('bbb', 1), ('ccc', 1), ('aaa', 2), ('bbb', 2), ('ccc', 2)]
>> We sort by last name first, then sort again by id. As you can see, the
>> sorting of groups with the same id is preserved, and our list is now
>> in the correct order.
>>
>> Hugo
>>
>
> It works when I use your example, but I don't understand why it won't work
> when I use 4-element tuples instead of 2:
>
l = [('wascas','aaa','fdvdfv', 1), ('rtgdsf','bbb','trfg', 1),
 ('addwe','ccc','esd', 1), ('xasd','aaa','wascaq', 2), ('nhy','bbb','asw',
 2), ('','ccc','dgdeg', 2)]
 l
> [('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe',
> 'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 2),
> ('', 'ccc', 'dgdeg', 2)]
 l.sort(key=itemgetter(3))
 l
> [('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe',
> 'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 2),
> ('', 'ccc', 'dgdeg', 2)]
 l.sort(key=itemgetter(1))
 l
> [('wascas', 'aaa', 'fdvdfv', 1), ('xasd', 'aaa', 'wasca    q', 2),
> ('rtgdsf', 'bbb', 'trfg', 1), ('nhy', 'bbb', '
> asw', 2), ('addwe', 'ccc', 'esd', 1), ('', 'ccc', 'dgdeg', 2)]

>
>
> Also I notice your original list and your end result list are in the same
> order.
>
> Thanks
>
> Dayo
>

In my original example, you can shuffle the list before you sort it
and it will still work. Try it, with a quick "from random import
shuffle; shuffle(l)".

Also, notice that you want to sort by your primary order *last*. I
sorted by last name first, then sorted by id second, which means the
final list's primary order is by id, and secondary order by last name.
So the sorting goes in reverse. In your example, you sort by id first,
then last name. So your final list's primary order is by last name.

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


Re: [Tutor] Sorting list of tuples in two passes

2011-08-28 Thread Steven D'Aprano

Dayo Adewunmi wrote:

It works when I use your example, but I don't understand why it won't 
work when I use 4-element tuples instead of 2:


What makes you say it doesn't work? It looks like it works to me:

 >>>l = [('wascas','aaa','fdvdfv', 1), ('rtgdsf','bbb','trfg', 1), 
('addwe','ccc','esd', 1), ('xasd','aaa','wascaq', 2), 
('nhy','bbb','asw', 2), ('','ccc','dgdeg', 2)]

 >>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe', 
'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 
2), ('', 'ccc', 'dgdeg', 2)]

 >>> l.sort(key=itemgetter(3))
 >>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe', 
'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 
2), ('', 'ccc', 'dgdeg', 2)]


List "l" is now sorted by the element in position 3 (counting from zero, 
not one): 1 1 1 2 2 2




 >>> l.sort(key=itemgetter(1))
 >>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('xasd', 'aaa', 'wascaq', 2), 
('rtgdsf', 'bbb', 'trfg', 1), ('nhy', 'bbb', '

asw', 2), ('addwe', 'ccc', 'esd', 1), ('', 'ccc', 'dgdeg', 2)]


And now elements are sorted in order of position 1:
aaa aaa bbb bbb ccc ccc

In the event of ties (and there are three pairs of ties) the elements 
keep the relative order they were in after the first sort.


The sorting worked just as expected.




--
Steven

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


Re: [Tutor] Sorting list of tuples in two passes

2011-08-28 Thread Dayo Adewunmi

On 08/28/2011 06:23 PM, Hugo Arts wrote:

On Sun, Aug 28, 2011 at 6:43 PM, Dayo Adewunmi  wrote:

Hi

I have a list of tuples that each have four elements:

[(firstName,lastName,userName,gidNumber),(.)]

I'm trying to sort this list in two passes. First by gidNumber and then the
subgroups by lastName. So far i've only been able to sort by gidNumber. But
I can't seem to wrap my mind around lambda, which is what my browsing around
seems to indicate is needed to achieve this?

Thanks

Dayo


Python's builtin sort is stable, which means that ordering of items
with the same key is preserved. This property means that you can do
multiple pass sorting very easily and efficiently just by sorting
twice:


# we'll simplify the problem a bit and have tuples with just last name and id.
l = [('aaa', 1), ('bbb', 1), ('ccc', 1), ('aaa', 2), ('bbb', 2), ('ccc', 2)]
l.sort(key=itemgetter(0))
l

[('aaa', 1), ('aaa', 2), ('bbb', 1), ('bbb', 2), ('ccc', 1), ('ccc', 2)]

l.sort(key=itemgetter(1))
l

[('aaa', 1), ('bbb', 1), ('ccc', 1), ('aaa', 2), ('bbb', 2), ('ccc', 2)]
We sort by last name first, then sort again by id. As you can see, the
sorting of groups with the same id is preserved, and our list is now
in the correct order.

Hugo



It works when I use your example, but I don't understand why it won't 
work when I use 4-element tuples instead of 2:


>>>l = [('wascas','aaa','fdvdfv', 1), ('rtgdsf','bbb','trfg', 1), 
('addwe','ccc','esd', 1), ('xasd','aaa','wascaq', 2), 
('nhy','bbb','asw', 2), ('','ccc','dgdeg', 2)]

>>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe', 
'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 
2), ('', 'ccc', 'dgdeg', 2)]

>>> l.sort(key=itemgetter(3))
>>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe', 
'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 
2), ('', 'ccc', 'dgdeg', 2)]

>>> l.sort(key=itemgetter(1))
>>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('xasd', 'aaa', 'wascaq', 2), 
('rtgdsf', 'bbb', 'trfg', 1), ('nhy', 'bbb', '

asw', 2), ('addwe', 'ccc', 'esd', 1), ('', 'ccc', 'dgdeg', 2)]
>>>


Also I notice your original list and your end result list are in the 
same order.


Thanks

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


Re: [Tutor] Sorting list of tuples in two passes

2011-08-28 Thread Hugo Arts
On Sun, Aug 28, 2011 at 6:43 PM, Dayo Adewunmi  wrote:
> Hi
>
> I have a list of tuples that each have four elements:
>
> [(firstName,lastName,userName,gidNumber),(.)]
>
> I'm trying to sort this list in two passes. First by gidNumber and then the
> subgroups by lastName. So far i've only been able to sort by gidNumber. But
> I can't seem to wrap my mind around lambda, which is what my browsing around
> seems to indicate is needed to achieve this?
>
> Thanks
>
> Dayo
>

Python's builtin sort is stable, which means that ordering of items
with the same key is preserved. This property means that you can do
multiple pass sorting very easily and efficiently just by sorting
twice:

>>> # we'll simplify the problem a bit and have tuples with just last name and 
>>> id.
>>> l = [('aaa', 1), ('bbb', 1), ('ccc', 1), ('aaa', 2), ('bbb', 2), ('ccc', 2)]
>>> l.sort(key=itemgetter(0))
>>> l
[('aaa', 1), ('aaa', 2), ('bbb', 1), ('bbb', 2), ('ccc', 1), ('ccc', 2)]
>>> l.sort(key=itemgetter(1))
>>> l
[('aaa', 1), ('bbb', 1), ('ccc', 1), ('aaa', 2), ('bbb', 2), ('ccc', 2)]
>>>

We sort by last name first, then sort again by id. As you can see, the
sorting of groups with the same id is preserved, and our list is now
in the correct order.

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


Re: [Tutor] need help to learn threading

2011-08-28 Thread Nitin Pawar
this one at tutorial point explains multi threading with examples ..

http://www.tutorialspoint.com/python/python_multithreading.htm

On Sun, Aug 28, 2011 at 9:50 PM, Payal  wrote:

> Hi all,
> Can someone suggest a resource for me to learn threading in python? I
> don't know threading in any other language.
> I tried a few online tutorials but got lost soon. How do I start?
>
> With warm regards,
> -Payal
> --
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


[Tutor] need help to learn threading

2011-08-28 Thread Payal
Hi all,
Can someone suggest a resource for me to learn threading in python? I
don't know threading in any other language.
I tried a few online tutorials but got lost soon. How do I start?

With warm regards,
-Payal
-- 


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


[Tutor] Sorting list of tuples in two passes

2011-08-28 Thread Dayo Adewunmi

Hi

I have a list of tuples that each have four elements:

[(firstName,lastName,userName,gidNumber),(.)]

I'm trying to sort this list in two passes. First by gidNumber and then 
the subgroups by lastName. So far i've only been able to sort by 
gidNumber. But I can't seem to wrap my mind around lambda, which is what 
my browsing around seems to indicate is needed to achieve this?


Thanks

Dayo

import ldap,re
from operator import itemgetter,attrgetter

l = ldap.initialize("ldap://172.20.0.1";)
l.simple_bind_s("","")

base_dn = 'ou=People,dc=aust,o=ami-net'
filter = '(objectclass=pilotPerson)'
attrs = ['uid', 'gidNumber', 'sn', 'cn']

users = l.search_s(base_dn, ldap.SCOPE_ONELEVEL, filter, attrs)

def onelist(users):
studentspubline = tuple()
students2xPubLines = []

for aUser in users:
# Get the user details from LDAP
userName = aUser[1]['uid'][0]
gidNumber = aUser[1]['gidNumber'][0]
lastName = aUser[1]['sn'][0]
fullName = aUser[1]['cn'][0]

# Get first names of users
splitFullName = fullName.split()
firstName = splitFullName[1]

if gidNumber[:1] == '9':
studentspubline = userName,lastName,fullName,gidNumber
students2xPubLines.append(studentspubline)

sortedStudents2x = sorted(students2xPubLines, key=itemgetter(3,2))

for userName,lastName,fullName,gidNumber in sortedStudents2x:
print "lastName: %s, gidNumber: %s" %(lastName, gidNumber)

onelist(users)

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


Re: [Tutor] Still stuck - but a bit further on.

2011-08-28 Thread Lisi
On Sunday 28 August 2011 16:20:45 Alan Gauld wrote:
>
> Notice the space at the start of the string?
> The stored entry is keyed on "Lisi" the search was for " Lisi"
> which doesn't exist.

I thought that I had tried that, but I obviously hadn't.  I clearly needed 
that break!

Thanks, Alan and Steven.

Lisi

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


Re: [Tutor] Still stuck - but a bit further on.

2011-08-28 Thread Alan Gauld

On 28/08/11 14:53, Lisi wrote:


Type the Name - leave blank to finishLisi


Notice no space before Lisi



Type the Street, Town, Phone. Leave blank to finishth, rc, 123457
Type the Name - leave blank to finish
Which name to display?(blank to finish) Lisi


Notice the space before Lisi?


Traceback (most recent call last):
   File "/usr/local/bin/AddressBook", line 17, in
 print name, AddressBook[name]
KeyError: ' Lisi'


Notice the space at the start of the string?
The stored entry is keyed on "Lisi" the search was for " Lisi"
which doesn't exist.

You could as an exercise make the program more robust to that kind of 
error by eliminating whitespace at each end of the key string both 
before storing the data and when searching...


You might also consider eliminating case errors by forcing both
the key string and search string to lowercase, but if you do
that you will want to store the name with case in the value
part and not rely on the key being part of the data...


--
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] Can't find error :-(

2011-08-28 Thread Alan Gauld

On 28/08/11 13:34, Lisi wrote:

On Sunday 28 August 2011 13:01:38 Peter Otten wrote:

Lisi wrote:

Here is the error message:
/usr/local/bin/AddressBook: line 6: syntax error near unexpected token



That's an error message by your shell. You made your script executable, but
forgot to add something like

#!/usr/bin/env python



I am very grateful to you both.  I had realised that the error might be
somewhere other than where it is indicated because Alan had already told me
so - but had not understood that it could be, not only in a different place,
but quite a way away.


The really important point here is that the error was a shell error not 
a Python error. (You need to check the format of the message to see the 
differences and get used to them) If it had been a Python error it would 
typically be within a line or two of the reported place (occasionally 
more but not usually) but shell errors can often be a long way off 
depending on how much the file content looks like shell script...



For future reference, how would I set about changing the encoding for just one
character or file?  I don't really want to change the encoding I use system
wide.


Mostly you shouldn't need to for the stuff in my tutorial, I certainly 
didn't use any funny characters so the default decoding should just work.


One of these days I'll add a topic on encodings but I need to really get 
my own brain wrapped around it before thenm, and I'm still a bit fuzzy 
in places myself!


--
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] Still stuck - but a bit further on.

2011-08-28 Thread Steven D'Aprano

Lisi wrote:


[...]

Type the Name - leave blank to finishLisi


You have no space in the name. It is "Lisi".



Type the Street, Town, Phone. Leave blank to finishth, rc, 123457
Type the Name - leave blank to finish
Which name to display?(blank to finish) Lisi


Here you have a space at the start of the name: " Lisi".


 Lisi
Traceback (most recent call last):
  File "/usr/local/bin/AddressBook", line 17, in 
print name, AddressBook[name]
KeyError: ' Lisi'


Sure enough, no Lisi-with-a-space.


I have tried various other words besides Lisi, each one less probable than its 
predecessor.  I am getting nowhere.  The error may well be somewhere else 
again, but I can't see where. :-(


For further debugging, print AddressBook and see what is in it.




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


Re: [Tutor] Can't find error :-(

2011-08-28 Thread Steven D'Aprano

Lisi wrote:

For future reference, how would I set about changing the encoding for just one 
character or file?  I don't really want to change the encoding I use system 
wide.


You can set the encoding for the entire source file with an encoding 
line like:


# -*- coding: utf-8 -*-

This MUST be in the first one or two lines of the file to take effect:

Optional shebang line.
Encoding line.

The exact form of the encoding line is very flexible. See here for more 
information:


http://docs.python.org/reference/lexical_analysis.html#encoding-declarations


The encoding line is only necessary if you want to include non-ASCII 
characters in your source code, either as variable names or in literal 
strings, e.g.:


µ = 2.5
name = u"Michael Groß"

(Warning: just because you insert an encoding line in the source file, 
doesn't mean your editor will obey it. Emacs and vim probably will, but 
for most other editors, you may need to manually set the encoding. E.g. 
in the kwrite editor, go to menu Tools > Encoding and choose whichever 
one you prefer.)


Without an encoding line, you would have to use:

name = u"Michael Gro\xdf"

or

name = u"Michael Gro\N{LATIN SMALL LETTER SHARP S}"


You might be able to get away without an encoding line if the non-ASCII 
characters are only in comments.



--
Steven

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


[Tutor] Still stuck - but a bit further on.

2011-08-28 Thread Lisi
Sorry, everybody.  I am still having problems with this script - tho' it now 
runs correctly nearly to the end!

Here is the whole script:

#! /usr/bin/python

# create an empty address book dictionary
AddressBook = {}

# read entries till an empty string
print
name = raw_input("Type the Name - leave blank to finish")
while name != "":
entry = raw_input("Type the Street, Town, Phone. Leave blank to finish")
AddressBook[name] = entry
name = raw_input("Type the Name - leave blank to finish")

# now ask for one to display
name = raw_input("Which name to display?(blank to finish)")
while name !="":
print name, AddressBook[name]
name = raw_input("Which name to display?(blank to finish)")

I started with:
lisi@Tux:/usr/local/bin$ AddressBook

Type the Name - leave blank to finishLisi
Type the Street, Town, Phone. Leave blank to finishth, rc, 123457
Type the Name - leave blank to finish
Which name to display?(blank to finish) Lisi
 Lisi
Traceback (most recent call last):
  File "/usr/local/bin/AddressBook", line 17, in 
print name, AddressBook[name]
KeyError: ' Lisi'


I have tried various other words besides Lisi, each one less probable than its 
predecessor.  I am getting nowhere.  The error may well be somewhere else 
again, but I can't see where. :-(

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


Re: [Tutor] Can't find error :-(

2011-08-28 Thread Lisi
On Sunday 28 August 2011 13:01:38 Peter Otten wrote:
> Lisi wrote:
> > Here is the error message:
> > /usr/local/bin/AddressBook: line 6: syntax error near unexpected token
> > `(' /usr/local/bin/AddressBook: line 6: `name = raw_input("Type the Name
> > - leave blank to finish")'
> >
> > (sorry KMail wrapped it.)
> >
> > Here is what I typed:
> > name = raw_input("Type the Name - leave blank to finish")
> >
> > Here is what I was copying:
> > name = raw_input("Type the Name - leave blank to finish")
> >
> > Help!!   All three are copied and pasted from their relevant places to
> > eliminate mis-copying at this stage.  What have I miscopied??  The
> > original and my copy look to me *exactly* the same.
>
> That's an error message by your shell. You made your script executable, but
> forgot to add something like
>
> #!/usr/bin/env python
>
> as its first line.
>
> NB. it's quite common that an error originates before the place where the
> interpreter complains; it would have been a good idea to post the five
> lines before the explicitly erroneous one, too.

Thanks to both.  I opened up the file to try retyping the ", although I had in 
fact typed it - the copying and pasting was into the email to ensure that I 
did not introduce any errors at that stage.  But looking at the file, I 
realised that I had forgotten my shebang line - and had put it right before I 
read this answer.  

I am very grateful to you both.  I had realised that the error might be 
somewhere other than where it is indicated because Alan had already told me 
so - but had not understood that it could be, not only in a different place, 
but quite a way away.  Not even when I put this right.  So thanks for that, 
Peter, and thank you again, both of you.

For future reference, how would I set about changing the encoding for just one 
character or file?  I don't really want to change the encoding I use system 
wide.

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


Re: [Tutor] Can't find error :-(

2011-08-28 Thread R. Alan Monroe

> /usr/local/bin/AddressBook: line 6: syntax error near unexpected token `('
> /usr/local/bin/AddressBook: line 6: `name = raw_input("Type the Name - leave 
> blank to finish")'

Were you accidentally trying a python 3 tutorial when your machine
only has python 2 installed (or vice versa)?

Alan

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


Re: [Tutor] Can't find error :-(

2011-08-28 Thread Peter Otten
Lisi wrote:

> Here is the error message:
> /usr/local/bin/AddressBook: line 6: syntax error near unexpected token `('
> /usr/local/bin/AddressBook: line 6: `name = raw_input("Type the Name -
> leave blank to finish")'
> 
> (sorry KMail wrapped it.)
> 
> Here is what I typed:
> name = raw_input("Type the Name - leave blank to finish")
> 
> Here is what I was copying:
> name = raw_input("Type the Name - leave blank to finish")
> 
> Help!!   All three are copied and pasted from their relevant places to
> eliminate mis-copying at this stage.  What have I miscopied??  The
> original and my copy look to me *exactly* the same.

That's an error message by your shell. You made your script executable, but 
forgot to add something like

#!/usr/bin/env python

as its first line.

NB. it's quite common that an error originates before the place where the 
interpreter complains; it would have been a good idea to post the five lines 
before the explicitly erroneous one, too.

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


Re: [Tutor] Can't find error :-(

2011-08-28 Thread James Thornton
If you copied and pasted the line, it's possible the quote marks are
of the wrong encoding -- try deleting the quote marks and adding them
back.

On Sun, Aug 28, 2011 at 6:34 AM, Lisi  wrote:
> Here is the error message:
> /usr/local/bin/AddressBook: line 6: syntax error near unexpected token `('
> /usr/local/bin/AddressBook: line 6: `name = raw_input("Type the Name - leave
> blank to finish")'
>
> (sorry KMail wrapped it.)
>
> Here is what I typed:
> name = raw_input("Type the Name - leave blank to finish")
>
> Here is what I was copying:
> name = raw_input("Type the Name - leave blank to finish")
>
> Help!!   All three are copied and pasted from their relevant places to
> eliminate mis-copying at this stage.  What have I miscopied??  The original
> and my copy look to me *exactly* the same.
>
> Lisi
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Bulbflow: A Python framework for graph databases (http://bulbflow.com)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Can't find error :-(

2011-08-28 Thread Lisi
Here is the error message:
/usr/local/bin/AddressBook: line 6: syntax error near unexpected token `('
/usr/local/bin/AddressBook: line 6: `name = raw_input("Type the Name - leave 
blank to finish")'

(sorry KMail wrapped it.)

Here is what I typed:
name = raw_input("Type the Name - leave blank to finish")

Here is what I was copying:
name = raw_input("Type the Name - leave blank to finish")

Help!!   All three are copied and pasted from their relevant places to 
eliminate mis-copying at this stage.  What have I miscopied??  The original 
and my copy look to me *exactly* the same.

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