Difficult loop?

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor
David | 15 Oct 17:32
Favicon

Re: Looking for Clues to Make a Program Produce avi Files Instead ...

David wrote:
> You could use mencoder, part of mplayer;
> mencoder -oac mp3lame -ovc lavc -vf scale=0:0:0:0:0:0:qpal
> somemovfile.mov -o outavifile.avi
>
> Here is a program to convert ogg theora to avi;
>
> #!/usr/bin/python
> #Filename = ogg_to_avi.py
>  
> import os,sys
>  
> print '\n\togg theora video to avi video\n'
> ogg = raw_input('Enter the ogg file to convert: ')
> avi = raw_input('Enter the avi to be created: ')
>  
> os.system("mencoder %s -ovc lavc -oac mp3lame -o %s" % (ogg, avi))
>  
> if os.path.exists(avi):
>     print '\n\tYour', avi, 'has been created.\n'
>     sys.exit()
>  
> else:
>     print 'error, something went wrong!'
>     sys.exit()
>
>
>   

--

-- 
powered by Gentoo/GNU Linux
http://linuxcrazy.com

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Cezary Krzyżanowski | 15 Oct 16:41

First time webaps as example

Hi!

I'm encouraging the students I teach to use python for their webapps.
It's usually their first encounter with python, mostly even with
webapps. C#, PHP+MySQL is something put into them in my university.

So I'm looking for good ideas of first-time webaps to write. I tell them
to use django/pylons/turbogears to write something like kittenwars,
hotornot, a wiki, a simple shop, paster, a messageboard.

I'd like to find some other simple and interesting apps to show them,
similar to kittenwar and hotornot, as they attract the most students and
make them much more enthusiastic.

Since I'm already asking, maybe some ideas for other, 'non-webaps' to
show them to mimic and try out stuff like Pair Programming, Test Driven
Developement and other 'strange' (to them) ways of programming.

Thanks in advance,
Cz <at> rny

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Rajeev Nair | 15 Oct 08:30

Re: using lists as values for key in dictionary?

hi.

Is it possible to use and how to acess it?




regards

rajeev
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Basil B Thoppil | 15 Oct 07:26

Use Regular Expressions

Hi everyone,
I am using the cPAMIE library for automation of websites. It has a function findTextbox(name) , which takes the name(a string) of the text box and searches for it and return True if it exists. Now I dont have a static name and so  I wanted to use regular expression. I cannot pass regular expressions directly since it doesnt take a regular expression. How can I make use of RE in my function.

regards
-basiL-

--



_______________________________________________________
"Dream is not what you see in sleep. Dream is the thing which does not let you sleep" - Dr.Abdul Kalam

_______________________________________________________
_______________________________________________________

_______   Basil B Thoppil
_______   Room No. C-22, Mens Hostel,
_______   Government Engineering College,
_______   Thrissur-9, Kerala.
_______   +919846717505



_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Wayne Watson | 15 Oct 07:22
Favicon

Looking for Clues to Make a Program Produce avi Files Instead ...

... of mov files. (See Subject). Our meteor software program uses a py program to produce a movie from images taken by an all-sky camera. The frames are in a simple but different format than likely used anywhere else. From these, it produces a mov file. As I understand it, mov files are used by QuickTime. If easily done, I'd like to produce avi files instead. Perhaps someone might be able to give me a clue if it's possible to modify this program for avi. Maybe there's a program that translates mov files to avi? I'm not sure if that degrades the final movie. No audio is involved.

I can post the file (600 lines+) or send it, if anyone wants to take a look at it.

--
Signature.html Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) "Things are going to get a lot worse befoe they get worse." -- Lily Tomlin Web Page: <www.speckledwithstars.net/>
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Telmo Andres Sanchez | 14 Oct 21:37

Learning to extract information from ABAQUS .dat and .rpt files using Python

Hello folks,

This is something that must have been asked several times. I want to post process data obtained after running a FEM in ABAQUS. The requested results are usually printed to the .dat file or .rpt file. I would like to learn how to extract the information that I need (i.e. displacements and stresses at the nodes) from this file using Python. The fact is that I am kind of new at ABAQUS and have zero knowledge about Python. Therefore, any help with websites, examples, tutorials, etc. would be appreciated.

Thank you!

Andres
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Luke Paireepinart | 14 Oct 16:11

Fwd: Technical aspect of os.path.isfile()

Oops, replied off-list accidentally.

---------- Forwarded message ----------
From: Luke Paireepinart <rabidpoobear <at> gmail.com>
Date: Tue, Oct 14, 2008 at 9:10 AM
Subject: Re: [Tutor] Technical aspect of os.path.isfile()
To: W W <srilyk <at> gmail.com>

File renames are much faster than file copies.  So just copy the files
with an extra extension, and then when they're done copying, rename
them.  I believe that this prevents a race condition because the file
is not being exclusively accessed by the rename function.
As far as dealing with it, if you really think it's an issue, you can
always do interprocess communciation to allow them to keep up with the
current state of the other.  If that's not viable there are probably
alternatives.

On Tue, Oct 14, 2008 at 8:13 AM, W W <srilyk <at> gmail.com> wrote:
> I suppose this is really more of a technical aspect, dealing with the
> possibility of a race condition (although in my personal case, the
> probability is infinitely small).
>
> I'm writing a program that uses the ConfigParser module to open a config
> file, and if that file isn't present, I'd like to create it. I'll also be
> syncing a series of files from one directory to another. I'll be creating a
> list of files in each dir using os.walk, and copying the files that aren't
> present in the destination dir.
>
>  After looking online I've found various opinions on what the "best" way is
> to deal with the possibility of a race condition. The way I intend to use my
> program, a race condition should never occur, but if I ever release the
> source, I'd like it to at least fail gracefully.
>
> Is there a solid way to prevent a race condition, or is it just sort of a
> "do your best" type situation?
>
> Thanks,
> Wayne
>
> --
> To be considered stupid and to be told so is more painful than being called
> gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
> every vice, has found its defenders, its rhetoric, its ennoblement and
> exaltation, but stupidity hasn't. - Primo Levi
>
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

URBAN LANDREMAN | 14 Oct 16:00
Favicon

Edit a Word document programmatically

Good morning,

I'm trying to write some Python code to programmatically do a Find and Replace of a string within a Microsoft Word document.
Any suggestions on where I could look to find how to do such a function?

Thank you.

Urban Landreman

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
W W | 14 Oct 15:13

Technical aspect of os.path.isfile()

I suppose this is really more of a technical aspect, dealing with the possibility of a race condition (although in my personal case, the probability is infinitely small).

I'm writing a program that uses the ConfigParser module to open a config file, and if that file isn't present, I'd like to create it. I'll also be syncing a series of files from one directory to another. I'll be creating a list of files in each dir using os.walk, and copying the files that aren't present in the destination dir.

 After looking online I've found various opinions on what the "best" way is to deal with the possibility of a race condition. The way I intend to use my program, a race condition should never occur, but if I ever release the source, I'd like it to at least fail gracefully.

Is there a solid way to prevent a race condition, or is it just sort of a "do your best" type situation?

Thanks,
Wayne

--
To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
W W | 14 Oct 01:23

Re: Python Basic Help

If you hit reply to all, you'll send your response to the list, too.

On Mon, Oct 13, 2008 at 2:05 PM, burmantanya <at> yahoo.ca <burmantanya <at> yahoo.ca> wrote:
<snip>
i am getting tons of error messages for the last one!!! i was wondering if we could use if loop inside another if loop

Yes, you can use loops inside other loops.

If you're getting errors, post the exact error message you get, for instance when I type this:

In [29]: import zanzubar

I get this error:
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)

C:\Documents and Settings\wayne\My Documents\mathbook\<ipython console> in <module>()

ImportError: No module named zanzubar

(I'm using IPython, so your error will be formatted a little different)
HTH,
Wayne
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Gmane