help me debug my "word capitalizer" script

2012-08-21 Thread Santosh Kumar
Here is the script I am using:

from os import linesep
from string import punctuation
from sys import argv

script, givenfile = argv

with open(givenfile) as file:
# List to store the capitalised lines.
lines = []
for line in file:
# Split words by spaces.
words = line.split(' ')
for i, word in enumerate(words):
if len(word.strip(punctuation)) > 3:
# Capitalise and replace words longer than 3 (without
punctuation)
words[i] = word.capitalize()
# Join the capitalised words with spaces.
lines.append(' '.join(words))
# Join the capitalised lines by the line separator
capitalised = linesep.join(lines)
# Optionally, write the capitalised words back to the file.

print(capitalised)


Purpose of the script:
To capitalize the first letter of any word in a given file, leaving
words which have 3 or less letters.

Bugs:
I know it has many bugs or/and it can be improved by cutting down the
code, but my current focus is to fix this bug:
  1. When I pass it any file, it does it stuff but inserts a blank
line everytime it processes a new line. (Please notice that I don't
want the output in an another file, I want it on screen).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Santosh Kumar
OK! The bug one fixed. Thanks to Andreas Perstinger.

Let's move to Bug #2:
 2. How do I escape the words that are already in uppercase? For example:

The input file has this:
NASA

The script changes this to:
Nasa

Is it possible to make this script look at a word, see if its first
character is capitalized, if capitalized then skip that word. If not
do the processing? I don't wan to use regex? Do I need it?
-- 
http://mail.python.org/mailman/listinfo/python-list


want to show list of available options and arguments in my command line utility

2012-09-15 Thread Santosh Kumar
I have a script that takes an which basically takes a command line
argument and prints after processing. If I don't give the argument to
the script, it gives me a ValueError:

ValueError: need more than 1 value to unpack

I was trying to utilizing this space, if there is no argument I want it
to show:
usages: myscriptpy [option] [argument]

options:
--help  print this help message and exit

Nothing more than that. I was looking on the argparse module, it can
do the stuffs I want, but
I don't to rewrite and mess up my current script. What should I do?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any algorithm to preserve whitespaces?

2013-01-23 Thread Santosh Kumar
I am in a problem.

words = line.split(' ')

preserve whitespaces but the problem is it writes an additional line
after every line.


And:

words = line.split()

works as I expect (does not adds addition line after every line) but
does not preserves whitespaces.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any algorithm to preserve whitespaces?

2013-01-23 Thread Santosh Kumar
Yes, Peter got it right.

Now, how can I replace:

script, givenfile = argv

with something better that takes argv[1] as input file as well as
reads input from stdin.

By input from stdin, I mean that currently when I do `cat foo.txt |
capitalizr` it throws a ValueError error:

Traceback (most recent call last):
  File "/home/santosh/bin/capitalizr", line 16, in 
script, givenfile = argv
ValueError: need more than 1 value to unpack

I want both input methods.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any algorithm to preserve whitespaces?

2013-01-24 Thread Santosh Kumar
On 1/24/13, Peter Otten <__pete...@web.de> wrote:
> Santosh Kumar wrote:
>
>> Yes, Peter got it right.
>>
>> Now, how can I replace:
>>
>> script, givenfile = argv
>>
>> with something better that takes argv[1] as input file as well as
>> reads input from stdin.
>>
>> By input from stdin, I mean that currently when I do `cat foo.txt |
>> capitalizr` it throws a ValueError error:
>>
>> Traceback (most recent call last):
>>   File "/home/santosh/bin/capitalizr", line 16, in 
>> script, givenfile = argv
>> ValueError: need more than 1 value to unpack
>>
>> I want both input methods.
>
> You can use argparse and its FileType:
>
> import argparse
> import sys
>
> parser = argparse.ArgumentParser()
> parser.add_argument("infile", type=argparse.FileType("r"), nargs="?",
> default=sys.stdin)
> args = parser.parse_args()
>
> for line in args.infile:
> print line.strip().title() # replace with your code
>

This works file when I do `script.py inputfile.txt`; capitalizes as
expected. But it work unexpected if I do `cat inputfile.txt |
script.py`; leaves the first word of each line and then capitalizes
remaining.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any algorithm to preserve whitespaces?

2013-01-24 Thread Santosh Kumar
But I can; see: http://pastebin.com/ZGGeZ71r

On 1/24/13, Peter Otten <__pete...@web.de> wrote:
> Santosh Kumar wrote:
>
>> On 1/24/13, Peter Otten <__pete...@web.de> wrote:
>>> Santosh Kumar wrote:
>>>
>>>> Yes, Peter got it right.
>>>>
>>>> Now, how can I replace:
>>>>
>>>> script, givenfile = argv
>>>>
>>>> with something better that takes argv[1] as input file as well as
>>>> reads input from stdin.
>>>>
>>>> By input from stdin, I mean that currently when I do `cat foo.txt |
>>>> capitalizr` it throws a ValueError error:
>>>>
>>>> Traceback (most recent call last):
>>>>   File "/home/santosh/bin/capitalizr", line 16, in 
>>>> script, givenfile = argv
>>>> ValueError: need more than 1 value to unpack
>>>>
>>>> I want both input methods.
>>>
>>> You can use argparse and its FileType:
>>>
>>> import argparse
>>> import sys
>>>
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("infile", type=argparse.FileType("r"), nargs="?",
>>> default=sys.stdin)
>>> args = parser.parse_args()
>>>
>>> for line in args.infile:
>>> print line.strip().title() # replace with your code
>>>
>>
>> This works file when I do `script.py inputfile.txt`; capitalizes as
>> expected. But it work unexpected if I do `cat inputfile.txt |
>> script.py`; leaves the first word of each line and then capitalizes
>> remaining.
>
> I cannot reproduce that:
>
> $ cat title.py
> #!/usr/bin/env python
> import argparse
> import sys
>
> parser = argparse.ArgumentParser()
> parser.add_argument("infile", type=argparse.FileType("r"), nargs="?",
> default=sys.stdin)
> args = parser.parse_args()
>
> for line in args.infile:
> print line.strip().title() # replace with your code
> $ cat inputfile.txt
> alpha beta
> gamma delta epsilon
> zeta
> $ cat inputfile.txt | ./title.py
> Alpha Beta
> Gamma Delta Epsilon
> Zeta
> $ ./title.py inputfile.txt
> Alpha Beta
> Gamma Delta Epsilon
> Zeta
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


some beginners questions about programming

2012-04-21 Thread Santosh Kumar
Hello Python Developers,

I have a very less experience with programming. I have digged a little bit
of all (I mean *C, **Java, JavaScript, PHP*) at introductory level and now
I have two question about it.

   1. Are *Arrays* and *Lists* same things?
   2. Are *Modules* and *Libraries* same things?

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


Getting started with PyGTK [Receiving Error]

2012-04-28 Thread Santosh Kumar
System Information

Ubuntu 11.10
Python 2.7.2

Problem


I think my Ubuntu has PyGTK and GTK both already installed. But
however when I am importing "gtk" in Python interactive mode then I am
getting the following warning:

(.:4126): Gtk-WARNING **: Unable to locate theme engine in
module_path: "pixmap",

(.:4126): Gtk-WARNING **: Unable to locate theme engine in
module_path: "pixmap",

(.:4126): Gtk-WARNING **: Unable to locate theme engine in
module_path: "pixmap",

(.:4126): Gtk-WARNING **: Unable to locate theme engine in
module_path: "pixmap",

On the other side, importing "PyGTK" works well (i.e. I don't receive
any error). What might be error?
-- 
http://mail.python.org/mailman/listinfo/python-list


How to remember last position and size (geometry) of PyQt application?

2015-11-23 Thread Santosh Kumar
Hello all fellow Python programmers!

I am using PyQt5 (5.5.1) with Python 3.4.0 (64-bit) on Windows 8.1
64-bit. I don't think this much data was needed. :P

I am having trouble restoring the position and size (geometry) of my
very simple PyQt app.

What I read online is that this is the default behavior and we need to
use QSettings to save and retrieve settings from Windows registry,
which is stored in
`\\HKEY_CURRENT_USER\Software\[CompanyName]\[AppName]\`.

Here are some of the links are read:
http://doc.qt.io/qt-5.5/restoring-geometry.html
http://doc.qt.io/qt-5.5/qwidget.html#saveGeometry
http://doc.qt.io/qt-5.5/qsettings.html#restoring-the-state-of-a-gui-application
and the last one:
https://ic3man5.wordpress.com/2013/01/26/save-qt-window-size-and-state-on-closeopen/

I could have followed those tutorials but those tutorials/docs were
written for C++ users.

C++ is not my glass of beer. Should I expect help from you guys? :)

Here is minimal working application:


import sys
from PyQt5.QtWidgets import QApplication, QWidget

class sViewer(QWidget):
"""Main class of sViewer"""
def __init__(self):
super(sViewer, self).__init__()
self.initUI()


def initUI(self):
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
view = sViewer()
sys.exit(app.exec_())
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to remember last position and size (geometry) of PyQt application?

2015-11-23 Thread Santosh Kumar
This question was reasked and answered on StackOverflow:
http://stackoverflow.com/q/33869721/939986
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me debug this script with argparse and if statements

2013-02-21 Thread Santosh Kumar
> To do what you're looking for there, I wouldn't bother with argparse
> at all - I'd just look at sys.argv[1] for the word you're looking for.
> Yes, it'd be a bit strict and simplistic, but by the look of things,
> you don't need sophistication.

You are right, but I think sys.argv is very basic. Before argparse
even I was using it, but it was very hard to manage if user has less
argument, more argument or more argument. I think argparse is the
better thing than sys.argv, and I believe there might be any way to
tackle my (argparse) problem. That is what I am looking here for.

-- 
Twitter  | Github 
-- 
http://mail.python.org/mailman/listinfo/python-list


How to install/uninstall manpages with distutils/setuptools?

2013-04-13 Thread Santosh Kumar
Hey,

I have an app hosted on PyPi, it actually is a small script which is
in bin/ directory of the project. Here is the part of setup.py file of
my app:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys, os, shutil
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

__AUTHOR__ = 'Santosh Kumar'
__AUTHOR_EMAIL__ = 'u...@domain.com'

setup(
name='sampleapp',
version='1.02.02',
author=__AUTHOR__,
author_email=__AUTHOR_EMAIL__,
packages=['sampler'],
scripts=['bin/sampler'],
url='https://github.com/sampleapp/sampleapp',
zip_safe=False,
include_package_data=True,
license=open('LICENSE').read(),
description='A sample application',
long_description=open('README.rst').read()
)

if 'install' in sys.argv:
man_path = '/usr/share/man/man1/'
if os.path.exists(man_path):
print("Installing man pages")
man_page = "doc/sampleapp.1.gz"
shutil.copy2(man_page, man_path)
os.chmod(man_path + 'sampleapp.1.gz', int('444', 8))

When uploaded on PyPi, this app can be installed either by downloading
the archive form https://pypi.python.org/pypi/ and doing python
setup.py install or by easy_install or pip. pip is my favorite because
it supports uninstall option.

I can install this app (script) with pip with no problem (to
/usr/bin/). But I can't install the manpage to /usr/share/man/man1/.
That is why I created installation of manpages in my setup.py.

So with the installation of manpages my installation is complete. But
the problem is I can't uninstall the manpages with `pip uninstall
sampleapp`, that will only uninstall the script. So my final question
is there any patch to make distutils install and uninstall man pages?

Please don't tell me about any other packages, I want to stick with
Python's own http://guide.python-distribute.org/
-- 
http://mail.python.org/mailman/listinfo/python-list