Bhishan Posted June 11, 2018 Posted June 11, 2018 (edited) Basically, I wanted to remove the initial string ">>> " from all the lines of selected text. The current workflow does not strip the string ">>> " and paste the original string. I have shared the workflow in github . How the problem can be fixed. Edited October 14, 2018 by Bhishan
vitor Posted June 11, 2018 Posted June 11, 2018 Your code is applying just to the beginning of the string, not to each line. But no need to call four lines of Python, including one import. It can be done in a single line of Bash: sed 's/^>>> //' <<< "${1}" Or, since you seem to also wanting to get rid of ... and whitespace: perl -pe 's/^[>.]{0,3}\s*//' <<< "${1}" Or better yet, if you want to get rid of any combination of >, ., or whitespace at the beginning of a line: perl -pe 's/^[>.\s]*//' <<< "${1}"
Bhishan Posted June 11, 2018 Author Posted June 11, 2018 (edited) @vitor Thanks, it worked, but partially. The workflow I tested: I also tried to put ` inside the bracket [`>.\s] but it did not work. Many many python tutorials are given in the following format: For example:, https://docs.python.org/2/library/collections.html Original text to copy: >>> # Tally occurrences of words in a list >>> cnt = Counter() >>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: ... cnt[word] += 1 >>> cnt Counter({'blue': 3, 'red': 2, 'green': 1}) >>> # Find the ten most common words in Hamlet >>> import re >>> words = re.findall(r'\w+', open('hamlet.txt').read().lower()) >>> Counter(words).most_common(10) [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631), ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)] The output I got is: `$>>> # Tally occurrences of words in a list cnt = Counter() for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: cnt[word] += 1 cnt Counter({'blue': 3, 'red': 2, 'green': 1}) # Find the ten most common words in Hamlet import re words = re.findall(r'\w+', open('hamlet.txt').read().lower()) Counter(words).most_common(10) [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631), ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)] ` I would like to remove backticks and copy the line only if it stars with ">>>". Preferred output: # Tally occurrences of words in a list cnt = Counter() for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: cnt[word] += 1 cnt # Find the ten most common words in Hamlet import re words = re.findall(r'\w+', open('hamlet.txt').read().lower()) Counter(words).most_common(10) **Changes** 1. Initial <<<oneSpace is removed. 2. Initial 3dotsOnespace is removed. 3. If the line does not start with <<< it will not be copied. 4. For some reason there is a hidden backtick ` it should not be copied. This would be highly helpful. In python documentation, many tutorials are in above format and copying them with without unwanted angles will be much helpful. I hope I would get further help. Thanks a lot! Edited June 11, 2018 by Bhishan
vitor Posted June 11, 2018 Posted June 11, 2018 I ask that next time you expose your problem all at once, so it doesn’t become again an XY problem. If you give us incomplete information, we can’t help you effectively. Here’s ruby code that’ll do it: regex = /^[>.]{3} / puts ARGV[0].split("\n").select { |l| l =~ regex }.map { |l| l.sub regex, ''}.join("\n")
deanishe Posted June 11, 2018 Posted June 11, 2018 8 hours ago, vitor said: But no need to call four lines of Python, including one import. It can be done in a single line of Bash There is if it's understandable code. I mean, your sed and perl and ruby don't take up much space, but they also ain't much fun to read… @Bhishan What are you actually trying to do? Copy examples of using the Python REPL and turn them into valid Python code?
Bhishan Posted June 11, 2018 Author Posted June 11, 2018 (edited) @deanishe Dr. deanishe, Today I learned little bit "sed" thanks to admin "vitor" and trying to fix my problem. In the mean time, I saw a comment with python, where I feel more comfortable working in. Yes I was trying to copy the python REPL documentation and pasting it without ">>>" sign probably created using "reST" and "Sphinx". I am familiar with python and extremely unfamiliar with "perl" and "ruby". If only I get one example in python for this problem next similar problems I would solve easily using python and regex module. Thanks for the concern, and I would again appreciate if this problem can be solved using also python in Alfred? (Without using Alfred I can definitely do this.) I hope it is possible in Alfred! Edited June 11, 2018 by Bhishan
Bhishan Posted June 11, 2018 Author Posted June 11, 2018 @vitor, Sorry This time it only prints only two backticks: ``. I am unfamiliar with perl and ruby and learning regex in python. I could not fix the problem in ruby and again seek for the help. As long as the workflow works, It is immaterial which language I use: whether perl, ruby, or the mighty python. P.S. I appreciate your effort and help and apologize for asking XY-problem since I thought If it was in python I would have figure out myself after learning one simple example.
deanishe Posted June 11, 2018 Posted June 11, 2018 (edited) 30 minutes ago, Bhishan said: Yes I was trying to copy the python REPL documentation What I meant is, you're trying to turn a Python REPL session into valid Python code. 30 minutes ago, Bhishan said: If only I get one example in python for this problem next similar problems I would solve easily using python and regex module. Yes, it's very simple to do, and there's no need at all for the re module. You just needed to be clear about what you were trying to achieve. Here it is in Python. You can't just keep the lines starting with >>>, you need the continuation lines, too. And because it's Python, you have to preserve the indentation. def extract_code_from_repl(string): """Take a Python REPL session and extract user input.""" code = [] lines = [s for s in string.split('\n') if s.strip()] for line in lines: if line[:4] in ('>>> ', '... '): # user input code.append(line[4:]) return '\n'.join(code) The above function correctly converts the examples I copied from the Python docs into working code. The backticks appear to be something weird happening at your end. If it continues, add string = string.strip('`$') to the top of the function. Edited June 11, 2018 by deanishe
Bhishan Posted June 11, 2018 Author Posted June 11, 2018 (edited) @deanishe It also pastes only two backticks. Tested: https://docs.python.org/2/library/collections.html Here is my workflow: https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/text_manipulation/z Text Manipulation_new_python.alfredworkflow?raw=true Edited June 11, 2018 by Bhishan
deanishe Posted June 11, 2018 Posted June 11, 2018 13 minutes ago, Bhishan said: It also pastes only two backticks. You're adding the backticks yourself in your Copy to Clipboard output… Don't use {query}. Use query = sys.argv[1] and "with input as argv".
Bhishan Posted June 11, 2018 Author Posted June 11, 2018 (edited) @deanishe Nearly perfect, It is a very simple issue, I can always delete backticks easily, but just to complete the question, it also pastes backticks. This time I used: I also tried this, but did not work: ans = extract_code_from_repl(query.strip("`")) # try1 ans = extract_code_from_repl(query.strip(r"`")) # try2 ans = extract_code_from_repl(query.strip("\`")) # try3 Edited June 11, 2018 by Bhishan
deanishe Posted June 11, 2018 Posted June 11, 2018 Like I just said, you're adding the backticks yourself in the Copy to Clipboard output:
Bhishan Posted June 11, 2018 Author Posted June 11, 2018 Oh my goodness! I only looked at the source code (Run Script ) not at the "Copy to Clipboard" part! It was really silly, sorry for that. The good thing is it worked now and I will tackle similar problems easily in the future. You made my day, thanks.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now