Re: [Tutor] help with random.randint (cont. -- now: pseudo code)

2010-02-02 Thread bob gailer

David wrote:

[snip]


My suggestion (untested):

MAX = 12
NQ = 20 # of questions to ask

# create a 2 dimensional array of 1's
row = [1]*MAX
pool = [row[:] for i in range(MAX)]

incorrect = [] # store incorrectly answered combos here

def askQuestions():  # generate and ask questions:
 for i in range(NQ):
   while 1: # loop till we get an unused combo
 x, y = [random.randint(1,MAX) for i in 'ab']
 if mtable[x][y] == 1: # combo is available
   break
   askQuestion(x,y)
   # indicate asked
   mtable[x][y] = 0
   mtable[y][x] = 0
 showStats()

def askQuestion(x,y):
 solution = x*y
 # take answer from user
 ok =  user answer == solution
 if ok:
   correct += 1
 else:
   incorrect.append((x,y))
 return ok

def askFaultyAnswers():
 answer = raw_input("Try again the incorrect questions? (y/n) ")
 if answer == "y":
   correct = 0
   for x,y in incorrect:
 ok = askQuestion(x,y)
 # could use ok to remove combo from incorrect list.
 showStats()

askQuestions()
askFaultyAnswers()
print "good-bye!"






I think it is sensible to

* first create all possible solutions, then
* kick out doublettes, and only then
* ask questions

I have some questions though:

Is the overall structure and flow of this program okay? What are the 
main problems you can spot immediately


Calculating kicking randomizing is overkill. My code uses a 2 dimension 
array to track which x,y combos are available.


break is only valid within a loop.

Recursively calling askQuestions is not a good idea. Save recursion for 
when it is is meaningful. Use a loop instead.


There is no need for an incorrect counter. we can calculate it later 
(incorrect = NQ -correct)




In the very end I would like to take this code as a basis for a 
wxPython program. Are there any structural requirements I am violating 
here?


Not that I can see. It is common practice to separate logic from display.


If I want to limit the number of questions asked, say to 20, would I 
operate with slicing methods on the randomised pool?


My solution does not use a randomized pool. Just a loop over range(NQ)


How would you go about showing stats for the second run (i.e. the 
FaultyAnswers)? Right now I am thinking of setting the global 
variables correct and incorrect to 0 from _within_ askFaultyAnswers; 
then I would run showStats() also from _within_ askFaultyAnswers. Good 
idea?


Yes indeed. That is what I did before reading your comment! Great minds 
think alike.


--
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] help with random.randint

2010-02-02 Thread David

Hello Bob,

thanks for your comments!


On 03/02/10 14:51, bob gailer wrote:


or if you seek terseness:

terms = [random.randint(1, 99) for i in 'ab']


Do I understand correctly that 'ab' here merely serves to produce a 
'dummy sequence' over which I can run the for loop?


David

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


Re: [Tutor] help with random.randint

2010-02-02 Thread bob gailer

David wrote:

Hello list,

I thought this was easy even for me, but I was wrong, I guess.
Here is what I want to do: take two random numbers between 1 and 99, 
and put them into a list. 

[snip]

Or you can use list comprehension:

terms = [random.randint(1, 99) for i in range(2)]

or if you seek terseness:

terms = [random.randint(1, 99) for i in 'ab']


--
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] help with random.randint (cont. -- now: pseudo code)

2010-02-02 Thread David

Hello Benno, list,

thanks for those clarifications, which, well, clarify things ;-)

This is my latest creation:

import random

def createTerms():
terms =  []
for i in range(2):
terms.append(random.randint(1, 99))
j = terms[0]
k = terms[1]
print "%3d\nx%2d" % (j, k)

createTerms()

Which works. However, it merely prints a multiplication task. Anyway, 
this was just a prelude. In the end, I want far more, namely to create, 
ask, and verify some multiplication questions. Here is my pseudo code of 
this little project:




pool = []
correct = 0
incorrect = 0

def createQuestions:
generate all multiplication combinations possible
append as tuple to pool
eliminate 'mirrored doubles' (i.e. 7x12 and 12x7)
randomize pool

def askQuestions:
for question in pool:
calculate solution
take answer from user
if user answer == solution:
correct += 1
remove question from pool
else:
incorrect += 1

def showStats:
print number of questions asked
print number of questions answered correctly
print percentage of correct answers

def askFaultyAnswers:
answer = raw_input("Try again the incorrect questions? (y/n) ")
if answer == "y":
aksQuestions()
else:
break


createQuestions()
askQuestions()
showStats()
askFaultyAnswers()
print "good-bye!"



I think it is sensible to

* first create all possible solutions, then
* kick out doublettes, and only then
* ask questions

I have some questions though:

Is the overall structure and flow of this program okay? What are the 
main problems you can spot immediately?


In the very end I would like to take this code as a basis for a wxPython 
program. Are there any structural requirements I am violating here?


If I want to limit the number of questions asked, say to 20, would I 
operate with slicing methods on the randomised pool?


How would you go about showing stats for the second run (i.e. the 
FaultyAnswers)? Right now I am thinking of setting the global variables 
correct and incorrect to 0 from _within_ askFaultyAnswers; then I would 
run showStats() also from _within_ askFaultyAnswers. Good idea?


Many thanks for your guidance and input!

David






On 03/02/10 12:25, Benno Lang wrote:

On Wed, Feb 3, 2010 at 12:21 PM, David  wrote:

Hello list,

I thought this was easy even for me, but I was wrong, I guess.
Here is what I want to do: take two random numbers between 1 and 99, and put
them into a list.

import random
terms =  []
for i in range(2):
terms = random.randint(1, 99)


All you're doing here is assigning an integer value to 'terms', twice.
This assignment means 'terms' is no longer a list, but is now just an
int. What you want is probably:
terms.append (random.randint(1, 99))


So I tried to change line 4 to the following:
terms += random.randint(1, 99)


You can't freely mix types with python operators, i.e. a_list += an_int
But you can use += with 2 ints or 2 lists, so you could do:
terms += [random.randint(1, 99)]
I think using append is much nicer though.


I understand this error thus: once an int has been placed into the list
terms, no further int can be added. But: terms is a mutable list, and NOT an
'int' object!


The int was never added to the list in the first place, because list
+= int is not something Python understands.


So here are my questions: what is the problem, and how can I generate two
random numbers and store them (preferably in a tuple)?


I hope what I wrote above answers the first question.
IIRC tuples are immutable, so you either to create the list first and
then convert it to a tuple:
terms_tuple = tuple(terms)

Or you can create a tuple from the beginning (without a loop):
terms_tuple = (random.randint(1, 99), random.randint(1, 99))

HTH,
benno



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


Re: [Tutor] help with random.randint

2010-02-02 Thread Benno Lang
On Wed, Feb 3, 2010 at 12:21 PM, David  wrote:
> Hello list,
>
> I thought this was easy even for me, but I was wrong, I guess.
> Here is what I want to do: take two random numbers between 1 and 99, and put
> them into a list.
>
> import random
> terms =  []
> for i in range(2):
>        terms = random.randint(1, 99)

All you're doing here is assigning an integer value to 'terms', twice.
This assignment means 'terms' is no longer a list, but is now just an
int. What you want is probably:
terms.append (random.randint(1, 99))

> So I tried to change line 4 to the following:
>        terms += random.randint(1, 99)

You can't freely mix types with python operators, i.e. a_list += an_int
But you can use += with 2 ints or 2 lists, so you could do:
terms += [random.randint(1, 99)]
I think using append is much nicer though.

> I understand this error thus: once an int has been placed into the list
> terms, no further int can be added. But: terms is a mutable list, and NOT an
> 'int' object!

The int was never added to the list in the first place, because list
+= int is not something Python understands.

> So here are my questions: what is the problem, and how can I generate two
> random numbers and store them (preferably in a tuple)?

I hope what I wrote above answers the first question.
IIRC tuples are immutable, so you either to create the list first and
then convert it to a tuple:
terms_tuple = tuple(terms)

Or you can create a tuple from the beginning (without a loop):
terms_tuple = (random.randint(1, 99), random.randint(1, 99))

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


[Tutor] help with random.randint

2010-02-02 Thread David

Hello list,

I thought this was easy even for me, but I was wrong, I guess.
Here is what I want to do: take two random numbers between 1 and 99, and 
put them into a list.


import random
terms =  []
for i in range(2):
terms = random.randint(1, 99)
print terms

This prints just one number (the last one generated in the loop?)

So I tried to change line 4 to the following:
terms += random.randint(1, 99)
hoping that it would add a second integer to my terms list. But I get an 
error:


/home/david/Documents/Python-Projekt/multiplier.py in ()
  2 terms =  []
  3 for i in range(2):
> 4 terms += random.randint(1, 99)
  5 print terms
  6

TypeError: 'int' object is not iterable
WARNING: Failure executing file: 

I understand this error thus: once an int has been placed into the list 
terms, no further int can be added. But: terms is a mutable list, and 
NOT an 'int' object!


So here are my questions: what is the problem, and how can I generate 
two random numbers and store them (preferably in a tuple)?


Many thanks,

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


Re: [Tutor] Question about importing

2010-02-02 Thread Alan Gauld


"Grigor Kolev"  wrote 


Can I use something like this
#--
import sys
sys.path.append("/home/user/other")
import module
#-


Yes but if you have a lot of modules in there that you might 
want to use in other programs you might prefer to add the 
folder to your PYTHONPATH environment variable. That 
way Python will always look in that folder for your modules.


Thats what I do - I gave a folder whee I put all my reusable 
modules. This folder is not under my Python install folder 
so that when I install a new Python and delete the old I 
don't lose my code... And the PYTHONPATH variable 
works with the new Python.


--
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] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 4:56 PM, Norman Khine  wrote:
> On Tue, Feb 2, 2010 at 10:11 PM, Kent Johnson  wrote:

>> Try this version:
>>
>> data = file.read()
>>
>> get_records = re.compile(r"""openInfoWindowHtml\(.*?\ticon:
>> myIcon\n""", re.DOTALL).findall
>> get_titles = re.compile(r"""(.*)<\/strong>""").findall
>> get_urls = re.compile(r"""a href=\"\/(.*)\">En savoir plus""").findall
>> get_latlngs = 
>> re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\n\s*(\-?\d+\.\d*)\)""").findall
>>
>> then as before.
>>
>> Your repr() call is essentially removing newlines from the input by
>> converting them to literal '\n' pairs. This allows your regex to work
>> without the DOTALL modifier.
>>
>> Note you will get slightly different results with my version - it will
>> give you correct utf-8 text for the titles whereas yours gives \
>> escapes. For example one of the titles is "CGTSM (Satére Mawé)". Your
>> version returns
>>
>> {'url': 'cgtsm-satere-mawe.html', 'lating': ('-2.77804',
>> '-79.649735'), 'title': 'CGTSM (Sat\\xe9re Maw\\xe9)'}
>>
>> Mine gives
>> {'url': 'cgtsm-satere-mawe.html', 'lating': ('-2.77804',
>> '-79.649735'), 'title': 'CGTSM (Sat\xc3\xa9re Maw\xc3\xa9)'}
>>
>> This is showing the repr() of the title so they both have \ but note
>> that yours has two \\ indicating that the \ is in the text; mine has
>> only one \.
>
> i am no expert, but there seems to be a bigger difference.
>
> with repr(), i get:
> Sat\\xe9re Maw\\xe9
>
> where as you get
>
> Sat\xc3\xa9re Maw\xc3\xa9
>
> repr()'s
> é == \\xe9
> whereas on your version
> é == \xc3\xa9

Right. Your version has four actual characters in the result - \, x,
e, 9. This is the escaped representation of the unicode representation
of e-acute. (The \ is doubled in the repr display.)

My version has two bytes in the result, with the values c3 and a9.
This is the utf-8 representation of e-acute.

If you want to accurately represent (i.e. print) the title at some
later time you probably want the utf-8 represetation.
>
>>
>> Kent
>>
>
> also, i still get an empty list when i run the code as suggested.

You didn't change the regexes. You have to change \\t and \\n to \t
and \n because the source text now has actual tabs and newlines, not
the escaped representations.

I know this is confusing, I'm sorry I don't have time or patience to
explain more.

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


Re: [Tutor] Question about importing

2010-02-02 Thread Dave Angel

Eike Welk wrote:

On Tuesday February 2 2010 20:28:03 Grigor Kolev wrote:
  

Can I use something like this
#--
import sys
sys.path.append("/home/user/other")
import module
#-




Yes I think so. I just tried something similar:
--


IPython 0.10 -- An enhanced Interactive Python.

<--- snip >

In [1]: import sys

In [2]: 
sys.path.append("/home/eike/codedir/freeode/trunk/freeode_py/freeode/")


<--- snip >
<--- The next line is a special command of IPython: >

In [8]: !ls /home/eike/codedir/freeode/trunk/freeode_py/freeode/
ast.py   pygenerator.pyctest_1_interpreter.pyc   
test_pygenerator.pyc
ast.pyc  simlcompiler.pytest_2_interpreter.py  
test_simlcompiler.py
__init__.py  simlcompiler.pyc   test_2_interpreter.pyc 

<--- snip >



In [9]: import simlcompiler
---
ImportError   Traceback (most recent call last)

/home/eike/ in ()

/home/eike/codedir/freeode/trunk/freeode_py/freeode/simlcompiler.py in 
()

 36 import stat
 37 from subprocess import Popen #, PIPE, STDOUT
---> 38 import pyparsing
 39 import freeode.simlparser as simlparser
 40 import freeode.interpreter as interpreter

ImportError: No module named pyparsing


--
Well... the import fails, but it finds the module and starts to import it. 



HTH,
Eike.



  
I have no idea what freode looks like, but I have a guess, based on your 
error messages.


I'd guess that you want to append without the freeode directory:


sys.path.append("/home/eike/codedir/freeode/trunk/freeode_py/")

and import with it.  That's because freeode is a package name, not a 
directory name (I can tell because __init__.py is present)

 import freeode.simlcompiler

See if that works any better.

DaveA

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


Re: [Tutor] parse text file

2010-02-02 Thread Norman Khine
On Tue, Feb 2, 2010 at 10:11 PM, Kent Johnson  wrote:
> On Tue, Feb 2, 2010 at 1:39 PM, Norman Khine  wrote:
>> On Tue, Feb 2, 2010 at 4:19 PM, Kent Johnson  wrote:
>>> On Tue, Feb 2, 2010 at 9:33 AM, Norman Khine  wrote:
 On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson  wrote:
> On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine  wrote:
>
> Why do you use repr() here?
>
>>>
>>> It smells of programming by guess rather than a correct solution to
>>> some problem. What happens if you take it out?
>>
>> when i take it out, i get an empty list.
>>
>> whereas both
>> data = repr( file.read().decode('latin-1') )
>> and
>> data = repr( file.read().decode('utf-8') )
>>
>> returns the full list.
>
> Try this version:
>
> data = file.read()
>
> get_records = re.compile(r"""openInfoWindowHtml\(.*?\ticon:
> myIcon\n""", re.DOTALL).findall
> get_titles = re.compile(r"""(.*)<\/strong>""").findall
> get_urls = re.compile(r"""a href=\"\/(.*)\">En savoir plus""").findall
> get_latlngs = 
> re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\n\s*(\-?\d+\.\d*)\)""").findall
>
> then as before.
>
> Your repr() call is essentially removing newlines from the input by
> converting them to literal '\n' pairs. This allows your regex to work
> without the DOTALL modifier.
>
> Note you will get slightly different results with my version - it will
> give you correct utf-8 text for the titles whereas yours gives \
> escapes. For example one of the titles is "CGTSM (Satére Mawé)". Your
> version returns
>
> {'url': 'cgtsm-satere-mawe.html', 'lating': ('-2.77804',
> '-79.649735'), 'title': 'CGTSM (Sat\\xe9re Maw\\xe9)'}
>
> Mine gives
> {'url': 'cgtsm-satere-mawe.html', 'lating': ('-2.77804',
> '-79.649735'), 'title': 'CGTSM (Sat\xc3\xa9re Maw\xc3\xa9)'}
>
> This is showing the repr() of the title so they both have \ but note
> that yours has two \\ indicating that the \ is in the text; mine has
> only one \.

i am no expert, but there seems to be a bigger difference.

with repr(), i get:
Sat\\xe9re Maw\\xe9

where as you get

Sat\xc3\xa9re Maw\xc3\xa9

repr()'s
é == \\xe9
whereas on your version
é == \xc3\xa9

>
> Kent
>

also, i still get an empty list when i run the code as suggested.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tracking program

2010-02-02 Thread GOW524
All of you guys rock, I have so much to learn before I even attempt to work  
on my program. Thank you again for all your help.


Luis

On Feb 2, 2010 4:49am, Philip Kilner  wrote:

Hi Luis,





Luis Ortega wrote:



> I am fairly new to programming (which means I have never, ever, ever



> written a program). I have a book or two in Python, and so far I like



> it. I have a stupid question to ask; Is it possible to write an



> employee's internet tracking program in Python?



>





Wayne is right that you could do it as a proxy, but one question you can



ask is "which bits do I need to write in Python, and which bits can I



use an off the shelf solution for?".





One choice here is to use an existing proxy server that logs activity,



and then write a Python application to analyse the logs.





That might be a more approachable problem for you at this point.





HTH







--





Regards,





PhilK







'work as if you lived in the early days of a better nation'



- alasdair gray


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


Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 1:39 PM, Norman Khine  wrote:
> On Tue, Feb 2, 2010 at 4:19 PM, Kent Johnson  wrote:
>> On Tue, Feb 2, 2010 at 9:33 AM, Norman Khine  wrote:
>>> On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson  wrote:
 On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine  wrote:

 Why do you use repr() here?

>>
>> It smells of programming by guess rather than a correct solution to
>> some problem. What happens if you take it out?
>
> when i take it out, i get an empty list.
>
> whereas both
> data = repr( file.read().decode('latin-1') )
> and
> data = repr( file.read().decode('utf-8') )
>
> returns the full list.

Try this version:

data = file.read()

get_records = re.compile(r"""openInfoWindowHtml\(.*?\ticon:
myIcon\n""", re.DOTALL).findall
get_titles = re.compile(r"""(.*)<\/strong>""").findall
get_urls = re.compile(r"""a href=\"\/(.*)\">En savoir plus""").findall
get_latlngs = 
re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\n\s*(\-?\d+\.\d*)\)""").findall

then as before.

Your repr() call is essentially removing newlines from the input by
converting them to literal '\n' pairs. This allows your regex to work
without the DOTALL modifier.

Note you will get slightly different results with my version - it will
give you correct utf-8 text for the titles whereas yours gives \
escapes. For example one of the titles is "CGTSM (Satére Mawé)". Your
version returns

{'url': 'cgtsm-satere-mawe.html', 'lating': ('-2.77804',
'-79.649735'), 'title': 'CGTSM (Sat\\xe9re Maw\\xe9)'}

Mine gives
{'url': 'cgtsm-satere-mawe.html', 'lating': ('-2.77804',
'-79.649735'), 'title': 'CGTSM (Sat\xc3\xa9re Maw\xc3\xa9)'}

This is showing the repr() of the title so they both have \ but note
that yours has two \\ indicating that the \ is in the text; mine has
only one \.

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


Re: [Tutor] Question about importing

2010-02-02 Thread David Hutto

--- On Tue, 2/2/10, Grigor 
Kolev 
 wrote:

From: Grigor Kolev 

Subject: Re: [Tutor] Question about 
importing
To: "David Hutto" 
Cc: 
"Python Tutor" 
Date:
 Tuesday, February 2,
 2010, 2:54 PM

В 11:47 -0800 на 02.02.2010
 (вт), David Hutto написа:
> 
> 
> --- On Tue, 2/2/10,
 Grigor Kolev  
wrote:
>         
>         From: Grigor Kolev 
>         Subject: 
Re: [Tutor] Question about importing
>         To: "David Hutto" 

>         Cc:
 "Python Tutor" 
>   
      Date: Tuesday, February 2, 2010, 2:28 PM
>         
> 
        В 10:33 -0800 на 02.02.2010 (вт), David Hutto написа:
>   
      > 
>         > 
>         >
 --- On Tue, 2/2/10, Григор 
 wrote:
>         >         
>         >         From:
 Григор 
>     
    >         Subject: [Tutor] Question about importing
>      
   >         To: "Python Tutor" 
> 
        >         Date: Tuesday, February 2, 2010, 12:07
 PM
>         >         
>         >         Hi all.
> 
        >         How can I import a module which is located in the
> 
        upper
>         >         directory.
>      
   >         
>         >         
>         >     
    I think the following might be what you're looking
>      
   for:
>         >         
>   
      >
>            http://docs.python.org/tutorial/modules.html#the-module-search-path
> 
        >         
>         > 
>         Can I use 
something like this
>      
   #--
>      
   import sys
>         sys.path.append("/home/user/other")
> 
        import module
>      
   #-
>         --
 
>         Grigor Kolev
 
>          
> 
        
>         That's exactly what it says to do, but I 
haven't figured out
>         the path to append to mine yet.
> 
        
> 


Thanks for your help

It wasn't that 
much help, and I was about to do something similar anyway.. So if it 
makes you feel better, now we both know that's how it works.
-- 
Grigor
 Kolev 





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


Re: [Tutor] Question about importing

2010-02-02 Thread Eike Welk
On Tuesday February 2 2010 20:28:03 Grigor Kolev wrote:
> Can I use something like this
> #--
> import sys
> sys.path.append("/home/user/other")
> import module
> #-
> 

Yes I think so. I just tried something similar:
--


IPython 0.10 -- An enhanced Interactive Python.

<--- snip >

In [1]: import sys

In [2]: 
sys.path.append("/home/eike/codedir/freeode/trunk/freeode_py/freeode/")

<--- snip >
<--- The next line is a special command of IPython: >

In [8]: !ls /home/eike/codedir/freeode/trunk/freeode_py/freeode/
ast.py   pygenerator.pyctest_1_interpreter.pyc  
 
test_pygenerator.pyc
ast.pyc  simlcompiler.pytest_2_interpreter.py  
test_simlcompiler.py
__init__.py  simlcompiler.pyc   test_2_interpreter.pyc 

<--- snip >


In [9]: import simlcompiler
---
ImportError   Traceback (most recent call last)

/home/eike/ in ()

/home/eike/codedir/freeode/trunk/freeode_py/freeode/simlcompiler.py in 
()
 36 import stat
 37 from subprocess import Popen #, PIPE, STDOUT
---> 38 import pyparsing
 39 import freeode.simlparser as simlparser
 40 import freeode.interpreter as interpreter

ImportError: No module named pyparsing


--
Well... the import fails, but it finds the module and starts to import it. 


HTH,
Eike.


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


Re: [Tutor] Question about importing

2010-02-02 Thread Grigor Kolev
В 11:47 -0800 на 02.02.2010 (вт), David Hutto написа:
> 
> 
> --- On Tue, 2/2/10, Grigor Kolev  wrote:
> 
> From: Grigor Kolev 
> Subject: Re: [Tutor] Question about importing
> To: "David Hutto" 
> Cc: "Python Tutor" 
> Date: Tuesday, February 2, 2010, 2:28 PM
> 
> В 10:33 -0800 на 02.02.2010 (вт), David Hutto написа:
> > 
> > 
> > --- On Tue, 2/2/10, Григор  wrote:
> > 
> > From: Григор 
> > Subject: [Tutor] Question about importing
> > To: "Python Tutor" 
> > Date: Tuesday, February 2, 2010, 12:07 PM
> > 
> > Hi all.
> > How can I import a module which is located in the
> upper
> > directory.
> > 
> > 
> > I think the following might be what you're looking
> for:
> > 
> >
>http://docs.python.org/tutorial/modules.html#the-module-search-path
> > 
> > 
> Can I use something like this
> #--
> import sys
> sys.path.append("/home/user/other")
> import module
> #-
> -- 
> Grigor Kolev 
>  
> 
> That's exactly what it says to do, but I haven't figured out
> the path to append to mine yet.
> 
> 


Thanks for your help

-- 
Grigor Kolev 

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


Re: [Tutor] Question about importing

2010-02-02 Thread David Hutto


--- On Tue, 2/2/10, Grigor Kolev  wrote:

From: Grigor Kolev 
Subject: Re: [Tutor] Question about importing
To: "David Hutto" 
Cc: "Python Tutor" 
Date: Tuesday, February 2, 2010, 2:28 PM

В 10:33 -0800 на 02.02.2010 (вт), David Hutto написа:
> 
> 
> --- On Tue, 2/2/10, Григор  wrote:
>         
>         From: Григор 
>         Subject: [Tutor] Question about importing
>         To: "Python Tutor" 
>         Date: Tuesday, February 2, 2010, 12:07 PM
>         
>         Hi all.
>         How can I import a module which is located in the upper
>         directory.
>         
>         
>         I think the following might be what you're looking for:
>         
>         http://docs.python.org/tutorial/modules.html#the-module-search-path
>         
> 
Can I use something like this
#--
import sys
sys.path.append("/home/user/other")
import module
#-
-- 
Grigor Kolev 
 

That's exactly what it says to do, but I haven't figured out the path to append 
to mine yet.



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


Re: [Tutor] Question about importing

2010-02-02 Thread Grigor Kolev
В 10:33 -0800 на 02.02.2010 (вт), David Hutto написа:
> 
> 
> --- On Tue, 2/2/10, Григор  wrote:
> 
> From: Григор 
> Subject: [Tutor] Question about importing
> To: "Python Tutor" 
> Date: Tuesday, February 2, 2010, 12:07 PM
> 
> Hi all.
> How can I import a module which is located in the upper
> directory.
> 
> 
> I think the following might be what you're looking for:
> 
> http://docs.python.org/tutorial/modules.html#the-module-search-path
> 
> 
Can I use something like this
#--
import sys
sys.path.append("/home/user/other")
import module
#-
-- 
Grigor Kolev 

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


Re: [Tutor] Shashwat Anand you helped with search

2010-02-02 Thread Alan Gauld


"jim serson"  wrote

I think I am using the right method in the wrong way. 


I'm not sure what you expect this to do...

If you could tell me if I am trying the correct method or give 
me a push in the right direction that would be grate thanks.


Maybe a push...


look_in = raw_input ("Enter the search file to look in ")
search = raw_input ("Enter your search item ")


OK, so far. You want to look for search in the file.


c = open(look_in, "r").read().count(search.split()


Assuming there should be another closing paren...
this opens the file and reads it into memory. 
It then counts the occurences of a list produced 
by splitting the search string. This gives a Typeerror for me!

Is that what you wanted?
Or do you actually want to search for each item in your 
search string? I think you are trying to cram too much 
into one line - if for no other eason that it makes debugging 
almost impossible.


Try this:

txt = open(look_in).read()
search2 = search.split()

print search2# is this what you expect?
   
for item in search2:

c = txt.count(item)
print item, txt.count(c)# is this what you expect?
if c > 0:   print search, ",", c,"Of your search was found"
else:   print item, "was not found"
  
It might give you clues as to what you really want.
But using multiple lines is usually a good idea, 
especially when trying to get it working. Its much easier to 
see where things are breaking that way...



--
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] parse text file

2010-02-02 Thread Norman Khine
On Tue, Feb 2, 2010 at 4:19 PM, Kent Johnson  wrote:
> On Tue, Feb 2, 2010 at 9:33 AM, Norman Khine  wrote:
>> On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson  wrote:
>>> On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine  wrote:
>>>
 here are the changes:

 import re
 file=open('producers_google_map_code.txt', 'r')
 data =  repr( file.read().decode('utf-8') )
>>>
>>> Why do you use repr() here?
>>
>> i have latin-1 chars in the producers_google_map_code.txt' file and
>> this is the only way to get it to read the data.
>>
>> is this incorrect?
>
> Well, the repr() call is after the file read. If your data is latin-1
> you should decode it as latin-1, not utf-8:
> data = file.read().decode('latin-1')
>
> Though if the decode('utf-8') succeeds, and you do have non-ascii
> characters in the data, they are probably encoded in utf-8, not
> latin-1. Are you sure you have latin-1?
>
> The repr() call converts back to ascii text, maybe that is what you want?
>
> Perhaps you put in the repr because you were having trouble printing?
>
> It smells of programming by guess rather than a correct solution to
> some problem. What happens if you take it out?

when i take it out, i get an empty list.

whereas both
data = repr( file.read().decode('latin-1') )
and
data = repr( file.read().decode('utf-8') )

returns the full list.

here is the file
http://cdn.admgard.org/documents/producers_google_map_code.txt

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


Re: [Tutor] Question about importing

2010-02-02 Thread David Hutto


--- On Tue, 2/2/10, Григор  wrote:

From: Григор 
Subject: [Tutor] Question about importing
To: "Python Tutor" 
Date: Tuesday, February 2, 2010, 12:07 PM

Hi all.
How can I import a module which is located in the upper directory.


I think the following might be what you're looking for:

http://docs.python.org/tutorial/modules.html#the-module-search-path



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


[Tutor] Question about importing

2010-02-02 Thread Григор
Hi all.
How can I import a module which is located in the upper directory.


-- 
Криле имат само тия, дето дето сърцето им иска да лети !
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Shashwat Anand you helped with search

2010-02-02 Thread jim serson

I am trying to have the search return several numbers or words in a single 
sentences now. I don't know if I am using the wrong format I am trying to use 
the split method because I thought I could return several parts of a sentence 
with it. 
For example if it had 1 2 3 4 5 I thought I could search and return 1 3 5 or if 
it had “to set the function” I could return
 “set function” from that line, but I can’t get it working properly. 
 
I have tried several different approaches with split. I have tried to split it 
at the raw_input() line, at the if statement, putting it in a loop and 
declaring it separately. I have looked up, in the python library to make sure I 
am entering it properly. It will ether run trough and not work like I expect or 
it will come up with an error. 
 
I think I am using the right method in the wrong way. If you could tell me if I 
am trying the correct method or give me a push in the right direction that 
would be grate thanks.
 
look_in = raw_input ("Enter the search file to look in ")
search = raw_input ("Enter your search item ")
 
c = open(look_in, "r").read().count(search.split()
if c:   print search, ",", c,"Of your search was found"
else:   print "Your search was not found"
  
_

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


Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 9:33 AM, Norman Khine  wrote:
> On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson  wrote:
>> On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine  wrote:
>>
>>> here are the changes:
>>>
>>> import re
>>> file=open('producers_google_map_code.txt', 'r')
>>> data =  repr( file.read().decode('utf-8') )
>>
>> Why do you use repr() here?
>
> i have latin-1 chars in the producers_google_map_code.txt' file and
> this is the only way to get it to read the data.
>
> is this incorrect?

Well, the repr() call is after the file read. If your data is latin-1
you should decode it as latin-1, not utf-8:
data = file.read().decode('latin-1')

Though if the decode('utf-8') succeeds, and you do have non-ascii
characters in the data, they are probably encoded in utf-8, not
latin-1. Are you sure you have latin-1?

The repr() call converts back to ascii text, maybe that is what you want?

Perhaps you put in the repr because you were having trouble printing?

It smells of programming by guess rather than a correct solution to
some problem. What happens if you take it out?

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


Re: [Tutor] how to pass data to aother function from a class?

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 8:04 AM, Zheng Jiekai  wrote:
> I'm beginning my python learning. My python version is 3.1
>
> I‘v never learnt OOP before.
> So I'm confused by the HTMLParser
>
> Here's the code:
 from html.parser import HTMLParser
 class parser(HTMLParser):
> def handle_data(self, data):
>  print(data)
>
 p = parser()
 page = """TitleI'm a paragraph!"""
 p.feed(page)
> Title
> I'm a paragraph!
>
>
> I'm wondering if I can pass the data generated by ' handle_data' to a
> function instead of just print the data.

Sure. In fact print() is a function. Just replace print(data) with
my_function(data).

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


Re: [Tutor] parse text file

2010-02-02 Thread Norman Khine
hello,
thank you all for the advise, here is the updated version with the changes.

import re
file = open('producers_google_map_code.txt', 'r')
data = repr( file.read().decode('utf-8') )

get_records = re.compile(r"""openInfoWindowHtml\(.*?\\ticon:
myIcon\\n""").findall
get_titles = re.compile(r"""(.*)<\/strong>""").findall
get_urls = re.compile(r"""a href=\"\/(.*)\">En savoir plus""").findall
get_latlngs = 
re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""").findall

records = get_records(data)
block_record = []
for record in records:
namespace = {}
titles = get_titles(record)
title = titles[-1] if titles else None
urls = get_urls(record)
url = urls[-1] if urls else None
latlngs = get_latlngs(record)
latlng = latlngs[-1] if latlngs else None
block_record.append( {'title':title, 'url':url, 'lating':latlng} )

print block_record


On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson  wrote:
> On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine  wrote:
>
>> here are the changes:
>>
>> import re
>> file=open('producers_google_map_code.txt', 'r')
>> data =  repr( file.read().decode('utf-8') )
>
> Why do you use repr() here?

i have latin-1 chars in the producers_google_map_code.txt' file and
this is the only way to get it to read the data.

is this incorrect?

>
>> get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
>> get_title = re.compile(r"""(.*)<\/strong>""")
>> get_url = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
>> get_latlng = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
>>
>> records = get_record.findall(data)
>> block_record = []
>> for record in records:
>>        namespace = {}
>>        titles = get_title.findall(record)
>>        for title in titles:
>>                namespace['title'] = title
>
>
> This is odd, you don't need a loop to get the last title, just use
>  namespace['title'] = get_title.findall(html)[-1]
>
> and similarly for url and latings.
>
> Kent
>
>
>>        urls = get_url.findall(record)
>>        for url in urls:
>>                namespace['url'] = url
>>        latlngs = get_latlng.findall(record)
>>        for latlng in latlngs:
>>                namespace['latlng'] = latlng
>>        block_record.append(namespace)
>>
>> print block_record
>>>
>>> The def of "namespace" would be clearer imo in a single line:
>>>    namespace = {title:t, url:url, lat:g}
>>
>> i am not sure how this will fit into the code!
>>
>>> This also reveals a kind of name confusion, doesn't it?
>>>
>>>
>>> Denis
>>>
>>>
>>>
>>>
>>> 
>>>
>>> la vita e estrany
>>>
>>> http://spir.wikidot.com/
>>> ___
>>> Tutor maillist  -  tu...@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>> ___
>> Tutor maillist  -  tu...@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] tracking program

2010-02-02 Thread Philip Kilner
Hi Luis,

Luis Ortega wrote:
> I am fairly new to programming (which means I have never, ever, ever
> written a program).  I have a book or two in Python, and so far I like
> it.  I have a stupid question to ask; Is it possible to write an
> employee's internet tracking program in Python? 
> 

Wayne is right that you could do it as a proxy, but one question you can
ask is "which bits do I need to write in Python, and which bits can I
use an off the shelf solution for?".

One choice here is to use an existing proxy server that logs activity,
and then write a Python application to analyse the logs.

That might be a more approachable problem for you at this point.

HTH


-- 

Regards,

PhilK


'work as if you lived in the early days of a better nation'
- alasdair gray


smime.p7s
Description: S/MIME Cryptographic Signature
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to pass data to aother function from a class?

2010-02-02 Thread Zheng Jiekai
I'm beginning my python learning. My python version is 3.1

I‘v never learnt OOP before.
So I'm confused by the HTMLParser

Here's the code:
>>> from html.parser import HTMLParser
>>> class parser(HTMLParser):
def handle_data(self, data):
 print(data)

>>> p = parser()
>>> page = """TitleI'm a paragraph!"""
>>> p.feed(page)
Title
I'm a paragraph!


I'm wondering if I can pass the data generated by ' handle_data' to a
function instead of just print the data.

Sorry for my poor English
and Thank you for tutoring!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine  wrote:

> here are the changes:
>
> import re
> file=open('producers_google_map_code.txt', 'r')
> data =  repr( file.read().decode('utf-8') )

Why do you use repr() here?

> get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
> get_title = re.compile(r"""(.*)<\/strong>""")
> get_url = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
> get_latlng = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
>
> records = get_record.findall(data)
> block_record = []
> for record in records:
>        namespace = {}
>        titles = get_title.findall(record)
>        for title in titles:
>                namespace['title'] = title


This is odd, you don't need a loop to get the last title, just use
  namespace['title'] = get_title.findall(html)[-1]

and similarly for url and latings.

Kent


>        urls = get_url.findall(record)
>        for url in urls:
>                namespace['url'] = url
>        latlngs = get_latlng.findall(record)
>        for latlng in latlngs:
>                namespace['latlng'] = latlng
>        block_record.append(namespace)
>
> print block_record
>>
>> The def of "namespace" would be clearer imo in a single line:
>>    namespace = {title:t, url:url, lat:g}
>
> i am not sure how this will fit into the code!
>
>> This also reveals a kind of name confusion, doesn't it?
>>
>>
>> Denis
>>
>>
>>
>>
>> 
>>
>> la vita e estrany
>>
>> http://spir.wikidot.com/
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
> ___
> Tutor maillist  -  tu...@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] parse text file

2010-02-02 Thread Dave Angel

Norman Khine wrote:

thanks denis,

On Tue, Feb 2, 2010 at 9:30 AM, spir  wrote:
  

On Mon, 1 Feb 2010 16:30:02 +0100
Norman Khine  wrote:



On Mon, Feb 1, 2010 at 1:19 PM, Kent Johnson  wrote:
  

On Mon, Feb 1, 2010 at 6:29 AM, Norman Khine  wrote:



thanks, what about the whitespace problem?
  

\s* will match any amount of whitespace includin newlines.


thank you, this worked well.

here is the code:

###
import re
file=en('producers_google_map_code.txt', 'r')
data =repr( file.read().decode('utf-8') )

block =e.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
b =lock.findall(data)
block_list =]
for html in b:
  namespace =}
  t =e.compile(r"""(.*)<\/strong>""")
  title =.findall(html)
  for item in title:
  namespace['title'] =tem
  u =e.compile(r"""a href=\"\/(.*)\">En savoir plus""")
  url =.findall(html)
  for item in url:
  namespace['url'] =tem
  g =e.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
  lat =.findall(html)
  for item in lat:
  namespace['LatLng'] =tem
  block_list.append(namespace)

###

can this be made better?
  

The 3 regex patterns are constants: they can be put out of the loop.

You may also rename b to blocks, and find a more a more accurate name for 
block_list; eg block_records, where record =et of (named) fields.

A short desc and/or example of the overall and partial data formats can greatly 
help later review, since regex patterns alone are hard to decode.



here are the changes:

import re
file=en('producers_google_map_code.txt', 'r')
data =repr( file.read().decode('utf-8') )

get_record =e.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
get_title =e.compile(r"""(.*)<\/strong>""")
get_url =e.compile(r"""a href=\"\/(.*)\">En savoir plus""")
get_latlng =e.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")

records =et_record.findall(data)
block_record =]
for record in records:
namespace =}
titles =et_title.findall(record)
for title in titles:
namespace['title'] =itle
urls =et_url.findall(record)
for url in urls:
namespace['url'] =rl
latlngs =et_latlng.findall(record)
for latlng in latlngs:
namespace['latlng'] =atlng
block_record.append(namespace)

print block_record
  

The def of "namespace" would be clearer imo in a single line:
   namespace =title:t, url:url, lat:g}



i am not sure how this will fit into the code!

  

This also reveals a kind of name confusion, doesn't it?


Denis




Your variable 'file' is hiding a built-in name for the file type.  No 
harm in this example, but it's a bad habit to get into.


What did you intend to happen if the number of titles, urls, and latIngs 
are not each exactly one?  As you have it now, if there's more than one, 
you spend time adding them all to the dictionary, but only the last one 
survives.  And if there aren't any, you don't make an entry in the 
dictionary.


If that's the exact behavior you want, then you could replace the loop 
with an if statement:   (untested)


if titles:
namespace['title'] = titles[-1]


On the other hand, if you want a None in your dictionary for missing 
information, then something like:  (untested)


for record in records:


titles = get_title.findall(record)
title = titles[-1] if titles else None
urls = get_url.findall(record)
url = urls[-1] if urls else None
latlngs = get_latlng.findall(record)
lating = latings[-1] if latings else None
block_record.append( {'title':title, 'url':url, 'lating':lating{ )


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


Re: [Tutor] parse text file

2010-02-02 Thread Stefan Behnel
Norman Khine, 02.02.2010 10:16:
> get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
> get_title = re.compile(r"""(.*)<\/strong>""")
> get_url = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
> get_latlng = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
> 
> records = get_record.findall(data)
> block_record = []
> for record in records:
>   namespace = {}
>   titles = get_title.findall(record)
>   for title in titles:
>   namespace['title'] = title

I usually go one step further:

find_all_titles = re.compile(r"""(.*)<\/strong>""").findall
for record in records:
titles = find_all_titles(record)

Both faster and more readable (as is so common in Python).

Stefan

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


Re: [Tutor] callable objects

2010-02-02 Thread Alan Gauld


"sudhir prasad"  wrote

i run module1 ,first it calls sm1 in module 2 and a list gets updated,now 
i
again pass the same list to sm2 in module2 but im getting an error " 
'list'

object is not callable"


That suggests that somewhere in your code you are trying to call the list.
ie putting parentheses after it:

lst = [1,2,3]
lst()   # tryiong to call list but list is "not callable"

So I think the error is to do with a mistake in your copde rather than
the module/submodule structure!

But that is guesswork without seeing the code.
When discussing an error always send the full error message so that
we can read the stack trace etc. And post the code if possible too - at
the very least the function where the error occurs!

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] parse text file

2010-02-02 Thread Norman Khine
thanks denis,

On Tue, Feb 2, 2010 at 9:30 AM, spir  wrote:
> On Mon, 1 Feb 2010 16:30:02 +0100
> Norman Khine  wrote:
>
>> On Mon, Feb 1, 2010 at 1:19 PM, Kent Johnson  wrote:
>> > On Mon, Feb 1, 2010 at 6:29 AM, Norman Khine  wrote:
>> >
>> >> thanks, what about the whitespace problem?
>> >
>> > \s* will match any amount of whitespace includin newlines.
>>
>> thank you, this worked well.
>>
>> here is the code:
>>
>> ###
>> import re
>> file=open('producers_google_map_code.txt', 'r')
>> data =  repr( file.read().decode('utf-8') )
>>
>> block = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
>> b = block.findall(data)
>> block_list = []
>> for html in b:
>>       namespace = {}
>>       t = re.compile(r"""(.*)<\/strong>""")
>>       title = t.findall(html)
>>       for item in title:
>>               namespace['title'] = item
>>       u = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
>>       url = u.findall(html)
>>       for item in url:
>>               namespace['url'] = item
>>       g = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
>>       lat = g.findall(html)
>>       for item in lat:
>>               namespace['LatLng'] = item
>>       block_list.append(namespace)
>>
>> ###
>>
>> can this be made better?
>
> The 3 regex patterns are constants: they can be put out of the loop.
>
> You may also rename b to blocks, and find a more a more accurate name for 
> block_list; eg block_records, where record = set of (named) fields.
>
> A short desc and/or example of the overall and partial data formats can 
> greatly help later review, since regex patterns alone are hard to decode.

here are the changes:

import re
file=open('producers_google_map_code.txt', 'r')
data =  repr( file.read().decode('utf-8') )

get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
get_title = re.compile(r"""(.*)<\/strong>""")
get_url = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
get_latlng = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")

records = get_record.findall(data)
block_record = []
for record in records:
namespace = {}
titles = get_title.findall(record)
for title in titles:
namespace['title'] = title
urls = get_url.findall(record)
for url in urls:
namespace['url'] = url
latlngs = get_latlng.findall(record)
for latlng in latlngs:
namespace['latlng'] = latlng
block_record.append(namespace)

print block_record
>
> The def of "namespace" would be clearer imo in a single line:
>    namespace = {title:t, url:url, lat:g}

i am not sure how this will fit into the code!

> This also reveals a kind of name confusion, doesn't it?
>
>
> Denis
>
>
>
>
> 
>
> la vita e estrany
>
> http://spir.wikidot.com/
> ___
> Tutor maillist  -  tu...@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] callable objects

2010-02-02 Thread sudhir prasad
hi,
in my porgram i have two modules say module 1 and module 2,module 2 consists
of two sub modules say sm1 and sm2,
i run module1 ,first it calls sm1 in module 2 and a list gets updated,now i
again pass the same list to sm2 in module2 but im getting an error " 'list'
object is not callable"
so i wrote a top module which calls module 1 and i defined the list in the
top module instead of module 1,but again m getting the same error
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] parse text file

2010-02-02 Thread spir
On Mon, 1 Feb 2010 16:30:02 +0100
Norman Khine  wrote:

> On Mon, Feb 1, 2010 at 1:19 PM, Kent Johnson  wrote:
> > On Mon, Feb 1, 2010 at 6:29 AM, Norman Khine  wrote:
> >
> >> thanks, what about the whitespace problem?
> >
> > \s* will match any amount of whitespace includin newlines.
> 
> thank you, this worked well.
> 
> here is the code:
> 
> ###
> import re
> file=open('producers_google_map_code.txt', 'r')
> data =  repr( file.read().decode('utf-8') )
> 
> block = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
> b = block.findall(data)
> block_list = []
> for html in b:
>   namespace = {}
>   t = re.compile(r"""(.*)<\/strong>""")
>   title = t.findall(html)
>   for item in title:
>   namespace['title'] = item
>   u = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
>   url = u.findall(html)
>   for item in url:
>   namespace['url'] = item
>   g = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
>   lat = g.findall(html)
>   for item in lat:
>   namespace['LatLng'] = item
>   block_list.append(namespace)
> 
> ###
> 
> can this be made better?

The 3 regex patterns are constants: they can be put out of the loop.

You may also rename b to blocks, and find a more a more accurate name for 
block_list; eg block_records, where record = set of (named) fields.

A short desc and/or example of the overall and partial data formats can greatly 
help later review, since regex patterns alone are hard to decode.

The def of "namespace" would be clearer imo in a single line:
namespace = {title:t, url:url, lat:g}
This also reveals a kind of name confusion, doesn't it?


Denis






la vita e estrany

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