c – Trigraph translator – Code Review Stack Exchange

Recently I thought about what a program would look like if all its characters that could become trigraphs became trigraphs. For example

int main(void)
{
    int array(10);
}

would become

int main(void)
??<
    int array??(10??);
??>

As a result I decided to make a program using ANSI C89 that takes a file in and converts each character to its corresponding trigraph if it has one and outputs the result to another file.

#include <errno.h>
#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 1024
#define WRITE_BUFFER(x, size)                                            
    do {                                                                 
        memcpy(buffer + BUFFER_SIZE + write_buffer_length, (x), (size)); 
        write_buffer_length += (size);                                   
    } while(0)                                                           

int main(int argc, char **argv)
{
    int j;

    /* ignore the first argument */
    --argc;
    ++argv;

    /* check for invalid arguments */
    if(argc % 2 != 0) {
        fprintf(stderr, "Error: invalid argumentsn");
        fprintf(stderr, "Usage: %s input output ...n", argv(-1));
        fprintf(stderr, "Example: %s main.c result.c n", argv(-1));
        return -1;
    }

    for(j = 0; j < argc; j += 2) {
        char buffer(BUFFER_SIZE + BUFFER_SIZE * 3);
        FILE *read_file, *write_file;
        size_t bytes_read = BUFFER_SIZE;

        if(strcmp(argv(j), argv(j + 1)) == 0) {
            printf("Warning: using the same file for input and output is not supportedn");
            continue;
        }

        /* open a file for reading */
        if((read_file = fopen(argv(j), "r")) == NULL) {
            fprintf(stderr, "Error: could not open %sn%s", argv(j), strerror(errno));
            return -2;
        }

        /* open a file for writing */
        if((write_file = fopen(argv(j + 1), "w")) == NULL) {
            fprintf(stderr, "Error: could not open %sn%s", argv(j + 1), strerror(errno));
            return -3;
        }

        /* read the file in BUFFER_SIZE chunks */
        while (bytes_read == BUFFER_SIZE) {
            size_t i, write_buffer_length;
            bytes_read = fread(buffer, 1, BUFFER_SIZE, read_file);

            /* process each character in the buffer
             * and if needed convert it to a trigraph
             */

            write_buffer_length = 0;
            for (i = 0; i < bytes_read; ++i) {
                char const ch = buffer(i);

                switch (ch) {
                    case '#':
                        WRITE_BUFFER("??=", 3);
                        break;
                    case '(':
                        WRITE_BUFFER("??(", 3);
                        break;
                    case ')':
                        WRITE_BUFFER("??)", 3);
                        break;
                    case '{':
                        WRITE_BUFFER("??<", 3);
                        break;
                    case '}':
                        WRITE_BUFFER("??>", 3);
                        break;
                    case '\':
                        WRITE_BUFFER("??/", 3);
                        break;
                    case '^':
                        WRITE_BUFFER("??'", 3);
                        break;
                    case '~':
                        WRITE_BUFFER("??-", 3);
                        break;
                    case '|':
                        WRITE_BUFFER("??!", 3);
                        break;
                    default:
                        WRITE_BUFFER(&ch, 1);
                        break;
                }
            }

            fwrite(buffer + BUFFER_SIZE, 1, write_buffer_length, write_file);
        }

        fclose(read_file);
        fclose(write_file);
    }

    return 0;
}

PDF parser in Python – Code Review Stack Exchange

Autodidact and inspired by David Beazley code (to improve my skills in Python) i would like to get your feed back on this Parser code.

NB: The lazy property let the code be computed only one time if used (https://github.com/dabeaz/python-cookbook/blob/master/src/8/lazily_computed_attributes/example1.py)



class lazyproperty:
    def __init__(self, func):
        self.func = func
    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            value = self.func(instance)
            setattr(instance, self.func.__name__, value)
            return value


class PDFParser():
    """

    """
    def __init__(self,filepath,page_num=0):
        self.filepath = filepath
        try:
            self._doc = fitz.open(filepath)
            self.page_num = page_num
            self._page = self._doc(page_num)
        except Exception as e: 
            print("Lecture PDF impossible. {}".format(e))

    @lazyproperty
    def text(self):
        return self._page.getText()

    @lazyproperty
    def _pixs(self):
        imgs = self._doc.getPageImageList(self.page_num)
        pixs =()
        for img in imgs:
            xref = img(0)
            pix = fitz.Pixmap(self._doc, xref)
            pixs.append(pix)
        return pixs

    @lazyproperty
    def _pixpage(self):
        pix = self._page.getPixmap(colorspace=fitz.csGRAY)
        return pix
    
    @property   
    def img(self):
        return self.imgs(0)


    @lazyproperty
    def imgs(self):
        pixs = self._pixs
        imgsarray = ()
        for pix in pixs:
            img = self.pix2np(pix)
            imgsarray.append(img)
        return imgsarray
        

    def write(self,outputdir,fullpage=False):
        filename = os.path.basename(self.filepath).split('.pdf')(0)
        def writePNG(pix,filepath):
            # This is GRAY or RGB
            try:       
                pix.writePNG(filepath)
            # CMYK: convert to RGB first
            except:               
                pix = fitz.Pixmap(fitz.csRGB, pix)
                pix.writePNG(filepath)
                pix = None
        if fullpage:
            filepath = os.path.join(outputdir,'{}_p{}.png'.format(filename,self.page_num))
            pix = self._pixpage
            writePNG(pix,filepath)
            return
        pixs = self._pixs
        for i,pix in enumerate(pixs):
            filepath = os.path.join(outputdir,'{}_p{}_i{}.png'.format(filename,self.page_num,i))
            writePNG(pix,filepath)
        return

    @staticmethod
    def pix2np(pix):
        """
        Convert pixmap to image np.ndarray
        https://stackoverflow.com/questions/53059007/python-opencv
        param pix: pixmap
        """
        import numpy as np
        #https://stackoverflow.com/questions/22236749/numpy-what-is-the-difference-between-frombuffer-and-fromstring
        im = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.h, pix.w, pix.n)
        try:
            im = np.ascontiguousarray(im(..., (2, 1, 0)))  # rgb to bgr
        except IndexError:
            #Trick to convert Gray rto BGR, (im.reshape)
            logger.warning("Shape of image array is {}".format(im.shape)) 
            im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)
            im = np.ascontiguousarray(im(..., (2, 1, 0)))
        return im

NonlinearModelFit operations – Mathematica Stack Exchange

I have attempted to exercise a non-linear model fit to my data, dadtAbs and got the following puzzle

nlm = NonlinearModelFit(
Transpose({dadtAbs, Table(t, {t, 1, tmax})}), (1 – aaa)bbb
NSPI(bbb, ddd, population,
t)(1 – ddd)(population – NSPI(bbb, ddd, population, t)), {aaa,
bbb, ddd}, t)

where dadtAbs is a list, population is a known constant, aaa,bbb,ddd are the desired answers, and t is the variable.

When I queried nlm, I got Out(15)=(0.). A copy of this output follows

!(
TagBox(
RowBox({“FittedModel”, “(“,
TagBox(
PanelBox(
TagBox(“0.", Short(#, 2)& ), FrameMargins->5), Editable -> False), ")"}), InterpretTemplate( FittedModel({ "Nonlinear", {$CellContextaaa -> 1., $CellContext`bbb -> 1.,
$
CellContextddd -> 1.}, {{$CellContextt},
705749 (1 – $CellContext`aaa) $CellContextbbb ( 1 - $CellContextddd) (
1 + 705748 E^((-705749) $CellContext`bbb (
1 – $
CellContextddd) $CellContextt))^(-1) (
705749 – 705749/(
1 + 705748 E^((-705749) $CellContext`bbb (
1 – $
CellContextddd) $CellContextt)))}}, {
1}, {{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}, {0,
8}, {0, 9}, {0, 10}, {0, 11}, {0, 12}, {0, 13}, {0, 14}, {0,
15}, {0, 16}, {0, 17}, {0, 18}, {0, 19}, {0, 20}, {0, 21}, {0,
22}, {0, 23}, {0, 24}, {0, 25}, {0, 26}, {0, 27}, {0, 28}, {0,
29}, {0, 30}, {0, 31}, {0, 32}, {0, 33}, {0, 34}, {0, 35}, {0,
36}, {0, 37}, {0, 38}, {0, 39}, {
Rational(7023641625, 262144) E^4, 40}, {
Rational(4484685606948421875, 34359738368) E^4, 41}, {
Rational(4483221011656261875, 34359738368) E^4, 42}, {
Rational(3667307533090826625, 68719476736) E^4, 43}, {
Rational(12983095125, 524288) E^4, 44}, {
Rational(4483873802700767475, 34359738368) E^4, 45}, {
Rational(406734848353258425, 17179869184) E^4, 46}, {
Rational(9376363711462722975, 68719476736) E^4, 47}, {
Rational(2851969929408975375, 34359738368) E^4, 48}, {
Rational(1220489427110028075, 68719476736) E^4, 49}, {
Rational(10190178800256279825, 68719476736) E^4, 50}, {
Rational(406748597615184825, 34359738368) E^4, 51}, {
Rational(407555420246199225, 17179869184) E^4, 52}, {
Rational(2855111877059842575, 68719476736) E^4, 53}, {
Rational(2034116859682093725, 68719476736) E^4, 54}, {
Rational(8555428712230663725, 68719476736) E^4, 55}, {
Rational(14266725159603264075, 68719476736) E^4, 56}, {
Rational(1219274108663128875, 68719476736) E^4, 57}, {
Rational(1223398659874956075, 8589934592) E^4, 58}, {
Rational(15073774173479137725, 68719476736) E^4, 59}, {
Rational(16715087365155365025, 68719476736) E^4, 60}, {
Rational(18341693524877365125, 68719476736) E^4, 61}, {
Rational(2046196393329556125, 68719476736) E^4, 62}, {
Rational(7728814462868070075, 68719476736) E^4, 63}, {
Rational(46883706400045237275, 68719476736) E^4, 64}, {
Rational(29755934616004466625, 34359738368) E^4, 65}, {
Rational(10999039001279724675, 34359738368) E^4, 66}, {
Rational(7748557849326591675, 68719476736) E^4, 67}, {
Rational(8566842054400980525, 68719476736) E^4, 68}, {
Rational(6940947552871876425, 68719476736) E^4, 69}, {
Rational(9362567611656719775, 68719476736) E^4, 70}, {
Rational(3666745680354929025, 17179869184) E^4, 71}, {
Rational(2852730870537884175, 34359738368) E^4, 72}, {
Rational(11015273112630099075, 68719476736) E^4, 73}, {
Rational(407567947400861625, 1073741824) E^4, 74}, {
Rational(25682732293051261575, 68719476736) E^4, 75}, {
Rational(2839091615712514575, 68719476736) E^4, 76}, {
Rational(15078277279016776125, 68719476736) E^4, 77}, {
Rational(25688840953426813575, 68719476736) E^4, 78}, {
Rational(19144471441385481975, 68719476736) E^4, 79}, {
Rational(1221280883834200875, 17179869184) E^4, 80}, {
Rational(406626909610167225, 17179869184) E^4, 81}, {
Rational(11820631132181697525, 68719476736) E^4, 82}, {
Rational(37108068764041505475, 68719476736) E^4, 83}, {
Rational(20770118043735987675, 68719476736) E^4, 84}, {
Rational(2029885783856020125, 34359738368) E^4, 85}, {
Rational(42782908914056793825, 68719476736) E^4, 86}, {
Rational(24058317959185363875, 34359738368) E^4, 87}, {
Rational(19973374395461598825, 17179869184) E^4, 88}, {
Rational(2037155300661037725, 4294967296) E^4, 89}, {
Rational(26502323934444370425, 68719476736) E^4, 90}, {
Rational(414560959269456825, 34359738368) E^4, 91}, {
Rational(5316132228825831525, 68719476736) E^4, 92}, {
Rational(10997220292703878275, 34359738368) E^4, 93}, {
Rational(5298258791689345125, 17179869184) E^4, 94}, {
Rational(8560691946974983725, 34359738368) E^4, 95}, {
Rational(13458632641728824025, 68719476736) E^4, 96}, {
Rational(21616217747472264525, 68719476736) E^4, 97}, {
Rational(8577268299801146925, 68719476736) E^4, 98}, {
Rational(1203255706190373675, 68719476736) E^4, 99}, {
Rational(21593315519992507725, 68719476736) E^4, 100}, {
Rational(6937879715884074825, 68719476736) E^4, 101}, {
Rational(3668212055791217025, 17179869184) E^4, 102}, {
Rational(18348208316833025925, 34359738368) E^4, 103}, {
Rational(13443083696125035225, 34359738368) E^4, 104}, {
Rational(6927997302308702025, 68719476736) E^4, 105}, {
Rational(19173650513263421175, 68719476736) E^4, 106}, {
Rational(6114206069652770775, 8589934592) E^4, 107}, {
Rational(13450426644804305625, 17179869184) E^4, 108}, {
Rational(408302391133776825, 2147483648) E^4, 109}, {
Rational(28928855148211306575, 68719476736) E^4, 110}, {
Rational(11824939996938638325, 68719476736) E^4, 111}, {
Rational(1225855350972146475, 34359738368) E^4, 112}, {
Rational(15091778633403822525, 68719476736) E^4, 113}, {
Rational(2849396523330408975, 34359738368) E^4, 114}, {
Rational(2040403768339348125, 8589934592) E^4, 115}, {
Rational(54210490564597962525, 68719476736) E^4, 116}, {
Rational(38717635827290315175, 68719476736) E^4, 117}, {
Rational(2855353568609948175, 17179869184) E^4, 118}, {
Rational(11024289044476755075, 68719476736) E^4, 119}, {
Rational(41161405722691657725, 68719476736) E^4, 120}, {
Rational(5297143750746855525, 34359738368) E^4, 121}, {
Rational(10194290888136779025, 68719476736) E^4, 122}, {
Rational(3675157618008074625, 68719476736) E^4, 123}, {
Rational(2859594699405365775, 68719476736) E^4, 124}, {
Rational(1227198531841778475, 34359738368) E^4, 125}, {
Rational(2852280049560015375, 17179869184) E^4, 126}, {
Rational(4484701181641900275, 34359738368) E^4, 127}, {
Rational(4490564579728146675, 68719476736) E^4, 128}, {
Rational(5293903415586740325, 68719476736) E^4, 129}, {
Rational(2040048685904788125, 34359738368) E^4, 130}, {
Rational(1220282031145509675, 68719476736) E^4, 131}, {
Rational(5300455668835047525, 34359738368) E^4, 132}, {
Rational(5305977746057549925, 68719476736) E^4, 133}, {
Rational(2034151361731885725, 34359738368) E^4, 134}, {
Rational(11003013751214931075, 68719476736) E^4, 135}, {
Rational(2852496702506242575, 68719476736) E^4, 136}, {
Rational(6932229710746257225, 68719476736) E^4, 137}, {
Rational(406568153241783225, 17179869184) E^4, 138}, {
Rational(406279366951095225, 34359738368) E^4, 139}, {
Rational(408748830727556025, 34359738368) E^4, 140}, {
Rational(408775566100253625, 34359738368) E^4, 141}, {
Rational(1225022276498776875, 68719476736) E^4, 142}, {
Rational(2041118921560160925, 68719476736) E^4, 143}, {
Rational(2035236398246394525, 68719476736) E^4, 144}, {
Rational(404726121642576825, 68719476736) E^4, 145}, {
Rational(1222567320026047275, 17179869184) E^4, 146}, {
Rational(3669726964714711425, 68719476736) E^4, 147}, {
Rational(1221547581009650475, 68719476736) E^4, 148}, {
Rational(5302600659598465125, 68719476736) E^4, 149}, {
Rational(2037987199345146525, 17179869184) E^4, 150}, {
Rational(13454004678530974425, 68719476736) E^4, 151}, {
Rational(9370971348570230175, 68719476736) E^4, 152}, {
Rational(408964364029316025, 34359738368) E^4, 153}, {
Rational(8556641875460666925, 68719476736) E^4, 154}, {
Rational(7747654962082815675, 68719476736) E^4, 155}, {
Rational(3669021962379684225, 17179869184) E^4, 156}, {
Rational(11001715367736147075, 68719476736) E^4, 157}, {
Rational(1222856530160575275, 34359738368) E^4, 158}, {
Rational(1223766327800767275, 17179869184) E^4, 159}, {
Rational(5296798225816705125, 68719476736) E^4, 160}, {
Rational(7750226026220953275, 68719476736) E^4, 161}, {
Rational(2037851709475117725, 8589934592) E^4, 162}, {
Rational(6113849133104623575, 34359738368) E^4, 163}, {
Rational(4491642020032069875, 68719476736) E^4, 164}, {
Rational(2037609503622445725, 8589934592) E^4, 165}, {
Rational(1223193133235954475, 17179869184) E^4, 166}, {
Rational(4479664340820345075, 68719476736) E^4, 167}, {
Rational(2036239375342074525, 34359738368) E^4, 168}, {
Rational(2854621528057090575, 34359738368) E^4, 169}, {
Rational(6115533540082773975, 17179869184) E^4, 170}, {
Rational(3666605793308273025, 17179869184) E^4, 171}, {
Rational(2039332784628628125, 34359738368) E^4, 172}, {
Rational(2043936279611898525, 68719476736) E^4, 173}, {
Rational(6108669353560764375, 68719476736) E^4, 174}, {
Rational(3668093732061194625, 68719476736) E^4, 175}, {
Rational(13454690402590212825, 68719476736) E^4, 176}, {
Rational(405723110115101625, 17179869184) E^4, 177}, {
Rational(4477995993923961075, 68719476736) E^4, 178}, {
Rational(2036720372840135325, 34359738368) E^4, 179}, {
Rational(2038346798445460125, 34359738368) E^4, 180}, {
Rational(6118430677854089175, 34359738368) E^4, 181}, {
Rational(9373407907201142175, 34359738368) E^4, 182}, {
Rational(2038534838216596125, 8589934592) E^4, 183}, {
Rational(6111262996382805975, 34359738368) E^4, 184}, {
Rational(408034399783095225, 17179869184) E^4, 185}, {
Rational(2859900703974658575, 68719476736) E^4, 186}, {
Rational(3668399934501693825, 17179869184) E^4, 187}, {
Rational(9377158705947561375, 34359738368) E^4, 188}, {
Rational(14261886170944166475, 68719476736) E^4, 189}, {
Rational(5300223577986586725, 34359738368) E^4, 190}, {
Rational(403542810024528825, 34359738368) E^4, 191}, {
Rational(11820591339631809525, 68719476736) E^4, 192}, {
Rational(21607230141864226125, 68719476736) E^4, 193}, {
Rational(2036277224480864925, 17179869184) E^4, 194}, {
Rational(408103301927914425, 8589934592) E^4, 195}, {
Rational(410376177873898425, 68719476736) E^4, 196}, {
Rational(9381629361536322975, 68719476736) E^4, 197}, {
Rational(5295846284164225125, 34359738368) E^4, 198}, {
Rational(2035457679567354525, 68719476736) E^4, 199}, {
Rational(2036382808277908125, 68719476736) E^4, 200}, {
Rational(3670432162830756225, 68719476736) E^4, 201}, {
Rational(2856745421848220175, 68719476736) E^4, 202}, {
Rational(407695673540381625, 17179869184) E^4, 203}, {
Rational(18355830157313691525, 68719476736) E^4, 204}, {
Rational(22417416580222403775, 34359738368) E^4, 205}, {
Rational(21600251514254011725, 68719476736) E^4, 206}, {
Rational(6928371158122334025, 68719476736) E^4, 207}, {
Rational(2038346873343892125, 8589934592) E^4, 208}, {
Rational(415971895699229625, 68719476736) E^4, 209}, {
Rational(1222572431234392875, 8589934592) E^4, 210}, {
Rational(11825073182840001525, 68719476736) E^4, 211}, {
Rational(6112031606182178775, 34359738368) E^4, 212}, {
Rational(2854561695705960975, 68719476736) E^4, 213}, {
Rational(405754530762116025, 68719476736) E^4, 214}, {
Rational(4486106680793874675, 68719476736) E^4, 215}, {
Rational(2042791715449338525, 68719476736) E^4, 216}, {
Rational(4481930390674194675, 68719476736) E^4, 217}, {
Rational(4484910808070111475, 34359738368) E^4, 218}, {
Rational(68918552325, 524288) E^4, 219}, {
Rational(2853748222674792975, 8589934592) E^4, 220}, {
Rational(33023055798617798025, 68719476736) E^4, 221}, {
Rational(40352106007450536075, 68719476736) E^4, 222}, {
Rational(28124476985015855325, 68719476736) E^4, 223}, {
Rational(14261366911378905675, 68719476736) E^4, 224}, {
Rational(3668855277951396225, 68719476736) E^4, 225}, {
Rational(3672729339776215425, 68719476736) E^4, 226}, {
Rational(6111506729118396375, 68719476736) E^4, 227}, {
Rational(1223414042271064875, 17179869184) E^4, 228}, {
Rational(1221668450589631275, 68719476736) E^4, 229}, {
Rational(6116613426836565975, 34359738368) E^4, 230}, {
Rational(2036958675961952925, 17179869184) E^4, 231}, {
Rational(2855602897245442575, 34359738368) E^4, 232}, {
Rational(11001693868518636675, 68719476736) E^4, 233}, {
Rational(407243830227946425, 68719476736) E^4, 234}, {
Rational(2038557913552583325, 68719476736) E^4, 235}, {
Rational(6930496257720158025, 68719476736) E^4, 236}, {
Rational(8566039640210388525, 68719476736) E^4, 237}, {
Rational(4481625319722565875, 34359738368) E^4, 238}, {
Rational(2039158007686036125, 34359738368) E^4, 239}, {
Rational(1220014653729727275, 34359738368) E^4, 240}, {
Rational(13450630313033220825, 68719476736) E^4, 241}, {
Rational(6113127144734191575, 68719476736) E^4, 242}, {
Rational(407819599440874425, 17179869184) E^4, 243}, {
Rational(3673138399478589825, 34359738368) E^4, 244}, {
Rational(31390985460832028325, 68719476736) E^4, 245}, {
Rational(42805745961089011425, 68719476736) E^4, 246}, {
Rational(46054144858134342825, 68719476736) E^4, 247}, {
Rational(14262076414354905675, 68719476736) E^4, 248}, {
Rational(1225290201544024875, 68719476736) E^4, 249}, {
Rational(3669829538175396225, 68719476736) E^4, 250}, {
Rational(2854605751312028175, 17179869184) E^4, 251}, {
Rational(406611266288823225, 8589934592) E^4, 252}, {
Rational(3667692616005233025, 34359738368) E^4, 253}, {
Rational(6117690771456290775, 34359738368) E^4, 254}, {
Rational(22414110102198928575, 68719476736) E^4, 255}, {
Rational(2036073772005882525, 34359738368) E^4, 256}, {
Rational(17529368527963437075, 68719476736) E^4, 257}, {
Rational(4492914975667372275, 68719476736) E^4, 258}, {
Rational(10184258825404081425, 68719476736) E^4, 259}, {
Rational(1221711526129394475, 68719476736) E^4, 260}, {
Rational(2855938091533570575, 34359738368) E^4, 261}},
Function(Null,
InternalLocalizedBlock({$CellContextaaa, $CellContext`bbb,
$
CellContextddd, $CellContextt}, #), {HoldAll}))& ),
Editable->False,
SelectWithContents->True,
Selectable->True))

❕NEWS – Tron foundation opens a manual withdraw for Tron holders in Okex Exchange. | Proxies123.com