Re: [Tutor] Example for read and readlines() (Asad)

2018-11-13 Thread Avi Gross
Asad,

Like many projects, there may be many ways to do things BUT some rules do
apply.

You can only read an open file ONCE unless you seek back to the beginning or
reopen it.

string = f3.read()
string1 = f3.readlines()

The first line reads the entire file into a single buffer.

The second program line won't work as intended. The first consumed the
entire file.

Much of the rest is not organized well enough for me to understand what you
want to do. I find it important for people to try some simple things like
examining the values step by step. Had you typed

print (string)
print (string1)

on a small sample file, you might have fixed that before continuing. Then
each step along the way you could examine and verify it made sense up to
that point.

Try writing the outline of the logic of your program first in English or
your native language as an algorithm. Then see what tools are needed. Look
at a sample of the log you are evaluating and see what it takes to locate
the lines you want and then to break out the parts you want to keep for
further use. 

What I see looks like this:

If you find one instance of the string "ERR1"
Then 
You want to find ALL (nonoverlapping) regions consisting of an upper-case
letter followed by two lower-case letters and a space and either a space or
digits 1 to 3 and digits 0-9 and a space and ...

Fairly complex pattern.

But you are searching the contents of the ENTIRE file for this and since you
seem to have wanted to replace all newlines by spaces and your pattern
includes spaces, this would match something that wrapped around from line to
line. Is this what you wanted?

You then switch gears to using the readlines version and I decided to get
back to my regularly scheduled life. As noted, that probably is an empty
string or worse. Good luck.

-Original Message-
From: Tutor  On Behalf Of
Asad
Sent: Sunday, November 11, 2018 8:54 PM
To: tutor@python.org
Subject: Re: [Tutor] Example for read and readlines() (Asad)

Hi All ,

   Thanks for the reply . I am building a framework for the two error
conditions, therefore I need to read and readlines because in one only regex
is required and in other regex+ n-1 line is required to process :

#Here we are opening the file and substituting space " " for each \n
encountered
f3 = open  (r"D:\QI\log.log", 'r')
string = f3.read()
string1 = f3.readlines()
regex = re.compile ( "\n" )
st = regex.sub ( " ", string )

if re.search('ERR1',st):
y=re.findall("[A-Z][a-z][a-z] [ 123][0-9]
[012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st)
print y

patchnumber = re.compile(r'(\d+)\/(\d+)')==> doesnot
work it only works if I use  #string = f3.read() for j in
range(len(string1)):
if re.search ( r'ERR2', string1[j] ):
print "Error line \n", string1[j - 1]
mo = patchnumber.search (string1[j-1])
a = mo.group()
print a
print os.getcwd()
break

Please advice how to proceed.

Thanks,



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


Re: [Tutor] Example for read and readlines() (Asad)

2018-11-13 Thread Cameron Simpson

On 12Nov2018 07:24, Asad  wrote:
  Thanks for the reply . I am building a framework for the two 
  error

conditions, therefore I need to read and readlines because in one only
regex is required and in other regex+ n-1 line is required to process :

#Here we are opening the file and substituting space " " for each \n
encountered
f3 = open  (r"D:\QI\log.log", 'r')
string = f3.read()
string1 = f3.readlines()


My first remark is that both these lines read _and_ _consume_ the file 
content. So "string" gets the entire file content, and "string1" gets an 
empty array of lines, because the file is already at the end, where 
there is no more data.


It is also better to use this idiom to read and then close a file:

 with open(r"D:\QI\log.log", 'r') as f3:
   string = f3.read()

This reliably closes f3 once the "with" suite completes, even if there's 
some kind of exception.


You need 2 copies of the file data. You can do this 2 ways. The first 
way is to read the file twice:


 with open(r"D:\QI\log.log", 'r') as f3:
   string = f3.read()
 with open(r"D:\QI\log.log", 'r') as f3:
   string1 = f3.readlines()

The efficient way is to read the file once, then make string from 
string1, or string1 from string. For example:


 with open(r"D:\QI\log.log", 'r') as f3:
   string1 = f3.readlines()
 string = ''.join(string1)


regex = re.compile ( "\n" )
st = regex.sub ( " ", string )


Using a regular expression to replace a fixed string such as "\n" is 
overkill. Consider:


 st = string.replace("\n", " ")

Python strings have a bunch of handy methods for common simple things.  
Have a read of the docs for further detail.



if re.search('ERR1',st):
   y=re.findall("[A-Z][a-z][a-z] [ 123][0-9]
[012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st)
   print y


On the other hand, a regexp is a good tool for something like the above.


patchnumber = re.compile(r'(\d+)\/(\d+)')==> doesnot
work it only works if I use  #string = f3.read()


This may be because "string" is a single string (the whole file text as 
one string). "string1" is a _list_ of individual strings, one for each 
line. Personally, i would call this "strings" or "lines" or some other 
plural word; your code will be easier to read, and easier to debug.


Conversely, a misleading name makes debugging harder because you expect 
the variable to contain what its name suggests, and if it doesn't this 
will impede you in finding problems, because you will be thinking the 
whrong thing about what your program is doing.



for j in range(len(string1)):
   if re.search ( r'ERR2', string1[j] ):
   print "Error line \n", string1[j - 1]
   mo = patchnumber.search (string1[j-1])
   a = mo.group()
   print a
   print os.getcwd()
   break

Please advice how to proceed.


mo.group() returns the whole match. The above seems to look for the 
string 'ERR2' in a line, and look for a patch number in the previous 
line. Is that what is it supposed to do?


If the above isn't working, it would help to see the failing output and 
a description of what good output is meant to look like.


Finally, please consider turning off "digest mode" in your list 
subscription. It will make things easier for everyone.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create a html hyperlink for a document

2018-11-13 Thread Steven D'Aprano
On Mon, Nov 12, 2018 at 04:44:17PM +0530, Asad wrote:
> Hi All ,
> 
> I am creating a python script to analyze the log and provide a
> solution , in the solution part I want to include a document and create a
> hyperlink to the document so that its clickable .
> I have python 2.6.6 cannot use :hyperlink module because this is production
> server and the module cannot be installed on it . Need some trick to create
> it .

If it wasn't obvious from my previous response, html hyperlinks are just 
text. There's no "trick" to create it: you just type the text which 
makes the HTML code.



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


Re: [Tutor] How to create a html hyperlink for a document

2018-11-13 Thread Steven D'Aprano
On Mon, Nov 12, 2018 at 04:44:17PM +0530, Asad wrote:
> Hi All ,
> 
> I am creating a python script to analyze the log and provide a
> solution , in the solution part I want to include a document and create a
> hyperlink to the document so that its clickable .
> I have python 2.6.6 cannot use :hyperlink module because this is production
> server and the module cannot be installed on it . Need some trick to create
> it .

This is not something you can control. Whether or not the link is 
clickable depends on the application you use to view the document.

For some applications, the answer is to just include a URL as text:

http://www.python.org/

will do. For other applications, you will need to format the URL as a 
HTML reference:

http://www.python.org/;>Python


or as markdown:

[Python](http://www.python.org/)


or possibly even ReST:

`Python `_.


or some other format. Or possibly you can't do it at all. Notepad, for 
example, has no concept of clickable links.



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