Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
interleavefiles.py
#1
i wanted to come up with a bigger script that made use of the builtin zip() function.  this script is a command to interleave the lines of 2 or more files.

!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""Interleave 2 or more files, line by line.

file          interleavefiles.py
command       interleavefiles
purpose       interleave 2 or more files, line by line
author        Phil D. Howard
email         10054452614123394844460370234029112340408691

The intent is that this command works correctly under both Python 2 and
Python 3.  Please report failures or code improvement to the author.
"""

__license__ = """
Copyright (C) 2017, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""

from sys import argv, stderr, stdout, version_info

def main(args):
    """main"""
    errors=0
    openfiles=[]
    for filename in args[1:]:
        try:
            openfile=open(filename,'r')
            openfiles.append(openfile)
        except IOError:
            print('unable to open file',repr(filename),file=stderr)
            errors+=1
    for lines in zip(*openfiles):
        for line in lines:
            print(line.rstrip())
    for openfile in openfiles:
        openfile.close()
    return 0

if __name__ == '__main__':
    try:
        result=main(argv)
        stdout.flush()
    except KeyboardInterrupt:
        print('')
        reselt=99
    except IOError:
        result=98
    if result in (None,True,):
        exit(0)
    if result in (False,):
        exit(1)
    if isinstance(result,(str,bytes,bytearray) if version_info.major>2 else (str,unicode)):
        print(result,file=stderr)
        exit(1)
    try:
        exit(int(result))
    except ValueError:
        print(str(result),file=stderr)
    except TypeError:
        exit(0 if result == None else 255)
# EOF
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I just skimmed through your file and noticed some little things:

Likely typos - missing # on start, returning 0 instead of errors (why would be defined?) and "reselt" on line 64. Main block error handling is confusing to me (copy/paste from something bigger? I cant see most of checked result options ever happen, 98/99 when main returns nonnegative int?).

Do you realize that bool is subclass of int with "canonical" representation 0/1, so checks like 0 in (False,) or 1 in (None, True) evaluate as True?
Reply
#3
What is the purpose of importing __future__ in python3 code?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(Mar-24-2017, 10:11 AM)zivoni Wrote: I just skimmed through your file and noticed some little things:

Likely typos - missing # on start, returning 0 instead of errors (why would be defined?) and "reselt" on line 64. Main block error handling is confusing to me (copy/paste from something bigger? I cant see most of checked result options ever happen, 98/99 when main returns nonnegative int?).

Do you realize that bool is subclass of int with "canonical" representation 0/1, so checks like 0 in (False,) or 1 in (None, True) evaluate as True?

the missing # in line 1 is a highlighting goof when i was copying the file to paste it in.  "reselt" is a genuine typo (more likely a brain glitch) i had not noticed.  thanks for noticing and letting me know.  a couple other files had the same thing because of code copying.  that whole section of code was copied from another source (that is now fixed).  most of my code starts with a copied starter code that includes common section and almost all code is intended to be runnable in either python3 and python2.

i know about the bool subclass relation but learned about it after a lot of code was wriiten.  my guess is this got overlooked because of the "in" since i was skipping uses of "is" that compare as intended.  and then there are other issues with this code so it is getting another rewrite.

what i need is a scheme to identify code that is already copied into files and implement a tool to re-do the copy-insertions when a source of such copy-insertion code has been updated.

(Mar-24-2017, 09:55 PM)Ofnuts Wrote: What is the purpose of importing __future__ in python3 code?

it's code that can be used in either python3 or python2.  or at least that is the intention as stated in lines 12 and 13.  i don't try to make separate files for each version.  so it is good that this import has no effect in python3.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Mar-25-2017, 06:05 AM)Skaperen Wrote: what i need is a scheme to identify code that is already copied into files and implement a tool to re-do the copy-insertions when a source of such copy-insertion code has been updated.

For new ones you can use some preprocessor (either generic one as m4, or some python specific) to do it.
Reply
#6
i could generate the files from a definition of how to generate it.  then i am keeping two files.  by having the merge definitions in the comments with the definition of where the insertion stops, and leave these definitions in the product, then i only need to keep that one file.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020