def __init__(self): self.offices = {} self.livingrooms = {} self.room_capacity = {} self.fellows = [] self.staff = [] self.rooms = {} self.all_employees = [] self.unallocated = [] self.accomodated_fellows = [] self.db = Database()
def main (): # Configure the application _load_config() # Connect to our database and all that good bullshit try: Database.connect(options.db_name, options.db_host, options.db_port) except Exception, e: logging.error('Could not connect to database[%s]: %s' % ( options.db_host, e )) return
def list_das_tag_by_date(cls, fromDate, toDate): listAdsTagDate = [] try: cn = Database.connection() cursor = cn.cursor(dictionary=True) sqlQuery = "select tag_id, ad_id, rev_real, click, imp " \ "from stats_ads_tags_date where date >= %s and date <= %s;" cursor.execute(sqlQuery, ("2019-07-01", "2019-07-30")) # cursor.execute(sqlQuery, (fromDate, toDate)) result = cursor.fetchall() if cursor.rowcount > 0: df = pd.DataFrame(result) df = pd.DataFrame( df.groupby(['tag_id', 'ad_id']).sum()[['rev_real', 'click', 'imp']]) print(df) # row.name[0] = tag_id , row.name[1] = ad_id for index, row in df.iterrows(): tagDate = ObjTagDate(row.name[0], row.name[1], row["rev_real"], row["click"], row["imp"]) listAdsTagDate.append(tagDate) except Exception as e: print("An exception occurred...." + e) finally: if cn.is_connected(): cursor.close() cn.close() return listAdsTagDate
def main(): # Load Configuration Options _load_config() # Daemonize and Write out our PID if options.environment != "dev": pid_file_path = _daemonize() # Connect to the MongoDB try: Database.connect(options.db_name, options.db_host, options.db_port) except Exception, e: logging.error("Could not connect to database: %s" % e) if options.environment != "dev": pid.remove(pid_file_path) sys.exit(1)
def create_ads_tags_operate(cls, recordsToInsert): print("recordsToInsert", recordsToInsert) try: cn = Database.connection() cursor = cn.cursor(dictionary=True) sqlQuery = " insert into operate_tag_ad_date(tag_id,ad_id,date,cpm_7,ctr_7)" \ " values(%s,%s,%s,%s,%s)" \ " on duplicate key update cpm_7=values(cpm_7), ctr_7=values(ctr_7)" cursor.executemany(sqlQuery, recordsToInsert) cn.commit() if cursor.rowcount > 0: print("rowcount :", cursor.rowcount) except Exception as e: print("Exception ..." + e) finally: if cn.is_connected(): cursor.close() cn.close()
def initDB(self): self.db = Database() self.db.createDB()
class MainWindow(QMainWindow): def __init__(self, mode): super().__init__() self.mode = mode self.initDB() self.initUI() def initUI(self): self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.btn_load.clicked.connect(self.loadData) if self.mode == "init": self.ui.btn_save.clicked.connect(self.parseData) elif self.mode == "load": # load flushed data dir_path = ROOT_DIR.replace("origin", "flush_20200722") self.ui.textbox_root.setText(dir_path) self.resetRoot(dir_path) self.ui.btn_save.clicked.connect(self.updateData) self.ui.textbox_root.returnPressed.connect(self.loadData) self.ui.tree_dataset.selectionModel().selectionChanged.connect(self.loadAudioFile) #self.ui.tree_dataset.doubleClicked.connect(self.loadAudioFile) #self.ui.textbox_root.textChanged.connect(self.resetRoot) #self.ui.tree_dataset.selectionModel().selectionChanged.connect(self.load_labeldata) self.loadLabel() def initDB(self): self.db = Database() self.db.createDB() def loadLabel(self): loc_dict = load_from_yaml(PATH_LOC) for loc in loc_dict['location']: self.ui.combo_location.addItem(loc['country']+' / '+loc['city']) for place in loc_dict['place']: item = QListWidgetItem() item.setFlags(item.flags() | Qt.ItemIsUserCheckable) item.setCheckState(Qt.Unchecked) item.setText(place) self.ui.list_location.addItem(item) def loadData(self): if self.ui.textbox_root.text() == '': self.ui.textbox_root.setText(ROOT_DIR) self.ui.model_dataset.setRootPath(ROOT_DIR) self.ui.tree_dataset.setRootIndex(self.ui.model_dataset.index(ROOT_DIR)) else: self.resetRoot(self.ui.textbox_root.text()) def parseData(self): if not os.path.exists(FLUSH_DIR): os.mkdir(FLUSH_DIR) try: audio = AudioSegment.from_file(self.file_path) loc = self.ui.combo_location.currentText().split(' / ') #fid = self.db.getCountFromTable("Recording") fid = self.db.queryFromTable("Recording", {'country': loc[0], "city": loc[1]}) fname = LOCAT_CODE[loc[0]]+'_'+LOCAT_CODE[loc[1]]+'_%.3d' %(fid+1) +'.wav' datetime_str = self.ui.textbox_timestamp.text() print('>>> Save as', fname) try: if " " in datetime_str: datetime_object = datetime.strptime(datetime_str, '%Y-%m-%d %H%M%S').strftime('%Y-%m-%d %H:%M:%S') else: datetime_object = datetime.strptime(datetime_str, '%Y-%m-%d') # Export as WAV audio.export(os.path.join(FLUSH_DIR, fname), format='wav') os.remove(self.file_path) # Commit to DB self.db.insertDB("Recording", { "filename": fname, "length": audio.duration_seconds, "description": self.ui.textbox_description.text(), "country": loc[0], "city": loc[1], "place": None, "soundsource": None, "timestamp": datetime_object }) # Clear data self.ui.textbox_description.clear() self.ui.textbox_timestamp.clear() for idx in range(self.ui.list_location.count()): item = self.ui.list_location.item(idx) if item.checkState() == Qt.Checked: item.setCheckState(Qt.Unchecked) print('>>> Done!\n') except ValueError as ve: print('ValueError Raised:', ve) except AttributeError as ae: print('FileNotExistError Raised: This is not an audio file') def updateData(self): print('>>> Update', self.record['filename']) loc = self.ui.combo_location.currentText().split(' / ') self.db.updateDB("Recording", { "id": self.record['id'], "description": self.ui.textbox_description.text(), "country": loc[0], "city": loc[1], "place": None, "soundsource": None }) def loadAudioFile(self): if self.ui.tree_dataset.selectedIndexes(): idx = self.ui.tree_dataset.selectedIndexes()[0] crawler = idx.model().itemData(idx) #crawler = idx.model().filePath(idx) if any(ftype in crawler[0] for ftype in ['.mp3', '.wav', '.WAV', '.m4a']): print('>>> Load', crawler[0]) self.file_path = idx.model().filePath(idx) if self.mode == "init": # Parse origin filename to description fspilt = self.file_path.split('/') self.ui.textbox_description.setText(fspilt[-1].split('.')[0]) # Parse datetime from folder date = fspilt[-2] datetime_object = datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d') self.ui.textbox_timestamp.setText(datetime_object) # Parse location from folder combo_item = fspilt[-4] + ' / ' + fspilt[-3] combo_idx = self.ui.combo_location.findText(combo_item, Qt.MatchFixedString) if combo_idx >= 0: self.ui.combo_location.setCurrentIndex(combo_idx) elif self.mode == "load": self.record = self.db.queryFromTable("Recording", {'filename': crawler[0]}) # Parse from DB # description self.ui.textbox_description.setText(self.record['description']) # timestamp self.ui.textbox_timestamp.setText(str(self.record['timestamp'])) # location combo_item = self.record['country'] + ' / ' + self.record['city'] combo_idx = self.ui.combo_location.findText(combo_item, Qt.MatchFixedString) if combo_idx >= 0: self.ui.combo_location.setCurrentIndex(combo_idx) else: print('Load', crawler[0]) def resetRoot(self, dir_path): #ROOT_DIR = self.ui.textbox_root.text() self.ui.model_dataset.setRootPath(dir_path) self.ui.tree_dataset.setRootIndex(self.ui.model_dataset.index(dir_path))
class Dojo(object): def __init__(self): self.offices = {} self.livingrooms = {} self.room_capacity = {} self.fellows = [] self.staff = [] self.rooms = {} self.all_employees = [] self.unallocated = [] self.accomodated_fellows = [] self.db = Database() def create_room(self, name, room_type): if not isinstance(name, str): return ['Room names can only be strings!', 'red'] if room_type.lower() not in ['office', 'livingroom']: return ['Only offices and livingrooms allowed!', 'red'] if name in self.rooms: return ['Room {} already exists!'.format(name), 'red'] else: class_ = getattr(sys.modules[__name__], room_type.capitalize()) room = class_(name) self.rooms[name] = [] #dict of all rooms. self.room_capacity[name] = room.capacity if room_type.lower() == 'office': self.offices[name] = [] else: self.livingrooms[name] = [] # insert into db self.db.insert_room(name, room_type) return [ '{} {} has been created successfully!'.format( room_type.capitalize(), name), 'green' ] def add_person(self, name, person_type, wants_accomodation='N'): """implementation of add_person command. create a staff or a fellow and assign them a room""" if not isinstance(name, str) or not isinstance(person_type, str): return 'names and person type should be a strings!' if person_type.lower() not in ['fellow', 'staff']: return 'Person type can only be a fellow or a staff!' if name.title() in self.all_employees: return (colored( 'oops!! look like someome with this name (%s) alredy exist!' % (name), "red")) else: name = name.title() # append to list of all employees self.all_employees.append(name) if person_type.lower() == 'fellow': # fellow = Fellow(name) #create an instance of Fellow self.fellows.append(name) #append to list of all fellows if self.offices: # call randomize method self.assign_random_room(name, 6, 'office') else: self.unallocated.append(name) print(colored('No office to assign!', "red")) if wants_accomodation.lower() in ['y', 'yes', 'yeah' ] and self.livingrooms: self.accomodated_fellows.append(name) # call randomize method self.assign_random_room(name, 4, "livingroom") else: if name not in self.unallocated: self.unallocated.append(name) print( colored( 'No Livingroom to assign OR You chose not to be accomodated !', "cyan")) return (colored( 'Fellow %s has been added successfully!' % (name), "green")) elif person_type.lower() == 'staff': if wants_accomodation.lower() in ['y', 'yes', 'yeah']: print( colored('Staff can not be allocated livingrooms!', "red")) #staff = Staff(name) self.staff.append(name) if self.offices: self.assign_random_room( name, 6, 'office') #call assign_random_office else: self.unallocated.append(name) print(colored('No office to assign!', "red")) return (colored( 'Staff %s has been added successfully!' % (name), "green")) def assign_random_room(self, name, occupants, room_type): """Method to randomize office allocation.""" name = name.title() callable_ = self.offices if room_type == 'office' else self.livingrooms list_ = list(callable_.keys()) checked = [ ] #add room here afer each check. to avoid checking same room twice while True: office = random.choice(list(callable_)) if len(callable_[office]) < occupants: callable_[office].append(name) self.rooms[office].append(name) print( colored( '{} has been assigned {} {}!'.format( name, room_type, office), "green")) return False if office not in checked: checked.append(office) if checked == list_: self.unallocated.append(name) print( colored( 'All %ss are full at the moment!' % (room_type), "red")) return False def print_room(self, name): """Prints the names of all the people in room_name on the screen.""" if name not in self.rooms: return colored("Room %s does not exist!" % (name), "red") count = 0 for person in self.rooms[name]: count += 1 print(colored("{}: {}".format(count, person.title()), "cyan")) print("-" * 50) return "{} names printed".format(count) def print_allocations(self): """ Prints a list of allocations onto the screen.""" if not self.rooms: return (colored("No allocations available", "cyan")) for room in self.rooms: print(room.upper()) print("-" * 50) if len(self.rooms[room]) > 0: print(', '.join(self.rooms[room])) else: print("No allocations in this room") print("") return " " def print_unallocated(self): """Prints the names of all unallocated people.""" if len(self.unallocated) <= 0: return colored("No unallocated Employeee at the moment!", "red") number = 0 for un_allocated in self.unallocated: number += 1 print("{}: {}".format(number, un_allocated.title())) print("-" * 50) return ("{} names printed".format(number)) def reallocate_person(self, name, room_name): """reallocate_person""" name = name.title() if name not in self.all_employees: return colored("Person %s does not exist!" % (name), "red") if room_name not in self.rooms: return colored("Room %s does not exist!" % (room_name), "red") if name in self.rooms[room_name]: return colored( "person %s already assigned room %s" % (name, room_name), "red") capacity = self.room_capacity[room_name] if len(self.rooms[room_name]) >= capacity: return colored("Opps! Room %s is fully packed!" % (room_name), "red") print( colored("Removing %s from current room if assigned any" % (name), "cyan")) #remove name from current room [ self.rooms[room].remove(person) for room in self.rooms for person in self.rooms[room] if person == name ] #remove name from unallocated if prevously unllocated if name in self.unallocated: self.unallocated.remove(name) print(colored("assigning %s new room %s" % (name, room_name), "cyan")) self.rooms[room_name].append(name) return (colored( "Employee %s successfully reallocated to room %s!" % (name, room_name), "green")) def load_people(self): """load_people""" file = open("files/load_people.txt", "r") count = 0 for line in file: count += 1 person = line.split() if len(person) == 3: self.add_person(person[0], person[1], person[2]) print( colored( colored( '%s %s has been added successfully!' % (person[1], person[0]), "green"))) else: self.add_person(person[0], person[1]) print( colored( colored( '%s %s has been added successfully!' % (person[1], person[0]), "green"))) file.close return (colored("%s people successfuly loaded from file" % (count), "green", attrs=["bold"]))
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initDB() self.initUI() def keyPressEvent(self, event): if event.key() == Qt.Key_Backspace: self._del_item() def _del_item(self): if self.ui.list_soundsource.selectedItems(): for item in self.ui.list_soundsource.selectedItems(): self.ui.list_soundsource.takeItem(self.ui.list_soundsource.row(item)) def _add_item(self): if self.ui.list_location.count() == 0: item = QListWidgetItem() item.setFlags(item.flags() | Qt.ItemIsEditable) self.ui.list_location.addItem(item) else: count = self.ui.list_location.count() itemlist = [] for idx in range(count-1): if self.ui.list_location.item(idx).text() == "": self.ui.list_location.takeItem(idx) count -= 1 item = self.ui.list_location.item(count-1) if item.text(): item = QListWidgetItem() item.setFlags(item.flags() | Qt.ItemIsEditable) self.ui.list_location.addItem(item) else: pass def initUI(self): self.ui = Ui_MainWindow() self.ui.setupUi(self) self.load() self.ui.btn_load.clicked.connect(self.load) self.ui.btn_save.clicked.connect(self.save) ## Drag and drop #self.ui.list_location.setDragDropMode(QAbstractItemView.DropOnly) self.ui.list_soundsource.setDragDropMode(QAbstractItemView.DropOnly) self.ui.tree_dataset.setDragDropMode(QAbstractItemView.DragOnly) # Add item in listwidget self._add_item() #self.ui.list_location.itemSelectionChanged.connect(self.addItem) self.ui.list_location.itemChanged.connect(self._add_item) def initDB(self): self.db = Database() self.db.createDB() def save(self): list_label = {} list_location = [] list_soundsource = [] for idx in range(self.ui.list_location.count()): item = self.ui.list_location.item(idx) if not item.text() == '': list_location.append(item.text()) for idx in range(self.ui.list_soundsource.count()): item = self.ui.list_soundsource.item(idx) if not item.text() == '': list_soundsource.append(item.text()) list_label['place'] = list_location list_label['soundsource'] = list_soundsource save_into_yaml(PATH_LOC, list_label) def load(self): self.loadData() self.loadLabel() def loadLabel(self): # Refresh the list self.ui.list_location.clear() # Load from yaml loc_dict = load_from_yaml(PATH_LOC) for key, value in loc_dict.items(): if not value == None: if key == 'location': for loc in loc_dict['location']: self.ui.combo_location.addItem(loc['country']+' / '+loc['city']) elif key == 'place': for place in loc_dict['place']: item = QListWidgetItem() item.setFlags(item.flags() | Qt.ItemIsEditable) item.setText(place) self.ui.list_location.addItem(item) elif key == 'soundsource': for sound in loc_dict['soundsource']: item = QListWidgetItem() item.setText(sound) self.ui.list_soundsource.addItem(item) self.ui.list_location.setSortingEnabled(True) self.ui.list_location.sortItems(Qt.DescendingOrder) self._add_item() def loadData(self): # Refresh the model self.ui.model_dataset.removeRows(0, self.ui.model_dataset.rowCount()) # Load from json load_from_json(PATH_LABEL, self.ui.root_node) self.ui.tree_dataset.setModel(self.ui.model_dataset) self.ui.tree_dataset.expandAll() self.ui.tree_dataset.setSortingEnabled(True) self.ui.tree_dataset.sortByColumn(0, Qt.AscendingOrder) def saveData(self): pass