def _dump_finished(self, path, replace = False): '''dump taskplanner.finished to local file. When replace = False, existing local file will not be overwrite. For safety ''' if replace: with open(path, 'wb') as f: f.write( jsontree.dumps(self.finished, sort_keys=True,indent=4,separators=(',' , ': ')) ) else: if os.path.exists(path): # already exists, error print '%s already exists, change name please!' % path else: with open(path, 'wb') as f: f.write( jsontree.dumps(self.finished, sort_keys=True,indent=4,separators=(',' , ': ')) )
def unit_test(): data = {'a': [1,2,3]} prt_jt(d2j(data)) # The speed comparison of Simple Dumps and Dumps with Pretty Indent import time d = {key: list(xrange(1000)) for key in xrange(1000)} st = time.clock() jt1 = jsontree.dumps(d) # 10 times faster than dumps with pretty indent print time.clock()-st st = time.clock() jt2 = jsontree.dumps(d, sort_keys=True,indent=4,separators=(',' , ': ')) print time.clock()-st
def dump_jt(jt, fname): with open(fname, 'wb') as f: f.write( jsontree.dumps(jt, sort_keys=True, indent=4, separators=(',', ': ')))
def dump_jt(jt, fname, fastmode = False, replace = False): '''dump json object to file. dump without pretty indent can be faster when fastmode = True Replace existing file when replace = True''' if os.path.exists(fname): # if exists, check replace option if replace: # replace existing file with open(fname, 'wb') as f: if fastmode: f.write(jsontree.dumps(jt)) else: f.write(jsontree.dumps(jt, sort_keys=True,indent=4,separators=(',' , ': '))) else: # stop, print error message print '%s already exists' % fname else: # if not exists, just write to it with open(fname, 'wb') as f: if fastmode: f.write(jsontree.dumps(jt)) else: f.write(jsontree.dumps(jt, sort_keys=True,indent=4,separators=(',' , ': ')))
def modify_markupset(self, oldset, newset): JSON = jsontree.loads(self.to_json()) JSON['markups'][newset] = JSON['markups'].pop(oldset) for val in JSON['nonterminals'].values(): if val['markup'].has_key(oldset): val['markup'][newset] = val['markup'].pop(oldset) test_str = jsontree.dumps(JSON) new = from_json(test_str) self.__dict__ = new.__dict__
def _dump_todo(self, path, replace=False): '''dump taskplanner.todo to local file. When replace = False, existing local file will not be overwrite. For safety ''' if replace: with open(path, 'wb') as f: f.write( jsontree.dumps(self.todo, sort_keys=True, indent=4, separators=(',', ': '))) else: if os.path.exists(path): # already exists, error print '%s already exists, change name please!' % path else: with open(path, 'wb') as f: f.write( jsontree.dumps(self.todo, sort_keys=True, indent=4, separators=(',', ': ')))
def rename_nonterminal_symbol(self, old_symbol_name, new_symbol_name): JSON = jsontree.loads(self.to_json()) try: JSON['nonterminals'][new_symbol_name] = JSON['nonterminals'].pop(old_symbol_name) except KeyError: # This is a dumb fix for the problem of enter keypress events on the add-symbol input # firing many times, triggering potentially dozens or hundreds of requests for an # already renamed symbol to be renamed again pass test_str = jsontree.dumps(JSON) test_str = test_str.replace("[[{0}]]".format(old_symbol_name), "[[{0}]]".format(new_symbol_name)) new = from_json(test_str) self.__dict__ = new.__dict__
def remove_tag(self, tagset_name, tag_name): """Remove the given tag from the grammar.""" grammar_dictionary = jsontree.loads(self.to_json()) grammar_dictionary['markups'][tagset_name].remove(tag_name) for nonterminal_symbol in grammar_dictionary['nonterminals'].values(): if tagset_name in nonterminal_symbol['markup']: try: nonterminal_symbol['markup'][tagset_name].remove(tag_name) except ValueError: pass test_str = jsontree.dumps(grammar_dictionary) new = from_json(test_str) self.__dict__ = new.__dict__
def update(self, _new_value): """Add to node config "raw" value with data from wireless nodes""" new_value = jsontree.clone(_new_value) try: if self.nodescfg.has_key(new_value.name): for k,v in self.nodescfg[new_value.name].sensors.iteritems(): self.nodescfg[new_value.name].sensors[k].raw = new_value[k] else: return except AttributeError: return #self.nodescfg = OrderedDict(self.nodescfg) return jsontree.dumps(self.nodescfg,sort_keys=True)
def rename_tag(self, tagset_name, old_tag_name, new_tag_name): grammar_dictionary = jsontree.loads(self.to_json()) index = grammar_dictionary['markups'][tagset_name].index(old_tag_name) grammar_dictionary['markups'][tagset_name][index] = new_tag_name for nonterminal_symbol in grammar_dictionary['nonterminals'].values(): if tagset_name in nonterminal_symbol['markup']: try: index = nonterminal_symbol['markup'][tagset_name].index(old_tag_name) nonterminal_symbol['markup'][tagset_name][index] = new_tag_name except ValueError: pass test_str = jsontree.dumps(grammar_dictionary) new = from_json(test_str) self.__dict__ = new.__dict__
def send_timescale(metric_name,data,value,POSTGRES_TABLE_NAME_JOBS): json_data = jsontree.dumps(data) try: con = psycopg2.connect(host=POSTGRES_DB_HOST, port=POSTGRES_DB_PORT, database=POSTGRES_DB_NAME, user=POSTGRES_DB_USER, password=POSTGRES_DB_PASSWORD ) con.autocommit = True except: print('Cant connect to db',file=sys.stderr) pg_timestamp = datetime.datetime.utcnow() #print(json_data,file=sys.stderr) with con: cur = con.cursor() query = "INSERT INTO %s (time, metric_name,data,value) VALUES (%%s, %%s, %%s, %%s)" % POSTGRES_TABLE_NAME_JOBS cur.execute(query, (pg_timestamp, metric_name,json_data,value))
def delete_nonterminal(self, nonterminal): def filterrule(rule): if "[[{0}]]".format(nonterminal) in rule['expansion']: return 0 else: return 1 JSON = jsontree.loads(self.to_json()) JSON['nonterminals'].pop(nonterminal) # delete nonterminal for val in JSON['nonterminals'].values(): val['rules'] = filter(filterrule, val['rules']) test_str = jsontree.dumps(JSON) new = from_json(test_str) self.__dict__ = new.__dict__
if np.random.rand() <= pr: ## shuffle 每周作息时间 np.random.shuffle(uv_step1) step_value = normal(uv_step1[dt.weekday()]) step[strdt(dt)] = step_value else: ## 按照标准作息时间来 step_value = normal(uv_step[dt.weekday()]) step[strdt(dt)] = step_value ''' Step3 随机选择部分日子,偷懒或者努力 ''' np.random.permutation(keys) lazydays, workoutdays = keys[0:29], keys[ 30:44] ## 随机取30天作为lazyday, 15天作为workoutday for i in lazydays: ## 这些天会偷懒 step[i] = normal([np.random.randint(1750, 2250), 300]) for i in workoutdays: ## 这些天会努力 step[i] = normal([np.random.randint(7000, 9000), 1500]) ''' Step4 把step转换为cal和mile, 转换系数在小范围内服从正太分布 ''' for k, v in step.iteritems(): mile[k] = v / (normal((2100, 50))) cal[k] = v / (normal((22, 1))) # vam.append( (dt, normal(uv_vam[dt.weekday()]) ) ) # ppt.pprint(step) ''' Step5 保存数据到本地文件 ''' data = {'step': step, 'mile': mile, 'calories': cal} sdata = jsontree.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')) with open('fitbit2014.json', 'wb') as f: f.write(sdata)
def prt_jt(jt): '''pretty print json object''' print jsontree.dumps(jt, sort_keys=True,indent=4,separators=(',' , ': '))
def dict_to_json(self, dictionary): return jsontree.loads(jsontree.dumps(dictionary))
root = 'data' for fname in os.listdir(root): ## 比较典型的 关键字作为数据录入的开关的生成器 name, _ = os.path.splitext(fname) with open(os.path.join(root, fname), 'rb') as f: ctt = list() counter = 0 start_flag = 0 for line in f.xreadlines(): line = line.strip() if 'Step' in line: counter += 1 start_flag = 1 ctt.append(list()) continue if (start_flag == 1) & (counter >= 0): ctt[counter-1].append(line) counter = 1 for l in ctt: data[name]['step ' + str(counter)] = '\n'.join(l) counter += 1 str = jsontree.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')) with open('hedis.json', 'wb') as f: f.write(str)
def dump_jt(jt, fname): with open(fname, 'wb') as f: f.write(jsontree.dumps(jt, sort_keys=True,indent=4,separators=(',' , ': ')))
def prt_jt(self, jt): print jsontree.dumps(jt, sort_keys=True,indent=4,separators=(',' , ': '))
def prt_jt(jt): return jsontree.dumps(jt, sort_keys=True,indent=4,separators=(',' , ': '))
def dumpJSON(cls, json_obj): print(jsontree.dumps(json_obj, indent=4))
def write_json(cls, json_obj, filepath): json_txt = jsontree.dumps(json_obj, indent=4) #print (all_json) with open(filepath, 'w') as file: file.write(json_txt)
def prt_jt(jt): print jsontree.dumps(jt, sort_keys=True, indent=4, separators=(',', ': '))
def d2j(dictionary): '''transform a dict data to jsontree object''' return jsontree.loads(jsontree.dumps(dictionary))