#!/usr/bin/python

# farback.py
#       --copyright--                   Copyright 2019 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       July 8, 2019            bar
#       --eodstamps--
##      \file
#       \namespace              tzpython.farback
#
#       TODO:
#           find the formula
#           make the loop count bigger or smaller as a function of how close the oldest count in avgs is to the loop counter
#           use median rather than average for the printout
#
#       20 thangs:
#           0.2544              1024
#           0.2208               512
#           0.1444               128
#           0.1004                64
#           0.0456                32
#           From https://mycurvefit.com/
#               Trying to get 0.1878 for 256
#               y = 507885100 + (51.39544 - 507885100)/(1 + (x/3.388145)^5.086578)
#               y = 382028.8 + (51.43029 - 382028.8)/(1 + (x/3.896344)^5.091247)^2744.884
#               y = 365946.3*x^4.305682
#               y = 170.5879 - 6471.473*x + 99449.16*x^2 - 593857.8*x^3 + 1394526*x^4
#               y = 24.36729 + 4.634505*e^(+21.11914*x)
#               y = 17921240000000*e^(-(x - 2.708005)^2/(2*0.3571911^2))
#
#
#
"""
farback.py

  Test farback logic.

"""

import  os
import  random
import  re
import  sys

import  tzlib
import  tz_os_priority


def oops(err_msg) :
    sys.stderr.write(err_msg + "\n")
    sys.exit(1)


if  __name__ == '__main__' :
    import  TZCommandLineAtFile
    import  TZKeyReady

    program_name        = os.path.basename(sys.argv[0] or __file__ or __doc__.split()[0])

    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)

    if  tzlib.array_find([ a.lower() for a in sys.argv ], [ "--help", "/help", "-h", "-?", "/?", "-?", "/h", ] ) >= 0 :
        ds  = re.sub(r'^(\s*)(\S+)', r'\1' + program_name, __doc__)
        print ds
        sys.exit(0)                         # well, there goes my 254 :)

    tz_os_priority.set_proc_to_idle_priority(0)                         # bring our CPU priority down to low

    how_many    = 10
    cut         = .1
    nu          = True
    while True :
        k   = TZKeyReady.get_key()
        if  k is not None :
            if  k.lower() in [ '\xff', '\x1b', '\x03', 'q', 'x', ] :
                break

            nu      = True
            if      False :
                nu  = False
            elif    k.lower() in [ 'j', ] :
                cut = max(0.001, cut * 0.98)
            elif    k.lower() in [ 'k', ] :
                cut = min(0.999, cut * 1.02)
            elif    k.lower()  in [ 's', ] :
                how_many    = max(2, how_many - 1)
            elif    k.lower()  in [ 'd', ] :
                how_many   += 1
            else    :
                nu  = False
            pass

        if  nu  :
            print
            tots    = [ 0 ] * how_many
            trycnt  = 0
        nu      = False



        thangs  = []
        for i in xrange(1, 100000) :
            thangs.insert(0, i)
            if  len(thangs) > how_many :
                j   = 1
                while j < how_many :
                    if  random.random() < cut :
                        break
                    j  += 1
                del(thangs[j])
            pass
        trycnt += 1
        avgs    = []
        for ii, t in enumerate(thangs) :
            tots[ii]   += t
            avgs.append(int(round(tots[ii] / float(trycnt))))

        print "%5u %6.4f" % ( len(avgs), cut, ), " ".join([ "%8d" % (avgs[0] - t) for t in reversed(avgs) ]), '\r',

    print


# eof
