Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, April 16, 2014

Good BitBucket Python API and CLI Packages

Since start using GIT as version control system, I use put all my
repos on BitBucket. I love the unlimited private repos from them. But
no command line tools for it is really painful for Linux users. Then I
start looking for good Python API for it to write simple script to
create/clone/list repos. But my Python knowledge make it a hard task
for me to do a good script.

Today just before I try to add "Description" to create repo, I found
the API "python-bitbucket-api" with Python script already. Try it out,
it is really good. Just some small issues like install and argument
parsing. Now everything works fine for me. Hope new features will
coming from original author(s).

If anyone is using BitBucket and looking for a command line tool for
it, you can give it a try: hg clone
https://enchanter@bitbucket.org/enchanter/python-bitbucket-api


Sunday, April 1, 2012

Python File Read operations

Open and read file from within the Python program as a single string using 'read'



import sys
import os
import string

def file_read(file):
      rfile = file.read()
      print rfile
      file = file_read(open('readme.txt', 'r'))

We can also use 
Readlines - as list of strings
Readline - reads only a single line

Read file from command line
Command line arguments lists as string with ‘sys.argv’
import sys
import os
import string

def file_read(file):
      rfile = file.read()
      print rfile
                
if len(sys.argv) == 1:
   file_read(sys.stdin)
else:
   file_read(open(sys.argv[1], 'r'))




Monday, December 21, 2009

Python Study Notes: Exit

How to exit from runing program:
import sys;

sys.exit();


Tuesday, April 21, 2009

Python Study -- Get Environment


os.getenv(key, default=value)

Get the "key" environment variable, if it is not defined, use the default value as the result.

#!/usr/bin/env python
import osimport sys
VT_ROOT = os.getenv("VDE_TOOL_DIR", default = "/opt/vde/services/instances/vt2124")VT_CMD = VT_ROOT + "/bin/vde_tool"CT_CMD = VT_CMD + " ct "
print CT_CMDos.system

Thursday, December 4, 2008

Dealing with Command Line Options in Python

I just wrote a bit of Python to generate some reports from the contents of a database. The one program that was more than just "display the data" was the one to print the transaction log. It included some sub-totals for various fields and paginated output.

The one task remaining was to give it some options. That is, to pass it some criteria that would modify the report. Specifically, I wanted a start and end date and the ability to change the sort order from the default which was transaction date.

Before just jumping into the brute force way of dealing with the command line I decided to see if there was anything interesting in the Python libraries that might turn this into something a bit more pleasant. Well, I found good news in the form of the optparse module. From O'Reilly's Python in a Nutshell, I found the following description:

The optparse module offers rich, powerful ways to parse the command-line options that the user passed upon starting your programs.

Sounds good. My next check was on python.org with equally positive information. So, I decided to give it a try. Here is the short story (followed by a bit of code).

  • First you instantiate the class OptionParser.
  • For each option you want to use, you use the add_option method where you can specify the options (both short and long ones are supported) and tell optparse what to do.
  • Once you are all set up, you use the parse_args method to do all the dirtywork. You then can just check for the existence of the options by checking for an attribute.
opts = OptionParser()
opts.add_option("--start", "-s", help="in format yyyy-mm-dd")
opts.add_option("--end", "-e", help="in format yyyy-mm-dd")
opts.add_option("--order", "-o", type="choice", choices=["tdate", "cname_id",
"account_id", "project_id", "id"], help="tdate|cname_id|account_id|project_id|id")
options, arguments = opts.parse_args()

if options.start:
...
if options.order:
...

That should be enough code. Each of the opts.add_option( lines adds an option to the list. For example, the first one says that it matches either --start or -s. The implied action is to store the argument associated with it. The help argument is used to specify a help message that is printed out either in case of an error calling the command or if the command is invoked with the -h option.

The third opts.add_option( line is a bit different. It defines the type of option as a choice and then includes a list of valid values. Entering anything other than one of these values results in an error message including the usage display.

The call to opts.parse_args() does the dirtywork for you. For example, if I had invoked the program with -s 2008-11-11 then options.start would be set to 2008-11-11.

All in all, I found optparse easy to understand and enjoyed separating all the error checking from using the arguments. There are lots more things it can do for you but this should get you started. It is now in my list of "Python tricks". __________________________

Phil Hughes