class PictureSize(Base): __tablename__ = "sizes" id = Column(Integer, autoincrement=True, primary_key=True) pic_id = Column(Integer, ForeignKey("pictures.id"), nullable=False) size = Column(VARCHAR(2), nullable=False) link = Column(VARCHAR, nullable=False) add_time = Column(TIMESTAMP, default=timestamp()) # raw = Column(LargeBinary, nullable=True, default=None) def __init__(self, picture_id, size, link, raw=None, **kwargs): super(PictureSize, self).__init__(**kwargs) self.pic_id = picture_id self.size = size self.link = link self.raw = raw self.add_time = timestamp() def __repr__(self): return f"Picture {self.pic_id} size: {self.size}, link - {self.link}" @staticmethod def get_by_link(link: str, local_session=session): return local_session.query(PictureSize).filter( PictureSize.link == link).first() @staticmethod def get_sizes_for_id(id: int, local_session=session) -> list: return list( map( lambda x: x[0], local_session.query( PictureSize.size).filter(PictureSize.pic_id == id).all()))
class Phrase(Base): __tablename__ = 'phrases' id = Column(Integer, primary_key=True, autoincrement=True) text = Column(VARCHAR, nullable=False) add_time = Column(TIMESTAMP, default=timestamp()) user_id = Column(Integer, ForeignKey('users.id'), nullable=False) def __init__(self, text: str, user_id: int, **kwargs): super(Phrase, self).__init__(**kwargs) self.text = text self.user_id = user_id self.add_time = timestamp() def __repr__(self): return f"{self.id}: {self.text}\n" @staticmethod def get_all_list(): return list(map(str, session.query(Phrase).all())) @staticmethod def get_all_str(): phrase_list = Phrase.get_all_list() return func.reduce(lambda a, b: a + b, phrase_list) if len(phrase_list) > 0 else "" @staticmethod def get(phrase_id: int, local_session=session): return local_session.query(Phrase).filter( Phrase.id == phrase_id).first() @staticmethod def get_random(): return random.choice(Phrase.get_all_list())
def __init__(self, picture_id, size, link, raw=None, **kwargs): super(PictureSize, self).__init__(**kwargs) self.pic_id = picture_id self.size = size self.link = link self.raw = raw self.add_time = timestamp()
def __init__(self, id: int, user_id: int, owner_id: int, access_key: str, **kwargs): super(Picture, self).__init__(**kwargs) self.id = id self.ups = self.downs = self.bads = 0 self.user_id = user_id self.owner_id = owner_id self.access_key = access_key self.add_time = timestamp()
def __init__(self, id: int, type: str, owner_id: int, access_key: str, track_code: str, url: str, user_id: int, **kwargs) -> None: super(RawLink, self).__init__(**kwargs) self.id = id self.type = type self.owner_id = owner_id self.access_key = access_key self.track_code = track_code self.url = url self.user_id = user_id self.add_time = timestamp()
class RawLink(Base): __tablename__ = "raw_links" id = Column(BigInteger, primary_key=True) type = Column(VARCHAR(15), nullable=False) owner_id = Column(BigInteger, nullable=False) access_key = Column(VARCHAR) track_code = Column(VARCHAR, nullable=True) url = Column(VARCHAR, nullable=True) user_id = Column(Integer, ForeignKey('users.id'), nullable=False) add_time = Column(TIMESTAMP, default=timestamp()) def __init__(self, id: int, type: str, owner_id: int, access_key: str, track_code: str, url: str, user_id: int, **kwargs) -> None: super(RawLink, self).__init__(**kwargs) self.id = id self.type = type self.owner_id = owner_id self.access_key = access_key self.track_code = track_code self.url = url self.user_id = user_id self.add_time = timestamp() @staticmethod def get(id: int, local_session=session): return local_session.query(RawLink).filter(RawLink.id == id).first() @staticmethod def get_all_with_type(type: str, local_session=session) -> list: return local_session.query(RawLink).filter(RawLink.type == type).all() def get_attachment_string(self) -> str: return f"{self.type}{self.owner_id}_{self.id}{ f'_{self.access_key}' if self.access_key else '' }" @staticmethod def get_random_audio(local_session: Session = session) -> 'RawLink': return local_session \ .query(RawLink) \ .filter(RawLink.type == 'audio') \ .filter(RawLink.url != None) \ .order_by(func.random()) \ .first() def get_api_string(self, peer_id: str) -> str: # TODO: cache file_name = wget.download(self.url) audio_obj: dict = vk_upload.audio_message( file_name, peer_id=peer_id).get('audio_message') os.remove(file_name) access_key = audio_obj.get('access_key') return f"audio_message{audio_obj.get('owner_id')}_{audio_obj.get('id')}" + ( f"_{access_key}" if access_key else "")
class PicMessage(Base): __tablename__ = 'messages' id = Column(Integer, autoincrement=True, primary_key=True) user_id = Column(Integer, ForeignKey('users.id'), nullable=False) pic_id = Column(Integer, ForeignKey('pictures.id'), nullable=False) time = Column(TIMESTAMP, default=timestamp()) text = Column(VARCHAR, default='') def __init__(self, user_id, picture_id, text='', **kwargs): super(PicMessage, self).__init__(**kwargs) self.user_id = user_id self.pic_id = picture_id self.time = timestamp() self.text = text
def __init__(self, id: int, **kwargs): super(User, self).__init__(**kwargs) self.id = id user = User.get(id) if not user: user = api.users.get(user_ids=self.id)[0] self.first_name = user.get("first_name") self.last_name = user.get("last_name") self.ups = self.downs = self.bads = self.all_pics = 0 self.add_time = timestamp() else: self.first_name = user.first_name self.last_name = user.last_name self.ups = user.ups self.downs = user.downs self.bads = user.bads self.all_pics = user.all_pics self.add_time = user.add_time
def __init__(self, user_id, picture_id, text='', **kwargs): super(PicMessage, self).__init__(**kwargs) self.user_id = user_id self.pic_id = picture_id self.time = timestamp() self.text = text
class Picture(Base): __tablename__ = 'pictures' id = Column(Integer, primary_key=True) ups = Column(Integer, default=0) downs = Column(Integer, default=0) bads = Column(Integer, default=0) user_id = Column(Integer, ForeignKey('users.id'), nullable=False) owner_id = Column(Integer, nullable=True) access_key = Column(String, nullable=True) add_time = Column(TIMESTAMP, default=timestamp()) picSize_rel = relationship("PictureSize", backref="picture") msg_rel = relationship("PicMessage", backref="picture") downloaded_pic_rel = relationship("DownloadedPic", backref='picture') def __init__(self, id: int, user_id: int, owner_id: int, access_key: str, **kwargs): super(Picture, self).__init__(**kwargs) self.id = id self.ups = self.downs = self.bads = 0 self.user_id = user_id self.owner_id = owner_id self.access_key = access_key self.add_time = timestamp() @staticmethod def get(id: int, local_session=session): return local_session.query(Picture).filter(Picture.id == id).first() def get_sizes(self, local_session=session) -> List[PictureSize]: return local_session.query(PictureSize).filter(PictureSize.pic_id == self.id).all() def get_best_size(self, local_session=session) -> Optional[PictureSize]: sizes: List[PictureSize] = self.get_sizes(local_session) for size_letter in reversed(size_letters): for size in sizes: if size.size == size_letter: return size return None def __repr__(self): return f"Picture {str(id)}: {str(self.ups)} ups, {str(self.downs)} downs, {str(self.downs)} bads" @staticmethod def get_all_ids(local_session=session) -> list: return list(map(lambda x: x[0], local_session.query(Picture.id).all())) @staticmethod def get_pics_count(local_session=session) -> int: return len(Picture.get_all_ids(local_session)) @staticmethod def get_random_pic(local_session: Session = session): return local_session \ .query(Picture) \ .order_by(func.random()) \ .first() @staticmethod def get_all_from_date_ordered( start_dt: datetime.datetime, local_session: Session = session, limit: int = None ) -> List['Picture']: return local_session\ .query(Picture)\ .filter( Picture.add_time > start_dt )\ .order_by( -(Picture.ups / (Picture.downs + 1)) * (Picture.ups + Picture.downs) )\ .limit(limit)\ .all() @staticmethod def get_best_for_user(user_id, local_session: Session = session, limit: int = None) -> List['Picture']: return local_session \ .query(Picture) \ .filter(Picture.user_id == user_id) \ .order_by( -(Picture.ups / (Picture.downs + 1)) * (Picture.ups + Picture.downs) )\ .limit(limit) \ .all() def get_api_string(self, peer_id: str, local_session=session) -> str: downloaded_pic: Optional[DownloadedPic] = DownloadedPic.get_by_pic_id(self.id, local_session) if downloaded_pic is None: file_name = wget.download(self.get_best_size(local_session).link) photo_obj: dict = vk_upload.photo_messages(file_name, peer_id=peer_id)[0] os.remove(file_name) downloaded_pic = DownloadedPic( id=photo_obj.get('id'), picture_id=self.id, album_id=photo_obj.get('album_id'), owner_id=photo_obj.get('owner_id'), access_key=photo_obj.get('access_key') ) local_session.add(downloaded_pic) local_session.commit() return downloaded_pic.get_api_str()
def __init__(self, text: str, user_id: int, **kwargs): super(Phrase, self).__init__(**kwargs) self.text = text self.user_id = user_id self.add_time = timestamp()
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-c', '--camera', type=str, default=defaults['camera'], help=f'fiber view camera to use, valid options are {fvchandler.cameras.keys()}') parser.add_argument('-n', '--num_dots', type=int, default=1, help=f'number of dots to centroid') parser.add_argument('-r', '--num_repeats', type=int, default=1, help=f'number of times to repeat the measurement') parser.add_argument('-p', '--plot', action='store_true', help='plot measured centroids') parser.add_argument('-e', '--exptime', type=float, default=defaults['exptime'], help='camera exposure time in seconds') parser.add_argument('-b', '--fitbox', type=int, default=defaults['fitbox'], help='window size for centroiding in pixels') parser.add_argument('-d', '--take_darks', action='store_true', help='take dark images (shutter closed). typically not needed, since we keep the test stand in a dark enough enclosure') parser.add_argument('-im', '--save_images', action='store_true', help='save image files to disk') parser.add_argument('-bs', '--save_biases', action='store_true', help='save bias image files to disk') parser.add_argument('-se', '--sim_errmax', type=float, default=defaults['sim_errmax'], help='measurement error max for simulator') parser.add_argument('-sb', '--sim_badmatchfreq', type=float, default=defaults['sim_badmatchfreq'], help='how often the simulator returns [0,0], indicating a bad match') inputs = parser.parse_args() if __name__ == '__main__': start_stamp = gl.timestamp() import simple_logger if not os.path.isdir(gl.dirs['temp']): os.mkdir(gl.dirs['temp']) path_prefix = os.path.join(gl.dirs['temp'], f'fvchandler_{start_stamp}') log_path = f'{path_prefix}.log' logger, _, _ = simple_logger.start_logger(log_path) logger.info(f'Beginning fvchandler stand-alone measurement run') logger.info(f'Inputs: {inputs}') params = defaults.copy() for key in ['camera', 'exptime', 'fitbox', 'sim_errmax', 'sim_badmatchfreq']: params[key] = getattr(inputs, key) logger.info(f'Initializing fvchandler with parameters: {params}') f = fvchandler.FVCHandler(params=params, take_darks=inputs.take_darks, save_images=inputs.save_images,
class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) first_name = Column(VARCHAR, nullable=False) last_name = Column(VARCHAR, nullable=False) ups = Column(Integer, default=0) downs = Column(Integer, default=0) bads = Column(Integer, default=0) all_pics = Column(Integer, default=0) add_time = Column(TIMESTAMP, default=timestamp()) pic_rel = relationship("Picture", backref="user") phrase_rel = relationship("Phrase", backref="user") msg_rel = relationship("PicMessage", backref="user") raw_link_rel = relationship("RawLink", backref="user") downloaded_pic_rel = relationship("DownloadedPic", backref='user') def __init__(self, id: int, **kwargs): super(User, self).__init__(**kwargs) self.id = id user = User.get(id) if not user: user = api.users.get(user_ids=self.id)[0] self.first_name = user.get("first_name") self.last_name = user.get("last_name") self.ups = self.downs = self.bads = self.all_pics = 0 self.add_time = timestamp() else: self.first_name = user.first_name self.last_name = user.last_name self.ups = user.ups self.downs = user.downs self.bads = user.bads self.all_pics = user.all_pics self.add_time = user.add_time @staticmethod def get(id: int, local_session=session) -> Optional['User']: if id < 0: return None return local_session.query(User).filter(User.id == id).first() def get_pics(self, local_session=session): return local_session.query(Picture).filter(Picture.id == self.id).all() def __eq__(self, other) -> bool: return self.id == other.id def __repr__(self) -> str: return f"User { str(self.id) }: {self.first_name} {self.last_name} - {str(self.ups)} ups, " \ f"{str(self.downs)} downs, {str(self.bads)} bads, {str(self.all_pics)} all" def show_stat(self) -> str: return f"{self.first_name}: {self.ups}↑ {self.downs}↓ {self.bads}💩 всего: {self.all_pics}" def get_formatted_name(self) -> str: return f"{self.first_name} {self.last_name}" # Returns latest pic sent by this user def __get_latest_pic(self): pass