def get_string(self, df_head_only=False): from pymatgen import Structure lines, scope = [], [] for key, value in self.document.iterate(): if isinstance(value, Table): lines[-1] = lines[-1].replace("{", "[+").replace("}", "]") header = any([isinstance(col, str) for col in value]) if isinstance(value.index, MultiIndex): value.reset_index(inplace=True) if df_head_only: value = value.head() csv_string = value.to_csv(index=False, header=header, float_format="%g", encoding="utf-8")[:-1] lines += csv_string.split("\n") if df_head_only: lines.append("...") elif isinstance(value, Structure): from pymatgen.io.cif import CifWriter cif = CifWriter(value, symprec=1e-10).__str__() lines.append( make_pair("".join([replacements.get(c, c) for c in key]), cif + ":end")) elif Quantity is not None and isinstance(value, Quantity): lines.append( make_pair(value.display_symbols[0], value.pretty_string())) else: level, key = key # truncate scope level_reduction = bool(level < len(scope)) if level_reduction: del scope[level:] # append scope if value is None: scope.append("".join([replacements.get(c, c) for c in key])) # correct scope to omit internal 'general' section scope_corr = scope if scope[0] == mp_level01_titles[0]: scope_corr = scope[1:] # insert scope line if (value is None and scope_corr) or (value is not None and level_reduction): lines.append("\n{" + ".".join(scope_corr) + "}") # insert key-value line if value is not None: val = str(value) value_lines = ([val] if val.startswith("http") else textwrap.wrap(val)) if len(value_lines) > 1: value_lines = [""] + value_lines + [":end"] lines.append( make_pair( "".join([replacements.get(c, c) for c in key]), "\n".join(value_lines), )) return "\n".join(lines) + "\n"
def get_string(self, df_head_only=False): from pymatgen import Structure lines, scope = [], [] for key,value in self.document.iterate(): if isinstance(value, Table): lines[-1] = lines[-1].replace('{', '[+').replace('}', ']') header = any([isinstance(col, str) for col in value]) if isinstance(value.index, MultiIndex): value.reset_index(inplace=True) if df_head_only: value = value.head() csv_string = value.to_csv( index=False, header=header, float_format='%g', encoding='utf-8' )[:-1] lines += csv_string.split('\n') if df_head_only: lines.append('...') elif isinstance(value, Structure): from pymatgen.io.cif import CifWriter cif = CifWriter(value, symprec=symprec).__str__() lines.append(make_pair( ''.join([replacements.get(c, c) for c in key]), cif+':end' )) elif Quantity is not None and isinstance(value, Quantity): lines.append(make_pair( value.display_symbols[0], value.pretty_string() )) else: level, key = key # truncate scope level_reduction = bool(level < len(scope)) if level_reduction: del scope[level:] # append scope if value is None: scope.append(''.join([ replacements.get(c, c) for c in key ])) # correct scope to omit internal 'general' section scope_corr = scope if scope[0] == mp_level01_titles[0]: scope_corr = scope[1:] # insert scope line if (value is None and scope_corr) or \ (value is not None and level_reduction): lines.append('\n{' + '.'.join(scope_corr) + '}') # insert key-value line if value is not None: val = str(value) value_lines = [val] if val.startswith('http') \ else textwrap.wrap(val) if len(value_lines) > 1: value_lines = [''] + value_lines + [':end'] lines.append(make_pair( ''.join([replacements.get(c, c) for c in key]), '\n'.join(value_lines) )) return '\n'.join(lines) + '\n'
def get_string(self, with_comments=False): lines = [] min_indentor = get_indentor() table_start = mp_level01_titles[1]+' ' for key,value in self.document.iterate(): if key is None and isinstance(value, dict): pd_obj = pandas.DataFrame.from_dict(value) csv_string = pd_obj.to_csv(index=False, float_format='%g')[:-1] lines += csv_string.split('\n') else: level, key = key key = get_indentor(n=key) if value is None else key sep = '' if min_indentor in key else ':' if lines and key == min_indentor: lines.append('') if isinstance(value, six.string_types): if value.startswith(table_start): value = value[len(table_start):] if ':' in value: # quote to ignore delimiter value = '"{}"'.format(value) lines.append(make_pair(key, value, sep=sep)) if with_comments: for idx_str, comment in self.comments.iteritems(): idx, ast = self.get_comment_index(idx_str) if ast: lines.insert(idx, '#'+comment) else: lines[idx] = ' #'.join([lines[idx], comment]) return '\n'.join(lines) + '\n'
def get_key_value(self): """print random key-value pair - type(key) = str, type(value) = anything - append comment now and then """ while 1: provider_name = self.fake.random_element(elements=self.providers) if provider_name != 'python' and \ provider_name != 'profile' and \ provider_name != 'credit_card': break provider = self.fake.provider(provider_name) methods = [ k for k, v in inspect.getmembers(provider, predicate=inspect.ismethod) if k != '__init__' ] while 1: method_name = self.fake.random_element(elements=methods) method = getattr(provider, method_name) argspec = inspect.getargspec(method) nargs = len(argspec.args) key = '_'.join([provider_name, method_name]) if (argspec.defaults is None and nargs == 1) or (argspec.defaults is not None and nargs - 1 == len(argspec.defaults)): value = method() if not isinstance(value, list) and \ not isinstance(value, dict) and \ not ':' in str(value): break if isinstance(value, str) and '\n' in value: value = repr(value) return make_pair(key, value)
def get_nested_key_values_from_dict(self, d, n=1): """convert a dict into nested level-n mp-csv representation""" for k0,v0 in d.iteritems(): print >>self.section, ' '.join([ get_indentor(n+1), k0, self.get_comment() ]) for k1,v1 in v0.iteritems(): print >>self.section, make_pair(k1, v1)
def get_nested_key_values_from_dict(self, d, n=1): """convert a dict into nested level-n mp-csv representation""" for k0, v0 in d.iteritems(): print >> self.section, ' '.join( [get_indentor(n + 1), k0, self.get_comment()]) for k1, v1 in v0.iteritems(): print >> self.section, make_pair(k1, v1)
def _make_level_n_section(self, sec, n): """recursively generate nested level-n section - level-1 mp_level01_titles allowed once per level-0 section - mp_level01_titles[0] can be arbitrarily deep nested ('general') - mp_level01_titles[1:] has no subsections ('data') - mp_level01_titles[2] only has level-2 subsections ('plots') - mp_level01_titles[1] has csv format, all others key:value - randomly throw in comment lines """ comments = self.get_comments() if comments != '': print >>self.section, comments print >>self.section, self._get_level_n_section_line(sec, n) if self.usable and self.main_general and sec == 0 and n == 0: self.get_player_general_section(n) comment = self.get_comment() if comment != '': print >>self.section, comment num_subsec = 0 if n == self.max_level or \ self.section_titles[-1] == mp_level01_titles[1] or \ ( n == 2 and self.section_titles[-2] == mp_level01_titles[2] ) \ else self.fake.random_int( min=int(n == 1 and \ self.section_titles[-1] == mp_level01_titles[2]), max=self.max_num_subsec ) for i in range(num_subsec): self._make_level_n_section(sec, n+1) self.section_structure.append('.'.join(self.section_titles)) self.section_titles.pop() # all subsections processed if num_subsec == 0: if self.section_titles[-1] == mp_level01_titles[1] or ( n == 0 and \ self.section_titles[-1] != mp_level01_titles[0].upper() ): if self.usable: self.data_gen.player_data.to_csv(self.section, index=False) else: print >>self.section, ' ==> data insert here if --usable' elif n == 2 and self.section_titles[-2] == mp_level01_titles[2]: column = self.fake.random_element( elements=self.data_gen.player_data.columns ) print >>self.section, make_pair('x', column) elif self.usable and not self.main_general and \ self.section_titles[-1] == mp_level01_titles[0]: self.get_player_general_section(n) elif n != 0 or ( n == 0 and \ self.section_titles[-1] != mp_level01_titles[0].upper() ): for r in range(self.max_data_rows): self._print_key_value()
def get_key_value(self): """print random key-value pair - type(key) = str, type(value) = anything - append comment now and then """ while 1: provider_name = self.fake.random_element(elements=self.providers) if provider_name != 'python' and \ provider_name != 'profile' and \ provider_name != 'credit_card': break provider = self.fake.provider(provider_name) methods = [ k for k,v in inspect.getmembers( provider, predicate=inspect.ismethod ) if k != '__init__' ] while 1: method_name = self.fake.random_element(elements=methods) method = getattr(provider, method_name) argspec = inspect.getargspec(method) nargs = len(argspec.args) key = '_'.join([provider_name, method_name]) if ( argspec.defaults is None and nargs == 1 ) or ( argspec.defaults is not None and nargs-1 == len(argspec.defaults) ): value = method() if not isinstance(value, list) and \ not isinstance(value, dict) and \ not ':' in str(value): break if isinstance(value, str) and '\n' in value: value = repr(value) return make_pair(key, value)
def get_string(self): from pymatgen import Structure lines, scope = [], [] table_start = mp_level01_titles[1] + '_' for key, value in self.document.iterate(): if isinstance(value, Table): header = any([ bool(isinstance(col, unicode) or isinstance(col, str)) for col in value ]) if isinstance(value.index, MultiIndex): value.reset_index(inplace=True) csv_string = value.to_csv(index=False, header=header, float_format='%g', encoding='utf-8')[:-1] lines += csv_string.decode('utf-8').split('\n') elif isinstance(value, Structure): from pymatgen.io.cif import CifWriter cif = CifWriter(value, symprec=symprec).__str__() lines.append( make_pair(''.join([replacements.get(c, c) for c in key]), cif + ':end')) else: level, key = key key = key if isinstance(key, unicode) else key.decode('utf-8') # truncate scope level_reduction = bool(level < len(scope)) if level_reduction: del scope[level:] # append scope and set delimiters if value is None: is_table = key.startswith(table_start) if is_table: # account for 'data_' prefix key = key[len(table_start):] start, end = '\n[+', ']' else: start, end = '\n{', '}' scope.append(''.join([replacements.get(c, c) for c in key])) # correct scope to omit internal 'general' section scope_corr = scope if scope[0] == mp_level01_titles[0]: scope_corr = scope[1:] # insert scope line if (value is None and scope_corr)or \ (value is not None and level_reduction): lines.append(start + '.'.join(scope_corr) + end) # insert key-value line if value is not None: val = unicode(value) if not isinstance(value, str) else value value_lines = [val] if val.startswith('http') \ else textwrap.wrap(val) if len(value_lines) > 1: value_lines = [''] + value_lines + [':end'] lines.append( make_pair( ''.join([replacements.get(c, c) for c in key]), '\n'.join(value_lines))) return '\n'.join(lines) + '\n'