Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Dave Angel

C M Caine wrote:

Thank you for the clarification, bob.

For any future readers of this thread I include this link[1] to effbot's
guide on lists, which I probably should have already read.

My intention now is to modify list contents in the following fashion:

for index, value in enumerate(L):
L[0] = some_func(value)

Is this the standard method?

[1]: http://effbot.org/zone/python-list.htm

Colin Caine

  

Almost.   You should have said

   L[index] = some_func(value)

The way you had it, it would only replace the zeroth item of the list.

Note also that if you insert or delete from the list while you're 
looping, you can get undefined results.  That's one reason it's common 
to build a new loop, and just assign it back when done.  Example would 
be the list comprehension you showed earlier.


DaveA

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


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
On 26 April 2010 23:45, Alan Gauld  wrote:
>
> "C M Caine"  wrote
>>
>> My intention now is to modify list contents in the following fashion:
>>
>> for index, value in enumerate(L):
>>   L[0] = some_func(value)
>
> I think you mean:
>     L[index] = some_func(value)

Yes, I do

>> Is this the standard method?
>
> Or use a List copmprehension.
>
> L = [some_func(value) for value in L]
>
> I'd say nowadays that the comprehension was most common.
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/

Thanks, I can see why the comprehensions are more popular.

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


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
>> What other strange behaviour should I expect from for loops?
>
> You should read up on immutable data types like strings and tuples.
> Start with [1].
>
> Greets
> Sander
>
> [1] http://docs.python.org/reference/datamodel.html
>

Thank you kindly for your reply, I'll be sure to read up on it.

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


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Alan Gauld


"C M Caine"  wrote 


My intention now is to modify list contents in the following fashion:

for index, value in enumerate(L):
   L[0] = some_func(value)


I think you mean:
 L[index] = some_func(value)


Is this the standard method?


Or use a List copmprehension.

L = [some_func(value) for value in L]

I'd say nowadays that the comprehension was most common.

--
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] For loop breaking string methods

2010-04-26 Thread Sander Sweers
On 26 April 2010 21:38, C M Caine  wrote:
> Why does this not work:
 L = [' foo ','bar ']
 for i in L:
>     i = i.strip()

str.strip() _returns_ a *new* string and leaves the original string
alone. The reason being that string are immutable so can not be
changed.

>>> s1 = ' foo '
>>> s1[1]
'f'
>>> s1[1] = 'g'

Traceback (most recent call last):
  File "", line 1, in 
s1[1] = 'g'
TypeError: 'str' object does not support item assignment

However lists are mutable and can be changed in place.

>>> l = [' foo ','bar ']
>>> l[0] = ' boo '
>>> l
[' boo ', 'bar ']
>>> l[1] = 'far '
>>> l
[' boo ', 'far ']


> But this does:
 L = [i.strip() for i in L]
 L
> ['foo', 'bar']

What you do here is create a *new* list object and assign it to
variable L. The new list is created with *new* string objects returned
by str.strip().

Putting the 2 together you could do something like below but I would
use a list comprehension like you did above:

>>> l = [' foo ','bar ']
>>> for x in range(len(l)):
l[x] = l[x].strip()

>>> l
['foo', 'bar']
>>>

> What other strange behaviour should I expect from for loops?

You should read up on immutable data types like strings and tuples.
Start with [1].

Greets
Sander

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


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
Thank you for the clarification, bob.

For any future readers of this thread I include this link[1] to effbot's
guide on lists, which I probably should have already read.

My intention now is to modify list contents in the following fashion:

for index, value in enumerate(L):
L[0] = some_func(value)

Is this the standard method?

[1]: http://effbot.org/zone/python-list.htm

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


Re: [Tutor] For loop breaking string methods

2010-04-26 Thread bob gailer

On 4/26/2010 3:38 PM, C M Caine wrote:

Why does this not work:


By "work" you mean "do what I want it to do".


>>> L = [' foo ','bar ']
>>> for i in L:
i = i.strip()


This creates a new local variable named i. It does not affect L. This 
has nothing to do with loops nor is it strange behavior - it is expected 
behavior.


Consider the loopless equivalent:

>>> i = L[0]
>>> i = i.strip()
>>> L
[' foo ', 'bar ']




>>> L
[' foo ', 'bar ']
>>> # note the leading whitespace that has not been removed.

But this does:
>>> L = [i.strip() for i in L]
>>> L
['foo', 'bar']

What other strange behaviour should I expect from for loops?



None - loops do not have "strange" behaviors.
Perhaps "unexpected" but that is a result of not understanding an aspect 
of the language.


--
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] For loop breaking string methods

2010-04-26 Thread C M Caine
Why does this not work:
>>> L = [' foo ','bar ']
>>> for i in L:
i = i.strip()


>>> L
[' foo ', 'bar ']
>>> # note the leading whitespace that has not been removed.

But this does:
>>> L = [i.strip() for i in L]
>>> L
['foo', 'bar']

What other strange behaviour should I expect from for loops?

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


Re: [Tutor] what is wrong with this code?

2010-04-26 Thread Steve Willoughby
On Mon, Apr 26, 2010 at 07:53:17PM +0200, Norman Khine wrote:
> ok this worked,
> 
> cursor.execute(drop_user_tables % ",".join(user_tables))
> 
> it seems that DROP TABLE (and other DDL statements) don't technically
> take SQL parameters.


That's correct.  The point of using %s as a placeholder for an SQL
value (not to be confused with %s in string formatting with the % operator
in Python), is to prevent them from being confused with SQL *statement*
code but made into proper values (strings, integers, and so forth).

But the names of tables, fields, etc., are NOT values, they are part
of the SQL syntax and you have to build them as part of the string
itself.
-- 
Steve Willoughby|  Using billion-dollar satellites
st...@alchemy.com   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem iterating over csv.DictReader

2010-04-26 Thread Sander Sweers
On 26 April 2010 15:00, Matthew Williams  wrote:
> What I'm looking for is a way to explicity reset the iterator, to tell it to
> go back to the beginning.

You will need to use the seek method on the fileobject.

f = open('insert your csv file here.csv', 'rb') #Note the b in 'rb'

#Do your processing

f.seek(0)

#Do some more

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


Re: [Tutor] what is wrong with this code?

2010-04-26 Thread Norman Khine
On Mon, Apr 26, 2010 at 7:45 PM, Norman Khine  wrote:
> thanks for the reply.
>
> On Mon, Apr 26, 2010 at 7:18 PM, Serdar Tumgoren  wrote:
>>
>>> user_tables = ['notification', 'userNotification', 'product_comments',
>>> 'product_donation_paypalTransaction', 'product_donation',
>>> 'productList_recommended', 'productList_user_assoc',
>>> 'profile_values']
>>>
>>> drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""
>>>
>>> try:
>>>    cursor.execute(drop_user_tables, (x for x in user_tables))
>>
>> Norman,
>> It looks like what you're after is the cursor's "executemany" method, which
>> is supported by many common database adapters that comply with DB-API2.0.
>>
>> If you're using sqlite3, there are more details here:
>>
>>     http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.executemany
>>
>> In your code, you seem to be trying to loop over the values in the
>> user_tables list. I haven't tested this, but instead try simply passing the
>> list of user_tables to executemany:
>>
>>     cursor.executemany(drop_user_tables, user_tables)
>
> i am using MySQLdb, to make changes to a database.
>>
>> If the above doesn't work or is not available with your database adapter,
>> how about just using a simple loop?
>>
>> for table in user_tables:
>>     cursor.execute(drop_user_tables, (table,))
>
> both ways, i got this error:
>
> $ py upgrade.py
> Error (1064, "You have an error in your SQL syntax; check the manual
> that corresponds to your MySQL server version for the right syntax to
> use near ''notification'' at line 1")
> aqoon:ookoodoo khinester$ py upgrade.py
> Error (1064, "You have an error in your SQL syntax; check the manual
> that corresponds to your MySQL server version for the right syntax to
> use near ''notification'' at line 1")
>
> but my query, is fine:
>
> drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""

ok this worked,

cursor.execute(drop_user_tables % ",".join(user_tables))

it seems that DROP TABLE (and other DDL statements) don't technically
take SQL parameters.

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


Re: [Tutor] what is wrong with this code?

2010-04-26 Thread Norman Khine
thanks for the reply.

On Mon, Apr 26, 2010 at 7:18 PM, Serdar Tumgoren  wrote:
>
>> user_tables = ['notification', 'userNotification', 'product_comments',
>> 'product_donation_paypalTransaction', 'product_donation',
>> 'productList_recommended', 'productList_user_assoc',
>> 'profile_values']
>>
>> drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""
>>
>> try:
>>    cursor.execute(drop_user_tables, (x for x in user_tables))
>
> Norman,
> It looks like what you're after is the cursor's "executemany" method, which
> is supported by many common database adapters that comply with DB-API2.0.
>
> If you're using sqlite3, there are more details here:
>
>     http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.executemany
>
> In your code, you seem to be trying to loop over the values in the
> user_tables list. I haven't tested this, but instead try simply passing the
> list of user_tables to executemany:
>
>     cursor.executemany(drop_user_tables, user_tables)

i am using MySQLdb, to make changes to a database.
>
> If the above doesn't work or is not available with your database adapter,
> how about just using a simple loop?
>
> for table in user_tables:
>     cursor.execute(drop_user_tables, (table,))

both ways, i got this error:

$ py upgrade.py
Error (1064, "You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near ''notification'' at line 1")
aqoon:ookoodoo khinester$ py upgrade.py
Error (1064, "You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near ''notification'' at line 1")

but my query, is fine:

drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""


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


Re: [Tutor] what is wrong with this code?

2010-04-26 Thread Serdar Tumgoren
> user_tables = ['notification', 'userNotification', 'product_comments',
> 'product_donation_paypalTransaction', 'product_donation',
> 'productList_recommended', 'productList_user_assoc',
> 'profile_values']
>
> drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""
>
> try:
>cursor.execute(drop_user_tables, (x for x in user_tables))
>

Norman,
It looks like what you're after is the cursor's "executemany" method, which
is supported by many common database adapters that comply with DB-API2.0.

If you're using sqlite3, there are more details here:

http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.executemany

In your code, you seem to be trying to loop over the values in the
user_tables list. I haven't tested this, but instead try simply passing the
list of user_tables to executemany:

cursor.executemany(drop_user_tables, user_tables)

If the above doesn't work or is not available with your database adapter,
how about just using a simple loop?

for table in user_tables:
cursor.execute(drop_user_tables, (table,))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] what is wrong with this code?

2010-04-26 Thread Norman Khine
hello, i have a list of tables i want to drop:

user_tables = ['notification', 'userNotification', 'product_comments',
'product_donation_paypalTransaction', 'product_donation',
'productList_recommended', 'productList_user_assoc',
'profile_values']

drop_user_tables = """DROP TABLE IF EXISTS db2.%s"""

try:
cursor.execute(drop_user_tables, (x for x in user_tables))


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


Re: [Tutor] Problem iterating over csv.DictReader

2010-04-26 Thread Serdar Tumgoren
> I have noticed this odd behaviour in the CSV DictReader Class, and at a
> loss to understand/ get around it.
>
> The aim is to read in a CSV file, and then iterate over the lines. The
> problem (seems) to be that once you have iterated over it once, you can't do
> it again.
>
> This is expected behavior. See below from the Python docs:

http://docs.python.org/glossary.html#term-iterator
http://docs.python.org/library/stdtypes.html#typeiter

If you'd like to make multiple passes over the lines from your CSV, store
them in a variable when you first read them in and then loop over that
variable instead.

One approach (though it may not be the best if you're dealing with huge
quantities of data):

reader = csv.DictReader(open(fname, 'r'), delimiter = ',', quotechar = '"')
data = [row for row in reader]
# now you can make multiple passes over the dictionaries stored in "data"


> I don't know if this is me (and it may well be) but it seems to be a
> recurrent issue, and means that a csv.DictReader doesn't behave in the same
> way as a normal dict object.
>

Correct. It's not supposed to behave like a normal dict. It behaves like a
reader object. See the python docs:

"Create an object which operates like a regular reader but maps the
information read into a dict whose keys are given by the optional *
fieldnames* parameter."
http://docs.python.org/library/csv.html#reader-objects

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


Re: [Tutor] Programming pic chips with python

2010-04-26 Thread Alan Gauld


"Humphrey"  wrote

I am new to python and i want to ask if python can be used in electronics 
like for programming programmable chips like the PIC16F series. 


It depends on your chip development environment.
If the programmer connects to a PC (via serial or USB for example) 
then usually there will be standard Windows libraries that expose 
an API onto the chip. In that case you can use the ctypes module 
in Python to communicate with the programmer.



If the programmer is standalone it will be much harder, 
maybe impossible. And if it is a bespoke platform using 
a standard OS(eg Linux) then its likely possible to get the 
Python source to build on the platform and use it, but how 
much use it would be will dpened on how the programmer 
makes itself available in an API.


It will take a fair bit of in depth research to find out exactly 
what will work in your case.


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


[Tutor] Problem iterating over csv.DictReader

2010-04-26 Thread Matthew Williams

Dear All,

I have noticed this odd behaviour in the CSV DictReader 
Class, and at a loss to understand/ get around it.


The aim is to read in a CSV file, and then iterate over 
the lines. The problem (seems) to be that once you have 
iterated over it once, you can't do it again.


I don't know if this is me (and it may well be) but it 
seems to be a recurrent issue, and means that a 
csv.DictReader doesn't behave in the same way as a normal 
dict object.


Code to confirm/ replicate below.

What I'm looking for is a way to explicity reset the 
iterator, to tell it to go back to the beginning.


Any ideas (or pointing out I am a moron) very welcome.

Matt


Python 2.6.3 (r253:75183, Oct 11 2009, 18:26:07)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2

import csv
fname = "insert your csv file here.csv"
inr = csv.DictReader(open(fname, 'r'), delimiter = ',', 
quotechar = '"')


for r in inr:
print r



for r in inr:
print r


Nothing.

If I reload the file, I can solve the issue


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


Re: [Tutor] Programming pic chips with python

2010-04-26 Thread Steve Willoughby
On Mon, Apr 26, 2010 at 12:18:45PM +0200, Humphrey wrote:
> I am new to python and i want to ask if python can be used in electronics 
> like for programming programmable chips like the PIC16F series. I want to 
> used it specifically in power conversion systems like Sine wave inverters and 
> uninterpretable power supply systems

In theory, any language could be used for something like this, but
generally speaking embedded systems like PICs (and I'd say particularly
for the PIC16 series which have very tiny memory storage), it may be
too much to expect the chip to carry a Python runtime inside it.  It
is an interesting idea, though, to either think of some sort of
tiny interpreter or a native code compiler.  

For really time-critical or memory-restrained applications, though,
which is typically the arena in which PICs and similar microcontrollers
exist, people usually program "closer to the bare metal" in assembly
or C.

You may be interested in looking at xwisp, though, as a related topic.
That is a PIC programmer (i.e., software to manage the transfer of
programs into the chip itself) written in Python.


-- 
Steve Willoughby|  Using billion-dollar satellites
st...@alchemy.com   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Programming pic chips with python

2010-04-26 Thread Humphrey
hi there

I am new to python and i want to ask if python can be used in electronics like 
for programming programmable chips like the PIC16F series. I want to used it 
specifically in power conversion systems like Sine wave inverters and 
uninterpretable power supply systems

kindest regards 

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


Re: [Tutor] module import problems

2010-04-26 Thread Rayon
my bad it was a simple error I was calling it form the wrong module thanks


From: Rayon 
Sent: Monday, April 26, 2010 3:47 AM
To: bob gailer ; tutor@python.org 
Subject: Re: [Tutor] module import problems


I have a project folder  report_db in that project folder I have to  packages 
_modules and _ tables

report_db   \
_tables 
_modules 
  I want to import some functions from _tables  to _modules so I can use them 
in  a function in _modules.

  


From: bob gailer 
Sent: Sunday, April 25, 2010 10:25 PM
To: tutor@python.org 
Subject: Re: [Tutor] module import problems


On 4/25/2010 9:56 PM, Rayon wrote: 
  I have a module with the name _table 
  in the same directory I have another by the name of _module

  I would like to import _table into _module

What exactly does that mean?

The import statement, when executed,  imports a module into the module 
containing the import statement.

And what is the purpose of the following line?


  from tables.report_db_engine import *




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


Re: [Tutor] Which Designer

2010-04-26 Thread Alan Gauld


"Steve Willoughby"  wrote


However, there are some real disadvantages to Tk(inter) as well, chiefly
that it is a least-common denominator which does a passable job of running
GUIs but they don't look consistent with the native look of Windows or OS/X


The new themed widgets in Tk have changed that, they are built on the
native widgets and look just like any other GUI. Available in Tkinter from
Python 2.7 and 3.1


or whatever.  And there is a lot of missing functionality.


This is still true although Tix addresses the biggest gaps - but is
sadly lacking documentation - you have to use the Tcl/Tk docs :-(
(I keep intending to do a write up on Tix but other things get in the way!)

And there are other bolt-ons too such as PMW.


I'm getting into wxPython at the moment, and I have to say it's at least
worth a look.  It's also available for every platform (but doesn't come
with Python), and is far more complete, and just about as easy to use
as Tk, but looks a lot more polished.


wxPython is definielt more powerful and in particular has support
for things like printing and drag n drop which are missing fromTk.


There are other toolkits with their advocates as well, of course, but if
someone were just starting out with Python GUI programming, I'd recommend
looking around at your options before starting with Tk.


I'd still advocate Tk because
a) It comes with Python so is standard
b) It is also the standard GUI in Ruby, Perl and Tcl so once learned is 
oportable

c) It is best documented with many books etc featuring it
d) It is easy to learn the basic GUI principles that are valid in any Framework
   (a bit like learning Python is good becauise it helps you learn other 
languages)


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] module import problems

2010-04-26 Thread Rayon
I have a project folder  report_db in that project folder I have to  packages 
_modules and _ tables

report_db   \
_tables 
_modules 
  I want to import some functions from _tables  to _modules so I can use them 
in  a function in _modules.

  


From: bob gailer 
Sent: Sunday, April 25, 2010 10:25 PM
To: tutor@python.org 
Subject: Re: [Tutor] module import problems


On 4/25/2010 9:56 PM, Rayon wrote: 
  I have a module with the name _table 
  in the same directory I have another by the name of _module

  I would like to import _table into _module

What exactly does that mean?

The import statement, when executed,  imports a module into the module 
containing the import statement.

And what is the purpose of the following line?


  from tables.report_db_engine import *




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