Jump to content

[SOLVED] Alfred workflow to remove initial ">>>" sign from selected string.


Recommended Posts

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.

 

 

alfred1.png

alfred2.png

Edited by Bhishan
Link to comment

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}"

Link to comment

@vitor Thanks, it worked, but partially.

 

The workflow I tested:

ans1.png.668da57ea3c0d5e7539815b939e8c56f.png

 

 

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 by Bhishan
Link to comment

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")

Link to comment
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?

 

 

Link to comment

@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 by Bhishan
Link to comment

@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.

 

Link to comment
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 by deanishe
Link to comment

@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

 

 

py3.png.9405bc26fbbca4b6f3fd14a383245e34.png

 

ans3.thumb.png.1a744afb4264fce69dae73c63dbef8d4.png

 

Edited by Bhishan
Link to comment

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.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...