Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

03-27-2011 , 03:56 PM
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.
** Python Support Thread ** Quote
03-27-2011 , 05:44 PM
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).
** Python Support Thread ** Quote
03-27-2011 , 07:27 PM
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.
** Python Support Thread ** Quote
03-27-2011 , 07:43 PM
Oh nice. This is definitely something that can be written pretty concisely in Python.
** Python Support Thread ** Quote
03-27-2011 , 09:08 PM

I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I'm leaving you.
** Python Support Thread ** Quote
03-28-2011 , 10:23 AM
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?
** Python Support Thread ** Quote
03-28-2011 , 01:31 PM
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
** Python Support Thread ** Quote
03-28-2011 , 03:48 PM
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
** Python Support Thread ** Quote
03-29-2011 , 12:06 AM
Quote:
Originally Posted by Ankimo
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.
** Python Support Thread ** Quote
03-31-2011 , 11:44 PM
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
** Python Support Thread ** Quote
04-01-2011 , 12:12 AM
Checkout optparse (http://docs.python.org/library/optparse.html), that'll clean that up a lot.
** Python Support Thread ** Quote
04-01-2011 , 01:17 AM
+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.
** Python Support Thread ** Quote
04-01-2011 , 08:17 AM
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!
** Python Support Thread ** Quote
04-01-2011 , 09:04 AM
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.
** Python Support Thread ** Quote
04-01-2011 , 09:40 AM
Quote:
Originally Posted by kerowo
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
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
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
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.
** Python Support Thread ** Quote
04-01-2011 , 10:09 AM
Quote:
Originally Posted by jjshabado
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.
The one time I always used to forget converting string to int was when reading from a database.

At least in Python3 all text is unicode, hurray (you won't appreciate this untill you al of a suddne have a Japanses/Chinese customer)
** Python Support Thread ** Quote
04-02-2011 , 01:12 AM
I use modified Python scripts to scrape/process HTML DOM objects. It is awesome for that. Developing full web-based apps... eh. I don't like it for that. But yeah I prefer it over Perl for regex and purely scripting-based stuff.
** Python Support Thread ** Quote
04-03-2011 , 02:12 AM
Quote:
Originally Posted by kyleb
I use modified Python scripts to scrape/process HTML DOM objects. It is awesome for that. Developing full web-based apps... eh. I don't like it for that. But yeah I prefer it over Perl for regex and purely scripting-based stuff.
It's funny - I was of the same opinion. Except I recently started working more on a pylons app and its light years ahead of any java stack I've ever used in terms of overall ease of development.

I'm still not sold on building large applications in it - but it's growing on me.
** Python Support Thread ** Quote
04-03-2011 , 09:37 AM
All I can say is that Python is amazing for building large applications and you can get stuff done extremly fast (and clean imo). I used to work on a project that would qualify as pretty big.

It's always fun to go to a customer, discuss and hear something along the lines of "well your software seems perfect except it can't do X yet", make a phone call, go to lunch with them and when you come back to their office said feature is commited with tests.
Rapid development (and having great developers) is a very strong tool for pitching a product fwiw.
** Python Support Thread ** Quote
04-03-2011 , 10:32 AM
What do you qualify as pretty big? In terms of team size and code base.

I definitely have an open mind when it comes to building large applications in Python. I'm skeptical though that it wouldn't turn into a nightmare with a team of more than 4 or 5 people and even worse it feels like a language where one bad developer can really screw over a lot of people.
** Python Support Thread ** Quote
04-03-2011 , 12:13 PM
Quote:
What do you qualify as pretty big?
ERP system that is used in pretty huge companies. Development team is >20 core developers

Code base is >5 million lines of code iirc (not that LOC is a meaningfull measure anyways)

Quote:
I'm skeptical though that it wouldn't turn into a nightmare with a team of more than 4 or 5 people and even worse it feels like a language where one bad developer can really screw over a lot of people.
Not sure what you mean by this but it's a lot more related to the way the code is developed than the language anyways. We used Scrum style development for the core stuff and four eyes per commit as well as a pretty huge collection of tests of all kinds and certain best practices that needed to be followed.
Add a strict separation of the stable branch from the current devel one and customer specific customizations and it's really hard to screw anything up

I'll say though that I was kind of sceptical that Python could be used for anything big before working on this system but I was won over pretty quickly. From a business POV you also have customers every now and then who will aks you "wtf is this Python, business software is supposed to be written in insert language, usually JAVA" so I suppose JAVA does have a marketing edge of sorts.

tl;dr: just try it

Last edited by clowntable; 04-03-2011 at 12:22 PM.
** Python Support Thread ** Quote
04-04-2011 , 03:10 PM
Quote:
Originally Posted by jjshabado
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
I have a feeling this is discouraged or is not in Python 3 or something and it "should" be

outline = "Number {0}".format(count)

which is meant to be more powerful and work in all recent versions of Python. But I could be totally wrong, I don't have Python 3.
** Python Support Thread ** Quote
04-04-2011 , 03:17 PM
str.format()
Quote:
This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.
** Python Support Thread ** Quote
04-04-2011 , 03:35 PM
Ah, interesting. Good to know. Looks like its available starting in 2.6 (which is what I use).
** Python Support Thread ** Quote
04-05-2011 , 02:52 AM
Starting to pick up programming again; my only experience was in QBASIC as a pre-teen back in the 90s. Decided on a whim yesterday to try to learn Python.

Anyway, Is there an easy (I'll settle for hard) way to display a variable using tkinter and have the display update as the variable changes?

I started off using a label, but it didn't update dynamically, and the solutions I've found online so far seem to go about solving it in a very roundabout way.

TIA for any help!
** Python Support Thread ** Quote

      
m