Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 03-27-2011, 03:56 PM   #1
lolcat
 
kerowo's Avatar
 
Join Date: Nov 2005
Posts: 20,753
** Python Support Thread **

I'm getting started with Python the old fashioned way; trying to do something for work so I look better than the rest of the schlubs I work with. I'll update this with what I'm looking to do and what current problems I'm researching. Feel free to add your own questions or answers.

We are in the process of rolling our own tools to get information about the servers we support through rest calls instead of running scripts on the servers themselves. What I'm trying to do is generate a list of users who have been offline for a day. There are rest calls that return all users on a server with their registration status. I'm using that to generate a list of registered users who I then make a rest call to see the last time they called into the server. If they haven't called in within a day they are "offline," and if they haven't called in within two years they have never been online.

The first pass into date math was almost successful and is what I'm going to be digging into today.
This works as long as the date is past the first of the month:
today = datetime.datetime.today()
#TODO: Fix for first day of month
start_date = today.replace(day=today.day-1).strftime("%Y-%m-%dT%H:%M:%S.000")

Any tips or tricks for dealing with date strings would be appreciated.
kerowo is online now   Reply With Quote
Old 03-27-2011, 05:44 PM   #2
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,079
Re: Python Support Thread

I'm not entirely sure what you're trying to do - but check out timedelta. It'll make life way easier when doing checks of time ranges (like within the past day).
jjshabado is offline   Reply With Quote
Old 03-27-2011, 07:27 PM   #3
lolcat
 
kerowo's Avatar
 
Join Date: Nov 2005
Posts: 20,753
Re: Python Support Thread

The rest calls use start dates that I calculate and timedelta is what I found and got working. We have equipment in people's homes that take readings and send them up to our servers. The rest calls let me see the latest reading from a time period and to see if they have ever got a read I need to walk back a month at a time and make another call.

I think it's all working, now I'm getting it to email the results so I can automate it.
kerowo is online now   Reply With Quote
Old 03-27-2011, 07:43 PM   #4
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,079
Re: Python Support Thread

Oh nice. This is definitely something that can be written pretty concisely in Python.
jjshabado is offline   Reply With Quote
Old 03-27-2011, 09:08 PM   #5
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,213
Re: Python Support Thread


I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I'm leaving you.
TheIrishThug is offline   Reply With Quote
Old 03-28-2011, 10:23 AM   #6
old hand
 
Join Date: Dec 2010
Location: Somewhere over the rainbow
Posts: 1,450
Re: Python Support Thread

wow new forum, great stuff!!

just curious but I figure I might as well ask before I invest what little time I have...

Is it possible to write a Windows based HUD with Python?

Any tips on tools, frameworks, libraries?
Ankimo is offline   Reply With Quote
Old 03-28-2011, 01:31 PM   #7
Carpal \'Tunnel
 
clowntable's Avatar
 
Join Date: Jun 2006
Location: 39, 46, 56, 59, 191
Posts: 39,784
Re: Python Support Thread

Certainly possible, I haven't done any Pythong GUI stuff outside Pygame but it can handle SQL pretty well (assuming the underlying database is SQL based) and PyQT or something looks good for gui stuff as well.

Also, we need the title to be bold and with stars like the other ones
clowntable is offline   Reply With Quote
Old 03-28-2011, 03:48 PM   #8
old hand
 
Join Date: Dec 2010
Location: Somewhere over the rainbow
Posts: 1,450
Re: Python Support Thread

Ok, I just realized that FPDB is written in Python so obv it's possible.

Maybe I'll just take a look at FPDB instead
Ankimo is offline   Reply With Quote
Old 03-29-2011, 12:06 AM   #9
old hand
 
sorrow's Avatar
 
Join Date: Apr 2008
Location: Perth, Western Australia
Posts: 1,500
Re: Python Support Thread

Quote:
Originally Posted by Ankimo View Post
Ok, I just realized that FPDB is written in Python so obv it's possible.

Maybe I'll just take a look at FPDB instead
Feel free to PM me on this, I know the codebase pretty well.
sorrow is offline   Reply With Quote
Old 03-31-2011, 11:44 PM   #10
lolcat
 
kerowo's Avatar
 
Join Date: Nov 2005
Posts: 20,753
Re: ** Python Support Thread **

A quickie to move up to a given number of files from one directory to another, we had to change an inbound directory that was getting killed and limit the number of files that hit it at once. Be kind, I'm new at this..., and I know I need to clean up the import...

Code:
#!/usr/bin/env python

import os, sys, urllib, time, datetime, unicodedata, shutil, glob

nargs = len(sys.argv)
today = datetime.datetime.today()
now = today.strftime("%Y-%m-%d %H:%M:%S")
SENDMAIL="/usr/sbin/sendmail"


# ensure there is a file name passed in
if not(nargs == 6):
	print "Moves up to a set number of files matching a pattern from source to target"
	print "Emails results"
	print "Usage: %s [source] [pattern] [target] [max] [email-address]" % sys.argv[0]
	print """./kerowoMoveFiles /ftp/interfaces/inbound/ "*.xml" /ftp/interfaces/inbound/temp 1500 mail@com"""
	sys.exit()
else:
	source = sys.argv[1]
	if (os.access(source,os.F_OK)) : #check for existance
		print "source path valid"
	else:
		print "source path invalid"
		sys.exit()
		
	try:
		pattern = sys.argv[2]
	except:
		print "pattern invalid"
		
	try:
		sourceList = glob.glob(source + pattern)
	except:
		print "glob of source failed"
		sys.exit()
		
	if len(sourceList) == 0:
		print "empty glob exiting, make sure source has closing slash"
		sys.exit()
	
	
	target = sys.argv[3]
	if (os.access(target,os.F_OK)) : #check for existance
		print "target path valid"
	else:
		print "target path invalid"
		sys.exit()
		
	
	count = 0
	try:
		limit = int(sys.argv[4])
		test = limit + 1
	except:
		print "invalid max"
		sys.exit()

	try:
		email = sys.argv[5]
	except:
		print "email address failed"
		sys.exit()

	for i in sourceList:
		shutil.move(i,target)
		count += 1
		if count >= limit:
			break


	p=os.popen("%s -t" % SENDMAIL, "w")
	toLine = "To:" + email + "\n"	
	p.write(toLine)
	subLine = "Subject: Files moved at " + now + "\n"
	p.write(subLine)
	p.write("\n")
	p.write("\n")
	outLine = "Number " + str(count)
	p.write(outLine)
	status = p.close()
	print "status of email : %s" % status
kerowo is online now   Reply With Quote
Old 04-01-2011, 12:12 AM   #11
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,079
Re: ** Python Support Thread **

Checkout optparse (http://docs.python.org/library/optparse.html), that'll clean that up a lot.
jjshabado is offline   Reply With Quote
Old 04-01-2011, 01:17 AM   #12
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,213
Re: ** Python Support Thread **

+1 for optparse.

Other than that there isn't much.
  • You can use "!=" instead of "not(nargs == 6)".
  • You should also decide if you are going to put parenthesis around your if clauses and stick with whatever convention you choose. They aren't necessary, but I use them because I am used to them being there from every other language I use.
  • At the end you are saving strings into variables, but only using those variables on the next line for p.write(). You might as well just build the string in the function call if you don't need them elsewhere.
TheIrishThug is offline   Reply With Quote
Old 04-01-2011, 08:17 AM   #13
lolcat
 
kerowo's Avatar
 
Join Date: Nov 2005
Posts: 20,753
Re: ** Python Support Thread **

I do need to look at optparse, it's been lower priority because I needed to learn everything else...

I probably tried =! or <> the first time and it didn't work and fell back on not().

So I should be able to say if x==1 or y==2: I think that is where I tend to over use parenthesis.

I'm still note entirely clear on when things can be converted to strings and when they can't. Doing it on a separate line then making the function call probably grew out of fixing two things on an error and trying again instead of fixing one thing...

Thanks for the input!
kerowo is online now   Reply With Quote
Old 04-01-2011, 09:04 AM   #14
Carpal \'Tunnel
 
jjshabado's Avatar
 
Join Date: Jul 2006
Posts: 11,079
Re: ** Python Support Thread **

Don't forget you can use the C style (is it actually called that?) string syntax.
So:

outLine = "Number " + str(count)

could be:

outLine = "Number %d" % count

It's probably 'good' (meaning it doesn't really matter) to always use the string syntax. I've also had a hell of a time becoming 100% sure on when to convert or not. I've had a few hard-to-debug bugs where I had a number as a string and forgot to convert it to an int before a comparison.
jjshabado is offline   Reply With Quote
Old 04-01-2011, 09:40 AM   #15
Pooh-Bah
 
TheIrishThug's Avatar
 
Join Date: Jan 2005
Location: Belligerent and numerous
Posts: 5,213
Re: ** Python Support Thread **

Quote:
Originally Posted by kerowo View Post
I do need to look at optparse, it's been lower priority because I needed to learn everything else...
Ok, just promise you will use it the next time you need to parse arguments. It's fairly straight forward and does all the dirty work for you.

Quote:
Originally Posted by kerowo View Post
I probably tried =! or <> the first time and it didn't work and fell back on not().
Should be !=, like you say it Not Equal. <> is also supported, so there must have been a different issue.

Quote:
Originally Posted by kerowo View Post
So I should be able to say if x==1 or y==2: I think that is where I tend to over use parenthesis.
Yes, that would work.

Quote:
Originally Posted by kerowo View Post
I'm still note entirely clear on when things can be converted to strings and when they can't. Doing it on a separate line then making the function call probably grew out of fixing two things on an error and trying again instead of fixing one thing...

Thanks for the input!
That's ok, you'll get it.
TheIrishThug is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 08:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive