Parsing file format to ensure file meets criteria

2009-12-17 Thread seafoid

Hi folks,

I am new to python and am having some trouble parsing a file.

I wish to parse a file and ensure that the format meets certain
restrictions.

The file format is as below (abbreviated):

c this is a comment
p wcnf 1468 817439 186181
286 32 0
186191 -198 -1098 0
186191 98 -1098 1123 0

Lines beginning c are comment lines and must precede all other lines.

Lines beginning p are header lines with the numbers being 'nvar', 'nclauses'
and 'hard' respectively.

All other lines are clause lines. These must contain at least two integers
followed by zero. There is no limit on the number of clause lines.

Header lines must precede clause lines.

In the above example:
nvar = 1468
nclauses = 817439
hard = 186191

Now for the interesting part...

The first number in a clause line = weight.
All else are literals.
Therefore, clause = weight + literals

weight <= hard
|literal| > 0
|literal| <= nvar
number of clause lines = nclauses

My attempts thus far have been a dismal failure, computing is so viciously
logical :confused:

My main problem is that below:

fname = raw_input('Please enter the name of the file: ')

z = open(fname, 'r')

z_list = [i.strip().split() for i in z]

#here each line is converted to a list, all nested within a list - all
elements of the list are strings, even integers are converted to strings

Question - how are nested lists indexed?

I then attempted to extract the comment, headers and clauses from the nested
list and assign them to a variable.

I tried:

for inner in z_list:
for lists in inner:
if lists[0] == 'c':
comment = lists[:]
elif lists[0] == 'p':
header = lists[:]
else:
clause = lists[:]
print comment, header, clause   

This does not work for some reasons which I understand. I have messed up the
indexing and my assignment of variables is wrong.

The aim was to extract the headers and comments and then be left with a
nested list of clauses.

Then I intended to converted the strings within the clauses nested list back
to integers and via indexing, check that all conditions are met. This would
have involved also converting the numerical strings within the header to
integers but the actual strings are proving a difficult problem to ignore.

Any suggestions?

If my mistakes are irritatingly stupid, please feel free to advise that I
r.t.f.m (read the f**king manual). However, thus far the manual has helped
me little.

Thanking you,
Seafoid.


-- 
View this message in context: 
http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-tp26837682p26837682.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing file format to ensure file meets criteria

2009-12-17 Thread seafoid

MRAB-2

Thank you for that!

Funny how something so simple clarifies a whole lot!

I will crack on now!

Once again,
Cheers and Thanks!

-- 
View this message in context: 
http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-tp26837682p26838085.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing file format to ensure file meets criteria

2009-12-17 Thread seafoid

Hi John,

I considered that, but in an attempt to really figure out this supposedly
simple language, I figure that I should try and solve this.

I will check out the modules for future reference.

Thanks,
Seafoid :-)

-- 
View this message in context: 
http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-tp26837682p26838132.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Line indexing in Python

2009-12-18 Thread seafoid

Hi Guys,

When python reads in a file, can lines be referred to via an index?

Example:

for line in file:
 if line[0] == '0':
 a.write(line)

This works, however, I am unsure if line[0] refers only to the first line or
the first character in all lines.

Is there an easy way to refer to a line with the first character being a
single letter that you know?

Thanks in advance,
Seafoid.
-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26845253.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Line indexing in Python

2009-12-18 Thread seafoid

Thanks for that Richard and Steve.

I have another question.

fname = raw_input('Please enter the name of the file: ')

# create file objects

blah = open(fname, 'r')
a = open('rubbish', 'w')

for line in blah:
if line.startswith("0"):
a.write(line)
elif line.endswith("0"):
lists_a = line.strip().split() 
print lists_a
elif line.startswith("0"):
lists_b = line.strip().split()
print lists_b

Essentially, I wish to take input from a file and based on the location of
zero, assign lines to lists.

Any suggestions?

Seafoid.

-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26845949.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Line indexing in Python

2009-12-18 Thread seafoid

Thanks for that Richard and Steve!

Below is my full code so far:

for line in file:
if line.startswith("1"):
a.write(line)
elif line.endswith("0"):
lists_a = line.strip().split()
print lists_a
elif line.startswith("2"):
lists_b = line.strip().split()
print list_a

Essentially, I want to read in a file and depending on location of 0, 1, 2,
write to another file or create lists.

The above passes without error warning but does nothing (semantic error?).

Any Suggestions?

Thanks in advance,
Seafoid.
-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26846049.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Line indexing in Python

2009-12-18 Thread seafoid

Thanks for that Lie.

I had to have a think about what you meant when you referred to control
going to a.write(line).

Have you any suggestions how I may render this code undead or should I scrap
it and create something new?

My confusion and ineptitude is perhaps explained by my being a biologist :-(

Thanks,
Seafoid.
-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26846854.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Line indexing in Python

2009-12-18 Thread seafoid

Hi Guys,

It has been point out that it is difficult for anyone to provide suggestions
if I do not outline more clearly my input file and an example of what I wish
to do with it (Thanks Rory!).

I mentioned it in this thread (Is creating different threads bad etiquette?
If so, lesson learned!):

http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-to26837682.html

Hope you guys may have some suggestions as I am stumped!

Thanks,
Seafoid :-)

seafoid wrote:
> 
> Hi Guys,
> 
> When python reads in a file, can lines be referred to via an index?
> 
> Example:
> 
> for line in file:
>  if line[0] == '0':
>  a.write(line)
> 
> This works, however, I am unsure if line[0] refers only to the first line
> or the first character in all lines.
> 
> Is there an easy way to refer to a line with the first character being a
> single letter that you know?
> 
> Thanks in advance,
> Seafoid.
> 

-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26847598.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Creating Classes

2009-12-18 Thread seafoid

Hey Guys,

I have started to read over classes as a brief respite from my parsing
problem.

When a class is defined, how does the class access the data upon which the
class should act?

Example:

class Seq:

def __init__(self, data, alphabet = Alphabet.generic_alphabet):
self.data = data 
self.alphabet = alphabet

def tostring(self):   
return self.data  

def tomutable(self):
return MutableSeq(self.data, self.alphabet)

def count(self, item):
return len([x for x in self.data if x == item])

I know what it should do, but have no idea how to feed it the data.

Methinks I need to invest in actual computing books as learning from
biologists is hazy!

Kind regards,
Seafoid.
  
-- 
View this message in context: 
http://old.nabble.com/Creating-Classes-tp26848375p26848375.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating Classes

2009-12-18 Thread seafoid

Steve, that has indeed clarified matters!

Thanks!

-- 
View this message in context: 
http://old.nabble.com/Creating-Classes-tp26848375p26849864.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Line indexing in Python

2009-12-18 Thread seafoid

Hey folks,

Is it possible to assign a list within a nested list to a variable?

Example:

l = [['1', '2', '3'], ['4', '5', '6']]

for i in l:
if i[0][1] == '1':
m = i

Indeed, I generally do not understand how to assign variables within a loop!

Is there an easy way to 'flatten' a nested list and assign the lists to
variables?

Thanks,
Seafoid.
-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26849921.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Line indexing in Python

2009-12-18 Thread seafoid

Rory,

You are a gentleman!

Thank you very much for your suggestion!

Kind Regards,
Seafoid.


Rory Campbell-Lange wrote:
> 
> On 18/12/09, seafoid (fitzp...@tcd.ie) wrote:
>> http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-to26837682.html
> 
> Your specification is confusing. However I suggest you break it down
> the code so that the steps in your programme are logical. Good luck.
> 
> # example psuedocode
> headers = {}
> header_clauses = {}
> current_header = None
> 
> def header_parser (input):
> split input into parts
> make unique header desciptor
> check not in headers else abort with error (?)
> add descriptor to headers hash
> # eg descriptor 1 = [attrib1, attrib2, attrib3]
> return descriptor
> 
> def clause_parser (input, current_header):
> if current_header is None: abort
> split clause into parts
> store in array in header_clauses [current_header]
> # this will make a data structure like this:
> # header_clauses = {
> #   descriptor1 = {[ clause parts ], [ clause parts ], ... }
> #   descriptor2 = {[ clause parts ], [ clause parts ], ... }
> 
> def comment_parser (input)
> pass
> 
> # now run over the file
> for l in lines:
> if l[0] == 'c':
> comment_parser(l)
> elif l[0] == 'p':
> current_header = header_parser(l)
> else:
> clause_parser(l, current_header)
> 
> # now that we have stored everything, check the data
> for h in headers:
> attrib1, attrib2, attrib3  = headers[h]
> for c in header_clauses:
> iterate over the arrays of clause parts either adding them
> up or comparing them to the header attributes
> 
> -- 
> Rory Campbell-Lange
> Director
> r...@campbell-lange.net
> 
> Campbell-Lange Workshop
> www.campbell-lange.net
> 0207 6311 555
> 3 Tottenham Street London W1T 2AF
> Registered in England No. 04551928
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Line-indexing-in-Python-tp26845253p26849944.html
Sent from the Python - python-list mailing list archive at Nabble.com.

-- 
http://mail.python.org/mailman/listinfo/python-list