writing command-line options into file

2011-03-14 Thread hiral
For following example, how to write command-line option OR metavar
into file...

parser.add_option("-opt1", metavar="MY_OPTION1", default=123)
parser.add_option("-opt2", metavar="YOUR_OPTION2" ,default= "abc")
parser.add_option('-opt3", metavar="FLAG", default=True)

do we have any facility to write command-line option OR 'metavar' into
file as follow...

output_file:
MY_OPTION1 123
YOUR_OPTION2 abc
FLAG True

OR
output_file:
opt1 123
opt2 abc
opt3 True

Any idea?

Please note that in actual application there are about 80 to 90
command-line options.

Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


result of os.times() is different with 'time' command Options

2010-03-12 Thread hiral
Hi,

Python version: 2.6

Script:
def pt(start_time, end_time):
def ptime(time, time_str):
min, sec = divmod(time, 60)
hr, min = divmod(min, 60)
stmt = time_str + '\t'
if hr:
stmt += str(hr) + 'h'
stmt += str(min) + 'm' + str(sec) + 's'
print stmt

if start_time and end_time:
real_time  = end_time[4] - start_time[4]
ptime(real_time, "real")
user_time  = end_time[0] - start_time[0]
ptime(user_time, "user")
sys_time   = end_time[1] - start_time[1]
ptime(sys_time, "sys")

import os, subprocess
cmd = ['ls']
print cmd
t1 = os.times()
subprocess.call(cmd)
t2 = os.times()
pt(t1, t2)
print ".end"


Output:
real0.0m0.010002421s
user0.0m0.0s
sys 0.0m0.0s


Command:
$ time ls

Output:
real0m0.007s
user0m0.000s
sys 0m0.000s


Is this the intended behaviour?

As per the link  it was fixed in
python 2.5.

Can anybody help.

Thank you.




-- 
http://mail.python.org/mailman/listinfo/python-list


python module/utility equivalent to 'time' (linux) and/or 'ntimer'(Windows)

2010-03-12 Thread hiral
Hi,

Is there any python module/utility available which would report the
time same as 'time' command in linux and/or report time same as
'ntimer' utility in Windows.

Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


to create variable from dict

2010-03-12 Thread hiral
Hi,

Is there any way to create variables which name matches with dict key?

For example:
dict1 = {"abc":'1", "def":"2"}

Now I am looking to have variable name abc and it's value be '1' etc.

Pl. suggest.

Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: result of os.times() is different with 'time' command Options

2010-03-14 Thread hiral
On Mar 15, 7:14 am, Tim Roberts  wrote:
> hiral wrote:
> >...
> >Output:
> >real    0.0m0.010002421s
> >user    0.0m0.0s
> >sys     0.0m0.0s
>
> >Command:
> >$ time ls
>
> >Output:
> >real    0m0.007s
> >user    0m0.000s
> >sys     0m0.000s
>
> >Is this the intended behaviour?
>
> What is it that you are wondering about?  The formatting difference is due
> to your code.  The difference between 10 milliseconds and 7 milliseconds
> could be due to any number of things.  First, you have the overhead of
> Python involved in your measurements.  Second, you have the variability of
> memory caching and disk caching.  Your Python code causes /bin/ls to be
> loaded into memory, and it's probably still in a file cache when you run
> the second command.
>
> You can't really do an analysis like this with a task that only takes a few
> milliseconds.  There are too many variables.
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.- Hide quoted text -
>
> - Show quoted text -

Thanks for your explanation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: to create variable from dict

2010-03-15 Thread hiral
On Mar 12, 8:02 pm, Jean-Michel Pichavant 
wrote:
> Luis M. González wrote:
> > On Mar 12, 10:59 am,hiral wrote:
>
> >> Hi,
>
> >> Is there any way to create variables which name matches with dict key?
>
> >> For example:
> >> dict1 = {"abc":'1", "def":"2"}
>
> >> Now I am looking to have variable name abc and it's value be '1' etc.
>
> >> Pl. suggest.
>
> >> Thank you.
>
> > Check out this thread (very recent):
> >http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> > Short answer: you can update globals() with a dictionary, as follows:
>
> > globals().update( dict1 )
>
> > Then you'll have each key-value pair as variables in the global
> > namespace.
> > The question is: should you do it?
>
> > Luis
>
> The answer is known: no, he should not do it :o)
>
> JM- Hide quoted text -
>
> - Show quoted text -

Thanks for your explanations.
-- 
http://mail.python.org/mailman/listinfo/python-list


output from popen

2010-04-05 Thread hiral
Hi,
I am trying following script...


Is there any module/utility like 'rsync' in python

2010-04-08 Thread hiral
Hi,

Is there any module/utility like 'rsync' in python.

Thank you in advance.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


how to import subprocess into my 'subprocess.py' file

2010-05-05 Thread hiral
Hi,

I am doing following in my 'subprocess.py' file...

  1 from __future__ import absolute_import
  2 from subprocess import *
  3 from subprocess import call as myCall
  4 from subprocess import Popen as myPopen
  5
  6 def getProperCmd(cmd):
  7 cmd += 'time' # this is just an example; in fact I am doing
lots of processing on cmd
  8 return cmd
  9
 10
 11 def call(cmd, **kwargs):
 12 return myCall(getProperCmd(cmd), **kwargs)
 13
 14 def Popen(cmd, **kwargs):
 15 return myPopen(getProperCmd(cmd), **kwargs)

When running this it gives following error...

Traceback (most recent call last):
  File "subprocess.py", line 2, in 
from subprocess import *
  File "subprocess.py", line 3, in 
from subprocess import call as myCall
ImportError: cannot import name call


So how can I create a python file (with the same name as standard
module name) with custom methods?

Thank you in advance.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


optparse: best way

2010-06-08 Thread hiral
Hi,

I am using optparser to do following...

Command syntax:
myscript -o[exension] other_arguments
where; extension can be 'exe', 'txt', 'pdf', 'ppt' etc.


Now to parse this, I am doing following...

parser.add_option("-oexe', dest=exe_file...)
parser.add_option("-otxt', dest=txt_file...)
parser.add_option("-opdf', dest=pdf_file...)
parser.add_option("-oppt', dest=ppt_file...)

The above way is the most simple way to parser options.
Can you please suggest any other best way / optimized way to parse
these kind of options.

Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: optparse: best way

2010-06-09 Thread hiral
On Jun 8, 3:03 pm, Jean-Michel Pichavant 
wrote:
> hiralwrote:
> > Hi,
>
> > I am using optparser to do following...
>
> > Command syntax:
> > myscript -o[exension] other_arguments
> >     where; extension can be 'exe', 'txt', 'pdf', 'ppt' etc.
>
> > Now to parse this, I am doing following...
>
> > parser.add_option("-oexe', dest=exe_file...)
> > parser.add_option("-otxt', dest=txt_file...)
> > parser.add_option("-opdf', dest=pdf_file...)
> > parser.add_option("-oppt', dest=ppt_file...)
>
> > The above way is the most simple way to parser options.
> > Can you please suggest any other best way / optimized way to parse
> > these kind of options.
>
> > Thank you in advance.
>
> Here's a solution:
>
> import optparse
>
> class Process:
>     PREFIX = 'dispatch_'
>     @staticmethod
>     def undef():
>         print 'unsupported file type'
>     @staticmethod
>     def dispatch_exe():
>         print 'Hello exe file !'
>
> def dispatchFileType(option, opt, value, parser):
>     """Called by the parser, -o option."""
>     # call the corresponding method in the process method
>     getattr(Process, Process.PREFIX + value, Process.undef)()
>
> parser = optparse.OptionParser()
> parser.add_option("-o", "--output-fileType", type="string",
> action="callback", callback=dispatchFileType)
>
> options, args = parser.parse_args()
>
> Cheers,
>
> JM- Hide quoted text -
>
> - Show quoted text -

Hi JM,

Here it gives...
$ python above_script.py -oexe abc
Hello exe file !
{'output_fileType': None} # print options
['abc'] # print args

In my case I require to have 'options' to consume 'abc' like...
{'output_fileType': 'abc'}

Thank you.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: optparse: best way

2010-06-09 Thread hiral
On Jun 8, 4:30 pm, Hrvoje Niksic  wrote:
> Thomas Jollans  writes:
> > UNIX and GNU recommendations. I've never actually heard of optparser,
> > but I'd expect it to have the usual limitations:
>
> Hiralprobably meant to write "optparse", which supports GNU-style
> options in a fairly standard and straightforward way.  Which includes
> that defining a "-o"/"--output-format" option that takes an argument
> allows you to write one of "-o exe", "-oexe", "--output-format=exe", or
> "--output-format exe".
>
> My recommendation is to use -o, and -oexe will work just fine.

Thank you all :) for your kind suggestins.
All your suggestions are fine and valid, which suggest to have option
'-o' and take its value 'exe ppt pdf txt' etc.

Yes, I am planning to use GNU style options...
One advantage with this that user can pass a.txt but can specify it as
'-oexe' and it would get executed as 'process_exe()'.

So to say we don't have support for '-o value' in python;
but there are ways to acheive this.
It seems as of now I should specify them as seperate options like...
> parser.add_option("-o', dest=exe_file...)
> parser.add_option("-oexe', dest=exe_file...)
> parser.add_option("-otxt', dest=txt_file...)
> parser.add_option("-opdf', dest=pdf_file...)
> parser.add_option("-oppt', dest=ppt_file...)

Thank you in advance.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replace in large text file ?

2010-06-09 Thread hiral
On Jun 6, 7:27 am, Steve  wrote:
> On 5 June, 08:53, Steve  wrote:
>
> > I am new to Python and am wanting  to replace characters in a very
> > large text file.6 GB
> > In plain language what I wish to do is:
>
> > Remove all comma's
> > Replace all @ with comma's
> > Save as a new file.
>
> > Any of you clever people know the best way to do this..idiot guide
> > please.
>
> > Thanks
>
> > Steve
>
> Many thanks for your suggestions.
>
> sed -i 's/Hello/hello/g' file
>
> Run twice on the CL..with the hello's changed for my needs did it in a
> few minutes ,
>
> Again thanks
>
> Steve

Hi Steve,

You can do...

sed "s/,//g"  | sed "s/@/,/g" > 

Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


subprocess.Popen()/call() and appending file

2010-06-14 Thread hiral
Hi,

Do we have any facility to append file from Popen()/call(); see below
example...

1 import subprocess
2 f=open('log', 'w')
3 ...# writing some log-into into log file
4 p = subprocess.Popen(cmd, stdout=f, stderr=f) # (Q)
5 ...# do remaining stuff

Q: At line# 4, the output of the 'cmd' will wipe-out everything in
'log' file. So to avoid this do we have any mechanism to append into
the existing file from subprocess.

Currently I am doing following...
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o, e = p.communicate()
print >> log, o
print >> log, e

Pleaese let me know if there is any better mechanism.

Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


subprocess.Popen()/call() and appending file

2010-06-14 Thread hiral
Hi,

Do we have any facility to append file from Popen()/call(); see below
example...

1 import subprocess
2 f=open('log', 'w')
3 ...# writing some log-into into log file
4 p = subprocess.Popen(cmd, stdout=f, stderr=f) # (Q)
5 ...# do remaining stuff

Q: At line# 4, the output of the 'cmd' will wipe-out everything in
'log' file. So to avoid this do we have any mechanism to append into
the existing file from subprocess.

Currently I am doing following...
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o, e = p.communicate()
print >> log, o
print >> log, e

Pleaese let me know if there is any better mechanism.

Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen()/call() and appending file

2010-06-15 Thread hiral
On Jun 14, 7:11 pm, Kushal Kumaran 
wrote:
> On Mon, Jun 14, 2010 at 7:01 PM,hiral wrote:
> > Hi,
>
> > Do we have any facility to append file from Popen()/call(); see below
> > example...
>
> > 1 import subprocess
> > 2 f=open('log', 'w')
> > 3 ...# writing some log-into into log file
> > 4 p = subprocess.Popen(cmd, stdout=f, stderr=f) # (Q)
> > 5 ...# do remaining stuff
>
> > Q: At line# 4, the output of the 'cmd' will wipe-out everything in
> > 'log' file. So to avoid this do we have any mechanism to append into
> > the existing file from subprocess.
>
> > Currently I am doing following...
> > p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
> > stderr=subprocess.PIPE)
> > o, e = p.communicate()
> > print >> log, o
> > print >> log, e
>
> > Pleaese let me know if there is any better mechanism.
>
> It is not actually wiping out the old contents.  You might be running
> into buffering issues.  Explicitly flushing should help when switching
> between the high level IO functions provided by file objects and the
> low level IO that subprocess uses.  Do a f.flush() before your
> subprocess.Popen call.
>
> --
> regards,
> kushal- Hide quoted text -
>
> - Show quoted text -

Thanx it works fine.
-- 
http://mail.python.org/mailman/listinfo/python-list


optparser: how to register callback to display binary's help

2010-10-13 Thread hiral
Hi,

I want to display help message of python script and then display help
message from the binary file (which also supports -h option):

Assumptions:
1) 'mybinary' - is linux executable file which supports '-h' and on '-
h' option it displays the help message
2) myscript.py - when passing '-h' option want to call 'mybinary -h'
and display help messages of both script and binary.

Code:

from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-e", "--execute", dest="file",
  help="Execute binary", metavar="FILE")
(options, args) = parser.parse_args()
if options.file:
subprocess.call(options.file)


Requirement:
$ python myscript.py -h



Thank you in advance.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: optparser: how to register callback to display binary's help

2010-10-15 Thread hiral
On Oct 13, 6:03 pm, Jean-Michel Pichavant 
wrote:
> hiral wrote:
> > Hi,
>
> > I want to display help message of python script and then display help
> > message from the binary file (which also supports -h option):
>
> > Assumptions:
> > 1) 'mybinary' - is linux executable file which supports '-h' and on '-
> > h' option it displays the help message
> > 2) myscript.py - when passing '-h' option want to call 'mybinary -h'
> > and display help messages of both script and binary.
>
> > Code:
> > 
> > from optparse import OptionParser
> > [...]
> > parser = OptionParser()
> > parser.add_option("-e", "--execute", dest="file",
> >                   help="Execute binary", metavar="FILE")
> > (options, args) = parser.parse_args()
> > if options.file:
> >     subprocess.call(options.file)
>
> > Requirement:
> > $ python myscript.py -h
> > 
> > 
>
> > Thank you in advance.
> > -Hiral
>
> Hi,
>
> Try something like
>
> def myHelp(option, opt, value, parser):
>     parser.print_help()
>     if hasattr(parser.values, 'file') and parser.values.file:
>         proc = subprocess.Popen([parser.values.file, '-h'],
> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>         print proc.stdout.read()
>         print proc.stderr.read()
>
> parser.remove_option('-h')
> parser.add_option("-h", "--help",
>                   help="display the help message", action='callback',
> callback = myHelp)
>
> Jean-Michel- Hide quoted text -
>
> - Show quoted text -

Hi Jean-Michel,

It helped !!!

Thank you.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


How to display unicode char in Windows

2010-10-15 Thread hiral
Hi,
I tried...


# coding: latin-1
print "**"
oo = "ö"
print "char=<%s>" % oo
print "**"


but it is not printing "ö" char; any idea?

Thank you.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to display unicode char in Windows

2010-10-16 Thread hiral
On Oct 15, 5:24 pm, "Michel Claveau -
MVP" wrote:
> Hi!
>
> 1) the good syntax is:
>     # -*- coding: latin-1 -*-
>     print "**"
>     oo = "ö"
>     print "char=<%s>" % oo
>     print "**"
>
> 2) in the console (commandLine), use this command:  CHCP 1252 {Enter}
> (before run your script)
>
> @-salutations
> --
> Michel Claveau

Thanks that worked.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to display unicode char in Windows

2010-10-16 Thread hiral
On Oct 15, 11:06 pm, John Nagle  wrote:
> On 10/15/2010 4:57 AM, hiral wrote:
>
> > Hi,
> > I tried...
>
> > 
> > # coding: latin-1
> > print "**"
> > oo = "ö"
> > print "char=<%s>" % oo
> > print "******"
> > 
>
> > but it is not printing "ö" char; any idea?
>
> > Thank you.
> > -Hiral
>
>     Unicode output to Windows consoles has been broken since
> 2007.  See
>
>    http://bugs.python.org/issue1602
>
>     Surprisingly, it actually worked with Python 2.5 and
> Windows 2000, if you changed the Windows console encoding
> to "chcp 65001". If you try that with Python 2.6 and
> Windows 7, you get "LookupError: unknown encoding: cp65001",
> because "cp65001" isn't in Python's encoding tables.
>
>                                 John Nagle

Thanks for explaining actual bug.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to display unicode char in Windows

2010-10-16 Thread hiral
On Oct 15, 11:38 pm, "Mark Tolonen"  wrote:
> "hiral"  wrote in message
>
> news:90b62600-a0a4-47d5-bb6f-a3ae14cf6...@9g2000prn.googlegroups.com...
>
> > Hi,
> > I tried...
>
> > 
> > # coding: latin-1
> > print "**"
> > oo = "ö"
> > print "char=<%s>" % oo
> > print "**"
> > 
>
> > but it is not printing "ö" char; any idea?
>
> 1) Make sure you save your source in the encoding declared, or change the
> encoding to match.  Any encoding that supports ö will work.
> 2) Use Unicode strings.
> 3) Only print characters your terminal supports, or you will get a
> UnicodeEncoding error.
>
> Below works in PythonWin GUI (utf-8 encoding) and US-Windows console (cp437
> encoding).
>
> 
> # coding: latin-1
> print u"**"
> oo = u"ö"
> print u"char=<%s>" % oo
> print u"******"
> 
>
> Coding line declares *source* encoding, so Python can *decode* characters
> contained in the source.
>
> "print" will *encode* characters to the terminal encoding, if known.
>
> -Mark

Thanks Mark.

Thank you.
-Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list


pipe using python

2010-10-16 Thread hiral
Hi,

Like we 'named pipes', how we can achieve comminication between more
than two processes.
And how it can be scaled to remote machines?
Any idea?

Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pipe using python

2010-10-16 Thread hiral
I am planning to use python module if available.
-- 
http://mail.python.org/mailman/listinfo/python-list


Seeking help with urllib2 response reading

2010-08-02 Thread Hiral Shah
Hi All,

I am facing problem reading response from urllib2 request. i.e. with read()
method.

I am trying to access XMLAPI using python. I am getting weird behavior from
server, when reading response.Code for calling XMLAPI is as follows:

  headers = {'Content-type': 'application/x-www-form-urlencoded'}
  request = urllib2.Request(siteXMLAPIURL,xmlStr,headers)
  response = urllib2.urlopen(request)
  print response.read()

In above code:
xmlStr: is String containing XML request
siteXMLAPIURL: URL of the site to send request. It is in form "
https://xxx.xxx.com/XXXService/XXXService

Problem Description:

I am calling URL for creating product by passing methodname(CreateProduct),
productId, productCreateDate, productCreateTime etc in XML form.  Call to
XMLAPI always succeed and product is created on site. But when I call
.read() method to print response, then "productCreateTimes" gets changed on
server !!

For example,
Case 1: Not using .read() method. (i.e. commenting print line in above code)
Calling API by passing productCreateTime = 08/02/10 08:00:00,and then
product is being created with time 08/02/10 08:00:00. i.e. everything is
fine here

Now,
Case 2: Using read() method (i.e. having print line is code)
Calling API by passing productCreateTime = 08/02/10 08:00:00 and then
product is being created with time 08/02/10 09:00:00. Here, server is adding
one more hour to productCrateTime. Which is really strange for me!!

FYI: If I access same API with java, everything is fine all time.

I am unable to understand why read() method is changing server behavior!!
Can anyone please guide me in same?

Thank you so much for your time. Waiting for your reply.

Thanks,
Hiral
-- 
http://mail.python.org/mailman/listinfo/python-list