Construct an import object from a string.
9278
def from_str(cls, s):
        """"""
        ast_obj = ast.parse(s).body[0]
        if not isinstance(ast_obj, cls._expected_ast_type):
            raise AssertionError(
                'Expected ast of type {!r} but got {!r}'.format(
                    cls._expected_ast_type,
                    ast_obj
                )
            )
        return cls(ast_obj)
797987
def perform_import(val):
    
    if val is None:
        return None
    elif isinstance(val, str):
        return import_from_string(val)
    elif isinstance(val, (list, tuple)):
        return [import_from_string(item) for item in val]
    return val
625252
def from_string(cls, source, **kwargs):
        
        error_handling = kwargs.pop('error_handling', None)
        new_file = cls(**kwargs)
        new_file.read(source.splitlines(True), error_handling=error_handling)
        return new_file
442228
def add_from_string(self, strdata):
        """"""
        if self.tree is None:
            self.tree = tree = ET.ElementTree(ET.fromstring(strdata))
            self.root = tree.getroot()
            self.objects = {}
        else:
            # TODO: append to current tree
            pass
665584
def _from_string(cls, serialized):
        
        parse = cls.URL_RE.match(serialized)
        if not parse:
            raise InvalidKeyError(cls, serialized)

        parse = parse.groupdict()
        if parse['definition_id']:
            parse['definition_id'] = cls.as_object_id(parse['definition_id'])

        return cls(**{key: parse.get(key) for key in cls.KEY_FIELDS})
320003
def obj_or_import_string(value, default=None):
    
    if isinstance(value, six.string_types):
        return import_string(value)
    elif value:
        return value
    return default
142556
def fromstring(cls, dis_string):
        """"""
        temp = tempfile.NamedTemporaryFile(delete=False)
        temp.write(dis_string)
        temp.close()
        dis_tree = cls(dis_filepath=temp.name)
        os.unlink(temp.name)
        return dis_tree
545308
def load_from_string(self, settings_string='', module_name='customsettings'):
        '''
        
        '''
        try:
            mod = imp.new_module(module_name)
            exec(settings_string, mod.__dict__)
        except TypeError:
            log.warning("Could not import settings")
        self.my_settings = {}
        try:
            self.my_settings = self._convert_to_dict(mod)
        except ImportError:
            log.warning("Settings unable to be loaded")

        return self.settings()
1258467
def loads(self, string):
        ""
        script_class = self.get_script_class()
        script = self._load(BytesIO(string), self._protocol, self._version)
        return script_class(script)
262997
def import_obj_from_str(s):
    
    ast_obj = ast.parse(s).body[0]
    return ast_type_to_import_type[type(ast_obj)](ast_obj)
226256
def import_from_string(val):
    
    try:
        module_path, class_name = val.rsplit('.', 1)
        module = import_module(module_path)
        return getattr(module, class_name)
    except (ImportError, AttributeError) as e:
        msg = f"Could not import {val}. {e.__class__.__name__}: {e}"
        raise ImportError(msg)
269801
def from_string(cls, prjs):
        
        def parse(v):
            try:
                return int(v)
            except ValueError:
                pass
            try:
                return float(v)
            except ValueError:
                return v

        parts = [o.lstrip('+') for o in prjs.strip().split()]

        items = map(
            lambda kv: len(kv) == 2 and (kv[0], parse(kv[1])) or (kv[0], True),
            (p.split('=') for p in parts))

        return cls({k: v for k, v in items if '+'+k in PROJ4_PARAMS.keys()})
1242729
def get_import_for_type(t):
    """"""
    t_as_string = to_str(t)
    if not is_identifier(t_as_string):
        # this class expanded into something other than a single identifier
        # we can ignore it.  This is the case when we encounter something
        # like the configman.converter.str_to_classes_in_namespaces
        # InnerClassList.  We can safely ignore these things here.
        return (None, None)
    if '.' in t_as_string:
        parts = t_as_string.split('.')
        return ('.'.join(parts[:-1]), parts[-1])
    else:
        if t_as_string in known_mapping_str_to_type:
            return (None, None)
        return (None, t_as_string)
1006576
def from_string(cls, string, *, default_func=None):
        '''
        '''
        if not isinstance(string, str):
            raise TypeError(f'service must be a string: {string}')

        parts = string.split('://', 1)
        if len(parts) == 2:
            protocol, address = parts
        else:
            item, = parts
            protocol = None
            if default_func:
                if default_func(item, ServicePart.HOST) and default_func(item, ServicePart.PORT):
                    protocol, address = item, ''
                else:
                    protocol, address = default_func(None, ServicePart.PROTOCOL), item
            if not protocol:
                raise ValueError(f'invalid service string: {string}')

        if default_func:
            default_func = partial(default_func, protocol.lower())
        address = NetAddress.from_string(address, default_func=default_func)
        return cls(protocol, address)
1342736
def from_string(cls, string):
        """"""
        if string is None:
            string = ''

        if not isinstance(string, six.string_types):
            raise TypeError('string has to be a string type, got: {}'.format(type(string)))

        dictionary = {}
        tokens = [token.strip() for token in shlex.split(string)]

        def list_tuples(some_iterable):
            items, nexts = itertools.tee(some_iterable, 2)
            nexts = itertools.chain(itertools.islice(nexts, 1, None), [None])
            return list(zip(items, nexts))

        for token_current, token_next in list_tuples(tokens):

            # If current token starts with a dash, it is a value so we skip it
            if not token_current.startswith('-'):
                continue

            # If the next token is None or starts with a dash, the current token must be a flag, so the value is True
            if not token_next or token_next.startswith('-'):
                dictionary[token_current.lstrip('-')] = True

            # Otherwise the current token is an option with the next token being its value
            else:
                dictionary[token_current.lstrip('-')] = token_next

        return cls.from_dictionary(dictionary)
73940
def from_string(cls, value):
        """"""
        match = cls.pattern.search(value)
        if match is None:
            raise ValueError('"%s" is not a valid media type' % value)
        try:
            return cls(match.group('mime_type'), float(match.group('weight') or 1))
        except ValueError:
            return cls(value)
511236
def from_str(cls, s):
        
        if not isinstance(s, str):
            raise TypeError("Expected an str instance, received %r" % (s,))
        return cls(cls.bits_from_str(s))