In Python and Windows environment how to supress certain key press and send some other key event for it

2017-04-02 Thread Krishnan Shankar
Hi All,

I was trying to build a VIM like shortcuts in windows. For example,

IF i press CAPSLOCK & h: It should "{Left}" move one to left. If CAPSLOCK &
b: It should "{Ctrl Down}{Left}{Ctrl Up}" move one word left etc.

I was successful in sending the key event. I used libraries like,

1. pynput
2. keyboard

for the same.

But i was unable to supress the Initial key event (ex: Caplock & h)

I tried using the keyboard to listen for key press events and supress them
as below.

>
import keyboard

def move_one_left():
"""
Wrapper for CAPS LOCK + b. The function sends LEFT ARROW Event
:return:
"""
keyboard.send(75)

def move_one_word_left():
"""
Wrapper for CAPS LOCK + b. The function sends LEFT ARROW Event
:return:
"""
keyboard.send('ctrl+left')

def main():
"""
This is the main loop which reads the key pressed.
According to the hotkey registered the function hooked is called.
The corresponding function will be the wrapped combination send back.
For example: CAPS + b is wrapped to Moving to left.
The main loop exits when Ctrl + c is done. So that is not registered.
:return:
"""
try:
# Start of the main loop
# Registering the hotkeys
# CAPS LOCK + H
keyboard.add_hotkey('caps lock+h', move_one_left, suppress=True)
# CAPS LOCK + B
keyboard.add_hotkey('caps lock+b', move_one_word_left,
suppress=True)

while True:
# Read the key pressed
print (keyboard.read_key())
except KeyboardInterrupt:
print("User has exited the program")

if __name__ == "__main__":
main()

<


This is working for sending the event for key press but the initial
keypress is also being send. The supress=True is not working.

Am I doing something wrong or is there any better way to supress the
initial key event and send another key event in place of that.


Regards,
Krishnan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reading csv file

2013-12-16 Thread Krishnan Shankar
Hi Igor

You can use the following way to do this using with operator.

def Read_CSV_File(filename):
  with open(filename, r) as csvfile:
  csvreader = csv.DictReader(csvfile)
  line = 1
  for row in csvreader:
  if line  6:
 reader.next()
 line++
 continue

 # process the CSV

Rest of the things are pretty much straightforward.

Regards,
Krishnan


On Tue, Dec 17, 2013 at 10:50 AM, Igor Korot ikoro...@gmail.com wrote:

 Hi, ALL,
 Is there a better way to do that:

 def Read_CSV_File(filename):
   file = open(filename, r)
   reader = csv.DictReader(file)
   line = 1
   for row in reader:
   if line  6:
  reader.next()
  line++
 # process the CSV

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

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


Accessing the Taskbar icons

2013-11-07 Thread Krishnan Shankar
Hi All,

I am automating an application in windows using python.

After installation i need to check if the applications icon has appeared in
Taskbar or not. If yes i need to right click the application.

I had been using pywinauto for the same but could not get the job done till
now.

I did the following,

app=pywinauto.application.Application()
hand=pywinauto.findwindows.find_windows(class='Shell_TrayWnd', title=u'')

When i use the handler, get the window and do a right click i am able to
click only in the taskbar and not icons. That maybe because i did not
recognise the icon yet.

Can you guide me how to do the same using pywinauto or pywin32?

Regards,
Krishnan
-- 
https://mail.python.org/mailman/listinfo/python-list


Doubt on generators behavior

2013-10-13 Thread Krishnan Shankar
Hi Friends,

I am new to Generators and was learning the same by experimenting. I wrote
a small code which is as below.

 def test_gen(var):
... print The number is, var
... if var % 2 == 0:
... yield var
... else:
... print Number is odd
...


But when i was executing i saw a behavior i could not understand. When i
gave an even number,
1. The generator object was created
2. When i gave next the argument was yielded.
3. In the subsequent next the Stop Iteration was raised.

 res = test_gen(78)
 res.next()
The number is 78
78
 res.next()
Traceback (most recent call last):
  File stdin, line 1, in module
StopIteration

But When i ran with an odd number the result of Number is odd is printed.
But it seems the generator runs again by itself to Stop Iteration.

 res2 = test_gen(77)
 res2.next()
The number is 77
Number is odd
Traceback (most recent call last):
  File stdin, line 1, in module
StopIteration


How did this happen automatically? I am not able to get the execution of a
generator. Can someone please help me in understanding?
Regards,
Krishnan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'int' object is not callable

2013-08-26 Thread Krishnan Shankar
Hi,

The problem is in the second line.

a = time.daylight()

The daylight is not a method in time module. It is clear here,
http://docs.python.org/2/library/time.html

Since it is not a method we cannot call it. It is just a integer variable .
It returns zero if DST timezone is not defined and returns non zero if
defined.

 import time
 a = time.daylight()
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'int' object is not callable
 a = time.daylight
 a
0
 type(time.daylight)
type 'int'

Regards,
Krishnan


On Tue, Aug 27, 2013 at 8:15 AM, autobotprime...@gmail.com wrote:

 dear friends when i try to execute following lines

 import time
 a = time.daylight()
 print(a)


 result is
 TypeError: 'int' object is not callable


 why is this error and how can i solve this problem?
 --
 http://mail.python.org/mailman/listinfo/python-list

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


List getting extended when assigned to itself

2013-08-24 Thread Krishnan Shankar
Hi Python Friends,

I came across an example which is as below,

 var = [1, 12, 123, 1234]
 var
[1, 12, 123, 1234]
 var[:0]
[]
 var[:0] = var
 var
[1, 12, 123, 1234, 1, 12, 123, 1234]


Here in var[:0] = var we are assigning an entire list to the beginning of
itself. So shouldn't it be something like,

[[1, 12, 123, 1234], 1, 12, 123, 1234]

It happens when we do the below,

 var = [1, 12, 123, 1234]
 var[0] = var
 var
[[...], 12, 123, 1234]


Literally var[0] = var and var[:0] = var almost meens the same. But why is
the difference in output? Can anyone explain what happens when
slicing assignment and direct assignment.

Regards,
Krishnan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Verifying Variable value

2013-08-14 Thread Krishnan Shankar
Hi Chandan,

Python has built-in module called pdb which can be used for debugging.
Importing it in the right place will be like setting break point in code
and will change the execution to debugging mode. We can use different
debugging commands ((n)ext, (c)ontinue, (s)tep  etc) to evaluate through
the code. Below is the link to module,

http://docs.python.org/2/library/pdb.html

We can run this code in the following way python -m pdb filename.py to
run it in pdb debugging mode. Else import pdb and set the trace at right
place like below.

For example
=
def method()
  import pdb;pdb.set_trace()   --  import and set_trace()
  a = 20
  b =30
  c =  a + b

method()

While running you will get pdb prompt to debug the code line by line. The
execution will be in user's hands.  Like below,

 c:\users\krishnan\desktop\test.py(3)method()
- a = 20
(Pdb) n
 c:\users\krishnan\desktop\test.py(4)method()
- b =30
(Pdb) n
 c:\users\krishnan\desktop\test.py(5)method()
- c =  a + b
(Pdb) n
--Return--
 c:\users\krishnan\desktop\test.py(5)method()-None
- c =  a + b
(Pdb) c

You can explore the module and find it useful for debugging. I hope this
helps

Regards,
Krishnan


On Wed, Aug 14, 2013 at 3:12 AM, chandan kumar chandan_...@yahoo.co.in
wrote:
Hi ,

Is there a way to validate variable values while debugging any python
code.Run below example  in debugging mode and i would like to know the
value of c (I know print is an option) with any other option other than
printing.
In C# or some other tools we can verify each statement and values. Is there
way to check each statement in python code like in c#.

Ex:
def method()
  a = 20
  b =30
  c =  a + b


Best Regards,
Chanadn

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


Re: Verifying Variable value

2013-08-14 Thread Krishnan Shankar
You can even use logging module in python to validate the variable values.
You can import the module and use any of the following levels in your
program

import logging

 logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO,
logging.DEBUG

For more you can refer to,

http://docs.python.org/2/library/logging.html
http://stackoverflow.com/questions/1623039/python-debugging-tips




On Wed, Aug 14, 2013 at 3:12 AM, chandan kumar chandan_...@yahoo.co.inwrote:

 Hi ,

 Is there a way to validate variable values while debugging any python
 code.Run below example  in debugging mode and i would like to know the
 value of c (I know print is an option) with any other option other than
 printing.
 In C# or some other tools we can verify each statement and values. Is
 there way to check each statement in python code like in c#.

 Ex:
 def method()
   a = 20
   b =30
   c =  a + b


 Best Regards,
 Chanadn

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


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


Re: .split() Qeustion

2013-08-13 Thread Krishnan Shankar
Hi,

How can I use the '.split()' method (am I right in calling it a method?)

The .split() is a method in Python which comes as in built method for
String objects in Python. Any string defined in python will have the
ability to call this function.

 var = 'Hello how r u?'
 dir(var)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']
 var.split()
['Hello', 'how', 'r', 'u?']


writing each comma between words in the pie list in the following code?
Also, is there a way to use .split instead of typing the apostrophes?
Thank you.

import random
pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
print(random.choice(pie))

If you are talking about having predefined list pie with limited elements
like above it is ok to code them straightaway with apostrophes and others
will know that it is a predefined list.

Suppose if the elements in list come as a line in a file or is a string, it
will be better to use split() method and form a list. I hope Gary has
provided the example for the same.

pie = 'keylime peach apple cherry pecan'.split()

I hope this clarifies your doubt.

Regards,
Krishnan



On Tue, Aug 13, 2013 at 9:51 PM, eschneide...@comcast.net wrote:

 How can I use the '.split()' method (am I right in calling it a method?)
 without instead of writing each comma between words in the pie list in the
 following code? Also, is there a way to use .split instead of typing the
 apostrophes? Thank you.

 import random
 pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
 print(random.choice(pie))

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

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


Introduction to my fellow Python Friends

2013-08-11 Thread Krishnan Shankar
Hi Friends,

I would like to introduce myself.

I am Krishnan from Chennai, India. I am using python for 2 years for Test
Automation. I am fascinated by the language and its capabilities. I am
willing to move into Python development and I am doing the best i can to
learn the language completely and contribute to open source.

I figured out that the best way is to talk to the experts and so i
subscribed to this mailing list. It will be cool if anybody can help me out
by telling the etiquette of this mailing list, like

1. How to acknowledge a reply? Should i put a one to one mail or send it to
the mailing list itself?
2. How can i see or get a question asked by someone else? (So that i can
reply for that with my best possible knowledge. I currently get as Python
mail Digest)
3. How can i use this mailing list in the best possible way?

I hope to have a wonderful time with Python here. I hope i am not wasting
your time. Sorry for the inconvenience if i am.

Regards,
Krishnan
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Basic Doubt

2013-08-10 Thread Krishnan Shankar
Hi Fellow Python Friends,

I am new to Python and recently subscribed to the mailing list.I have a
doubt regarding the basics of Python. Please help me in understanding the
below concept.

So doubt is on variables and their contained value.

Why does in the below example from Interpreter exploration value of c take
pre existing memory location.

 a=10
 id(a)
21665504
 b=a
 id(b)
21665504
 c=10
 id(c)
21665504

I am actually assigning new value to c. But from the value of id() all
three variables take same location. With variables a and b it is ok. But
why c taking the same location?

Regards,
Krishnan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Basic Doubt

2013-08-10 Thread Krishnan Shankar
Thanks Tim,

This takes me to one more question.

'is' operator is used to compare objects and it should not be used to
compare data.

So can it be compared with 'False'.

i.e. Is this code possible

if a is False:
print 'Yes'
if b is False:
print 'No'

Because i recommended this should not be done. But my colleagues say it is
correct.

Regards,
Krishnan


On Sat, Aug 10, 2013 at 10:10 PM, Tim Chase
python.l...@tim.thechases.comwrote:

 On 2013-08-10 21:03, Krishnan Shankar wrote:
   a=10
   id(a)
  21665504
   b=a
   id(b)
  21665504
   c=10
   id(c)
  21665504
 
  I am actually assigning new value to c. But from the value of id()
  all three variables take same location. With variables a and b it
  is ok. But why c taking the same location?

 As an internal optimization, CPython caches small integer values

a = 256
b = 256
a is b
   True
a = 257
b = 257
a is b
   False

 Because it's an internal implementation detail, you shouldn't count
 on this behavior (Jython or Cython or IronPython may differ; or
 future versions of Python may cache a different range of numbers).

 Generally, if you are using the is operator to compare against
 anything other than None, you're doing it wrong. There are exceptions
 to this, but it takes knowing the particulars.

 -tkc




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