示例#1
0
 def set_items(self,
               items: Iterable,
               inplace: bool,
               count: Optional[int] = None) -> Optional[Native]:
     if inplace:
         self.set_data(items, inplace=True)
         if Auto.is_defined(count):
             self._set_count(count, inplace=True)
     else:
         if Auto.is_defined(count):
             return self.make_new(items, count=count)
         else:
             return self.make_new(items)
示例#2
0
def fold_lists(
    list_items: Array,
    key_fields: Array,
    list_fields: Array,
    skip_missing: bool = False,
    item_type: Optional[ItemType] = None,
) -> Union[Row, Record]:
    if list_items:
        if not Auto.is_defined(item_type):
            first_item = list_items[0]
            item_type = ItemType.detect(first_item)
        if item_type == ItemType.Record:
            return fold_records(list_items,
                                key_fields,
                                list_fields,
                                skip_missing=skip_missing)
        elif item_type in (ItemType.Row, ItemType.StructRow):
            return fold_rows(list_items,
                             key_fields,
                             list_fields,
                             skip_missing=skip_missing)
        else:
            raise ValueError(
                'fold_lists(): expected Record, Row or StructRow, got {}'.
                format(item_type))
    elif not skip_missing:
        raise ValueError('fold_lists(): list_items must be non-empty')
示例#3
0
 def get_context(self) -> Parent:
     parent = self.get_parent()
     if Auto.is_defined(parent, check_name=False):
         if parent.is_context():
             return parent
         elif hasattr(parent, 'get_context'):
             return parent.get_context()
示例#4
0
 def __init__(
     self,
     name: str = AUTO,
     source: Source = None,
     context: Context = None,
     check: bool = True,
 ):
     name = Auto.delayed_acquire(name, get_generated_name,
                                 self._get_default_name_prefix())
     if Auto.is_defined(context):
         if Auto.is_defined(source):
             source.set_context(context)
         else:
             source = context
     super().__init__(name=name, source=source, check=check)
     if Auto.is_defined(self.get_context()):
         self.put_into_context(check=check)
示例#5
0
 def get_str_count(self, default: str = '(iter)') -> str:
     if hasattr(self, 'get_count'):
         count = self.get_count()
     else:
         count = None
     if Auto.is_defined(count):
         return str(count)
     else:
         return default
示例#6
0
 def func(item) -> Union[dict, tuple]:
     detected_type = item_type
     if not Auto.is_defined(detected_type):
         detected_type = ItemType.detect(item)
     return gr.fold_lists(item,
                          keys,
                          values,
                          skip_missing=skip_missing,
                          item_type=detected_type)
示例#7
0
 def get_records(self, columns: AutoColumns = AUTO) -> Generator:
     if Auto.is_defined(columns):
         available_columns = self.get_columns()
         for r in self.get_rows():
             yield {
                 k: v
                 for k, v in zip(available_columns, r) if k in columns
             }
     else:
         yield from self.get_items_of_type(item_type=ItemType.Record)
示例#8
0
 def get_cropped(self, line: str) -> str:
     max_len = self.get_max_total_len()
     if Auto.is_defined(max_len):
         if max_len < len(line):
             crop_str = self.get_crop_str()
             crop_len = max_len - len(crop_str)
             if crop_len > 0:
                 line = line[:crop_len] + crop_str
             else:
                 line = crop_str[:-crop_len]
     return line
示例#9
0
 def _get_enumerated_items(self, item_type=AUTO) -> Generator:
     if item_type == 'Any' or item_type == 'Any' or not Auto.is_defined(
             item_type):
         items = self.get_items()
     elif hasattr(self, 'get_items_of_type'):
         items = self.get_items_of_type(item_type)
     else:
         items = self.get_items()
         if hasattr(self, 'get_item_type'):
             received_item_type = self.get_item_type()
             assert item_type == received_item_type, '{} != {}'.format(
                 item_type, received_item_type)
     for n, i in enumerate(items):
         yield n, i
示例#10
0
 def __init__(
     self,
     align_right: bool = False,
     min_len: Count = AUTO,
     max_len: Count = AUTO,
     including_framing: bool = False,
     crop: str = CROP_SUFFIX,
     fill: str = FILL_CHAR,
     prefix: str = '',
     suffix: str = '',
     default: str = DEFAULT_STR,
 ):
     if Auto.is_defined(max_len):
         assert len(
             crop
         ) <= max_len, 'Expected len(crop) <= max_len, got len({}) > {}'.format(
             crop, max_len)
     self._min_len = min_len
     self._max_len = max_len
     self._crop = crop
     self._fill = fill
     self._align_right = align_right
     self._prefix = prefix
     self._suffix = suffix
     self._default = default
     if including_framing:
         framing_len = self.get_framing_len()
         if Auto.is_defined(max_len):
             assert max_len >= framing_len, 'Expected max_len >= framing_len, got {}<{}'.format(
                 max_len, framing_len)
             self._max_len -= framing_len
         if Auto.is_defined(min_len):
             if min_len > framing_len:
                 self._min_len -= framing_len
             else:
                 self._min_len = 0
示例#11
0
 def _assert_is_appropriate_child(
     cls,
     obj,
     msg: Union[str, Auto] = AUTO,
     skip_missing: bool = False,
 ) -> None:
     if not cls._is_appropriate_child(obj, skip_missing=skip_missing):
         if not Auto.is_defined(msg):
             template = '{}._assert_is_appropriate_child({}): Expected child as {} instance, got {}'
             expected_class_names = [
                 c.__name__ for c in cls.get_child_obj_classes()
             ]
             msg = template.format(cls.__name__,
                                   obj, ', '.join(expected_class_names),
                                   type(obj))
         raise TypeError(msg)
示例#12
0
 def set_context(self,
                 context: ContextInterface,
                 reset: bool = True,
                 inplace: bool = True) -> Native:
     if inplace:
         if self._is_stub_instance(context):
             return self
         elif self._has_context_as_source():
             if reset:
                 assert isinstance(context, ContextInterface)
                 assert isinstance(context, AbstractNamed)
                 self.set_source(context)
         elif Auto.is_defined(self.get_source()):
             self.get_source().set_context(context, reset=reset)
         return self.put_into_context(check=not reset)
     else:
         contextual = self.set_outplace(context=context)
         return self._assume_native(contextual)
示例#13
0
 def take(self,
          count: Union[int, bool] = 1,
          inplace: bool = False) -> Optional[Native]:
     if (count and isinstance(count, bool)
         ) or not Auto.is_defined(count):  # True, None, AUTO
         return self
     elif isinstance(count, int):
         if count > 0:
             items = self._get_first_items(count)
         elif count < 0:
             items = self._get_last_items(-count)
         else:  # count in (0, False)
             items = list()
         result_count = None
         if self.is_in_memory():
             if not is_in_memory(items):
                 items = list(items)
             if self._has_count_attribute():
                 result_count = len(items)
         return self.set_items(items, count=result_count, inplace=inplace)
     else:
         raise TypeError(
             'Expected count as int or boolean, got {}'.format(count))
示例#14
0
 def get_str_count(self, default: str = '(iter)') -> str:
     count = self.get_count()
     if Auto.is_defined(count):
         return str(count)
     else:
         return default
示例#15
0
文件: arguments.py 项目: az365/snakee
def is_defined(obj, check_name: bool = True) -> bool:
    return Auto.is_defined(obj, check_name=check_name)
示例#16
0
 def get_items(self, item_type: Union[ItemType, Auto] = AUTO) -> Iterable:
     if Auto.is_defined(item_type):
         return self.get_items_of_type(item_type)
     else:
         return self.get_stream_data()
示例#17
0
 def is_defined(self) -> bool:
     return Auto.is_defined(self.get_name())
示例#18
0
 def get_count_repr(self, default: str = '<iter>') -> str:
     count = self.get_str_count()
     if not Auto.is_defined(count):
         count = default
     return '{} items'.format(count)