[Tutor] Extracting xml text

2010-06-20 Thread T.R. D.
Hi,

I'm trying to parse a list of xml strings and so far it looks like the
xml.parsers.expat is the way to go but I'm not quite sure how it works.

I'm trying to parse something similar to the following.  I'd like to collect
all headings and bodies and associate them in a variable (dictionary for
example). How would I use the expat class to do this?


Tove
Jani
Reminder
Don't forget me this weekend!



Jani
Tovi
Reminder 2
Don't forget to bring snacks!




Here's the code that I have so far based on the documents I've read:

actions = test.actions


# Parse stepgreen actions

parser = xml.parsers.expat.ParserCreate()

parser.Parse(reminderXML)
print parser.StartElementHandler;

This doesn't work (even if there's one item in the list).

Could someone please point me in the right direction? I haven't found any
examples of what I'd like to do and I don't understand how expat works.
Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extracting xml text

2010-06-20 Thread Karim


Hello,

The following is an example which works for me to count opening tags in 
a xml doc,

if it can help:

# -*- coding: iso-8859-1 -*-
import sys
from xml.parsers.expat import ParserCreate

n = 0
def start_element(name, attrs): # callback declaration
global n
n += 1

parser = ParserCreate() # création du parser
parser.StartElementHandler = start_element # initialization
parser.ParseFile(file(sys.argv[1])) # get the input file
print '%d opening tags' % n

Regards
Karim
France


On 06/20/2010 08:03 AM, T.R. D. wrote:
xml strings and so far it looks like the xml.parsers.expat is the way 
to go but I'm not quite sure how it works.


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


Re: [Tutor] Extracting xml text

2010-06-20 Thread Stefan Behnel

T.R. D., 20.06.2010 08:03:

I'm trying to parse a list of xml strings and so far it looks like the
xml.parsers.expat is the way to go but I'm not quite sure how it works.

I'm trying to parse something similar to the following.  I'd like to collect
all headings and bodies and associate them in a variable (dictionary for
example). How would I use the expat class to do this?


Well, you *could* use it, but I *would* not recommend it. :)




Tove
Jani
Reminder
Don't forget me this weekend!



Jani
Tovi
Reminder 2
Don't forget to bring snacks!



Use ElementTree's iterparse:

from xml.etree.cElementTree import iterparse

for _, element in iterparse("the_file.xml"):
if element.tag == 'note':
# find text in interesting child elements
print element.findtext('heading'), element.findtext('body')

# safe some memory by removing the handled content
element.clear()

iterparse() iterates over parser events, but it builds an in-memory XML 
tree while doing so. That makes it trivial to find things in the stream. 
The above code receives an event whenever a tag closes, and starts working 
when the closing tag is a 'note' element, i.e. when the complete subtree of 
the note element has been parsed into memory.


Stefan

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


Re: [Tutor] Extracting xml text

2010-06-20 Thread Karim


In fact you must initialize the handler before parsing the xml doc and 
it should work.


Regards
Karim
France

On 06/20/2010 10:12 AM, Karim wrote:


Hello,

The following is an example which works for me to count opening tags 
in a xml doc,

if it can help:

# -*- coding: iso-8859-1 -*-
import sys
from xml.parsers.expat import ParserCreate

n = 0
def start_element(name, attrs): # callback declaration
global n
n += 1

parser = ParserCreate() # création du parser
parser.StartElementHandler = start_element # initialization
parser.ParseFile(file(sys.argv[1])) # get the input file
print '%d opening tags' % n

Regards
Karim
France


On 06/20/2010 08:03 AM, T.R. D. wrote:
xml strings and so far it looks like the xml.parsers.expat is the way 
to go but I'm not quite sure how it works.


___
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] Extracting xml text

2010-06-20 Thread Karim


Hello Stefan,

I know you are promoting Etree and I am very interesting in it.
Is there any chance to have it integrated in future standard Python version?

Regards
Karim

On 06/20/2010 10:14 AM, Stefan Behnel wrote:

T.R. D., 20.06.2010 08:03:

I'm trying to parse a list of xml strings and so far it looks like the
xml.parsers.expat is the way to go but I'm not quite sure how it works.

I'm trying to parse something similar to the following.  I'd like to 
collect

all headings and bodies and associate them in a variable (dictionary for
example). How would I use the expat class to do this?


Well, you *could* use it, but I *would* not recommend it. :)




Tove
Jani
Reminder
Don't forget me this weekend!



Jani
Tovi
Reminder 2
Don't forget to bring snacks!



Use ElementTree's iterparse:

from xml.etree.cElementTree import iterparse

for _, element in iterparse("the_file.xml"):
if element.tag == 'note':
# find text in interesting child elements
print element.findtext('heading'), element.findtext('body')

# safe some memory by removing the handled content
element.clear()

iterparse() iterates over parser events, but it builds an in-memory 
XML tree while doing so. That makes it trivial to find things in the 
stream. The above code receives an event whenever a tag closes, and 
starts working when the closing tag is a 'note' element, i.e. when the 
complete subtree of the note element has been parsed into memory.


Stefan

___
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] Extracting xml text

2010-06-20 Thread Stefan Behnel

Hi,

please don't top-post, it makes your replies hard to read in context.

Karim, 20.06.2010 10:24:

On 06/20/2010 10:14 AM, Stefan Behnel wrote:

Use ElementTree's iterparse:

from xml.etree.cElementTree import iterparse

>> [...]
>

I know you are promoting Etree and I am very interesting in it.
Is there any chance to have it integrated in future standard Python
version?


The import above comes directly from the standard library (Python 2.5 and 
later). You may be referring to lxml.etree, which will most likely never 
make it into the stdlib.


Stefan

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


Re: [Tutor] How to add extra Vbox fields dynamically

2010-06-20 Thread Timo

On 20-06-10 04:04, Lang Hurst wrote:
OK, I just did the ugliest hack, from someone who only seems to do 
ugly hacks.  I set up a bunch of textview areas and defaulted them to 
'not visible'.  Then as I loop through my query results, I make them 
visible one at a time.  Well, it works perfect, but it just doesn't 
seem right for some reason.
You also posted this on the PyGTK mailing list, which is the correct 
place for these problems. Please post your PyGTK questions only there, 
do not double post. I'll answer you there.


Cheers,
Timo



Lang Hurst wrote:
I hope that I'm asking this in the right place.  I don't have too 
much trouble hacking together command line stuff, but the GUI part is 
a struggle for me.


I created a UI in glade.  It has a couple of Vboxes for information.  
The final box is filled with a TextView.  In my program, I'm 
connecting to a database and pulling out a series of records.  As it 
stands, I can pull out all the records and view them in the TextView, 
but I would like to be able to have each result be a separate 
TextView (which I then have to figure out how to make clickable...)


Right now, this part looks like:

   query = 'SELECT subject, chapter_module, credits, final_test_score,
   notes FROM credits WHERE id=' + student[0][6]
   cursor.execute(query)
   credits = cursor.fetchall()
   temp = ''
   for credit in credits:
   sub_buf = 15 - len(credit[0])
   chap_buf = 15 - len(credit[1])
   cred_buf = 5 - len(credit[2])
   score_buf = 5 - len(credit[1])
   temp = temp + credit[0] + " " * sub_buf + credit[1] + " " *
   chap_buf + "Credits: " + credit[2] + " " * chap_buf +  "Score: " +
   credit[3] + "\n\nNOTES: " + credit[4] + "\n" + " " * 5 + "-" * 50 +
   "\n\n"

   # I would like to loop something here
# to have multiple text areas added

   buff = self.builder.get_object('textview1').get_buffer()
   buff.set_text(temp)


This works fine.  It pulls the records out of the database, and then 
cats the results together and throws it into my TextView.  I'm happy 
with the results so far, but I would like to be able to click on each 
record if I see something that needs to be modified.  As always, any 
help is appreciated.


Also, can anyone recommend a good book for gtk + glade + python?  I 
went out and bought Learning Python, but book at B&N that were 
remotely GUI related seems very outdated or just tkinter related, and 
just about all the gtk+python examples and tutorials don't use 
glade.  Thanks again.



-Lang






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


[Tutor] re.sub() query

2010-06-20 Thread Payal
Hi,
Is it possible to solve the below, w/o making a re object?

>>> a
'Mary Had a Little Lamb'
>>> p=re.compile('l',re.I)
>>> re.sub(p,'-',a)
'Mary Had a -itt-e -amb'

I cannot figure how to se re.I w/o involving  p.

Thanks.
With warm regards,
-Payal
-- 



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


Re: [Tutor] Question

2010-06-20 Thread Payal
On Sat, Jun 19, 2010 at 11:24:44AM +, ALAN GAULD wrote:
> Actually that's a point. I favour learning two languages that are 
> semantically 
> similar buut syntactically different. Thats why I chose JavaScript and 
> VBScript 
> as my tutorial languages, because the syntax of each is so different you are 
> less likely to get confused, but the underlying programming model is very 
> similar in each case.

Hijacking the thread a bit. What about learning Jython and Python? Do I
need to know Java to learn Jython?

With warm regards,
-Payal
-- 

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


Re: [Tutor] re.sub() query

2010-06-20 Thread Evert Rol
> Is it possible to solve the below, w/o making a re object?
> 
 a
> 'Mary Had a Little Lamb'
 p=re.compile('l',re.I)
 re.sub(p,'-',a)
> 'Mary Had a -itt-e -amb'
> 
> I cannot figure how to se re.I w/o involving  p.

Would this work?
>>> re.sub('(?i)l', '-', a)

See http://docs.python.org/library/re.html#regular-expression-syntax , search 
for iLmsux, which provide flags inside the regex.

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


Re: [Tutor] re.sub() query

2010-06-20 Thread Payal
On Sun, Jun 20, 2010 at 12:03:47PM +0200, Evert Rol wrote:
> >>> re.sub('(?i)l', '-', a)
> 
> See http://docs.python.org/library/re.html#regular-expression-syntax , search 
> for iLmsux, which provide flags inside the regex.
 
Goodness that was fast. Thanks a lot Evert. For records,

>>> re.sub('l(?i)','-',a)
'Mary Had a -itt-e -amb'


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


Re: [Tutor] re.sub() query

2010-06-20 Thread Mark Lawrence

On 20/06/2010 10:54, Payal wrote:

Hi,
Is it possible to solve the below, w/o making a re object?


a

'Mary Had a Little Lamb'

p=re.compile('l',re.I)
re.sub(p,'-',a)

'Mary Had a -itt-e -amb'

I cannot figure how to se re.I w/o involving  p.

Thanks.
With warm regards,
-Payal


You can do this.

>>> re.sub('[lL]','-',a)
'Mary Had a -itt-e -amb'

However it strikes me as overkill to use an re for something that could 
be done with the string replace function.


Kindest regards.

Mark Lawrence.

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


[Tutor] (no subject)

2010-06-20 Thread Edward Lang



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


Re: [Tutor] Tutor Digest, Vol 76, Issue 57

2010-06-20 Thread Edward Lang
e

tutor-requ...@python.org wrote:

>Send Tutor mailing list submissions to
>   tutor@python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>   http://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
>   tutor-requ...@python.org
>
>You can reach the person managing the list at
>   tutor-ow...@python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>
>
>Today's Topics:
>
>   1. Re: Python glade (Lang Hurst)
>   2. Re: Question (Alan Gauld)
>   3. Re: Question (Steven D'Aprano)
>
>
>--
>
>Message: 1
>Date: Fri, 18 Jun 2010 21:50:23 -0700
>From: Lang Hurst 
>To: tutor@python.org
>Subject: Re: [Tutor] Python glade
>Message-ID: <4c1c4c8f.7080...@tharin.com>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>Found the problem.  If you want to do this, you have to access the 
>gtkEntry like this
>
>self.builder.get_object('student_change').set_completion(completion)
>
>
>instead of
>
>self.student_change.set_completion(completion)
>
>
>
>
>
>Lang Hurst wrote:
>> OK, I created a UI in glade which has the following:
>>
>> 
>>True
>>True
>>> name="invisible_char">●
>>25
>>>handler="student_change_activate_cb"/>
>>
>>
>>
>> basically, a text box.  I'm trying to set it up to automatically 
>> complete names as I type.  My py file has the following:
>>
>> def __init__(self):
>>  self.builder = gtk.Builder()
>>  self.builder.add_from_file('gradebook.glade')
>>  self.window = self.builder.get_object('winapp')
>>  self.builder.connect_signals(self)
>>#  self.student_change = gtk.Entry()
>>  completion = gtk.EntryCompletion()
>>  self.names = gtk.ListStore(str)
>>  query = "SELECT * from students"
>>  db = sqlite3.connect('gradebook.db')
>>  cursor = db.cursor()
>>  cursor.execute(query)
>>  students = cursor.fetchall()
>>  for student in students:
>>  self.names.append([student[1]])
>>  print student[1]
>>  cursor.close()
>>  completion.set_model(self.names)
>>  self.student_change.set_completion(completion)
>>  completion.set_text_column(0)
>>
>>
>> When I try to run this, I get
>>
>>AttributeError: 'appGUI' object has no attribute 'student_change'
>>
>>
>> But if I uncomment the self.student_change line from up above, it runs 
>> but doesn't do completion.
>>
>> I modeled this after
>>
>> http://www.koders.com/python/fid755022E2A82A54C79A7CF86C00438E6F825676C3.aspx?s=gtk#L4
>>  
>>
>>
>> I'm pretty sure the problem is somewhere in the gtk.Builder part of 
>> what I'm doing, but I can't for the life of me figure this out.
>>
>
>
>-- 
>There are no stupid questions, just stupid people.
>
>
>
>--
>
>Message: 2
>Date: Sat, 19 Jun 2010 09:19:09 +0100
>From: "Alan Gauld" 
>To: tutor@python.org
>Subject: Re: [Tutor] Question
>Message-ID: 
>Content-Type: text/plain; format=flowed; charset="iso-8859-1";
>   reply-type=original
>
>"Independent Learner"  wrote 
>
>> ~I was wondering if I should try to learn 2 programming languages 
>> at once, Python and C++. 
>
>No, no no! If it had been a different pair I might have said try it. 
>But C++ is one of the most difficult, complex and difficult 
>programming lamnguages out there. It is full of subtle things 
>that can trip you up and cause very weird and subtle bugs 
>that are diffficult to find. And it has similar concepts to Python 
>but implemented so entirely differently that studying the two 
>together will be an exercise in frustration.
>
>Part of the reason why C++ is so difficult is because it is 
>so powerful. You have full access to the machine through 
>the C language elements, plus a full OOP environment, 
>plus a powerful generic type system. Plus it combines 
>static and dynamic variables with a reference model all with 
>slightly different syntax and semantic behaviours.
>
>At work I hardly ever recommend that people go on language 
>training courses, C++ is the exception! You can learn C++ 
>by yourself but you will need a good book and a lot of 
>time and patience.
>
>> Obviously I am working on learning python right now, 
>> I have gotten up to Classes
>
>Stick with Python and get comfortable with that.
>
>Then move onto C++ as a separate and significant project
>if you really feel you have a need to know it.
>
>> there are still a lot of things I am not really fully 
>> comprehending, but like I said I have a pretty good idea. 
>
>Ask questions here. That's what the tutor list is for.
>
>> ~So is it better to learn 1 programming language 
>> first, then learn another. Or better to pretty much 
>> learn 

Re: [Tutor] Question

2010-06-20 Thread bob gailer

On 6/20/2010 5:59 AM, Payal wrote:

On Sat, Jun 19, 2010 at 11:24:44AM +, ALAN GAULD wrote:
   

Actually that's a point. I favour learning two languages that are semantically
similar buut syntactically different. Thats why I chose JavaScript and VBScript
as my tutorial languages, because the syntax of each is so different you are
less likely to get confused, but the underlying programming model is very
similar in each case.
 

Hijacking the thread a bit. What about learning Jython and Python? Do I
need to know Java to learn Jython?
   


No. Jython is "just" a Python interpreter written in Java. It allows you 
to use Java classes in your Python code. You might want to learn Java to 
take advantage of that.



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

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


Re: [Tutor] Question

2010-06-20 Thread Alan Gauld


"Steven D'Aprano"  wrote

Yea I took an intro to comp sci class(like 2 years ago) and a
computer programming logic class(also like 2 years ago) both
using pseudocode


Good grief! How do they teach a class in computer programming using
pseudocode???


Yes, doing that 2 years ago seems odd.
OTOH When I started at university the first year of
comp sci we did everything in pseudo code. That
was to emphasise that comp sci was the science
of computation not computer programming. So
the emphasis was on the theory of algorithms,
logic and so on. (And comp sci dept was just a
sub department of the math school...)

But that was in the days when programs were punched
out on paper tape (no card readers for me Bob :-) and
the results came back in the post two days later - usually
saying "missing semi colon: line 93". So
using pseudo code to focus on the content rather
than the mechancs made sense. 2 years ago that
surely wouldn't have been true! But maybe the prof
harked back to those "golden days"...


--
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] Extracting xml text

2010-06-20 Thread T.R. D.
Thanks all for your help.

I decided to go with iterparse but trying the simple example in the python
interpreter led to an error (see below) and when I tried this with a much
larger xml sample, it seemed to print the full elements, not the specific
values of the element. For example, given what I entered in the python
interpreter, the result would have been the full xml example, and not
"Reminder" "Don't forget me this weekend".

Did I do something wrong in the sample below? Thanks again.

>>> from xml.etree.cElementTree import iterparse
>>> sample = '''\
... 
... Tove
... Jani
... Reminder
... Don't forget me this weekend!
... 
... '''
>>> print sample

Tove
Jani
Reminder
Don't forget me this weekend!


>>> for event, elem in iterparse(sample):
... if elem.tag == 'note':
... print elem.findtext('heading'), elem.findtext('body')
... elem.clear()
...
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 52, in __init__
IOError: [Errno 2] No such file or directory:
"\n\tTove\n\tJani\n\tReminder\n\tDon't
forget me this weekend!\n\n"
>>>


On Sun, Jun 20, 2010 at 4:32 AM, Stefan Behnel  wrote:

> Hi,
>
> please don't top-post, it makes your replies hard to read in context.
>
> Karim, 20.06.2010 10:24:
>
>> On 06/20/2010 10:14 AM, Stefan Behnel wrote:
>>
>>> Use ElementTree's iterparse:
>>>
>>> from xml.etree.cElementTree import iterparse
>>>
>> >> [...]
>
> >
>
>> I know you are promoting Etree and I am very interesting in it.
>> Is there any chance to have it integrated in future standard Python
>> version?
>>
>
> The import above comes directly from the standard library (Python 2.5 and
> later). You may be referring to lxml.etree, which will most likely never
> make it into the stdlib.
>
>
> Stefan
>
> ___
> 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] Extracting xml text

2010-06-20 Thread Stefan Behnel

T.R. D., 20.06.2010 16:04:

I decided to go with iterparse but trying the simple example in the python
interpreter led to an error (see below) and when I tried this with a much
larger xml sample, it seemed to print the full elements, not the specific
values of the element. For example, given what I entered in the python
interpreter, the result would have been the full xml example, and not
"Reminder" "Don't forget me this weekend".

Did I do something wrong in the sample below?


Yes. You didn't follow the example and you didn't read the docs.



>>> from xml.etree.cElementTree import iterparse
>>> sample = '''\
...
...Tove
...Jani
...Reminder
...Don't forget me this weekend!
...
... '''
>>> print sample

 Tove
 Jani
 Reminder
 Don't forget me this weekend!


>>> for event, elem in iterparse(sample):
... if elem.tag == 'note':
... print elem.findtext('heading'), elem.findtext('body')
... elem.clear()
...
...
Traceback (most recent call last):
   File "", line 1, in
   File "", line 52, in __init__
IOError: [Errno 2] No such file or directory:
"\n\tTove\n\tJani\n\tReminder\n\tDon't
forget me this weekend!\n\n"


That's pretty telling, isn't it? It's looking for the file of which you 
passed the filename. Since you passed XML string data as filename here, it 
can't find it.


Wrap the data in a StringIO.StringIO instance instead (or io.BytesIO in 
Python 2.6 and later) and pass that into iterparse().


Stefan

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


Re: [Tutor] Question

2010-06-20 Thread Alan Gauld

"Mark Lawrence"  wrote
...I take issue with this "learn a language in five minutes" bit. 
It took me five years to get to grips with plain old C.


C is non trivial but not hard (certainly compared to C++)
But 5 years is enough to do a lot more than "get to grips with"
a language, that's what I'd expect it to take to become fully
fluent, maybe even the office expert!


Assume that the same applies to all other languages


No, the diffference is huge between languages. I learned both
awk and Logo in a week. Smalltalk took me three attempts and
I still struggle every time I go back to it after 24 years!  Same
with Lisp, I am still learning Lisp  despite having written production
code in it 15 years ago! (Fortunately I have 3 Lisp guru's, one of
whom was on the standards comittee, to whom I can turn for help!)

But C only took me about 6 months to become proficient(*).
COBOL about the same. Forth is easy to learn but difficult
to master.

(*)By which I mean good enough to write a control system
for a cable extrusion line in a factory and a data encryption
card for the original IBM PC, including intercepting BIOS
calls etc. A guru C programmer could find lots of faults in
my code but it worked and the clients paid money for
the results!

C++ took me about 2 years to feel comfortable and
about 5 years to become one of  the office gurus.
The biggest system I've ever been involved with was
primarily written in C++ - about 3 million lines of code.
It only took about 2 years away to lose that skill again!

to make it simple, that means for me to get to grips with 30 
languages takes 150 years.  I don't think I'll live to do that.


To me, "get to grips" means good enough to write effective
working code that can be used in professional production systems.
A professional programmer often has no choice but to learn a new
language and a new start will be lucky to get more than a
couple of months to get up to full speed. You will be expected
to be writing code within a couple of weeks, albeit with the
manual permanently by your side.

As I said earlier I've worked on single projects with over a
dozen different languages going at once, if we'd waited for
5 years for each language we'd still be studying yet! As it
was the total project time was only 3 years. And some of
those languages were bespoke creations for that project.

You don't need to be an expert in a language to be productive.
And learning new languages invariably improves your skills
in the ones you already know. That's why I've learned several
languages that I've never used in projects - but the concepts
they embodied were significant (Smalltalk is a good example.
A colleague was using it in 1986 and I was fascinated by
this new OOP paradigm so I started to use his environment
during lunches and so on and learn the basics, but I have
never use Smalltalk myself in a paid project - but I learned
a lot from it. Haskell is another in the same category)

Learning multiple languages is a way of improving your
skills in your existing languages. It makes you a better
programmer.

But, it only works once you understand the basics and for
that sticking to a single language is probably best.
And Python is an ideal starter.

Alan G. 



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


Re: [Tutor] Question

2010-06-20 Thread Alan Gauld


"Payal"  wrote
Hijacking the thread a bit. What about learning Jython and Python? 
Do I

need to know Java to learn Jython?


No, but you will need to know the class heirarchy and reading
the documentation will be difficulty without at least a basic
knowledge of the Java syntax. Also you will probably need to use
the Java tools so familiarity with the JDK is useful.

OTOH you can treat Jython exactly like Python and never
look at Java - but that would be pointless! All you would have
is a slower, less fully featured version of Python! Jython only
makes sense if you are mixing Python and Java code in some
way. And if you are doing that you probably need to know
Java too.

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] How to add extra Vbox fields dynamically

2010-06-20 Thread Lang Hurst
OK, figured that was probably bad etiquette, but there doesn't seem to 
be close to the same traffic.  Mea culpa.  I won't do it again.  I think 
most of my issues have to do with the gtk part, so I'll post there for 
the most part.  Thanks.


Timo wrote:

On 20-06-10 04:04, Lang Hurst wrote:
OK, I just did the ugliest hack, from someone who only seems to do 
ugly hacks.  I set up a bunch of textview areas and defaulted them to 
'not visible'.  Then as I loop through my query results, I make them 
visible one at a time.  Well, it works perfect, but it just doesn't 
seem right for some reason.
You also posted this on the PyGTK mailing list, which is the correct 
place for these problems. Please post your PyGTK questions only there, 
do not double post. I'll answer you there.


Cheers,
Timo



Lang Hurst wrote:
I hope that I'm asking this in the right place.  I don't have too 
much trouble hacking together command line stuff, but the GUI part 
is a struggle for me.


I created a UI in glade.  It has a couple of Vboxes for 
information.  The final box is filled with a TextView.  In my 
program, I'm connecting to a database and pulling out a series of 
records.  As it stands, I can pull out all the records and view them 
in the TextView, but I would like to be able to have each result be 
a separate TextView (which I then have to figure out how to make 
clickable...)


Right now, this part looks like:

   query = 'SELECT subject, chapter_module, credits, final_test_score,
   notes FROM credits WHERE id=' + student[0][6]
   cursor.execute(query)
   credits = cursor.fetchall()
   temp = ''
   for credit in credits:
   sub_buf = 15 - len(credit[0])
   chap_buf = 15 - len(credit[1])
   cred_buf = 5 - len(credit[2])
   score_buf = 5 - len(credit[1])
   temp = temp + credit[0] + " " * sub_buf + credit[1] + " " *
   chap_buf + "Credits: " + credit[2] + " " * chap_buf +  "Score: " +
   credit[3] + "\n\nNOTES: " + credit[4] + "\n" + " " * 5 + "-" * 50 +
   "\n\n"

   # I would like to loop something here
# to have multiple text areas added

   buff = self.builder.get_object('textview1').get_buffer()
   buff.set_text(temp)


This works fine.  It pulls the records out of the database, and then 
cats the results together and throws it into my TextView.  I'm happy 
with the results so far, but I would like to be able to click on 
each record if I see something that needs to be modified.  As 
always, any help is appreciated.


Also, can anyone recommend a good book for gtk + glade + python?  I 
went out and bought Learning Python, but book at B&N that were 
remotely GUI related seems very outdated or just tkinter related, 
and just about all the gtk+python examples and tutorials don't use 
glade.  Thanks again.



-Lang






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





--
There are no stupid questions, just stupid people.

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


[Tutor] Converting audio samples from 16-bit signed int to float?

2010-06-20 Thread Joe Veldhuis
Hello all. I'm writing a program that needs to capture audio from a soundcard 
and run FFTs to determine peak frequency for further processing. The 
soundcard's native capture format is 16-bit little-endian signed integer 
samples (values 0-65535), and of course the FFT function requires 
floating-point values (-1.0 - +1.0).

So, what is the most efficient way to do the necessary conversion? I'm using 
the pyalsaaudio module to access the soundcard, if it matters.

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


[Tutor] New to this

2010-06-20 Thread Neil Thorman
I'm picking this up as a hobby really, not having done any programming since
Acorn I'm pretty much starting form scratch (and even back in the BASIC day
I never really got to grips with files).
This is from Alan Gauld's Learning to Program: Handling Files.
http://www.freenetpages.co.uk/hp/alan.gauld/

Can I just check I'm getting
it?


*Menu.txt contains*

*Spam & Eggs*
*Spam & Chips*
*Spam & Spam*

>>>inp = file("menu.txt", "r")
*What is inp? What does it now contain?*
*The following creates a list;*
*
*
*
>>>print inp.readlines()
['spam & eggs\n', 'spam & chips\n', 'spam & spam']

but if I do it again I get:
>>> print inp.readlines()
[]

I'm baffled, why is inp now empty?

Many thanks

Neil

ps. I'm working in the IDLE Python Shell.
*
*
*
*
*
*
*
*
*


This email is confidential and intended for addressee only .
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Data exchange formats...

2010-06-20 Thread Modulok
List,

What's the best format to send data across the wire between processes?

I have some simple 'insensitive' data I need to send from a client, to
a server via a TCP socket. Things like 'count = 10, name="foo"' and so
forth. Basic values. I would use something like the 'pickle' module to
pack them up send as encoded strings, which would then be loaded on
the server. It'd be nice, but the server has no authentication.
Therefore:

"Warning The pickle module is not intended to be secure against
erroneous or maliciously constructed data. Never unpickle data
received from an untrusted or unauthenticated source."

Currently I'm sending strings and using regular expressions on the
server to pluck out the needed data, but thought there must be
something cleaner, nicer and better. Ideas?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this

2010-06-20 Thread Alex Hall
On 6/20/10, Neil Thorman  wrote:
> I'm picking this up as a hobby really, not having done any programming since
> Acorn I'm pretty much starting form scratch (and even back in the BASIC day
> I never really got to grips with files).
> This is from Alan Gauld's Learning to Program: Handling Files.
> http://www.freenetpages.co.uk/hp/alan.gauld/
>
> Can I just check I'm getting
> it?
>
>
> *Menu.txt contains*
>
> *Spam & Eggs*
> *Spam & Chips*
> *Spam & Spam*
>
inp = file("menu.txt", "r")
> *What is inp? What does it now contain?*
It is now a reference to the location of the txt file. Python calls
these file "objects", where an object is just something on which you
can call functions. If you had a dog object you might call a "bark"
method; here, we have a file object, so we can see what the file is.
inp is not the file itself, just as any object is not the full
object's info but rather a pointer to where that info is. If you were
to print inp, I think you would get a memory address.
> *The following creates a list;*
> *
> *
> *
print inp.readlines()
> ['spam & eggs\n', 'spam & chips\n', 'spam & spam']
>
> but if I do it again I get:
 print inp.readlines()
> []
>
> I'm baffled, why is inp now empty?
I suspect you have hit the end of the file. I rarely work with files
myself, but I believe there is a way to reset your pointer in the file
to the top; after the readlines call, that pointer is at the end of
the file.
>
> Many thanks
>
> Neil
>
> ps. I'm working in the IDLE Python Shell.
> *
> *
> *
> *
> *
> *
> *
> *
> *
>
>
> This email is confidential and intended for addressee only .
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting audio samples from 16-bit signed int to float?

2010-06-20 Thread Adam Bark
On 20 June 2010 23:52, Joe Veldhuis  wrote:

> Hello all. I'm writing a program that needs to capture audio from a
> soundcard and run FFTs to determine peak frequency for further processing.
> The soundcard's native capture format is 16-bit little-endian signed integer
> samples (values 0-65535), and of course the FFT function requires
> floating-point values (-1.0 - +1.0).
>
> So, what is the most efficient way to do the necessary conversion? I'm
> using the pyalsaaudio module to access the soundcard, if it matters.
>
> -Joe Veldhuis
>

Not sure it's the best way but the most obvious to me would be divide by
32767.5 (ie half 65535) and minus 1 puts you into the right range:

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


Re: [Tutor] New to this

2010-06-20 Thread Adam Bark
On 20 June 2010 19:38, Neil Thorman  wrote:

> I'm picking this up as a hobby really, not having done any programming
> since Acorn I'm pretty much starting form scratch (and even back in the
> BASIC day I never really got to grips with files).
> This is from Alan Gauld's Learning to Program: Handling Files.
> http://www.freenetpages.co.uk/hp/alan.gauld/
>
> Can I just check I'm getting
> it?
>
>
> *Menu.txt contains*
>
> *Spam & Eggs*
> *Spam & Chips*
> *Spam & Spam*
>
> >>>inp = file("menu.txt", "r")
> *What is inp? What does it now contain?*
> *The following creates a list;*
>

inp is a file object.


> *
> *
> *
> >>>print inp.readlines()
> ['spam & eggs\n', 'spam & chips\n', 'spam & spam']
>
> *
>

readlines is a method of inp. It's basically a function that works
specifically on that object, in this case it reads each line from the file
and puts it into a list and then returns it; the shell then prints it out.


> *
> but if I do it again I get:
> >>> print inp.readlines()
> []
>
> *
>

At this point you have reached the end of the file so there are no more
lines to read, if you just want one line at a time then you should use
inp.read()


> *
> I'm baffled, why is inp now empty?
> *
>

inp isn't really empty it's still a file object there is just no data left
to read.


> *
>
> Many thanks
>
> Neil
>
> ps. I'm working in the IDLE Python Shell.
> *
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this

2010-06-20 Thread Alan Gauld


"Neil Thorman"  wrote 


inp = file("menu.txt", "r")

*What is inp? What does it now contain?*

print inp.readlines()

['spam & eggs\n', 'spam & chips\n', 'spam & spam']

but if I do it again I get:

print inp.readlines()

[]

I'm baffled, why is inp now empty?


OK, I've been asked thios before so I guess I need to add an 
explanation to the tutor!


When you work with  files its like using a cursor to indicate 
where you are in the file. When you firwst open the file the 
cursor is at the beginning of the file. As you read content the 
cursor moves through the file. If you use teadlines() you read 
the whole file in so that the cursor is at the end of the file. 
If you try calling readlines again there is no more data to 
read so you get an empty list.


You can reset the cursor using the seek() method

inp.seek(0)

Now readlines() will return the data again.

I'll try to write that up a bit more clearly and add it to the files 
topic.


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] Data exchange formats...

2010-06-20 Thread Alan Gauld


"Modulok"  wrote

What's the best format to send data across the wire between 
processes?


That depends on what you measure as 'best' - data volume, speed of
transmission, security, data complexity, flexibility, ease of decoding 
etc etc.


a server via a TCP socket. Things like 'count = 10, name="foo"' and 
so

forth. Basic values.


You could use the struct module to pack them as binary then unpack
at the far end. Its space efficient and relatively easy to pack/unpack
and marginally more secure than plain text. But only marginally!


I would use something like the 'pickle' module to
pack them up send as encoded strings, which would then be loaded on
the server. It'd be nice, but the server has no authentication.
Therefore:

"Warning The pickle module is not intended to be secure against
erroneous or maliciously constructed data. Never unpickle data
received from an untrusted or unauthenticated source."


That might not be a problem if security is not an issue.


Currently I'm sending strings and using regular expressions on the
server to pluck out the needed data, but thought there must be
something cleaner, nicer and better. Ideas?


XML? There are a variety of parsers out there.
http form submission? Again there are numerous CGI solutions.
You could even use https for security...

Or you can construct a plain text string - comma separated
using the csv module, and then encryupt using one of the
crypto algorithms available.

It all depends on what you want to do.

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] New to this

2010-06-20 Thread Alan Gauld


"Neil Thorman"  wrote


This is from Alan Gauld's Learning to Program: Handling Files.
http://www.freenetpages.co.uk/hp/alan.gauld/


One other thing. That page is now quite old and out of date. You 
should switch to the current web site:


http://www.alan-g.me.uk/


inp = file("menu.txt", "r")

*What is inp? What does it now contain?*


inp is a variable and it is referencing the file object you created
using the file() function. (As the tutor points out you could
also use open() and in fact open() is now preferred over 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] Data exchange formats...

2010-06-20 Thread Steven D'Aprano
On Mon, 21 Jun 2010 09:01:12 am Modulok wrote:
> List,
>
> What's the best format to send data across the wire between
> processes?

Consider json or yaml. json comes in the standard library, at least in 
version 2.6; yaml does not. I don't know if they are secure, but it's 
worth checking.

If your data consists of key:value pairs (which apparently it does), 
also consider the plistlib module.


> I have some simple 'insensitive' data I need to send from a client,
> to a server via a TCP socket. Things like 'count = 10, name="foo"'
> and so forth. Basic values. I would use something like the 'pickle'
> module to pack them up send as encoded strings, which would then be
> loaded on the server. It'd be nice, but the server has no
> authentication. Therefore:
>
> "Warning The pickle module is not intended to be secure against
> erroneous or maliciously constructed data. Never unpickle data
> received from an untrusted or unauthenticated source."

What's your threat model? Malicious sys admin on the remote client? CIA 
listening in? Business competitor injecting erroneous data? Receiving 
data from random places on the Internet? You control both machines, 
right next to each other in the same locked server room guarded by 
crocodiles, and you have the only key?

Depending on how serious your threat model is, the right solution might 
be to insist on authentication on both machines and use SSH between the 
two.


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


Re: [Tutor] New to this

2010-06-20 Thread Steven D'Aprano
On Mon, 21 Jun 2010 09:02:55 am Alex Hall wrote:
> On 6/20/10, Neil Thorman  wrote:
[...]
> inp = file("menu.txt", "r")
> >
> > *What is inp? What does it now contain?*
>
> It is now a reference to the location of the txt file. 

[pedantic]
No, it's actually an abstract data structure that wraps the actual file 
itself. A reference to the *location* of the file would be:

inp = "menu.txt"

Locations are strings. File objects are file objects.
[/pedantic]

But in a practical sense, pay no attention to the man behind the curtain 
(the implementation details). inp is a file in every way which matters, 
just like after:

n = 42
x = 1.234

n is an int and x a float in every way which matters.


> Python calls 
> these file "objects", where an object is just something on which you
> can call functions. If you had a dog object you might call a "bark"
> method; here, we have a file object, so we can see what the file is.
> inp is not the file itself, just as any object is not the full 
> object's info but rather a pointer to where that info is. If you were
> to print inp, I think you would get a memory address.

You would get a string printed to the console, like printing *anything*.

>>> print inp


That string happens to contain a hex number which looks like it could be 
a memory address, but that's an implementation detail because CPython 
doesn't sufficiently abstract its objects from the underlying C 
implementation.

Python file objects aren't "files" only in the sense that they exist in 
memory rather than on disk in the file system, but other than that, I 
believe your explanation is at too low a level to be helpful. Neil said 
he's a beginner who hasn't done any programming since BASIC on an 
Acorn, and you're talking about low-level details like memory 
locations. Let me guess, you're also a C programmer?

As far as coding in Python is concerned, inp = file(pathname) creates a 
file object, which *is* a file in all practical sense. Everything else 
is just an implementation detail, which could change depending on the 
version of Python, the operating system, and the underlying hardware.


[...]
> print inp.readlines()
> >
> > ['spam & eggs\n', 'spam & chips\n', 'spam & spam']
> >
> > but if I do it again I get:
>  print inp.readlines()
> >
> > []
> >
> > I'm baffled, why is inp now empty?
>
> I suspect you have hit the end of the file. 

Yes. readlines reads from the current file position, like all read 
operations. To read all the text again, you have to reset the file 
position with seek:

inp.seek(0)



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


Re: [Tutor] New to this

2010-06-20 Thread Alex Hall
On 6/20/10, Steven D'Aprano  wrote:
> On Mon, 21 Jun 2010 09:02:55 am Alex Hall wrote:
>> On 6/20/10, Neil Thorman  wrote:
> [...]
>> inp = file("menu.txt", "r")
>> >
>> > *What is inp? What does it now contain?*
>>
>> It is now a reference to the location of the txt file.
>
> [pedantic]
> No, it's actually an abstract data structure that wraps the actual file
> itself. A reference to the *location* of the file would be:
>
> inp = "menu.txt"
>
> Locations are strings. File objects are file objects.
> [/pedantic]
>
> But in a practical sense, pay no attention to the man behind the curtain
> (the implementation details). inp is a file in every way which matters,
> just like after:
>
> n = 42
> x = 1.234
>
> n is an int and x a float in every way which matters.
>
>
>> Python calls
>> these file "objects", where an object is just something on which you
>> can call functions. If you had a dog object you might call a "bark"
>> method; here, we have a file object, so we can see what the file is.
>> inp is not the file itself, just as any object is not the full
>> object's info but rather a pointer to where that info is. If you were
>> to print inp, I think you would get a memory address.
>
> You would get a string printed to the console, like printing *anything*.
>
 print inp
> 
Oh right, the object's toString method (or whatever Python calls this;
Java and Javascript call it toString, and it exists for all objects).
>
> That string happens to contain a hex number which looks like it could be
> a memory address, but that's an implementation detail because CPython
> doesn't sufficiently abstract its objects from the underlying C
> implementation.
>
> Python file objects aren't "files" only in the sense that they exist in
> memory rather than on disk in the file system, but other than that, I
> believe your explanation is at too low a level to be helpful. Neil said
> he's a beginner who hasn't done any programming since BASIC on an
> Acorn, and you're talking about low-level details like memory
> locations. Let me guess, you're also a C programmer?
Good point, and sorry for going into too much detail, much of which is
not on the mark anyway. :) No, I have hardly touched c++, but I am
starting my final year of a computer science degree in a few months so
I have had all the details of objects and how the computer actually
accesses them in several classes.
>
> As far as coding in Python is concerned, inp = file(pathname) creates a
> file object, which *is* a file in all practical sense. Everything else
> is just an implementation detail, which could change depending on the
> version of Python, the operating system, and the underlying hardware.
Very true.
>
>
> [...]
>> print inp.readlines()
>> >
>> > ['spam & eggs\n', 'spam & chips\n', 'spam & spam']
>> >
>> > but if I do it again I get:
>>  print inp.readlines()
>> >
>> > []
>> >
>> > I'm baffled, why is inp now empty?
>>
>> I suspect you have hit the end of the file.
>
> Yes. readlines reads from the current file position, like all read
> operations. To read all the text again, you have to reset the file
> position with seek:
>
> inp.seek(0)
>
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor