I have a simple but long loop in Python 3 that calls a simple function. I want to stop the execution, and that should be possible with Ctrl-C but that doesn't work. How to do this "manual keyboard interrupt"? I use Anconda3 for windows64, with Python 3.7.
Ctrl-C doesnt do anything, and also the red button in the IPython console doesn't do anything.
# -*- coding: utf-8 -*-
"""
Python3.7
Created on Wed Jul 24 12:59:31 2019
This proves that Ctr C doesn't work in Spyder
#author: Rob
"""
#%% loop in main program
def hello():
print('Hello Spyder '+str(nr))
import time
nr = 0
while nr < 42:
nr +=1
hello()
time.sleep(1)
#%% loop in function
def hello():
nr = 0
while nr < 42:
nr +=1
print('Hello Spyder '+str(nr))
time.sleep(1)
import time
hello()
I expect the code to stop when using keyboard CTrl-C. But the code just keeps on executing til the end.
probably already solved this yourself, but try this.
import time
i = 0
try:
while i < 100:
print (i)
i = i + 1
time.sleep(1)
except KeyboardInterrupt:
print("interrupted")
Related
I'm trying to making a BLAST search with a nucleotide sequence and print the best matching hit but not sure about which option/command should I use. There are options like max_hpsp and best_hit_overhang. I don't have an idea about their differences and I want to print just 1 hit. (best matching one) Should i use max_hpsp 1?
I wrote this code but it's still not useful. If you could tell me, where I am mistaken and what should to do, I would be very appreciated :) Thank you!
from Bio.Blast import NCBIWWW
seq = Seq("GTTGA......CT")
def best_matching_hit(seq):
try:
result_handle = NCBIWWW.qblast("blastn", "nt", seq)
except:
print('BLAST run failed!')
return None
blast_record = NCBIXML.read(result_handle)
for hit in blast_record.alignments:
for hsp in hit.hsps:
if hsp.expect == max_hsps 1:
print(hit.title)
print(hsp.sbjct)
best_matching_hit(seq)
this returns just one hit , the first one I suppose, as per
Limiting the number of hits in a Biopython NCBIWWW Search on Biostars:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 7 15:28:11 2021
#author: Pietro
https://stackoverflow.com/questions/67872118/how-to-print-the-best-matching-hit-in-the-blast-search-biopython
"""
from Bio.Blast import NCBIWWW
from Bio.Seq import Seq
seq = Seq("ATGGCGTGGAATGAGCCTGGAAATAACAACGGCAACAATGGCCGCGATAATGACCCTTGGGGTAATAA\
TAATCGTGGTGGCCAGCGTCCTGGTGGCCGAGATCAAGGTCCGCCAGATTTAGATGAAGTGTTCAACAA\
ACTGAGTCAAAAGCTGGGTGGCAAGTTTGGTAAAAAAGGCGGCGGTGGTTCCTCTATCGGCGGTGGCGG\
TGGTGCAATTGGCTTTGGTGTCATTGCGATCATTGCAATTGCGGTGTGGATTTTCGCTGGTTTTTACAC\
CATCGGTGAAGCAGAGCGTGGTGTTGTACTGCGTTTAGGTAAATACGATCGTATCGTAGACCCAGGCCT\
TAACTGGCGTCCTCGTTTTATTGATGAATACGAAGCGGTTAACGTACAAGCGATTCGCTCACTACGTGC\
ATCTGGTCTAATGCTGACGAAAGATGAAAACGTAGTAACGGTTGCAATGGACGTTCAATACCGAGTTGC\
TGACCCATACAAATACCTATACCGCGTGACCAATGCAGATGATAGCTTGCGTCAAGCAACAGACTCTGC\
GCTACGTGCGGTAATTGGTGATTCACTAATGGATAGCATTCTAACCAGTGGTCGTCAGCAAATTCGTCA\
AAGCACTCAAGAAACACTAAACCAAATCATCGATAGCTATGATATGGGTCTGGTGATTGTTGACGTGAA\
CTTCCAGTCTGCACGTCCGCCAGAGCAAGTAAAAGATGCGTTTGATGACGCGATTGCTGCGCGTGAGGA\
TGAAGAGCGTTTCATCCGTGAAGCAGAAGCTTACAAGAACGAAATCTTGCCGAAGGCAACGGGTCGTGC\
TGAACGTTTGAAGAAGGAAGCTCAAGGTTACAACGAGCGTGTAACTAACGAAGCATTAGGTCAAGTAGC\
ACAGTTTGAAAAACTACTACCTGAATACCAAGCGGCTCCTGGCGTAACACGTGACCGTCTGTACATTGA\
CGCGATGGAAGAGGTTTACACCAACACATCTAAAGTGTTGATTGACTCTGAATCAAGCGGCAACCTTTT\
GTACCTACCAATCGATAAATTGGCAGGTCAAGAAGGCCAAACAGACACTAAACGTAAATCGAAATCTTC\
TTCAACCTACGATCACATTCAACTAGAGTCTGAGCGTACACAAGAAGAAACATCGAACACGCAGTCTCG\
TTCAACAGGTACACGTCAAGGGAGATACTAA")
def best_matching_hit(seq):
try:
result_handle = NCBIWWW.qblast("blastn", "nt", seq, hitlist_size=1)
except:
print('BLAST run failed!')
return None
blast_record = result_handle.read()
print(blast_record)
best_matching_hit(seq)
I have a simple pipeline that receives data from PubSub, prints it and then at every 10 seconds fires a window into a GroupByKey and prints that message again.
However this window seems to be delaying sometimes. Is this a google limitation or is there something wrong with my code:
with beam.Pipeline(options=pipeline_options) as pipe:
messages = (
pipe
| beam.io.ReadFromPubSub(subscription=known_args.input_subscription).with_output_types(bytes)
| 'decode' >> beam.Map(lambda x: x.decode('utf-8'))
| 'Ex' >> beam.ParDo(ExtractorAndPrinter())
| beam.WindowInto(window.FixedWindows(10), allowed_lateness=0, accumulation_mode=AccumulationMode.DISCARDING, trigger=AfterProcessingTime(10) )
| 'group' >> beam.GroupByKey()
| 'PRINTER' >> beam.ParDo(PrinterWorker()))
Edit for the most recent code. I removed the triggers however the problem persists:
class ExtractorAndCounter(beam.DoFn):
def __init__(self):
beam.DoFn.__init__(self)
def process(self, element, *args, **kwargs):
import logging
logging.info(element)
return [("Message", json.loads(element)["Message"])]
class PrinterWorker(beam.DoFn):
def __init__(self):
beam.DoFn.__init__(self)
def process(self, element, *args, **kwargs):
import logging
logging.info(element)
return [str(element)]
class DefineTimestamp(beam.DoFn):
def process(self, element, *args, **kwargs):
from datetime import datetime
return [(str(datetime.now()), element)]
def run(argv=None, save_main_session=True):
"""Build and run the pipeline."""
parser = argparse.ArgumentParser()
parser.add_argument(
'--output_topic',
required=True,
help=(
'Output PubSub topic of the form '
'"projects/<PROJECT>/topics/<TOPIC>".'))
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--input_topic',
help=(
'Input PubSub topic of the form '
'"projects/<PROJECT>/topics/<TOPIC>".'))
group.add_argument(
'--input_subscription',
help=(
'Input PubSub subscription of the form '
'"projects/<PROJECT>/subscriptions/<SUBSCRIPTION>."'))
known_args, pipeline_args = parser.parse_known_args(argv)
pipeline_options = PipelineOptions(pipeline_args)
pipeline_options.view_as(SetupOptions).save_main_session = save_main_session
pipeline_options.view_as(StandardOptions).streaming = True
with beam.Pipeline(options=pipeline_options) as pipe:
messages = (
pipe
| beam.io.ReadFromPubSub(subscription=known_args.input_subscription).with_output_types(bytes)
| 'decode' >> beam.Map(lambda x: x.decode('utf-8'))
| 'Ex' >> beam.ParDo(ExtractorAndCounter())
| beam.WindowInto(window.FixedWindows(10))
| 'group' >> beam.GroupByKey()
| 'PRINTER' >> beam.ParDo(PrinterWorker())
| 'encode' >> beam.Map(lambda x: x.encode('utf-8'))
| beam.io.WriteToPubSub(known_args.output_topic))
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
run()
So what this basically ask the pipeline to do is to group elements to 10 second windows and fire every window after 10 seconds have passed since the first element was received for each window (and discard the rest of the data for that window). Was that your intention ?
Assuming this was the case, note that triggering depends on the time elements were received by the system as well as the time the first element is received for each window. Probably this is why you are seeing some variation in your results.
I think if you need more consistent grouping for your elements you should use event time triggers instead of processing time triggers.
All the triggers are based on best effort means they will be fired sometime after the specified duration, 10 sec in this case. Generally it happen close to the time specified but a few seconds delay is possible.
Also, the triggers are set for Key+Window. The window is derived from event time.
It is possible that the first GBK pint at 10:30:04 is due to the first element which as at 10:29:52
The 2nd GBK print at 10:30:07 is due to the first element at 10:29:56
So it will be good to print the window and event timestamp for each element and then co-relate the data.
I tried to implement FizzBuzz in elixir. I think it works, but I have some weird behavior and it may be my environment. I can run the following .exs file using elixir fizzbuzz.exs and on the first run it works as you would expect. Even on the second run it would work as you would expect. However, if I wait 7 or 8 seconds and run it again, it will stop at 13, or maybe 14. It might even make it to 50. There really isn't anything too consistent about what I am experiencing. I've reproduced it on the first run, I've reproduced it after waiting two seconds between runs. I've even gone a few minutes without being able to reproduce it at all. Please help me...
IO.puts "Hello world from Elixir"
defmodule Fizzbuzz do
def run(max) do
run(1, max)
end
def run(n, max) do
if rem(n, 3) == 0 do
IO.puts "FIZZ"
end
if rem(n, 5) == 0 do
IO.puts "BUZZ"
end
if rem(n, 3) != 0 && rem(n, 5) != 0 do
IO.puts n
end
if n < max do
run(n + 1, max)
end
end
end
Fizzbuzz.run(100)
#: Elixir -v
Erlang/OTP 21 [erts-10.2] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]
Elixir 1.9.4 (compiled with Erlang/OTP 20)
First, try updating to the latest release of Elixir, 1.13, currently.
Try running your file with mix run <filename>, so in your case:
mix run fizzbuzz.exs
I'm playing with Lua following this link: https://www.lua.org/pil/4.2.html and get confused about a point.
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> x=10
> local i=1
> while i<=x do
>> local x = i*2
>> print(x)
>> i=i+1
>> end
stdin:1: attempt to compare nil with number
stack traceback:
stdin:1: in main chunk
[C]: in ?
I guess this error message indicates that something is wrong with the expression while i<=x. Any comments are greatly appreciated.
EDIT: I just realize that it's probably because it does not work in a terminal.
It did not work out in an interactive terminal. Because local i=1 is understood by terminal as a chunk by itself once you hit enter. That is why the "attempt to compare nil with number" error; because i is not defined, i.e., nil in this case. To correct it, put the first two lines and the while loop inside of a do chuck as the following.
> do
>> x = 10
>> local i=1
>> while i<=x do
>> local x = i*2
>> print(x)
>> i = i+1
>> end
>> end
2
4
6
8
10
12
14
16
18
20
>
Actually the problem is in local i=1 try
> local i = 1
> print(i)
The problem is that when running the console, it seems that the line is a chunk and the variable is local inside that chunk.
you can fix that by using a global variable or you can do this
> local i = 1 do
>> print(i)
>> end
Which results in chunk structure like this [local i [print(i)]] therefore i can be accessed. Note also that local x = i*2 is valid since it is inside the while - do chunk.
Your code would also work correctly, if it was inside a Lua file.
I could reproduce the problem in Lua 5.3.4 as well.
If you read on in the Lua docs, chapter 4.2 – Local Variables and Blocks, you'll get to the sentence
Beware that this example will not work as expected if you enter it in interactive mode. The second line, local i = 1, is a complete chunk by itself.
This addresses exactly the issue in question. So it seems that the Lua interpreter has limited support for an outmost chunk (that is clearly present in a Lua file). But this behaviour seems to me acceptable and understandable in view of the compactness of language and interpreter.
So, when in interactive mode,
either leave out the local before variable i to make it work:
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
Lua>x=10
Lua>i=1
Lua>while i<=x do
...>local x=i*2
...>print(x)
...>i=i+1
...>end
or start enclose the whole by a block:
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
Lua>do
...>local x=10
...>local i=1
...>while i<=x do
...>local x=i*2
...>print(x)
...>i=i+1
...>end
...>end
Both options will produce to the regular (and expected) output:
2
4
6
8
10
12
14
16
18
20
So I was struggling with this problem a long time last night and finally figured it out so I wanted to post here in case someone ran across the same issue.
The goal is to parse the output of FFmpeg so that it would run in a Sidekiq worker and save progress and duration to an ActiveRecord model so I could get a progress bar in the UI by polling the database.
How do I parse ffmpeg duration and time real-time without waiting for the process to finish?
There are two problems:
How to parse output real-time in ruby?
Since FFmpeg writes progress output on one line, how do you get that output?
For the first one I looked at the ruby Open3 module and this article was particularly helpful. The gist is this:
require 'open3'
cmd = "bash my_long_running_command.sh"
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
stdout.each do |line|
# this will print as stdout is being written to the stream real-time
puts line
end
end
Now this works well for many commands and all but not with FFmpeg. FFmpeg is weird in that it writes it's progress and output to STDERR instead of STDOUT.
Not only that but as I finally noticed the line delimiter of FFmpeg is not \n as it is in most bash commands but rather \r which is how it is able to edit the progress inline. Now that we know that the only updates are the stream and the delimiter, which can be overridden in the each method as the first argument.
require 'open3'
cmd = "ffmpeg -i input.mp4 ... output.mp4"
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
duration = 0
progress = 0
stderr.each("\r") do |line|
next unless (line.include?("time=") || line.include?("Duration:"))
if duration == 0
if line =~ /Duration:\s+(\d{2}):(\d{2}):(\d{2}).(\d{1,2})/
duration = $1.to_i * 3600 + $2.to_i * 60 + $3
end
end
percentage = if line =~ /time(\d{2}):(\d{2}):(\d{2})/
progress = $1.to_i * 3600 + $2.to_i * 60 + $3
(progress.to_f/duration.to_f) * 100
end
puts "#{percentage}%"
end
end
You can also use a gem that I found that does something very similar called stremio-ffmpeg that I sadly found after I already implemented my solution. If you would like to do the same thing as above, just do:
require 'streamio-ffmpeg'
movie = FFMPEG::Movie.new('input.mp4')
duration = movie.duration
movie.transcode('output.mp4', options_as_a_string) do |progress|
percentage = (progress.to_f/duration.to_f) * 100
puts "#{percentage}%"
end