def all_zones(time): fmt = '%H:%M %Z%z' e = timezone('US/Eastern') c = timezone('US/Central') m = timezone('US/Mountain') p = timezone('US/Pacific') return (time.astimezone(e).strftime(fmt), time.astimezone(c).strftime(fmt), time.astimezone(m).strftime(fmt), time.astimezone(p).strftime(fmt))
def _to_timestamp(time_string): import calendar utc = pytz.timezone("UTC") time = datetime.strptime(time_string, "%d.%m.%Y %H:%M") time = pytz.timezone(app.config['APP_TZ']).localize(time) time = utc.normalize(time.astimezone(utc)) return calendar.timegm(time.utctimetuple())
async def tz_convert(self,ctx,hour:int,minutes:int,timezonee:str): """Converts the given HOUR and MINUTE from TIMEZONEE to the timezones added to the server using tz add Compatable timezones: https://jdavisbro.github.io/luigi/timezones.txt""" if timezonee not in list(pytz.all_timezones): await ctx.send("That is not a timezone") return time = datetime.datetime.now() timezonee = timezone(timezonee) time = time.replace(tzinfo=timezonee,hour=hour,minute=minutes,second=0,microsecond=0) f = open("timezones.json","r") timezones = json.loads(f.read()) try: timezones[str(ctx.guild.id)] except KeyError: timezones[str(ctx.guild.id)] = [] if not timezones[str(ctx.guild.id)]: await ctx.send("There are no timezones in this server.") return embed = discord.Embed(title="Heres that time in the timezones added to the server.", colour=discord.Colour.from_rgb(random.randint(1,250),random.randint(1,250),random.randint(1,250))) for timezoneee in timezones[str(ctx.guild.id)]: timeinthistimezone = time.astimezone(timezone(timezoneee)) if int(timeinthistimezone.strftime("%H")) > 12: timeinthistimezone = f"{timeinthistimezone.strftime('%H:%M')} ({int(timeinthistimezone.strftime('%H'))-12}:{timeinthistimezone.strftime('%M')}pm)" else: timeinthistimezone = f"{timeinthistimezone.strftime('%H:%M')} ({timeinthistimezone.strftime('%H:%M')}am)" embed.add_field(name=timezoneee, value=timeinthistimezone, inline=True) await ctx.send(embed=embed)
def main(): mytimezone = stations.city['ottawa'].timezone db = Db() for row in db.execute('SELECT date from observations'): existingResults.add(row[0]) oldestExistingResult = min(existingResults) #time, observation = getOne(None, db) #print(time, observation) time = dt.datetime.fromtimestamp(oldestExistingResult, dt.timezone.utc) while time != None: prevTime = time time, observation = getOne(time.astimezone(mytimezone), db) print(time, observation) if time is None or time == prevTime: time = prevTime - dt.timedelta(minutes=1) exit(0) while threading.active_count() > 1: try: tsv, d = results.get(timeout=1) except queue.Empty: print(threading.enumerate()) continue sqlCmd = 'REPLACE INTO observations VALUES (?,?)' try: c.execute(sqlCmd, (tsv, d)) conn.commit() except sqlite3.OperationalError: print(sqlCmd, (tsv, d)) raise conn.close()
def monitor_order(self): logging.info("Start Monitoring") monitoring_success_notif = False exited = False # print(self.candles) for candle in self.candles: low = candle.low high = candle.high time = candle.time tz = pytz.timezone('Asia/Kolkata') time = time.astimezone(tz) # print(time) if self.txn_type == "sell": if low <= self.target: self.exit_order_no = 100000000002 self.exit_time = time self.exit_price = low self.succeed_exit() send_push("Successful exit: " + self.stockname, "exit with profit") break else: if high >= self.target: self.exit_order_no = 100000000002 self.exit_time = time self.exit_price = high self.succeed_exit() send_push("Successful exit: " + self.stockname, "exit with profit") break if self.txn_type == "sell": if high >= self.stoploss: self.exit_order_no = 100000000002 self.exit_time = time self.exit_price = high self.succeed_exit() send_push("Lossy exit: " + self.stockname, "exit with loss") break else: if low <= self.stoploss: self.exit_order_no = 100000000002 self.exit_time = time self.exit_price = low self.succeed_exit() send_push("Lossy exit: " + self.stockname, "exit with loss") break
def get_time(self, time): time = datetime.strptime(time, '%Y-%m-%d') tz_current = pytz.timezone(self._context.get('tz') or 'UTC') # get timezone user tz_database = pytz.timezone('UTC') time = tz_database.localize(time) time = time.astimezone(tz_current).strftime(_('%m-%d-%Y')) return time
def get_time(self): time = self.event["start_time"] from_zone = tz.gettz("UTC") time = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%SZ") time = time.replace(tzinfo=from_zone) to_zone = tz.gettz(self.event["timezone"]) return time.astimezone(to_zone)
def getTime(bot, update): bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) from_zone = tz.gettz('UTC') to_zone = tz.gettz('Singapore') time = datetime.datetime.utcnow() time = time.replace(tzinfo=from_zone) time = time.astimezone(to_zone) update.message.reply_text(time.strftime("%c"))
def to_utc(time: datetime) -> datetime: """Convert a datetime-aware time zone into raw UTC""" tzone = datetime.now().astimezone().tzinfo if time.tzinfo is not None: # Convert to UTC time = time.astimezone(tz=timezone.utc) else: # Assume local tz, convert to URC time = time.replace(tzinfo=tzone).astimezone(tz=timezone.utc) # Return an offset-naive datetime return time.replace(tzinfo=None)
def get_today(self): time = datetime.datetime.now() tz_current = pytz.timezone(self._context.get('tz') or 'UTC') # get timezone user tz_database = pytz.timezone('UTC') time = tz_database.localize(time) time = time.astimezone(tz_current) today_w_timezone = time.strftime('%Y-%m-%d') return today_w_timezone
def __init__( self, time=None, priority=None, source=None, rule=None, output=None, output_fields=None, hostname=None, ): self.time: datetime = time.astimezone(tz.tzutc()) self.priority: OutputsResponse.Priority = priority self.source: OutputsResponse.Source = source self.rule: str = rule self.output: str = output self.output_fields: Dict = output_fields self.hostname: str = hostname
def api_show_complains_by_status(request): convert_session_id_to_user(request) community_id = request.GET.get("community_id", None) complains_status = request.GET.get("status", None) profile = ProfileDetail.objects.get(profile=request.user) if community_id: community = Community.objects.get(id=community_id) else: response_data = {'info': '没有传入小区id', 'success': False} return HttpResponse(simplejson.dumps(response_data), content_type='application/json') if request.user.is_staff: complains = Complaints.objects.filter(community=community, status=int(str(complains_status))).order_by('-timestamp') elif profile.is_admin: complains = Complaints.objects.filter(community=community, status=int(str(complains_status)), handler=request.user).order_by('-timestamp') else: complains = Complaints.objects.filter(community=community, status=int(str(complains_status)), author=request.user.username).order_by('-timestamp') if len(complains) > 0: paginator = Paginator(complains, 20) page_count = paginator.num_pages page = request.GET.get('page') try: complains_list = paginator.page(page).object_list except PageNotAnInteger: complains_list = paginator.page(1) except EmptyPage: complains_list = paginator.page(paginator.num_pages) complain_list = list() local_time = None for complain_detail in complains_list: time = complain_detail.timestamp local = time.astimezone(UTC(8)) complete_time = complain_detail.complete_time if complete_time: local_time = complete_time.astimezone(UTC(8)) data = { 'id': complain_detail.id, 'complain_author': complain_detail.author, 'author_community': complain_detail.community.title, 'author_floor': complain_detail.author_detail.floor, 'author_room': complain_detail.author_detail.gate_card, 'content': complain_detail.content, 'complete_time':str(local_time).split('.')[0], 'type': complain_detail.type, 'deal_status': complain_detail.status, 'pleased': complain_detail.pleased, 'src': complain_detail.src.name, 'time': str(local).split('.')[0], 'handler': str(complain_detail.handler) } complain_list.append(data) response_data = {'complains_list': complain_list, 'page_count': page_count, 'success': True} return HttpResponse(simplejson.dumps(response_data), content_type='application/json') else: response_data = {'success': False, 'info': '没有搜到要找的结果'} return HttpResponse(simplejson.dumps(response_data), content_type='application/json')
def status_report(controller): tz = pytz.timezone('Europe/Amsterdam') (ra, dec) = controller.getPosition(coordinateMode=NexstarCoordinateMode.RA_DEC) print("position RA/DEC ................... : ra = {}, dec = {}".format( ra, dec)) (azimuth, altitude) = controller.getPosition( coordinateMode=NexstarCoordinateMode.AZM_ALT) print("position AZM/ALT .................. : azimuth = {}, altitude = {}". format(azimuth, altitude)) tracking_mode = controller.getTrackingMode() print("tracking mode ..................... : {}".format(tracking_mode)) (latitude, longitude) = controller.getLocation() print( "location .......................... : latitude = {}, longitude = {}". format(latitude, longitude)) (time, dst) = controller.getTime() time = time.astimezone(tz) print("time .............................. : time = {}, dst = {}".format( time.strftime("%Y-%m-%d %H:%M:%S %Z"), dst)) (versionMajor, versionMinor) = controller.getVersion() print("version ........................... : {}.{}".format( versionMajor, versionMinor)) model = controller.getModel() print("model ............................. : {}".format(model)) alignmentComplete = controller.getAlignmentComplete() print("alignment complete ................ : {}".format(alignmentComplete)) gotoInProgress = controller.getGotoInProgress() print("goto in progress .................. : {}".format(gotoInProgress)) if True: for deviceId in NexstarDeviceId: try: (versionMajor, versionMinor) = controller.getDeviceVersion(deviceId) status = "version = {}.{}".format(versionMajor, versionMinor) except NexstarPassthroughError as exception: status = "error: {}".format(exception) print("device {} {} : {}".format( deviceId.name, "." * (27 - len(str(deviceId.name))), status)) model = controller.getModel() print("model ............................. : {}".format(model))
def get_title_report(self): data = {} time = datetime.now() tz_current = pytz.timezone(self._context.get('tz') or 'UTC') # get timezone user tz_database = pytz.timezone('UTC') time = tz_database.localize(time) time = time.astimezone(tz_current).strftime(_('%m-%d-%Y')) time = _('Report Date: ') + time title_report = _('Import Product Report') data.update({'time_report': time, 'title_report': title_report}) return data
def _tr_event_id(self, values, obj): """Translates euxfel event ID from some source into a hummingbird one""" pulseid = int(obj["image.pulseId"]) timestamp = int(obj['timestamp.sec']) + int( obj['timestamp.frac']) * 1e-18 + pulseid * 1e-2 time = datetime.datetime.fromtimestamp(timestamp, tz=timezone('utc')) time = time.astimezone(tz=timezone('CET')) rec = Record('Timestamp', time, ureg.s) rec.pulseId = int(obj['image.pulseId']) rec.cellId = int(obj['image.cellId']) rec.trainId = int(obj['image.trainId']) rec.timestamp = timestamp values[rec.name] = rec
def format_time(db=None, config=None, zone=None, nick=None, channel=None, time=None): """Return a formatted string of the given time in the given zone. `time`, if given, should be a naive `datetime.datetime` object and will be treated as being in the UTC timezone. If it is not given, the current time will be used. If `zone` is given and `pytz` is available, `zone` must be present in the IANA Time Zone Database; `get_timezone` can be helpful for this. If `zone` is not given or `pytz` is not available, UTC will be assumed. The format for the string is chosen in the following order: 1. The format for `nick` in `db`, if one is set and valid. 2. The format for `channel` in `db`, if one is set and valid. 3. The default format in `config`, if one is set and valid. 4. ISO-8601 If `db` is not given or is not set up, steps 1 and 2 are skipped. If config is not given, step 3 will be skipped.""" tformat = None if db: if nick and nick in db.preferences: tformat = db.preferences.get(nick, 'time_format') if not tformat and channel in db.preferences: tformat = db.preferences.get(channel, 'time_format') if not tformat and config and config.has_option('core', 'default_time_format'): tformat = config.core.default_time_format if not tformat: tformat = '%F - %T%Z' if not time: time = datetime.datetime.utcnow() if not pytz or not zone: return time.strftime(tformat) else: if not time.tzinfo: utc = pytz.timezone('UTC') time = utc.localize(time) zone = pytz.timezone(zone) return time.astimezone(zone).strftime(tformat)
def draw_header(header2, month, categories, branches): font = ImageFont.truetype("app/static/fonts/Ubuntu-M.ttf", 60) draw = ImageDraw.Draw(header2) # Group (Categories) draw.text((1175, 290), ", ".join([cat.name for cat in categories]), font=font) # Location (Branches) draw.text((4300, 290), ", ".join([b.name for b in branches]), font=font) # Time time = datetime.now(pytz.utc) time = time.astimezone(pytz.timezone("Asia/Kolkata")) draw.text((4300, 110), time.strftime("%I:%M:%S %p"), font=font) # Month at 6020,110 draw.text((6020, 110), month.strftime("%B"), font=font) # YEAR at 6020,290 draw.text((6020, 290), month.strftime("%Y"), font=font)
def suggestion_time_category_at(self, time): for suggestion_time in self.__configuration.suggestion_times: suggestion_time_today = suggestion_time.get_datetime_on(time) suggestion_time_today_utc = suggestion_time_today.astimezone( pytz.UTC) time_utc = time.astimezone(pytz.UTC) difference = None if time_utc < suggestion_time_today_utc: difference = suggestion_time_today_utc - time_utc else: difference = time_utc - suggestion_time_today_utc decision_window_seconds = self.__get_decision_window_minutes() * 60 if difference.seconds is not None and difference.seconds < decision_window_seconds: return suggestion_time.category return None
def status_report(controller): tz = pytz.timezone('Europe/Amsterdam') (ra, dec) = controller.getPosition(coordinateMode = NexstarCoordinateMode.RA_DEC) print("position RA/DEC ................... : ra = {}, dec = {}".format(ra, dec)) (azimuth, altitude) = controller.getPosition(coordinateMode = NexstarCoordinateMode.AZM_ALT) print("position AZM/ALT .................. : azimuth = {}, altitude = {}".format(azimuth, altitude)) tracking_mode = controller.getTrackingMode() print("tracking mode ..................... : {}".format(tracking_mode)) (latitude, longitude) = controller.getLocation() print("location .......................... : latitude = {}, longitude = {}".format(latitude, longitude)) (time, dst) = controller.getTime() time = time.astimezone(tz) print("time .............................. : time = {}, dst = {}".format(time.strftime("%Y-%m-%d %H:%M:%S %Z"), dst)) (versionMajor, versionMinor) = controller.getVersion() print("version ........................... : {}.{}".format(versionMajor, versionMinor)) model = controller.getModel() print("model ............................. : {}".format(model)) alignmentComplete = controller.getAlignmentComplete() print("alignment complete ................ : {}".format(alignmentComplete)) gotoInProgress = controller.getGotoInProgress() print("goto in progress .................. : {}".format(gotoInProgress)) if True: for deviceId in NexstarDeviceId: try: (versionMajor, versionMinor) = controller.getDeviceVersion(deviceId) status = "version = {}.{}".format(versionMajor, versionMinor) except NexstarPassthroughError as exception: status = "error: {}".format(exception) print("device {} {} : {}".format(deviceId.name, "." * (27 - len(str(deviceId.name))), status)) model = controller.getModel() print("model ............................. : {}".format(model))
def format_time(db=None, config=None, zone=None, nick=None, channel=None, time=None): """Return a formatted string of the given time in the given zone. `time`, if given, should be a naive `datetime.datetime` object and will be treated as being in the UTC timezone. If it is not given, the current time will be used. If `zone` is given and `pytz` is available, `zone` must be present in the IANA Time Zone Database; `get_timezone` can be helpful for this. If `zone` is not given or `pytz` is not available, UTC will be assumed. The format for the string is chosen in the following order: 1. The format for the nick `nick` in `db`, if one is set and valid. 2. The format for the channel `channel` in `db`, if one is set and valid. 3. The default format in `config`, if one is set and valid. 4. ISO-8601 If `db` is not given or is not set up, steps 1 and 2 are skipped. If config is not given, step 3 will be skipped.""" tformat = None if db: if nick: tformat = db.get_nick_value(nick, 'time_format') if not tformat and channel: tformat = db.get_channel_value(channel, 'time_format') if not tformat and config and config.has_option('core', 'default_time_format'): tformat = config.core.default_time_format if not tformat: tformat = '%F - %T%Z' if not time: time = datetime.datetime.utcnow() if not pytz or not zone: return time.strftime(tformat) else: if not time.tzinfo: utc = pytz.timezone('UTC') time = utc.localize(time) zone = pytz.timezone(zone) return time.astimezone(zone).strftime(tformat)
def MontarEnviar(competition, zone, dateList): currentDate = date.today().strftime('%Y %m %d') currentDate = currentDate.split() currentDate = [int(x) for x in currentDate] brazilTimeZone = timezone('Brazil/East') time = zone.localize(datetime(dateList[0], dateList[1], dateList[2], dateList[3], dateList[4])) printFormat = '%d/%m/%Y %H:%M' brazilTimeStr = time.astimezone(brazilTimeZone).strftime(printFormat) delta = date(dateList[0], dateList[1],dateList[2]) - date(currentDate[0], currentDate[1],currentDate[2]) if(delta.days == 0): enviarEmail('', competition + ' Match Hoje! ' + brazilTimeStr) elif(delta.days == 1): enviarEmail('', competition + ' Match Amanha! ' + brazilTimeStr) else: print('Ainda faltam ' + str(delta.days) + ' dias para o ' + competition)
def organize_message(self, message): message = json.loads(message) if 'side' in message.keys(): currency_id = self.get_currency_id() # time is a str with ISO 8601 format: 2018-11-19T20:29:20.550000Z time = message['time'] ####### Handle time format # Default is EUROPE time, convert to current time zone >>>>>>>>> time = dateutil.parser.parse(time) localtime = time.astimezone(pytz.timezone("US/Eastern")) localiso = localtime.isoformat() time = dateutil.parser.parse(localiso).strftime( '%Y-%m-%d %H:%M:%S') # open = float(message['open_24h']) price = float(message['price']) # best_bid = float(message['best_bid']) # best_ask = float(message['best_ask']) return (currency_id, time, price)
def set_time(self, time, do_clone=False): """ Устанавливает в текущем объекте (или создает новый) время в значение из time::(time, datetime) !! Если TZ нового времени отличается от self, то возможна некорректная работа. Если time не содержит tz - приводится к текущему """ if time.tzinfo is None: time = time.replace(tzinfo=self.dt.tzinfo) else: time = time.astimezone(self.dt.tzinfo) ndt = self.dt.replace(hour=time.hour, minute=time.minute, second=time.second) if do_clone: return type(self)(ndt) else: self.dt = ndt return self
def getTimeDifference(found): global timeFormatted timezone = found.split(" ")[-1] print(timezone) try: zone = pytz.timezone(timezones[timezone.upper()]) except KeyError: return try: time = parser.parse(found.upper()) except ValueError: return time = time.replace(tzinfo=None) time = zone.localize(time) utc = pytz.timezone("UTC") time = time.astimezone(utc) now = datetime.datetime.now() now = suomi.localize(now) now = now.astimezone(utc) diff = (time - now).seconds / 3600 print(time) print("HOURS: ", diff) print(now) timeFormatted = time if (time - now).days > 0: time = time - datetime.timedelta(days=1) timeFormatted = time return diff else: return diff
def fetch_activities(date): global points try: response = requests.get( 'https://api.fitbit.com/1/user/-/activities/list.json', headers={ 'Authorization': 'Bearer ' + FITBIT_ACCESS_TOKEN, 'Accept-Language': FITBIT_LANGUAGE }, params={ 'beforeDate': date, 'sort': 'desc', 'limit': 5, 'offset': 0 }) response.raise_for_status() except requests.exceptions.HTTPError as err: print("HTTP request failed: %s" % (err)) sys.exit() data = response.json() print("Got activities from Fitbit for before_date: " + date) for activity in data['activities']: fields = {} if 'activeDuration' in activity: fields['activeDuration'] = int(activity['activeDuration']) if 'averageHeartRate' in activity: fields['averageHeartRate'] = int(activity['averageHeartRate']) if 'calories' in activity: fields['calories'] = int(activity['calories']) if 'duration' in activity: fields['duration'] = int(activity['duration']) if 'distance' in activity: fields['distance'] = float(activity['distance']) fields['distanceUnit'] = activity['distanceUnit'] if 'pace' in activity: fields['pace'] = float(activity['pace']) if 'speed' in activity: fields['speed'] = float(activity['speed']) if 'elevationGain' in activity: fields['elevationGain'] = int(activity['elevationGain']) if 'steps' in activity: fields['steps'] = int(activity['steps']) if 'tcxLink' in activity: gps_track_list = get_gps(activity['tcxLink'], activity['activityName'], activity['logId']) if len(gps_track_list) != 0: print("Got GPS from Fitbit for log id: " + str(activity['logId'])) points += gps_track_list for level in activity['activityLevel']: if level['name'] == 'sedentary': fields[level['name'] + "Minutes"] = int(level['minutes']) else: fields[level['name'] + "ActiveMinutes"] = int(level['minutes']) time = datetime.fromisoformat(activity['startTime'].strip("Z")) utc_time = time.astimezone(pytz.utc).isoformat() points.append({ "measurement": "activity", "time": utc_time, "tags": { "activityName": activity['activityName'] }, "fields": fields })
def time_to_str(t): time = t.replace(tzinfo=dateutil.tz.tzutc()) local_time = time.astimezone(dateutil.tz.tzlocal()) return local_time.strftime('%d.%m.%Y %H:%M')
sys.exit(1) #Run mythfilldatabase: if showdetails: subprocess.call('mythfilldatabase --refresh 1 --file --sourceid ' + mythsourceid + ' --xmlfile ./xmltv.xml', shell=True) else: print('Running mythfilldatabase') subprocess.call('mythfilldatabase --quiet --refresh 1 --file --sourceid ' + mythsourceid + ' --xmlfile ./xmltv.xml', shell=True) pym = mythRecord(mythlanip, mythport) chaninfo = pym.GetChannelInfoList(SourceID=mythsourceid, Details='true') if chaninfo: chaninfo = chaninfo['ChannelInfoList']['ChannelInfos'] else: print('Error in mythrecmaze.py (unable to fetch channel information)') sys.exit(1) for i in range(len(chaninfo)): for j in range(len(newepisodes)): if len(newepisodes[j]) > 3: if newepisodes[j][3] == chaninfo[i]['XMLTVID']: time = newepisodes[j][4] time = datetime.strptime (time[0:22]+time[23:25], "%Y-%m-%dT%H:%M:%S%z") time = datetime.strftime(time.astimezone(timezone.utc), "%Y-%m-%dT%H:%M:%S") mythchid = chaninfo[i]['ChanId'] recRule = pym.GetRecordSchedule(ChanId=mythchid, StartTime=time) if recRule: recRule = recRule['RecRule'] recRule['Type'] = 'Single Record' recRule['Station'] = recRule['CallSign'] pym.AddRecordSchedule(recRule) else: print('Error: No record schedule found')
def convert_tz(time, timezone): return timezone.normalize(time.astimezone(timezone))
def main(): homepath = os.path.expanduser('~') if os.path.isfile(homepath + '/.mythrecmaze/mythrecmaze0.log'): os.rename(homepath + '/.mythrecmaze/mythrecmaze0.log', homepath + '/.mythrecmaze/mythrecmaze1.log') if not os.path.isdir(homepath + '/.mythrecmaze'): os.mkdir(homepath + '/.mythrecmaze') try: logging.basicConfig(format='%(levelname)s:%(message)s', filename=homepath + '/.mythrecmaze/mythrecmaze0.log', filemode='w', level=logging.INFO) except IOError: logging.basicConfig(format='%(levelname)s:%(message)s', filename='/tmp/mythrecmaze0.log', filemode='w', level=logging.INFO) logging.info(" " + str(datetime.now()) + " Starting mythrecmaze.py") if not os.path.isfile("/opt/mythrecmaze/userhomepath.dat"): logging.info(" Aborting (Required file userhomepath.dat not found)") sys.exit(0) config = configparser.RawConfigParser() config.read('/opt/mythrecmaze/userhomepath.dat') uhomepath = config.get('userhomepath', 'uhp') mythlanip = False #Display GUI if ran manually by user: if not 'mythtv' in homepath: while True: try: option = subprocess.check_output( "zenity --list --title='Mythrecmaze' --text='Select Option' --column='0' \ 'Change settings' \ 'Check for shows to record now and exit' \ 'View log' \ 'Exit Mythrecmaze' --hide-header \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) option = option.strip().decode('utf-8') except subprocess.CalledProcessError: logging.info(' Exiting (Options dialog canceled)') sys.exit(0) if option == 'Exit Mythrecmaze': logging.info(' Exiting (User selected exit)') sys.exit(0) if option == 'Change settings': try: cfg = subprocess.check_output( "zenity --forms --title='Mythrecmaze' --text='Configuration' \ --add-entry='MythTV backend server IP address (example: 192.168.1.50)' \ --add-entry='MythTV backend web server port (default: 6544)' \ --add-entry='MythTV channel source id (default: 1)' \ --add-entry='TVmaze API key (enter single API key)' \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) cfg = cfg.strip().decode('utf-8').split("|") except subprocess.CalledProcessError: continue mythlanip = cfg[0] if isbadipv4(mythlanip): logging.info( ' Aborting (invalid MythTV backend server IP address)') sys.exit(0) mythport = cfg[1] if len(mythport) == 0: mythport = '6544' elif not mythport.isdigit(): logging.info( ' Aborting (invalid MythTV backend web server port number)' ) sys.exit(0) mythsourceid = cfg[2] if len(mythsourceid) == 0: mythsourceid = '1' elif not mythsourceid.isdigit(): logging.info( ' Aborting (invalid MythTV channel source id number)') sys.exit(0) mazetokens = [cfg[3]] if len(mazetokens) == 0: logging.info(' Aborting (TVmaze API key is required)') sys.exit(0) while True: try: mazetoken = subprocess.check_output( "zenity --forms --title='Mythrecmaze' \ --text='Enter additional TVmaze API key or leave blank if none' \ --add-entry='TVmaze API key (enter single API key)' \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) mazetoken = mazetoken.strip().decode('utf-8') except subprocess.CalledProcessError: break if len(mazetoken) == 0: break else: mazetokens.append(mazetoken) config.add_section('mythrecmazesettings') config.set('mythrecmazesettings', 'mythlanip', mythlanip) config.set('mythrecmazesettings', 'mythport', mythport) config.set('mythrecmazesettings', 'mythsourceid', mythsourceid) config.set('mythrecmazesettings', 'mazetokens', ','.join(mazetokens)) config.set('mythrecmazesettings', 'showdetails', 'False') showdetails = False with open(homepath + '/.mythrecmaze/mythrecmaze.cfg', 'w') as configfile: config.write(configfile) if option == 'View log': viewedlog = False if os.path.isfile(homepath + '/.mythrecmaze/mythrecmaze1.log'): subprocess.call( "zenity --text-info --title='Mythrecmaze Previous Manual Run Log' --width=600 \ --height=500 --filename=" + homepath + "/.mythrecmaze/mythrecmaze1.log \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) viewedlog = True if os.path.isfile( '/home/mythtv/.mythrecmaze/mythrecmaze0.log'): subprocess.call( "zenity --text-info --title='Mythrecmaze Previous Automatic Run Log' --width=600 \ --height=500 --filename=/home/mythtv/.mythrecmaze/mythrecmaze0.log \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) viewedlog = True if not viewedlog: subprocess.call( "zenity --info --title='Mythrecmaze' --text='No log file found' --width=150 \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) if option == 'Check for shows to record now and exit': break if os.path.isfile(uhomepath + '/.mythrecmaze/mythrecmaze.cfg') and not mythlanip: config.read(uhomepath + '/.mythrecmaze/mythrecmaze.cfg') mythlanip = config.get('mythrecmazesettings', 'mythlanip') mythport = config.get('mythrecmazesettings', 'mythport') mythsourceid = config.get('mythrecmazesettings', 'mythsourceid') mazetokens = config.get('mythrecmazesettings', 'mazetokens').split(',') showdetails = config.getboolean('mythrecmazesettings', 'showdetails') if not mythlanip: logging.info(" Aborting (Required file mythrecmaze.cfg not found)") if not 'mythtv' in homepath: subprocess.call( "zenity --info --title='Mythrecmaze' --text='Aborting (Unable to retreive settings)' --width=300 \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) sys.exit(0) logging.info(' Opening TVmaze connection') episodes = getICalsEpisodes(mazetokens) newepisodes = episodes #Skip episodes prevoiusly processed (do all if ran manually): if len(episodes) > 0 and 'mythtv' in homepath: if os.path.isfile(homepath + '/.mythrecmaze/mythrecmaze.pickle'): with open(homepath + '/.mythrecmaze/mythrecmaze.pickle', 'rb') as f: prevepisodes = pickle.load(f) #Episodes not in previous episodes list: newepisodes = list( itertools.compress(newepisodes, (not x in prevepisodes for x in newepisodes))) with open(homepath + '/.mythrecmaze/mythrecmaze.pickle', 'wb') as f: pickle.dump(episodes, f, pickle.HIGHEST_PROTOCOL) else: if len(episodes) == 0: newepisodes = [] logging.info(' Downloading TVmaze schedule') noschedule = True usingNonMazeChIds = False if os.path.isfile(uhomepath + '/xmltvidmap.csv'): usingNonMazeChIds = True with open(uhomepath + '/xmltvidmap.csv', 'r') as f: reader = csv.reader(f) channelsMap = list(reader) channelsMazeInclude = [] for i in range(len(channelsMap)): channelsMazeInclude = channelsMazeInclude + [channelsMap[i][1]] #Write out schedule for each unique day in newepisodes and add network id and airstamp to new episodes list: if 'mythtv' in homepath: tmp_xml_file = '/tmp/xmltvmrm_m.xml' else: tmp_xml_file = '/tmp/xmltvmrm.xml' with open(tmp_xml_file, 'w') as xml_file: xml_file.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + '\n') xml_file.write('<!DOCTYPE tv SYSTEM "xmltv.dtd">' + '\n') xml_file.write('\n') xml_file.write( '<tv source-info-name="TVmaze" generator-info-name="mythrecmaze.py">' + '\n') schedule_dicts = getSchedule( 'https://api.tvmaze.com/schedule') #Always get today's schedule daysdone = [] channelsMazeSkipped = [] torecord = 'To be recorded: \n' for i in range(len(newepisodes) + 1): overlapcheck = [] if i > 0: day = newepisodes[i - 1][0] if not day in daysdone: scheduleday = day schedule_dicts = getSchedule( 'https://api.tvmaze.com/schedule?date=' + day[0:4] + '-' + day[4:6] + '-' + day[6:8]) else: day = date.today().strftime("%Y%m%d") scheduleday = day #New episodes to record: for j in range(len(schedule_dicts)): skip = True episodeid = schedule_dicts[j]['id'] for k in range( len(episodes) ): #episodes instead of newepisodes to avoid overlaps in guide data if str(episodeid) == episodes[k][2]: skip = False break if skip: continue name = schedule_dicts[j]['show']['name'] try: ch_id = str(schedule_dicts[j]['show']['network']['id']) tm = schedule_dicts[j]['airstamp'] start = tm[0:4] + tm[5:7] + tm[8:10] + tm[11:13] + tm[ 14:16] + tm[17:19] + ' ' + tm[19:22] + tm[23:25] start_time = datetime.strptime(start[0:14], "%Y%m%d%H%M%S") except: logging.info(' Incomplete schedule information for ' + name) logging.info(' Skipping EPG entry for ' + name) skip = True if i > 0: if str(episodeid) == newepisodes[i - 1][2]: logging.info( ' Error in mythrecmaze.py (Incomplete schedule information for show to be recorded)' ) sys.exit(1) if usingNonMazeChIds and not skip: if ch_id in channelsMazeInclude: ch_id = channelsMap[channelsMazeInclude.index( ch_id)][0] else: if not ch_id in channelsMazeSkipped: if showdetails: logging.info( ' Skipping TVmaze network id ' + ch_id + ' (not included in xmltvidmap.csv)') channelsMazeSkipped = channelsMazeSkipped + [ch_id] continue if i > 0 and not skip: if str(episodeid) == newepisodes[i - 1][2]: if day != scheduleday: logging.info( ' Error in mythrecmaze.py (unexpected schedule sort order)' ) sys.exit(1) torecord = torecord + name + ' on ' + newepisodes[ i - 1][0][4:6] + '/' + newepisodes[i - 1][0][ 6:9] + '/' + newepisodes[i - 1][0][0:4] + '\n' newepisodes[i - 1] = newepisodes[i - 1] + [ch_id] + [tm] if not day in daysdone and not skip: runtime = schedule_dicts[j]['runtime'] try: description = re.sub('<[^<]+?>', '', schedule_dicts[j]['summary']) except: description = '' try: stop_time = start_time + timedelta(minutes=runtime) except TypeError: logging.info(' Unable to determine runtime for: ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) logging.info(' Guessing 60 minutes runtime for ' + name) stop_time = start_time + timedelta(minutes=60) stop = stop_time.strftime( "%Y%m%d%H%M%S") + ' ' + tm[19:22] + tm[23:25] for k in range(len(overlapcheck)): if start_time < overlapcheck[k][ 2] and stop_time > overlapcheck[k][ 1] and ch_id == overlapcheck[k][0]: if i > 0: if str(episodeid) == newepisodes[i - 1][2]: logging.info( 'Warning: Uncorrected time overlap detected for ' + name + ' at ' + start_time.strftime("%Y-%m-%d %H:%M")) break overlapcheck.append([ch_id, start_time, stop_time]) if not skip: xml_file.write(' <programme start="' + start + '" stop="' + stop + '" channel="' + ch_id + '">' + '\n') xml_file.write(' <title lang="en">' + name + '</title>' + '\n') xml_file.write(' <sub-title lang="en">' + schedule_dicts[j]['name'] + '</sub-title>' + '\n') xml_file.write(' <desc lang="en">' + description + '</desc>' + '\n') genres = schedule_dicts[j]['show']['genres'] if len(genres) > 0: for l in range(len(genres)): xml_file.write(' <category lang="en">' + genres[l] + '</category>' + '\n') xml_file.write( ' <category lang="en">Show</category>' + '\n') xml_file.write(' </programme>' + '\n') if noschedule: noschedule = False # Guide data other than episodes to record: for j in range(len(schedule_dicts)): skip = False episodeid = schedule_dicts[j]['id'] for k in range(len(episodes)): if str(episodeid) == episodes[k][2]: skip = True break if skip: continue name = schedule_dicts[j]['show']['name'] try: ch_id = str(schedule_dicts[j]['show']['network']['id']) tm = schedule_dicts[j]['airstamp'] start = tm[0:4] + tm[5:7] + tm[8:10] + tm[11:13] + tm[ 14:16] + tm[17:19] + ' ' + tm[19:22] + tm[23:25] start_time = datetime.strptime(start[0:14], "%Y%m%d%H%M%S") except: logging.info(' Incomplete schedule information for ' + name) logging.info(' Skipping EPG entry for ' + name) skip = True if usingNonMazeChIds and not skip: if ch_id in channelsMazeInclude: ch_id = channelsMap[channelsMazeInclude.index( ch_id)][0] else: if not ch_id in channelsMazeSkipped: if showdetails: logging.info( ' Skipping TVmaze network id ' + ch_id + ' (not included in xmltvidmap.csv)') channelsMazeSkipped = channelsMazeSkipped + [ch_id] continue if i > 0 and not skip: if str(episodeid) == newepisodes[i - 1][2]: if day != scheduleday: logging.info( ' Error in mythrecmaze.py (unexpected schedule sort order)' ) sys.exit(1) if not day in daysdone and not skip: runtime = schedule_dicts[j]['runtime'] try: description = re.sub('<[^<]+?>', '', schedule_dicts[j]['summary']) except: description = '' try: stop_time = start_time + timedelta(minutes=runtime) except TypeError: logging.info(' Unable to determine runtime for: ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) logging.info(' Guessing 60 minutes runtime for ' + name) stop_time = start_time + timedelta(minutes=60) stop = stop_time.strftime( "%Y%m%d%H%M%S") + ' ' + tm[19:22] + tm[23:25] for k in range(len(overlapcheck)): if start_time < overlapcheck[k][ 2] and stop_time > overlapcheck[k][ 1] and ch_id == overlapcheck[k][0]: if start_time >= overlapcheck[k][ 1] and stop_time <= overlapcheck[k][2]: skip = True break elif start_time < overlapcheck[k][ 1] and stop_time > overlapcheck[k][2]: skip = True break elif start_time < overlapcheck[k][ 1] and stop_time <= overlapcheck[k][2]: stop_time = overlapcheck[k][1] try: stop = stop_time.strftime( "%Y%m%d%H%M%S" ) + ' ' + tm[19:22] + tm[23:25] except: logging.info( ' Overlap detected for ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) logging.info( ' Skipping EPG entry for ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) skip = True break elif start_time >= overlapcheck[k][ 1] and stop_time > overlapcheck[k][2]: start_time = overlapcheck[k][2] try: start = start_time.strftime( "%Y%m%d%H%M%S" ) + ' ' + tm[19:22] + tm[23:25] except: logging.info( ' Overlap detected for ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) logging.info( ' Skipping EPG entry for ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) skip = True break else: skip = True break logging.info( ' Overlap detected and time adjusted for ' + name + ' ' + start_time.strftime("%Y-%m-%d %H:%M")) overlapcheck.append([ch_id, start_time, stop_time]) if not skip: xml_file.write(' <programme start="' + start + '" stop="' + stop + '" channel="' + ch_id + '">' + '\n') xml_file.write(' <title lang="en">' + name + '</title>' + '\n') xml_file.write(' <sub-title lang="en">' + schedule_dicts[j]['name'] + '</sub-title>' + '\n') xml_file.write(' <desc lang="en">' + description + '</desc>' + '\n') genres = schedule_dicts[j]['show']['genres'] if len(genres) > 0: for l in range(len(genres)): xml_file.write(' <category lang="en">' + genres[l] + '</category>' + '\n') xml_file.write( ' <category lang="en">Show</category>' + '\n') xml_file.write(' </programme>' + '\n') if noschedule: noschedule = False daysdone = daysdone + [day] xml_file.write('</tv>') if noschedule: logging.info(' Error in mythrecmaze.py (no schedule data)') sys.exit(1) if not 'mythtv' in homepath: if len(torecord) > 17: subprocess.call("zenity --info --title='Mythrecmaze' --text='" + torecord + "' --width=300 \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) else: subprocess.call( "zenity --info --title='Mythrecmaze' --text='Nothing new found to record' --width=300 \ --window-icon=/opt/mythrecmaze/mythrecmaze.svg", shell=True) else: if len(torecord) > 17: logging.info(' ' + torecord) #Run mythfilldatabase: logging.info(' Running mythfilldatabase') subprocess.call('mythfilldatabase --quiet --refresh 1 --file --sourceid ' + mythsourceid + ' --xmlfile ' + tmp_xml_file, shell=True) pym = mythRecord(mythlanip, mythport) chaninfo = pym.GetChannelInfoList(SourceID=mythsourceid, Details='true') if chaninfo: chaninfo = chaninfo['ChannelInfoList']['ChannelInfos'] else: logging.info( ' Error in mythrecmaze.py (unable to fetch channel information)') sys.exit(1) for i in range(len(chaninfo)): for j in range(len(newepisodes)): if len(newepisodes[j]) > 3: if newepisodes[j][3] == chaninfo[i]['XMLTVID']: time = newepisodes[j][4] time = datetime.strptime(time[0:22] + time[23:25], "%Y-%m-%dT%H:%M:%S%z") time = datetime.strftime(time.astimezone(timezone.utc), "%Y-%m-%dT%H:%M:%S") mythchid = chaninfo[i]['ChanId'] recRule = pym.GetRecordSchedule(ChanId=mythchid, StartTime=time) if recRule: recRule = recRule['RecRule'] recRule['Type'] = 'Single Record' recRule['Station'] = recRule['CallSign'] pym.AddRecordSchedule(recRule) else: logging.info(' Error: No record schedule found')
def tzconv(userID,tstr,dstr): ''' Grabs a timestring in HH:MM format, and converts it into next sunday + timezones ''' #check if time is within bounds if int(tstr.split(':')[0])<0 or int(tstr.split(':')[0])>23 or int(tstr.split(':')[1])<0 or int(tstr.split(':')[1])>59 : estr="Please provide a valid time" sendMessage(estr) return #check if day is within bounds if(dstr!='1'): if int(dstr.split('-')[0])<0 or int(dstr.split('-')[0])>9999 or int(dstr.split('-')[1])<1 or int(dstr.split('-')[1])>12 or int(dstr.split('-')[2])<1 or int(dstr.split('-')[2])>31: estr="Please provide a valid date" sendMessage(estr) return if int(dstr.split('-')[1]) == 2: if calendar.isleap(int(dstr.split('-')[0])): if int(dstr.split('-')[2]) > 29: estr="Please provide a valid date" sendMessage(estr) return else: if int(dstr.split('-')[2]) > 28: estr="Please provide a valid date" sendMessage(estr) return db = sqlite3.connect("data/db.db") cursor = db.cursor() cursor.execute('''SELECT userID from user where userID=?''',[userID]) d=cursor.fetchone() if d==None: estr = "You must register first!" sendMessage(estr) return #convert time into all other registered timezones time=pytz.datetime.datetime.strptime(tstr, "%H:%M") nowtime=pytz.datetime.datetime.now() time=time.replace(year=nowtime.year, month=nowtime.month, day=nowtime.day) if(dstr=='1'): if(time.weekday() != 6): time=time.replace(day=time.day + (6-time.weekday())) #sends to the next sunday if not already a sunday else: nowtime=pytz.datetime.datetime.strptime(dstr, "%Y-%m-%d") cursor.execute('''SELECT tz from user WHERE userID=? ''',[userID]) tzdata=cursor.fetchone()[0] cursor.execute('''SELECT DISTINCT tz from user''') d = cursor.fetchall() t1=pytz.timezone(pytz.country_timezones(tzdata)[0]) nowtime=t1.normalize(t1.localize(nowtime)) time=t1.normalize(t1.localize(time)) sendstr='' for x in range(0,len(d)): curtobj=time.astimezone(pytz.timezone((pytz.country_timezones(d[x][0]))[0])) sendstr=sendstr+str(curtobj.year) + '-' + str(curtobj.month) + '-' + str(curtobj.day) + ' ' + ('0'+str(curtobj.hour) if curtobj.hour<10 else str(curtobj.hour)) + ':' + ('0'+str(curtobj.minute) if curtobj.minute<10 else str(curtobj.minute)) + ' in ' + str(curtobj.tzinfo) sendstr=sendstr+'\n' print(sendstr) sendMessage(sendstr)
def __init__(self, time): time = time.astimezone(datetime.timezone.utc) self.payload = struct.pack('<7H', time.year, time.month, time.day, time.hour, time.minute, time.second, time.microsecond // 1000)
def status_report(controller): tz = pytz.timezone('Europe/London') (ra, dec) = controller.getPosition(coordinateMode=NexstarCoordinateMode.RA_DEC) print("position RA/DEC ................... : ra = {}, dec = {}".format( ra, dec)) (azimuth, altitude) = controller.getPosition( coordinateMode=NexstarCoordinateMode.AZM_ALT) print("position AZM/ALT .................. : azimuth = {}, altitude = {}". format(azimuth, altitude)) tracking_mode = controller.getTrackingMode() print("tracking mode ..................... : {}".format(tracking_mode)) (latitude, longitude) = controller.getLocation() print( "location .......................... : latitude = {}, longitude = {}". format(latitude, longitude)) (time, dst) = controller.getTime() time = time.astimezone(tz) print("time .............................. : time = {}, dst = {}".format( time.strftime("%Y-%m-%d %H:%M:%S %Z"), dst)) (versionMajor, versionMinor) = controller.getVersion() print("version ........................... : {}.{}".format( versionMajor, versionMinor)) model = controller.getModel() print("model ............................. : {}".format(model)) alignmentComplete = controller.getAlignmentComplete() print("alignment complete ................ : {}".format(alignmentComplete)) gotoInProgress = controller.getGotoInProgress() print("goto in progress .................. : {}".format(gotoInProgress)) if True: for deviceId in NexstarDeviceId: try: (versionMajor, versionMinor) = controller.getDeviceVersion(deviceId) status = "version = {}.{}".format(versionMajor, versionMinor) except NexstarPassthroughError as exception: status = "error: {}".format(exception) print("device {} {} : {}".format( deviceId.name, "." * (27 - len(str(deviceId.name))), status)) model = controller.getModel() print("model ............................. : {}".format(model)) # Below are some low-level HC <-> MC commands: # # Standard boot sequence: # 3b 03 0d 11 05 da 3b 05 11 0d 05 0f 87 42 # 3b 03 0d 10 05 db 3b 05 10 0d 05 0f 87 43 # 3b 03 0d 10 fe e2 3b 05 10 0d fe 06 0d cd # 3b 03 0d 10 fc e4 3b 04 10 0d fc 00 e3 # 3b 03 0d 11 fc e3 3b 04 11 0d fc 01 e1 # Slew left (hand controller) # # 3b 04 0d 10 25 09 b1 3b 04 10 0d 25 01 b9 # 3b 04 0d 10 24 00 bb 3b 04 10 0d 24 01 ba # # Slew right (hand controller) # # 3b 04 0d 10 24 09 b2 3b 04 10 0d 24 01 ba # 3b 04 0d 10 24 00 bb 3b 04 10 0d 24 01 ba # # Slew up (hand controller) # # 3b 04 0d 11 24 09 b1 3b 04 11 0d 24 01 b9 # 3b 04 0d 11 24 00 ba 3b 04 11 0d 24 01 b9 # # Slew down (hand controller) # # 3b 04 0d 11 25 09 b0 3b 04 11 0d 25 01 b8 # 3b 04 0d 11 24 00 ba 3b 04 11 0d 24 01 b9
def _to_endomondo_time(time): return time.astimezone(pytz.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
def utc_to_local(time): time = add_utc_timezone(time) pacific = pytz.timezone("US/Pacific") time = time.astimezone(pacific) return time
def toTimezone(time,to_zone): return time.astimezone(tz.gettz(to_zone))
def convert_time(ts): eastern = pytz.timezone('US/Eastern') time = datetime.utcfromtimestamp(ts) time = time.astimezone(eastern).strftime('%Y-%m-%d %I:%M %p') return time[12:]
"https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRGx6TVdZU0FtVnVHZ0pKVGlnQVAB?hl=en-IN&gl=IN&ceid=IN%3Aen" ) soup = BeautifulSoup(r.text, "lxml") all_news = soup.find_all("div", class_="xrnccd") main_data = [] sub_data = [] for item in all_news: data = [] title = item.find("h3", class_="ipQwMb ekueJc gEATFF RD0gLb") time = item.time['datetime'] date, time = time.split('T') time = time[:-1] time = f"{date} {time} +0000" time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S %z') time = time.astimezone(pytz.timezone('Asia/Kolkata')) # Create link link = item.a['href'] link = ''.join(["https://news.google.com", link[1:]]) # Use Article from newspaper try: art = Article(link, language="en") art.download() art.parse() art.nlp() # Create main data data.append(f"Article's Date :- {time.date()} {time.time()}") data.append(f"Article's Title :- {title.text}")
def cluster_analysis(self, distance_table: pd.DataFrame, species_table: Optional[pd.DataFrame], input_file: str) -> None: with open(self.output_name("Cluster analysis"), mode='w') as output_file: # extracting options distance_kind = distances_short_names[distances_names.index( self.cluster_distance.get())] try: cluster_threshold = float(self.cluster_size.get()) except Exception: warnings.warn( f"Invalid cluster threshold {self.cluster_size.get()}.\nUsing default: 0.3" ) cluster_threshold = 0.3 # preparing the table nodes = distance_table["seqid (query 1)"].unique() distance_table = distance_table[[ "seqid (query 1)", "seqid (query 2)", distance_kind ]].copy() distance_table.columns = ["seqid1", "seqid2", "distance"] # calculating components connected_table = distance_table.loc[(distance_table['distance'] < cluster_threshold)] graph = nx.from_pandas_edgelist(connected_table, source="seqid1", target="seqid2") graph.add_nodes_from(nodes) components = nx.connected_components(graph) print( f"Samples were clustered at a threshold of {cluster_threshold:.3g} ({cluster_threshold*100:.2g}%) uncorrected p-distance", file=output_file) # add cluster classification to the table cluster_of: Dict[str, int] = {} max_samples = 0 min_samples = len(distance_table) for i, component in enumerate(components): print(f'Cluster{i+1}: {", ".join(component)}', file=output_file) min_samples = min(min_samples, len(component)) max_samples = max(max_samples, len(component)) for seqid in component: cluster_of[seqid] = i num_clusters = i + 1 distance_table["cluster1"] = distance_table["seqid1"].map( cluster_of) distance_table["cluster2"] = distance_table["seqid2"].map( cluster_of) print("\n", file=output_file) max_in_cluster_distances = distance_table.loc[ distance_table["cluster1"] == distance_table["cluster2"]][[ "cluster1", "distance" ]].groupby("cluster1").max()["distance"] print( "Maximum intra-sample distance within clusters (marked with # if above specified threshold):", file=output_file) big_clusters = 0 for cluster_i, distance in max_in_cluster_distances.items(): if math.isnan(distance): distance = 0 if distance > cluster_threshold: big_clusters += 1 print(f'Cluster{cluster_i+1}: {distance:.4g} #', file=output_file) else: print(f'Cluster{cluster_i+1}: {distance:.4g}', file=output_file) output_file.write("\n") min_between_cluster_distance = distance_table.loc[ distance_table["cluster1"] > distance_table["cluster2"]][[ "cluster1", "cluster2", "distance" ]].groupby(["cluster1", "cluster2"]).min()["distance"].unstack() for i in range(num_clusters): min_between_cluster_distance.at[(i, i)] = 0 min_between_cluster_distance.sort_index(axis=0, inplace=True) min_between_cluster_distance.index.name = '' min_between_cluster_distance.rename( index=(lambda i: f"Cluster{i+1}"), columns=(lambda i: f"Cluster{i+1}"), inplace=True) print("Minimum distance between clusters:\n", file=output_file) min_between_cluster_distance.to_csv(output_file, sep='\t', line_terminator='\n', float_format="%.4g", index=True) output_file.write('\n') print("Total number of clusters:", num_clusters, file=output_file) print( "Number of clusters violating threshold for intra-cluster distances:", big_clusters, file=output_file) output_file.write('\n') print( f"A total of {num_clusters} clusters were found, containing between {min_samples} and {max_samples} samples.", file=output_file) output_file.write('\n') if species_table is not None: species_table["cluster"] = species_table.index.to_series().map( cluster_of) + 1 species_table.drop_duplicates(inplace=True) print( "Comparison of cluster assignment with species assignment in the input file:", file=output_file) exists_multicluster_species = False for (species, clusters) in species_table.groupby("species")["cluster"]: if len(clusters) > 1: print( f"Sequences of {species} are included in {len(clusters)} clusters:", ", ".join(clusters.astype(str)), file=output_file) exists_multicluster_species = True if exists_multicluster_species: print( "Sequences of all other species are included in only one cluster, respectively", file=output_file) else: print( "Sequences of all species are included in only one cluster, respectively.", file=output_file) output_file.write('\n') print( "List of clusters containing sequences of more than one species (according to species assignment in the input file):", file=output_file) exists_multispecies_cluster = False for (cluster, species) in species_table.groupby("cluster")["species"]: if len(species) > 1: print( f"Cluster {cluster} contains sequences of {len(species)} species:", ", ".join(species), file=output_file) exists_multispecies_cluster = True if exists_multispecies_cluster: print( "All other clusters contain sequences of only a single species, respectively.", file=output_file) else: print( "All clusters contain sequences of only one species, respectively.", file=output_file) output_file.write('\n') time = datetime.datetime.now() with open(os.path.join( self.output_dir, "taxi2_cluster_" + time.strftime("%Y-%m-%dT%H%M%S") + ".spart"), mode='w') as spart_file: print("begin spart;", file=spart_file) print("project_name = taxi2_clustering;", file=spart_file) print("Date = " + time.astimezone().isoformat(), file=spart_file) print("N_spartitions = " + str(num_clusters) + " : " + spart_form(input_file) + ";", file=spart_file) print("N_individuals = " + str(len(cluster_of)) + ";", file=spart_file) print("N_subsets = " + str(num_clusters) + ";", file=spart_file) print( f"[Generated by a simple {cluster_threshold*100:.2g}% threshold clustering in taxi2]", file=spart_file) print( "[WARNING: The sample names below may have been changed to fit SPART specification (only alphanumeric characters and _ )]", file=spart_file) print( f"[The following clusters included sequences differing by a distance above the threshold: {cluster_threshold:.3g}]", file=spart_file) print("Individual_assignment = ", file=spart_file, end='') for specimen, cluster in cluster_of.items(): print(f"\n{spart_form(specimen)}: {cluster + 1}", file=spart_file, end='') print(";\n", file=spart_file) print("end;", file=spart_file)
def local_to_utc(time): utc = pytz.timezone("UTC") time = time.astimezone(utc) return time