[Tutor] PyMOTW: difflib

2008-04-05 Thread Dick Moores


See
<
http://blog.doughellmann.com/2007/10/pymotw-difflib.html>
And my try with the Differ example,
<
http://py77.python.pastebin.com/f41ec1ae8>, which also shows the
error,
"E:\Python25\pythonw.exe" -u
"E:\PythonWork\demo_pymotw-difflib.py" 
Traceback (most recent call last):
 File "E:\PythonWork\demo_pymotw-difflib.py", line 12, in

   from difflib_data import *
ImportError: No module named difflib_data
What is difflib_data ?
Thanks,
Dick Moores




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


Re: [Tutor] Diff for Python

2008-04-05 Thread Marc Tompkins
Sorry - forgot to reply to the list the first time...
On Sat, Apr 5, 2008 at 3:04 PM, Alan Gauld <[EMAIL PROTECTED]>
wrote:

> On Windows you can use FC - File Compare.
> Its not as powerful as diff but it will highlight differences.
>

On Windows, I've tried a bunch of diff tools - it's probably the way my
brain is wired, but I generally find it harder to understand what the diff
tool is telling me than it would have been to print out the damn files and
compare them on paper.  I feel like I'm being given clues so I can work out
the puzzle myself... if I wanted that, I would do a crossword, not use a
software tool.

So my tool of choice (since I discovered it about three months ago) is the
Compare plugin in Notepad++.  It simply displays the files in separate child
windows, forcibly aligns them with "soft" newlines, and synchronizes the
windows' scrollbars to keep them lined up side by side.  It also shades the
lines in different colors depending on whether the lines are the same in
both files, or one file has a line that the other doesn't, or both files
have the line but different versions.  None of this is new, of course, but
I've never used a tool before that got it all so _right_ and made it so
simple to use and to read.  (Open two or more files in the editor, hit
Alt-D, read.  If necessary, cut and paste between the windows - hit Alt-D
again to resync - read.)

I'm sure there are more sophisticated choices.  Honestly, I sometimes feel a
little guilty using it, 'cause I think I ought to be working harder...  I'm
sure that both vi and emacs do this in a way that mere mortals such as I
cannot appreciate, but I think you must have had to start using either vi or
emacs at a very early age to be able to enjoy the experience.  I'm putting
on my flame-retardant Nomex suit as I type this.

(Tying this thread in with one from last week...)
As a general-purpose Windows editor, I definitely recommend Notepad++.
(It's free, but I moved to it from TextPad, in which I had invested $50.  If
you knew me, you'd know what high praise this is for Notepad++.)  For Python
/ wxPython development, though, I love me some SPE.

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


Re: [Tutor] Diff for Python

2008-04-05 Thread Alan Gauld
On Windows you can use FC - File Compare.
Its not as powerful as diff but it will highlight differences.

Help FC

will get you the switch options.

Or just use cygwin - any Unix user on Windows
should get cygwin as a matter of course IMHO! :-)

Alan G.


"Eric Walstad" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> And my whoops, I should have sent my first one to the list, too.
>
> I don't run Windows very often.  I think 'WinDiff' is what I used
> there.  Have you tried that?
>
> There's always cygwin, too.
>
> Eric.
> On Sat, Apr 5, 2008 at 12:28 PM, Wayne Watson
> <[EMAIL PROTECTED]> wrote:
>>
>>  Whoop, I should have specified I'm on Win XP.
>>
>>
>>  Eric Walstad wrote:
>>  Hi Wayne,
>>
>> On Fri, Apr 4, 2008 at 8:37 PM, Wayne Watson
>> <[EMAIL PROTECTED]> wrote:
>>
>>
>>  Is there a Linux diff-like command for Python code? I'd like to 
>> see the
>>  difference between two py files.
>>
>>  Why don't you just use diff?
>> What OS are you on?
>>
>> diff -Bu fileone.py filezero.py
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


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


Re: [Tutor] Diff for Python

2008-04-05 Thread Eric Walstad
And my whoops, I should have sent my first one to the list, too.

I don't run Windows very often.  I think 'WinDiff' is what I used
there.  Have you tried that?

There's always cygwin, too.

Eric.
On Sat, Apr 5, 2008 at 12:28 PM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
>
>  Whoop, I should have specified I'm on Win XP.
>
>
>  Eric Walstad wrote:
>  Hi Wayne,
>
> On Fri, Apr 4, 2008 at 8:37 PM, Wayne Watson
> <[EMAIL PROTECTED]> wrote:
>
>
>  Is there a Linux diff-like command for Python code? I'd like to see the
>  difference between two py files.
>
>  Why don't you just use diff?
> What OS are you on?
>
> diff -Bu fileone.py filezero.py
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code optmisation

2008-04-05 Thread W W
On Sat, Apr 5, 2008 at 2:44 AM, Alan Gauld <[EMAIL PROTECTED]> wrote:
> "yogi" <[EMAIL PROTECTED]> wrote

>  > Question 2: If I have convert this code into a function.
>  > Should I ?
>
>  Only if you have a need to reuse it in a bigger context
>  or of you want to parameterize it. You could maybe break
>  it out into smaller helper functions such as the one I
>  suggested above.

Personally, functions are always fun!
...

And useful. Even if you don't need to play with them later, there's no
loss, because the exercise is in and of itself a good one. Plus, I
think functions tend (though not always) force you to clean up your
code: As Alan has mentioned, there are a few places that you duplicate
code. The exercise of converting to functions will often point out the
fact that you have multiple code.

Have fun!
-Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Diff for Python

2008-04-05 Thread W W
Vim also has a similar command

On Sat, Apr 5, 2008 at 12:59 AM, Terry Carroll <[EMAIL PROTECTED]> wrote:
> On Fri, 4 Apr 2008, Wayne Watson wrote:
>
>  > Is there a Linux diff-like command for Python code? I'd like to see the
>  > difference between two py files.
>
>  You could just use diff.
>
>  Python itself also has difflib:
>
>  http://python.org/doc/2.5/lib/module-difflib.html
>
>
>
>  ___
>  Tutor maillist  -  Tutor@python.org
>  http://mail.python.org/mailman/listinfo/tutor
>



-- 
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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code optmisation

2008-04-05 Thread Alan Gauld
"yogi" <[EMAIL PROTECTED]> wrote

> #/bin/python
> import sys, os, csv, re
> x =  0  #Define Zero for now
> var = 100   #Taking the variation
> # This programme finds the SNPs from the range passed
> # csv splits columns and this file is tab spaced
> fis = csv.reader(open("divs.map", "rb"), delimiter='\t', 
> quoting=csv.QUOTE_NONE)
> for row in fis:
> # csv splits columns and this file is ","  spaced
>gvalues = csv.reader(open("genvalues", "rb"), delimiter=',', 
> quoting=csv.QUOTE_NONE)

Move this outside the loop otherwise you re-read the file
for every line in the other file - slow!

>for gvalue in gvalues:
> # To see  Columns (chr) Match
>if row[0] == gvalue[0]:
> # If  Column 3  (range) is Zero  print row
>if int(gvalue[3]) ==  x:
>a = int(gvalue[1]) - var
>b = int(gvalue[2]) + var + 1
>if int(a <= int(row[3]) <= b):
>print   row

I'd probably use names like 'lo' and 'hi' instead of 'a'
and 'b' but thats a nit pick... but you don't want to convert
the result of the test to an int, the result is a boolean and
you never use the int you create so its just wasted
processing power...

> # If  Column 3  (range) is not zero find matches and print row
>else:
>a = int(gvalue[1]) - var
>b = int(gvalue[2]) + var + 1

Repeated code, you could move this above the if test
since its used by both conditions. Easier to maintain if
you change the rules...

>if int(a <= int(row[3]) <= b):

>print row
again you don;t need the int() conversion.


>c = int(gvalue[3]) - var
>d = int(gvalue[4]) + var + 1
>if int(c <= int(row[3]) <= 
> d):

and again. You do this so often I'd consider making it a
helper function

def inLimits(min, max, val):
 lo = int(min) - var
 hi = int(max) + var + 1
 return lo <= int(val) <= hi

Your else clause then becomes

else:
if inLmits(gvalue[1],gvalue[2],row[3])
   print row
   if inLimits(gvalue[3], gvalue[4], row[3]
   print row

Which is slightly more readable I think.


> Question1 : Is there a better way ?

There's always a better way.
As a general rule for processing large volumes of data
I tend to go for a SQL database. But thats mainly based
on my experience that if you want to do one lot of queries
you'll eventually want to do more - and SQL is designed
for doing queries on large datasets, Python isn't (although
Python can do SQL...).

> Question2 : For now I'm using shells time  call  for calculating
> time required. Does Python provide a more fine grained check.

try timeit...

> Question 2: If I have convert this code into a function.
> Should I ?

Only if you have a need to reuse it in a bigger context
or of you want to parameterize it. You could maybe break
it out into smaller helper functions such as the one I
suggested above.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Question about global variables on modules

2008-04-05 Thread Alan Gauld
"Tiago Katcipis" <[EMAIL PROTECTED]> wrote

> results of a matrix using jacob. I want to inside the module (inside 
> a
> function on the module )assign a value to a global variable, but the
> only way i found to do this inside the own module function is 
> importing
> the module inside himself. Is there another way of doing this?

Look at the 'global' keyword.

Explained further in the 'What's in a Name' topic of my tutorial.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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