473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to parse this line of code manually

Hi all,

It is well known that Python is appreciated for its merit of concise.
However, I found the over concise code is too hard to understand for
me.

Consider, for instance,
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
NWORDS)

Shall I understand the code in set() as
for e2 in edits1(e1) {
if e2 in NWORDS {
for e1 in edits1(word) {
e2
}
}
}

And a general question is: Is there any tip available to understand
the code in one line, or what's the parsing priority (left to right,
right to left, or other possibilities)

Any suggestions are welcome!

The code is a simple spell checker from
http://www.norvig.com/spell-correct.html

Best regards,
Davy

Aug 28 '07 #1
4 1821
On Aug 28, 11:00 am, Davy <zhushe...@gmail.comwrote:
Hi all,

It is well known that Python is appreciated for its merit of concise.
However, I found the over concise code is too hard to understand for
me.

Consider, for instance,
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
NWORDS)

Shall I understand the code in set() as
for e2 in edits1(e1) {
if e2 in NWORDS {
for e1 in edits1(word) {
e2
}
}

}
[SNIP]
Hi all, I figured it myself. It is left to righ parse, right?
So the above one is like
for e1 in edits1(word) {
for e2 in edits1(e1) {
if e2 in NWORDS {
push e2 to set
}
}
}
And a general question is: Is there any tip available to understand
the code in one line, or what's the parsing priority (left to right,
right to left, or other possibilities)

Any suggestions are welcome!

The code is a simple spell checker fromhttp://www.norvig.com/spell-correct.html

Best regards,
Davy

Aug 28 '07 #2
On 2007-08-28, Davy <zh*******@gmail.comwrote:
On Aug 28, 11:00 am, Davy <zhushe...@gmail.comwrote:
>Hi all,

It is well known that Python is appreciated for its merit of concise.
However, I found the over concise code is too hard to understand for
me.

Consider, for instance,
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
NWORDS)

Shall I understand the code in set() as
for e2 in edits1(e1) {
if e2 in NWORDS {
for e1 in edits1(word) {
e2
}
}

}
[SNIP]
Hi all, I figured it myself. It is left to righ parse, right?
So the above one is like
for e1 in edits1(word) {
for e2 in edits1(e1) {
if e2 in NWORDS {
push e2 to set
}
}
}
This is correct, although I am not sure what language you are using here, it
looks like a strange mix of Python and C to me.
>Any suggestions are welcome!
The idea is known as List comprehension (for lists, obviously), and comes from
functional programming, Bird & Wadler used it in their book.

The notation is very close to mathematics:

{ e2 | e1: edits(word), e2: edits(e1) in NWORDS }

or in LaTeX:

$\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}),
\forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$

:-)

(which in words is something like: collect values e2, where e1 comes from
'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS)
Sincerely,
Albert
Aug 28 '07 #3
On Aug 28, 2:59 am, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:
On 2007-08-28, Davy <zhushe...@gmail.comwrote:
On Aug 28, 11:00 am, Davy <zhushe...@gmail.comwrote:
Hi all,
It is well known that Python is appreciated for its merit of concise.
However, I found the over concise code is too hard to understand for
me.
Consider, for instance,
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
NWORDS)
Shall I understand the code in set() as
for e2 in edits1(e1) {
if e2 in NWORDS {
for e1 in edits1(word) {
e2
}
}
}
[SNIP]
Hi all, I figured it myself. It is left to righ parse, right?
So the above one is like
for e1 in edits1(word) {
for e2 in edits1(e1) {
if e2 in NWORDS {
push e2 to set
}
}
}

This is correct, although I am not sure what language you are using here, it
looks like a strange mix of Python and C to me.
Any suggestions are welcome!

The idea is known as List comprehension (for lists, obviously), and comes from
functional programming, Bird & Wadler used it in their book.

The notation is very close to mathematics:

{ e2 | e1: edits(word), e2: edits(e1) in NWORDS }

or in LaTeX:

$\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}),
\forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$

:-)

(which in words is something like: collect values e2, where e1 comes from
'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS)
For more examples:
http://docs.python.org/tut/node7.htm...00000000000000

A 'list comprehension' with parentheses instead of square-brackets
creates a generator instead of a list, which can be more memory-
efficient and allows for lazy evaluation.

Aug 28 '07 #4
On Aug 28, 7:28 pm, Dustan <DustanGro...@gmail.comwrote:
On Aug 28, 2:59 am, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:


On 2007-08-28, Davy <zhushe...@gmail.comwrote:
On Aug 28, 11:00 am, Davy <zhushe...@gmail.comwrote:
>Hi all,
>It is well known that Python is appreciated for its merit of concise.
>However, I found the over concise code is too hard to understand for
>me.
>Consider, for instance,
>def known_edits2(word):
> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
>NWORDS)
>Shall I understand the code in set() as
>for e2 in edits1(e1) {
> if e2 in NWORDS {
> for e1 in edits1(word) {
> e2
> }
> }
>}
[SNIP]
Hi all, I figured it myself. It is left to righ parse, right?
So the above one is like
for e1 in edits1(word) {
for e2 in edits1(e1) {
if e2 in NWORDS {
push e2 to set
}
}
}
This is correct, although I am not sure what language you are using here, it
looks like a strange mix of Python and C to me.
>Any suggestions are welcome!
The idea is known as List comprehension (for lists, obviously), and comes from
functional programming, Bird & Wadler used it in their book.
The notation is very close to mathematics:
{ e2 | e1: edits(word), e2: edits(e1) in NWORDS }
or in LaTeX:
$\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}),
\forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$
:-)
(which in words is something like: collect values e2, where e1 comes from
'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS)

For more examples:http://docs.python.org/tut/node7.htm...00000000000000
[SNIP]
Hi Hofkamp and Dustan,

Thank you for your help :)

Davy
>
A 'list comprehension' with parentheses instead of square-brackets
creates a generator instead of a list, which can be more memory-
efficient and allows for lazy evaluation.- Hide quoted text -

- Show quoted text -

Aug 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: M.B. | last post by:
Hi I'm currently converting an application from Java to C#, and in this application I need to read a C# source file, extract some information and then write a modified version of the original...
22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
22
by: Illya Havsiyevych | last post by:
Hello How easily parse VB/VBA code by VB/VBA code ? Is any ready solutions ? Thank's, illya
3
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
8
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and I need to parse strings that look similar to the one below. All 5 rows will make up one string. I have a form where a use can copy/paste data like what you...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
5
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.