[Tutor] find() problem

2010-08-24 Thread Roelof Wobben

Hello, 

 

I have this exercise :

 

Now rewrite the count_letters function so that instead of traversing the 
string, it repeatedly calls find (the version from Optional parameters), with 
the optional third parameter to locate new occurences of the letter being 
counted.

 

 

And I have this solution :

 

'''
def find(strng, ch, start, step=1):
index = start
while 0 <= index < len(strng):
if strng[index] == ch:
return index 
index += step
return -1

 
fruit=""
letter=""
fruit= raw_input("Enter a sort of fruit: ")
letter = raw_input("Enter the character which must be counted: ")
start=0 
aantal=0
while 0 <=start < len(fruit):
x=find (fruit,letter, start)
aantal +=1
start=x
print "De letter", letter , "komt", aantal , "maal voor in het woord", fruit

 

But it looks like it's in a indefinitive loop.

What went wrong here ?

 

I work with Python 2.7 on a Win7 machine.

 

Roelof

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


[Tutor] FW: find() problem

2010-08-24 Thread Roelof Wobben


 


From: rwob...@hotmail.com
To: obe...@gmail.com
Subject: RE: [Tutor] find() problem
Date: Tue, 24 Aug 2010 12:25:24 +





 
> Date: Tue, 24 Aug 2010 08:09:54 -0400
> Subject: Re: [Tutor] find() problem
> From: obe...@gmail.com
> To: rwob...@hotmail.com
> 
> > I will try again.
> >
> > def find(strng, ch, start, step=1):
> > index = start
> The problem lies here, if you do a print on index, it never gets past
> the first index of the number, so
> your while loop below goes into an infinite loop.
> 
> For example:
> find('starfruit','t',0) <- First time will return 1
> find('starfruit','t',1) <- Still returns 1. Infinite loop
> 
> > while 0 <= index < len(strng):
> > if strng[index] == ch:
> > return index
> > index += step
> > return -1
> >
> > fruit=""
> > letter=""
> > fruit= raw_input("Enter a sort of fruit: ")
> > letter = raw_input("Enter the character which must be counted: ")
> > start=0
> > aantal=0
> > while 0 <=start < len(fruit):
> > x=find (fruit,letter, start)
> > aantal +=1
> > start=x
> > print "De letter", letter , "komt", aantal , "maal voor in het woord", fruit
> >
> 
> HTH,
> Tino

Hello, 
 
Your right. index get never past 1 
But in my opinion when something is found then x will be the place where the 
character is be found.
After that the counter is increased and start gets the value of x.
So start should change.
 
Now finding out why this is not happening.
 
But thanks for the help.
 
Roelof
 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: find() problem

2010-08-24 Thread Evert Rol
> > > def find(strng, ch, start, step=1):
> > > index = start
> > The problem lies here, if you do a print on index, it never gets past
> > the first index of the number, so
> > your while loop below goes into an infinite loop.
> > 
> > For example:
> > find('starfruit','t',0) <- First time will return 1
> > find('starfruit','t',1) <- Still returns 1. Infinite loop
> > 
> > > while 0 <= index < len(strng):
> > > if strng[index] == ch:
> > > return index
> > > index += step
> > > return -1
> > >
> > > fruit=""
> > > letter=""
> > > fruit= raw_input("Enter a sort of fruit: ")
> > > letter = raw_input("Enter the character which must be counted: ")
> > > start=0
> > > aantal=0
> > > while 0 <=start < len(fruit):
> > > x=find (fruit,letter, start)
> > > aantal +=1
> > > start=x
> > > print "De letter", letter , "komt", aantal , "maal voor in het woord", 
> > > fruit
> > >
> > 
> > HTH,
> > Tino
> 
> Hello, 
>  
> Your right. index get never past 1 
> But in my opinion when something is found then x will be the place where the 
> character is be found.
> After that the counter is increased and start gets the value of x.
> So start should change.
>  
> Now finding out why this is not happening.

You're including the previous found index; so it will just find the character 
at that same index again. You need to start the next search one index further 
down the word.


>  
> But thanks for the help.
>  
> Roelof
>  
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


[Tutor] Memory error for list creation

2010-08-24 Thread Triantafyllos Gkikopoulos
Hi,
 
 I am looking for an alternative to:
 


Please consider the environment. Do you really need to print this email? 


 
>>> listx=[[[] for k in range(ds)] for j in range(i)]
 
as right now I am getting a Memory error on this, I tried this also on a 
cluster node with something like 16GB of memory and it didn't solve the problem.
 
listx is subsequently used:
 
>>> for x in something:
>>>  for y in x:
>>>   listx[ii][y[1]].append(y[0])
>>>  ii+=1
 
 
 
 For reference values for k range from 300 -1200 and for i ~5000.
 
 
I though about using scipy/numpy array but then I wouldn't be able to have the 
flexibility of using append or not having to predefine the size.
 
 
 Thanks
 

The University of Dundee is a registered Scottish charity, No: SC015096
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: find() problem

2010-08-24 Thread Roelof Wobben


 

> Subject: Re: [Tutor] FW: find() problem
> From: evert@gmail.com
> Date: Tue, 24 Aug 2010 17:03:07 +0200
> CC: tutor@python.org
> To: rwob...@hotmail.com
> 
> > > > def find(strng, ch, start, step=1):
> > > > index = start
> > > The problem lies here, if you do a print on index, it never gets past
> > > the first index of the number, so
> > > your while loop below goes into an infinite loop.
> > > 
> > > For example:
> > > find('starfruit','t',0) <- First time will return 1
> > > find('starfruit','t',1) <- Still returns 1. Infinite loop
> > > 
> > > > while 0 <= index < len(strng):
> > > > if strng[index] == ch:
> > > > return index
> > > > index += step
> > > > return -1
> > > >
> > > > fruit=""
> > > > letter=""
> > > > fruit= raw_input("Enter a sort of fruit: ")
> > > > letter = raw_input("Enter the character which must be counted: ")
> > > > start=0
> > > > aantal=0
> > > > while 0 <=start < len(fruit):
> > > > x=find (fruit,letter, start)
> > > > aantal +=1
> > > > start=x
> > > > print "De letter", letter , "komt", aantal , "maal voor in het woord", 
> > > > fruit
> > > >
> > > 
> > > HTH,
> > > Tino
> > 
> > Hello, 
> > 
> > Your right. index get never past 1 
> > But in my opinion when something is found then x will be the place where 
> > the character is be found.
> > After that the counter is increased and start gets the value of x.
> > So start should change.
> > 
> > Now finding out why this is not happening.
> 
> You're including the previous found index; so it will just find the character 
> at that same index again. You need to start the next search one index further 
> down the word.


 

Hello Evert.
 
Stupid mistake.
I change start=x to start=x+1 
But still it's not working properly.
 
I can see that on this example.
 
Fruit : banaan
letter : a
 
That a is being found at 2,4,5 which is correct but the programm don't end.
So there's another error somewhere,
 
Roelof

 

> 
> 
> > 
> > But thanks for the help.
> > 
> > Roelof
> > 
> > ___
> > 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] Memory error for list creation

2010-08-24 Thread Wayne Werner
On Tue, Aug 24, 2010 at 9:54 AM, Triantafyllos Gkikopoulos <
t.gkikopou...@dundee.ac.uk> wrote:

>  Hi,
>
>  I am looking for an alternative to:
>
>
> 
> Please consider the environment. Do you really need to print this email?
>
>
>
> >>> listx=[[[] for k in range(ds)] for j in range(i)]
>
> as right now I am getting a Memory error on this, I tried this also on a
> cluster node with something like 16GB of memory and it didn't solve the
> problem.
>
>

Unless you're using Python 3.x, use xrange - because here you're creating i
copies of the list 0..ds-1.

If you're trying to create a matrix this isn't the most effective way to get
it, but if you want a jagged array then using xrange should help your memory
consumption.


> listx is subsequently used:
>
> >>> for x in something:
> >>>  for y in x:
> >>>   listx[ii][y[1]].append(y[0])
> >>>  ii+=1
>
>
>
>  For reference values for k range from 300 -1200 and for i ~5000.
>
>
> I though about using scipy/numpy array but then I wouldn't be able to have
> the flexibility of using append or not having to predefine the size.
>

If you need the ability that bad, you can convert the array to a list and
vice versa - of course those operations take time, so YMMV.

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


Re: [Tutor] FW: find() problem

2010-08-24 Thread Evert Rol
> > > > > def find(strng, ch, start, step=1):
> > > > > index = start
> > > > The problem lies here, if you do a print on index, it never gets past
> > > > the first index of the number, so
> > > > your while loop below goes into an infinite loop.
> > > > 
> > > > For example:
> > > > find('starfruit','t',0) <- First time will return 1
> > > > find('starfruit','t',1) <- Still returns 1. Infinite loop
> > > > 
> > > > > while 0 <= index < len(strng):
> > > > > if strng[index] == ch:
> > > > > return index
> > > > > index += step
> > > > > return -1

Why are you returning -1 here? 
-1 is a valid list index. 
And with your last change, it makes your main condition valid.


> > > > >
> > > > > fruit=""
> > > > > letter=""
> > > > > fruit= raw_input("Enter a sort of fruit: ")
> > > > > letter = raw_input("Enter the character which must be counted: ")
> > > > > start=0
> > > > > aantal=0
> > > > > while 0 <=start < len(fruit):
> > > > > x=find (fruit,letter, start)
> > > > > aantal +=1
> > > > > start=x
> > > > > print "De letter", letter , "komt", aantal , "maal voor in het 
> > > > > woord", fruit
> > > > >
> > > > 
> > > > HTH,
> > > > Tino
> > > 
> > > Hello, 
> > > 
> > > Your right. index get never past 1 
> > > But in my opinion when something is found then x will be the place where 
> > > the character is be found.
> > > After that the counter is increased and start gets the value of x.
> > > So start should change.
> > > 
> > > Now finding out why this is not happening.
> > 
> > You're including the previous found index; so it will just find the 
> > character at that same index again. You need to start the next search one 
> > index further down the word.
> 
>  
> Hello Evert.
>  
> Stupid mistake.
> I change start=x to start=x+1 
> But still it's not working properly.
>  
> I can see that on this example.
>  
> Fruit : banaan
> letter : a
>  
> That a is being found at 2,4,5 which is correct but the programm don't end.
> So there's another error somewhere,
>  
> Roelof
>  
> > 
> > 
> > > 
> > > But thanks for the help.
> > > 
> > > Roelof
> > > 
> > > ___
> > > Tutor maillist - Tutor@python.org
> > > To unsubscribe or change subscription options:
> > > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Adding all numbers in a file or list

2010-08-24 Thread Nitin Das
alternatively you can use the lambda , reduce function for summing up all
the numbers in a list for e.g:-

lis = [1,2,3,4,5]

p = reduce(lambda x,y : x+y, lis)

p will have the value = 15.


--nitin

On Mon, Aug 23, 2010 at 9:05 PM, aug dawg  wrote:

> Oh okay, sorry about that.
>
> Thanks for the help!
>
>
>
> On Mon, Aug 23, 2010 at 11:33 AM, Sander Sweers 
> wrote:
>
>> On 23 August 2010 17:24, aug dawg  wrote:
>> > So it's sum(list_name) ?
>>
>> Correct, but it is not limited to lists. Any itterable with
>> ints/floats will do, for example a tuple is also accepted.
>>
>> Greets
>> Sander
>>
>> PS: Please use reply to all so others on this list may benefit from
>> the questions/answers ;-)
>>
>
>
> ___
> 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] Adding all numbers in a file or list

2010-08-24 Thread Shashwat Anand
On Tue, Aug 24, 2010 at 9:53 PM, Nitin Das  wrote:

> alternatively you can use the lambda , reduce function for summing up all
> the numbers in a list for e.g:-
>
> lis = [1,2,3,4,5]
>
> p = reduce(lambda x,y : x+y, lis)
>
> p will have the value = 15.
>

Another approach,

>>> lis = [1,2,3,4,5]
>>> reduce(operator.add, lis)
15

However use sum() for this, which is the most obvious way to do it.


>
> --nitin
>
> On Mon, Aug 23, 2010 at 9:05 PM, aug dawg  wrote:
>
>> Oh okay, sorry about that.
>>
>> Thanks for the help!
>>
>>
>>
>> On Mon, Aug 23, 2010 at 11:33 AM, Sander Sweers 
>> wrote:
>>
>>> On 23 August 2010 17:24, aug dawg  wrote:
>>> > So it's sum(list_name) ?
>>>
>>> Correct, but it is not limited to lists. Any itterable with
>>> ints/floats will do, for example a tuple is also accepted.
>>>
>>> Greets
>>> Sander
>>>
>>> PS: Please use reply to all so others on this list may benefit from
>>> the questions/answers ;-)
>>>
>>
>>
>> ___
>> 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
>
>


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


Re: [Tutor] FW: find() problem

2010-08-24 Thread Roelof Wobben

Hello, 

 

I found it.

This one does the trick :

 

def find(strng, ch, start, step=1):
index=start
while 0 <= index < len(strng):
if strng[index] == ch:
  return index 
index += step
return -2


fruit=""
letter=""
fruit= raw_input("Enter a sort of fruit: ")
letter = raw_input("Enter the character which must be counted: ")
index=1
aantal=0
while 0 <=index < len(fruit):
x=find (fruit,letter, index)
index=x+1
if x<>-2 :
aantal=aantal+1
print "De letter", letter , "komt", aantal , "maal voor in het woord", fruit

 

Roelof


 
> Subject: Re: [Tutor] FW: find() problem
> From: evert@gmail.com
> Date: Tue, 24 Aug 2010 17:44:22 +0200
> CC: tutor@python.org
> To: rwob...@hotmail.com
> 
> > > > > > def find(strng, ch, start, step=1):
> > > > > > index = start
> > > > > The problem lies here, if you do a print on index, it never gets past
> > > > > the first index of the number, so
> > > > > your while loop below goes into an infinite loop.
> > > > > 
> > > > > For example:
> > > > > find('starfruit','t',0) <- First time will return 1
> > > > > find('starfruit','t',1) <- Still returns 1. Infinite loop
> > > > > 
> > > > > > while 0 <= index < len(strng):
> > > > > > if strng[index] == ch:
> > > > > > return index
> > > > > > index += step
> > > > > > return -1
> 
> Why are you returning -1 here? 
> -1 is a valid list index. 
> And with your last change, it makes your main condition valid.
> 
> 
> > > > > >
> > > > > > fruit=""
> > > > > > letter=""
> > > > > > fruit= raw_input("Enter a sort of fruit: ")
> > > > > > letter = raw_input("Enter the character which must be counted: ")
> > > > > > start=0
> > > > > > aantal=0
> > > > > > while 0 <=start < len(fruit):
> > > > > > x=find (fruit,letter, start)
> > > > > > aantal +=1
> > > > > > start=x
> > > > > > print "De letter", letter , "komt", aantal , "maal voor in het 
> > > > > > woord", fruit
> > > > > >
> > > > > 
> > > > > HTH,
> > > > > Tino
> > > > 
> > > > Hello, 
> > > > 
> > > > Your right. index get never past 1 
> > > > But in my opinion when something is found then x will be the place 
> > > > where the character is be found.
> > > > After that the counter is increased and start gets the value of x.
> > > > So start should change.
> > > > 
> > > > Now finding out why this is not happening.
> > > 
> > > You're including the previous found index; so it will just find the 
> > > character at that same index again. You need to start the next search one 
> > > index further down the word.
> > 
> > 
> > Hello Evert.
> > 
> > Stupid mistake.
> > I change start=x to start=x+1 
> > But still it's not working properly.
> > 
> > I can see that on this example.
> > 
> > Fruit : banaan
> > letter : a
> > 
> > That a is being found at 2,4,5 which is correct but the programm don't end.
> > So there's another error somewhere,
> > 
> > Roelof
> > 
> > > 
> > > 
> > > > 
> > > > But thanks for the help.
> > > > 
> > > > Roelof
> > > > 
> > > > ___
> > > > Tutor maillist - Tutor@python.org
> > > > To unsubscribe or change subscription options:
> > > > http://mail.python.org/mailman/listinfo/tutor
> > > 
> > 
> > ___
> > Tutor maillist - Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: find() problem

2010-08-24 Thread Evert Rol
> I found it.

Good.
Few generic comments nonetheless, just for the fun of it ;-).

> This one does the trick :
>  
> def find(strng, ch, start, step=1):
> index=start
> while 0 <= index < len(strng):
> if strng[index] == ch:
>   return index 
> index += step
> return -2

You can actually use 'return index'. When you reach the end of the function, 
index == len(strng), which would be invalid in your main while condition, and 
the +1 would still keep it that way.
But read on if you want to keep the return -1 from the book.

> 
> fruit=""
> letter=""
> fruit= raw_input("Enter a sort of fruit: ")
> letter = raw_input("Enter the character which must be counted: ")
> index=1
> aantal=0
> while 0 <=index < len(fruit):
> x=find (fruit,letter, index)

You can check for x == -1 here (if you have the old find() from the book), and 
then break out of the loop. Provided the book has already dealt with the break 
statement. If you use break, your while condition can simply be 'while True:'

> index=x+1
> if x<>-2 :

Don't use <>, but != instead. 
If you ever use Python 3, it doesn't work anymore: 
http://docs.python.org/release/3.0.1/whatsnew/3.0.html#removed-syntax


> aantal=aantal+1
> print "De letter", letter , "komt", aantal , "maal voor in het woord", fruit
>  
> Roelof
> 
> > 
> > > > > > > def find(strng, ch, start, step=1):
> > > > > > > index = start
> > > > > > The problem lies here, if you do a print on index, it never gets 
> > > > > > past
> > > > > > the first index of the number, so
> > > > > > your while loop below goes into an infinite loop.
> > > > > > 
> > > > > > For example:
> > > > > > find('starfruit','t',0) <- First time will return 1
> > > > > > find('starfruit','t',1) <- Still returns 1. Infinite loop
> > > > > > 
> > > > > > > while 0 <= index < len(strng):
> > > > > > > if strng[index] == ch:
> > > > > > > return index
> > > > > > > index += step
> > > > > > > return -1
> > 
> > Why are you returning -1 here? 
> > -1 is a valid list index. 
> > And with your last change, it makes your main condition valid.
> > 
> > 
> > > > > > >
> > > > > > > fruit=""
> > > > > > > letter=""
> > > > > > > fruit= raw_input("Enter a sort of fruit: ")
> > > > > > > letter = raw_input("Enter the character which must be counted: ")
> > > > > > > start=0
> > > > > > > aantal=0
> > > > > > > while 0 <=start < len(fruit):
> > > > > > > x=find (fruit,letter, start)
> > > > > > > aantal +=1
> > > > > > > start=x
> > > > > > > print "De letter", letter , "komt", aantal , "maal voor in het 
> > > > > > > woord", fruit
> > > > > > >
> > > > > > 
> > > > > > HTH,
> > > > > > Tino
> > > > > 
> > > > > Hello, 
> > > > > 
> > > > > Your right. index get never past 1 
> > > > > But in my opinion when something is found then x will be the place 
> > > > > where the character is be found.
> > > > > After that the counter is increased and start gets the value of x.
> > > > > So start should change.
> > > > > 
> > > > > Now finding out why this is not happening.
> > > > 
> > > > You're including the previous found index; so it will just find the 
> > > > character at that same index again. You need to start the next search 
> > > > one index further down the word.
> > > 
> > > 
> > > Hello Evert.
> > > 
> > > Stupid mistake.
> > > I change start=x to start=x+1 
> > > But still it's not working properly.
> > > 
> > > I can see that on this example.
> > > 
> > > Fruit : banaan
> > > letter : a
> > > 
> > > That a is being found at 2,4,5 which is correct but the programm don't 
> > > end.
> > > So there's another error somewhere,
> > > 
> > > Roelof
> > > 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Databases in Python

2010-08-24 Thread aug dawg
The other day, I wrote a little database just to fiddle around, but when I
try to run it it says that it has an unexpected indent. From what I can
tell, it doesn't. Here's the code. I'm using SPE.

database = []
datafile = open('/home/~/the-db/data')
for line in datafile:
database.append(line)

while tf2 != True:
command = raw_input("Enter a command. >> ")

while tf != True:
if "add" in command:
adder = raw_input("What to add? >> ")
data_base.append(adder)
if "no more" in adder:
tf = True

if "read" in command:
print(database)
 if "search" in command:
searcher = raw_input("Enter term to search >> ")
if searcher in database:
# Figure this out.
 if "exit database" in command:
print "Bye!"
sys.exit()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Databases in Python

2010-08-24 Thread aug dawg
It says that it's on line 25, on the print("Bye!").
Forgot to say that.


On Tue, Aug 24, 2010 at 12:44 PM, aug dawg  wrote:

> The other day, I wrote a little database just to fiddle around, but when I
> try to run it it says that it has an unexpected indent. From what I can
> tell, it doesn't. Here's the code. I'm using SPE.
>
> database = []
> datafile = open('/home/~/the-db/data')
> for line in datafile:
> database.append(line)
>
> while tf2 != True:
> command = raw_input("Enter a command. >> ")
>
> while tf != True:
>  if "add" in command:
> adder = raw_input("What to add? >> ")
>  data_base.append(adder)
> if "no more" in adder:
> tf = True
>
> if "read" in command:
> print(database)
>  if "search" in command:
> searcher = raw_input("Enter term to search >> ")
>  if searcher in database:
> # Figure this out.
>  if "exit database" in command:
> print "Bye!"
> sys.exit()
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Databases in Python

2010-08-24 Thread Walter Prins
On 24 August 2010 17:47, aug dawg  wrote:

> if searcher in database:
>> # Figure this out.
>>  if "exit database" in command:
>> print "Bye!"
>> sys.exit()
>>
>
The first thing that caught my eye was the "#figure me out" line -- python
is expecting a statement there, a comment doesn't count.  You can use "pass"
for the time being.  (See
http://docs.python.org/reference/simple_stmts.html#grammar-token-pass_stmt)

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


Re: [Tutor] Databases in Python

2010-08-24 Thread Che M

> The other day, I wrote a little database just to fiddle around, 
> but when I try to run it it says that it has an unexpected indent. 
> From what I can tell, it doesn't. Here's the code. I'm using SPE.

In the future, you should copy/paste error message you get into
your email.  This way it indicates to others the exact problem you
had.

One thing I noticed that is wrong in your code is this line:

if searcher in database:# Figure this out.  
The comment doesn't count as a line of interpretable code, so
you have nothing in this code block.  If you want to do that, use
pass, like:

if searcher in database:pass
# Figure this out.  
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Databases in Python

2010-08-24 Thread Marc Tompkins
On Tue, Aug 24, 2010 at 12:44 PM, aug dawg  wrote:

>  if searcher in database:
>> # Figure this out.
>>
>> You need some sort of actual Python statement there as a placeholder -
even just "print()".

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


Re: [Tutor] Databases in Python

2010-08-24 Thread aug dawg
Oh yeah. That was just a comment that I forgot to take out.


On Tue, Aug 24, 2010 at 1:01 PM, Walter Prins  wrote:

>
>
> On 24 August 2010 17:47, aug dawg  wrote:
>
>>  if searcher in database:
>>>  # Figure this out.
>>>  if "exit database" in command:
>>> print "Bye!"
>>> sys.exit()
>>>
>>
> The first thing that caught my eye was the "#figure me out" line -- python
> is expecting a statement there, a comment doesn't count.  You can use "pass"
> for the time being.  (See
> http://docs.python.org/reference/simple_stmts.html#grammar-token-pass_stmt
> )
>
> Walter
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Databases in Python

2010-08-24 Thread aug dawg
It's not catching that, but I haven't gotten there with the bugs yet. One
more thing I can't figure out.

line 11
select-db = raw_input("Which database to add to? >> ")
SyntaxError: can't assign to operator

I think it might be the >> at the end, but when I try it in the Python
interpreter, it works fine.

On Tue, Aug 24, 2010 at 1:07 PM, Marc Tompkins wrote:

> On Tue, Aug 24, 2010 at 12:44 PM, aug dawg  wrote:
>
>>  if searcher in database:
>>> # Figure this out.
>>>
>>> You need some sort of actual Python statement there as a placeholder -
> even just "print()".
>
> --
> www.fsrtechnologies.com
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: find() problem

2010-08-24 Thread Evert Rol
>>> I found it.
>> 
>> Good.
>> Few generic comments nonetheless, just for the fun of it ;-).
>> 
>>> This one does the trick :
>>> 
>>> def find(strng, ch, start, step=1):
>>> index=start
>>> while 0 <= index < len(strng):
>>> if strng[index] == ch:
>>> return index 
>>> index += step
>>> return -2
>> 
>> You can actually use 'return index'. When you reach the end of the function, 
>> index == len(strng), which would be invalid in your main while condition, 
>> and the +1 would still keep it that way.
>> But read on if you want to keep the return -1 from the book.
> 
> 
> Hello Evert, 
> 
> I don't get this.
> If you reach the end of the function , index==len(string) , then this loops 
> end. 
> index does then have the value of the last found lettter. So why +1 would it 
> keep it that way.

In your function, you keep adding step to index. If you won't find a match for 
a letter anymore, index reaches len(strng) and thus you break out of the while 
loop. Thus you continue to the return statement, and you return index (which 
equals len(strng) now).
Remember that len(strng) is the length of the string, but because Python's 
indices are zero-based, that does *not* equal the index of the last element: 
fruit[len(fruit)] will give an IndexError. So index does not have the value of 
the last found letter (actually, it never has, because it is an index, not a 
letter). 
In your main while loop, now, index == len(fruit), thus the condition fails. If 
you add one, you would get '0 <= len(fruit)+1 < len(fruit)', (index substituted 
with "len(fruit)"), which just as easily fails.

But if you don't get, better not use it; that generally leads to bugs.


> 
>> 
>>> 
>>> fruit=""
>>> letter=""
>>> fruit= raw_input("Enter a sort of fruit: ")
>>> letter = raw_input("Enter the character which must be counted: ")
>>> index=1
>>> aantal=0
>>> while 0 <=index < len(fruit):
>>> x=find (fruit,letter, index)
>> 
>> You can check for x == -1 here (if you have the old find() from the book), 
>> and then break out of the loop. Provided the book has already dealt with the 
>> break statement. If you use break, your while condition can simply be 'while 
>> True:'
>> 
>>> index=x+1
>>> if x<>-2 :
> 
> I don't thought about that but it is also a possibility.
> 
>> 
>> Don't use <>, but != instead. 
>> If you ever use Python 3, it doesn't work anymore: 
>> http://docs.python.org/release/3.0.1/whatsnew/3.0.html#removed-syntax
> 
> 
> Oke,  Point taken.
> 
> 



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


[Tutor] Retriving previous user inputs in a gui

2010-08-24 Thread Karim


Hello All,

I am figuring this out. I want a sort of file who store values entered 
previously in a gui.
Th e next time the user launch his gui in the same directory the gui 
load the previous
value from this file. Is there any modules in Tkinter for that? I 
suppose the file could
be in xml format or whatever? pyGTK solution is welcome too! Even if I 
prefered to use

the standard package.

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


Re: [Tutor] find() problem

2010-08-24 Thread Alan Gauld


"Roelof Wobben"  wrote


But it looks like it's in a indefinitive loop.

What went wrong here ?


When debugging this kind of thing insert a raw_input() statement
in the loop(to force a pause)  and just before it print out the key
variables. That way you can see what is happening and what is
not working the way you expect.

In this case you could print out the start, index and character 
values.


HTH,


--
Alan Gauld
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] Databases in Python

2010-08-24 Thread christopher . henk
aug dawg wrote on 08/24/2010 01:13:01 PM:

> It's not catching that, but I haven't gotten there with the bugs yet. 
One more thing I can't figure out.
> 
> line 11
> select-db = raw_input("Which database to add to? >> ")
> SyntaxError: can't assign to operator
> 
> I think it might be the >> at the end, but when I try it in the Python 
interpreter, it works fine.
> 
The error is triggered by your variable name.  You can't use a dash, 
Python is interpreting that as a minus sign.  You can use the underscore 
"_" instead.

more here:
http://www.pasteur.fr/formation/infobio/python/ch02s03.html
http://docs.python.org/reference/lexical_analysis.html#identifiers


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


Re: [Tutor] Databases in Python

2010-08-24 Thread aug dawg
Now it says that the variable adder is not defined. Does anyone know about
this?


On Tue, Aug 24, 2010 at 1:40 PM,
wrote:

>
> aug dawg wrote on 08/24/2010 01:13:01 PM:
>
>
> > It's not catching that, but I haven't gotten there with the bugs yet. One
> more thing I can't figure out.
>
> >
> > line 11
> > select-db = raw_input("Which database to add to? >> ")
> > SyntaxError: can't assign to operator
> >
> > I think it might be the >> at the end, but when I try it in the Python
> interpreter, it works fine.
> >
> The error is triggered by your variable name.  You can't use a dash, Python
> is interpreting that as a minus sign.  You can use the underscore "_"
> instead.
>
> more here:
> http://www.pasteur.fr/formation/infobio/python/ch02s03.html
> http://docs.python.org/reference/lexical_analysis.html#identifiers
>
>
> Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Databases in Python

2010-08-24 Thread Alan Gauld


"aug dawg"  wrote


   select-db = raw_input("Which database to add to? >> ")
SyntaxError: can't assign to operator

I think it might be the >> at the end, but when I try it in the 
Python

interpreter, it works fine.


You have a minus sign in your variable name - at least thats
how Python sees it...


--
Alan Gauld
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] Retriving previous user inputs in a gui

2010-08-24 Thread Alan Gauld

"Karim"  wrote

I am figuring this out. I want a sort of file who store values 
entered previously in a gui.


Thats easy enough - have you succeeded with this bit - check the
file with a text editor...

Th e next time the user launch his gui in the same directory the gui 
load the previous value from this file.


Again thats pretty easy, did you get this working - create the file
with a text editior?


Is there any modules in Tkinter for that?


No, Tkinter is for building GUIs. Python core language includes
functions for reading and writing to files. Can you do this outside a 
GUI?


Write a program to read the users name and save it to a file.
Write a program to read the file and print the name it finds.
Write a program that says Hello  if it finds a name and asks for 
a name if it doesn't.


Now translate that to your GUI.


suppose the file could
be in xml format or whatever?


Yes, it could be in whatever. CSV, XML, ConfigParser or even plain 
text.


HTH,

--
Alan Gauld
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] Retriving previous user inputs in a gui

2010-08-24 Thread Karim


Thank you Alan for your answer.
In fact I want to do it in python format.
I want to source it (I will declare it each
input as a python variable).
I don't want to parse it. I just want to source it
like an external file in bash for example.
Is there a way to not use string evaluation. But really load it
 as python setting file inside python program.

PS: ConfigParser, hum very good , this could be of use for other part...

Karim

On 08/24/2010 08:03 PM, Alan Gauld wrote:

"Karim"  wrote

I am figuring this out. I want a sort of file who store values 
entered previously in a gui.


Thats easy enough - have you succeeded with this bit - check the
file with a text editor...

Th e next time the user launch his gui in the same directory the gui 
load the previous value from this file.


Again thats pretty easy, did you get this working - create the file
with a text editior?


Is there any modules in Tkinter for that?


No, Tkinter is for building GUIs. Python core language includes
functions for reading and writing to files. Can you do this outside a 
GUI?


Write a program to read the users name and save it to a file.
Write a program to read the file and print the name it finds.
Write a program that says Hello  if it finds a name and asks for 
a name if it doesn't.


Now translate that to your GUI.


suppose the file could
be in xml format or whatever?


Yes, it could be in whatever. CSV, XML, ConfigParser or even plain text.

HTH,



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


Re: [Tutor] Databases in Python

2010-08-24 Thread christopher . henk
aug dawg  wrote on 08/24/2010 01:55:14 PM:

> Now it says that the variable adder is not defined. Does anyone know 
about this?
> 
It is best if you send the full error message, it helps pinpoint the 
problem.
my copy of your code was:
database = []
datafile = open('/home/~/the-db/data')
for line in datafile:
database.append(line)

while tf2 != True:
command = raw_input("Enter a command. >> ")

while tf != True:
if "add" in command:
adder = raw_input("What to add? >> ")
data_base.append(adder)
if "no more" in adder:
tf = True

if "read" in command:
print(database)
if "search" in command:
searcher = raw_input("Enter term to search >> ")
if searcher in database:
# Figure this out.
if "exit database" in command:
print "Bye!"
sys.exit()

I am not sure what you typed in as your command or how your source was 
formatted (the tabs did not come through to me) so I cannot say for sure.
If "add" was not your command, adder does not get defined. 
My guess is that you typed in a different command so you skipped the 
assignment but are checking for "no more" in adder, or attempting to 
append adder.  This would trigger that error.  Did you get the "What to 
add? >>" message?

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


Re: [Tutor] Retriving previous user inputs in a gui

2010-08-24 Thread Karim


Ok I find a solution (need to test it) that seems correct:

Suppose we have a python file mySourceFile with this setting:

EntryTextName = "myName"
EntryTextMail   = "mym...@gmail.com"

In the calling script or main python file we could define a function 
sourceConfigGui as follow:


def sourceConfigGui(mySourceFile,path_to_mysourcefile):

import mySourceFile
import sys
sys.path.append(path_to_mysourcefile)


If you have any other solution to source a external py file let me know.

Regards



On 08/24/2010 08:21 PM, Karim wrote:


Thank you Alan for your answer.
In fact I want to do it in python format.
I want to source it (I will declare it each
input as a python variable).
I don't want to parse it. I just want to source it
like an external file in bash for example.
Is there a way to not use string evaluation. But really load it
 as python setting file inside python program.

PS: ConfigParser, hum very good , this could be of use for other part...

Karim

On 08/24/2010 08:03 PM, Alan Gauld wrote:

"Karim"  wrote

I am figuring this out. I want a sort of file who store values 
entered previously in a gui.


Thats easy enough - have you succeeded with this bit - check the
file with a text editor...

Th e next time the user launch his gui in the same directory the gui 
load the previous value from this file.


Again thats pretty easy, did you get this working - create the file
with a text editior?


Is there any modules in Tkinter for that?


No, Tkinter is for building GUIs. Python core language includes
functions for reading and writing to files. Can you do this outside a 
GUI?


Write a program to read the users name and save it to a file.
Write a program to read the file and print the name it finds.
Write a program that says Hello  if it finds a name and asks 
for a name if it doesn't.


Now translate that to your GUI.


suppose the file could
be in xml format or whatever?


Yes, it could be in whatever. CSV, XML, ConfigParser or even plain text.

HTH,



___
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] Databases in Python

2010-08-24 Thread aug dawg
Oh, stupid me forgot the error message. Well, for some odd reason it works
now, but there are still some issues. When I tried it just a few minutes
ago, it worked fine, it seemed to me.
If anyone have any tips, please let me know. Thanks everyone for the tips.

On Tue, Aug 24, 2010 at 2:24 PM,
wrote:

>
>
> aug dawg  wrote on 08/24/2010 01:55:14 PM:
>
>
> > Now it says that the variable adder is not defined. Does anyone know
> about this?
> >
> It is best if you send the full error message, it helps pinpoint the
> problem.
> my copy of your code was:
> database = []
> datafile = open('/home/~/the-db/data')
> for line in datafile:
> database.append(line)
>
> while tf2 != True:
> command = raw_input("Enter a command. >> ")
>
> while tf != True:
> if "add" in command:
> adder = raw_input("What to add? >> ")
> data_base.append(adder)
> if "no more" in adder:
> tf = True
>
> if "read" in command:
> print(database)
> if "search" in command:
> searcher = raw_input("Enter term to search >> ")
> if searcher in database:
> # Figure this out.
> if "exit database" in command:
> print "Bye!"
> sys.exit()
>
> I am not sure what you typed in as your command or how your source was
> formatted (the tabs did not come through to me) so I cannot say for sure.
> If "add" was not your command, adder does not get defined.
> My guess is that you typed in a different command so you skipped the
> assignment but are checking for "no more" in adder, or attempting to append
> adder.  This would trigger that error.  Did you get the "What to add? >>"
> message?
>
> Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Retriving previous user inputs in a gui

2010-08-24 Thread Karim


Correction indents disappear (sic !) and lines are inverted (my mistake too)
 :o):

def sourceConfigGui(mySourceFile,path_to_mysourcefile):
import sys
sys.path.append(path_to_mysourcefile)
import mySourceFile

Karim

On 08/24/2010 09:28 PM, Karim wrote:


Ok I find a solution (need to test it) that seems correct:

Suppose we have a python file mySourceFile with this setting:

EntryTextName = "myName"
EntryTextMail   = "mym...@gmail.com"

In the calling script or main python file we could define a function 
sourceConfigGui as follow:


def sourceConfigGui(mySourceFile,path_to_mysourcefile):

import mySourceFile
import sys
sys.path.append(path_to_mysourcefile)


If you have any other solution to source a external py file let me know.

Regards



On 08/24/2010 08:21 PM, Karim wrote:


Thank you Alan for your answer.
In fact I want to do it in python format.
I want to source it (I will declare it each
input as a python variable).
I don't want to parse it. I just want to source it
like an external file in bash for example.
Is there a way to not use string evaluation. But really load it
 as python setting file inside python program.

PS: ConfigParser, hum very good , this could be of use for other part...

Karim

On 08/24/2010 08:03 PM, Alan Gauld wrote:

"Karim"  wrote

I am figuring this out. I want a sort of file who store values 
entered previously in a gui.


Thats easy enough - have you succeeded with this bit - check the
file with a text editor...

Th e next time the user launch his gui in the same directory the 
gui load the previous value from this file.


Again thats pretty easy, did you get this working - create the file
with a text editior?


Is there any modules in Tkinter for that?


No, Tkinter is for building GUIs. Python core language includes
functions for reading and writing to files. Can you do this outside 
a GUI?


Write a program to read the users name and save it to a file.
Write a program to read the file and print the name it finds.
Write a program that says Hello  if it finds a name and asks 
for a name if it doesn't.


Now translate that to your GUI.


suppose the file could
be in xml format or whatever?


Yes, it could be in whatever. CSV, XML, ConfigParser or even plain 
text.


HTH,



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


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


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


Re: [Tutor] Retriving previous user inputs in a gui

2010-08-24 Thread Karim


after tests I get the following:

>>> import params
>>> dir(params)
['EntryTextMail', 'EntryTextName', '__builtins__', '__doc__', 
'__file__', '__name__', '__package__']

>>> params.EntryTextName
'myName'
>>> params.EntryTextMail
'mym...@gmail.com'

But the file to import should have '.py' extension (.py) (if 
there is a way to avoid that
I wanted to use a 'hidden' file kind of ".config" , I can create a 
dynamical link to that
file with .py then import it and at last delete the symbolic 
link). I should do a small class

for that to reuse it.

At first I tried to access it as variables instead of using 
.variable. I learnt something here!


Regards
Karim

On 08/24/2010 09:38 PM, Karim wrote:


Correction indents disappear (sic !) and lines are inverted (my 
mistake too)

 :o):

def sourceConfigGui(mySourceFile,path_to_mysourcefile):
import sys
sys.path.append(path_to_mysourcefile)
import mySourceFile

Karim

On 08/24/2010 09:28 PM, Karim wrote:


Ok I find a solution (need to test it) that seems correct:

Suppose we have a python file mySourceFile with this setting:

EntryTextName = "myName"
EntryTextMail   = "mym...@gmail.com"

In the calling script or main python file we could define a function 
sourceConfigGui as follow:


def sourceConfigGui(mySourceFile,path_to_mysourcefile):

import mySourceFile
import sys
sys.path.append(path_to_mysourcefile)


If you have any other solution to source a external py file let me know.

Regards



On 08/24/2010 08:21 PM, Karim wrote:


Thank you Alan for your answer.
In fact I want to do it in python format.
I want to source it (I will declare it each
input as a python variable).
I don't want to parse it. I just want to source it
like an external file in bash for example.
Is there a way to not use string evaluation. But really load it
 as python setting file inside python program.

PS: ConfigParser, hum very good , this could be of use for other 
part...


Karim

On 08/24/2010 08:03 PM, Alan Gauld wrote:

"Karim"  wrote

I am figuring this out. I want a sort of file who store values 
entered previously in a gui.


Thats easy enough - have you succeeded with this bit - check the
file with a text editor...

Th e next time the user launch his gui in the same directory the 
gui load the previous value from this file.


Again thats pretty easy, did you get this working - create the file
with a text editior?


Is there any modules in Tkinter for that?


No, Tkinter is for building GUIs. Python core language includes
functions for reading and writing to files. Can you do this outside 
a GUI?


Write a program to read the users name and save it to a file.
Write a program to read the file and print the name it finds.
Write a program that says Hello  if it finds a name and asks 
for a name if it doesn't.


Now translate that to your GUI.


suppose the file could
be in xml format or whatever?


Yes, it could be in whatever. CSV, XML, ConfigParser or even plain 
text.


HTH,



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


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


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


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


[Tutor] Controlling a device with ioctl's?

2010-08-24 Thread Joe Veldhuis
Hello to all. I'm working on writing a tool that will control a piece of 
hardware using ioctl's on its device node. Specifically, I'm trying to 
configure and tune a DVB-S receiver on Linux.

Just for starters, I want to try opening the frontend and setting the LNB 
voltage. An example in C:

###
/* all the normal #include's omitted */
#include 

fd = open("/dev/dvb/adapter1/frontend0", O_RDWR)
r = ioctl (fd, FE_SET_VOLTAGE, SEC_VOLTAGE_18);
if (r == -1)
perror ("ioctl FE_SET_VOLTAGE");
###

The relevant definitions in linux/dvb/frontend.h:

###
typedef enum fe_sec_voltage {
SEC_VOLTAGE_13,
SEC_VOLTAGE_18,
SEC_VOLTAGE_OFF
} fe_sec_voltage_t;
...
#define FE_SET_VOLTAGE _IO('o', 67)  /* fe_sec_voltage_t */
###

So, I wrote the following in Python:

###
import fcntl

fe_set_voltage = 67
sec_voltage_13 = 0
sec_voltage_18 = 1
sec_voltage_off = 2

fd = open("/dev/dvb/adapter1/frontend0", "wb")
fcntl.ioctl(fd, fe_set_voltage, sec_voltage_18)
###

This fails with "IOError: [Errno 95] Operation not supported".

Considering I'm not too good with C, and know absolutely /nothing/ about device 
drivers or kernel code, I'm certain I'm missing something simple and obvious. 
Anyone want to set me straight?

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


Re: [Tutor] FW: find() problem

2010-08-24 Thread Steven D'Aprano
On Wed, 25 Aug 2010 01:44:22 am Evert Rol wrote:

> Why are you returning -1 here?
> -1 is a valid list index.

So? str.find() does the same thing. It guarantees to only return 0 or 
positive indexes if it finds the substring, and only returns -1 to 
indicate not found.


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


Re: [Tutor] Controlling a device with ioctl's?

2010-08-24 Thread Sander Sweers
On 25 August 2010 00:04, Joe Veldhuis  wrote:
> Hello to all. I'm working on writing a tool that will control a piece of 
> hardware using ioctl's on its device node. Specifically, I'm trying to 
> configure and tune a DVB-S receiver on Linux.
>
> Just for starters, I want to try opening the frontend and setting the LNB 
> voltage. An example in C:

Maybe you can use the python v4l2 bindings from [1] as example how to
use it for dvb. Not used it or have any experience with anything like
this but it might help..

Greets
Sander

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


Re: [Tutor] Adding all numbers in a file or list

2010-08-24 Thread Steven D'Aprano
On Wed, 25 Aug 2010 02:23:10 am Nitin Das wrote:
> alternatively you can use the lambda , reduce function for summing up
> all the numbers in a list for e.g:-
>
> lis = [1,2,3,4,5]
> p = reduce(lambda x,y : x+y, lis)
>
> p will have the value = 15.

Sure, you *can* do this, by why would you re-invent the wheel like that? 
As an exercise to teach reduce, sure, or as a demonstration of lambda, 
or if you have to support Python 2.2 or older (but that's like four 
versions out of date!). sum() is really the only sensible way to do it 
these days.

But if you insist on using reduce like that, it will probably be much 
faster to do this:

import operator
reduce(operator.add, lis)


particularly on the older versions where sum() isn't available.




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


Re: [Tutor] Retriving previous user inputs in a gui

2010-08-24 Thread Alan Gauld


"Karim"  wrote


>>> import params
>>> dir(params)
['EntryTextMail', 'EntryTextName', '__builtins__', '__doc__',


But the file to import should have '.py' extension (.py) (if 
there is a way to avoid that I wanted to use a 'hidden' file kind of 
".config" ,


You can exec a file and you can read the file into a string as a 
variable
then exec the string. BUT doing this is a huge security risk since 
anyone
can put any kind of arbitrary code in your config file and you will 
blindly

execute it. That's why config files are generally not executable code
but some kind of data format - it's much safer and very little extra 
work.


At first I tried to access it as variables instead of using 
.variable. I learnt something here!


Any time you import a module you need to use the module name
to access its contents - or use the from moo import * format, but
that introduces even more risk!

I strongly recommend that you think again and use a data format
config file.

HTH,

--
Alan Gauld
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] Controlling a device with ioctl's?

2010-08-24 Thread Alan Gauld


"Joe Veldhuis"  wrote

 control a piece of hardware using ioctl's on its device node. 


I've never tried this from Python but


#include 

fd = open("/dev/dvb/adapter1/frontend0", O_RDWR)


Notice the fd - that means file descriptor not file pointer.
So ioctl takes a file descriptor in C and I assume the same 
in Python.



So, I wrote the following in Python:


fd = open("/dev/dvb/adapter1/frontend0", "wb")


So I suspect this might need to be

import os
fd = os.open(.)

But as I say I've never actually used ioctl in Python...

Alan G.


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


Re: [Tutor] Adding all numbers in a file or list

2010-08-24 Thread aug dawg
Got it. Thanks everyone!


On Tue, Aug 24, 2010 at 6:22 PM, Steven D'Aprano wrote:

> On Wed, 25 Aug 2010 02:23:10 am Nitin Das wrote:
> > alternatively you can use the lambda , reduce function for summing up
> > all the numbers in a list for e.g:-
> >
> > lis = [1,2,3,4,5]
> > p = reduce(lambda x,y : x+y, lis)
> >
> > p will have the value = 15.
>
> Sure, you *can* do this, by why would you re-invent the wheel like that?
> As an exercise to teach reduce, sure, or as a demonstration of lambda,
> or if you have to support Python 2.2 or older (but that's like four
> versions out of date!). sum() is really the only sensible way to do it
> these days.
>
> But if you insist on using reduce like that, it will probably be much
> faster to do this:
>
> import operator
> reduce(operator.add, lis)
>
>
> particularly on the older versions where sum() isn't available.
>
>
>
>
> --
> Steven D'Aprano
> ___
> 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] Controlling a device with ioctl's?

2010-08-24 Thread Joe Veldhuis
A bit more work than I expected, but at least I have an idea what to do now. 
I'm working on writing a DVB binding based on the V4L2 binding you mentioned, 
currently about 30% complete - I can query the card's status! :)

Thanks for the help so far, might post again if I run into more trouble.

-Joe

On Wed, 25 Aug 2010 00:21:12 +0200
Sander Sweers  wrote:

> Maybe you can use the python v4l2 bindings from [1] as example how to
> use it for dvb. Not used it or have any experience with anything like
> this but it might help..
> 
> Greets
> Sander
> 
> [1] http://pypi.python.org/pypi/v4l2
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python: can't open file 'ex1.py' : [Errno 2] No such file or directory

2010-08-24 Thread Carter Danforth
Hi everyone,

This is my first email to group - I'm just starting to pick up Python and
I'm going through the exercises in Zed Shaw's "Learn Python the Hard Way"
ebook. Anyhow, I can't seem to be executing any files in terminal for some
reason, in this case the file ex1.py:

C:\Users\Carter Danforth\python ex1.py
python: can't open file 'ex1.py': [Errno 2] No such file or directory

ex1.py is located in "pythonpractice" on my desktop and I've updated the
modules, here's the output from sys.path:

['', 'C:\\Windows\\system32\\python27.zip', 'C:\\Users\\Carter
Danforth\\Desktop\\pythonpractice', 'C:\\Python27\\DLLs',
'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk', 'C:\\Python27',
'C:\\Python27\\lib\\site-packages']

And the environmental variable is set to have python on my path:

C:\Users\Carter Danforth>python
Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on
win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

I'm not sure why I keep getting this error message and why I'm not able to
execute any .py files. Any help would be great appreciated.

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


Re: [Tutor] Retriving previous user inputs in a gui

2010-08-24 Thread Karim


Thanks Alan for you advice!

Our environment is secured because the program is only for internal use.
We are supporting electronic designers. Nobody at work will write delete
codes inside (I hope). But, sure, I will check configParser module. I wanted
a straight forward config file because for TCL/TK GUI .config file is 
already

existing in TCL format that executing same syntax '*set myvar myvar_value*'.
I wanted to recreate this mechanism for python gui.

Thanks to point out this security issue.
Is there any equivalent to JAVACC in python (or lex yacc) to create grammary
for config or format file?

Regards
Karim

On 08/25/2010 01:57 AM, Alan Gauld wrote:


"Karim"  wrote


>>> import params
>>> dir(params)
['EntryTextMail', 'EntryTextName', '__builtins__', '__doc__',


But the file to import should have '.py' extension (.py) (if 
there is a way to avoid that I wanted to use a 'hidden' file kind of 
".config" ,


You can exec a file and you can read the file into a string as a variable
then exec the string. BUT doing this is a huge security risk since anyone
can put any kind of arbitrary code in your config file and you will 
blindly

execute it. That's why config files are generally not executable code
but some kind of data format - it's much safer and very little extra 
work.


At first I tried to access it as variables instead of using 
.variable. I learnt something here!


Any time you import a module you need to use the module name
to access its contents - or use the from moo import * format, but
that introduces even more risk!

I strongly recommend that you think again and use a data format
config file.

HTH,



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


Re: [Tutor] python: can't open file 'ex1.py' : [Errno 2] No such file or directory

2010-08-24 Thread Karim


If it is in the sys.path you should import it: *import ex1*.
Then execute something like *ex1.main()* if you did a main().
Other python */ex.py* should world.

Regards
Karim

On 08/25/2010 05:22 AM, Carter Danforth wrote:

Hi everyone,

This is my first email to group - I'm just starting to pick up Python 
and I'm going through the exercises in Zed Shaw's "Learn Python the 
Hard Way" ebook. Anyhow, I can't seem to be executing any files in 
terminal for some reason, in this case the file ex1.py:


C:\Users\Carter Danforth\python ex1.py
python: can't open file 'ex1.py': [Errno 2] No such file or directory

ex1.py is located in "pythonpractice" on my desktop and I've updated 
the modules, here's the output from sys.path:


['', 'C:\\Windows\\system32\\python27.zip', 'C:\\Users\\Carter 
Danforth\\Desktop\\pythonpractice', 'C:\\Python27\\DLLs', 
'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 
'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 
'C:\\Python27\\lib\\site-packages']


And the environmental variable is set to have python on my path:

C:\Users\Carter Danforth>python
Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit 
(AMD64)] on win

32
Type "help", "copyright", "credits" or "license" for more information.
>>>

I'm not sure why I keep getting this error message and why I'm not 
able to execute any .py files. Any help would be great appreciated.


Carter


___
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] continuous running of a method

2010-08-24 Thread Nitin Das
The problem with this while loop is if your random value doesn't lie between
the mentioned range then ur 100% cpu would be utilized. The one thing u can
do is to sleep for some time lets say 0.5 sec after every while loop
iteration , in this case ur cpu utilization will go down.

--nitin

On Mon, Aug 23, 2010 at 8:21 PM, bob gailer  wrote:

>  On 8/23/2010 1:00 AM, Greg Bair wrote:
>
>> I have a method (I'll call it foo) that will either return None or an
>> object depending on a random value generated.  What I want to happen is that
>> if I call foo(), i.e, f = foo() and it returns None, to re-call it until it
>> returns something else.  I would think this would be done with a while loop,
>> but can't seem to get the details right.
>>
>
> Even though a while will work, you will have tied up the CPU until the loop
> terminates. This is never a good idea.
>
> What is your higher level goal?
>
>
> --
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] continuous running of a method

2010-08-24 Thread Greg Bair

On 08/25/2010 01:25 AM, Nitin Das wrote:
The problem with this while loop is if your random value doesn't lie 
between the mentioned range then ur 100% cpu would be utilized. The 
one thing u can do is to sleep for some time lets say 0.5 sec after 
every while loop iteration , in this case ur cpu utilization will go down.

It's not that the value doesn't lie between the mentioned range.

What I'm doing is randomly fetching an item from a list of dicts 
(multi-dimensional ones from a JSON response) and accessing a value from 
it, but not every item has the key I'm looking for (can't change that).  
I suppose what I could do to not randomize every time is to iterate 
through the list and create a new list that only contains dicts that 
have that key, then get a random one from that.  I suppose that would be 
more efficient.


Any thoughts?

Greg


--nitin

On Mon, Aug 23, 2010 at 8:21 PM, bob gailer > wrote:


 On 8/23/2010 1:00 AM, Greg Bair wrote:

I have a method (I'll call it foo) that will either return
None or an object depending on a random value generated.  What
I want to happen is that if I call foo(), i.e, f = foo() and
it returns None, to re-call it until it returns something
else.  I would think this would be done with a while loop, but
can't seem to get the details right.


Even though a while will work, you will have tied up the CPU until
the loop terminates. This is never a good idea.

What is your higher level goal?




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