Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Suggestions for improving code

Reply
Thread Tools

Suggestions for improving code

 
 
Aditya Mahajan
Guest
Posts: n/a
 
      03-11-2006
I am trying to convert a plain text file into a tex file with
somemarkup. I have the following piece of code that does most of the
work. It works fine but does not "look good". Can someone suggest how
can I improve this code.

---------------[snip]----------------------
file = File.new(filename, 'r')
texfile = File.open(basename + ".tex", 'w')

CHAPTER = Regexp.new("CHAPTER")
SPACES = Regexp.new("\s\s\s\s")
BLANK = Regexp.new(/^\s*$/)

chapter = true
verse = false
prev_line = ""

file.each_line do |line|
if chapter && !BLANK.match(line)
chapter = false
texfile.puts "\\chapter{#{line.chomp}}"
elsif CHAPTER.match(line)
chapter = true
elsif verse && !SPACES.match(line)
texfile.puts '\stoplines\stopnarrower'
texfile.puts line.chomp
verse = false
elsif !verse && BLANK.match(prev_line) && SPACES.match(line)
texfile.puts '\startnarrower\startlines'
texfile.puts line.chomp
verse = true
else
texfile.puts line.chomp
end
prev_line = line
end
------------------[snip]--------------------

Thanks,
Aditya

--
Aditya Mahajan, EECS Systems, University of Michigan
http://www.eecs.umich.edu/~adityam || Ph: 7342624008


 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      03-12-2006
Aditya Mahajan wrote:
> I am trying to convert a plain text file into a tex file with
> somemarkup. I have the following piece of code that does most of the
> work. It works fine but does not "look good". Can someone suggest how
> can I improve this code.
>
> ---------------[snip]----------------------
> file = File.new(filename, 'r')
> texfile = File.open(basename + ".tex", 'w')
>
> CHAPTER = Regexp.new("CHAPTER")
> SPACES = Regexp.new("\s\s\s\s")
> BLANK = Regexp.new(/^\s*$/)
>
> chapter = true
> verse = false
> prev_line = ""
>
> file.each_line do |line|
> if chapter && !BLANK.match(line)
> chapter = false
> texfile.puts "\\chapter{#{line.chomp}}"
> elsif CHAPTER.match(line)
> chapter = true
> elsif verse && !SPACES.match(line)
> texfile.puts '\stoplines\stopnarrower'
> texfile.puts line.chomp
> verse = false
> elsif !verse && BLANK.match(prev_line) && SPACES.match(line)
> texfile.puts '\startnarrower\startlines'
> texfile.puts line.chomp
> verse = true
> else
> texfile.puts line.chomp
> end
> prev_line = line
> end
> ------------------[snip]--------------------
>
> Thanks,
> Aditya
>
> --
> Aditya Mahajan, EECS Systems, University of Michigan
> http://www.eecs.umich.edu/~adityam || Ph: 7342624008



ruby -p01e'gsub(/(\n\s*\n)((?:^\s{4}.*\n)+)/,
"\\1\\startnarrower\\startlines\n\\2\\stoplines\\s topnarrower\n");
gsub(/(CHAPTER\s+)(.*)\n/,"\\chapter{\\2}\n")' in >out

 
Reply With Quote
 
 
 
 
Aditya Mahajan
Guest
Posts: n/a
 
      03-14-2006
<--- On Mar 12, William James wrote --->

> Aditya Mahajan wrote:
>> I am trying to convert a plain text file into a tex file with
>> somemarkup. I have the following piece of code that does most of the
>> work. It works fine but does not "look good". Can someone suggest how
>> can I improve this code.
>>
>> ---------------[snip]----------------------
>> file = File.new(filename, 'r')
>> texfile = File.open(basename + ".tex", 'w')
>>
>> CHAPTER = Regexp.new("CHAPTER")
>> SPACES = Regexp.new("\s\s\s\s")
>> BLANK = Regexp.new(/^\s*$/)
>>
>> chapter = true
>> verse = false
>> prev_line = ""
>>
>> file.each_line do |line|
>> if chapter && !BLANK.match(line)
>> chapter = false
>> texfile.puts "\\chapter{#{line.chomp}}"
>> elsif CHAPTER.match(line)
>> chapter = true
>> elsif verse && !SPACES.match(line)
>> texfile.puts '\stoplines\stopnarrower'
>> texfile.puts line.chomp
>> verse = false
>> elsif !verse && BLANK.match(prev_line) && SPACES.match(line)
>> texfile.puts '\startnarrower\startlines'
>> texfile.puts line.chomp
>> verse = true
>> else
>> texfile.puts line.chomp
>> end
>> prev_line = line
>> end
>> ------------------[snip]--------------------
>>

> ruby -p01e'gsub(/(\n\s*\n)((?:^\s{4}.*\n)+)/,
> "\\1\\startnarrower\\startlines\n\\2\\stoplines\\s topnarrower\n");
> gsub(/(CHAPTER\s+)(.*)\n/,"\\chapter{\\2}\n")' in >out
>

Thank you for the regex. The chapter part of your regex is better than
what I was doing, it does not correctly identify narrower region.
I think that I can tweak it a little to make it work correctly. But
what I want to know is there a better way to do this in ruby.

I am not too comfortable with coding using a regex as it can be very
difficult to maintain. Each time I have to look into the expression
and try to understand it again.

Basically the logic of the program depends on the "state" which I am
keeping track of using flags. Your code gets rid of the flags using a
two pass algorithm. What are the pros and cons. In using a gsub, the
program needs to read the entire file before it can make any changes.
I thought that this would be memory inefficient but for a ~160 kb
file, it is almost instantaneous. Even for a 3MB file it takes less
than a second. At what file sizes should one read the file line by
line rather than entire file in a single shot?

Thanks


--
Aditya Mahajan, EECS Systems, University of Michigan
http://www.eecs.umich.edu/~adityam || Ph: 7342624008



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: suggestions for improving code fragment please Ian Kelly Python 0 02-28-2013 07:58 PM
Re: suggestions for improving code fragment please Joel Goldstick Python 0 02-28-2013 07:56 PM
Re: suggestions for improving code fragment please Rick Johnson Python 0 02-28-2013 07:55 PM
Personal archive tool, looking for suggestions on improving the code mo reina Python 5 07-27-2010 03:47 PM
Looking for suggestions on improving numpy code David Lees Python 2 02-25-2008 05:11 PM



Advertisments