Posted in Uncategorized

python tricks

In [23]: class Foo: 
    ...:     a = 1 
    ...:     b = {'c': 2} 
    ...:                                                                                                                                                                                                                                                                     

In [24]: f1 = Foo()                                                                                                                                                                                                                                                          

In [25]: f2 = Foo()                                                                                                                                                                                                                                                          

In [26]: f1.a = 11                                                                                                                                                                                                                                                           

In [27]: f1.b['c'] = 12                                                                                                                                                                                                                                                      

In [28]: f2.a, f2.b                                                                                                                                                                                                                                                          

Out[28]: (1, {'c': 12})

In [29]: f1.a, f1.b                                                                                                                                                                                                                                                          

Out[29]: (11, {'c': 12})

Posted in Uncategorized

python/numpy/pandas data types

Data typeDescription
bool_Boolean (True or False) stored as a byte
int_Default integer type (same as C long; normally either int64 or int32)
intcIdentical to C int (normally int32 or int64)
intpInteger used for indexing (same as C ssize_t; normally either int32 or int64)
int8Byte (-128 to 127)
int16Integer (-32768 to 32767)
int32Integer (-2147483648 to 2147483647)
int64Integer (-9223372036854775808 to 9223372036854775807)
uint8Unsigned integer (0 to 255)
uint16Unsigned integer (0 to 65535)
uint32Unsigned integer (0 to 4294967295)
uint64Unsigned integer (0 to 18446744073709551615)
float_Shorthand for float64.
float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_Shorthand for complex128.
complex64Complex number, represented by two 32-bit floats
complex128Complex number, represented by two 64-bit floats
Posted in Uncategorized

jupyter tricks

increase cell width

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

increase max columns and max rows to display

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)


Posted in Uncategorized

singleton in python

Reference: python-3-patterns-idioms-test

class OnlyOne:
    class __OnlyOne:
        def __init__(self, arg):
            self.val = arg
        def __str__(self):
            return repr(self) + self.val
    instance = None
    def __init__(self, arg):
        if not OnlyOne.instance:
            OnlyOne.instance = OnlyOne.__OnlyOne(arg)
        else:
            OnlyOne.instance.val = arg
    def __getattr__(self, name):
        return getattr(self.instance, name)