def parse_posts(self, allPosts, searchWithAndBetweenWords, words_to_saerch): print words_to_saerch listOfAllPosts = [] finalPostsToDisplay = [] for posts in allPosts: for i in posts: if (i.get('message') is None) or (any( i.get('message') in pst.getMessage() for pst in listOfAllPosts)): print i.get('message') pass else: #encodeString = i.get('message') try: encodeString = i.get('message').encode('utf8') except: encodeString = unicode(i.get('message')).encode('utf8') if searchWithAndBetweenWords != 'on': if any(word in encodeString for word in words_to_saerch): post = Post( i.get('from').get('name'), i.get('message'), i.get('to').get('data')[0].get('name'), i.get('created_time')) listOfAllPosts.append(post) else: #If set to ON if all(word in encodeString for word in words_to_saerch): post = Post( i.get('from').get('name'), i.get('message'), i.get('to').get('data')[0].get('name'), i.get('created_time')) listOfAllPosts.append(post) return listOfAllPosts
def UserAddPost(self, name, size): done = False shown = False while not done: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT or checkForkey( "q"): # If user clicked close done = True # Flag that we are done so we exit this loop if not shown: x, y = pygame.mouse.get_pos() if self.isMouseInWindow(x, y): if any(pygame.mouse.get_pressed() ): # if the mouse was pressed if self.checkPress( name, x, y, size): # check where and add the post shown = True # the post been posted wait(0.1) # so the click on the mouse will stop if self.canAdd( x, y, size ): # if can add the post where the mouse is, draw the post in black p = Post(name, [x, y], size) self.addPost(p) else: # if can't add the post where the mouse is, draw the post in red p = Post(name, [x, y], size, color=RED) self.addPost(p) self.draw( ) # draw the post and then remove it just to show to user when he can add the post self.removePost(p) pygame.display.quit() # if the user press on exit so quit if shown: # if the post, posted return return for child in self.children: # if not try to add it in the sub boards child.init() child.UserAddPost(name, size)
def computeQCriterion(array): vars0 = [ 'gradxVelocityX', 'gradyVelocityX', 'gradzVelocityX', 'gradxVelocityY', 'gradyVelocityY', 'gradzVelocityY', 'gradxVelocityZ', 'gradyVelocityZ', 'gradzVelocityZ' ] # Compute velocity presvx = KCore.isNamePresent(array, 'VelocityX') if presvx == -1: ret = P.computeVariables(array, ['VelocityX', 'VelocityY', 'VelocityZ']) a = C.addVars([array, ret]) else: a = array # compute gradient of velocity presgx = KCore.isNamePresent(a, 'gradxVelocityX') if presgx == -1: gradU = P.computeGrad(a, 'VelocityX') gradV = P.computeGrad(a, 'VelocityY') gradW = P.computeGrad(a, 'VelocityZ') grads = C.addVars([gradU, gradV, gradW]) else: grads = C.extractVars(a, vars0) grads = C.initVars( grads, 'QCriterion=-0.5*({gradxVelocityX}*{gradxVelocityX}+{gradyVelocityY}*{gradyVelocityY}+{gradzVelocityZ}*{gradzVelocityZ}+2*{gradyVelocityX}*{gradxVelocityY}+2*{gradzVelocityX}*{gradxVelocityZ}+2*{gradzVelocityY}*{gradyVelocityZ})' ) ret = C.extractVars(grads, ['QCriterion']) return ret
def new_blog(): if not current_user.is_admin: return render_extensions('401.html') if request.method == 'POST': try: content = str(request.form['content']) except Exception: content = '' try: slug = str(request.form['slug']) except Exception: slug = '' try: title = str(request.form['title']) except Exception: title = '' post = Post(title=title, body=content, slug=slug) post.save() current_user.posts.append(post) current_user.save() return render_extensions('admin/new_blog.html')
def edit_blog(blog_id): if not current_user.is_admin: return render_extensions('401.html') if request.method == 'POST': try: content = str(request.form['content']) except Exception: content = '' try: slug = str(request.form['slug']) except Exception: slug = '' try: title = str(request.form['title']) except Exception: title = '' post = Post(title=title, body=content, slug=slug) post.save() current_user.posts.append(post) current_user.save() post_obj = Post.query.filter_by(id=int(blog_id)).first() post_content = { 'title': str(post_obj.title), 'slug': str(post_obj.slug), 'body': str(post_obj.body), } return render_extensions('admin/edit_blog.html', post=post_content)
def regular_processor(sorted_sim, max_iteration = 100, iteration_magnitude=25, thresholds = [ 0.1, 0.5, 0.9, 1 ], subsample=0): stats = [ ('blank','N', 't', 'S', 'SH', 'Epi', 'D', 'proportion_cancer', 'sd_S', 'sd_SH', 'sd_Epi', 'sd_D', 'sd_in_proportion' ) ] # sort genomes # obtain genomes g = sorted_sim.sorted_genomes print('Processing started at '+str(time.ctime())) start_time = time.time() for i in range(1,max_iteration,10): print 'Sampling N=',iteration_magnitude*i for t in thresholds: # repeated estimation to_be_averaged = [] for r in range(0,10): to_be_processed, N_real, proportion_cancer = Post.split_genomes(g, N=iteration_magnitude*i, t=t) fa = Post.frequency_analyze(to_be_processed, subsample=subsample) to_be_averaged.append( Post.get_stats(*fa) + ( proportion_cancer, ) ) # to_be_averaged.append(proportion_cancer) print '->Completed processing a threshold=',t avgd = tuple(np.mean(np.array(to_be_averaged), axis=0)) sds = tuple(np.std(np.array(to_be_averaged), axis=0)) this_iteration = (0,N_real, t) + avgd + sds stats.append(this_iteration) print 'Completed sampling of N=',iteration_magnitude*i sys.stdout.flush() print('regular_processor successfully completed at '+str(time.ctime())) print('Total time taken:'+str((time.time() - start_time))+'seconds') return stats
def load(graph): with open('users_data.json') as f: data = json.load(f) graph.numUsers = 0 graph.maxUsers = int(data["maxUsers"]) graph.num_posts = int(data["num_posts"]) for one in data["Users"]: graph.AddUser( person.Person(one["name"], one["email"], one["password"], one["age"], one["location"], one["gender"])) graph.Users[-1].groups = [int(i) for i in one["groups"]] graph.Users[-1].posts = [int(i) for i in one["posts"]] graph.Users[-1].admin = [int(i) for i in one["admin"]] for x in one["requests_sent"]: graph.Users[-1].requests_sent[int(x)] = int( one["requests_sent"][x]) for x in one["requests_received"]: graph.Users[-1].requests_received[int(x)] = int( one["requests_received"][x]) graph.edges = [] for row in range(0, len(data["edges"])): graph.edges.append([]) for col in range(0, len(data["edges"][row])): graph.edges[row].append([ int(data["edges"][row][col][0]), int(data["edges"][row][col][1]) ]) for i in data["Posts"]: graph.Posts.append(post.Post(i["text"], int(i["user_id"]))) graph.Posts[-1].post_id = int(i["post_id"]) graph.Posts[-1].time = i["time"] for j in i["Comments"]: graph.Posts[-1].Comments.append( post.Comment(j["text"], int(j["user_id"]), int(j["post_id"]))) graph.Posts[-1].Comments[-1].time = j["time"] graph.Posts[-1].Comments[-1].comment_id = int(j["comment_id"]) for g in data["Groups"]: graph.Groups.append( group.Group(0, g["group_name"], g["group_description"])) graph.Groups[-1].admin_id = [int(i) for i in g["admin_id"]] graph.Groups[-1].group_id = int(g["group_id"]) graph.Groups[-1].group_members = [int(i) for i in g["group_members"]] graph.Groups[-1].group_posts = [int(i) for i in g["group_posts"]] graph.Groups[-1].group_count = int(g["group_count"])
def run(self): startime = time.time() while (time.time() - startime < self.T): for act in self.actList: if time.time() - startime > self.T: return if act[0] == "GET": self.__result.append( Get.Get(act[1].strip(), act[2].strip()).submit()) elif act[0] == "POST": self.__result.append( Post.Post(act[1].strip(), act[2].strip(), act[3].strip()).submit()) else: if not act[1] == "-1": time.sleep(int(act[1])) else: time.sleep(R.randint(0, 5)) while True: if Global.mutex.acquire(5): # print self.i Global.result.append(self.__result) Global.mutex.release() break else: time.sleep(R.randint(0, 3))
def post_checker(self, conn, cursor, i, p: Post): cursor.execute("""SELECT id FROM avito WHERE id_av=? AND price=?""", (p.id_elem, p.price)) if not cursor.fetchone(): try: p.phone, p.user_name = ( "", "") # self.get_phone_num(i.url, p.id_elem) cursor.execute( """SELECT price FROM avito WHERE id_av=? ORDER BY id DESC""", (p.id_elem, )) res = cursor.fetchall() if res: for r in res: p.last_price += f"{r[0]} -> " p.last_price += f"{p.price}" except Exception as exp: logging.error( f'Exception of type {type(exp).__name__!s} in post_checker(): {exp}' ) try: self.send_new_posts(p) except Exception as ex: logging.error( f'Exception of type {type(ex).__name__!s} in send_new_posts(): {ex}' ) else: cursor.execute( """INSERT INTO avito (id_av, price) VALUES(?, ?)""", (p.id_elem, p.price)) conn.commit()
def test_transition_friend_to_enemy(self): self.person1.friends = set() self.person2.friends = set() self.person1.friends.add(self.person2) self.person2.friends.add(self.person1) self.person1.personality.facets = Personality.PersonalityFacet() self.person2.personality.facets = Personality.PersonalityFacet() self.person1.affinity_map = { self.person2: self.m.friends_affinity * 0.5 } self.person1.personality.interests = { 1: self.m.friends_affinity, 2: self.m.enemies_affinity } posts_to_hate = [] for x in range(2): posts_to_hate.append(Post.Post(self.person2, [ 2, ])) self.person1.process_post(posts_to_hate[0]) self.assertTrue(self.person2 in self.person1.friends, "Person2 should now be person1's friend, but is not.") self.assertTrue(self.person2 not in self.person1.enemies, "Person2 should not be person1's enemy, but is.") self.person1.process_post(posts_to_hate[1]) self.assertTrue(self.person2 not in self.person1.friends, "Person2 should not be person1's friend, but is.") self.assertTrue(self.person2 in self.person1.enemies, "Person2 should be person1's enemy, but is not.")
def test_post_shares(self): self.person1.personality.repost_probability = 2 for x in range(2, 30): self.m.agents[x].online = True self.m.agents[x].personality.repost_probability = 0.5 self.m.agents[x].personality.probability_read_reposts = 0.5 self.person1.friends.add(self.m.agents[x]) self.person1.personality.interests = {1: 1, 2: -1} post_to_share = Post.Post(self.person2, [ 1, ]) self.person2.dispatch_post(post_to_share) self.person1.settle_reposts() total_reposts = 0 formed_opinion = 0 for x in range(2, 30): if self.m.agents[x].inbox != None: total_reposts += 1 self.m.agents[x].settle_reposts() if self.person2 in self.m.agents[x].affinity_map: formed_opinion += 1 self.assertGreater(total_reposts, 0, "More than 0 reposts should have occured") self.assertLess(total_reposts, 28, "Fewer than 28 reposts should have occured") self.assertGreater(formed_opinion, 0, "More than 0 opinions should have been formed") self.assertLess(formed_opinion, 28, "Fewer than 28 opinions should have been formed")
def nearest(a): f = P.exteriorFaces(a) f = T.translate(f, (0.15, 0., 0.)) if isinstance(a[0], list): a = a[0] hook = C.createHook(a, function='faceCenters') faces = C.nearestFaces(hook, f) C.freeHook(hook)
def timeline(self): while 1: options_list = [ ' 1. Add a Post ', ' 2. View All Posts ', ' 3. Back ' ] for item in options_list: print(item, end="") option = int(input("Enter option")) if option is 1: content = input("Enter Contents: ") postcol = PostList.PostList() post = Post.Post() post.content = content post.postBy = self.logged_in_user post.today = datetime.date.today() postcol.read() postcol.insert(post) postcol.write() elif option is 2: postcol = PostList.PostList() postcol.read() postcol.print_of_Friends(self.logged_in_user) elif option is 3: break
def modelPlots(self,m): # some of the plots can be generated using the standard # routine, with some modifications super(ConfTWSA,self).modelPlots(m) for page in self.layout.pages: for sec in page.figures.keys(): for fig in page.figures[sec]: fig.side = fig.side.replace("MEAN","ANOMALY MAGNITUDE") # bname = os.path.join(self.output_path,"%s_Benchmark.nc" % (self.name )) fname = os.path.join(self.output_path,"%s_%s.nc" % (self.name,m.name)) # get the HTML page page = [page for page in self.layout.pages if "MeanState" in page.name][0] if not os.path.isfile(bname): return if not os.path.isfile(fname): return obs = Variable(filename = bname, variable_name = "twsa", groupname = "MeanState") mod = Variable(filename = fname, variable_name = "twsa", groupname = "MeanState") for i,basin in enumerate(self.basins): page.addFigure("Spatially integrated regional mean", basin, "MNAME_global_%s.png" % basin, basin,False,longname=basin) fig,ax = plt.subplots(figsize=(6.8,2.8),tight_layout=True) ax.plot(obs.time/365+1850,obs.data[:,i],lw=2,color='k',alpha=0.5) ax.plot(mod.time/365+1850,mod.data[:,i],lw=2,color=m.color ) ax.grid() ax.set_ylabel(post.UnitStringToMatplotlib(obs.unit)) fig.savefig(os.path.join(self.output_path,"%s_global_%s.png" % (m.name,basin))) plt.close()
def check_events(db_conn): if isinstance(db_conn, sqlite3.Connection): current_date = dt.date.strftime(dt.date.today(), '%m/%d/%y') current_time = str(dt.datetime.now().time()).split(sep=':') current_time = str(current_time[0] + ':' + current_time[1]) print(current_time) events = db_conn.execute('select EventID, ScheduledDate, \ ScheduledTime, EventType, ID \ from ScheduledEvents') for row in events.fetchall(): if current_time == row['ScheduledTime']: Post.publish_post(db_conn, row['ID']) else: return
def computeVorticity(array): presvx = KCore.isNamePresent(array, 'VelocityX') if presvx == -1: ret = P.computeVariables(array, ['VelocityX', 'VelocityY', 'VelocityZ']) a = C.addVars([array, ret]) rot = P.computeCurl(a, ['VelocityX', 'VelocityY', 'VelocityZ']) else: rot = P.computeCurl(array, ['VelocityX', 'VelocityY', 'VelocityZ']) if isinstance(rot[0], list): for r in rot: r[0] = 'VorticityX,VorticityY,VorticityZ' else: rot[0] = 'VorticityX,VorticityY,VorticityZ' return rot
def post(self, post_id): post_key = db.Key.from_path("Post", int(post_id), parent=Post.blog_key()) post = db.get(post_key) if (self.request.get("change-title") or (self.request.get('change-content')) or (self.request.get('change-youtube'))): if post.creator == self.user.name: post.title = self.request.get('change-title') post.content = self.request.get("change-content") link = self.request.get("change-youtube") youtube = self.request.get('change-youtube') if not self.get_youtube(youtube): error = "Requires a valid Youtube URL" return self.render("edit_post.html", post=post, error=error) else: link = self.get_youtube(youtube) post.youtube = link post.put() time.sleep(0.1) return self.redirect("/%s" % post_id) if (self.request.get("cancel_edit_post")): return self.redirect("/%s" % post_id)
def identify(a): f = P.exteriorElts(a) if (isinstance(a[0], list) == True): a = a[0] hook = C.createHook(a, function='elementCenters') elts = C.identifyElements(hook, f) C.freeHook(hook) return elts
def GlstatPlot(wkdir): imagedir = wkdir + '\\image' if not os.path.exists(imagedir): os.mkdir(wkdir + '\\image') file = r'\\glstat' TestFile = Glstate(wkdir + file, 0, 0, 0, 0) res = TestFile.run PltData = res[[ 'time', 'kinetic energy', 'internal energy', 'total energy', 'external work' ]] res_plot = Post.CurvePlot(' ', 'time', 'kinetic energy', -1, 1, 111, 0.3, PltData, legend=True) res_plot.frame res_plot.maxmin() pic = wkdir + '\image' + file # plt.show() plt.savefig(pic, dpi=100) print '#' * 20 print 'GLSTAT is plotted: %s' % (pic) print '#' * 20
def get_content(self, index): entry = self.get_entry(index) if (entry is None): return None path = self.mailbox.path_of(entry.filename) post = Post.Post(path, entry, True) return post
def identify(a): a = C.convertArray2NGon(a) f = P.exteriorElts(a) if isinstance(a[0], list): a = a[0] hook = C.createHook(a, function='faceCenters') faces = C.identifyFaces(hook, f) C.freeHook(hook) return faces
def run(self): start = len(self.post_dir) files = self.get_all_post_file(self.post_dir) for file in files: categories = file[start:].split('/')[1:-1] print('file: %s' % file) Post.Post(file, categories).run() print('complete!')
def main(argv): try: if len(sys.argv) < 3: if sys.argv[1] == "-h" or sys.argv[1] == "--help": usage() sys.exit() else: clierror() sys.exit(2) else: if sys.argv[2] != "1" and sys.argv[2] != "2": # check the input clierror() sys.exit(2) else: opts, args = getopt.getopt(argv, "so:pc", ["show", "ofile=", "post", "copy"]) lyrics = Get.getlyrics(sys.argv[1], sys.argv[2]) except getopt.GetoptError: print("error: missing a mandatory option.Use -h or --help for help") sys.exit(2) for opt, arg in opts: if opt in ("-s", "--show"): print(lyrics) elif opt in ("-o", "--ofile"): out.out(arg, lyrics) elif opt in ("-p", "--post"): try: for i in range(0, 100): input(i + 1) Post.post("♬" + lyrics.splitlines()[i][11:].replace(" ", "")) print("♬" + lyrics.splitlines()[i][11:].replace(" ", "")) except: print("输出完成") elif opt in ("-c", "--copy"): try: for i in range(1, 100): Copy.inputtxt("♬" + lyrics.splitlines()[i][11:].replace(" ", "")) print(lyrics.splitlines()[i][11:].replace(" ", "")) input(i + 1) except: print("输出完成")
def nearest(a): f = P.exteriorElts(a) f = T.translate(f, (0.15, 0., 0.)) if (isinstance(a[0], list) == True): a = a[0] hook = C.createHook(a, function='elementCenters') elts = C.nearestElements(hook, f) C.freeHook(hook) return elts
def checkPress(self, name, x, y, size): if self.canAdd(x, y, size): p = Post(name, [x, y], size) self.addPost(p) self.draw() return True else: return False
def post(self): global db_timer if not self.user: self.redirect('/blog') subject = self.request.get('subject') content = self.request.get('content') if subject and content: p = Post(parent = blog_key(), subject = subject, content = content) p.put() Post.allPosts(True) db_timer = time.time() self.redirect('/blog/%s' % str(p.key().id())) else: error = "subject and content, please!" self.render("newpost.html", subject=subject, content=content, error=error)
def post(self): if not self.user: return self.redirect("/login") title = self.request.get("title") content = self.request.get("content") youtube = None if self.request.get("youtube"): link = self.request.get("youtube") try: checkQ = link.split("/")[3] if '//youtu.be' in link: checkQ = checkQ.replace("?", "&") vid_id, times = checkQ.split("&") times = map(int, re.findall(r'\d+', times)) starting = 60 * times[0] + times[1] checkQ = "%s?start=%s" % (vid_id, starting) else: checkQ = link.split('=')[1] youtube = checkQ except IndexError: error = "Requires a valid Youtube Link!" return self.render("new_post.html", title=title, content=content, error=error) if title and content: creator = self.user.name posting = Post.Post(parent=Post.blog_key(), title=title, content=content, youtube=youtube, creator=creator) posting.put() posting_id = posting.key().id() time.sleep(0.1) return self.redirect("/%s" % str(posting_id)) else: error = "Each post requires both a title and content!" return self.render("new_post.html", title=title, content=content, error=error)
def get(self, post_id): post_key = db.Key.from_path("Post", int(post_id), parent=Post.blog_key()) post = db.get(post_key) if not self.user: return self.redirect('/login') if not (self.user.name == post.creator): error = "Only the author of the post can edit this post." return self.render("permalink.html", post=post, error=error) return self.render('edit_post.html', post=post)
def fetchPostReply(self, code, theme): self.re_connect() record = list() sql = "SELECT sender, MSG, time FROM poster WHERE code = %s AND theme = %s AND type = %s" args = (code, theme, "reply") self.cursor.execute(sql, args) for i in range(0, self.cursor.rowcount): row = self.cursor.fetchone() record.append(Post.Post(row[0], theme, row[1], row[2], "reply")) return record
def fenxiang_dazhaohu_liuyan(driver, date): stri = u"发起分享" fenxiang = Post.get_numbers(driver, stri.encode("utf-8"), date); stri = u"打招呼" dazhao = Post.get_numbers(driver, stri.encode("utf-8"), date); stri = u"留言" liuyan = Post.get_numbers(driver, stri.encode("utf-8"), date); print "before: fenxiang(" + str(fenxiang) + ")dazhaohu(" + str(dazhao) + ")liuyan(" + str(liuyan) + ")" if ((fenxiang >= 3) and (dazhao >= 5) and (liuyan >= 5)): return #goto XianLiao and find posts PostList = Post.prepare_post_list(driver) for post in PostList: try: ids = getIds(driver, post); except: continue if (fenxiang < 3): try: Post.fen_xiang(driver, ids[0]) fenxiang += 1 except: pass if (dazhao < 5): try: Post.da_zhao_hu(driver, ids[1]) dazhao += 1 except: pass if (liuyan < 5): try: Post.liu_yan(driver, ids[1]) liuyan += 1 except: pass print "after: fenxiang(" + str(fenxiang) + ")dazhaohu(" + str(dazhao) + ")liuyan(" + str(liuyan) + ")" return
def post(self, post_id): post_key = db.Key.from_path("Post", int(post_id), parent=Post.blog_key()) post = db.get(post_key) if self.request.get("cancel_delete"): return self.redirect("/%s" % post_id) if self.request.get('delete_post'): db.delete(post_key) time.sleep(0.1) return self.redirect("/")
def run(self): startime = time.time() pt = Post.Post(self.url, self.path, self.postdata) while (time.time() - startime < self.T): self.__result.append(pt.submit()) while True: if Global.mutex.acquire(5): # print self.i Global.result.append(self.__result) Global.mutex.release() break else: time.sleep(R.randint(0, 3))
def get_post(self, id): post = Post(id, self.name) post.load() return post
def add_post(self, text, metainfo): """ Extract the frequency of words of text and create a Post TODO extract related words Args: text (str): main text or the post metainfo (dictionary): title, number of likes and shares, etc """ text = text.encode('ascii','ignore') #sentimental analysis t = TextBlob(text) metainfo["polarity"] = t.sentiment.polarity metainfo["subjectivity"] = t.sentiment.subjectivity vader = vaderSentiment(text) metainfo["vader"] = vader text = text.translate(string.maketrans("",""), string.punctuation) #removing stop words stop = stopwords.words('english') #frequency in english language english_freq = {} reverse_stem = {} list_words = [] for i in text.split(): i = i.lower() if i not in stop: freq = wikiwords.freq(i, lambda x: 0.000001) st = self.stemmer.stem(i) if not reverse_stem.has_key(st): reverse_stem[st] = i if english_freq.has_key(st):english_freq[st] += freq else: english_freq[st] = freq list_words.append(st) #get frequencies of words frequencies = FreqDist(list_words) main_words = [] for word, count in frequencies.items(): english_freq[word] /= count #print reverse_stem[word] + ": " + str(english_freq[word]) tf_idf = (-1.0)*log(count+0.1)/(log(english_freq[word])) main_words.append([tf_idf, count, reverse_stem[word], word]) #select just most important words main_words.sort(reverse=True) NUM_MAX = 100 if len(main_words) > NUM_MAX: main_words = main_words[0:NUM_MAX] #create dict final_main_words = {} for w in main_words: final_main_words[w[3]] = w[0:3] self.last_id_added += 1 id = self.last_id_added post = Post(id, self.name, metainfo, frequencies, final_main_words, reverse_stem) post.save() self.update_index(list_words, frequencies) self.save() return post
def filter_upvotes(c, posts, n): return filter(lambda (post): c(Post.upvotes(post), n), posts)
def filter_title_length(c, posts, n): return filter(lambda (post): c(Post.title(post).length, n), posts)
def filter_time(c, posts, n): return filter(lambda(post): c(Post.post_time(post), n), posts)