Re: [Tutor] Python3.0 and Tkinter on ubuntu 8.10 HOWTO

2009-01-14 Thread Emad Nawfal (عماد نوفل)
Great source for Unicode lovers

On Wed, Jan 14, 2009 at 3:53 PM, Robert Berman  wrote:

> Very good and most welcome.
>
> Robert Berman
>
> Vern Ceder wrote:
>
>> Since there was some interest in the question of how to get a full Python
>> 3.0, including Tkinter and IDLE, compiled on Ubuntu Intrepid 8.10, I've
>> written up what I've done and posted it at
>> http://learnpython.wordpress.com/2009/01/14/installing-python-30-on-ubuntu/
>>
>> Cheers,
>> Vern Ceder
>>
>>  ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lists and Entry

2009-01-14 Thread Emad Nawfal (عماد نوفل)
On Wed, Jan 14, 2009 at 5:52 AM, Mr Gerard Kelly  wrote:

> There is a little Tkinter program. It lets you type something in a box,
> and will display it at the command line.
>
>
> from Tkinter import *
>
> master = Tk()
>
> e = Entry(master)
> e.pack()
>
> e.focus_set()
>
> def callback():
>  s=e.get()
>  print s
>
> b = Button(master, text="get", width=10, command=callback)
> b.pack()
>
> mainloop()
>
>
>
> The get() method returns a string, how do I make it return a list?
> I want to be able to type in 1,2,3 into the box and get [1,2,3] to
> appear on the command line.
>
> If I change the callback() method so that it says
> s=[e.get()]
> I just get a list with one element, the string: ['1,2,3']
>
> If I make it
> s=list(e.get())
> I get a list with every character as an element: ['1', ',', '2', ',', '3']
>
> How to just get plain [1,2,3]?
>
> many thanks

One possibility is to split the string on the comma. If you have a string
like :
s = '1,2,3' then s.split(',') = [1,2,3]
If you later want to change this into a list of integers for example, you
might use something like  a list comprehension. Look at the following
interactive session:
>>> s = "1,2,3"

>>> m = s.split(',')
>>> m
['1', '2', '3']

>>> f = [int(i) for i in m]
>>> f
[1, 2, 3]
>>>


This is a reply from a programming amateur, so I think you might consider it
until you get a better solution.

>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python3.0 and tkinter on ubuntu 8.10

2009-01-13 Thread Emad Nawfal (عماد نوفل)
On Tue, Jan 13, 2009 at 4:02 PM, Vern Ceder wrote:

> Hi,
>
> I have Python 3.0/Tkinter/IDLE working fine on Ubuntu 8.10, but it takes a
> certain amount of fiddling.
>
> 1. Make sure the stock Ubuntu Python 3.0 package is not installed
>
> 2. download the Python 3.0 source from python.org
>
> 3. install the following packages: build-essential libsqlite3-dev
> libreadline5-dev libncurses5-dev zlib1g-dev libbz2-dev libssl-dev
> libgdbm-dev tk-dev
>
> 4. unpack the Python source and switch to that folder
>
> 5. build Python using the standard ./configure, make, make install sequence
> - if you want more detail/help on that process, just ask...
>
> I'd be happy to explain this process in more detail if anyone wants...
>
> Cheers,
> Vern
>
Yes Vern, it works. Thank you so much.
I installed the missing packages and downloaded and installed the Python3.0
source and it's now perfect.
Thanks again

>
>  Hi Tutors,I have downloaded Python3.0 and started playing with it. I like
>> it
>> because of the utf-8 default encoding, but I'm having trouble importing
>> tkinter. I get the following:
>>
>>> >>> import turtle
>
 Traceback (most recent call last):
>>  File "", line 1, in 
>>  File "/usr/local/lib/python3.0/turtle.py", line 107, in 
>>import tkinter as TK
>>  File "/usr/local/lib/python3.0/tkinter/__init__.py", line 39, in 
>>import _tkinter # If this fails your Python may not be configured for
>> Tk
>> ImportError: No module named _tkinter
>>
>>> >>> import tkinter
>
 Traceback (most recent call last):
>>  File "", line 1, in 
>>  File "/usr/local/lib/python3.0/tkinter/__init__.py", line 39, in 
>>import _tkinter # If this fails your Python may not be configured for
>> Tk
>> ImportError: No module named _tkinter
>>
>>> >>>
>

>> Any idea how this can be solved on Ubuntu 8.10. I don't have this problem
>> with the default Python installation (2.5.2)
>> Thank you
>>
>
>
> --
> This time for sure!
>   -Bullwinkle J. Moose
> -
> Vern Ceder, Director of Technology
> Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
> vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python3.0 and tkinter on ubuntu 8.10

2009-01-13 Thread Emad Nawfal (عماد نوفل)
On Tue, Jan 13, 2009 at 3:30 PM, Robert Berman  wrote:

>  Emad,
>
> A number of people in the Ubuntu community have attempted to work with
> Python3.0 under Ubuntu 8.10 and almost all have found it to be a frustrating
> and painful experience with some having disastrous consequences. I was
> looking for the latest discussion thread on this issue in the Ubuntu forums,
> but the forums are currently down.
>
> You might want to use Google to track this under Python 3.0 + Ubuntu 8.10.
>
> The overall consensus of opinion is to wait until Python 3.0 became the
> default under Ubuntu before using it.
> YMMV.
>
> Robert Berman
>
> Emad Nawfal (عماد نوفل) wrote:
>
> Hi Tutors,I have downloaded Python3.0 and started playing with it. I like
> it because of the utf-8 default encoding, but I'm having trouble importing
> tkinter. I get the following:
> >>> import turtle
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python3.0/turtle.py", line 107, in 
> import tkinter as TK
>   File "/usr/local/lib/python3.0/tkinter/__init__.py", line 39, in 
> import _tkinter # If this fails your Python may not be configured for
> Tk
> ImportError: No module named _tkinter
> >>> import tkinter
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/local/lib/python3.0/tkinter/__init__.py", line 39, in 
> import _tkinter # If this fails your Python may not be configured for
> Tk
> ImportError: No module named _tkinter
> >>>
>
> Any idea how this can be solved on Ubuntu 8.10. I don't have this problem
> with the default Python installation (2.5.2)
> Thank you
>
>
> --
> لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
> الغزالي
> "No victim has ever been more repressed and alienated than the truth"
>
> Emad Soliman Nawfal
> Indiana University, Bloomington
>
> 
>
> --
>
> ___
> Tutor maillist  -  
> tu...@python.orghttp://mail.python.org/mailman/listinfo/tutor
>
>
Thanks Robert for the advice about Ubuntu. I did a lot of Googling before I
posted my question, but could not find an answer. I do basic things with
Python that involve using lots of Arabic text, and this is why python3.0 was
supposed to make my life easier, but I'm learning GUI building now, and it
has given me the first frustration.

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python3.0 and tkinter on ubuntu 8.10

2009-01-13 Thread Emad Nawfal (عماد نوفل)
Hi Tutors,I have downloaded Python3.0 and started playing with it. I like it
because of the utf-8 default encoding, but I'm having trouble importing
tkinter. I get the following:
>>> import turtle
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.0/turtle.py", line 107, in 
import tkinter as TK
  File "/usr/local/lib/python3.0/tkinter/__init__.py", line 39, in 
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
>>> import tkinter
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.0/tkinter/__init__.py", line 39, in 
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
>>>

Any idea how this can be solved on Ubuntu 8.10. I don't have this problem
with the default Python installation (2.5.2)
Thank you


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HOW DO I PYTHONIZE A BASICALLY BASIC PROGRAM?

2009-01-12 Thread Emad Nawfal (عماد نوفل)
On Mon, Jan 12, 2009 at 5:23 PM, WM.  wrote:

> # The natural numbers(natnum), under 1000, divisible by 3 or by 5 are to be
> added together.
> natnum = 0
> num3 = 0
> num5 = 0
> cume = 0
> # The 'and' is the 15 filter; the 'or' is the 3 or 5 filter.
> while natnum <= 999:
>num3 = natnum/3
>num5 = natnum/5
>if natnum - (num3 * 3) == 0 and natnum - (num5 * 5) == 0:
>cume = cume + natnum
>elif natnum - (num3 * 3) == 0 or natnum - (num5 * 5) == 0:
>if natnum - (num3 * 3) == 0:
>cume = cume + natnum
>elif natnum - (num5 * 5) == 0:
>cume = cume + natnum
>natnum = natnum + 1
> print cume
>
>
> This problem was kicked around last month and I did not understand any of
> the scripts.  So I tried to recall the BASIC ifs and loops. The project
> euler guys say it works, but how might it be made more Pythonic?
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


I guess using a list comprehension will work
>>>x = [number for number in range(1000) if number%3 ==0 or number%2 ==0]
>>> sum(x)
333167
>>>

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Emad Nawfal (عماد نوفل)
2008/12/26 Kent Johnson 

> On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (عماد نوفل)
>  wrote:
> > suppose I have an external program that prints "testing the subprocess
> > module"
> > I know I can run it through the commands module like this:
> >
> >>>> a = commands.getoutput("python3.0 hello.py")
> >>>> a
> > 'testing the subprocess module'
>
>
> > I cannot figure out how to do the same thing in the subprocess module.
> Can
> > somebody please explain how to get the same behaviour from, say,
> > subprocess.call
>
> Sometthing like this, I think:
>
> proc = subprocess.Popen('python3.0 hello.py',
>   shell=True,
>   stdout=subprocess.PIPE,
>   )
> stdout_value = proc.communicate()[0]
>
> (Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html)
>
> Kent
>
Thank you Kent.
It works, but isn't the commands module much simpler?  I don't know why it's
no more available in Python3.0


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] beginsWith multiple prefixes

2008-12-26 Thread Emad Nawfal (عماد نوفل)
On Fri, Dec 26, 2008 at 6:47 AM, Alan Gauld wrote:

>
> "Kent Johnson"  wrote
>
>> for d in os.listdir():
>>>  if MyString(d).upper().beginswith():
>>>
>>
>> But that won't work, the result of calling upper() will be a normal
>> str, not a MyString.
>>
>
> Ah yes. Immutability of strings strikes again. upper() returns a new
> string, I forgot about that. pity.
>
> You can do
>
> if MyString(d.upper()).beginswith(...)
>
> But that loses a lot in elegance and is hardly better than using a
> fiunction.
>
> Alan G
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Thank you Alan and everybody. I simply prefer the built-in one. I had no
idea it could take a tuple.
What is amazing is that I learn  more from this list than I do from any
other source.

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Emad Nawfal (عماد نوفل)
Hello Tutors, and Happy New Year and Holidays,
suppose I have an external program that prints "testing the subprocess
module"
I know I can run it through the commands module like this:

>>> a = commands.getoutput("python3.0 hello.py")
>>> a
'testing the subprocess module'
>>> len(a)
29
>>> b = a.split()
>>> b
['testing', 'the', 'subprocess', 'module']
>>> for word in b:
... if word[-1] == 'e': print word
...
the
module
>>>

I cannot figure out how to do the same thing in the subprocess module. Can
somebody please explain how to get the same behaviour from, say,
subprocess.call

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] beginsWith multiple prefixes

2008-12-25 Thread Emad Nawfal (عماد نوفل)
On 12/25/08, Kent Johnson  wrote:
>
> On Wed, Dec 24, 2008 at 4:04 PM, Emad Nawfal (عماد نوفل)
>  wrote:
> > Hi Tutors,
> > I want a function that acts like the startswith method, but can take
> > multiple prefixes. As an amateur programmer, I came up with this one, and
> it
> > works fine, but my experience tells me that my solutions are not always
> the
> > best ones around. Can you please tell me what a better option might be:
>
>
> Since Python 2.5, startswith() accepts a tuple as the match parameter,
> so you can write
> def beginsWith(word, listname):
>   return word.startswith(tuple(listname))
>
> or just us startswith() directly.
>
>
> Kent
>

Thank you Kent. I read the documentation, but did not notice it, now I can
see it clearly. This also applies to endswith
Thank you all for your helpfulness.

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] beginsWith multiple prefixes

2008-12-25 Thread Emad Nawfal (عماد نوفل)
On 12/25/08, Bill Campbell  wrote:
>
> On Wed, Dec 24, 2008, bob gailer wrote:
> > Emad Nawfal ( ) wrote:
> >> Hi Tutors,
> >> I want a function that acts like the startswith method, but can take
> >> multiple prefixes. As an amateur programmer, I came up with this one,
> >> and it works fine, but my experience tells me that my solutions are
> >> not always the best ones around. Can you please tell me what a better
> >> option might be:
>
> ...
>
> > Above does a lot more work than necessary. Try:
> >
> > def beginsWith(word, listname):
> >for prefix in listname:
> >   if word.startswith(prefix):
> >  return True
>
>
> It might be more useful to return the prefix that matched as the
> caller already knows what ``word'' is.
>
> Bill
> --
> INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
> URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
> Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
> Fax:(206) 232-9186
>
> Basic Definitions of Science:
> If it's green or wiggles, it's biology.
> If it stinks, it's chemistry.
> If it doesn't work, it's physics.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Thank you so much Bob and Bill. This is really much better than mine. Bill's
suggestion is not applicable in my current script, but I believe I will need
that soon.
Thank you both.
Thank you all tutors


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] beginsWith multiple prefixes

2008-12-24 Thread Emad Nawfal (عماد نوفل)
Hi Tutors,
I want a function that acts like the startswith method, but can take
multiple prefixes. As an amateur programmer, I came up with this one, and it
works fine, but my experience tells me that my solutions are not always the
best ones around. Can you please tell me what a better option might be:



def beginsWith(word, listname):
count = 0
x = range(len(word))
for i in x:
if word[:i] in listname:
count+=1

break

if count > 0:
return True
else:
return False


# main
text = "ana mary fify floor school security".split()

prefixes = ["an", "ma", "fi", "sec"]

for word in text:
if beginsWith(word, prefixes):
print(word+" (Match)")
else:
print(word)

#This produces the following:
IDLE 3.0   No Subprocess 
>>>
ana (Match)
mary (Match)
fify (Match)
floor
school
security (Match)
>>>

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] py_compile and chmod +x

2008-12-19 Thread Emad Nawfal (عماد نوفل)
Thank you so much. This has been very helpful.

On Fri, Dec 19, 2008 at 12:21 PM, Andreas Kostyrka wrote:

> Am Fri, 19 Dec 2008 09:01:24 -0500
> schrieb "Emad Nawfal (عماد نوفل)" :
>
> You are not supposed to compile the main script.
>
> pyc files are automatically generated when you import the py file (but
> not execute it), if you are allowed to write them beside the py file.
>
> When a pyc file exists, and it's newer than the py file, python will
> import the py file from the pyc file, thus saving on parsing the py
> file.
>
> In effect the pyc file is nothing more than a file cache for the parsed
> and bytecompiled version of the py file.
>
> Andreas
>
> > Hi Tutors,
> > #! /usr/bin/env python
> > print "Hello Tutors"
> >
> > I have this script saved as hello.py. Why can  I execute it, but not
> > the compiled version?  or am I doing something wrong? Just curious.
> > Any help appreciated.
> > For example :
> >
> >
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ chmod  +x  hello.py
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py
> > Hello Tutors
> >
> > Now I compile it in Python:
> > >>> import py_compile
> > >>> py_compile.compile("hello.py")
> > >>>
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc
> >
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc
> > : command not found: �
> > ./hello.pyc: line 2: syntax error near unexpected token `('
> > ./hello.pyc: line 2: `�KIc @sdGHd S( s
> >  Hello
> > TutorsNhello.py s'
> > e...@emad-laptop:~/Desktop/Programming/Haskell$
> >
> >
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] py_compile and chmod +x

2008-12-19 Thread Emad Nawfal (عماد نوفل)
Thank you so much everybody. I was just curious.
This has been very useful.

On Fri, Dec 19, 2008 at 12:21 PM, Andreas Kostyrka wrote:

> Am Fri, 19 Dec 2008 09:01:24 -0500
> schrieb "Emad Nawfal (عماد نوفل)" :
>
> You are not supposed to compile the main script.
>
> pyc files are automatically generated when you import the py file (but
> not execute it), if you are allowed to write them beside the py file.
>
> When a pyc file exists, and it's newer than the py file, python will
> import the py file from the pyc file, thus saving on parsing the py
> file.
>
> In effect the pyc file is nothing more than a file cache for the parsed
> and bytecompiled version of the py file.
>
> Andreas
>
> > Hi Tutors,
> > #! /usr/bin/env python
> > print "Hello Tutors"
> >
> > I have this script saved as hello.py. Why can  I execute it, but not
> > the compiled version?  or am I doing something wrong? Just curious.
> > Any help appreciated.
> > For example :
> >
> >
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ chmod  +x  hello.py
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py
> > Hello Tutors
> >
> > Now I compile it in Python:
> > >>> import py_compile
> > >>> py_compile.compile("hello.py")
> > >>>
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc
> >
> > e...@emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc
> > : command not found: �
> > ./hello.pyc: line 2: syntax error near unexpected token `('
> > ./hello.pyc: line 2: `�KIc @sdGHd S( s
> >  Hello
> > TutorsNhello.py s'
> > e...@emad-laptop:~/Desktop/Programming/Haskell$
> >
> >
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] py_compile and chmod +x

2008-12-19 Thread Emad Nawfal (عماد نوفل)
Hi Tutors,
#! /usr/bin/env python
print "Hello Tutors"

I have this script saved as hello.py. Why can  I execute it, but not the
compiled version?  or am I doing something wrong? Just curious. Any help
appreciated.
For example :


e...@emad-laptop:~/Desktop/Programming/Haskell$ chmod  +x  hello.py
e...@emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py
Hello Tutors

Now I compile it in Python:
>>> import py_compile
>>> py_compile.compile("hello.py")
>>>
e...@emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc

e...@emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc
: command not found: �
./hello.pyc: line 2: syntax error near unexpected token `('
./hello.pyc: line 2: `�k...@sdGHdS(s
 Hello
TutorsNhello.pys'
e...@emad-laptop:~/Desktop/Programming/Haskell$


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newbie question about list element manipulation after split()

2008-10-16 Thread Emad Nawfal (عماد نوفل)
On Thu, Oct 16, 2008 at 4:25 AM, Visvaldas K. <[EMAIL PROTECTED]>wrote:

> Hi,
>
> I feel very stupid, but I am stuck. I could try some workaround, but I want
> to understand what I am doing wrong.
>
> The line:
>
> for line in open("parameterfile").readlines( ):
>print line.split()# trouble line
>
>
> The "trouble line" works fine in this example. It prints
> what I want, something which looks like lists of words:
>
> ['CT', 'CT', '268.0', '1.529']...  etc.
>
> However, if I want to manipulate the individual list elements, I run into
> trouble. While
>
> print line.split()[0]
>
> still works, (it yields CT  - first element),
>
> however,
>
> print line.split()[1]
>
> gives error:
>
> File "readatoms.py", line 103, in 
>print line.split()[1]
> IndexError: list index out of range
>
> Could you please tell me what's wrong. (I come from Perl background so
> Python seems out-of-the-body experience to me).
>
> Sincerely,
>
> Vis
>

Here is a solution:
>>> for line in open('DBAN1001.txt').readlines():
... line = line.split()
... if len(line) > 1: # This is what makes it work for me.
... print line[1]
...
it's
have
the
way
that
is
is
prison
we


It works fine. It seems that in the file you have there are lines whose
length is less than you should expect, or maybe a more experienced Python
programmer can give you a better answer.


>
>
>
>
>
>
>
>
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newbie question about list element manipulation after split()

2008-10-16 Thread Emad Nawfal (عماد نوفل)
Here is a solution:
>>> for line in open('DBAN1001.txt').readlines():
... line = line.split()
... if len(line) > 1: # This is what makes it work for me.
... print line[1]
...
it's
have
the
way
that
is
is
prison
we


It works fine. It seems that in the file you have there are lines whose
length is less than you should expect, or maybe a more experienced Python
programmer can give you a better answer.


On Thu, Oct 16, 2008 at 4:25 AM, Visvaldas K. <[EMAIL PROTECTED]>wrote:

> Hi,
>
> I feel very stupid, but I am stuck. I could try some workaround, but I want
> to understand what I am doing wrong.
>
> The line:
>
> for line in open("parameterfile").readlines( ):
>print line.split()# trouble line
>
>
> The "trouble line" works fine in this example. It prints
> what I want, something which looks like lists of words:
>
> ['CT', 'CT', '268.0', '1.529']...  etc.
>
> However, if I want to manipulate the individual list elements, I run into
> trouble. While
>
> print line.split()[0]
>
> still works, (it yields CT  - first element),
>
> however,
>
> print line.split()[1]
>
> gives error:
>
> File "readatoms.py", line 103, in 
>print line.split()[1]
> IndexError: list index out of range
>
> Could you please tell me what's wrong. (I come from Perl background so
> Python seems out-of-the-body experience to me).
>
> Sincerely,
>
> Vis
>
>
>
>
>
>
>
>
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difficult loop?

2008-10-15 Thread Emad Nawfal (عماد نوفل)
2008/10/15 Emad Nawfal (عماد نوفل) <[EMAIL PROTECTED]>

>
>
> 2008/10/15 John Fouhy <[EMAIL PROTECTED]>
>
>> 2008/10/16 Emad Nawfal (عماد نوفل) <[EMAIL PROTECTED]>:
>> > One more question,
>> > I'm a linguistics person, I know some Java and some Python (and failed
>> to
>> > learn Prolog). What things do I need to learn to write such "difficult"
>> > scripts? Are there any recommendations? books, certain things to learn?
>>
>> Well, there's a few tutorials around that you could work through.  The
>> best way to learn, in my opinion, is to find something not-too-hard
>> that you want to do and then start building it.  Combine that with the
>> documentation and ask questions on this list when you get stuck.
>>
>> For example, you could look at the code I wrote and figure out how to
>> make the last change yourself :-)
>>
>
>
> I will.
> Thank you so much for your helpfulness.
>
>>
Hi John and Tutors,
I have added a function notDouble, and changed a couple of  lines (marked
below), and the results are exactly what I want. Please let me know whether
this is the right, or the good, way to do it.

shortVowels = 'aiuo'
double = '~'
def notDouble(c):  # I added this function
return c not in double
def notSV(c):
   return c not in (shortVowels + '+' )

def nextSV(s, idx):
   """ Find the next short vowel, or ~ + short vowel.

   s :: string to search
   idx :: index to start from

   Return: short vowel, or ~ + short vowel, or _ if no following short
vowel.
   """

   try:
   if s[idx+1] in shortVowels:
   return s[idx+1]
   elif s[idx+1] == double:
   return s[idx+1:idx+3]
   else:
   return '_'
   except IndexError:
   return '_'

def processChar(s, idx):
   """ Process a single character of s according to the rules.

   s :: string to work with
   idx :: index to start from

   Output tuple: (preceeding, following, last)
   """

   preceeding = filter(notSV, s[:idx])[-5:]
   preceeding = filter(notDouble, preceeding)[-5:] # I added this
   preceeding = '_'*(5-len(preceeding)) + preceeding

   following = filter(notSV, s[idx+1:])[:5]
   following = filter(notDouble, following)[:5] # I added this
   following = following + '_'*(5-len(following))

   last = nextSV(s, idx)

   return preceeding, following, last

def process(s):
   """ Process and print string according to the rules. """

   for i, c in enumerate(s):
   if c in shortVowels + '+' + double: # I added double
   continue
   preceeding, following, last = processChar(s, i)
   print '  '.join(preceeding + c + following) + '  ' + last

if __name__ == '__main__':
   test = "AlmutasAhilwn"
   process(test)

>
>> --
>> John.
>>
>
>
>
> --
> لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
> الغزالي
> "No victim has ever been more repressed and alienated than the truth"
>
> Emad Soliman Nawfal
> Indiana University, Bloomington
> http://emnawfal.googlepages.com
> 
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difficult loop?

2008-10-15 Thread Emad Nawfal (عماد نوفل)
2008/10/15 John Fouhy <[EMAIL PROTECTED]>

> 2008/10/16 Emad Nawfal (عماد نوفل) <[EMAIL PROTECTED]>:
> > One more question,
> > I'm a linguistics person, I know some Java and some Python (and failed to
> > learn Prolog). What things do I need to learn to write such "difficult"
> > scripts? Are there any recommendations? books, certain things to learn?
>
> Well, there's a few tutorials around that you could work through.  The
> best way to learn, in my opinion, is to find something not-too-hard
> that you want to do and then start building it.  Combine that with the
> documentation and ask questions on this list when you get stuck.
>
> For example, you could look at the code I wrote and figure out how to
> make the last change yourself :-)



I will.
Thank you so much for your helpfulness.

>
> --
> John.
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difficult loop?

2008-10-15 Thread Emad Nawfal (عماد نوفل)
That's great.
There is still only one thing that I was not aware of when I asked the
question.
When the double consonant sign  "~" is there, it should slo be skipped like
the short vowels. In other words, it cannot appear except in the pre-final
position along with another short vowel.
Is that easy to fix?

One more question,
I'm a linguistics person, I know some Java and some Python (and failed to
learn Prolog). What things do I need to learn to write such "difficult"
scripts? Are there any recommendations? books, certain things to learn?

To see how my friend's script was used, just look at the paper in this link:
http://jones.ling.indiana.edu/~skuebler/papers/vocal.pdf

Thank you so much,
Emad

2008/10/15 John Fouhy <[EMAIL PROTECTED]>

> 2008/10/16 Emad Nawfal (عماد نوفل) <[EMAIL PROTECTED]>:
> > The focus  letter will always be # 6 on the line. A is not a short vowel,
> > and it is not followed by a short vowel, so the last character should be
> > "_", not a "u"
>
> Oh, I see.  I misunderstood the meaning of "followed by".  I can fix
> that by changing one function in my code:
>
> def nextSV(s, idx):
>""" Find the next short vowel, or ~ + short vowel.
>
>s :: string to search
>idx :: index to start from
>
>Return: short vowel, or ~ + short vowel, or _ if no following short
> vowel.
>"""
>
> try:
>if s[idx+1] in shortVowels:
>return s[idx+1]
>elif s[idx+1] == double:
>return s[idx+1:idx+3]
>else:
>return '_'
>except IndexError:
>return '_'
>
> > The purpose of this program is to calculate vector similarities in the
> > context of the letters in a machine learning approach called
> "Memory-based
> > learning"
>
> I did wonder what it was for ;-)
>
> --
> John.
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difficult loop?

2008-10-15 Thread Emad Nawfal (عماد نوفل)
2008/10/15 John Fouhy <[EMAIL PROTECTED]>

> 2008/10/16 Emad Nawfal (عماد نوفل) <[EMAIL PROTECTED]>:
> > Dear Tutors,
> > I needed a program to go through  a word like this "Almuta$r~id"
> >
> > 1-  a, i,u,  and o are the short vowels
> > 2-  ~ is the double consonant.
> > I wanted the program to do the following:
> >
> > - For each letter of the word, if the letter is not a short vowel, print
> the
> > letter with the 5 preceding letters (that are not short vowels),  then
> the
> > letter itself, then the 5 following letters (no short vowels), then the
> > short vowel following the letter.
> > - If the letter is followed by the double character "~", do the same, but
> > instead of printing just the vowel following the letter as the last
> > character, print the "~" + the short vowel
> > -  if the letter is not followed by a vowel, print an underscore as the
> last
> > character.
> >
> > - if there a "+" sign, ignore it.
> > - If there are fewer than  5 preceding or following letters, print an
> > underscore in place of each missing letter.
> >
> > For example, the word "Almuta$r~id" would be printed as follows:
> >
> > _  _  _  _  _  A  l  m  t  $  r  _
> > _  _  _  _  A  l  m  t  $  r  d  _
> > _  _  _  A  l  m  t  $  r  d  _  u
> > _  _  A  l  m  t  $  r  d  _  _  a
> > _  A  l  m  t  $  r  d  _  _  _  a
> > A  l  m  t  $  r  d  _  _  _  _  ~i
> > l  m  t  $  r  d  _  _  _  _  _  _
>
> Hi Emad,
>
> I had a crack at this.  My solution is below.  I think it is simpler
> than the one your friend provided :-)  Although I think there is
> something I don't understand about your rules, because I get slightly
> different results for your sample word:
>
> _  _  _  _  _  A  l  m  t  $  r  u
> _  _  _  _  A  l  m  t  $  r  ~  u
> _  _  _  A  l  m  t  $  r  ~  d  u
> _  _  A  l  m  t  $  r  ~  d  _  a
> _  A  l  m  t  $  r  ~  d  _  _  ~i
> A  l  m  t  $  r  ~  d  _  _  _  ~i
> l  m  t  $  r  ~  d  _  _  _  _  i
> m  t  $  r  ~  d  _  _  _  _  _  _
>
> Let me know if you have any trouble understanding any of the code:
>
> #
>
> shortVowels = 'aiuo'
> double = '~'
>
> def notSV(c):
>return c not in (shortVowels + '+')
>
> def nextSV(s, idx):
>""" Find the next short vowel, or ~ + short vowel.
>
>s :: string to search
>idx :: index to start from
>
>Return: short vowel, or ~ + short vowel, or _ if no following short
> vowel.
>"""
>
>for i, c in enumerate(s[idx+1:]):
>if c in shortVowels:
>return c
>elif c == double:
># return c and the character after it.
># assuming ~ is always followed by a short vowel
>return s[i+idx+1:i+idx+3]
>
># If we exit the loop without finding a short vowel or a ~
>return '_'
>
> def processChar(s, idx):
>""" Process a single character of s according to the rules.
>
>s :: string to work with
>idx :: index to start from
>
>Output tuple: (preceeding, following, last)
>"""
>
>preceeding = filter(notSV, s[:idx])[-5:]
>preceeding = '_'*(5-len(preceeding)) + preceeding
>
>following = filter(notSV, s[idx+1:])[:5]
>following = following + '_'*(5-len(following))
>
>last = nextSV(s, idx)
>
>return preceeding, following, last
>
> def process(s):
>""" Process and print string according to the rules. """
>
>for i, c in enumerate(s):
>if c in shortVowels + '+':
>continue
>preceeding, following, last = processChar(s, i)
>print '  '.join(preceeding + c + following) + '  ' + last
>
> if __name__ == '__main__':
>test = "Almuta$r~id"
>process(test)
>

Thank you so much John for thinking about this. The difference is here:
_  _  _  _  _  A  l  m  t  $  r  u
_  _  _  _  A  l  m  t  $  r  ~  u
_  _  _  A  l  m  t  $  r  ~  d  u

The focus  letter will always be # 6 on the line. A is not a short vowel,
and it is not followed by a short vowel, so the last character should be
"_", not a "u"

The same is true for lines 2 and 3 . The letter "l" is not followed by a
short vowel, so the last character should be "_"

Same for line 3 as well. "m" is not a short vowel, and it is not followed by
a short vowel

The purpose of this program is to calculate vector similarities in the
context of the letters in a machine learning approach called "Memory-based
learning"
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Difficult loop?

2008-10-15 Thread Emad Nawfal (عماد نوفل)
Dear Tutors,
I needed a program to go through  a word like this "Almuta$r~id"

1-  a, i,u,  and o are the short vowels
2-  ~ is the double consonant.
I wanted the program to do the following:

- For each letter of the word, if the letter is not a short vowel, print the
letter with the 5 preceding letters (that are not short vowels),  then the
letter itself, then the 5 following letters (no short vowels), then the
short vowel following the letter.
- If the letter is followed by the double character "~", do the same, but
instead of printing just the vowel following the letter as the last
character, print the "~" + the short vowel
-  if the letter is not followed by a vowel, print an underscore as the last
character.

- if there a "+" sign, ignore it.
- If there are fewer than  5 preceding or following letters, print an
underscore in place of each missing letter.

For example, the word "Almuta$r~id" would be printed as follows:

_  _  _  _  _  A  l  m  t  $  r  _
_  _  _  _  A  l  m  t  $  r  d  _
_  _  _  A  l  m  t  $  r  d  _  u
_  _  A  l  m  t  $  r  d  _  _  a
_  A  l  m  t  $  r  d  _  _  _  a
A  l  m  t  $  r  d  _  _  _  _  ~i
l  m  t  $  r  d  _  _  _  _  _  _

I took the problem to a friend of mine who is a Haskel programmer. He wrote
the following python script which works perfectly, but I'm wondering whether
there is an easier, more Pythonic, way to write this:


# Given a transliterated Arabic text like:
# luwnog biyt$ Al+wilAy+At+u ...

# + are to be ignored
# y, A, and w are the long vowels
# shadda is written ~

# produce a table with:
# (letter, vowel, pre5, post5)
# where vowel is either - if the letter is not followed by a vowel or a
vowel
# or a shadda with a vowel, pre5 and post5 are the letters surrounding the
# letter

###

# Initializes a few variables to be used or updated by the main loop below
space = ' '
plus = '+'
dash = '_'
shadda = '~'
vowels = ('a', 'e', 'i', 'o', 'u')
skips = (space,plus) + vowels

# a small input for testing

inputString = "Al+muta$ar~id+i Al+muta$ar~id+i Al+muta$ar~id+i"

# for convenience surround the input with six dashes both ways to make the
# loop below uniform

def makeWord (s): return dash*6 + s + dash*6

# A few utility constants, variables, and functions...

def shiftLeft (context, ch): return context[1:] + (ch,)

def isSkip (ch): return (ch in skips)

def isShadda (ch): return (ch == shadda)

def isVowel (ch): return (ch in vowels)

def nextCh (str,i):
c = str[i]
try:
if isSkip(c):
return nextCh(str,i+1)
elif isShadda(str[i+1]):
if isVowel(str[i+2]):
return (c,str[i+1:i+3],i+3)
else:
return (c,dash,i+2)
elif isVowel(str[i+1]):
return (c,str[i+1],i+2)
else:
return (c,dash,i+1)
except IndexError:
return (c,dash,i+1)

def advance (str,pre,post,horizon):
(cc,cv) = post[0]
(hc,hv,nextHorizon) = nextCh(str,horizon)
nextPre = shiftLeft(pre,(cc,cv))
nextPost = shiftLeft(post,(hc,hv))
return (cc,cv,nextPre,nextPost,nextHorizon)

def printLine (cc,cv,pre,post):
if cc == dash: return
simplePre = [c for (c,v) in pre]
simplePost = [c for (c,v) in post[1:]]
for c in simplePre: print "%s " % c,
print "%s " % cc,
for c in simplePost: print "%s " % c,
print cv

def processWord (str):
d = (dash,dash)
pre = (d,d,d,d,d)
post = (d,d,d,d,d,d)
horizon = 6
while horizon < len(str):
(cc,cv,nextPre,nextPost,nextHorizon) = advance(str,pre,post,horizon)
printLine(cc,cv,pre,post)
pre = nextPre
post = nextPost
horizon = nextHorizon

def main ():
strlist = map(makeWord, inputString.split())
map(processWord,strlist)

main()


###



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple python scrip for collocation discovery

2008-08-17 Thread Emad Nawfal (عماد نوفل)
Hello Kent, Bob, Steve
Thank you all for your help and suggestions. I believe I 'll have a good
program soon.

2008/8/17 Kent Johnson <[EMAIL PROTECTED]>

> On 8/16/08, Emad Nawfal (عماد نوفل) <[EMAIL PROTECTED]> wrote:
> > #! usr/bin/python
> > # Chi-squared collocation discovery
> > # Important definitions first. Let's suppose that we
> > # are trying to find whether "powerful computers" is a collocation
> > # N = The number of all bigrams in the corpus
> > # O11 = how many times the bigram "powerful computers" occurs in the
> corpus
> > # O22 = the number of bigrams not having either word in our collocation =
> N
> > - O11
> > #  O12 = The number of bigrams whose second word is our second word
> > # but whose first word is not "powerful"
>
> This is just the number of occurrances of the second word - O11, isn't it?
>
> > # O21 = The number of bigrams whose first word is our first word, but
> whose
> > second word
> > # is different from oour second word
>
> This is the number of occurrances of the first word - O11.
>
> So one way to solve this would be to make two dictionaries - one which
> counts bigrams and one which counts words. Then you would get the
> numbers with just three dictionary lookups.
>
> Kent
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple python scrip for collocation discovery

2008-08-16 Thread Emad Nawfal (عماد نوفل)
Thank you so much Steve,
I followed your advice about calculating o the fly and it really rang a
bell. Now I have this script. It's faster and does not give me the nasty
memory error message the first one sometimes did:
# Chi-squared collocation discovery
# Important definitions first. Let's suppose that we
# are trying to find whether "powerful computers" is a collocation
# N = The number of all bigrams in the corpus
# O11 = how many times the bigram "powerful computers" occurs in the corpus
# O22 = the number of bigrams not having either word in our collocation = N
- O11
#  O12 = The number of bigrams whose second word is our second word
# but whose first word is not "powerful"
# O21 = The number of bigrams whose first word is our first word, but whose
second word
# is different from oour second word
###

print """
*
*   Welcome to the Collocationer
**
*   *
*
"""
# Let's first get the text and turn into bigrams
#tested_collocate = raw_input("Enter the bigram you think is a
collocation\n")
#word1 = tested_collocate.split()[0]
#word2 = tested_collocate.split()[1]
word1 = 'United'
word2 = 'States'

infile = file("1.txt")
# initilize the counters

N = 0
O11= 0
O22 = 0
O12 = 0
O21 = 0
for line in infile:
length = len(line.split()) # a variable to hold the length of each line

if len(line.split()) <=1:
continue
for word in line.split():
N+=1
for i,v in enumerate(line.split()):
if i< length-1:
if word1 == v and word2 == line.split()[i+1]:
O11 +=1
for i,v in enumerate(line.split()):
if i < length -1:
if word1 != v and word2 != line.split()[i+1]:
O22+=1
for i,v in enumerate(line.split()):
if i< length-1:
if word1 != v and word2 == line.split()[i+1]:
O12+=1
for i,v in enumerate(line.split()):
if i< length-1:
if word1 == v and word2 != line.split()[i+1]:
O21+=1




chi2 = (N * ((O11 * O22 - O12 * O21) ** 2))/ float((O11 + O12) * (O11 + O21)
* (O12 + O22) * (O21 + O22))
print "Chi-Squared = ", chi2
if chi2 > 3.841:
print "These two words form a collocation"
else:
print "These two words do not form a collocation"






On Sat, Aug 16, 2008 at 2:09 PM, Steve Willoughby <[EMAIL PROTECTED]> wrote:

> On Sat, Aug 16, 2008 at 01:55:36PM -0400, Emad Nawfal ( ) wrote:
> > Hello Tutors,
> > I'm trying to write a small scrip to find collocations using chi squared,
>
> Minor nit, but the word you're looking for here is "script".  "Scrip"
> is also an English word but means something completely different.
> Looking professional in a field includes using the jargon correctly.
>
> > depending on a fairly big corpus.
> > The program below does a good job, but it's too slow, and I need to
> process
> > something like 50 million words.
> > How can I make it run fast?
>
> How fast is fast enough?
> What's its time now?
> Can you identify where it might be slowing down?
>
> Depending on the order of magnitude of the speedup you're looking
> to achieve, the answer could be very different.
>
> > # Let's first get the text and turn into bigrams
> > bigrams = []
> > infile = file("corpus.txt")
> > text = infile.read().lower().split()
>
> This strikes me as needlessly memory-consuming.  You might want to
> iterate over lines of text instead of sucking the entire file into
> a giant string.  I'm guessing you want to recognize words next to
> one another even if separated by a newline?  Be aware of the cost,
> though, of passing potentially huge data values around.
>
> > infile.close()
> > for i,v in enumerate(text): # get words and their ranking number
> >  if i < len(text)-1: # This guarntees that the list index is not out
> of
> > range
> >   bigram = v, text[i+1] # each word and the two succeding words
> >   bigrams.append(bigram)
>
> Why don't you trust enumerate's i values, out of curiosity?
>
> It seems to me if you think of what you're collecting here
> you could do some calculating on the fly as you look through
> the list of words and wouldn't need to be building these
> lists and then going back through them.
>
> I'm trying to give some vague help without doing the work for you
> because we don't do homework exercises for people :)
>
> --
> Steve Willoughby|  Using billion-dollar satellites
> [EMAIL PROTECTED]   |  to hunt for Tupperware.
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___

Re: [Tutor] simple python scrip for collocation discovery

2008-08-16 Thread Emad Nawfal (عماد نوفل)
Dear Steve,
Thank you so much for your help.
Actually this is not homework. It's gonna be used in building a collocation
dictionary as part of my dissertation. Please remeber that I'm not a
programmer, so many of the terminologies may not be accessible to me.

Thank you also for attracting my attention to the typo

On Sat, Aug 16, 2008 at 2:09 PM, Steve Willoughby <[EMAIL PROTECTED]> wrote:

> On Sat, Aug 16, 2008 at 01:55:36PM -0400, Emad Nawfal ( ) wrote:
> > Hello Tutors,
> > I'm trying to write a small scrip to find collocations using chi squared,
>
> Minor nit, but the word you're looking for here is "script".  "Scrip"
> is also an English word but means something completely different.
> Looking professional in a field includes using the jargon correctly.
>
> > depending on a fairly big corpus.
> > The program below does a good job, but it's too slow, and I need to
> process
> > something like 50 million words.
> > How can I make it run fast?
>
> How fast is fast enough?
> What's its time now?
> Can you identify where it might be slowing down?
>
> Depending on the order of magnitude of the speedup you're looking
> to achieve, the answer could be very different.
>
> > # Let's first get the text and turn into bigrams
> > bigrams = []
> > infile = file("corpus.txt")
> > text = infile.read().lower().split()
>
> This strikes me as needlessly memory-consuming.  You might want to
> iterate over lines of text instead of sucking the entire file into
> a giant string.  I'm guessing you want to recognize words next to
> one another even if separated by a newline?  Be aware of the cost,
> though, of passing potentially huge data values around.
>
> > infile.close()
> > for i,v in enumerate(text): # get words and their ranking number
> >  if i < len(text)-1: # This guarntees that the list index is not out
> of
> > range
> >   bigram = v, text[i+1] # each word and the two succeding words
> >   bigrams.append(bigram)
>
> Why don't you trust enumerate's i values, out of curiosity?
>
> It seems to me if you think of what you're collecting here
> you could do some calculating on the fly as you look through
> the list of words and wouldn't need to be building these
> lists and then going back through them.
>
> I'm trying to give some vague help without doing the work for you
> because we don't do homework exercises for people :)
>
> --
> Steve Willoughby|  Using billion-dollar satellites
> [EMAIL PROTECTED]   |  to hunt for Tupperware.
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] simple python scrip for collocation discovery

2008-08-16 Thread Emad Nawfal (عماد نوفل)
Hello Tutors,
I'm trying to write a small scrip to find collocations using chi squared,
depending on a fairly big corpus.
The program below does a good job, but it's too slow, and I need to process
something like 50 million words.
How can I make it run fast?
Your help is appreciated.
Emad nawfal


#! usr/bin/python
# Chi-squared collocation discovery
# Important definitions first. Let's suppose that we
# are trying to find whether "powerful computers" is a collocation
# N = The number of all bigrams in the corpus
# O11 = how many times the bigram "powerful computers" occurs in the corpus
# O22 = the number of bigrams not having either word in our collocation = N
- O11
#  O12 = The number of bigrams whose second word is our second word
# but whose first word is not "powerful"
# O21 = The number of bigrams whose first word is our first word, but whose
second word
# is different from oour second word
###

print """
*
*   Welcome to the Collocationer
**
*   *
*
"""
# Let's first get the text and turn into bigrams
bigrams = []
infile = file("corpus.txt")
text = infile.read().lower().split()
infile.close()
for i,v in enumerate(text): # get words and their ranking number
 if i < len(text)-1: # This guarntees that the list index is not out of
range
  bigram = v, text[i+1] # each word and the two succeding words
  bigrams.append(bigram)



tested_collocate = raw_input("Enter the bigram you think is a
collocation\n")
word1 = tested_collocate.split()[0]
word2 = tested_collocate.split()[1]

N = len(bigrams)
O11 = bigrams.count(tuple(tested_collocate.split()))
O22 = [bigram for bigram in bigrams if word1 !=  bigram[0] and word2 !=
bigram[1]]
O12 = [bigram for bigram in bigrams if bigram[1] == word2 and bigram[0] !=
word1]
O21 = [bigram for bigram in bigrams if bigram[0]== word1 and bigram[1] !=
word2]


O22 = len(O22)
O12 = len(O12)
O21 = len(O21)


chi2 = (N * ((O11 * O22 - O12 * O21) ** 2))/ float((O11 + O12) * (O11 + O21)
* (O12 + O22) * (O21 + O22))
print "Chi-Squared = ", chi2
if chi2 > 3.841:
print "These two words form a collocation"
else:
print "These two words do not form a collocation"

raw_input('Enter to Exit')









-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner problem: name 'convertToFahrenheit' is not defined

2008-08-14 Thread Emad Nawfal (عماد نوفل)
On Thu, Aug 14, 2008 at 7:08 PM, Joseph Bae <[EMAIL PROTECTED]> wrote:

> Hi all,
>
> I'm new to Python (and programming in general) and need some help!
>
> Here is my code so far for a temperature conversion program (converts
> between Fahrenheit and Celsius):
>
>
>
> temp = input("Enter A Number : ")
> convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? : ")
>
> if convertTo == "F":
> convertedTemp = convertToFahrenheit(temp)
> print "%d Celsius = %d Fahrenheit" % (temp, convertedTemp)
> else:
> convertedTemp = convertToCelsius(temp)
> print "%d Fahrenheit = %d Celsius" % (temp, convertedTemp)
>
> def convertToFahrenheit(t):
> tF = (9.0/5.0) * (t + 32)
> return tF
>
> def convertToCelsius(t):
> tC = (9.0/5.0) * (t - 32)
> return tC
>
>
>
> It worked fine without having extra functions but once I put
> convertToFahrenheit and convertToCelsius in (just for practice really), this
> happened:
>
> Enter A Number : 50
> Convert to (F)ahrenheit or (C)elsius? : F
> Traceback (most recent call last):
>  File "TemperatureConverter.py", line 5, in 
>   convertedTemp = convertToFahrenheit(temp)
> NameError: name 'convertToFahrenheit' is not defined
>
> This is most likely a very simple error, but can someone please clarify for
> me why it's behaving this way?
>

You just need to define the functions before you use them
Arranging the order od your program makes it work. Like this
def convertToFahrenheit(t):
tF = (9.0/5.0) * (t + 32)
return tF

def convertToCelsius(t):
tC = (9.0/5.0) * (t - 32)
return tC
temp = input("Enter A Number : ")
convertTo = raw_input("Convert To (F)ahrenheit or (C)elsius? : ")

if convertTo == "F":
convertedTemp = convertToFahrenheit(temp)
print "%d Celsius = %d Fahrenheit" % (temp, convertedTemp)
else:
convertedTemp = convertToCelsius(temp)
print "%d Fahrenheit = %d Celsius" % (temp, convertedTemp)



>
>
> Thanks!
>
> Joe
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading List from File

2008-07-31 Thread Emad Nawfal (عماد نوفل)
On Thu, Jul 31, 2008 at 8:07 AM, S Python <[EMAIL PROTECTED]> wrote:

> Hi Everyone,
>
> I am trying to read a comma-delimitted list ("aaa","bbb","ccc") from a text
> file and assign those values to a list, x, such that:
>
> x = ["aaa", "bbb", "ccc"]
>
> The code that I have come up with looks like this:
>
> >>> x = []
> >>> f = open(r'c:\test.txt', 'r')
> >>> x.extend(f.readlines())
> >>> x
> ['"aaa","bbb","ccc"']
>
> If you look closely, there is an extra pair of single quotes (') that
> encapsulates the string.  Therefore, len(x) returns 1, instead of 3.  Is
> there a function to "separate" this list out?  I hope my question makes
> sense.
>
> Thanks in advance.
>
> Samir
>
>
This is an answer by a novice, and it may not be the best around;
Why don't you first get rid of the quotation marks and then split on the
comma:

>>> f = open(r'c:\test.txt', 'r').read().replace('"', '')
>>> x = []
>>> x.extend(f.split(","))
>>> x
['aa', ' bb', ' cc']
>>> len(x)
3

>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor