signature:
def receive ( self ) :
    """Receive TCP response , looping to get whole thing or timeout ."""

implementation:
try :
    buffer = self . _socket . recv ( BUFFER_SIZE )
except socket . timeout as error : # Something is wrong , assume it ' s offline temporarily
    _LOGGER . error ( "Error receiving: %s" , error )
    # self . _ socket . close ( )
    return ""
# Read until a newline or timeout
buffering = True
response = ''
while buffering :
    if '\n' in buffer . decode ( "utf8" ) :
        response = buffer . decode ( "utf8" ) . split ( '\n' ) [ 0 ]
        buffering = False
    else :
        try :
            more = self . _socket . recv ( BUFFER_SIZE )
        except socket . timeout :
            more = None
        if not more :
            buffering = False
            response = buffer . decode ( "utf8" )
        else :
            buffer += more
return response


signature:
def get_data ( self ) :
    """Get current light data as dictionary with light zids as keys ."""

implementation:
response = self . send_command ( GET_LIGHTS_COMMAND )
_LOGGER . debug ( "get_data response: %s" , repr ( response ) )
if not response :
    _LOGGER . debug ( "Empty response: %s" , response )
    return { }
response = response . strip ( )
# Check string before splitting ( avoid IndexError if malformed )
if not ( response . startswith ( "GLB" ) and response . endswith ( ";" ) ) :
    _LOGGER . debug ( "Invalid response: %s" , repr ( response ) )
    return { }
# deconstruct response string into light data . Example data :
# GLB 143E , 1,1,25,255,255,255,0,0;287B , 1,1,22,255,255,255,0,0 ; \ r \ n
response = response [ 4 : - 3 ]
# strip start ( GLB ) and end ( ; \ r \ n )
light_strings = response . split ( ';' )
light_data_by_id = { }
for light_string in light_strings :
    values = light_string . split ( ',' )
    try :
        light_data_by_id [ values [ 0 ] ] = [ int ( values [ 2 ] ) , int ( values [ 4 ] ) , int ( values [ 5 ] ) , int ( values [ 6 ] ) , int ( values [ 7 ] ) ]
    except ValueError as error :
        _LOGGER . error ( "Error %s: %s (%s)" , error , values , response )
    except IndexError as error :
        _LOGGER . error ( "Error %s: %s (%s)" , error , values , response )
return light_data_by_id


signature:
def get_lights ( self ) :
    """Get current light data , set and return as list of Bulb objects ."""

implementation:
# Throttle updates . Use cached data if within UPDATE _ INTERVAL _ SECONDS
now = datetime . datetime . now ( )
if ( now - self . _last_updated ) < datetime . timedelta ( seconds = UPDATE_INTERVAL_SECONDS ) : # _ LOGGER . debug ( " Using cached light data " )
    return self . _bulbs
else :
    self . _last_updated = now
light_data = self . get_data ( )
_LOGGER . debug ( "got: %s" , light_data )
if not light_data :
    return [ ]
if self . _bulbs : # Bulbs already created , just update values
    for bulb in self . _bulbs : # use the values for the bulb with the correct ID
        try :
            values = light_data [ bulb . zid ]
            bulb . _online , bulb . _red , bulb . _green , bulb . _blue , bulb . _level = values
        except KeyError :
            pass
else :
    for light_id in light_data :
        self . _bulbs . append ( Bulb ( self , light_id , * light_data [ light_id ] ) )
# return a list of Bulb objects
return self . _bulbs


signature:
def rgb_color ( self ) :
    """Return the color property as list of [ R , G , B ] , each 0-255."""

implementation:
self . update ( )
return [ self . _red , self . _green , self . _blue ]


signature:
def turn_on ( self ) :
    """Turn bulb on ( full brightness ) ."""

implementation:
command = "C {},,,,100,\r\n" . format ( self . _zid )
response = self . _hub . send_command ( command )
_LOGGER . debug ( "Turn on %s: %s" , repr ( command ) , response )
return response


signature:
def set_brightness ( self , brightness ) :
    """Set brightness of bulb ."""

implementation:
command = "C {},,,,{},\r\n" . format ( self . _zid , brightness )
response = self . _hub . send_command ( command )
_LOGGER . debug ( "Set brightness %s: %s" , repr ( command ) , response )
return response


signature:
def set_all ( self , red , green , blue , brightness ) :
    """Set color and brightness of bulb ."""

implementation:
command = "C {},{},{},{},{},\r\n" . format ( self . _zid , red , green , blue , brightness )
response = self . _hub . send_command ( command )
_LOGGER . debug ( "Set all %s: %s" , repr ( command ) , response )
return response


signature:
def update ( self ) :
    """Update light objects to their current values ."""

implementation:
bulbs = self . _hub . get_lights ( )
if not bulbs :
    _LOGGER . debug ( "%s is offline, send command failed" , self . _zid )
    self . _online = False


signature:
def functional ( ifunctional ) :
    """fun ( fn ) - > function or
 fun ( fn , args . . . ) - > call of fn ( args . . . )
 : param ifunctional : f
 : return : decorated function"""

implementation:
@ wraps ( ifunctional )
def wrapper ( fn , * args , ** kw ) :
    fn = ifunctional ( fn )
    if args or kw :
        return fn ( * args , ** kw )
    else :
        return fn
return wrapper


implementation:
@ wraps ( fn )
def wrapped ( * args , ** kwargs ) :
    args = map ( tuplefy , args )
    return fn ( * args , ** kwargs )
return wrapped


signature:
def tuple_args ( fn ) :
    """args 파싱 유틸 function
 fun ( p1 , p2 , . . . pn , * * kwargs ) or fun ( [ p1 , p2 , . . ] , * * kwargs )
 ex ) 샘플 : :
 @ tuple _ arg
 def f ( args , * * kwargs ) :
 for d in args :
 print d
 f ( 1,2,3 ) = > f ( [ 1,2,3 ] )
 : param function fn :
 : return :"""

implementation:
@ wraps ( fn )
def wrapped ( * args , ** kwargs ) :
    if len ( args ) == 1 :
        if isinstance ( args [ 0 ] , tuple ) :
            return fn ( args [ 0 ] , ** kwargs )
        elif isinstance ( args [ 0 ] , list ) :
            return fn ( tuple ( args [ 0 ] ) , ** kwargs )
    return fn ( args , ** kwargs )
return wrapped


signature:
def unpack_args ( classfun , nth = 0 ) :
    """args 갯수가 nth + 1 개 일때 , 그게 만약 tuple이면 , unpack
 : param classfun :
 : param nth : nth = 0 , 일반 함수 , nth = 1 : 클래스 함수 1이 self니깐 .
 : return :"""

implementation:
if classfun :
    nth = 1
def deco ( fn ) :
    def wrapped ( * args , ** kwargs ) :
        if len ( args ) == nth + 1 and isinstance ( args [ nth ] , ( tuple , list ) ) :
            args = tuple ( args [ : nth ] + args [ nth ] )
            return fn ( * args , ** kwargs )
        else :
            return fn ( * args , ** kwargs )
    return wrapped
return deco


signature:
def optional_str ( deco ) :
    """string 1개만 deco 인자로 오거나 없거나 .
 : param deco :
 : return :"""

implementation:
@ wraps ( deco )
def dispatcher ( * args , ** kwargs ) : # when only function arg
    if not kwargs and len ( args ) == 1 and not isinstance ( args [ 0 ] , str ) and args [ 0 ] is not None :
        decorator = deco ( )
        return decorator ( args [ 0 ] )
    else : # decorator with args
        decorator = deco ( * args , ** kwargs )
        return decorator
return dispatcher


signature:
def patchmethod ( * cls , ** kwargs ) :
    """클래스 멤버 패치 @ patchmethod ( Cls1 , . . . , [ name = ' membername ' ] )
 ex )
 class A ( object ) :
 def _ _ init _ _ ( self , data ) :
 self . data = data
 @ patchmethod ( A )
 def sample ( self ) :
 ' ' ' haha docstrings ' ' '
 print self . data
 @ patchmethod ( A , name = ' membermethod )
 def sample ( self ) :
 ' ' ' haha docstrings ' ' '
 print self . data
 a = A ( )
 a . sample ( )"""

implementation:
def _patch ( fun ) :
    m = kwargs . pop ( 'name' , None ) or fun . __name__
    for c in cls :
        setattr ( c , m , fun )
        # c . _ _ dict _ _ [ m ] . _ _ doc _ _ = fun . _ _ doc _ _
def wrap ( fun ) :
    _patch ( fun )
    return fun
return wrap


signature:
def patchproperty ( * cls , ** kwargs ) :
    """class getter 함수 패치 decorator
 EX )
 class B ( A ) :
 pass
 @ patchproperty ( B )
 def prop ( self ) :
 return ' hello '
 : param cls :
 : param kwargs :"""

implementation:
def _patch ( fun ) :
    m = kwargs . pop ( 'property' , None ) or fun . __name__
    p = property ( fun )
    for c in cls :
        setattr ( c , m , p )
def wrap ( fun ) :
    _patch ( fun )
    return fun
return wrap


signature:
def on_interrupt ( handler , reraise = False ) :
    """context for handling keyboardinterrupt
 ex )
 with on _ interrupt ( handler ) :
 critical _ work _ to _ prevent ( )
 from logger import logg
 on _ interrupt . signal = None
 : param function handler :
 : param bool reraise :
 : return : context"""

implementation:
def _handler ( sig , frame ) :
    handler . signal = ( sig , frame )
    handler . _reraise = handler ( )
handler . _reraise = False
handler . signal = None
oldhandler = signal . getsignal ( signal . SIGINT )
signal . signal ( signal . SIGINT , _handler )
yield handler
signal . signal ( signal . SIGINT , oldhandler )
if ( reraise or handler . _reraise ) and handler . signal :
    oldhandler ( * handler . signal )


signature:
def interrupt_guard ( msg = '' , reraise = True ) :
    """context for guard keyboardinterrupt
 ex )
 with interrupt _ guard ( ' need long time ' ) :
 critical _ work _ to _ prevent ( )
 : param str msg : message to print when interrupted
 : param reraise : re - raise or not when exit
 : return : context"""

implementation:
def echo ( ) :
    print ( msg )
return on_interrupt ( echo , reraise = reraise )


signature:
def is_main_alive ( ) :
    """is 메인 쓰레드 alive ?
 : rtype : bool"""

implementation:
for t in threading . enumerate ( ) :
    if t . name == 'MainThread' :
        return t . is_alive ( )
print ( 'MainThread not found' )
return False


signature:
def retrieve_document ( file_path , directory = 'sec_filings' ) :
    '''This function takes a file path beginning with edgar and stores the form in a directory .
 The default directory is sec _ filings but can be changed through a keyword argument .'''

implementation:
ftp = FTP ( 'ftp.sec.gov' , timeout = None )
ftp . login ( )
name = file_path . replace ( '/' , '_' )
if not os . path . exists ( directory ) :
    os . makedirs ( directory )
with tempfile . TemporaryFile ( ) as temp :
    ftp . retrbinary ( 'RETR %s' % file_path , temp . write )
    temp . seek ( 0 )
    with open ( '{}/{}' . format ( directory , name ) , 'w+' ) as f :
        f . write ( temp . read ( ) . decode ( "utf-8" ) )
    f . closed
    records = temp
    retry = False
ftp . close ( )


signature:
def action ( self , item ) :
    """for overriding
 : param item :
 : return :"""

implementation:
fun , args , kwargs = item
return fun ( * args , ** kwargs )


signature:
def push_job ( self , fun , * args , ** kwargs ) :
    """put job if possible , non - blocking
 : param fun :
 : param args :
 : param kwargs :
 : return :"""

implementation:
assert callable ( fun )
return self . put ( ( fun , args , kwargs ) , block = True )


signature:
def put_job ( self , fun , * args , ** kwargs ) :
    """put job if possible , non - blocking
 : param fun :
 : param args :
 : param kwargs :
 : return :"""

implementation:
if not args and not kwargs and isinstance ( fun , ( tuple , list ) ) : # ex ) q . put _ job ( [ fun , args , kwargs ] )
    fun , args , kwargs = fun
assert callable ( fun )
return self . put ( ( fun , args , kwargs ) , block = False )


signature:
def add_flag ( * args , ** kwargs ) :
    """define a single flag .
 add _ flag ( flagname , default _ value , help = ' ' , * * kwargs )
 add _ flag ( [ ( flagname , default _ value , help ) , . . . ] )
 or
 define flags without help message
 add _ flag ( flagname , default _ value , help = ' ' , * * kwargs )
 add _ flag ( ' gpu ' , 1 , help = ' CUDA _ VISIBLE _ DEVICES ' )
 : param args :
 : param kwargs :
 : return :"""

implementation:
if len ( args ) == 1 and isinstance ( args [ 0 ] , ( list , tuple ) ) :
    for a in args [ 0 ] :
        flag . add_flag ( * a )
elif args :
    flag . add_flag ( * args , ** kwargs )
else :
    for f , v in kwargs . items ( ) :
        flag . add_flag ( f , v )


signature:
def mkdir_if_not ( filepath , ispath = False ) :
    """path 부분이 없으면 mkdir 을 한다 .
 : param filepath : 파일 패쓰
 : return : filpath 그대로 리턴"""

implementation:
if not ispath :
    p , _ = os . path . split ( filepath )
else :
    p = filepath
if not p :
    return filepath
if not os . path . exists ( p ) : # M . info ( ' % s not exist , trying mkdir ' , p )
    try :
        os . makedirs ( p )
    except FileExistsError as e :
        logg . warn ( str ( e ) )
return filepath


signature:
def readlines ( filepath ) :
    """read lines from a textfile
 : param filepath :
 : return : list [ line ]"""

implementation:
with open ( filepath , 'rt' ) as f :
    lines = f . readlines ( )
    lines = map ( str . strip , lines )
    lines = [ l for l in lines if l ]
return lines


signature:
def readtxt ( filepath ) :
    """read file as is"""

implementation:
with open ( filepath , 'rt' ) as f :
    lines = f . readlines ( )
return '' . join ( lines )


signature:
def savefile ( obj , filepath , compress = True ) :
    """파일 있으면 덮어씀
 : param obj :
 : param str filepath :
 : param compress :
 : return :"""

implementation:
try :
    import cPickle as pickle
except Exception :
    import pickle
import joblib
# 일단 임시 파일에 저장 .
tmpfile = filepath + '.tmp'
mkdir_if_not ( tmpfile )
if compress :
    joblib . dump ( obj , tmpfile , compress = 3 , cache_size = 100 , protocol = pickle . HIGHEST_PROTOCOL )
else :
    joblib . dump ( obj , tmpfile , compress = 0 )
os . rename ( tmpfile , filepath )
return obj


signature:
def loadfile ( filepath , mmap_mode = None ) :
    """: param filepath :
 : param mmap _ mode : { None , ‘ r + ’ , ‘ r ’ , ‘ w + ’ , ‘ c ’ } see . joblib . load
 : return :"""

implementation:
import joblib
try :
    return joblib . load ( filepath , mmap_mode = mmap_mode )
except IOError :
    return None


signature:
def load_or_run ( filepath , fun , * args , ** kwargs ) :
    """계산된 결과 파일이 있으면 로딩하고 , 없으면 계산후 저장
 ex )
 res = load _ or _ run ( ' file _ loadorsave ' , funlongtime , . . . . , force = False )
 : param filepath :
 : param fun :
 : param force :
 : return :"""

implementation:
force = kwargs . pop ( 'force' , False )
compress = kwargs . pop ( 'compress' , True )
if not filepath . startswith ( '/' ) or not filepath . startswith ( '~' ) :
    filepath = os . path . join ( '/tmp/snipy/load_or_run/' , filepath )
if not force and os . path . exists ( filepath ) : # 저장되어 있는 것 로딩
    mmap_mode = 'r+' if not compress else None
    return loadfile ( filepath , mmap_mode = mmap_mode )
res = fun ( * args , ** kwargs )
savefile ( res , filepath , compress = compress )
return res


signature:
def fnmatches ( fname , patterns , matchfun ) :
    """matches ?
 : param fname : file name
 : type fname : str
 : param patterns : list of filename pattern . see fnmatch . fnamtch
 : type patterns : [ str ]
 : rtype : generator of bool"""

implementation:
import fnmatch
matchfun = matchfun or fnmatch . fnmatch
for p in patterns :
    yield matchfun ( fname , p )


signature:
def listdir ( p , match = '*' , exclude = '' , listtype = 'file' , matchfun = None ) :
    """list file ( or folder ) for this path ( NOT recursive )
 : param p :
 : param match :
 : param exclude :
 : param listtype : ( ' file ' | ' filepath ' | ' dir ' | ' all ' )
 : param matchfun : match fun ( default fnmatch . fnmatch ) True / False = matchfun ( name , pattern )
 : rtype :"""

implementation:
if listtype == 'file' :
    gen = listfile ( p )
elif listtype == 'filepath' :
    gen = listfilepath ( p )
elif listtype == 'dir' :
    gen = listfolder ( p )
elif listtype == 'dirpath' :
    gen = listfolderpath ( p )
else : # list file or folder
    gen = ( entry . name for entry in scandir . scandir ( p ) )
return filter_pattern ( gen , match , exclude , matchfun )


signature:
def listfile ( p ) :
    """generator of list files in the path .
 filenames only"""

implementation:
try :
    for entry in scandir . scandir ( p ) :
        if entry . is_file ( ) :
            yield entry . name
except OSError :
    return


signature:
def listfilepath ( p ) :
    """generator of list files in the path .
 filenames only"""

implementation:
for entry in scandir . scandir ( p ) :
    if entry . is_file ( ) :
        yield entry . path


signature:
def listfolder ( p ) :
    """generator of list folder in the path .
 folders only"""

implementation:
for entry in scandir . scandir ( p ) :
    if entry . is_dir ( ) :
        yield entry . name


signature:
def listfolderpath ( p ) :
    """generator of list folder in the path .
 folders only"""

implementation:
for entry in scandir . scandir ( p ) :
    if entry . is_dir ( ) :
        yield entry . path


signature:
def _pred_pattern ( match = '*' , exclude = '' , patterntype = 'fnmatch' ) :
    """internal use"""

implementation:
m , x = match , exclude
if m == '*' :
    if not x :
        pred = lambda n : True
    else :
        x = [ x ] if _is_str ( x ) else x
        matcher = get_match_fun ( x , patterntype )
        pred = lambda n : not matcher ( n )
else :
    m = [ m ] if _is_str ( m ) else m
    if not x :
        matcher = get_match_fun ( m , patterntype )
        pred = lambda n : matcher ( n )
    else :
        x = [ x ] if _is_str ( x ) else x
        matcher_m = get_match_fun ( m , patterntype )
        matcher_x = get_match_fun ( x , patterntype )
        pred = lambda n : matcher_m ( n ) and not matcher_x ( n )
return pred


signature:
def findfolder ( toppath , match = '*' , exclude = '' ) :
    """recursively find folder path from toppath .
 patterns to decide to walk folder path or not
 : type toppath : str
 : type match : str or list ( str )
 : type exclude : str or list ( str )
 : rtype : generator for path str"""

implementation:
pred = _pred_pattern ( match , exclude )
return ( p for p in walkfolder ( toppath , pred ) )


signature:
def walkfolder ( toppath , pred ) :
    """walk folder if pred ( foldername ) is True
 : type toppath : str
 : type pred : function ( str ) = > bool"""

implementation:
for entry in scandir . scandir ( toppath ) :
    if not entry . is_dir ( ) or not pred ( entry . name ) :
        continue
    yield entry . path
    for p in walkfolder ( entry . path , pred ) :
        yield p


signature:
def tempfolder ( prefix = '' ) :
    """임시 폴더를 만들어서 리턴"""

implementation:
import uuid
p = prefix + str ( uuid . uuid4 ( ) )
d = tempdir ( )
tmpd = os . path . join ( d , p )
return mkdir_if_not ( tmpd , ispath = True )


signature:
def imsize ( fname ) :
    """return image size ( height , width )
 : param fname :
 : return :"""

implementation:
from PIL import Image
im = Image . open ( fname )
return im . size [ 1 ] , im . size [ 0 ]


signature:
def intersect ( self , other ) :
    """self와 other 키가 동일한 아이템의 dictobj
 : type other : dict
 : rtype : dictobj :"""

implementation: return DictObj ( { k : self [ k ] for k in self if k in other } )


signature:
def from_dict ( dic ) :
    """recursive dict to dictobj 컨버트
 : param dic :
 : return :"""

implementation: return DictObj ( { k : DictObj . convert_ifdic ( v ) for k , v in dic . items ( ) } )


signature:
def _clean_up ( paths ) :
    """Clean up after ourselves , removing created files .
 @ param { [ String ] } A list of file paths specifying the files we ' ve created
 during run . Will all be deleted .
 @ return { None }"""

implementation:
print ( 'Cleaning up' )
# Iterate over the given paths , unlinking them
for path in paths :
    print ( 'Removing %s' % path )
    os . unlink ( path )


signature:
def _create_index_file ( root_dir , location , image_files , dirs , force_no_processing = False ) :
    """Create an index file in the given location , supplying known lists of
 present image files and subdirectories .
 @ param { String } root _ dir - The root directory of the entire crawl . Used to
 ascertain whether the given location is the top level .
 @ param { String } location - The current directory of the crawl . The index
 file will be created here .
 @ param { [ String ] } image _ files - A list of image file names in the location .
 These will be displayed in the index file ' s gallery .
 @ param { [ String ] } dirs - The subdirectories of the location directory .
 These will be displayed as links further down the file structure .
 @ param { Boolean = False } force _ no _ processing - If True , do not attempt to
 actually process thumbnails , PIL images or anything . Simply index
 < img > tags with original file src attributes .
 @ return { String } The full path ( location plus filename ) of the newly
 created index file . Intended for usage cleaning up created files ."""

implementation:
# Put together HTML as a list of the lines we ' ll want to include
# Issue # 2 exists to do this better than HTML in - code
header_text = 'imageMe: ' + location + ' [' + str ( len ( image_files ) ) + ' image(s)]'
html = [ '<!DOCTYPE html>' , '' , '    ' , '        <title>imageMe</title>' '        ' , '    ' , '    ' , '
' , '
' + header_text + '
' ]
# Populate the present subdirectories - this includes ' . . ' unless we ' re at
# the top level
directories = [ ]
if root_dir != location :
    directories = [ '..' ]
directories += dirs
if len ( directories ) > 0 :
    html . append ( '
' )
# For each subdirectory , include a link to its index file
for directory in directories :
    link = directory + '/' + INDEX_FILE_NAME
    html += [ '
' , '    ' + directory + '' , '
' ]
# Populate the image gallery table
# Counter to cycle down through table rows
table_row_count = 1
html += [ '
' , '
' ]
# For each image file , potentially create a new
 and create a new
for image_file in image_files :
    if table_row_count == 1 :
        html . append ( '
' )
    img_src = _get_thumbnail_src_from_file ( location , image_file , force_no_processing )
    link_target = _get_image_link_target_from_file ( location , image_file , force_no_processing )
    html += [ '    ' , '    ' , '        ' , '    ' , '    	' ]
    if table_row_count == IMAGES_PER_ROW :
        table_row_count = 0
        html . append ( '
' )
    table_row_count += 1
html += [ '
' , '
' ]
html += [ '
' , '    ' , '' ]
# Actually create the file , now we ' ve put together the HTML content
index_file_path = _get_index_file_path ( location )
print ( 'Creating index file %s' % index_file_path )
index_file = open ( index_file_path , 'w' )
index_file . write ( '\n' . join ( html ) )
index_file . close ( )
# Return the path for cleaning up later
return index_file_path


signature:
def _create_index_files ( root_dir , force_no_processing = False ) :
    """Crawl the root directory downwards , generating an index HTML file in each
 directory on the way down .
 @ param { String } root _ dir - The top level directory to crawl down from . In
 normal usage , this will be ' . ' .
 @ param { Boolean = False } force _ no _ processing - If True , do not attempt to
 actually process thumbnails , PIL images or anything . Simply index
 < img > tags with original file src attributes .
 @ return { [ String ] } Full file paths of all created files ."""

implementation:
# Initialise list of created file paths to build up as we make them
created_files = [ ]
# Walk the root dir downwards , creating index files as we go
for here , dirs , files in os . walk ( root_dir ) :
    print ( 'Processing %s' % here )
    # Sort the subdirectories by name
    dirs = sorted ( dirs )
    # Get image files - all files in the directory matching IMAGE _ FILE _ REGEX
    image_files = [ f for f in files if re . match ( IMAGE_FILE_REGEX , f ) ]
    # Sort the image files by name
    image_files = sorted ( image_files )
    # Create this directory ' s index file and add its name to the created
    # files list
    created_files . append ( _create_index_file ( root_dir , here , image_files , dirs , force_no_processing ) )
# Return the list of created files
return created_files


signature:
def _get_image_from_file ( dir_path , image_file ) :
    """Get an instance of PIL . Image from the given file .
 @ param { String } dir _ path - The directory containing the image file
 @ param { String } image _ file - The filename of the image file within dir _ path
 @ return { PIL . Image } An instance of the image file as a PIL Image , or None
 if the functionality is not available . This could be because PIL is not
 present , or because it can ' t process the given file type ."""

implementation:
# Save ourselves the effort if PIL is not present , and return None now
if not PIL_ENABLED :
    return None
# Put together full path
path = os . path . join ( dir_path , image_file )
# Try to read the image
img = None
try :
    img = Image . open ( path )
except IOError as exptn :
    print ( 'Error loading image file %s: %s' % ( path , exptn ) )
# Return image or None
return img


signature:
def _get_image_link_target_from_file ( dir_path , image_file , force_no_processing = False ) :
    """Get the value to be used as the href for links from thumbnail images . For
 most image formats this will simply be the image file name itself . However ,
 some image formats ( tif ) are not natively displayable by many browsers and
 therefore we must link to image data in another format .
 @ param { String } dir _ path - The directory containing the image file
 @ param { String } image _ file - The filename of the image file within dir _ path
 @ param { Boolean = False } force _ no _ processing - If True , do not attempt to
 actually process a thumbnail , PIL image or anything . Simply return the
 image filename as src .
 @ return { String } The href to use ."""

implementation:
# If we ' ve specified to force no processing , just return the image filename
if force_no_processing :
    return image_file
# First try to get an image
img = _get_image_from_file ( dir_path , image_file )
# If format is directly displayable in - browser , just return the filename
# Else , we need to return a full - sized chunk of displayable image data
if img . format . lower ( ) in [ 'tif' , 'tiff' ] :
    return _get_image_src_from_file ( dir_path , image_file , force_no_processing )
return image_file


signature:
def _get_image_src_from_file ( dir_path , image_file , force_no_processing = False ) :
    """Get base - 64 encoded data as a string for the given image file ' s full image ,
 for use directly in HTML < img > tags , or a path to the original if image
 scaling is not supported .
 This is a full - sized version of _ get _ thumbnail _ src _ from _ file , for use in
 image formats which cannot be displayed directly in - browser , and therefore
 need processed versions even at full size .
 @ param { String } dir _ path - The directory containing the image file
 @ param { String } image _ file - The filename of the image file within dir _ path
 @ param { Boolean = False } force _ no _ processing - If True , do not attempt to
 actually process a thumbnail , PIL image or anything . Simply return the
 image filename as src .
 @ return { String } The base - 64 encoded image data string , or path to the file
 itself if not supported ."""

implementation:
# If we ' ve specified to force no processing , just return the image filename
if force_no_processing :
    if image_file . endswith ( 'tif' ) or image_file . endswith ( 'tiff' ) :
        return UNSUPPORTED_IMAGE_TYPE_DATA
    return image_file
# First try to get an image
img = _get_image_from_file ( dir_path , image_file )
return _get_src_from_image ( img , image_file )


signature:
def _get_src_from_image ( img , fallback_image_file ) :
    """Get base - 64 encoded data as a string for the given image . Fallback to return
 fallback _ image _ file if cannot get the image data or img is None .
 @ param { Image } img - The PIL Image to get src data for
 @ param { String } fallback _ image _ file - The filename of the image file ,
 to be used when image data capture fails
 @ return { String } The base - 64 encoded image data string , or path to the file
 itself if not supported ."""

implementation:
# If the image is None , then we can ' t process , so we should return the
# path to the file itself
if img is None :
    return fallback_image_file
# Target format should be the same as the original image format , unless it ' s
# a TIF / TIFF , which can ' t be displayed by most browsers ; we convert these
# to jpeg
target_format = img . format
if target_format . lower ( ) in [ 'tif' , 'tiff' ] :
    target_format = 'JPEG'
# If we have an actual Image , great - put together the base64 image string
try :
    bytesio = io . BytesIO ( )
    img . save ( bytesio , target_format )
    byte_value = bytesio . getvalue ( )
    b64 = base64 . b64encode ( byte_value )
    return 'data:image/%s;base64,%s' % ( target_format . lower ( ) , b64 )
except IOError as exptn :
    print ( 'IOError while saving image bytes: %s' % exptn )
    return fallback_image_file


signature:
def _get_thumbnail_image_from_file ( dir_path , image_file ) :
    """Get a PIL . Image from the given image file which has been scaled down to
 THUMBNAIL _ WIDTH wide .
 @ param { String } dir _ path - The directory containing the image file
 @ param { String } image _ file - The filename of the image file within dir _ path
 @ return { PIL . Image } An instance of the thumbnail as a PIL Image , or None
 if the functionality is not available . See _ get _ image _ from _ file for
 details ."""

implementation:
# Get image
img = _get_image_from_file ( dir_path , image_file )
# If it ' s not supported , exit now
if img is None :
    return None
if img . format . lower ( ) == 'gif' :
    return None
# Get image dimensions
img_width , img_height = img . size
# We need to perform a resize - first , work out the scale ratio to take the
# image width to THUMBNAIL _ WIDTH ( THUMBNAIL _ WIDTH : img _ width ratio )
scale_ratio = THUMBNAIL_WIDTH / float ( img_width )
# Work out target image height based on the scale ratio
target_height = int ( scale_ratio * img_height )
# Perform the resize
try :
    img . thumbnail ( ( THUMBNAIL_WIDTH , target_height ) , resample = RESAMPLE )
except IOError as exptn :
    print ( 'WARNING: IOError when thumbnailing %s/%s: %s' % ( dir_path , image_file , exptn ) )
    return None
# Return the resized image
return img


signature:
def _get_thumbnail_src_from_file ( dir_path , image_file , force_no_processing = False ) :
    """Get base - 64 encoded data as a string for the given image file ' s thumbnail ,
 for use directly in HTML < img > tags , or a path to the original if image
 scaling is not supported .
 @ param { String } dir _ path - The directory containing the image file
 @ param { String } image _ file - The filename of the image file within dir _ path
 @ param { Boolean = False } force _ no _ processing - If True , do not attempt to
 actually process a thumbnail , PIL image or anything . Simply return the
 image filename as src .
 @ return { String } The base - 64 encoded image data string , or path to the file
 itself if not supported ."""

implementation:
# If we ' ve specified to force no processing , just return the image filename
if force_no_processing :
    if image_file . endswith ( 'tif' ) or image_file . endswith ( 'tiff' ) :
        return UNSUPPORTED_IMAGE_TYPE_DATA
    return image_file
# First try to get a thumbnail image
img = _get_thumbnail_image_from_file ( dir_path , image_file )
return _get_src_from_image ( img , image_file )


signature:
def _run_server ( ) :
    """Run the image server . This is blocking . Will handle user KeyboardInterrupt
 and other exceptions appropriately and return control once the server is
 stopped .
 @ return { None }"""

implementation:
# Get the port to run on
port = _get_server_port ( )
# Configure allow _ reuse _ address to make re - runs of the script less painful -
# if this is not True then waiting for the address to be freed after the
# last run can block a subsequent run
SocketServer . TCPServer . allow_reuse_address = True
# Create the server instance
server = SocketServer . TCPServer ( ( '' , port ) , SimpleHTTPServer . SimpleHTTPRequestHandler )
# Print out before actually running the server ( cheeky / optimistic , however
# you want to look at it )
print ( 'Your images are at http://127.0.0.1:%d/%s' % ( port , INDEX_FILE_NAME ) )
# Try to run the server
try : # Run it - this call blocks until the server is killed
    server . serve_forever ( )
except KeyboardInterrupt : # This is the expected way of the server being killed , since imageMe is
# intended for ad - hoc running from command line
    print ( 'User interrupted, stopping' )
except Exception as exptn : # Catch everything else - this will handle shutdowns via other signals
# and faults actually starting the server in the first place
    print ( exptn )
    print ( 'Unhandled exception in server, stopping' )


signature:
def serve_dir ( dir_path ) :
    """Generate indexes and run server from the given directory downwards .
 @ param { String } dir _ path - The directory path ( absolute , or relative to CWD )
 @ return { None }"""

implementation:
# Create index files , and store the list of their paths for cleanup later
# This time , force no processing - this gives us a fast first - pass in terms
# of page generation , but potentially slow serving for large image files
print ( 'Performing first pass index file generation' )
created_files = _create_index_files ( dir_path , True )
if ( PIL_ENABLED ) : # If PIL is enabled , we ' d like to process the HTML indexes to include
# generated thumbnails - this slows down generation so we don ' t do it
# first time around , but now we ' re serving it ' s good to do in the
# background
    print ( 'Performing PIL-enchanced optimised index file generation in background' )
    background_indexer = BackgroundIndexFileGenerator ( dir_path )
    background_indexer . run ( )
# Run the server in the current location - this blocks until it ' s stopped
_run_server ( )
# Clean up the index files created earlier so we don ' t make a mess of
# the image directories
_clean_up ( created_files )


signature:
def modulename ( cls , depth = 1 ) :
    """get caller ' s _ _ name _ _"""

implementation:
depth += cls . extra_depth
frame = sys . _getframe ( depth )
return frame . f_globals [ '__name__' ]


signature:
def deco_optional ( decorator ) :
    """optional argument 를 포함하는 decorator를 만드는 decorator"""

implementation:
@ functools . wraps ( decorator )
def dispatcher ( * args , ** kwargs ) :
    one_arg = len ( args ) == 1 and not kwargs
    if one_arg and inspect . isfunction ( args [ 0 ] ) :
        decor_obj = decorator ( )
        return decor_obj ( args [ 0 ] )
    else :
        return decorator ( * args , ** kwargs )
return dispatcher


signature:
def optional ( deco ) :
    """decorator option은 kwargs만 허용
 : param deco :
 : return :"""

implementation:
@ functools . wraps ( deco )
def dispatcher ( * args , ** kwargs ) :
    decorator = deco ( ** kwargs )
    if args :
        assert len ( args ) == 1
        return decorator ( args [ 0 ] )
    else :
        return decorator
return dispatcher


signature:
def bindargs ( fun , * argsbind , ** kwbind ) :
    """_ = bind . placeholder # unbound placeholder ( arg )
 f = bind ( fun , _ , _ , arg3 , kw = kw1 , kw2 = kw2 ) , f ( arg1 , arg2)
 : param fun :
 : param argsbind :
 : param kwbind :
 : return :"""

implementation:
assert argsbind
argsb = list ( argsbind )
iargs = [ i for i in range ( len ( argsbind ) ) if argsbind [ i ] is bind . placeholder ]
# iargs = [ a is bind . placeholder for a in argsbind ]
@ functools . wraps ( fun )
def wrapped ( * args , ** kwargs ) :
    kws = kwbind . copy ( )
    args_this = [ a for a in argsb ]
    for i , arg in zip ( iargs , args ) :
        args_this [ i ] = arg
    args_this . extend ( args [ len ( iargs ) : ] )
    # kwargs . update ( kwbind )
    kws . update ( kwargs )
    # return fun ( * argsb , * * kws )
    return fun ( * args_this , ** kws )
return wrapped


signature:
def bindkw ( fun , ** kwbind ) :
    """kwarg 바인딩된 함수 return .
 ex )
 def fun ( opt1 , opt2 ) :
 print opt1 , opt2
 f = bind ( fun , opt1 = 2 , opt2 = 3)
 : param function fun :
 : param kwbind :
 : return : function"""

implementation:
@ functools . wraps ( fun )
def wrapped ( * args , ** kwargs ) :
    kws = kwbind . copy ( )
    kws . update ( kwargs )
    return fun ( * args , ** kws )
return wrapped


signature:
def default ( fun , ** kwdefault ) :
    """change default value for function
 ex )
 def sample ( a , b = 1 , c = 1 ) :
 print ' from sample : ' , a , b , c
 return a , b , c
 fun = default ( sample , b = 4 , c = 5)
 print fun . default # get default value dictionary
 fun ( 1 ) # print 1 , 5 , 5 and return
 : param fun :
 : param kwdefault :
 : return :"""

implementation:
@ functools . wraps ( fun )
def wrapped ( * args , ** kwargs ) :
    merge = wrapped . default . copy ( )
    merge . update ( kwargs )
    return fun ( * args , ** merge )
wrapped . default = kwdefault
return wrapped


signature:
def setup_once ( initfn ) :
    """call class instance method for initial setup : :
 class B ( object ) :
 def init ( self , a ) :
 print ' init call : ' , a
 @ setup _ once ( init )
 def mycall ( self , a ) :
 print ' real call : ' , a
 b = B ( )
 b . mycall ( 222)
 b . mycall ( 333)
 : param function initfn :
 : return : decorated method"""

implementation:
def wrap ( method ) :
    finit = initfn . __name__
    fnname = method . __name__
    @ functools . wraps ( method )
    def wrapped ( self , * args , ** kwargs ) :
        @ functools . wraps ( method )
        def aftersetup ( * a , ** kw ) :
            return method ( self , * a , ** kw )
        setupfn = getattr ( self , finit )
        setupfn ( * args , ** kwargs )
        res = method ( self , * args , ** kwargs )
        setattr ( self , fnname , aftersetup )
        return res
    return wrapped
return wrap


signature:
def static ( ** kwargs ) :
    """USE carefully ^ ^"""

implementation:
def wrap ( fn ) :
    fn . func_globals [ 'static' ] = fn
    fn . __dict__ . update ( kwargs )
    return fn
return wrap


signature:
def rand_crop ( sz , * imagez ) :
    """random crop
 # assume imagez has same size ( H , W )
 # assume sz is less or equal than size of image
 : param sz : cropped image sz
 : param imagez : imagez
 : return : rand cropped image pairs or function bound to sz"""

implementation:
def _rand_crop ( * imgz ) :
    imsz = imgz [ 0 ] . shape [ : 2 ]
    assert imsz [ 0 ] >= sz [ 0 ] and imsz [ 1 ] >= sz [ 1 ]
    si = np . random . randint ( imsz [ 0 ] - sz [ 0 ] ) if imsz [ 0 ] > sz [ 0 ] else 0
    sj = np . random . randint ( imsz [ 1 ] - sz [ 1 ] ) if imsz [ 1 ] > sz [ 1 ] else 0
    slicei = slice ( si , si + sz [ 0 ] )
    slicej = slice ( sj , sj + sz [ 1 ] )
    outs = tuple ( img [ slicei , slicej ] for img in imgz )
    return tuple_or_not ( * outs )
return _rand_crop ( * imagez ) if imagez else _rand_crop


signature:
def rand_rotate ( anglerange , * imagez ) :
    """: param anglerange :
 : param imagez :
 : return :"""

implementation:
r = float ( anglerange [ 1 ] - anglerange [ 0 ] )
s = anglerange [ 0 ]
def _rand_rotate ( * imgz ) :
    angle = np . random . random ( 1 ) [ 0 ] * r + s
    out = tuple ( rotate ( img , angle ) for img in imgz )
    return tuple_or_not ( out )
return _rand_rotate ( * imagez ) if imagez else _rand_rotate


signature:
def blend_discrete ( images , depthmask , depth = None ) :
    """depthmask : shape of [ batch , h , w ]"""

implementation:
imshape = images . shape
depth = depth or images . shape [ 3 ]
blend = np . empty ( shape = ( imshape [ 0 ] , imshape [ 1 ] , imshape [ 2 ] ) )
for d in range ( depth ) :
    imask = ( depthmask == d )
    channel = images [ ... , d ]
    blend [ imask ] = channel [ imask ]
return np . expand_dims ( blend , axis = - 1 )


signature:
def rand_blend_mask ( shape , rand = rand . uniform ( - 10 , 10 ) , ** kwargs ) :
    """random blending masks"""

implementation:
# batch , channel = shape [ 0 ] , shape [ 3]
z = rand ( shape [ 0 ] )
# seed
noise = snoise2dz ( ( shape [ 1 ] , shape [ 2 ] ) , z , ** kwargs )
return noise


signature:
def snoise2dvec ( size , * params , ** kwargs ) : # , vlacunarity ) :
    """vector parameters
 : param size :
 : param vz :
 : param vscale :
 : param voctave :
 : param vpersistence :
 : param vlacunarity :
 : return :"""

implementation:
data = ( snoise2d ( size , * p , ** kwargs ) for p in zip ( * params ) )
# , vlacunarity ) )
return np . stack ( data , 0 )


signature:
def snoise2d ( size , z = 0.0 , scale = 0.05 , octaves = 1 , persistence = 0.25 , lacunarity = 2.0 ) :
    """z value as like a seed"""

implementation:
import noise
data = np . empty ( size , dtype = 'float32' )
for y in range ( size [ 0 ] ) :
    for x in range ( size [ 1 ] ) :
        v = noise . snoise3 ( x * scale , y * scale , z , octaves = octaves , persistence = persistence , lacunarity = lacunarity )
        data [ x , y ] = v
data = data * 0.5 + 0.5
if __debug__ :
    assert data . min ( ) >= 0. and data . max ( ) <= 1.0
return data


signature:
def snoise2dz ( size , z , scale = 0.05 , octaves = 1 , persistence = 0.25 , lacunarity = 2.0 ) :
    """z as seeds
 scale이 작을 수록 패턴이 커지는 효과"""

implementation:
import noise
z_l = len ( z )
data = np . empty ( ( z_l , size [ 0 ] , size [ 1 ] ) , dtype = 'float32' )
for iz in range ( z_l ) :
    zvalue = z [ iz ]
    for y in range ( size [ 0 ] ) :
        for x in range ( size [ 1 ] ) :
            v = noise . snoise3 ( x * scale , y * scale , zvalue , octaves = octaves , persistence = persistence , lacunarity = lacunarity )
            data [ iz , y , x ] = v
data = data * 0.5 + 0.5
if __debug__ :
    assert data . min ( ) >= 0. and data . max ( ) <= 1.0
return data


signature:
def rand_brightness ( imagez , scale = 1.0 , randfun = rand . normal ( 0. , .1 ) , clamp = ( 0. , 1. ) ) :
    """: param images :
 : param scale : scale for random value
 : param randfun : any randfun binding except shape
 : param clamp : clamping range
 : return :"""

implementation:
l , h = clamp
r = randfun ( ( imagez [ 0 ] . shape [ 0 ] , 1 , 1 , 1 ) ) * scale
def apply ( im ) :
    im += r
    im [ im < l ] = l
    im [ im > h ] = h
    return im
return tuple ( map ( apply , imagez ) )


signature:
def elastic_transform ( im , alpha = 0.5 , sigma = 0.2 , affine_sigma = 1. ) :
    """Based on https : / / gist . github . com / erniejunior / 601cdf56d2b424757de5
 elastic deformation of images as described in [ Simard2003]"""

implementation:
# fixme : not implemented for multi channel !
import cv2
islist = isinstance ( im , ( tuple , list ) )
ima = im [ 0 ] if islist else im
# image shape
shape = ima . shape
shape_size = shape [ : 2 ]
# Random affine transform
center_square = np . float32 ( shape_size ) // 2
square_size = min ( shape_size ) // 3
pts1 = np . float32 ( [ center_square + square_size , [ center_square [ 0 ] + square_size , center_square [ 1 ] - square_size ] , center_square - square_size ] )
pts2 = pts1 + np . random . uniform ( - affine_sigma , affine_sigma , size = pts1 . shape ) . astype ( np . float32 )
M = cv2 . getAffineTransform ( pts1 , pts2 )
if islist :
    res = [ ]
    for i , ima in enumerate ( im ) :
        if i == 0 :
            res . append ( cv2 . warpAffine ( ima , M , shape_size [ : : - 1 ] , borderMode = cv2 . BORDER_REFLECT_101 ) )
        else :
            res . append ( cv2 . warpAffine ( ima , M , shape_size [ : : - 1 ] ) )
    im = res
else :
    ima = cv2 . warpAffine ( ima , M , shape_size [ : : - 1 ] , borderMode = cv2 . BORDER_REFLECT_101 )
    # ima = cv2 . warpAffine ( ima , M , shape _ size [ : : - 1 ] )
# fast gaussian filter
blur_size = int ( 4 * sigma ) | 1
dx = cv2 . GaussianBlur ( ( np . random . rand ( * shape ) * 2 - 1 ) , ksize = ( blur_size , blur_size ) , sigmaX = sigma ) * alpha
dy = cv2 . GaussianBlur ( ( np . random . rand ( * shape ) * 2 - 1 ) , ksize = ( blur_size , blur_size ) , sigmaX = sigma ) * alpha
# remap
x , y = np . meshgrid ( np . arange ( shape [ 1 ] ) , np . arange ( shape [ 0 ] ) )
map_x , map_y = ( y + dy ) . astype ( 'float32' ) , ( x + dx ) . astype ( 'float32' )
def remap ( data ) :
    r = cv2 . remap ( data , map_y , map_x , interpolation = cv2 . INTER_LINEAR , borderMode = cv2 . BORDER_REFLECT_101 )
    return r [ ... , np . newaxis ]
if islist :
    return tuple ( [ remap ( ima ) for ima in im ] )
else :
    return remap ( ima )


signature:
def rotate_crop ( centerij , sz , angle , img = None , mode = 'constant' , ** kwargs ) :
    """rotate and crop
 if no img , then return crop function
 : param centerij :
 : param sz :
 : param angle :
 : param img : [ h , w , d ]
 : param mode : padding option
 : return : cropped image or function"""

implementation:
# crop enough size ( 2 * sqrt ( sum ( sz ^ 2 ) )
# rotate
from skimage import transform
sz = np . array ( sz )
crop_half = int ( np . ceil ( np . sqrt ( np . square ( sz ) . sum ( ) ) ) )
if centerij [ 0 ] >= crop_half or centerij [ 1 ] >= crop_half :
    raise NotImplementedError
slicei = slice ( centerij [ 0 ] - crop_half , centerij [ 0 ] + crop_half )
slicej = slice ( centerij [ 1 ] - crop_half , centerij [ 1 ] + crop_half )
# slicei = ( centerij [ 0 ] - crop _ half , centerij [ 0 ] + crop _ half )
# slicej = ( centerij [ 1 ] - crop _ half , centerij [ 1 ] + crop _ half )
# def _ pad _ if _ need ( im ) :
# imshape = im . shape
# pad _ need = slicei [ 0 ] < 0 or slicej [ 0 ] < 0 or slice
# padwidth = [ ( slicei [ 0 ] , np . maximum ( 0 , slicei [ 1 ] - imshape [ 0 ] ) ) ,
# ( slicej [ 0 ] , np . maximum ( 0 , slicej [ 1 ] - imshape [ 1 ] ) ) ]
def _rotate_cropcenter ( im ) :
    enoughcrop = im [ slicei , slicej ]
    rotated = transform . rotate ( enoughcrop , angle , resize = False , preserve_range = True , mode = mode , ** kwargs )
    return cropcenter ( sz , rotated )
if img is not None :
    return _rotate_cropcenter ( img )
return _rotate_cropcenter


signature:
def crop ( img , center , sz , mode = 'constant' ) :
    """crop sz from ij as center
 : param img :
 : param center : ij
 : param sz :
 : param mode :
 : return :"""

implementation:
center = np . array ( center )
sz = np . array ( sz )
istart = ( center - sz / 2. ) . astype ( 'int32' )
iend = istart + sz
imsz = img . shape [ : 2 ]
if np . any ( istart < 0 ) or np . any ( iend > imsz ) : # padding
    padwidth = [ ( np . minimum ( 0 , istart [ 0 ] ) , np . maximum ( 0 , iend [ 0 ] - imsz [ 0 ] ) ) , ( np . minimum ( 0 , istart [ 1 ] ) , np . maximum ( 0 , iend [ 1 ] - imsz [ 1 ] ) ) ]
    padwidth += [ ( 0 , 0 ) ] * ( len ( img . shape ) - 2 )
    img = np . pad ( img , padwidth , mode = mode )
    istart = ( np . maximum ( 0 , istart [ 0 ] ) , np . maximum ( 0 , istart [ 1 ] ) )
    return img [ istart [ 0 ] : istart [ 0 ] + sz [ 0 ] , istart [ 1 ] : istart [ 1 ] + sz [ 1 ] ]
return img [ istart [ 0 ] : iend [ 0 ] , istart [ 1 ] : iend [ 1 ] ]


signature:
def cropcenter ( sz , img = None ) :
    """if no img , then return crop function
 : param sz :
 : param img :
 : return :"""

implementation:
l = len ( sz )
sz = np . array ( sz )
def wrapped ( im ) :
    imsz = np . array ( im . shape )
    s = ( imsz [ : l ] - sz ) / 2
    # start index
    to = s + sz
    # end index
    # img [ s [ 0 ] : to [ 0 ] , . . . s [ end ] : to [ end ] , . . . ]
    slices = [ slice ( s , e ) for s , e in zip ( s , to ) ]
    return im [ slices ]
if img is not None :
    return wrapped ( img )
return wrapped


signature:
def pad_if_need ( sz_atleast , img , mode = 'constant' ) : # fixme : function or . . . .
    """pad img if need to guarantee minumum size
 : param sz _ atleast : [ H , W ] at least
 : param img : image np . array [ H , W , . . . ]
 : param mode : str , padding mode
 : return : padded image or asis if enought size"""

implementation:
# sz _ atleast = np . asarray ( sz _ atleast )
imsz = img . shape [ : 2 ]
# assume img [ H , W , . . . ]
padneed = np . asarray ( ( sz_atleast [ 0 ] - imsz [ 0 ] , sz_atleast [ 1 ] - imsz [ 1 ] ) )
if np . any ( padneed > 0 ) : # need padding
    padding = np . zeros ( ( img . ndim , 2 ) , dtype = 'int16' )
    padneed = np . maximum ( padneed , 0 )
    padding [ : 2 , 0 ] = padneed / 2
    padding [ : 2 , 1 ] = padneed - padneed / 2
    img = np . pad ( img , padding , mode = mode )
return img


signature:
def canny ( img , threshold1 = 255 / 3 , threshold2 = 255 , ** kwargs ) :
    """canny edge"""

implementation:
import cv2
# edges = None , apertureSize = None , L2gradient = None
if img . ndim <= 3 :
    edge = cv2 . Canny ( img , threshold1 , threshold2 , ** kwargs )
    if edge . ndim == 2 :
        edge = np . expand_dims ( edge , 2 )
elif img . ndim == 4 : # batch
    edge = np . asarray ( [ cv2 . Canny ( i , threshold1 , threshold2 , ** kwargs ) for i in img ] )
    if edge . ndim == 3 :
        edge = np . expand_dims ( edge , 3 )
else :
    raise ValueError ( 'above 5d?' )
return edge


signature:
def guess_package_path ( searchfrom ) :
    """package path . return None if failed to guess"""

implementation:
from snipy . io import fileutil
current = searchfrom + '/'
init_found = False
pack_found = False
while not init_found and current != '/' :
    current = os . path . dirname ( current )
    initfile = os . path . join ( current , '__init__.py' )
    init_found = os . path . exists ( initfile )
if not init_found : # search for breadth
    searchfrom = dirname ( searchfrom )
    for folder in fileutil . listfolder ( searchfrom ) :
        current = os . path . join ( searchfrom , folder )
        initfile = os . path . join ( current , '__init__.py' )
        init_found = os . path . exists ( initfile )
        if init_found :
            break
while init_found :
    current = os . path . dirname ( current )
    initfile = os . path . join ( current , '__init__.py' )
    init_found = os . path . exists ( initfile )
    pack_found = not init_found
return current if pack_found else None


signature:
def find_package_path ( searchfrom ) :
    """package path . return None if failed to guess"""

implementation:
current = searchfrom + '/'
init_found = False
pack_found = False
while not init_found and current != '/' :
    current = os . path . dirname ( current )
    initfile = os . path . join ( current , '__init__.py' )
    init_found = os . path . exists ( initfile )
while init_found :
    current = os . path . dirname ( current )
    initfile = os . path . join ( current , '__init__.py' )
    init_found = os . path . exists ( initfile )
    pack_found = not init_found
return current if pack_found else None


signature:
def append_this_package_path ( depth = 1 ) :
    """this _ package . py 에서 사용
 import snipy . this _ package"""

implementation:
from . caller import caller
logg . debug ( 'caller module %s' , caller . modulename ( depth + 1 ) )
c = caller . abspath ( depth + 1 )
logg . debug ( 'caller path %s' , c )
p = guess_package_path ( dirname ( c ) )
if p :
    logg . debug ( 'appending sys path %s' , p )
    append_sys_path ( p )
else : # do some logging
    logg . debug ( 'failed to guess package path for: %s' , c )


signature:
def flows ( args ) :
    """todo : add some example
 : param args :
 : return :"""

implementation:
def flow_if_not ( fun ) : # t = type ( fun )
    if isinstance ( fun , iterator ) :
        return fun
    elif isinstance ( fun , type ) and 'itertools' in str ( fun . __class__ ) :
        return fun
    else :
        try :
            return flow ( fun )
        except AttributeError : # generator object has no attribute ' _ _ module _ _ '
            return fun
return FlowList ( map ( flow_if_not , args ) )


signature:
def forever ( it ) :
    """forever
 todo : add example"""

implementation:
while True : # generator 두번쨰 iteration 무한 루프 방지
    i = iter ( it )
    try :
        yield i . next ( )
    except StopIteration :
        raise StopIteration
    while True :
        try :
            yield i . next ( )
        except StopIteration :
            break


signature:
def ibatch ( size , iterable = None , rest = False ) :
    """add example
 : param size :
 : param iterable :
 : param rest :
 : return :"""

implementation:
@ iterflow
def exact_size ( it ) :
    it = iter ( it )
    while True :
        yield [ it . next ( ) for _ in xrange ( size ) ]
@ iterflow
def at_most ( it ) :
    it = iter ( it )
    while True :
        data = [ ]
        for _ in xrange ( size ) :
            try :
                data . append ( it . next ( ) )
            except StopIteration :
                if data :
                    yield data
                raise StopIteration
        yield data
ibatchit = at_most if rest else exact_size
return ibatchit if iterable is None else ibatchit ( iterable )


signature:
def batchzip ( size , iterable = None , rest = False ) :
    """todo : add example
 : param size :
 : param iterable :
 : param rest :
 : return :"""

implementation:
fn = ibatch ( size , rest = rest ) >> zipflow
return fn if iterable is None else fn ( iterable )


signature:
def batchstack ( size , iterable = None , rest = False ) :
    """todo : add example
 : param size :
 : param iterable :
 : param rest :
 : return :"""

implementation:
def stack ( data ) :
    import numpy as np
    return map ( np . vstack , data )
fn = batchzip ( size , rest = rest ) >> flow ( stack )
return fn if iterable is None else fn ( iterable )


signature:
def shuffle ( qsize = 1024 , iterable = None ) :
    """add example
 : param qsize :
 : param iterable :
 : return :"""

implementation:
@ iterflow
def shuffleit ( it ) :
    from random import randrange
    q = [ ]
    for i , d in enumerate ( it ) :
        q . insert ( randrange ( 0 , len ( q ) + 1 ) , d )
        if i < qsize :
            continue
        yield q . pop ( randrange ( 0 , len ( q ) ) )
    while q :
        yield q . pop ( randrange ( 0 , len ( q ) ) )
return shuffleit if iterable is None else shuffleit ( iterable )


signature:
def to_permutation_matrix ( matches ) :
    """Converts a permutation into a permutation matrix .
 ` matches ` is a dictionary whose keys are vertices and whose values are
 partners . For each vertex ` ` u ` ` and ` ` v ` ` , entry ( ` ` u ` ` , ` ` v ` ` ) in the
 returned matrix will be a ` ` 1 ` ` if and only if ` ` matches [ u ] = = v ` ` .
 Pre - condition : ` matches ` must be a permutation on an initial subset of the
 natural numbers .
 Returns a permutation matrix as a square NumPy array ."""

implementation:
n = len ( matches )
P = np . zeros ( ( n , n ) )
# This is a cleverer way of doing
# for ( u , v ) in matches . items ( ) :
# P [ u , v ] = 1
P [ list ( zip ( * ( matches . items ( ) ) ) ) ] = 1
return P


signature:
def four_blocks ( topleft , topright , bottomleft , bottomright ) :
    """Convenience function that creates a block matrix with the specified
 blocks .
 Each argument must be a NumPy matrix . The two top matrices must have the
 same number of rows , as must the two bottom matrices . The two left matrices
 must have the same number of columns , as must the two right matrices ."""

implementation: return vstack ( hstack ( topleft , topright ) , hstack ( bottomleft , bottomright ) )


signature:
def to_bipartite_matrix ( A ) :
    """Returns the adjacency matrix of a bipartite graph whose biadjacency
 matrix is ` A ` .
 ` A ` must be a NumPy array .
 If ` A ` has * * m * * rows and * * n * * columns , then the returned matrix has * * m +
 n * * rows and columns ."""

implementation:
m , n = A . shape
return four_blocks ( zeros ( m , m ) , A , A . T , zeros ( n , n ) )


signature:
def to_pattern_matrix ( D ) :
    """Returns the Boolean matrix in the same shape as ` D ` with ones exactly
 where there are nonzero entries in ` D ` .
 ` D ` must be a NumPy array ."""

implementation:
result = np . zeros_like ( D )
# This is a cleverer way of doing
# for ( u , v ) in zip ( * ( D . nonzero ( ) ) ) :
# result [ u , v ] = 1
result [ D . nonzero ( ) ] = 1
return result


signature:
def birkhoff_von_neumann_decomposition ( D ) :
    """Returns the Birkhoff - - von Neumann decomposition of the doubly
 stochastic matrix ` D ` .
 The input ` D ` must be a square NumPy array representing a doubly
 stochastic matrix ( that is , a matrix whose entries are nonnegative
 reals and whose row sums and column sums are all 1 ) . Each doubly
 stochastic matrix is a convex combination of at most ` ` n * * 2 ` `
 permutation matrices , where ` ` n ` ` is the dimension of the input
 array .
 The returned value is a list of pairs whose length is at most ` ` n * *
 2 ` ` . In each pair , the first element is a real number in the interval * * ( 0,
 1 ] * * and the second element is a NumPy array representing a permutation
 matrix . This represents the doubly stochastic matrix as a convex
 combination of the permutation matrices .
 The input matrix may also be a scalar multiple of a doubly
 stochastic matrix , in which case the row sums and column sums must
 each be * c * , for some positive real number * c * . This may be useful
 in avoiding precision issues : given a doubly stochastic matrix that
 will have many entries close to one , multiply it by a large positive
 integer . The returned permutation matrices will be the same
 regardless of whether the given matrix is a doubly stochastic matrix
 or a scalar multiple of a doubly stochastic matrix , but in the
 latter case , the coefficients will all be scaled by the appropriate
 scalar multiple , and their sum will be that scalar instead of one .
 For example : :
 > > > import numpy as np
 > > > from birkhoff import birkhoff _ von _ neumann _ decomposition as decomp
 > > > D = np . ones ( ( 2 , 2 ) )
 > > > zipped _ pairs = decomp ( D )
 > > > coefficients , permutations = zip ( * zipped _ pairs )
 > > > coefficients
 (1.0 , 1.0)
 > > > permutations [ 0]
 array ( [ [ 1 . , 0 . ] ,
 [ 0 . , 1 . ] ] )
 > > > permutations [ 1]
 array ( [ [ 0 . , 1 . ] ,
 [ 1 . , 0 . ] ] )
 > > > zipped _ pairs = decomp ( D / 2 ) # halve each value in the matrix
 > > > coefficients , permutations = zip ( * zipped _ pairs )
 > > > coefficients # will be half as large as before
 (0.5 , 0.5)
 > > > permutations [ 0 ] # will be the same as before
 array ( [ [ 1 . , 0 . ] ,
 [ 0 . , 1 . ] ] )
 > > > permutations [ 1]
 array ( [ [ 0 . , 1 . ] ,
 [ 1 . , 0 . ] ] )
 The returned list of pairs is given in the order computed by the algorithm
 ( so in particular they are not sorted in any way ) ."""

implementation:
m , n = D . shape
if m != n :
    raise ValueError ( 'Input matrix must be square ({} x {})' . format ( m , n ) )
indices = list ( itertools . product ( range ( m ) , range ( n ) ) )
# These two lists will store the result as we build it up each iteration .
coefficients = [ ]
permutations = [ ]
# Create a copy of D so that we don ' t modify it directly . Cast the
# entries of the matrix to floating point numbers , regardless of
# whether they were integers .
S = D . astype ( 'float' )
while not np . all ( S == 0 ) : # Create an undirected graph whose adjacency matrix contains a 1
# exactly where the matrix S has a nonzero entry .
    W = to_pattern_matrix ( S )
    # Construct the bipartite graph whose left and right vertices both
    # represent the vertex set of the pattern graph ( whose adjacency matrix
    # is ` ` W ` ` ) .
    X = to_bipartite_matrix ( W )
    # Convert the matrix of a bipartite graph into a NetworkX graph object .
    G = from_numpy_matrix ( X )
    # Compute a perfect matching for this graph . The dictionary ` M ` has one
    # entry for each matched vertex ( in both the left and the right vertex
    # sets ) , and the corresponding value is its partner .
    # The bipartite maximum matching algorithm requires specifying
    # the left set of nodes in the bipartite graph . By construction ,
    # the left set of nodes is { 0 , . . . , n - 1 } and the right set is
    # { n , . . . , 2n - 1 } ; see ` to _ bipartite _ matrix ( ) ` .
    left_nodes = range ( n )
    M = maximum_matching ( G , left_nodes )
    # However , since we have both a left vertex set and a right vertex set ,
    # each representing the original vertex set of the pattern graph
    # ( ` ` W ` ` ) , we need to convert any vertex greater than ` ` n ` ` to its
    # original vertex number . To do this ,
    # - ignore any keys greater than ` ` n ` ` , since they are already
    # covered by earlier key / value pairs ,
    # - ensure that all values are less than ` ` n ` ` .
    M = { u : v % n for u , v in M . items ( ) if u < n }
    # Convert that perfect matching to a permutation matrix .
    P = to_permutation_matrix ( M )
    # Get the smallest entry of S corresponding to the 1 entries in the
    # permutation matrix .
    q = min ( S [ i , j ] for ( i , j ) in indices if P [ i , j ] == 1 )
    # Store the coefficient and the permutation matrix for later .
    coefficients . append ( q )
    permutations . append ( P )
    # Subtract P scaled by q . After this subtraction , S has a zero entry
    # where the value q used to live .
    S -= q * P
    # PRECISION ISSUE : There seems to be a problem with floating point
    # precision here , so we need to round down to 0 any entry that is very
    # small .
    S [ np . abs ( S ) < TOLERANCE ] = 0.0
return list ( zip ( coefficients , permutations ) )


signature:
def bump_version ( version , which = None ) :
    """Returns the result of incrementing ` version ` .
 If ` which ` is not specified , the " patch " part of the version number will be
 incremented . If ` which ` is specified , it must be ` ` ' major ' ` ` , ` ` ' minor ' ` ` ,
 or ` ` ' patch ' ` ` . If it is one of these three strings , the corresponding part
 of the version number will be incremented instead of the patch number .
 Returns a string representing the next version number .
 Example : :
 > > > bump _ version ( ' 2.7.1 ' )
 '2.7.2'
 > > > bump _ version ( ' 2.7.1 ' , ' minor ' )
 '2.8.0'
 > > > bump _ version ( ' 2.7.1 ' , ' major ' )
 '3.0.0'"""

implementation:
try :
    parts = [ int ( n ) for n in version . split ( '.' ) ]
except ValueError :
    fail ( 'Current version is not numeric' )
if len ( parts ) != 3 :
    fail ( 'Current version is not semantic versioning' )
# Determine where to increment the version number
PARTS = { 'major' : 0 , 'minor' : 1 , 'patch' : 2 }
index = PARTS [ which ] if which in PARTS else 2
# Increment the version number at that index and set the subsequent parts
# to 0.
before , middle , after = parts [ : index ] , parts [ index ] , parts [ index + 1 : ]
middle += 1
return '.' . join ( str ( n ) for n in before + [ middle ] + after )


signature:
def get_version ( filename , pattern ) :
    """Gets the current version from the specified file .
 This function assumes the file includes a string of the form : :
 < pattern > = < version >"""

implementation:
with open ( filename ) as f :
    match = re . search ( r"^(\s*%s\s*=\s*')(.+?)(')(?sm)" % pattern , f . read ( ) )
if match :
    before , version , after = match . groups ( )
    return version
fail ( 'Could not find {} in {}' . format ( pattern , filename ) )


signature:
def fail ( message = None , exit_status = None ) :
    """Prints the specified message and exits the program with the specified
 exit status ."""

implementation:
print ( 'Error:' , message , file = sys . stderr )
sys . exit ( exit_status or 1 )


signature:
def git_tag ( tag ) :
    """Tags the current version ."""

implementation:
print ( 'Tagging "{}"' . format ( tag ) )
msg = '"Released version {}"' . format ( tag )
Popen ( [ 'git' , 'tag' , '-s' , '-m' , msg , tag ] ) . wait ( )


signature:
def initialize ( self , templates_path , global_data ) :
    """initialize with templates ' path
 parameters
 templates _ path str the position of templates directory
 global _ data dict globa data can be got in any templates"""

implementation:
self . env = Environment ( loader = FileSystemLoader ( templates_path ) )
self . env . trim_blocks = True
self . global_data = global_data


signature:
def render ( self , template , ** data ) :
    """Render data with template , return html unicodes .
 parameters
 template str the template ' s filename
 data dict the data to render"""

implementation:
# make a copy and update the copy
dct = self . global_data . copy ( )
dct . update ( data )
try :
    html = self . env . get_template ( template ) . render ( ** dct )
except TemplateNotFound :
    raise JinjaTemplateNotFound
return html


signature:
def render_to ( self , path , template , ** data ) :
    """Render data with template and then write to path"""

implementation:
html = self . render ( template , ** data )
with open ( path , 'w' ) as f :
    f . write ( html . encode ( charset ) )


signature:
def render ( template , ** data ) :
    """shortcut to render data with ` template ` . Just add exception
 catch to ` renderer . render `"""

implementation:
try :
    return renderer . render ( template , ** data )
except JinjaTemplateNotFound as e :
    logger . error ( e . __doc__ + ', Template: %r' % template )
    sys . exit ( e . exit_code )


signature:
def replace_relative_url_to_absolute ( self , content ) :
    """Replace ' . . / ' leaded url with absolute uri ."""

implementation:
p = os . path . join ( os . getcwd ( ) , './src' , '../' )
return content . replace ( '../' , p )


signature:
def get_dataframe ( self ) :
    """Get the DataFrame for this view .
 Defaults to using ` self . dataframe ` .
 This method should always be used rather than accessing ` self . dataframe `
 directly , as ` self . dataframe ` gets evaluated only once , and those results
 are cached for all subsequent requests .
 You may want to override this if you need to provide different
 dataframes depending on the incoming request ."""

implementation:
assert self . dataframe is not None , ( "'%s' should either include a `dataframe` attribute, " "or override the `get_dataframe()` method." % self . __class__ . __name__ )
dataframe = self . dataframe
return dataframe


signature:
def index_row ( self , dataframe ) :
    """Indexes the row based on the request parameters ."""

implementation: return dataframe . loc [ self . kwargs [ self . lookup_url_kwarg ] ] . to_frame ( ) . T


signature:
def get_object ( self ) :
    """Returns the row the view is displaying .
 You may want to override this if you need to provide non - standard
 queryset lookups . Eg if objects are referenced using multiple
 keyword arguments in the url conf ."""

implementation:
dataframe = self . filter_dataframe ( self . get_dataframe ( ) )
assert self . lookup_url_kwarg in self . kwargs , ( 'Expected view %s to be called with a URL keyword argument ' 'named "%s". Fix your URL conf, or set the `.lookup_field` ' 'attribute on the view correctly.' % ( self . __class__ . __name__ , self . lookup_url_kwarg ) )
try :
    obj = self . index_row ( dataframe )
except ( IndexError , KeyError , ValueError ) :
    raise Http404
# May raise a permission denied
self . check_object_permissions ( self . request , obj )
return obj


signature:
def paginator ( self ) :
    """The paginator instance associated with the view , or ` None ` ."""

implementation:
if not hasattr ( self , '_paginator' ) :
    if self . pagination_class is None :
        self . _paginator = None
    else :
        self . _paginator = self . pagination_class ( )
return self . _paginator


signature:
def paginate_dataframe ( self , dataframe ) :
    """Return a single page of results , or ` None ` if pagination is disabled ."""

implementation:
if self . paginator is None :
    return None
return self . paginator . paginate_dataframe ( dataframe , self . request , view = self )


signature:
def parse ( self ) :
    """parse config , return a dict"""

implementation:
if exists ( self . filepath ) :
    content = open ( self . filepath ) . read ( ) . decode ( charset )
else :
    content = ""
try :
    config = toml . loads ( content )
except toml . TomlSyntaxError :
    raise ConfigSyntaxError
return config


signature:
def chunks ( lst , number ) :
    """A generator , split list ` lst ` into ` number ` equal size parts .
 usage : :
 > > > parts = chunks ( range ( 8 ) , 3)
 > > > parts
 < generator object chunks at 0xb73bd964 >
 > > > list ( parts )
 [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] , [ 6 , 7 ] ]"""

implementation:
lst_len = len ( lst )
for i in xrange ( 0 , lst_len , number ) :
    yield lst [ i : i + number ]


signature:
def update_nested_dict ( a , b ) :
    """update nested dict ` a ` with another dict b .
 usage : :
 > > > a = { ' x ' : { ' y ' : 1 } }
 > > > b = { ' x ' : { ' z ' : 2 , ' y ' : 3 } , ' w ' : 4}
 > > > update _ nested _ dict ( a , b )
 { ' x ' : { ' y ' : 3 , ' z ' : 2 } , ' w ' : 4}"""

implementation:
for k , v in b . iteritems ( ) :
    if isinstance ( v , dict ) :
        d = a . setdefault ( k , { } )
        update_nested_dict ( d , v )
    else :
        a [ k ] = v
return a


signature:
def render_to ( path , template , ** data ) :
    """shortcut to render data with ` template ` and then write to ` path ` .
 Just add exception catch to ` renderer . render _ to `"""

implementation:
try :
    renderer . render_to ( path , template , ** data )
except JinjaTemplateNotFound as e :
    logger . error ( e . __doc__ + ', Template: %r' % template )
    sys . exit ( e . exit_code )


signature:
def block_code ( self , text , lang ) :
    """text : unicode text to render"""

implementation:
if not lang :
    return self . _code_no_lexer ( text )
try :
    lexer = get_lexer_by_name ( lang , stripall = True )
except ClassNotFound : # lexer not found , use plain text
    return self . _code_no_lexer ( text )
formatter = HtmlFormatter ( )
return highlight ( text , lexer , formatter )


signature:
def parse ( self , source ) :
    """Parse ascii post source , return dict"""

implementation:
rt , title , title_pic , markdown = libparser . parse ( source )
if rt == - 1 :
    raise SeparatorNotFound
elif rt == - 2 :
    raise PostTitleNotFound
# change to unicode
title , title_pic , markdown = map ( to_unicode , ( title , title_pic , markdown ) )
# render to html
html = self . markdown . render ( markdown )
summary = self . markdown . render ( markdown [ : 200 ] )
return { 'title' : title , 'markdown' : markdown , 'html' : html , 'summary' : summary , 'title_pic' : title_pic }


signature:
def parse_filename ( self , filepath ) :
    """parse post source files name to datetime object"""

implementation:
name = os . path . basename ( filepath ) [ : - src_ext_len ]
try :
    dt = datetime . strptime ( name , "%Y-%m-%d-%H-%M" )
except ValueError :
    raise PostNameInvalid
return { 'name' : name , 'datetime' : dt , 'filepath' : filepath }


signature:
def run_server ( self , port ) :
    """run a server binding to port"""

implementation:
try :
    self . server = MultiThreadedHTTPServer ( ( '0.0.0.0' , port ) , Handler )
except socket . error , e : # failed to bind port
    logger . error ( str ( e ) )
    sys . exit ( 1 )
logger . info ( "HTTP serve at http://0.0.0.0:%d (ctrl-c to stop) ..." % port )
try :
    self . server . serve_forever ( )
except KeyboardInterrupt :
    logger . info ( "^C received, shutting down server" )
    self . shutdown_server ( )


signature:
def get_files_stat ( self ) :
    """get source files ' update time"""

implementation:
if not exists ( Post . src_dir ) :
    logger . error ( SourceDirectoryNotFound . __doc__ )
    sys . exit ( SourceDirectoryNotFound . exit_code )
paths = [ ]
for fn in ls ( Post . src_dir ) :
    if fn . endswith ( src_ext ) :
        paths . append ( join ( Post . src_dir , fn ) )
# config . toml
if exists ( config . filepath ) :
    paths . append ( config . filepath )
# files : a < filepath to updated time > dict
files = dict ( ( p , stat ( p ) . st_mtime ) for p in paths )
return files


signature:
def watch_files ( self ) :
    """watch files for changes , if changed , rebuild blog . this thread
 will quit if the main process ends"""

implementation:
try :
    while 1 :
        sleep ( 1 )
        # check every 1s
        try :
            files_stat = self . get_files_stat ( )
        except SystemExit :
            logger . error ( "Error occurred, server shut down" )
            self . shutdown_server ( )
        if self . files_stat != files_stat :
            logger . info ( "Changes detected, start rebuilding.." )
            try :
                generator . re_generate ( )
                global _root
                _root = generator . root
            except SystemExit : # catch sys . exit , it means fatal error
                logger . error ( "Error occurred, server shut down" )
                self . shutdown_server ( )
            self . files_stat = files_stat
            # update files ' stat
except KeyboardInterrupt : # I dont know why , but this exception won ' t be catched
# because absolutly each KeyboardInterrupt is catched by
# the server thread , which will terminate this thread the same time
    logger . info ( "^C received, shutting down watcher" )
    self . shutdown_watcher ( )


signature:
def parse ( src ) :
    """Note : src should be ascii string"""

implementation:
rt = libparser . parse ( byref ( post ) , src )
return ( rt , string_at ( post . title , post . tsz ) , string_at ( post . tpic , post . tpsz ) , post . body )


signature:
def deploy_blog ( ) :
    """Deploy new blog to current directory"""

implementation:
logger . info ( deploy_blog . __doc__ )
# ` rsync - aqu path / to / res / * . `
call ( 'rsync -aqu ' + join ( dirname ( __file__ ) , 'res' , '*' ) + ' .' , shell = True )
logger . success ( 'Done' )
logger . info ( 'Please edit config.toml to meet your needs' )


signature:
def new_post ( ) :
    """Touch a new post in src /"""

implementation:
logger . info ( new_post . __doc__ )
# make the new post ' s filename
now = datetime . datetime . now ( )
now_s = now . strftime ( '%Y-%m-%d-%H-%M' )
filepath = join ( Post . src_dir , now_s + src_ext )
# check if ` src / ` exists
if not exists ( Post . src_dir ) :
    logger . error ( SourceDirectoryNotFound . __doc__ )
    sys . exit ( SourceDirectoryNotFound . exit_code )
# write sample content to new post
content = ( 'Title\n' 'Title Picture URL\n' '---\n' 'Markdown content ..' )
f = open ( filepath , 'w' )
f . write ( content )
f . close ( )
logger . success ( 'New post created: %s' % filepath )


signature:
def clean ( ) :
    """Clean htmls rux built : ` rm - rf post page index . html `"""

implementation:
logger . info ( clean . __doc__ )
paths = [ 'post' , 'page' , 'index.html' ]
call ( [ 'rm' , '-rf' ] + paths )
logger . success ( 'Done' )


signature:
def resolve_blocks ( template , context ) :
    '''Return a BlockContext instance of all the { % block % } tags in the template .
 If template is a string , it will be resolved through get _ template'''

implementation:
try :
    blocks = context . render_context [ BLOCK_CONTEXT_KEY ]
except KeyError :
    blocks = context . render_context [ BLOCK_CONTEXT_KEY ] = BlockContext ( )
# If it ' s just the name , resolve into template
if isinstance ( template , six . string_types ) :
    template = get_template ( template )
# For Django 1.8 compatibility
template = getattr ( template , 'template' , template )
# Add this templates blocks as the first
local_blocks = { block . name : block for block in template . nodelist . get_nodes_by_type ( BlockNode ) }
blocks . add_blocks ( local_blocks )
# Do we extend a parent template ?
extends = template . nodelist . get_nodes_by_type ( ExtendsNode )
if extends : # Can only have one extends in a template
    extends_node = extends [ 0 ]
    # Get the parent , and recurse
    parent_template = extends_node . get_parent ( context )
    resolve_blocks ( parent_template , context )
return blocks


signature:
def parse_widget_name ( widget ) :
    '''Parse a alias : block _ name string into separate parts .'''

implementation:
try :
    alias , block_name = widget . split ( ':' , 1 )
except ValueError :
    raise template . TemplateSyntaxError ( 'widget name must be "alias:block_name" - %s' % widget )
return alias , block_name


signature:
def using ( context , alias ) :
    '''Temporarily update the context to use the BlockContext for the given alias .'''

implementation:
# An empty alias means look in the current widget set .
if alias == '' :
    yield context
else :
    try :
        widgets = context . render_context [ WIDGET_CONTEXT_KEY ]
    except KeyError :
        raise template . TemplateSyntaxError ( 'No widget libraries loaded!' )
    try :
        block_set = widgets [ alias ]
    except KeyError :
        raise template . TemplateSyntaxError ( 'No widget library loaded for alias: %r' % alias )
    context . render_context . push ( )
    context . render_context [ BLOCK_CONTEXT_KEY ] = block_set
    context . render_context [ WIDGET_CONTEXT_KEY ] = widgets
    yield context
    context . render_context . pop ( )


signature:
def find_block ( context , * names ) :
    '''Find the first matching block in the current block _ context'''

implementation:
block_set = context . render_context [ BLOCK_CONTEXT_KEY ]
for name in names :
    block = block_set . get_block ( name )
    if block is not None :
        return block
raise template . TemplateSyntaxError ( 'No widget found for: %r' % ( names , ) )


signature:
def load_widgets ( context , ** kwargs ) :
    '''Load a series of widget libraries .'''

implementation:
_soft = kwargs . pop ( '_soft' , False )
try :
    widgets = context . render_context [ WIDGET_CONTEXT_KEY ]
except KeyError :
    widgets = context . render_context [ WIDGET_CONTEXT_KEY ] = { }
for alias , template_name in kwargs . items ( ) :
    if _soft and alias in widgets :
        continue
    with context . render_context . push ( { BLOCK_CONTEXT_KEY : BlockContext ( ) } ) :
        blocks = resolve_blocks ( template_name , context )
        widgets [ alias ] = blocks
return ''


signature:
def auto_widget ( field ) :
    '''Return a list of widget names for the provided field .'''

implementation:
# Auto - detect
info = { 'widget' : field . field . widget . __class__ . __name__ , 'field' : field . field . __class__ . __name__ , 'name' : field . name , }
return [ fmt . format ( ** info ) for fmt in ( '{field}_{widget}_{name}' , '{field}_{name}' , '{widget}_{name}' , '{field}_{widget}' , '{name}' , '{widget}' , '{field}' , ) ]


signature:
def reuse ( context , block_list , ** kwargs ) :
    '''Allow reuse of a block within a template .
 { % reuse ' _ myblock ' foo = bar % }
 If passed a list of block names , will use the first that matches :
 { % reuse list _ of _ block _ names . . . . % }'''

implementation:
try :
    block_context = context . render_context [ BLOCK_CONTEXT_KEY ]
except KeyError :
    block_context = BlockContext ( )
if not isinstance ( block_list , ( list , tuple ) ) :
    block_list = [ block_list ]
for block in block_list :
    block = block_context . get_block ( block )
    if block :
        break
else :
    return ''
with context . push ( kwargs ) :
    return block . render ( context )


signature:
def display ( self ) :
    """When dealing with optgroups , ensure that the value is properly force _ text ' d ."""

implementation:
if not self . is_group ( ) :
    return self . _display
return ( ( force_text ( k ) , v ) for k , v in self . _display )


signature:
def _list_key ( self , key ) :
    """boilerplate"""

implementation:
ret = [ ]
for msg_json in self . client . lrange ( key , 0 , - 1 ) :
    ret . append ( self . _fromJSON ( msg_json ) )
return ret


signature:
def create_message ( self , level , msg_text , extra_tags = '' , date = None , url = None ) :
    """Message instances are namedtuples of type ` Message ` .
 The date field is already serialized in datetime . isoformat ECMA - 262 format"""

implementation:
if not date :
    now = timezone . now ( )
else :
    now = date
r = now . isoformat ( )
if now . microsecond :
    r = r [ : 23 ] + r [ 26 : ]
if r . endswith ( '+00:00' ) :
    r = r [ : - 6 ] + 'Z'
fingerprint = r + msg_text
msg_id = hashlib . sha256 ( fingerprint . encode ( 'ascii' , 'ignore' ) ) . hexdigest ( )
return Message ( id = msg_id , message = msg_text , level = level , tags = extra_tags , date = r , url = url )


signature:
def add_message_for ( users , level , message_text , extra_tags = '' , date = None , url = None , fail_silently = False ) :
    """Send a message to a list of users without passing through ` django . contrib . messages `
 : param users : an iterable containing the recipients of the messages
 : param level : message level
 : param message _ text : the string containing the message
 : param extra _ tags : like the Django api , a string containing extra tags for the message
 : param date : a date , different than the default timezone . now
 : param url : an optional url
 : param fail _ silently : not used at the moment"""

implementation:
BackendClass = stored_messages_settings . STORAGE_BACKEND
backend = BackendClass ( )
m = backend . create_message ( level , message_text , extra_tags , date , url )
backend . archive_store ( users , m )
backend . inbox_store ( users , m )


signature:
def broadcast_message ( level , message_text , extra_tags = '' , date = None , url = None , fail_silently = False ) :
    """Send a message to all users aka broadcast .
 : param level : message level
 : param message _ text : the string containing the message
 : param extra _ tags : like the Django api , a string containing extra tags for the message
 : param date : a date , different than the default timezone . now
 : param url : an optional url
 : param fail _ silently : not used at the moment"""

implementation:
from django . contrib . auth import get_user_model
users = get_user_model ( ) . objects . all ( )
add_message_for ( users , level , message_text , extra_tags = extra_tags , date = date , url = url , fail_silently = fail_silently )


signature:
def mark_read ( user , message ) :
    """Mark message instance as read for user .
 Returns True if the message was ` unread ` and thus actually marked as ` read ` or False in case
 it is already ` read ` or it does not exist at all .
 : param user : user instance for the recipient
 : param message : a Message instance to mark as read"""

implementation:
BackendClass = stored_messages_settings . STORAGE_BACKEND
backend = BackendClass ( )
backend . inbox_delete ( user , message )


signature:
def mark_all_read ( user ) :
    """Mark all message instances for a user as read .
 : param user : user instance for the recipient"""

implementation:
BackendClass = stored_messages_settings . STORAGE_BACKEND
backend = BackendClass ( )
backend . inbox_purge ( user )


signature:
def mark_all_read ( request ) :
    """Mark all messages as read ( i . e . delete from inbox ) for current logged in user"""

implementation:
from . settings import stored_messages_settings
backend = stored_messages_settings . STORAGE_BACKEND ( )
backend . inbox_purge ( request . user )
return Response ( { "message" : "All messages read" } )


signature:
def read ( self , request , pk = None ) :
    """Mark the message as read ( i . e . delete from inbox )"""

implementation:
from . settings import stored_messages_settings
backend = stored_messages_settings . STORAGE_BACKEND ( )
try :
    backend . inbox_delete ( request . user , pk )
except MessageDoesNotExist as e :
    return Response ( e . message , status = '404' )
return Response ( { 'status' : 'message marked as read' } )


signature:
def stored_messages_list ( context , num_elements = 10 ) :
    """Renders a list of unread stored messages for the current user"""

implementation:
if "user" in context :
    user = context [ "user" ]
    if user . is_authenticated ( ) :
        qs = Inbox . objects . select_related ( "message" ) . filter ( user = user )
        return { "messages" : qs [ : num_elements ] , "count" : qs . count ( ) , }


signature:
def stored_messages_count ( context ) :
    """Renders a list of unread stored messages for the current user"""

implementation:
if "user" in context :
    user = context [ "user" ]
    if user . is_authenticated ( ) :
        return Inbox . objects . select_related ( "message" ) . filter ( user = user ) . count ( )


signature:
def stored_messages_archive ( context , num_elements = 10 ) :
    """Renders a list of archived messages for the current user"""

implementation:
if "user" in context :
    user = context [ "user" ]
    if user . is_authenticated ( ) :
        qs = MessageArchive . objects . select_related ( "message" ) . filter ( user = user )
        return { "messages" : qs [ : num_elements ] , "count" : qs . count ( ) , }


signature:
def _get ( self , * args , ** kwargs ) :
    """Retrieve unread messages for current user , both from the inbox and
 from other storages"""

implementation:
messages , all_retrieved = super ( StorageMixin , self ) . _get ( * args , ** kwargs )
if self . user . is_authenticated ( ) :
    inbox_messages = self . backend . inbox_list ( self . user )
else :
    inbox_messages = [ ]
return messages + inbox_messages , all_retrieved


signature:
def add ( self , level , message , extra_tags = '' ) :
    """If the message level was configured for being stored and request . user
 is not anonymous , save it to the database . Otherwise , let some other
 class handle the message .
 Notice : controls like checking the message is not empty and the level
 is above the filter need to be performed here , but it could happen
 they ' ll be performed again later if the message does not need to be
 stored ."""

implementation:
if not message :
    return
# Check that the message level is not less than the recording level .
level = int ( level )
if level < self . level :
    return
# Check if the message doesn ' t have a level that needs to be persisted
if level not in stored_messages_settings . STORE_LEVELS or self . user . is_anonymous ( ) :
    return super ( StorageMixin , self ) . add ( level , message , extra_tags )
self . added_new = True
m = self . backend . create_message ( level , message , extra_tags )
self . backend . archive_store ( [ self . user ] , m )
self . _queued_messages . append ( m )


signature:
def _store ( self , messages , response , * args , ** kwargs ) :
    """persistent messages are already in the database inside the ' archive ' ,
 so we can say they ' re already " stored " .
 Here we put them in the inbox , or remove from the inbox in case the
 messages were iterated .
 messages contains only new msgs if self . used = = True
 else contains both new and unread messages"""

implementation:
contrib_messages = [ ]
if self . user . is_authenticated ( ) :
    if not messages : # erase inbox
        self . backend . inbox_purge ( self . user )
    else :
        for m in messages :
            try :
                self . backend . inbox_store ( [ self . user ] , m )
            except MessageTypeNotSupported :
                contrib_messages . append ( m )
super ( StorageMixin , self ) . _store ( contrib_messages , response , * args , ** kwargs )


signature:
def _prepare_messages ( self , messages ) :
    """Like the base class method , prepares a list of messages for storage
 but avoid to do this for ` models . Message ` instances ."""

implementation:
for message in messages :
    if not self . backend . can_handle ( message ) :
        message . _prepare ( )


signature:
def jocker ( test_options = None ) :
    """Main entry point for script ."""

implementation:
version = ver_check ( )
options = test_options or docopt ( __doc__ , version = version )
_set_global_verbosity_level ( options . get ( '--verbose' ) )
jocker_lgr . debug ( options )
jocker_run ( options )


signature:
def init ( base_level = DEFAULT_BASE_LOGGING_LEVEL , verbose_level = DEFAULT_VERBOSE_LOGGING_LEVEL , logging_config = None ) :
    """initializes a base logger
 you can use this to init a logger in any of your files .
 this will use config . py ' s LOGGER param and logging . dictConfig to configure
 the logger for you .
 : param int | logging . LEVEL base _ level : desired base logging level
 : param int | logging . LEVEL verbose _ level : desired verbose logging level
 : param dict logging _ dict : dictConfig based configuration .
 used to override the default configuration from config . py
 : rtype : ` python logger `"""

implementation:
if logging_config is None :
    logging_config = { }
logging_config = logging_config or LOGGER
# TODO : ( IMPRV ) only perform file related actions if file handler is
# TODO : ( IMPRV ) defined .
log_file = LOGGER [ 'handlers' ] [ 'file' ] [ 'filename' ]
log_dir = os . path . dirname ( os . path . expanduser ( log_file ) )
if os . path . isfile ( log_dir ) :
    sys . exit ( 'file {0} exists - log directory cannot be created ' 'there. please remove the file and try again.' . format ( log_dir ) )
try :
    if not os . path . exists ( log_dir ) and not len ( log_dir ) == 0 :
        os . makedirs ( log_dir )
    dictconfig . dictConfig ( logging_config )
    lgr = logging . getLogger ( 'user' )
    lgr . setLevel ( base_level )
    return lgr
except ValueError as e :
    sys . exit ( 'could not initialize logger.' ' verify your logger config' ' and permissions to write to {0} ({1})' . format ( log_file , e ) )


signature:
def cfg_convert ( self , value ) :
    """Default converter for the cfg : / / protocol ."""

implementation:
rest = value
m = self . WORD_PATTERN . match ( rest )
if m is None :
    raise ValueError ( "Unable to convert %r" % value )
else :
    rest = rest [ m . end ( ) : ]
    d = self . config [ m . groups ( ) [ 0 ] ]
    # print d , rest
    while rest :
        m = self . DOT_PATTERN . match ( rest )
        if m :
            d = d [ m . groups ( ) [ 0 ] ]
        else :
            m = self . INDEX_PATTERN . match ( rest )
            if m :
                idx = m . groups ( ) [ 0 ]
                if not self . DIGIT_PATTERN . match ( idx ) :
                    d = d [ idx ]
                else :
                    try :
                        n = int ( idx )
                        d = d [ n ]
                    except TypeError :
                        d = d [ idx ]
        if m :
            rest = rest [ m . end ( ) : ]
        else :
            raise ValueError ( 'Unable to convert ' '%r at %r' % ( value , rest ) )
# rest should be empty
return d


signature:
def configure_custom ( self , config ) :
    """Configure an object with a user - supplied factory ."""

implementation:
c = config . pop ( '()' )
if not hasattr ( c , '__call__' ) and hasattr ( types , 'ClassType' ) and isinstance ( c , types . ClassType ) :
    c = self . resolve ( c )
props = config . pop ( '.' , None )
# Check for valid identifiers
kwargs = dict ( ( k , config [ k ] ) for k in config if valid_ident ( k ) )
result = c ( ** kwargs )
if props :
    for name , value in props . items ( ) :
        setattr ( result , name , value )
return result


signature:
def _set_global_verbosity_level ( is_verbose_output = False ) :
    """sets the global verbosity level for console and the jocker _ lgr logger .
 : param bool is _ verbose _ output : should be output be verbose"""

implementation:
global verbose_output
# TODO : ( IMPRV ) only raise exceptions in verbose mode
verbose_output = is_verbose_output
if verbose_output :
    jocker_lgr . setLevel ( logging . DEBUG )
else :
    jocker_lgr . setLevel ( logging . INFO )


signature:
def _import_config ( config_file ) :
    """returns a configuration object
 : param string config _ file : path to config file"""

implementation:
# get config file path
jocker_lgr . debug ( 'config file is: {0}' . format ( config_file ) )
# append to path for importing
try :
    jocker_lgr . debug ( 'importing config...' )
    with open ( config_file , 'r' ) as c :
        return yaml . safe_load ( c . read ( ) )
except IOError as ex :
    jocker_lgr . error ( str ( ex ) )
    raise RuntimeError ( 'cannot access config file' )
except yaml . parser . ParserError as ex :
    jocker_lgr . error ( 'invalid yaml file: {0}' . format ( ex ) )
    raise RuntimeError ( 'invalid yaml file' )


signature:
def execute ( varsfile , templatefile , outputfile = None , configfile = None , dryrun = False , build = False , push = False , verbose = False ) :
    """generates a Dockerfile , builds an image and pushes it to DockerHub
 A ` Dockerfile ` will be generated by Jinja2 according to the ` varsfile `
 imported . If build is true , an image will be generated from the
 ` outputfile ` which is the generated Dockerfile and committed to the
 image : tag string supplied to ` build ` .
 If push is true , a build will be triggered and the produced image
 will be pushed to DockerHub upon completion .
 : param string varsfile : path to file with variables .
 : param string templatefile : path to template file to use .
 : param string outputfile : path to output Dockerfile .
 : param string configfile : path to yaml file with docker - py config .
 : param bool dryrun : mock run .
 : param build : False or the image : tag to build to .
 : param push : False or the image : tag to build to . ( triggers build )
 : param bool verbose : verbose output ."""

implementation:
if dryrun and ( build or push ) :
    jocker_lgr . error ( 'dryrun requested, cannot build.' )
    sys . exit ( 100 )
_set_global_verbosity_level ( verbose )
j = Jocker ( varsfile , templatefile , outputfile , configfile , dryrun , build , push )
formatted_text = j . generate ( )
if dryrun :
    g = j . dryrun ( formatted_text )
if build or push :
    j . build_image ( )
if push :
    j . push_image ( )
if dryrun :
    return g


signature:
def _parse_dumb_push_output ( self , string ) :
    """since the push process outputs a single unicode string consisting of
 multiple JSON formatted " status " lines , we need to parse it so that it
 can be read as multiple strings .
 This will receive the string as an input , count curly braces and ignore
 any newlines . When the curly braces stack is 0 , it will append the
 entire string it has read up until then to a list and so forth .
 : param string : the string to parse
 : rtype : list of JSON ' s"""

implementation:
stack = 0
json_list = [ ]
tmp_json = ''
for char in string :
    if not char == '\r' and not char == '\n' :
        tmp_json += char
    if char == '{' :
        stack += 1
    elif char == '}' :
        stack -= 1
    if stack == 0 :
        if not len ( tmp_json ) == 0 :
            json_list . append ( tmp_json )
        tmp_json = ''
return json_list


signature:
def upload_gif ( gif ) :
    """Uploads an image file to Imgur"""

implementation:
client_id = os . environ . get ( 'IMGUR_API_ID' )
client_secret = os . environ . get ( 'IMGUR_API_SECRET' )
if client_id is None or client_secret is None :
    click . echo ( 'Cannot upload - could not find IMGUR_API_ID or IMGUR_API_SECRET environment variables' )
    return
client = ImgurClient ( client_id , client_secret )
click . echo ( 'Uploading file {}' . format ( click . format_filename ( gif ) ) )
response = client . upload_from_path ( gif )
click . echo ( 'File uploaded - see your gif at {}' . format ( response [ 'link' ] ) )


signature:
def is_dot ( ip ) :
    """Return true if the IP address is in dotted decimal notation ."""

implementation:
octets = str ( ip ) . split ( '.' )
if len ( octets ) != 4 :
    return False
for i in octets :
    try :
        val = int ( i )
    except ValueError :
        return False
    if val > 255 or val < 0 :
        return False
return True


signature:
def is_bin ( ip ) :
    """Return true if the IP address is in binary notation ."""

implementation:
try :
    ip = str ( ip )
    if len ( ip ) != 32 :
        return False
    dec = int ( ip , 2 )
except ( TypeError , ValueError ) :
    return False
if dec > 4294967295 or dec < 0 :
    return False
return True


signature:
def is_oct ( ip ) :
    """Return true if the IP address is in octal notation ."""

implementation:
try :
    dec = int ( str ( ip ) , 8 )
except ( TypeError , ValueError ) :
    return False
if dec > 0o37777777777 or dec < 0 :
    return False
return True


signature:
def is_dec ( ip ) :
    """Return true if the IP address is in decimal notation ."""

implementation:
try :
    dec = int ( str ( ip ) )
except ValueError :
    return False
if dec > 4294967295 or dec < 0 :
    return False
return True


signature:
def _check_nm ( nm , notation ) :
    """Function internally used to check if the given netmask
 is of the specified notation ."""

implementation:
# Convert to decimal , and check if it ' s in the list of valid netmasks .
_NM_CHECK_FUNCT = { NM_DOT : _dot_to_dec , NM_HEX : _hex_to_dec , NM_BIN : _bin_to_dec , NM_OCT : _oct_to_dec , NM_DEC : _dec_to_dec_long }
try :
    dec = _NM_CHECK_FUNCT [ notation ] ( nm , check = True )
except ValueError :
    return False
if dec in _NETMASKS_VALUES :
    return True
return False


signature:
def is_bits_nm ( nm ) :
    """Return true if the netmask is in bits notatation ."""

implementation:
try :
    bits = int ( str ( nm ) )
except ValueError :
    return False
if bits > 32 or bits < 0 :
    return False
return True


signature:
def is_wildcard_nm ( nm ) :
    """Return true if the netmask is in wildcard bits notatation ."""

implementation:
try :
    dec = 0xFFFFFFFF - _dot_to_dec ( nm , check = True )
except ValueError :
    return False
if dec in _NETMASKS_VALUES :
    return True
return False


signature:
def _dot_to_dec ( ip , check = True ) :
    """Dotted decimal notation to decimal conversion ."""

implementation:
if check and not is_dot ( ip ) :
    raise ValueError ( '_dot_to_dec: invalid IP: "%s"' % ip )
octets = str ( ip ) . split ( '.' )
dec = 0
dec |= int ( octets [ 0 ] ) << 24
dec |= int ( octets [ 1 ] ) << 16
dec |= int ( octets [ 2 ] ) << 8
dec |= int ( octets [ 3 ] )
return dec


signature:
def _dec_to_dot ( ip ) :
    """Decimal to dotted decimal notation conversion ."""

implementation:
first = int ( ( ip >> 24 ) & 255 )
second = int ( ( ip >> 16 ) & 255 )
third = int ( ( ip >> 8 ) & 255 )
fourth = int ( ip & 255 )
return '%d.%d.%d.%d' % ( first , second , third , fourth )


signature:
def _hex_to_dec ( ip , check = True ) :
    """Hexadecimal to decimal conversion ."""

implementation:
if check and not is_hex ( ip ) :
    raise ValueError ( '_hex_to_dec: invalid IP: "%s"' % ip )
if isinstance ( ip , int ) :
    ip = hex ( ip )
return int ( str ( ip ) , 16 )


signature:
def _oct_to_dec ( ip , check = True ) :
    """Octal to decimal conversion ."""

implementation:
if check and not is_oct ( ip ) :
    raise ValueError ( '_oct_to_dec: invalid IP: "%s"' % ip )
if isinstance ( ip , int ) :
    ip = oct ( ip )
return int ( str ( ip ) , 8 )


signature:
def _bin_to_dec ( ip , check = True ) :
    """Binary to decimal conversion ."""

implementation:
if check and not is_bin ( ip ) :
    raise ValueError ( '_bin_to_dec: invalid IP: "%s"' % ip )
if isinstance ( ip , int ) :
    ip = str ( ip )
return int ( str ( ip ) , 2 )


signature:
def _BYTES_TO_BITS ( ) :
    """Generate a table to convert a whole byte to binary .
 This code was taken from the Python Cookbook , 2nd edition - O ' Reilly ."""

implementation:
the_table = 256 * [ None ]
bits_per_byte = list ( range ( 7 , - 1 , - 1 ) )
for n in range ( 256 ) :
    l = n
    bits = 8 * [ None ]
    for i in bits_per_byte :
        bits [ i ] = '01' [ n & 1 ]
        n >>= 1
    the_table [ l ] = '' . join ( bits )
return the_table