Jump to content

Bhishan

Member
  • Posts

    146
  • Joined

  • Last visited

Posts posted by Bhishan

  1. **Question**

    How to plot column0 vs column2 of a data file using Alfred and Python?

     

    Shared Workflow

    ===============

     https://github.com/bhishanpdl/Shared/blob/master/Alfred_questions/python_plots/aa python plots.alfredworkflow?raw=true

     

    Sample datafile: https://raw.githubusercontent.com/bhishanpdl/Shared/master/Alfred_questions/python_plots/data.txt

     

    The given workflow reads the column0 vs column1 of the given data file and plots it.

    Is it possible to make plot column0 vs column2 using arguments?

     

    Required Method:

    =============

    1. selet the given file in Finder

    2. double tap shift (open alfred3)

    3. plot 0 2      # This should plot column0 vs column2

     

     

    The python script used:

    =================

    import sys
    import numpy as np 
    import matplotlib.pyplot as plt
    
    ifile = sys.argv[1]
    
    x,y = np.genfromtxt(ifile,delimiter='',usecols=(0,1),\
                      comments='#',unpack=True,dtype=None).astype(np.float64)
    
    plt.plot(x,y,'ro')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.xlim(min(x)*0.9, max(x)*1.1)
    plt.ylim(min(y)*0.9, max(y)*1.1)
    plt.show()

     

     

    Problem:

    ========

    I could not figure out how to pass the arguments to the python workflow when we select the file from the Finder.

     

     

    Help will be appreciated. 

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

     

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

     

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

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

     

  6.  

    The last bash script in the workflow has following code:

    query=$1
    if [ $query = "hide" ]; then
        
       defaults write com.apple.Dock autohide -bool TRUE; killall Dock            
    fi
    if [ $query = "show" ]; then
        
       defaults write com.apple.Dock autohide -bool FALSE; killall Dock            
    fi

     

    Can we refactor this code so that it would look nicer, and still works on Alfred?

     

    My attempt was this, but it did not work:

     

    [[ "${1}" == 'var_show' ]] && defaults write com.apple.Dock autohide -bool FALSE; killall Dock
    [[ "${1}" == 'var_hide' ]] && defaults write com.apple.Dock autohide -bool TRUE; killall Dock

     

  7. @vitor   Thanks a lot. It works now.

     

    But, can we change the bash script like this:

     

    # Wanted version
    [[ "${1}" == 'var_show' ]] && defaults write com.apple.Dock autohide -bool FALSE; killall Dock
    [[ "${1}" == 'var_hide' ]] && defaults write com.apple.Dock autohide -bool TRUE; killall Dock
    
    # Ugly but working version:
    query=$1
    if [ $query = "hide" ]; then
        
       defaults write com.apple.Dock autohide -bool TRUE; killall Dock            
    fi
    if [ $query = "show" ]; then
        
       defaults write com.apple.Dock autohide -bool FALSE; killall Dock            
    fi

     

×
×
  • Create New...