[Tutor] Learning python scripts for practical linux activities.

2015-01-15 Thread dw
Hello,
I'm new the the group and new to programming in Python.
I would like to find a source, book etc of Python learning projects.
Projects that are useful for standard Linux activities like bulk
renaming files, managing repository packages.
Maybe python scripts that incorporate "LAME" for modifying audio files.
Anything of that nature.
As I learn Python, I would like to create Python utilities.
I'm hoping there is some learning material that might lead me in that
direction.
Sincere thanks
d
-- 
 bw...@fastmail.net

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


[Tutor] Testing a string to see if it contains a substring

2015-01-21 Thread dw
Hello Python Friends.
I have a string array, called "line_array".

There may be up to 50 or more elements in the array.
So:
- line_array[1] may contain "01/04/2013  10:43 AM17,410,217
DEV-ALL-01-04-13.rlc\n"
- line_array[2] may contain "01/25/2013  03:21 PM17,431,230
DEV-ALL-01-25-2013.rlc\n"
- line_array[3] may contain "\n"

I want to retain all elements which are valid (i.e. contains a date
value xx/xx/)
So I'm using a regex search for the date value located at the start of
each element...this way

misc_int1 =
re.search(r'[0-9]{2}/[0-9]{2}/[0-9]{4}',line_array[x]).start()

This method works really well when the regex date characters are found.
It returns the value 0 to misc_int1 because the date regex starts at 0
position.
I was hoping the .start() method would return a -1 value for the
instance in which the regex search string is not found.
That way I could iterate through the elements and retain only the valid
ones.

However, the .start() method throws an error if the regex search string
is not found.

Your suggestions greatly appreciated!
Thanks!!
d :-]

-- 
 bw...@fastmail.net

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


Re: [Tutor] Testing a string to see if it contains a substring (Steve and Mark)

2015-01-23 Thread dw
Thanks so much Steve and Mark!
You've given me a lot to chew on. :-D
I'll pursue!
More Python FUN!!




Based on your description, I think the best way to do this is:

# remove blank lines
line_array = [line for line in line_array if line != '\n']


Possibly this is even nicer:

# get rid of unnecessary leading and trailing whitespace on each line
# and then remove blanks
line_array = [line.strip() for line in line_array]
line_array = [line for line in line_array if line]


This is an alternative, but perhaps a little cryptic for those not 
familiar with functional programming styles:

line_array = filter(None, map(str.strip, line_array))

No regexes required!

However, it isn't clear from your example whether non-blank lines 
*always* include a date. Suppose you have to filter date lines from 
non-date lines?

Start with a regex and a tiny helper function, which we can use lambda 
to embed directly in the call to filter:

DATE = r'\d{2}/\d{2}/\d{4}'
line_array = filter(lambda line: re.search(DATE, line), line_array)

In Python version 3, you may need to wrap that in a call to list:

line_array = list(filter(lambda line: re.search(DATE, line),
line_array))

but that isn't needed in Python 2.

If that's a bit cryptic, here it is again as a list comp:

DATE = r'\d{2}/\d{2}/\d{4}'
line_array = [line for line in line_array if re.search(DATE, line)]


Let's get rid of the whitespace at the same time!

line_array = [line.strip() for line in line_array if 
  re.search(DATE, line)]


And if that's still too cryptic ("what's a list comp?") here it is again 
expanded out in full:


temp = []
for line in line_array:
if re.search(DATE, line):
temp.append(line.strip())
line_array = temp


How does this work? It works because the two main re functions, 
re.match and re.search, return None when then regex isn't found, and a 
MatchObject when it is found. None has the property that it is 
considered "false" in a boolean context, while MatchObjects are always 
consider "true".

We don't care *where* the date is found in the string, only whether or 
not it is found, so there is no need to check the starting position.



-- 
Steven

=
I'd use 
https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime 
to test the first ten characters of the string.  I'll leave that and 
handling IndexError or ValueError to you :)
-- 
 bw...@fastmail.net

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


Re: [Tutor] Testing a string to see if it contains a substring (dw)

2015-01-23 Thread dw
Thanks for your good comments.
I do think I found a work around

body = ""
for x in range(0,len(line_array)):
test_line = len(re.findall(r'[0-9]{2}/[0-9]{2}/[0-9]{4}',
line_array[x]))
if test_line > 0:
body = body + line_array[x]+"\n"

For each iteration re.findall returns a list object based upon the regex
search.
If an element contains "01/04/2013".the regex is found, and
re.findall returns it as a list object ['01/04/2013']
I can then perform a len function on that which will = 1, since the list
contains only one string object.
For the row in which the regex is not found, re.findall will return []
Which is essentially a list with 0 objects, and the len function will
then = 0.

Thanks Alan G for your example!!
It will be fun to try it!! :-]
I look for the KISS model in coding, and I like your example.
-- 
 bw...@fastmail.net

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


[Tutor] Initiating an external email transmission

2015-01-23 Thread dw
I have a python script that I've created.
It logs into an online email service and sends out an email.
It works in my Linux box, and it works in my Windows 7 box.
I've also converted it with Py2EXE and it works as an executable in my
Win-7 box.

The issue is, I made it for someone else to use in his PC.
And its not working for him.
His system is Windows 7.
And I'm wondering if perhaps he has a firewall blocking it?
Its his personal laptop.
Any thoughts would be appreciated
==
here is the code with account info x'd out

#!/usr/bin/python
#this is a funky way of clearing the screen
for x in range(0, 20):
print("\n")

#Use checkip.dynds.org to get IP address 
import urllib2
u = urllib2.urlopen('http://checkip.dyndns.org')
myIP = u.next()
print("The data returned is: \n%s"%myIP)

#Now we parse the returned string leaving only the IP address
temp=myIP.split('>')
getip=temp[6]
temp=getip.split('<')
getip=temp[0]
temp=getip.split()
myIP=temp[3]

#include the python SMTP library file
import smtplib
print("Importing smtplib file")

#Call SMTP library function to establish the email service
#Here we need the server name and the port assignment
print("Establishing server variables")
server = smtplib.SMTP('xxx.xxx.xxx', 587)
print("Establishing SMTP protocol ehlo")
server.ehlo()
server.starttls()

#Next establish the user account of the online email service
#we establish a connect using the user account credentials
print("Establishing login")

server.login("xx...@x.com", "")

#Use the variable 'msg' for the text within the message
print("creating message")

msg = "\r\n".join([
  "From: x...@x.com",
  "To: x...@x.com",
  "Subject: Automated Test Message from Python program",
  "",
  "The IP address of this PC is %s" % (myIP)
  ])

#Establish FROM and TO email addresses
fromaddr="x...@.com"
toaddr="x...@x.com"

#Send the message
print("Sending email")
server.sendmail(fromaddr, toaddr, msg)
print("Disconneting")
server.quit()
-- 
 bw...@fastmail.net

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


Re: [Tutor] Using if statement with csv file

2015-01-28 Thread dw
Hi Tammy,
I wonder how big your csv file is?
I have ever read a small csv file into one variable.
Even a csv file with 100 rows.
And then split it into identified fields from there.
I look at the csv file with Notepad++ to see if the data rows end with a
\r or a \n.
I then split that into a list and each row is an element in the list.
I can then split each row by the comma delimiter, to resolve down to
each field.
Just an idea.




I have a csv file. I would like to create a filter or if statement on a
column but it is not producing the right results. It displays
everythingHere is the example:import csvwith open('test.csv') as
csvfile:reader = csv.DictReader(csvfile)for row in reader:if
row['Absent'] > 10  print rowI just want the column Absent to show
me all of the numbers that are greater than 10.  It gives me all the
results.  I am not sure what to do.Thank you, Tammy
-- 
 bw...@fastmail.net

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


[Tutor] Wondering about a project

2015-02-02 Thread dw
Hi Python gang.
In my Ubuntu system, I'm pondering a first GUI Python project:
1) Prompt for a user input string.
The intended string would be a youtube URL
2) Call youtube-dl to download the file
3) When the file is downloaded, call GnomePlayer or VLC to play the file

I'm new at Python and haven't built a GUI application yet.
I wonder if this project would be over my head?
Perhaps I should attempt to build it in terminal mode first?
-- 
 bw...@fastmail.net

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


Re: [Tutor] Wondering about a project

2015-02-03 Thread dw
Well I've made some progress in terminal mode.
The OS is Ubuntu 12.04 LXDE desktop
This script works.
It prompts for the url, lets the user confirm download and then runs the
video in Gnome-Player.  

Once Gnome-Player is dropped by the user, it prompts to either move the
file to storage or delete.  Then returns for another round.
---
#!/usr/bin/python
from subprocess import call
import glob,re,shutil,os,time

geturl=""
while True:
call(["clear"])
geturl= input("Enter Youtube URL ")
if len(geturl)==0:
break

def unquote(geturl):
return re.compile('%([0-9a-fA-F]{2})',re.M).sub(lambda m:
chr(int(m.group(1),16)), geturl)
print("Download %s?" %geturl)
answer= raw_input("Press ENTER to continue: ")
call(["youtube-dl", geturl])

#get the youtube video file names
YTfile=glob.glob('./*.mp4')
print("%s Youtube files: "%(YTfile))

#clean up the file names
for x in range(len(YTfile)):
YTfile[x] = YTfile[x][2:]
print("%s: "+str(x)+" "+YTfile[x]+'\n')

#now play the youtube video
#for x in range(len(YTfile)):
call(["gnome-mplayer", YTfile[0]])

#Decide to save the file or delete it
dofile = raw_input('Save or Delete: S or D ')
if dofile=='S':

#save the file by moving it into folder
for x in range(len(YTfile)):
shutil.move(YTfile[x],'/home/user/Documents/YOUTUBEFILES')  

#delete the file
else:
os.remove(YTfile[0])
print("Deleted %s "%YTfile[0])
time.sleep( 3 )
-- 
 bw...@fastmail.net

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


Re: [Tutor] Wondering about a project

2015-02-03 Thread dw
Thanks Alan for the tips!!!
I greatly appreciate.  :-D
Duane
-- 
 bw...@fastmail.net

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


Re: [Tutor] Wondering about a project

2015-02-03 Thread dw
Thanks Dave A for your Tips!!
I greatly appreciate.  :-D
Duane
-- 
 bw...@fastmail.net

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


[Tutor] Wondering if there is a print in location command for terminal?

2015-02-09 Thread dw
Hi Python Gang,
In the olden days of Basic, there was a "Locate" command to locate
strings in specific x/y locations on the terminal.

For example:
Locate 9,10:print "THE POWER OF PYTHON"
Would count down from top of terminal 9 increments.
And the count (from left to right of terminal) 10 spaces.
Then print the string there.

I wonder if what I'm looking for is the "Curses" library?
I'm running Python 2.6 in Linux and 2.7 in Window.
Thanks!
dw :-]
-- 
 bw...@fastmail.net

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


[Tutor] Suggestions on pyserial for RS232 com monitor?

2015-02-18 Thread dw
Hi Python Gang.
I have a project where I'm sending serial data to a Ten-Tec RX320 radio
receiver.
I'm using COM2 @1200 baud(standard N,8,1 parameters).
I have a need to verify the data that I'm sending to the device.
It occurred to me that python installed on a laptop might be a great
idea.
I can send the commands to the laptop instead of the radio and monitor
the commands.
Does anyone have any suggestions on a python script that would wait for
incoming data on a select com and display the data once sent?
I would probably install python 2.7 in the laptop for this project,
since the laptop has Ubuntu 12.04 OS.
Any suggestions, script, or point me in a direction would be greatly
appreciated.

Sincere thanks!
dw :-]
-- 
 bw...@fastmail.net

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