pyutils module - Skaperen - Oct-04-2016
i'll upload/embed it here and see if i live through it.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# Copyright (C) 2016 - 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 using the short first name, middle initial,
# and last name, separated by periods, with gmail dot com as the host part
# of an email address.
#-----------------------------------------------------------------------------
# The intent is that this module works corrently under both Python 2 and
# Python 3. Please report failures to the author.
#-----------------------------------------------------------------------------
# file pyutils.py
# purpose collection of my useful python stuff
# email 10054452614123394844460370234029112340408691
#-----------------------------------------------------------------------------
from __future__ import division, print_function
from base64 import b16decode, b16encode
from base64 import b32decode, b32encode
from base64 import b64decode, b64encode
from decimal import Decimal, InvalidOperation
from hashlib import sha256, sha384, sha512
from os import chdir as cd, environ as env
from random import randint
from signal import SIGKILL, SIGTERM
from subprocess import call, PIPE, Popen
from sys import argv, stderr, stdout, version_info
from time import sleep, time
if version_info[:3] >= (3,4,0):
from base64 import a85decode, a85encode
from base64 import b64decode, b85encode
__y__=('ano','bai','da','ie','ja','jah','jes','kylla','oui','si','sim','taip','tak','yes')
__n__=('dim','ei','ez','ingen','inno','nao','ne','nee','nei','nein','nej','neniu','nie','no','non','nu')
__t__=('cierto','egia','ekte','ikke','istina','pravi','rigtigt','sann','satt','totta','true','vera','veraj','verdade','veritable','vero','vrai','waar','wahr','wier')
__f__=('falsa','falsch','false','falsk','falso','faux','lazan','rangar','sbagliate','vaara','vals')
__yt__=set(__y__+__t__)
__nf__=set(__n__+__f__)
#-----------------------------------------------------------------------------
# function is(various)
# purpose various function to test data types
# note isint() returns True also for type bool
#-----------------------------------------------------------------------------
def isstr(x): return True if 'split' in dir(x) else False
def isdict(x): return True if 'items' in dir(x) else False
def isanylist(x): return False if 'split' in dir(x) else True if 'index' in dir(x) else False
def isonlylist(x): return False if 'split' in dir(x) else True if 'sort' in dir(x) else False
def istuple(x): return False if 'sort' in dir(x) else True if 'index' in dir(x) else False
def isint(x): return True if 'bit_length' in dir(x) else False
def isfloat(x): return True if 'is_integer' in dir(x) else False
def isdecimal(x): return True if 'radix' in dir(x) else False
def isdatetime(x): return True if 'time' in dir(x) else False
def isdate(x): return True if 'isocalendar' in dir(x) else False
def isnum(x): return True if 'bit_length' in dir(x) else True if 'is_integer' in dir(x) else True if 'radix' in dir(x) else False
#-----------------------------------------------------------------------------
# function fint
# purpose convert x to an int only if x is a float with an int value
#-----------------------------------------------------------------------------
def fint(x):
if 'is_integer' in dir(x):
if x.is_integer():
return int(x)
return x
#-----------------------------------------------------------------------------
# function tprint
# purpose a print function that avoids exceptions such as bad pipelines
#-----------------------------------------------------------------------------
def tprint( *msg, **opt ):
try:
return print( *msg, **opt )
except IOError:
return None
#-----------------------------------------------------------------------------
# function eprint
# purpose like tprint() but defaults to file=sys.stderr
#-----------------------------------------------------------------------------
def eprint( *msg, **opt ):
if 'file' in opt:
f = opt[ 'file' ]
else:
f = stderr
return tprint( *msg, file=f, **opt )
#-----------------------------------------------------------------------------
# function b(various)
# purpose various functions to convert binary encoding
#-----------------------------------------------------------------------------
def b16to32(x): return b32encode(b16decode(x.upper()))
def b16to64(x,*a): return b64encode(b16decode(x.upper()),*a)
def b32to16(x): return b16encode(b32decode(x)).lower()
def b32to64(x,*a): return b64encode(b32decode(x),*a)
def b64to16(x,*a): return b16encode(b64decode(x,*a)).lower()
def b64to32(x,*a): return b32encode(b64decode(x,*a))
if version_info[:3] >= (3,4,0):
def b16to85(x): return b85encode(b16decode(x.upper()))
def b32to85(x): return b85encode(b32decode(x))
def b64to85(x,*a): return b85encode(b64decode(x,*a))
def b85to16(x): return b16encode(b85decode(x)).lower()
def b85to32(x): return b32encode(b85decode(x))
def b85to64(x,*a): return b64encode(b85decode(x),*a)
#-----------------------------------------------------------------------------
# function mc_env
# pupose mixed case environment lookup (UPPER CASE PREVAILS)
#-----------------------------------------------------------------------------
def mc_env( name ):
if name.upper() in environ:
return env[ name.upper() ]
if name.lower() in environ:
return env[ name.lower() ]
if name in environ:
return env[ name ]
return None
#-----------------------------------------------------------------------------
# function valid_decimal
# purpose non-exception test if given value can be decimal.Decimal.
#-----------------------------------------------------------------------------
def valid_decimal( decarg ):
try:
result = Decimal( decarg )
except InvalidOperation:
result = None
return result
#-----------------------------------------------------------------------------
# function unmod
# purpose Return the remainder after subtracting the modulus.
# note If a list or tuple of numbers is given, return in kind
# arguments 1 (num) number
# 2 (num) modulus
# returns (num) number - (number % modulus)
# example unmod(456789,1000) -> 456000
# unmod(256,15) -> 240
# usage unmod(seconds,3600) -> start of this hour in seconds
# unmod(seconds,86400) -> start of this day in seconds
# unmod(minutes,1440) -> start of this day in minutes
#-----------------------------------------------------------------------------
def unmod(n,m):
if not isnum(m):
raise TypeError('Not a number passed to unmod() arg2: %s (modulus)' % repr(m))
if type(n) in (tuple,list):
ns = [unmod(x,m) for x in n]
if type(n) in (tuple,):
return tuple(ns)
return ns
if not isnum(n):
raise TypeError('Not a number passed to unmod() arg1: %s (value)' % repr(n))
if m == 0:
raise ValueError('Zero passed to unmod() arg2 (modulus)')
if m < 0:
raise ValueError('Negative value passed to unmod() arg2: %s (modulus)' % repr(m))
return n - (n % m)
#-----------------------------------------------------------------------------
# function find_unicode
# purpose In a unicode string, find the position of the first character
# that requires the unicode type and cannot be represented in
# mere string type.
# returns (int) position or the value specified by notfound=
#-----------------------------------------------------------------------------
def find_unicode(u,notfound=-1):
l = len(u)
for n in xrange(l):
if ord(u[n]) > 127:
return n
return notfound
#-----------------------------------------------------------------------------
# function scale
# purpose Change the scale of a given number.
# arguments 1 (num) the beginning of the new scale.
# 2 (num) the ending of the new scale.
# 3 (num) the beginning of the old scale.
# 4 (num) the ending of the old scale.
# 5 (num or tuple/list/dict of nums) value(s) to be rescaled
# returns (like arg 5) rescaled value(s) or None
#-----------------------------------------------------------------------------
def scale( new1, new2, old1, old2, value ):
delta = old2 - old1
if istuple( value ):
t = ()
for v in value:
t = t + ( ( (new2 - new1) * (v - old1) + (new1 * delta) ) / delta, )
return t
if isanylist( value ):
l = []
for v in value:
l += [ ( (new2 - new1) * (v - old1) + (new1 * delta) ) / delta ]
return l
if isdict( value ):
d = {}
for k in value.keys():
d[k] = ( (new2 - new1) * (value[k] - old1) + (new1 * delta) ) / delta
return d
if isnum( value ):
return ( (new2 - new1) * (value - old1) + (new1 * delta) ) / delta
return None
#-----------------------------------------------------------------------------
# function plural
# purpose Syntactic sugar to yield 's' for plural cases else ''
# and allow substituting other strings.
# usage print( 'I see %d thing%s here'%(n,plural(n)) )
# print( 'il y a %d chose%s ici'%(n,plural(n)) )
# print( 'Det er %d element%s her'%(n,plural(n,'','er')) )
# print( 'There %s %d thing%s here'%(plural(n,'is','are'),n,plural(n)) )
#-----------------------------------------------------------------------------
def plural( value, one='', not_one='s' ):
return one if value==1 else not_one
#-----------------------------------------------------------------------------
# function yes_or_no
# purpose Determine if a given string means yes/true or no/false.
# arguments 1: the string or value to check
# no= alternate value to return for no
# yes= alternate value to return for yes
# unknown= alternate value to return for unknown
# returns False or no= if string/value indicates no/false
# True or yes= if string/value indicates yes/true
# None or unknwon= if string/value cannot be determined
# note Only a few European languages are represented here.
#-----------------------------------------------------------------------------
def yes_or_no(x,no=False,yes=True,unknown=None):
if x == None:
return unknown
if isnum( x ): # isnum also matches bool
if x:
return yes
else:
return no
elif isstr( x ):
if x.lower() in __nf__:
return no
elif x.lower() in __yt__:
return yes
return unknown
oui_ou_non = yes_or_no
ja_oder_nein = yes_or_no
ja_of_nee = yes_or_no
ja_eller_nej = yes_or_no
ja_eller_nei = yes_or_no
#-----------------------------------------------------------------------------
# function test_pyutils
# purpose perform tests of functions in this module with results output to stdout
#-----------------------------------------------------------------------------
def test_pyutils( args ):
tprint( '\nTesting scale' )
for c in (-40,0,10,15.20,25,100 ):
f = scale( 32,212,0,100, c )
c = scale( 0,100,32,212, f )
tprint( str(f)+'F', '==', str(c)+'C')
tprint( '\nTesting plural' )
for n in (1,2,3):
tprint( '' )
tprint( 'Il y a %d chose%s ici'%(n,plural(n)) )
tprint( 'Det er %d element%s her'%(n,plural(n,'','er')) )
tprint( 'There %s %d thing%s here'%(plural(n,'is','are'),n,plural(n)) )
tprint( '\nTesting oui_ou_non' )
for f in ('oui','non','yes','no','True','False','None','unknown',True,False,None,-1,0,1,2):
if oui_ou_non( f ):
e = True
else:
e = False
tprint( 'for', repr(f), 'the result is', repr(e) )
eprint( '' )
return 0
#-----------------------------------------------------------------------------
def main( args ):
return test_pyutils( args )
#-----------------------------------------------------------------------------
if __name__ == '__main__':
result = main( argv )
stdout.flush()
try:
exit( int( result ) )
except ValueError:
print( str( result ), file=stderr )
exit( 1 )
except TypeError:
if result == None:
exit( 0 )
exit( 255 )
# EOF
works! i used "(Select All)" and saved it and checked it and got an identical file. md5 checksum is: 45a5d821d22b508421255ba6663a9e45
RE: pyutils module - Skaperen - Oct-04-2016
see, i make mistakes. now i gotta do it all over, again (the edit button is gone)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# Copyright (C) 2016 - 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 using the short first name, middle initial,
# and last name, separated by periods, with gmail dot com as the host part
# of an email address.
#-----------------------------------------------------------------------------
# The intent is that this module works corrently under both Python 2 and
# Python 3. Please report failures to the author.
#-----------------------------------------------------------------------------
# file pyutils.py
# purpose collection of my useful python stuff
# email 10054452614123394844460370234029112340408691
#-----------------------------------------------------------------------------
from __future__ import division, print_function
from base64 import b16decode, b16encode
from base64 import b32decode, b32encode
from base64 import b64decode, b64encode
from decimal import Decimal, InvalidOperation
from hashlib import sha256, sha384, sha512
from os import chdir as cd, environ as env
from random import randint
from signal import SIGKILL, SIGTERM
from subprocess import call, PIPE, Popen
from sys import argv, stderr, stdout, version_info
from time import sleep, time
if version_info[:3] >= (3,4,0):
from base64 import a85decode, a85encode
from base64 import b85decode, b85encode
__y__=('ano','bai','da','ie','ja','jah','jes','kylla','oui','si','sim','taip','tak','yes')
__n__=('dim','ei','ez','ingen','inno','nao','ne','nee','nei','nein','nej','neniu','nie','no','non','nu')
__t__=('cierto','egia','ekte','ikke','istina','pravi','rigtigt','sann','satt','totta','true','vera','veraj','verdade','veritable','vero','vrai','waar','wahr','wier')
__f__=('falsa','falsch','false','falsk','falso','faux','lazan','rangar','sbagliate','vaara','vals')
__yt__=set(__y__+__t__)
__nf__=set(__n__+__f__)
#-----------------------------------------------------------------------------
# function is(various)
# purpose various function to test data types
# note isint() returns True also for type bool
#-----------------------------------------------------------------------------
def isstr(x): return True if 'split' in dir(x) else False
def isdict(x): return True if 'items' in dir(x) else False
def isanylist(x): return False if 'split' in dir(x) else True if 'index' in dir(x) else False
def isonlylist(x): return False if 'split' in dir(x) else True if 'sort' in dir(x) else False
def istuple(x): return False if 'sort' in dir(x) else True if 'index' in dir(x) else False
def isint(x): return True if 'bit_length' in dir(x) else False
def isfloat(x): return True if 'is_integer' in dir(x) else False
def isdecimal(x): return True if 'radix' in dir(x) else False
def isdatetime(x): return True if 'time' in dir(x) else False
def isdate(x): return True if 'isocalendar' in dir(x) else False
def isnum(x): return True if 'bit_length' in dir(x) else True if 'is_integer' in dir(x) else True if 'radix' in dir(x) else False
#-----------------------------------------------------------------------------
# function fint
# purpose convert x to an int only if x is a float with an int value
#-----------------------------------------------------------------------------
def fint(x):
if 'is_integer' in dir(x):
if x.is_integer():
return int(x)
return x
#-----------------------------------------------------------------------------
# function tprint
# purpose a print function that avoids exceptions such as bad pipelines
#-----------------------------------------------------------------------------
def tprint( *msg, **opt ):
try:
return print( *msg, **opt )
except IOError:
return None
#-----------------------------------------------------------------------------
# function eprint
# purpose like tprint() but defaults to file=sys.stderr
#-----------------------------------------------------------------------------
def eprint( *msg, **opt ):
if 'file' in opt:
f = opt[ 'file' ]
else:
f = stderr
return tprint( *msg, file=f, **opt )
#-----------------------------------------------------------------------------
# function b(various)
# purpose various functions to convert binary encoding
#-----------------------------------------------------------------------------
def b16to32(x): return b32encode(b16decode(x.upper()))
def b16to64(x,*a): return b64encode(b16decode(x.upper()),*a)
def b32to16(x): return b16encode(b32decode(x)).lower()
def b32to64(x,*a): return b64encode(b32decode(x),*a)
def b64to16(x,*a): return b16encode(b64decode(x,*a)).lower()
def b64to32(x,*a): return b32encode(b64decode(x,*a))
if version_info[:3] >= (3,4,0):
def b16to85(x): return b85encode(b16decode(x.upper()))
def b32to85(x): return b85encode(b32decode(x))
def b64to85(x,*a): return b85encode(b64decode(x,*a))
def b85to16(x): return b16encode(b85decode(x)).lower()
def b85to32(x): return b32encode(b85decode(x))
def b85to64(x,*a): return b64encode(b85decode(x),*a)
#-----------------------------------------------------------------------------
# function mc_env
# pupose mixed case environment lookup (UPPER CASE PREVAILS)
#-----------------------------------------------------------------------------
def mc_env( name ):
if name.upper() in environ:
return env[ name.upper() ]
if name.lower() in environ:
return env[ name.lower() ]
if name in environ:
return env[ name ]
return None
#-----------------------------------------------------------------------------
# function valid_decimal
# purpose non-exception test if given value can be decimal.Decimal.
#-----------------------------------------------------------------------------
def valid_decimal( decarg ):
try:
result = Decimal( decarg )
except InvalidOperation:
result = None
return result
#-----------------------------------------------------------------------------
# function unmod
# purpose Return the remainder after subtracting the modulus.
# note If a list or tuple of numbers is given, return in kind
# arguments 1 (num) number
# 2 (num) modulus
# returns (num) number - (number % modulus)
# example unmod(456789,1000) -> 456000
# unmod(256,15) -> 240
# usage unmod(seconds,3600) -> start of this hour in seconds
# unmod(seconds,86400) -> start of this day in seconds
# unmod(minutes,1440) -> start of this day in minutes
#-----------------------------------------------------------------------------
def unmod(n,m):
if not isnum(m):
raise TypeError('Not a number passed to unmod() arg2: %s (modulus)' % repr(m))
if type(n) in (tuple,list):
ns = [unmod(x,m) for x in n]
if type(n) in (tuple,):
return tuple(ns)
return ns
if not isnum(n):
raise TypeError('Not a number passed to unmod() arg1: %s (value)' % repr(n))
if m == 0:
raise ValueError('Zero passed to unmod() arg2 (modulus)')
if m < 0:
raise ValueError('Negative value passed to unmod() arg2: %s (modulus)' % repr(m))
return n - (n % m)
#-----------------------------------------------------------------------------
# function find_unicode
# purpose In a unicode string, find the position of the first character
# that requires the unicode type and cannot be represented in
# mere string type.
# returns (int) position or the value specified by notfound=
#-----------------------------------------------------------------------------
def find_unicode(u,notfound=-1):
l = len(u)
for n in xrange(l):
if ord(u[n]) > 127:
return n
return notfound
#-----------------------------------------------------------------------------
# function scale
# purpose Change the scale of a given number.
# arguments 1 (num) the beginning of the new scale.
# 2 (num) the ending of the new scale.
# 3 (num) the beginning of the old scale.
# 4 (num) the ending of the old scale.
# 5 (num or tuple/list/dict of nums) value(s) to be rescaled
# returns (like arg 5) rescaled value(s) or None
#-----------------------------------------------------------------------------
def scale( new1, new2, old1, old2, value ):
delta = old2 - old1
if istuple( value ):
t = ()
for v in value:
t = t + ( ( (new2 - new1) * (v - old1) + (new1 * delta) ) / delta, )
return t
if isanylist( value ):
l = []
for v in value:
l += [ ( (new2 - new1) * (v - old1) + (new1 * delta) ) / delta ]
return l
if isdict( value ):
d = {}
for k in value.keys():
d[k] = ( (new2 - new1) * (value[k] - old1) + (new1 * delta) ) / delta
return d
if isnum( value ):
return ( (new2 - new1) * (value - old1) + (new1 * delta) ) / delta
return None
#-----------------------------------------------------------------------------
# function plural
# purpose Syntactic sugar to yield 's' for plural cases else ''
# and allow substituting other strings.
# usage print( 'I see %d thing%s here'%(n,plural(n)) )
# print( 'il y a %d chose%s ici'%(n,plural(n)) )
# print( 'Det er %d element%s her'%(n,plural(n,'','er')) )
# print( 'There %s %d thing%s here'%(plural(n,'is','are'),n,plural(n)) )
#-----------------------------------------------------------------------------
def plural( value, one='', not_one='s' ):
return one if value==1 else not_one
#-----------------------------------------------------------------------------
# function yes_or_no
# purpose Determine if a given string means yes/true or no/false.
# arguments 1: the string or value to check
# no= alternate value to return for no
# yes= alternate value to return for yes
# unknown= alternate value to return for unknown
# returns False or no= if string/value indicates no/false
# True or yes= if string/value indicates yes/true
# None or unknwon= if string/value cannot be determined
# note Only a few European languages are represented here.
#-----------------------------------------------------------------------------
def yes_or_no(x,no=False,yes=True,unknown=None):
if x == None:
return unknown
if isnum( x ): # isnum also matches bool
if x:
return yes
else:
return no
elif isstr( x ):
if x.lower() in __nf__:
return no
elif x.lower() in __yt__:
return yes
return unknown
oui_ou_non = yes_or_no
ja_oder_nein = yes_or_no
ja_of_nee = yes_or_no
ja_eller_nej = yes_or_no
ja_eller_nei = yes_or_no
#-----------------------------------------------------------------------------
# function test_pyutils
# purpose perform tests of functions in this module with results output to stdout
#-----------------------------------------------------------------------------
def test_pyutils( args ):
tprint( '\nTesting scale' )
for c in (-40,0,10,15.20,25,100 ):
f = scale( 32,212,0,100, c )
c = scale( 0,100,32,212, f )
tprint( str(f)+'F', '==', str(c)+'C')
tprint( '\nTesting plural' )
for n in (1,2,3):
tprint( '' )
tprint( 'Il y a %d chose%s ici'%(n,plural(n)) )
tprint( 'Det er %d element%s her'%(n,plural(n,'','er')) )
tprint( 'There %s %d thing%s here'%(plural(n,'is','are'),n,plural(n)) )
tprint( '\nTesting oui_ou_non' )
for f in ('oui','non','yes','no','True','False','None','unknown',True,False,None,-1,0,1,2):
if oui_ou_non( f ):
e = True
else:
e = False
tprint( 'for', repr(f), 'the result is', repr(e) )
eprint( '' )
return 0
#-----------------------------------------------------------------------------
def main( args ):
return test_pyutils( args )
#-----------------------------------------------------------------------------
if __name__ == '__main__':
result = main( argv )
stdout.flush()
try:
exit( int( result ) )
except ValueError:
print( str( result ), file=stderr )
exit( 1 )
except TypeError:
if result == None:
exit( 0 )
exit( 255 )
# EOF
new md5 checksum is: 76d7e080645a69fceb38526eee192db1
RE: pyutils module - nilamo - Oct-04-2016
Quote:if version_info[:3] >= (3,4,0):
from base64 import a85decode, a85encode
...but then you never use the imported functions? And they're also undefined, if you're using an older version of python?
RE: pyutils module - metulburr - Oct-04-2016
Quote:see, i make mistakes. now i gotta do it all over, again (the edit button is gone)
This is actually better for the flow of dialogue. This is not the best example, but imagine if there were a few other people's posts between your first and second post. If you edit your initial post, the posts after it does not makes sense. Imagine someone coming to this thread having the exact same problem, and not being able to understand what is going on because people are editing their posts.
RE: pyutils module - nilamo - Oct-04-2016
What happened a lot at the old forums, was someone would ask a homework question, then after they got the answer, they'd edit the original post to delete all traces of themselves. It's why the first reply was almost always just a quote of what they said, so they couldn't do that, lol.
RE: pyutils module - metulburr - Oct-04-2016
I made a new help doc...i know this is going to be asked numerous times in the future.
http://python-forum.io/misc.php?action=help&hid=32
:s
RE: pyutils module - Skaperen - Oct-05-2016
(Oct-04-2016, 07:39 PM)nilamo Wrote: Quote:if version_info[:3] >= (3,4,0):
from base64 import a85decode, a85encode
...but then you never use the imported functions? And they're also undefined, if you're using an older version of python?
is importing a function and not using it in the module considered bad?
i used to have a separate module just to import frequently used functions. i merged that module because there were a lot of imported functions that were the same. i wanted to have just one import in my other code so i merged them. then i made the base64 stuff more consistent by filling in the gaps for encode/decode. i have yet to figure out the difference between a85... and b85... i decided to include both, but maybe i don't need to. if the a85 functions would be less often used in other code or a85 is an odd encoding then i would be inclined to remove them. but i would not remove the version test. is that an issue?
(Oct-04-2016, 07:58 PM)metulburr Wrote: I made a new help doc...i know this is going to be asked numerous times in the future.
http://python-forum.io/misc.php?action=help&hid=32
:s
the difference between the code inserts was a typo ("b64" where it should have been "b85" on line45). if i had been able to edit the inserted code, i would be fixing a typo. but code could be considered different; someone may have already downloaded the bad one.
a discussion around code (or documentation) might be good to have a wikipedia style history flow so everyone can see all the changes. but i have seen no forum with this feature. maybe a parallel wiki.python-forum.io would have uses.
the new way to display code is nice.
RE: pyutils module - metulburr - Oct-05-2016
Quote:everyone can see all the changes. but i have seen no forum with this feature
That would be ideal. I was actually looking for a plugin like this when i first installed this software. If we were able to do that, then we could be more lenient in editing.
RE: pyutils module - nilamo - Oct-05-2016
(Oct-05-2016, 04:25 AM)Skaperen Wrote: is importing a function and not using it in the module considered bad? No, it's just odd that the function is only sometimes there... there's no fallback for an older version of python. And if it's not being used within the module, and only there to help make something *else* easier, then you should probably add a comment saying so.
RE: pyutils module - Skaperen - Oct-06-2016
(Oct-05-2016, 02:34 PM)nilamo Wrote: (Oct-05-2016, 04:25 AM)Skaperen Wrote: is importing a function and not using it in the module considered bad? No, it's just odd that the function is only sometimes there... there's no fallback for an older version of python. And if it's not being used within the module, and only there to help make something *else* easier, then you should probably add a comment saying so.
the a85 and b85 functions were added in Python 3.4.0. i want other stuff to still work if my module is imported in an earlier version. i do the same test for the def of functions that depend on those a85 and b85 functions, so that importing my module (maybe by code i provide in the future) is not the point of failure. dealing with cases like this is always tricky.
|