<s> from datetime import datetime , timedelta <EOL> import functools <EOL> import numpy as np <EOL> import pandas as pd <EOL> from . options import OPTIONS <EOL> from . pycompat import iteritems , unicode_type , bytes_type , dask_array_type <EOL> def pretty_print ( x , numchars ) : <EOL> """<STR_LIT>""" <EOL> s = str ( x ) <EOL> if len ( s ) > numchars : <EOL> return s [ : ( numchars - <NUM_LIT:3> ) ] + '<STR_LIT>' <EOL> else : <EOL> return s + '<STR_LIT:U+0020>' * ( numchars - len ( s ) ) <EOL> def wrap_indent ( text , start = '<STR_LIT>' , length = None ) : <EOL> if length is None : <EOL> length = len ( start ) <EOL> indent = '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * length <EOL> return start + indent . join ( x for x in text . splitlines ( ) ) <EOL> def _get_indexer_at_least_n_items ( shape , n_desired ) : <EOL> assert <NUM_LIT:0> < n_desired <= np . prod ( shape ) <EOL> cum_items = np . cumprod ( shape [ : : - <NUM_LIT:1> ] ) <EOL> n_steps = np . argmax ( cum_items >= n_desired ) <EOL> stop = int ( np . ceil ( float ( n_desired ) / np . r_ [ <NUM_LIT:1> , cum_items ] [ n_steps ] ) ) <EOL> indexer = ( ( <NUM_LIT:0> , ) * ( len ( shape ) - <NUM_LIT:1> - n_steps ) + ( slice ( stop ) , ) + <EOL> ( slice ( None ) , ) * n_steps ) <EOL> return indexer <EOL> def first_n_items ( x , n_desired ) : <EOL> """<STR_LIT>""" <EOL> if n_desired < <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if x . size == <NUM_LIT:0> : <EOL> return [ ] <EOL> if n_desired < x . size : <EOL> indexer = _get_indexer_at_least_n_items ( x . shape , n_desired ) <EOL> x = x [ indexer ] <EOL> return np . asarray ( x ) . flat [ : n_desired ] <EOL> def format_timestamp ( t ) : <EOL> """<STR_LIT>""" <EOL> datetime_str = str ( pd . Timestamp ( t ) ) <EOL> try : <EOL> date_str , time_str = datetime_str . split ( ) <EOL> except ValueError : <EOL> return datetime_str <EOL> else : <EOL> if time_str == '<STR_LIT>' : <EOL> return date_str <EOL> else : <EOL> return '<STR_LIT>' % ( date_str , time_str ) <EOL> def format_timedelta ( t , timedelta_format = None ) : <EOL> """<STR_LIT>""" <EOL> timedelta_str = str ( pd . Timedelta ( t ) ) <EOL> try : <EOL> days_str , time_str = timedelta_str . split ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> return timedelta_str <EOL> else : <EOL> if timedelta_format == '<STR_LIT:date>' : <EOL> return days_str + '<STR_LIT>' <EOL> elif timedelta_format == '<STR_LIT:time>' : <EOL> return time_str <EOL> else : <EOL> return timedelta_str <EOL> def format_item ( x , timedelta_format = None , quote_strings = True ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , ( np . datetime64 , datetime ) ) : <EOL> return format_timestamp ( x ) <EOL> if isinstance ( x , ( np . timedelta64 , timedelta ) ) : <EOL> return format_timedelta ( x , timedelta_format = timedelta_format ) <EOL> elif isinstance ( x , ( unicode_type , bytes_type ) ) : <EOL> return repr ( x ) if quote_strings else x <EOL> elif isinstance ( x , ( float , np . float ) ) : <EOL> return '<STR_LIT>' . format ( x ) <EOL> else : <EOL> return str ( x ) <EOL> def format_items ( x ) : <EOL> """<STR_LIT>""" <EOL> x = np . asarray ( x ) <EOL> timedelta_format = '<STR_LIT>' <EOL> if np . issubdtype ( x . dtype , np . timedelta64 ) : <EOL> x = np . asarray ( x , dtype = '<STR_LIT>' ) <EOL> day_part = ( x [ ~ pd . isnull ( x ) ] <EOL> . astype ( '<STR_LIT>' ) <EOL> . astype ( '<STR_LIT>' ) ) <EOL> time_needed = x != day_part <EOL> day_needed = day_part != np . timedelta64 ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> if np . logical_not ( day_needed ) . all ( ) : <EOL> timedelta_format = '<STR_LIT:time>' <EOL> elif np . logical_not ( time_needed ) . all ( ) : <EOL> timedelta_format = '<STR_LIT:date>' <EOL> formatted = [ format_item ( xi , timedelta_format ) for xi in x ] <EOL> return formatted <EOL> def format_array_flat ( items_ndarray , max_width ) : <EOL> """<STR_LIT>""" <EOL> max_possibly_relevant = max ( int ( np . ceil ( max_width / <NUM_LIT> ) ) , <NUM_LIT:1> ) <EOL> relevant_items = first_n_items ( items_ndarray , max_possibly_relevant ) <EOL> pprint_items = format_items ( relevant_items ) <EOL> cum_len = np . cumsum ( [ len ( s ) + <NUM_LIT:1> for s in pprint_items ] ) - <NUM_LIT:1> <EOL> if ( max_possibly_relevant < items_ndarray . size or <EOL> ( cum_len > max_width ) . any ( ) ) : <EOL> end_padding = '<STR_LIT>' <EOL> count = max ( np . argmax ( ( cum_len + len ( end_padding ) ) > max_width ) , <NUM_LIT:1> ) <EOL> pprint_items = pprint_items [ : count ] <EOL> else : <EOL> end_padding = '<STR_LIT>' <EOL> pprint_str = '<STR_LIT:U+0020>' . join ( pprint_items ) + end_padding <EOL> return pprint_str <EOL> def _summarize_var_or_coord ( name , var , col_width , show_values = True , <EOL> marker = '<STR_LIT:U+0020>' , max_width = None ) : <EOL> if max_width is None : <EOL> max_width = OPTIONS [ '<STR_LIT>' ] <EOL> first_col = pretty_print ( '<STR_LIT>' % ( marker , name ) , col_width ) <EOL> dims_str = '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( map ( str , var . dims ) ) if var . dims else '<STR_LIT>' <EOL> front_str = first_col + dims_str + ( '<STR_LIT>' % var . dtype ) <EOL> if show_values : <EOL> values_str = format_array_flat ( var , max_width - len ( front_str ) ) <EOL> else : <EOL> values_str = '<STR_LIT>' <EOL> return front_str + values_str <EOL> def _not_remote ( var ) : <EOL> """<STR_LIT>""" <EOL> source = var . encoding . get ( '<STR_LIT:source>' ) <EOL> if source and source . startswith ( '<STR_LIT:http>' ) and not var . _in_memory : <EOL> return False <EOL> return True <EOL> def summarize_var ( name , var , col_width ) : <EOL> show_values = _not_remote ( var ) <EOL> return _summarize_var_or_coord ( name , var , col_width , show_values ) <EOL> def summarize_coord ( name , var , col_width ) : <EOL> is_index = name in var . dims <EOL> show_values = is_index or _not_remote ( var ) <EOL> marker = '<STR_LIT:*>' if is_index else '<STR_LIT:U+0020>' <EOL> return _summarize_var_or_coord ( name , var , col_width , show_values , marker ) <EOL> def _maybe_truncate ( obj , maxlen = <NUM_LIT> ) : <EOL> s = str ( obj ) <EOL> if len ( s ) > maxlen : <EOL> s = s [ : ( maxlen - <NUM_LIT:3> ) ] + '<STR_LIT>' <EOL> return s <EOL> def summarize_attr ( key , value , col_width = None ) : <EOL> return '<STR_LIT>' % ( key , _maybe_truncate ( value ) ) <EOL> EMPTY_REPR = '<STR_LIT>' <EOL> def _calculate_col_width ( mapping ) : <EOL> max_name_length = max ( len ( str ( k ) ) for k in mapping ) if mapping else <NUM_LIT:0> <EOL> col_width = max ( max_name_length , <NUM_LIT:7> ) + <NUM_LIT:6> <EOL> return col_width <EOL> def _mapping_repr ( mapping , title , summarizer , col_width = None ) : <EOL> if col_width is None : <EOL> col_width = _calculate_col_width ( mapping ) <EOL> summary = [ '<STR_LIT>' % title ] <EOL> if mapping : <EOL> summary += [ summarizer ( k , v , col_width ) for k , v in mapping . items ( ) ] <EOL> else : <EOL> summary += [ EMPTY_REPR ] <EOL> return '<STR_LIT:\n>' . join ( summary ) <EOL> coords_repr = functools . partial ( _mapping_repr , title = '<STR_LIT>' , <EOL> summarizer = summarize_coord ) <EOL> vars_repr = functools . partial ( _mapping_repr , title = '<STR_LIT>' , <EOL> summarizer = summarize_var ) <EOL> attrs_repr = functools . partial ( _mapping_repr , title = '<STR_LIT>' , <EOL> summarizer = summarize_attr ) <EOL> def indexes_repr ( indexes ) : <EOL> summary = [ ] <EOL> for k , v in indexes . items ( ) : <EOL> summary . append ( wrap_indent ( repr ( v ) , '<STR_LIT>' % k ) ) <EOL> return '<STR_LIT:\n>' . join ( summary ) <EOL> def array_repr ( arr ) : <EOL> if hasattr ( arr , '<STR_LIT:name>' ) and arr . name is not None : <EOL> name_str = '<STR_LIT>' % arr . name <EOL> else : <EOL> name_str = '<STR_LIT>' <EOL> dim_summary = '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v <EOL> in zip ( arr . dims , arr . shape ) ) <EOL> summary = [ '<STR_LIT>' <EOL> % ( type ( arr ) . __name__ , name_str , dim_summary ) ] <EOL> if isinstance ( getattr ( arr , '<STR_LIT>' , arr ) . _data , dask_array_type ) : <EOL> summary . append ( repr ( arr . data ) ) <EOL> elif arr . _in_memory or arr . size < <NUM_LIT> : <EOL> summary . append ( repr ( arr . values ) ) <EOL> else : <EOL> summary . append ( '<STR_LIT>' % ( arr . size , arr . dtype ) ) <EOL> if hasattr ( arr , '<STR_LIT>' ) : <EOL> if arr . coords : <EOL> summary . append ( repr ( arr . coords ) ) <EOL> if arr . attrs : <EOL> summary . append ( attrs_repr ( arr . attrs ) ) <EOL> return '<STR_LIT:\n>' . join ( summary ) <EOL> def dataset_repr ( ds ) : <EOL> summary = [ '<STR_LIT>' % type ( ds ) . __name__ ] <EOL> col_width = _calculate_col_width ( ds ) <EOL> dims_start = pretty_print ( '<STR_LIT>' , col_width ) <EOL> all_dim_strings = [ '<STR_LIT>' % ( k , v ) for k , v in iteritems ( ds . dims ) ] <EOL> summary . append ( '<STR_LIT>' % ( dims_start , '<STR_LIT:U+002CU+0020>' . join ( all_dim_strings ) ) ) <EOL> summary . append ( coords_repr ( ds . coords , col_width = col_width ) ) <EOL> summary . append ( vars_repr ( ds . data_vars , col_width = col_width ) ) <EOL> if ds . attrs : <EOL> summary . append ( attrs_repr ( ds . attrs ) ) <EOL> return '<STR_LIT:\n>' . join ( summary ) </s>
