def get_bucket(self, bucket, validate=True): """ Retrieve a bucket by name. Keyword arguments: bucket - The name of the bucket validate - If ``True``, the function will try to verify the bucket exists on the service-side. (Default: ``True``) """ if not validate: return Bucket(self, bucket) response = self.make_request("HEAD", bucket) if response.status == 200: return Bucket(self, bucket) elif response.status == 401: err = get_response_error(response) err.code = "invalid_access_key_id" err.message = "Request not authenticated, Access Key ID is either " \ "missing or invalid." raise err elif response.status == 403: err = get_response_error(response) err.code = "permission_denied" err.message = "You don't have enough permission to accomplish " \ "this request." raise err elif response.status == 404: err = get_response_error(response) err.code = "bucket_not_exists" err.message = "The bucket you are accessing doesn't exist." raise err else: err = get_response_error(response) raise err
def run_livecheck(repo_name): from bucket import Bucket from dupestats import DupeStats from authorset import AuthorSet from rewriter import rewritechart bucket = Bucket(path=repo_name, desc=f"{repo_name}-descriptors") bucket.extract() stat = DupeStats(path=repo_name, dump_file=f"dupestats_{repo_name}.json") stat.dupe_stats() authorSet = AuthorSet( chartdir=repo_name, authorset_charts=f"authorsets_{repo_name}_charts.json", authorset_maint=f"authorsets_{repo_name}_maint.json", authorset_email=f"authorsets_{repo_name}_emails.json", authorset_heatmap=f"authorsets_{repo_name}-heatmap.png", authorset_dot=f"authorsets_{repo_name}.dot", authorset_png=f"autorsets_{repo_name}.png", authorset_pdf=f"authorsets_{repo_name}.pdf", dupestats_charts=f"dupestats_{repo_name}.json") authorSet.preprocess() authorSet.process() authorSet.processproposals()
def __init__(self, tcpHandler): Bucket.__init__(self, tcpHandler) Bucket.bucketNbr = 0 Bucket.bucketList[0] = "{} {}".format(self.myHost, self.myPort) Bucket.coHost, Bucket.coPort = self.myHost, self.myPort Coordinator.totalBuckets = 1 self.server.serve_forever()
def setUp(self): self.bin = Bucket('test') c1 = Coin(1, 1) c2 = Coin(2, 2) c3 = Coin(3, 3) self.bin.add_coin(c1, 7) self.bin.add_coin(c2, 8) self.bin.add_coin(c3, 9)
def get_bucket_from_cluster(self, bucket, num_attempt=1, timeout=1): api = '%s%s%s?basic_stats=true' \ % (self.baseUrl, 'pools/default/buckets/', bucket.name) status, content, _ = self._http_request(api) num = 1 while not status and num_attempt > num: self.sleep("Try to get {0} again after {1} sec".format( api, timeout)) time.sleep(timeout) status, content, _ = self._http_request(api) num += 1 if status: parsed = json.loads(content) if 'vBucketServerMap' in parsed: vBucketServerMap = parsed['vBucketServerMap'] serverList = vBucketServerMap['serverList'] bucket.servers.extend(serverList) if "numReplicas" in vBucketServerMap: bucket.replicaNumber = vBucketServerMap["numReplicas"] # vBucketMapForward if 'vBucketMapForward' in vBucketServerMap: # let's gather the forward map vBucketMapForward = vBucketServerMap['vBucketMapForward'] counter = 0 for vbucket in vBucketMapForward: # there will be n number of replicas vbucketInfo = Bucket.vBucket() vbucketInfo.master = serverList[vbucket[0]] if vbucket: for i in range(1, len(vbucket)): if vbucket[i] != -1: vbucketInfo.replica.append( serverList[vbucket[i]]) vbucketInfo.id = counter counter += 1 bucket.forward_map.append(vbucketInfo) vBucketMap = vBucketServerMap['vBucketMap'] counter = 0 # Reset the list to avoid appending through multiple calls bucket.vbuckets = list() for vbucket in vBucketMap: # there will be n number of replicas vbucketInfo = Bucket.vBucket() vbucketInfo.master = serverList[vbucket[0]] if vbucket: for i in range(1, len(vbucket)): if vbucket[i] != -1: vbucketInfo.replica.append( serverList[vbucket[i]]) vbucketInfo.id = counter counter += 1 bucket.vbuckets.append(vbucketInfo) bucket.vbActiveNumNonResident = 100 if "vbActiveNumNonResident" in parsed["basicStats"]: bucket.vbActiveNumNonResident = \ parsed["basicStats"]["vbActiveNumNonResident"] bucket.maxTTL = parsed["maxTTL"] return bucket
def load_sync(path): """ Loads image from S3 :param string path: Path to load """ bucket = os.environ.get('AWS_LOADER_BUCKET') region = os.environ.get('AWS_REGION', 'eu-west-1') bucket_loader = Bucket(bucket, region) return bucket_loader.get(path)
def getBucketStats(bucketName, timeSlice): bucket_id = args.cluster + ":buckets:" + timeSlice rb = cb_bucket.get(bucket_id) if rb != '': for cur_bucket in rb.value: if cur_bucket["name"] == bucketName: bucket = Bucket(cur_bucket) docId = args.cluster + ":bucket:" + bucketName + ":stats:" + timeSlice rs = cb_bucket.get(docId) bucket.setStats(rs.value) return bucket
def __init__(self, debug, samples, requiredAccQty, queueSize, tokenArrivalSpeed, bucketCapacity, generatorType, lamb, tOn, tOff): self.debug = debug self.samples = samples self.requiredAccQty = requiredAccQty self.bucket = Bucket(tokenArrivalSpeed, bucketCapacity, bucketCapacity) if generatorType == "poisson": self.generator = PoissonGenerator(lamb) elif generatorType == "onoff": self.generator = OnOffGenerator(lamb, tOn, tOff) self.queue = QueueWrapper(queueSize)
def load_sync(path): """ Loads image from S3 :param string path: Path to load """ bucket = os.environ.get('AWS_LOADER_BUCKET') region = os.environ.get('AWS_REGION', 'eu-west-1') accessKeyId = os.environ.get('AWS_ACCESS_KEY_ID') secretAccessKey = os.environ.get('AWS_SECRET_KEY_ID') bucket_loader = Bucket(bucket, region, accessKeyId, secretAccessKey) return bucket_loader.get(path)
def remove_stopwords(tweet_text,file): tweet_tweet = tweet_text.lower() tweet = tweet_tweet.split() for palavra in tweet: for word_stop in STOPWORDS: if palavra == word_stop: tweet.remove(palavra) elif palavra.startswith('#'): Bucket.add_termo_in_bucket(palavra ) tweet_string = ' '.join(tweet) Bucket.add_ref_tweet_in_bucket(tweet, file) return unicodedata.normalize('NFKD', tweet_string).encode('ASCII', 'ignore')
def modify(self, tens, delBuckets=None, repBuckets=None): ''' len(delBuckets) + len(tens.shape()) - len(newBuckets) == len(self.tensor().shape()) Creates a copy of this Node with tens as its Tensor. Omits buckets at indices listed in delBuckets. Replaces Buckets at indices listed in repBuckets with new Bucket objects. Raises a ValueError if repBuckets and delBuckets contain overlapping elements. ''' if delBuckets is None: delBuckets = [] if repBuckets is None: repBuckets = [] assert self in self._network.topLevelNodes() assert len(set(delBuckets).intersection(set(repBuckets))) == 0 assert len(delBuckets) + len(tens.shape()) - \ len(self.tensor().shape()) >= 0 Buckets = [] for i, b in enumerate(self.buckets()): if i not in delBuckets: if i not in repBuckets: Buckets.append(b) else: Buckets.append(Bucket(self.network())) n = Node( tens, self._network, children=[self], Buckets=Buckets, logScalar=self._logScalar) return n
def collectItemAndChange(self): """collect item and change after buy in bucket""" bucket = Bucket() if self._collectItem(): self._totalSales = self._totalSales + self._currentItem.getPrice() bucket.setItem(self._currentItem) returnedChanges = self._collectChange() bucket.setChanges(returnedChanges) else: bucket.setItem(None) refund_changes = self._refund() bucket.setChanges(refund_changes) self._currentBalance = 0 self._currentItem = None self._currentlyInsertedCoins = [] return bucket
def main(): # bit stream: raw data bit_stream = [] bit_bucket = [] for i in range(10): bit_stream.append([]) bit_bucket.append(Bucket()) count = 0 number_stream = [] with open('cmsc5741_stream_data.txt', 'r') as file_read: integer_stream = read_stream_data(file_read) for data in integer_stream: update_bit_stream_and_bucket( data, bit_stream, bit_bucket) # function convert data into int # estimation estimation_sum = estimate_sum_of_last_hundred( bit_stream, bit_bucket) # real number_stream.insert(0, data) if len(number_stream) > 100: number_stream.pop() true_sum = sum(int(num) for num in number_stream) # output count = count + 1 print('%d true_sum = %d, estimate_sum = %d, error = %.3f%%' % (count, true_sum, estimation_sum, (estimation_sum - true_sum) / true_sum * 100)) # whenever output is needed output_bit_stream_and_bucket(bit_stream, bit_bucket)
def my_other_method(bucket: Bucket) -> tuple: bucket_name_prefix = bucket.get_prefix() bucket_name_1 = bucket_name_prefix + "_name_1" b1 = get_bucket(bucket_name_1) bucket_name_2 = bucket_name_prefix + "_name_2" b2 = get_bucket(bucket_name_2) return b1, b2
def execute(message): msg=message.decode("utf-8") msg.strip() lista = msg.split() print(lista) command = lista[0].upper() try: if command == "INSERT": Coordinator.insert(int(lista[1]), lista[2]) return "ACK" elif command == "QUERY": return Bucket.query(int(lista[1])) elif command == "REGISTER": bucketNbr = Coordinator.totalBuckets Coordinator.totalBuckets += 1 Bucket.bucketList[bucketNbr]="{0} {1}".format( lista[1], lista[2]) print(Bucket.bucketList) return "{}".format(bucketNbr) elif command == "POPULATE": return "POPULATION "+' '.join("{} {}".format(k,v) for k,v in Bucket.bucketList.items()) elif command == "SPLIT": Coordinator.split() return "ACK" else: return "NOPE" except KeyError: return "key error"
def __generate_buckets(self, dna, number_of_buckets): score_cards = dna.structure bucket_size = len(score_cards) / number_of_buckets iterable = iter(score_cards) buckets = list(iter(lambda: tuple(islice(iterable, bucket_size)), ())) buckets = map(lambda x: Bucket(x), buckets) return buckets
def __init__(self, query_date=None): """ 基本的工具, :param query_date: """ self.bucket = Bucket() self.query_date = date.today().strftime( '%Y-%m-%d') if query_date is None else query_date self.aws_path = SETTINGS['AWS_PATH'].format( dt=self.query_date.replace('-', '')) if not exists(dirname(self.aws_path)): os.makedirs(dirname(self.aws_path)) # Base Mongo Config self.client = MongoClient(SETTINGS['MONGO_HOST'], SETTINGS['MONGO_PORT']) self.collection = self.client[SETTINGS['MONGO_DB']][ SETTINGS['MONGO_COLLECTION']] # Filtering Config filter_path = join(dirname(abspath(__file__)), 'filter') if not exists(filter_path): os.makedirs(filter_path) self.filter_filename = join(filter_path, 'filter.txt') self._get_unique_from_file()
def requestRehash(oldBucket, newBucket): if oldBucket == Bucket.bucketNbr: Bucket.rehash(Bucket.fs) else: bucketAddress = Bucket.bucketList[oldBucket].split() bucketHost, bucketPort = bucketAddress[0], int(bucketAddress[1]) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((bucketHost, bucketPort)) print("Connected to {}:{}".format(bucketHost, bucketPort)) data = "REHASH {}".format(Bucket.fs.extent) sock.sendall(bytes(data + "\n", "utf-8")) received = str(sock.recv(1024), "utf-8") finally: #Close connection sock.close()
def rebuild(self): old_buckets = self.buckets self.bucket_count *= 2 self.buckets = [Bucket() for _ in range(self.bucket_count)] self.size = 0 for bucket in old_buckets: for item in bucket: self.insert(item.key, item.value)
def getAllPossibleNextStates(volumes): volumeStates = [] # empty a single bucket and fill a single bucket for i in range(len(volumes)): # if bucket isn't empty, add state with this bucket empty if volumes[i] > 0: stateWithEmpty = [] for j in range(len(volumes)): if i == j: stateWithEmpty.append(0) else: stateWithEmpty.append(volumes[j]) volumeStates.append(stateWithEmpty) # if bucket isn't full, add state with this bucket filled if volumes[i] < capacities[i]: stateWithFill = [] for j in range(len(volumes)): if i == j: stateWithFill.append(capacities[j]) else: stateWithFill.append(volumes[j]) volumeStates.append(stateWithFill) # pour a bucket into another for i in range(len(volumes)): for j in range(len(volumes)): if i != j: newState = [] pouringBucket = Bucket(capacities[i], volumes[i]) acceptingBucket = Bucket(capacities[j], volumes[j]) pouringBucket.pourIn(acceptingBucket) # if pouring the bucket resulted in a transfer of water, add new state if pouringBucket.volume != volumes[i]: for k in range(len(volumes)): if k == i: newState.append(pouringBucket.volume) elif k == j: newState.append(acceptingBucket.volume) else: newState.append(volumes[k]) volumeStates.append(newState) return volumeStates
def bucket(self, bucket_name): """ 构造bucket对象 参数: bucket_name: bucket名称 """ b = Bucket(self, bucket_name) return b
def test_bucket_sync_multiple(self): myBucket = Bucket(20000, drain_period=86400) user1 = _randomBytes() user2 = _randomBytes() user3 = _randomBytes() self.assertTrue(myBucket.userCanSyncAmount(user1, 10000, 0)) self.assertTrue(myBucket.userCanSyncAmount(user2, 15000, 0)) self.assertTrue(myBucket.userCanSyncAmount(user3, 20000, 0)) self.assertTrue(myBucket.userCanSyncAmount(user1, 10000, 1000)) self.assertFalse(myBucket.userCanSyncAmount(user2, 15000, 10000)) self.assertTrue(myBucket.userCanSyncAmount(user3, 20000, 100000)) self.assertFalse(myBucket.userCanSyncAmount(user1, 10000, 10000)) self.assertTrue(myBucket.userCanSyncAmount(user2, 1000, 20000)) self.assertFalse(myBucket.userCanSyncAmount(user3, 20000, 110000))
def addNodeFromArray(self, arr): ''' Takes as input an array and constructs a Tensor and Node around it, then adds the Node to this Network. ''' t = Tensor(arr.shape, arr) return Node(t, self, Buckets=[Bucket(self) for _ in range(len(arr.shape))])
def assemble_orders(rein, job_ids): """ Take a list of job_ids and build their entire orders. The idea here is that one Job ID should allow us to query each available server for each document type that is associated with it, then filter out cruft by focusing on who's signed correctly. TODO: look for attempted changes in foundational info like participants public keys and redeem scripts. """ urls = Bucket.get_urls(rein) documents = [] arg_job_ids = ','.join(job_ids) for url in urls: # queries remote server for all docs associated with a job_id res = Document.get_documents_by_job_id(rein, url, arg_job_ids) if res: documents += res order_ids = {} order_id = None for job_id in job_ids: order_id = Order.get_order_id(rein, job_id) if not order_id: o = Order(job_id, testnet=rein.testnet) rein.session.add(o) rein.session.commit() order_id = Order.get_order_id(rein, job_id) order_ids[job_id] = order_id if not order_id: return 0 for document in documents: doc_type = Document.get_document_type(document) if not doc_type: rein.log.info('doc_type not detected') continue doc_hash = Document.calc_hash(document) job_id = Document.get_job_id(document) d = rein.session.query(Document).filter( Document.doc_hash == doc_hash).first() if d: d.set_order_id(order_ids[job_id]) rein.session.add(d) else: new_document = Document(rein, doc_type, document, order_id, 'remote', source_key=None, sig_verified=True, testnet=rein.testnet) rein.session.add(new_document) rein.session.commit() return len(documents)
class Storage(Storage): """ Custom storage for django. """ def __init__(self): self.bucket = Bucket() def _open(self, name, mode="rb"): name = self._normalize_name(name) return StorageFile(name, mode, self) def _save(self, name, content): name = self._normalize_name(name) try: self.bucket.save(name, content) except Error, e: raise IOError("Storage Error:{}".format(e)) return name
def requestRehash(oldBucket): if oldBucket == Bucket.bucketNbr: Bucket.rehash(Bucket.fs) else: bucketAddress = Bucket.bucketList[oldBucket].split() bucketHost, bucketPort = bucketAddress[0], int(bucketAddress[1]) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((bucketHost, bucketPort)) print("Connected to {}:{}".format(bucketHost, bucketPort)) data = "REHASH {}".format(Bucket.fs.extent) sock.sendall(bytes(data + "\n", "utf-8")) # received = str(sock.recv(1024), "utf-8") # Waiting for the response will cause a dead lock if the target # bucket would like to populate bucketList finally: #Close connection sock.close()
def __init__(self, water_level_setpoint, time_interval=0.1, maximum_flowrate=10, initial_water_amount=0, radius=15): ''' maximum_flowrate in litre/minute time_interval in seconds radius and water_level_setpoint are in cm ''' self.bucket = Bucket(radius=self.cm_to_m(radius), initial_water_amount=initial_water_amount) self.controller = Controller( set_point=self.cm_to_m(water_level_setpoint), water_level=self.bucket.water_level, maximum_flowrate=self.lpm_to_m3ps(maximum_flowrate)) self.time_interval = time_interval
def test_bucket_clean(self): myBucket = Bucket(20000, drain_period=86400) user1 = _randomBytes() user2 = _randomBytes() user3 = _randomBytes() self.assertTrue(myBucket.userCanSyncAmount(user1, 10000, 0)) self.assertTrue(myBucket.userCanSyncAmount(user2, 15000, 0)) self.assertTrue(myBucket.userCanSyncAmount(user3, 20000, 0)) myBucket.cleanUsers(50000) self.assertEqual(myBucket.userCount(), 2) self.assertTrue(myBucket.userCanSyncAmount(user1, 20000, 50000)) self.assertFalse(myBucket.userCanSyncAmount(user2, 20000, 50000)) self.assertFalse(myBucket.userCanSyncAmount(user3, 15000, 50000))
def splitBucket(self, bucket): """ splits the bucket mid way when its size goes beyond the limit, thereby creating two buckets @param bucket: Bucket object of the bucket to be split @type bucket: Kademlia.bucket.Bucket """ bucket.peerList.sort() mid = bucket.peerList[int(BUCKET_SIZE/2)].nodeId newBucket = Bucket(mid, bucket.maxValue) bucket.maxValue = mid - 1 self.bucketList.insert(self.bucketList.index(bucket) + 1, newBucket) # transfer nodes from bucket to newBucket for peer in bucket.peerList: if peer.nodeId > bucket.maxValue: newBucket.addPeer(peer) # remove the transferred nodes from the old bucket for peer in newBucket.peerList: bucket.removePeer(peer)
def __init__(self, *args, **kwargs): """""" self._n_bkts = kwargs.pop('n_bkts', None) super(Metabucket, self).__init__(*args, **kwargs) if self._n_bkts is None: self._n_bkts = super(Metabucket, self).n_bkts self._buckets = [Bucket(self._config, name='Sents-%d' % i) for i in xrange(self.n_bkts)] self._sizes = None self._data = None self._len2bkt = None return
def add_bucket(self): operation_time = time.time() self.buckets.append( Bucket(bucket_id=self.n_buckets, debug=self.debug, chrono=self.chrono, universe=self.universe, source=self.source, vectorizer=self.vectorizer, stemming=self.stemming)) self.n_buckets += 1 self.debug.log("\tFinished in: " + str(time.time() - operation_time))
class TweetVectorsHandler: """ Similar to the postings_handler, but simpler - builds a dictionary for tweets and their average Glove vectors and dumps them to the disk once MAX_SIZE is reached in memory. """ def __init__(self, config, first_bucket_index=0): self.bucket = Bucket() self.bucket_index = first_bucket_index self.size = 0 self.config = config def __flush_bucket(self, doc_dictionary): # just write to the desk and clean the bucket new_posting = [] for term in self.bucket.get_dict_terms(): new_posting.append(self.bucket.get_term_posting(term)[0]) doc_dictionary[term][5] = (self.bucket_index, len(new_posting) - 1) self.size = 0 self.bucket.clean_bucket() start_time = time.time() utils.save_obj(new_posting, "avgVector" + str(self.bucket_index)) # print("glove vector write time: ", time.time()-start_time) self.bucket_index += 1 def append_tweet(self, doc_id, vector, inverted_idx): # gets tweet vector for insert to the bucket self.bucket.append_tweet(doc_id, vector) self.size += 1 if self.size > MAX_SIZE: self.__flush_bucket(inverted_idx) def finish_indexing(self, doc_dictionary): self.__flush_bucket(doc_dictionary)
def register(): error = None register_from = RegisterForm() if request.method == 'POST': # print(request.form) if register_from.validate_on_submit(): # if True: ID = request.form.get('id') username = request.form.get('username') password = request.form.get('password1') password2 = request.form.get('password2') # f = form.photo.data filename = images.save(register_from.photo.data) print(ID, username, password, password2) # ID = request.form['ID'] # username = request.form['username'] # password = request.form['password'] # f.save(f.filename) query = datastore_client.query(kind='user') for entity in query.fetch(): # print('id',entity['id'], 'pd', entity['password']) if str(ID) == entity["id"]: error = "The ID already exists" return render_template('register.html', form=register_from, error=error) elif str(username) == entity['user_name']: error = "The username already exists" return render_template('register.html', form=register_from, error=error) temp_uploaded_url = Bucket(bucket_name, PATH_BASE + str(filename)).image_url store_user(ID, username, password, temp_uploaded_url) render_template('index.html', error=error) else: return render_template('register.html', form=register_from, error=error) return render_template('index.html') else: return render_template('register.html', form=register_from, error=error)
def run_game(): """The function that runs the game""" #Initialize pygame and settings pygame.init() ai_settings = Settings( ) #creating 'ai_settings' from our imported Settings class, which acts as the overall settings for our game #Initializing 'screen' surface screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) #Initializing screen size pygame.display.set_caption( "Apple Bucket Game") #Captioning our screen (the window we play in) bg_color = (ai_settings.bg_color) #Setting background color #Making a bucket and apple: bucket = Bucket(ai_settings, screen) apple = Apple(ai_settings, screen) #Creating an instance that represents our stats stats = GameStats(ai_settings) #Starting the main loop for the game: while True: abgf.check_events(ai_settings, screen, bucket) #Checking for events (player input) #If still have lives if stats.game_active: bucket.update() #Updates bucket position apple = abgf.update_apple( ai_settings, apple, screen, bucket, stats ) #Updating falling apple or redraws if it collided or hit bottom apple.blitme() abgf.update_screen(ai_settings, screen, bucket, apple) #Drawing updates things on the screen
def _split_bucket(self, old_bucket_index): """ Splits the specified bucket into two new buckets which together cover the same range in the key/ID space. """ # Resize the range of the current (old) bucket. old_bucket = self._buckets[old_bucket_index] split_point = old_bucket.range_max - ( old_bucket.range_max - old_bucket.range_min) / 2 # Create a new bucket to cover the range split off from the old # bucket. new_bucket = Bucket(split_point, old_bucket.range_max) old_bucket.range_max = split_point # Now, add the new bucket into the routing table. self._buckets.insert(old_bucket_index + 1, new_bucket) # Finally, copy all nodes that belong to the new bucket into it... for contact in old_bucket._contacts: if new_bucket.key_in_range(contact.network_id): new_bucket.add_contact(contact) # ...and remove them from the old bucket for contact in new_bucket._contacts: old_bucket.remove_contact(contact)
def test_bucket_sync_too_fast(self): myBucket = Bucket(20000, drain_period=86400) user = _randomBytes() self.assertTrue(myBucket.userCanSyncAmount(user, 10000, 1234)) self.assertEqual(myBucket.currentSizeForUser(user, 1234), 10000) self.assertTrue(myBucket.userCanSyncAmount(user, 10000, 1235)) self.assertFalse(myBucket.userCanSyncAmount(user, 10000, 1236))
def assemble_order(rein, document): """ Take one document and build the entire order based on it. The idea here is that one Job ID should allow us to query each available server for each document type that is associated with it, then filter out bogus shit by focusing on who's signed correct stuff. This kind of command can also look for attempted changes in foundational info like participants public keys and redeem scripts. If this works well, we can reduce how much data is required at each stage. Finally, we should be able to serialize a job from end to end so it can be easily reviewed by a mediator. """ parsed = parse_document(document.contents) if 'Job ID' not in parsed: return 0 job_id = parsed['Job ID'] urls = Bucket.get_urls(rein) documents = [] if job_id: for url in urls: # queries remote server for all docs associated with a job_id res = Document.get_documents_by_job_id(rein, url, job_id) if res: documents += res order_id = Order.get_order_id(rein, job_id) if not order_id: o = Order(job_id, testnet=rein.testnet) rein.session.add(o) rein.session.commit() for document in documents: doc_type = Document.get_document_type(document) if not doc_type: rein.log.info('doc_type not detected') continue doc_hash = Document.calc_hash(document) d = rein.session.query(Document).filter( Document.doc_hash == doc_hash).first() if d: d.set_order_id(order_id) rein.session.add(d) else: new_document = Document(rein, doc_type, document, order_id, 'remote', source_key=None, sig_verified=True, testnet=rein.testnet) rein.session.add(new_document) rein.session.commit() return len(documents)
def create_bucket(self, bucket, zone=Zone.DEFAULT): """ Create a new bucket. Keyword arguments: bucket - The name of the bucket zone - The zone at which bucket and its objects will locate. (Default: follow the service-side rule) """ headers = {"Location": zone} response = self.make_request("PUT", bucket, headers=headers) if response.status in [200, 201]: return Bucket(self, bucket) else: raise get_response_error(response)
def execute(message): msg=message.decode("utf-8") msg.strip() lista = msg.split() print(lista) command = lista[0].upper() if len(lista) > 1 and command in ("INSERT", "QUERY"): key = int(lista[1]) location = address(key, Bucket.fs) if location != Bucket.bucketNbr: return Bucket.forward(location, msg) try: if command == "INSERT": if len(lista) < 3: return "Invalid Command." else: return Coordinator.insert(int(lista[1]), lista[2]) #return "ACK" elif command == "QUERY": if len(lista) < 2: return "Invalid Command." else: return Bucket.query(int(lista[1])) elif command == "REGISTER": bucketNbr = Coordinator.totalBuckets Coordinator.totalBuckets += 1 Bucket.bucketList[bucketNbr] = "{0} {1}".format(lista[1], lista[2]) print(Bucket.bucketList) return "{}".format(bucketNbr) elif command == "POPULATE": return "POPULATION "+' '.join("{} {}".format(k,v) for k,v in Bucket.bucketList.items()) elif command == "SPLIT": return Coordinator.split() elif command == "SHOW": return Coordinator.show() else: return "NOPE" except KeyError: return "key error"
def assemble_order(rein, document): """ Take one document and build the entire order based on it. The idea here is that one Job ID should allow us to query each available server for each document type that is associated with it, then filter out bogus shit by focusing on who's signed correct stuff. This kind of command can also look for attempted changes in foundational info like participants public keys and redeem scripts. If this works well, we can reduce how much data is required at each stage. Finally, we should be able to serialize a job from end to end so it can be easily reviewed by a mediator. """ parsed = parse_document(document.contents) if 'Job ID' not in parsed: return 0 job_id = parsed['Job ID'] urls = Bucket.get_urls(rein) documents = [] if job_id: for url in urls: res = Document.get_documents_by_job_id(rein, url, job_id) if res: documents += res order_id = Order.get_order_id(rein, job_id) if not order_id: o = Order(job_id, testnet=rein.testnet) rein.session.add(o) rein.session.commit() for document in documents: doc_type = Document.get_document_type(document) if not doc_type: rein.log.info('doc_type not detected') continue doc_hash = Document.calc_hash(document) d = rein.session.query(Document).filter(Document.doc_hash == doc_hash).first() if d: d.set_order_id(order_id) else: new_document = Document(rein, doc_type, document, order_id, 'external', source_key=None, sig_verified=True, testnet=rein.testnet) rein.session.add(new_document) rein.session.commit() return len(documents)
class grapes: def __init__(self, debug): # Set up a clock for managing the frame rate. self.clock = pygame.time.Clock() self.state = 'START' # Setup game variables # self.background = Background(0, 0) # self.bucket = Bucket(-100, 100) self.grapes = [] self.spawnCount = 0 self.changeGoalCount = 0 self.paused = False self.debug = debug # Begin button variables self.startButtonX = 0 self.startButtonY = 0 self.startButtonSurface = None self.startButtonWidth = 0 self.startButtonHeight = 0 # Setup current level variables self.level = 0 self.score = 0 self.totalScore = 0 self.goalScore = 0 self.spawnTime = 0 self.goalResetTime = 0 self.grapeVelocity = 0 self.maxGrapesPerTick = 0 # Setup goal variables self.currentVerts = -1 self.currentDisplayGrape = None def set_paused(self, paused): self.paused = paused # Called to save the state of the game to the Journal. def write_file(self, file_path): pass # Called to load the state of the game from the Journal. def read_file(self, file_path): pass # Takes the player to the next level def nextLevel(self): # Increment total score self.totalScore += self.score # Increment the level and reset the level score self.level += 1 self.score = 0 # Calculate the goal score self.goalScore = self.level * self.level * 40 # Determine level index index = ((self.level - 1) % Background.TOTAL_LEVELS) + 1 # Calculate spawn rate, goal change rate, and fall speed maxCalcLevel = 15 calcLevel = self.level - 1 if calcLevel > maxCalcLevel: calcLevel = maxCalcLevel self.spawnTime = 45 - int((calcLevel * 3.5)) self.goalResetTime = 270 - int((calcLevel * 2)) self.grapeVelocity = 5 + int((calcLevel * 1.5)) self.maxGrapesPerTick = 1 + int((calcLevel / 1.5)) if self.spawnTime < 10: self.spawnTime = 10 if self.goalResetTime < 30: self.goalResetTime = 30 if self.grapeVelocity > 17: self.grapeVelocity = 17 if self.maxGrapesPerTick > 5: self.maxGrapesPerTick = 5 # Start the music pygame.mixer.music.stop() pygame.mixer.music.load("assets/levels/" + str(index) + "/music.ogg") pygame.mixer.music.play(-1) # Loop the music # Generate first goal self.generateNewGoal() # Generate a new goal for the player def generateNewGoal(self): self.currentVerts = random.randint(Grape.MIN_VERTS, Grape.MAX_VERTS) self.currentDisplayGrape = Grape(40, 10 + 26 + 80, self.currentVerts, 0) self.currentDisplayGrape.color = (25, 252, 0) self.randMod = random.randint(1, 5) # Spawns a grape def spawnGrape(self, width, offsetIndex): # Don't spawn grapes off the edge of the screen self.grapes.append(Grape(random.randrange(Grape.DEFAULT_RADIUS, width - Grape.DEFAULT_RADIUS), -Grape.DEFAULT_RADIUS * (offsetIndex + 1), random.randint(Grape.MIN_VERTS, Grape.MAX_VERTS), self.grapeVelocity)) # The main game loop. def run(self): self.running = True screen = pygame.display.get_surface() # These needed to be moved for the activity to work self.background = Background(0, 0) self.bucket = Bucket(-100, 100) # Load the font self.font = pygame.font.SysFont("monospace", 33) self.juiceFont = pygame.font.SysFont("monospace", 30) self.titleFont = pygame.font.SysFont("monospace", 120) # Mixer setup pygame.mixer.init() # Sound setup self.squishEffect = pygame.mixer.Sound('assets/squish.wav') self.incorrectEffect = pygame.mixer.Sound('assets/incorrect.wav') # Start the first level self.nextLevel() while self.running: # Pump GTK messages. while Gtk.events_pending(): Gtk.main_iteration() pos = pygame.mouse.get_pos() # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.VIDEORESIZE: pygame.display.set_mode(event.size, pygame.RESIZABLE) elif event.type == pygame.MOUSEMOTION and self.state == 'GAME': x, y = pos # Center the bucket x -= self.bucket.sprite.get_width() / 2 self.bucket.setPos(x, screen.get_height() * 0.8) elif event.type == pygame.KEYDOWN: # Shortcut to next level if self.debug and event.key == pygame.K_n: self.nextLevel() elif event.key == pygame.K_p: # Toggle pause status self.set_paused(not self.paused) elif event.type == pygame.MOUSEBUTTONDOWN and self.state == 'START': x, y = pos width, height = self.titleFont.size("Begin") if x > self.startButtonX and x < self.startButtonX + self.startButtonWidth and y > self.startButtonY and y < self.startButtonY + self.startButtonHeight: self.state = 'GAME' if self.state == 'START': self.background.draw(1, screen, False); titleText = "Grapes of Math" (titleWidth, titleHeight) = self.titleFont.size(titleText) title = self.titleFont.render(titleText, 1, (200, 200, 200)) screen.blit(title, (screen.get_width() / 2 - (titleWidth / 2), 50)) startText = "Begin" (self.startButtonWidth, self.startButtonHeight) = self.titleFont.size(startText) # Only generate this the first draw if self.startButtonX == 0: overlayColor = (0, 0, 0, 127) overlayRect = pygame.Rect(0, 0, self.startButtonWidth, self.startButtonHeight) overlaySurface = pygame.Surface((300, 160), pygame.SRCALPHA) overlaySurface.fill(overlayColor, overlayRect) self.startButtonX = (screen.get_width() / 2 - (self.startButtonWidth / 2)) self.startButtonY = 200 screen.blit(overlaySurface, (self.startButtonX, self.startButtonY)) startButton = self.titleFont.render(startText, 1, (200, 200, 200)) screen.blit(startButton, (self.startButtonX, self.startButtonY)) elif self.state == 'GAME': if not self.paused: # Spawn Grapes if self.spawnCount > random.randrange(self.spawnTime - 5, self.spawnTime): for i in range(0, random.randint(1, self.maxGrapesPerTick)): self.spawnGrape(screen.get_width(), i) self.spawnCount = 0 self.spawnCount += 1 # Change goal if self.changeGoalCount > random.randrange(self.goalResetTime - 7, self.goalResetTime): self.generateNewGoal() self.changeGoalCount = 0 self.changeGoalCount += 1 # Clear Display screen.fill((255, 255, 255)) # 255 for white # Draw the background self.background.draw(self.level, screen, True) # Draw paused text if paused if self.paused: pauseText = "Paused" (pauseWidth, pauseHeight) = self.titleFont.size(pauseText) pauseLabel = self.titleFont.render(pauseText, 1, (255, 255, 255)) pauseX = (screen.get_width() / 2) - (pauseWidth / 2) pauseY = (screen.get_height() / 2) - (pauseHeight / 2) screen.blit(pauseLabel, (pauseX, pauseY)) # Draw the bucket self.bucket.draw(screen) clone = list(self.grapes) for i, g in enumerate(clone): if not self.paused: g.falling = True g.update() g.draw(screen) if self.bucket.catchGrape(g.x, g.y, g.r): # Delete the grape del self.grapes[i] # Check if the grape is correct if g.numVerts == self.currentVerts: self.score += int(g.value * 1.5) self.squishEffect.play() if self.score >= self.goalScore: self.nextLevel() else: self.score -= g.value / 3 if self.score < 0: self.score = 0 self.incorrectEffect.play() else: g.draw(screen) # Text drawing textX = 16 textY = 16 # Draw the current level text label = self.font.render("Level " + str(self.level), 1, (176, 229, 255)) screen.blit(label, (textX, textY)) textY += 26 # Draw the score label = self.juiceFont.render("Grape Juice: " + str(self.score) + " / " + str(self.goalScore), 1, (219, 140, 213)) screen.blit(label, (textX, textY)) textY += 26; # Draw the current goal levelText = "Collect grapes with " + str(self.currentVerts) + " sides" if self.level == 4: levelText = "Collect grapes with " + str(self.currentVerts + self.randMod) + ' - ' + str(self.randMod) + " sides" label = self.juiceFont.render(levelText, 1, (162, 252, 151)) screen.blit(label, (textX, textY)) # Only draw on level one if self.level == 1: # Draw the current goal self.currentDisplayGrape.draw(screen) # Flip Display pygame.display.flip() # Try to stay at 30 FPS self.clock.tick(30)
def run(self): self.running = True screen = pygame.display.get_surface() # These needed to be moved for the activity to work self.background = Background(0, 0) self.bucket = Bucket(-100, 100) # Load the font self.font = pygame.font.SysFont("monospace", 33) self.juiceFont = pygame.font.SysFont("monospace", 30) self.titleFont = pygame.font.SysFont("monospace", 120) # Mixer setup pygame.mixer.init() # Sound setup self.squishEffect = pygame.mixer.Sound('assets/squish.wav') self.incorrectEffect = pygame.mixer.Sound('assets/incorrect.wav') # Start the first level self.nextLevel() while self.running: # Pump GTK messages. while Gtk.events_pending(): Gtk.main_iteration() pos = pygame.mouse.get_pos() # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.VIDEORESIZE: pygame.display.set_mode(event.size, pygame.RESIZABLE) elif event.type == pygame.MOUSEMOTION and self.state == 'GAME': x, y = pos # Center the bucket x -= self.bucket.sprite.get_width() / 2 self.bucket.setPos(x, screen.get_height() * 0.8) elif event.type == pygame.KEYDOWN: # Shortcut to next level if self.debug and event.key == pygame.K_n: self.nextLevel() elif event.key == pygame.K_p: # Toggle pause status self.set_paused(not self.paused) elif event.type == pygame.MOUSEBUTTONDOWN and self.state == 'START': x, y = pos width, height = self.titleFont.size("Begin") if x > self.startButtonX and x < self.startButtonX + self.startButtonWidth and y > self.startButtonY and y < self.startButtonY + self.startButtonHeight: self.state = 'GAME' if self.state == 'START': self.background.draw(1, screen, False); titleText = "Grapes of Math" (titleWidth, titleHeight) = self.titleFont.size(titleText) title = self.titleFont.render(titleText, 1, (200, 200, 200)) screen.blit(title, (screen.get_width() / 2 - (titleWidth / 2), 50)) startText = "Begin" (self.startButtonWidth, self.startButtonHeight) = self.titleFont.size(startText) # Only generate this the first draw if self.startButtonX == 0: overlayColor = (0, 0, 0, 127) overlayRect = pygame.Rect(0, 0, self.startButtonWidth, self.startButtonHeight) overlaySurface = pygame.Surface((300, 160), pygame.SRCALPHA) overlaySurface.fill(overlayColor, overlayRect) self.startButtonX = (screen.get_width() / 2 - (self.startButtonWidth / 2)) self.startButtonY = 200 screen.blit(overlaySurface, (self.startButtonX, self.startButtonY)) startButton = self.titleFont.render(startText, 1, (200, 200, 200)) screen.blit(startButton, (self.startButtonX, self.startButtonY)) elif self.state == 'GAME': if not self.paused: # Spawn Grapes if self.spawnCount > random.randrange(self.spawnTime - 5, self.spawnTime): for i in range(0, random.randint(1, self.maxGrapesPerTick)): self.spawnGrape(screen.get_width(), i) self.spawnCount = 0 self.spawnCount += 1 # Change goal if self.changeGoalCount > random.randrange(self.goalResetTime - 7, self.goalResetTime): self.generateNewGoal() self.changeGoalCount = 0 self.changeGoalCount += 1 # Clear Display screen.fill((255, 255, 255)) # 255 for white # Draw the background self.background.draw(self.level, screen, True) # Draw paused text if paused if self.paused: pauseText = "Paused" (pauseWidth, pauseHeight) = self.titleFont.size(pauseText) pauseLabel = self.titleFont.render(pauseText, 1, (255, 255, 255)) pauseX = (screen.get_width() / 2) - (pauseWidth / 2) pauseY = (screen.get_height() / 2) - (pauseHeight / 2) screen.blit(pauseLabel, (pauseX, pauseY)) # Draw the bucket self.bucket.draw(screen) clone = list(self.grapes) for i, g in enumerate(clone): if not self.paused: g.falling = True g.update() g.draw(screen) if self.bucket.catchGrape(g.x, g.y, g.r): # Delete the grape del self.grapes[i] # Check if the grape is correct if g.numVerts == self.currentVerts: self.score += int(g.value * 1.5) self.squishEffect.play() if self.score >= self.goalScore: self.nextLevel() else: self.score -= g.value / 3 if self.score < 0: self.score = 0 self.incorrectEffect.play() else: g.draw(screen) # Text drawing textX = 16 textY = 16 # Draw the current level text label = self.font.render("Level " + str(self.level), 1, (176, 229, 255)) screen.blit(label, (textX, textY)) textY += 26 # Draw the score label = self.juiceFont.render("Grape Juice: " + str(self.score) + " / " + str(self.goalScore), 1, (219, 140, 213)) screen.blit(label, (textX, textY)) textY += 26; # Draw the current goal levelText = "Collect grapes with " + str(self.currentVerts) + " sides" if self.level == 4: levelText = "Collect grapes with " + str(self.currentVerts + self.randMod) + ' - ' + str(self.randMod) + " sides" label = self.juiceFont.render(levelText, 1, (162, 252, 151)) screen.blit(label, (textX, textY)) # Only draw on level one if self.level == 1: # Draw the current goal self.currentDisplayGrape.draw(screen) # Flip Display pygame.display.flip() # Try to stay at 30 FPS self.clock.tick(30)
def test_sendtobucket_allfonts(self): '''TEST : Checking S3 Bucket data sending - all fonts''' match = exts + ('.afm', '.css') self.assertTrue(Bucket.send_to_bucket(self.testBucket, 'test11/test22/test33/', 'testfont/', match, 'testfont/'))
def test_sendtobucket_onefont(self): '''TEST : Checking S3 Bucket data sending - one font''' self.assertTrue(Bucket.send_to_bucket(self.testBucket, 'test1/test2/test3/', 'testfont/', '.ttf', 'testfont/'))
def __init__(self): self.bucket = Bucket()
class Simulation: def __init__(self, debug, samples, requiredAccQty, queueSize, tokenArrivalSpeed, bucketCapacity, generatorType, lamb, tOn, tOff): self.debug = debug self.samples = samples self.requiredAccQty = requiredAccQty self.bucket = Bucket(tokenArrivalSpeed, bucketCapacity, bucketCapacity) if generatorType == "poisson": self.generator = PoissonGenerator(lamb) elif generatorType == "onoff": self.generator = OnOffGenerator(lamb, tOn, tOff) self.queue = QueueWrapper(queueSize) def acceptRequest(self, req): req.acceptanceTime = self.currentTime self.accQty += 1 self.accTotalWaitTime += req.waitTime() logging.debug("Request %d accepted at %f. It waited %f.", req.id, self.currentTime, req.waitTime()) def rejectRequest(self, req): self.rejQty += 1 logging.debug("Request %d rejected at %f.", req.id, self.currentTime) def simulate(self): self.samplesTotalAverageWaitTime = 0.0 self.samplesTotalRejectedRatio = 0.0 for sample in xrange(0, self.samples): logging.debug("Starting the simulation: %d out of %d samples.", sample + 1, self.samples) self.currentTime = 0.0 self.generator.reset(self.currentTime) self.bucket.reset(self.currentTime) self.queue.reset() self.accQty = 0 self.rejQty = 0 self.accTotalWaitTime = 0.0 while self.accQty < self.requiredAccQty: self.performStep() if self.debug: sys.stdin.read(1) self.samplesTotalAverageWaitTime += (self.accTotalWaitTime / self.accQty) self.samplesTotalRejectedRatio += (1.0 * self.rejQty / (self.rejQty + self.accQty)) logging.debug("Sample %d results: D = %f, PB = %f", sample, (self.accTotalWaitTime / self.accQty), (1.0 * self.rejQty / (self.rejQty + self.accQty))) print "D = " + str(self.samplesTotalAverageWaitTime / self.samples) print "PB = " + str(self.samplesTotalRejectedRatio / self.samples) def performStep(self): if self.generator.nextRequestArrival < self.bucket.nextTokenArrival: self.currentTime = self.generator.nextRequestArrival req = self.generator.generateRequest(self.currentTime) logging.debug("A new request %d arrived at %f.", req.id, self.currentTime) if self.queue.type == "NoQueue": if self.bucket.getToken(): self.acceptRequest(req) else: self.rejectRequest(req) elif not self.queue.addRequest(req): self.rejectRequest(req) else: self.currentTime = self.bucket.nextTokenArrival self.bucket.addToken(self.currentTime) while not self.queue.noRequests(): if self.bucket.getToken(): req = self.queue.getRequest() self.acceptRequest(req) else: break