Python Forum
name these two functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name these two functions
#1
i wrote a function that converts an int to a string of digits. it has options to set the base, set the minimum width, set the digits to use (implies the maximum base by its length), and to set an alternate string to return if the converted number needs more digits than the specified width (else it returns a longer string with the converted digits).

i also wrote a function to convert the digits to an int. this function has a base= option.

i would like to name these functions with the same length and a pattern that has a difference describing the difference between them. the names i initially gave them are "tilbase" and "frabase", where "til" == "to" and "fra" == "from".
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 suggest dumps() and loads() because you are serializing integers.
Reply
#3
i also have more for a couple of specific bases, 57 and 64, with special character for certain things. the base 64 pair matches the coding youtube uses for their video IDs (a 64 bit number). the base 57 is enough to encode a 64 bit int (base 56 cannot) and does not include special characters or "IOiol" that could look like 0 or 1. i am guessing i could call them "dumps57", "loads57", "dump64", an "loads64"?

i just renamed all 6.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
now that i have played around with them doing testing on them, i'm getting the feeling the names should have some reference to ints since ints are the other side of their conversion and because the "string" side is more than just strings.

since you helped and people here like to see code (and maybe my ugly code) i'll post the current iteration.


dumps.py
def dumps(*args,**opts):
    r"""Dump one or more ints to strings in a given base.""" \
    r""" More keyword arguments are minwidth=, longstr=."""
    base85 = '0123456789abcdefghijklmnopqrstuvwxyz' \
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#'
    d = opts.pop('digits',base85)
    b = opts.pop('base',len(d))
    if b<2 or b>len(d):
        return False
    w = opts.pop('minwidth',1)
    q = opts.pop('longstr',None)
    if opts:
        m = '' if len(opts)==1 else 's'
        m = ' invalid keyword argument'+m+' for this function: '
        m = 'dumps: '+str(len(opts))+m
        error_message = m+', '.join(sorted([x for x in opts]))
        raise TypeError(error_message)
    if q:
        while len(q)<w:
            q=q+q
    z = []
    for n in args:
        try:
            r = type(d)('')
        except:
            r = type(d)(b'')
        while n or len(r)<w:
            m = n%b
            r = d[m:m+1]+r
            n //= b
        l = len(r)
        if l>w:
            r = q[:w] if q else d[0:1]*(w-l)+r
        z.append(r)
    return z[0] if len(z)==1 else z
loads.py
def loads(*args,**opts):
    r"""Load one or more ints from strings in a given base."""
    base85 = '0123456789abcdefghijklmnopqrstuvwxyz' \
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#'
    d = opts.pop('digits',base85)
    b = opts.pop('base',len(d))
    if b<2 or b>len(d):
        return -1
    if opts:
        m = '' if len(opts)==1 else 's'
        m = ' invalid keyword argument'+m+' for this function: '
        m = 'loads: '+str(len(opts))+m
        error_message = m+', '.join(sorted([x for x in opts]))
        raise TypeError(error_message)
    t,r,n = {},[],0
    for x in d:
        t[x],n = n,n+1
    from collections import abc
    for a in args:
        if not isinstance(a,abc.Sequence):
            return -2
        i = 0
        for c in a[:-1]:
            if c not in t:
                return -3
            i += t[c]
            i *= b
            if a[-1] not in t:
                return -4
        r.append(i+t[a[-1]])
    return r[0] if len(r)==1 else r
the default base is 85. unless explicitly set, it will be the length of the sequence of digits. you can use any slice-able sequence type, including bytes, lists, and tuples. with lists and tuples you can make anything (not just characters) be your "digits", as long as they are hash-able objects. just set digits= to the desired sequence type containing all you "digits" in proper order starting the digit object for 0. there are still a few subtle bugs i'm still chasing down.
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