Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, October 03, 2018

Python script: MapIt.py


Based on an example, script of mapIt.py, from the video of Automate the Boring Stuff with Python, I figured out that the codes should be changed in Mac OS X, as I mentioned that there are multiple versions of python in Max OS in my previous blog.

In addition to those changes, there are some steps that should done in Mac to make the script as executable in terminal. I further make it working in Spotlight as well. Here are my notes.

Script

Based on the video, I made minor changes on the script. Here is what I have now:


  1. #! /usr/bin/env python3
  2. import webbrowser, sys, pyperclip
  3. sys.argv  # example ['mapit.py', '870', 'Valencia', 'St.']
  4. address = 'Home'
  5. if len(sys.argv) > 1 :
  6.    address = ' '.join(sys.argv[1:]) # ['870', 'Valencia', 'St.']
  7. else :
  8.    address = pyperclip.paste()
  9. # https://www.google.com/maps/place/<address>
  10. webbrowser.open('https://www.google.com/maps/place/' + address)

The first line is important if you want to run the script directly from terminal or Spotlight. Still there are some other steps to be done in order to run the script directly from terminal or Spotlight.

Notice that I specify the name of python is python3! Another minor change is the default address value: Home. I am using Mac and the Google map knows where my home is in my local Mac account.

I saved the script at my local user account path: ~/Programming/py/mapit.py With script ready, I test the script in terminal:

...$ python3 Programming/py/mapit.py Park Ave 83 St New York

As soon as I type in the above command, a new tab page of Google Map is opened in Safari with the correct address.



Make the Script Executable

So far so good. However, I would prefer to open the script directly from terminal, or from Spotlight, without specifying python3. For the case of terminal, first I have to make the script executable with the following commands:

...$chmod a+x ~/Programming/py/mapit.py

The second step is to add my script path to env PATH so that I can simply type in mapit.py anywhere in command line. This is done by adding the path in ~/.bash_profile:

  1. PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}:/Users/dchu/Programming/py"
  2. export PATH

With above setup ready, now I can run the script anywhere in terminal:

...$ mapit.py Donggaodi Hongxing St Beijing

Isn't that cool?




Make the Script Runnable in Spotlight


Spotlight on Mac is a very convenient way to launch an app, to open a file, and to find information. However, unlike Windows Run, it not the way to direcly launch an app with arguments. Even though I set my script mapit.py as executable, I cannot run it from Spotlight. It will only open the script in Xcode!

One way to get it work in Spotlight is to change the script to .command extension. Mac OS will recognize this type of file as a runnable command.

I prefer to keep my script as .py. In UNIX or Mac, I can create an alias file of script: hard link(only hard link works in Spotlight). Here is the command to set it up:

  1. ...$ cd ~/Programming/py
  2. ...$ ln mapit.py mapit.command

The good thing is that any change to my script will be automatically reflected in the command file.

In order to open the Google Map with specified address, I need to copy the address to clipboard first. Then I open my Spotlight and type in mapit.command. It will pick up the address and open Google map in my default browser.

The only thing is that I cannot specify address directly as arguments in Spotlight. This is the limitation of it, and I cannot find a way to do that.

This is why I need to use pyperclip package in my script.




Note: another minor thing has to be done to make it runnable cleanly. The command is running in terminal. When the Spotlight launches the command and opens the map in browser, it will leave the terminal in open status. I prefer to close the terminal automatically.

To do that, go to terminal Preferences..., change the shell setting to Close the Windows:




The last word I have to say is that even the python script is very powerful and convenient to open a map with an address. I think Mac's Automator is much more powerful. It is easy to create (as a service), and can be set to selected text to open a map. Even though, I think it is a very good practice and great programming experience.

References


Read More...

Tuesday, September 25, 2018

Python and Packages

Recently I started to watch a long training video on YouTube, 9+ hours. I just spent fraction of a few minutes daily as alternative refreshment. During the training period, I tired to use Idle app to practice some codes. Up to the point of installing third party packages, I could not get import to work on my Mac. I spent couple of days struggling and finally I figured it out. I think it is worthwhile to take some notes about this experience.

Tool to Install Packages


The first hurdle is that there is no pip, a tool to install third party python packages, available in terminal on Mac. It seems that python is part of Mac OS system, but the version of python is an old version: 2.5 & 2.7. pip was not installed on my Mac.

Eventually I got pip installed by using easy_install tool:

sudo easy_install install pip

After that, I could install pyperclip, a package for using clipboard copy and paste features.

sudo pip install pyperclip

However, the import of pyperclip in python shell and script still does not working!

Version Issue

I could not find out any solution from web easily. It seems that all the solutions in the top search list I found from web are old ones, ie for python 2.*. From one place I read a hint about the issue: different versions of python on Mac. It is true that python came with Mac OS is version 2.3, 2.5 & 2.7. The newest version, for the time being, I installed on Mac is python 3.7.

Further investigation, I found that the tool of pip is actually used to install packages for python 2.7. The pip is located at the path of:

/Library//Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip

The tool I need to install pyperclip is actually pip3! This one is at the path of:

/Library//Frameworks/Python.framework/Versions/3.7/bin/pip3

When I sorted it out, it is so easy to install third party packages for python 3.7. For example:

sudo pip3 install pyperclip

For my interest, I further found out the locations of packages for different versions. For python 2.7, the location of package is at:

  1. find /Library/Python/ -name pyperclip
  2. /Library/Python//2.7/site-packages/pyperclip

and for python version 3.7 it is at:

  1. find /Library/Frameworks/Python.framework/ -name pyperclip
  2. /Library/Frameworks/Python.framework//Versions/3.7/lib/python3.7/site-packages/pyperclip


Python also in Different Versions!


One more interesting thing I found out is that, on my Mac, the name of python executable also has different ones! For the python came with Mac OS is actually version 2.5, and the executable name is python:

/Library//Frameworks/Python.framework/Versions/2.5/bin/python

and for the newest version, 3.7 I installed, the name is python3:

/Library/Frameworks/Python.framework//Versions/3.7/bin/python3


From the terminal, the command of python will bring up to version 2.5:

  1. ...$ python
  2. Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
  3. [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
  4. Type "help", "copyright", "credits" or "license" for more information
  5. >>>

or type option -h to get all available options. -V is for getting python version:

  1. python -h
  2. ...
  3. Python 2.5


In order to run python3 from terminal, the PATH has to be changed: adding python3's path. This is the result of my updated PATH:

  1. echo $PATH
  2. /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:

The PATH is set in the file of .bash_profile:

    more ~/.bash_profile

  1. # Setting PATH for Python 3.7
  2. # The original version is saved in .bash_profile.pysave
  3. PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}:/Users/dchu/Programming/py"
  4. export PATH

Even with this change, you have to type the correct python executable name, ie python3, to enter python shell:

  1. python3
  2. Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
  3. [Clang 6.0 (clang-600.0.57)] on darwin
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import pyperclip
  6. >>>

Finally, I could do import pyperclip in python shell and scripts without any errors.

By the way, here are the options to get all the commands, options, and the version for the tool of pip or pip3:

  1. pip -h
  2. ...
  3. pip -V
  4. pip 18.0 from /Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip (python 2.7)
  5. pip3 -V
  6. pip 10.0.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)


References



Read More...

Monday, May 07, 2012

Use VIM for Objective-C Syntax Highlighting

In my previous blog, I mentioned about using VIM for code editing. One of VIM feature is to provide syntax highlighting. I think that I added object-c syntax highlighting to my VIM long time ago. However, when I tried to edit a .m file, I did not see the colorful syntax.

I checked my ~/.vim folder and I found some plugin files for cocoa there. I must have done the objective-c syntax stuff. The file type for objective-c is not .m or ft=m. The file type should be something else.

Quickly I found the file type. Here is the command to enable syntax for objective-c file content:

:set ft=objc

Reference




Read More...

Wednesday, April 30, 2008

Python

Recently, I started to learn Python script language through Python Tutorial. It is a very interesting programming language. It is very powerful and unique. The reason I have interest in Python is that I read an article about Google's open source project. It mentioned that Google promotes this open source language. After reading several sections in the tutorial, I really like Python.

Another reason is that Apple's OS installs Python by default. It is a script language used as Apple's script language. I am going to learn and write some scripts. Python may be a good choice.

I just read a section about Class in Python. Within a Class, there are two types of members, one for attributes and another for methods. You don't need to define all the methods in a class. You can add or delete attributes to a class dynamically. It is really convenient. However, if you defines a attribute, you cannot dynamically to delete it.

Regarding del statement, you can use it to delete a definition of a class as well. If you create an instance of a class and then delete the class, the instance still works For example, you can still call the instance's attributes or methods. I think that the instance is created in memory like an object. In Python, every thing is object.

Read More...