[Tutor] Desktop Notifications on Linux

2014-04-10 Thread Dharmit Shah
Hi all,

I am trying to create a script that will go through the
/var/log/secure file on a Linux system and provide desktop
notifications for failed login attempts.

Here is the code - http://pastebin.com/MXP8Yd91
And here's notification.py - http://pastebin.com/BhsSTP6H

I am facing issue in the function new_attempts_from_last. I am not
able to raise a desktop notification from this function. It always
fails with this traceback - http://pastebin.com/cgHMScv3

I see this error only when I try to raise a notification from the
aforementioned function. If I run test examples under
/usr/share/doc/notify-python/examples, it works just fine. Also, if I
try to raise a notification from under if __name__ == __main__:, it
works without any issues. So I don't think there's any issue with OS's
notification daemon. Running from python shell like below also works
fine:

In [1]: import notification

In [2]: notification.notification(Hey)

What am I missing or doing wrong here?

If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop
environment.

For readability purposes, I have provided pastebin links. Let me know
if this is not the correct way.

Regards,
Dharmit.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Desktop Notifications on Linux

2014-04-10 Thread Dharmit Shah
Hi Peter,


On Thu, Apr 10, 2014 at 2:30 PM, Peter Otten __pete...@web.de wrote:
 Dharmit Shah wrote:

 I am trying to create a script that will go through the
 /var/log/secure file on a Linux system and provide desktop
 notifications for failed login attempts.

 Here is the code - http://pastebin.com/MXP8Yd91
 And here's notification.py - http://pastebin.com/BhsSTP6H

 I am facing issue in the function new_attempts_from_last. I am not
 able to raise a desktop notification from this function. It always
 fails with this traceback - http://pastebin.com/cgHMScv3

 I see this error only when I try to raise a notification from the
 aforementioned function. If I run test examples under
 /usr/share/doc/notify-python/examples, it works just fine. Also, if I
 try to raise a notification from under if __name__ == __main__:, it
 works without any issues. So I don't think there's any issue with OS's
 notification daemon. Running from python shell like below also works
 fine:

 In [1]: import notification

 In [2]: notification.notification(Hey)

 What am I missing or doing wrong here?

 If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop
 environment.

 For readability purposes, I have provided pastebin links. Let me know
 if this is not the correct way.


Thanks for your prompt response.

 Maybe you are running the code as a user that has no desktop? Here's a
 strapped-down demo:

 $ cat notify_demo.py
 import pynotify
 import sys

 pynotify.init(Notification)
 n = pynotify.Notification(Notification,  .join(sys.argv[1:]))
 n.show()
 $ python notify_demo.py hello world

 The notification appeared on my desktop. However, when trying again as root
 I got the error you are seeing:

 $ sudo su
 # python notify_demo.py hello again
 Traceback (most recent call last):
   File notify_demo.py, line 6, in module
 n.show()
 glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name
 org.freedesktop.Notifications was not provided by any .service files


That does ring some bells. I am logged into my F20 system as non-root
user but since reading /var/log/secure file requires superuser
privileges, I am running it as sudo:

  sudo python secure.py

That probably explains the issue I am facing. I will add the user to
the root group and see if it helps.

Regards,
Dharmit.

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Python Question..Please help

2013-09-27 Thread Dharmit Shah
Also, comparison is case sensitive. Meaning, 'A' and 'a' are not the same.

Hope that helps. :)

On Fri, Sep 27, 2013 at 1:14 PM, Amit Saha amitsaha...@gmail.com wrote:
 On Fri, Sep 27, 2013 at 3:48 PM, Jacqueline Canales
 jackiexxd...@gmail.com wrote:
 So I have been trying to do this program using ifs and or loops.
 I am having a hard time solving this question, If you could please assist me
 in the right direction.

 Write a program that lists all the composers on the list ['Antheil',
 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen'] whose name starts and ends
 with the same letter (so Nielsen gets lsited, but Antheil doesn't).

 I know below it prints the entire list of composers but i dont know  how to
 do the program above. I think I am thinking to much into but ive looked at
 all my notes and online resources and having a hard time coming up with
 anything.
 Please help!

 composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
 for person in composers:
 print(person)

 So, here you are printing every compose as you rightly state above.
 What you now need to do is:

 For each of the composers (`person'), you need to check if the first
 letter and the last letter are the same. Here;s a hint:

 s='abba'

 The first letter:

 s[0]
 'a'

 The last letter:

 s[-1]
 'a'


 If you now compare these, you will know if they are the same and hence
 you print him/her.

 Hope that helps.
 -Amit.


 --
 http://echorand.me
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor



-- 
Dharmit Shah
www.about.me/dharmit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Recursion always returns None

2012-08-28 Thread Dharmit Shah
Hello,

I am trying to do the following :

1) Ask user for the length of the word that he'd like to guess (for
hangman game).
2) Pick a random word from /usr/share/dict/words (which I understand
is not the best choice for hangman).
3) Call a function that would pick a random word to proceed further.

Below is the code for the part I described above :

[code]

#!/bin/env python
import random

def pick_random(l, ln):   # picks a random word from the list
l of length ln
global mystery
word = random.choice(l)
if word.__len__() != ln:
pick_random(l, ln)# recursion
else:
print Should return %s % word # prints the chosen random
word correctly
return word  # always
return None, why? :(

if __name__ == __main__:
ln = raw_input(How long word can you guess (number of alphabets) : )
ln = int(ln)
l = []
with open(/usr/share/dict/words, r) as f:
for i in f.readlines():
i = i.split(\n)[0]
if i.isalpha():
l.append(i)

word = pick_random(l, ln)
print word

[/code]

Sample output :

$ python hangman.py
How long word can you guess (number of alphabets) : 6
Should return inarch
None
$

The problem is that the last line print word always prints None. I
know I am doing something wrong in the recursion part of the function
pick_random. Can someone please point what I am missing. Thank you!

Cheers,
Dharmit

-- 
Dharmit Shah
www.about.me/dharmit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recursion always returns None

2012-08-28 Thread Dharmit Shah
Hello,

@Hugo Arts : Thank you! That was awesome to read. Thanks for the len()
suggestion.

@ Steve : Thank you. As suggested by Dave Angel, I am going to try the
loop. And even before implementing it, I can feel that it's going to
be more efficient than recursion.

@Dave Angel : Thank you for the loop idea. It didn't strike me at all.

@All : Thanks a bunch for helping me out.

:)

Cheers,
Dharmit


On Tue, Aug 28, 2012 at 7:18 PM, Dave Angel d...@davea.name wrote:
 On 08/28/2012 07:23 AM, Dharmit Shah wrote:
 Hello,

 I am trying to do the following :

 1) Ask user for the length of the word that he'd like to guess (for
 hangman game).
 2) Pick a random word from /usr/share/dict/words (which I understand
 is not the best choice for hangman).
 3) Call a function that would pick a random word to proceed further.

 Below is the code for the part I described above :

 [code]

 #!/bin/env python
 import random

 def pick_random(l, ln):   # picks a random word from the list
 l of length ln
 global mystery
 word = random.choice(l)
 if word.__len__() != ln:
 pick_random(l, ln)# recursion
 else:
 print Should return %s % word # prints the chosen random
 word correctly
 return word  # always
 return None, why? :(

 There's no return statement here, to cover the case where the if-clause
 succeeded.

 if __name__ == __main__:
 ln = raw_input(How long word can you guess (number of alphabets) : )
 ln = int(ln)
 l = []
 with open(/usr/share/dict/words, r) as f:
 for i in f.readlines():
 i = i.split(\n)[0]
 if i.isalpha():
 l.append(i)

 word = pick_random(l, ln)
 print word

 [/code]

 Sample output :

 $ python hangman.py
 How long word can you guess (number of alphabets) : 6
 Should return inarch
 None
 $

 The problem is that the last line print word always prints None. I
 know I am doing something wrong in the recursion part of the function
 pick_random. Can someone please point what I am missing. Thank you!

 Cheers,
 Dharmit


 There are two things wrong, one of which has already been pointed out.
 But the most fundamental thing that's wrong is that once you have called
 the recursion, you don't return a value at all, simply falling off the
 end of the function.  Python returns a value of None when you omit the
 return statement.

 So you should add a statement 'return word', which will eliminate the
 None.  But of course it'll be the wrong word.  To fix that, you need to
 assign the results of the call to pick_random() to the same local
 variable, word.

 As others have pointed out, this is a poor choice for recursion.
 Recursion can be more readable for some problems, when the problem
 statement is naturally recursive.  But even then, it can frequently lead
 to stack overruns, and performance problems.  But in this case a simple
 loop would make much more sense.  So unless the instructor is requiring
 you to use recursion, please redo it as a loop.

 While we're at it, please use the len() function, rather than __len__()
 method.  And instead doing a split() method for eliminating the
 linefeeds, what you really want to do is rstrip().



 --

 DaveA




-- 
Dharmit Shah
www.about.me/dharmit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to replace the '\'s in a path with '/'s?

2011-07-31 Thread Dharmit Shah
Ruchard,

Try return path.replace('\\', '/'). That gave me the output desired by you.
I don't know the reason. But I guess it's because \ is used as escape
character. I am sure someone in the list will point out the accurate reason.

On Sun, Jul 31, 2011 at 10:58 AM, Richard D. Moores rdmoo...@gmail.comwrote:

 64-bit Vista
 Python 3.2.1

 I would like to write a function that would take a path such as
 'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
 and return 'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf' . I've
 tried this:

 def test(path):
return path.replace('\', '/')

 print(test('C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'))

 gets me

 File c:\P32Working\untitled-5.py, line 2
return path.replace('\', '/')
^
 SyntaxError: EOL while scanning string literal
 Process terminated with an exit code of 1

 Thanks,

 Dick


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
Regards

Dharmit Shah http://about.me/dharmit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Web Programming

2011-07-20 Thread Dharmit Shah
Hi all,

I have been reading Head First
Pythonhttp://www.headfirstlabs.com/books/hfpython/ since
some time now. I am stuck in a chapter on Web Development. Web Development
has never been an area of my interest and hence I feel stuck in there. If
anyone here has read the book, I wish to know if it's okay to skip that
chapter and read further?

-- 
Regards

Dharmit Shah http://about.me/dharmit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Recursively flatten the list

2011-03-23 Thread Dharmit Shah
Hello,

I am learning Python and yesterday I cam across a definition wherein I was
supposed to flatten a list recursively. I am getting the solution properly
but wanted to know if I can optimize the code further.

#!/usr/bin/env python
new_list=[]
def flatten(num_list):

   flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
  [2, 9, 2, 1, 13, 2, 8, 2, 6]
   flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
  [9, 7, 1, 13, 2, 8, 7, 6]
   flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
  [9, 7, 1, 13, 2, 8, 2, 6]
   flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
  [5, 5, 1, 5, 5, 5, 5, 6]

global new_list
for i in num_list:
if type(i) == type([]):
new_list = flatten(i)
else:
new_list.append(i)
tmp = new_list
new_list=[]
return tmp

if __name__==__main__:
import doctest
doctest.testmod()

PS - My knowledge of Python is still very basic and I am trying to dive into
it as deeper as I can. Solutions on Stackoverflow.com were beyond my
understandability.

-- 
Regards

Dharmit Shah
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print Hello, World!

2011-02-03 Thread Dharmit Shah
I'd also recommend using
http://www.openbookproject.net/thinkcs/python/english2e/ . Currently I am
learning from it. Once u are through with it u can read the book called Dive
into Python. it's for experienced users. Google it.

Hope that helps.

On Thu, Feb 3, 2011 at 1:20 PM, davidheise...@gmail.com wrote:


 Seven years ago, my story was similar. I started off with The Python Quick
 Book (Manning) and Python - Visual Quickstart Guide (Peachpit Press).
 Both are very easy to follow. I still pick up the Quick book once in a
 while for reference.

 This Tutor list helped a lot. I learned by trying out the things people
 offered as solutions to problems from people like you and me.

 Asking questions here is a way to help a lot of new Python programmers, and
 a few older ones, too.

 There are a lot more resources these days, too. Search on Python in
 YouTube.

 There are a lot of on-line tutorials, too.


 - Original Message - From: Doug Marvel 
 smokeinourlig...@gmail.com
 To: tutor@python.org
 Sent: Wednesday, February 02, 2011 6:00 PM
 Subject: [Tutor] print Hello, World!


  Hey folks,

 I'm Doug. I've been using computers since second grade, and I know a
 little about them. I am, however, completely new to programming. I
 don't even know what I know about it. I'd like some social interaction
 with this, but I can't go back to school until summer or fall of this
 year. I don't want to wait to start learning this as I feel like I'm
 already about a million years behind. I asked the Oracle
 (www.google.com) and after messing around with the Python Shell and
 getting a lot of error messages, I decided I need some remote help.
 Here's where I'm at:

 - I have downloaded and installed Python 2.6.4. Successfully, I think.
 - I am running Windows XP SP3 (though I'm going to see if I can do
 this on my laptop, which has Windows 7)
 - I have toyed around with some tutorials, but all they really taught
 me is that I need a teacher.

 I'm sure you guys are busy, but I read that the most basic questions
 are okay. As I'm sure there is at least one good resource on the net
 for people in my position, I'd like some suggestions on where to
 start. I plan on bothering you all as little as possible, but I am
 seriously hoping to make real progress between now and my first class.
 I have a feeling once I get a basic understanding, I'll run away with
 it. It's just very... big right now. So this list seems like a good
 thing, but tell me if I'm in the wrong place.

 I am hoping for a link to a somewhat comprehensive online resource
 that explains from the beginning in English, plain English, as this is
 the only language I speak. Something to get my foot in the door would
 be awesome.


 Cheers,
 Doug Marvel
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
Regards

Dharmit Shah
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor