2022年4月27日 星期三

python json object is not serialable

problem: in class or object, we need define json encode data


Solution:

add  def __jsonencode__(self): 

create AdvancedJSONEncoder



example: 

-------------

import json


class User(object):

    def __init__(self, username):

        self.username = username

    def __jsonencode__(self):

        return {'username': self.username}

        

class AdvancedJSONEncoder(json.JSONEncoder):

    def default(self, obj):

        if hasattr(obj, '__jsonencode__'):

            return obj.__jsonencode__()


        #if isinstance(obj, set):

        #    return list(obj)

        return json.JSONEncoder.default(self, obj)



user = User('foo')

print(json.dumps(user,cls=AdvancedJSONEncoder))

-------------

good

https://myapollo.com.tw/zh-tw/python-make-json-serializable-class

other

https://www.kmp.tw/post/jsondumpsdatetimeerror/


python argument empty array error


Finally: 

. Python’s default arguments are evaluated once when the function is defined, not each time the function is called.


 There is a technique called memoization 


https://nikos7am.com/posts/mutable-default-arguments/


Avoid using an empty list as a default argument to a function


A very common error in Python is the use of an empty list as a default argument to a function. This is not the right way to do it and can cause unwanted behavior. See for example below:

def append_to_list(element, list_to_append=[]):
    list_to_append.append(element)
    return list_to_append
>>> a = append_to_list(10)
[10]
>>> b = append_to_list(20)
[10, 20]

2022年4月7日 星期四

x509 and open ssl

 中華憑證

https://publicca.hinet.net/download/SSL/Lighttpd_INSTALL.pdf

https://justry.io/%E5%A6%82%E4%BD%95%E5%9C%A8apache%E5%AE%89%E8%A3%9Dssl%E6%86%91%E8%AD%89/



SSL 憑證製作與匯入/ 中繼憑證/根憑證

https://ithelp.ithome.com.tw/m/articles/10282250
https://www.sslbuyer.com/index.php?option=com_content&view=article&id=183:what-is-certificate-chain&catid=25&Itemid=4031


x509
https://zh.wikipedia.org/zh-tw/X.509
https://www.researchgate.net/figure/X509-SSL-certificate-format-46_fig3_321580115



憑證學習
https://blog.dexiang.me/zh-tw/technologies/x509/
https://haway.30cm.gg/ssl-key-csr-crt-pem/

憑證名稱欄
https://stellvia7.pixnet.net/blog/post/117141052-%5B%E8%BD%89%E8%B2%BC%5D-%E6%86%91%E8%AD%89%E7%9A%84%E9%81%8B%E4%BD%9C%E6%96%B9%E5%BC%8F-%28x.509%29

工具
#csr check https://www.sslshopper.com/csr-decoder.html
https://support.qacafe.com/knowledge-base/how-do-i-display-the-contents-of-a-ssl-certificate/
https://www.sslshopper.com/article-most-common-openssl-commands.html
https://knowledge.digicert.com/solution/SO29559.html

2022年4月6日 星期三

python exception strange with finally return will result no exception

 


def say():

    try:

        1/0

    except Exception as e:

        print("exception in side")

        raise Exception("say exception")

    finally:

        print("finally")

        # have this line happen no exception disappear

        return True

        

try:

    say()

except Exception as e:

    print("exception out side")

    print(e)



#>>>

exception in side

finally


#>>> no return statement is corrent

exception in side

finally

exception out side

say exception

2022年4月5日 星期二

python pass function in class with multiple parameter

 


class myTest:

    def __init__(self):

        self.doSubs = [{'func':self.shout}, {'func':self.say}]

        #self.doSub = self.shout

    

    def shout(self, text = 'Good', name = 'day'): 

        return text.upper() + ', ' + name

    

    def say(self, text = "hi"): 

        return text.upper()


 #   def doRun(self):

 #       print(self.doSub('Hello', 'andy'))

        

    def doRun(self):

        for job in self.doSubs:

            print(job['func']())

            #print(job['func']('Hello', 'andy'))

        

        

test = myTest()

#print( test.shout('hello', 'andy') )

#yell = shout

test.doRun() 

    

#print(yell('Hello', 'andy'))