Date Parsing Question

2010-09-03 Thread Gavin
Hi,

I'm using the python-dateutil package :  http://labix.org/python-dateutil
to parse a set of randomly formatted strings into dates.  Because the
formats are varied, I can't use time.strptime() because I don't know
what the format is upfront.

python-dateutil seems to work very well if everything is in English,
however, it does not seem to work for other languages and the
documentation does not seem to have any information about locale
support.

Here's an example showing my problem:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from dateutil.parser import *
>>> import datetime
>>> import time
>>> date_string1 = time.strftime("%Y-%B-%d %H:%M:%S",(2010,10,3,1,1,1,1,1,1))
>>> print date_string1
2010-October-03 01:01:01
>>> parse(date_string1)
datetime.datetime(2010, 10, 3, 1, 1, 1)

everything is ok so far, now retry with a date in german:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "german")
'German_Germany.1252'
>>> locale.getlocale()
('de_DE', 'cp1252')

>>> date_string1 = time.strftime("%Y-%B-%d %H:%M:%S",(2010,10,3,1,1,1,1,1,1))
>>> print date_string1
2010-Oktober-03 01:01:01
>>> parse(date_string1)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'date_string' is not defined
>>> parse(date_string1)
Traceback (most recent call last):
  File "", line 1, in 
  File "c:\python26\lib\site-packages\python_dateutil-1.5-py2.6.egg
\dateutil\parser.py", line 697, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
  File "c:\python26\lib\site-packages\python_dateutil-1.5-py2.6.egg
\dateutil\parser.py", line 303, in parse
raise ValueError, "unknown string format"
ValueError: unknown string format


Am I out of luck with this package?  Just wondering if anyone has used
this to work with non-english dates.  I'm also open to other ideas to
handle this.

Appreciate the assistance,

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


DOS problem (simple fix??)

2005-01-07 Thread Gavin Bauer
My DOS window (running in windows ME) closes the second it finishes
running my programs. As you can imagine, this makes it hard to see the
results. I've gotten in the habit of putting raw_input("Press enter to
exit") at the end of every program, and in addition to being pain in
the butt, it often fails to work. Being new to programming in general,
I make more mistakes than most people. My programs often have errors
before they get to my raw_input command. They then display the error
and immediately close. It is naturally a lot easier to fix an error
when you know what the error is. This makes debugging even more
annoying than it ordinarily would be, if you can imagine that. I've
heard that it should be a simple preference fix, but I've asked around
and no one seems to know how.

Thank you, and please make all answers simple enough to be understood
by a highschool student and his father :) .
-- 
http://mail.python.org/mailman/listinfo/python-list


$5 Campaign - Packt Publishing

2015-12-23 Thread gavin . packt
Hey,

Packt Publishing is inviting the tech world to explore its extensive library of 
eBooks and video courses for one amazing price. For the rest of December and 
into the New Year, every single eBook and video course Packt has ever created 
will be available on the publisher's website for just $5!

Check it out: http://bit.ly/1O8QkNG

Offer lasts till 8th Jan, 2016.
-- 
https://mail.python.org/mailman/listinfo/python-list


Context manager with class methods

2011-09-22 Thread Gavin Panella
Hi,

On Python 2.6 and 3.1 the following code works fine:

class Foo(object):

@classmethod
def __enter__(cls):
print("__enter__")

@classmethod
def __exit__(cls, exc_type, exc_value, traceback):
print("__exit__")

with Foo: pass

However, in 2.7 and 3.2 I get:

Traceback (most recent call last):
  File "", line 1, in 
AttributeError: __exit__

Is this a regression or a deliberate change? Off the top of my head I
can't think that this pattern is particularly useful, but it seems
like something that ought to work.

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


Spaces from string specifier

2007-09-13 Thread Gavin Tomlins
Greetings all,

 

I'm trying to work out when using a format specifier I get spaces in the
resulting string.  Eg. Looking at the outputted string you can see that
there are spaces after T5LAT, F4LAT etc. as I result from trying to keep the
code aligned 

 

Does anyone have any insights in how to prevent this ? 

 

Regards

 

Gavin

 

--- Code Fragment ---

 

fmtSqlP300Amp = 'UPDATE Patient SET  \

O2AMP = "%s", O1AMP = "%s", OzAMP = "%s", PzAMP = "%s",
P4AMP = "%s", CP4AMP = "%s", T6AMP = "%s", C4AMP = "%s", TP8AMP = "%s",
T4AMP = "%s", T5AMP = "%s", P3AMP = "%s", \

CP3AMP = "%s", CPzAMP = "%s", CzAMP = "%s", FC4AMP = "%s",
FT8AMP = "%s", TP7AMP = "%s", C3AMP = "%s", FCzAMP = "%s", FzAMP = "%s",
F4AMP = "%s", F8AMP = "%s", \

T3AMP = "%s", FT7AMP = "%s", FC3AMP = "%s", F3AMP = "%s",
FP2AMP = "%s", F7AMP = "%s", FP1AMP = "%s" \

WHERE Pat_Id = "%s;"'

 

sqlAmpString = fmtSqlP300Amp % (
self.ERPAmpSites[O2],self.ERPAmpSites[O1],self.ERPAmpSites[Oz],self.ERPAmpSi
tes[Pz],self.ERPAmpSites[P4],self.ERPAmpSites[CP4],self.ERPAmpSites[T6], \

 
self.ERPAmpSites[C4],self.ERPAmpSites[TP8],self.ERPAmpSites[T4],self.ERPAmpS
ites[T5],self.ERPAmpSites[P3],self.ERPAmpSites[CP3], \

 
self.ERPAmpSites[CPz],self.ERPAmpSites[Cz],self.ERPAmpSites[FC4],self.ERPAmp
Sites[FT8],self.ERPAmpSites[TP7],self.ERPAmpSites[C3], \

 
self.ERPAmpSites[FCz],self.ERPAmpSites[Fz],self.ERPAmpSites[F4],self.ERPAmpS
ites[F8],self.ERPAmpSites[T3],self.ERPAmpSites[FT7], \

 
self.ERPAmpSites[FC3],self.ERPAmpSites[F3],self.ERPAmpSites[FP2],self.ERPAmp
Sites[F7],self.ERPAmpSites[FP1], self.ERPPatId)

 

 

print sqlAmpString

--- Output ---



sqlAmpString



UPDATE Patient SET  O2AMP = "2.43119", O1AMP = "2.77413",
OzAMP = "2.47781", PzAMP = "2.50786", P4AMP = "4.579", CP4AMP = "3.50302",
T6AMP = "9.55172", C4AMP = "1.21119", TP8AMP = "5.6498", T4AMP = "1.29149",
T5AMP = "0.565705", P3AMP = "-0.957866", CP3AMP =
"-0.231566", CPzAMP = "0.970299", CzAMP = "-0.214471", FC4AMP = "-0.607667",
FT8AMP = "-0.226904", TP7AMP = "-1.51269", C3AMP = "-1.26248", FCzAMP =
"-1.61319", FzAMP = "-2.55086", F4AMP = "-1.87688", F8AMP = "-1.11846",
T3AMP = "-1.98619", FT7AMP = "-2.0613", FC3AMP = "-1.8458", F3AMP =
"-2.23433", FP2AMP = "-2.01364", F7AMP = "-1.80539", FP1AMP = "-2.00328"
WHERE Pat_Id = "GT0001;"

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

Checking the existence of parsed variables

2008-01-23 Thread Gavin Lusby

Hello all,
I am regularly writing programs that parses ever changing lists of
variables.
The method I do it is by loading it in to a dictionary of the form:
d['variable_name']=variable_value

So when it comes to a method that uses a required set of variables I have to
make tens of the following statements
if d.has_key('swr'): 
swr = float(d['swr'])
else: 
print 'ERROR: COREYTAB variable swr not specified'; iErr += 1

When I thought a pythonic way of doing it would be to use a variable
variable.(sorry that sounds ridiculous) but it would work like this:

_start_code_
___
# make all input parameters equal the string of themselves
type = 'type'; tabnum = 'tabnum';  coreyw = 'coreyw'; coreyow =
'coreyow'
swc = 'swc'; swr = 'swr'; kro_swc = 'kro_swc'; krw_swr = 'krw_swr'
coreyg = 'coreyg'; coreygo = 'coreygo'; sgi = 'sgi'; sgc = 'sgc'
sgr = 'sgr'; kro_sgc = 'kro_sgc'; krg_swc = 'krg_swc'
# error check parameters existence and if there, over-write their info
# if there are integer values, then must remember to re-typecast them
later
for var in (type, tabnum, coreyw, coreyow, swc, swr, kro_swc, krw_swr,
coreyg, coreygo, sgi, sgc, sgr, kro_sgc, krg_swc):
if d.has_key(var):
if isnumeric(d[var]):
var = float(d[var])
else:
var = d[var]
else:
print 'ERROR: getcorey() parameter '+str(var)+' not specified'
iErr += 1
_end_code___
___

Since this doesn't work, any other ideas out there?

Many thanks in advance, Gavin

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

File.write() not working in a sleepy loop

2008-02-19 Thread Gavin Lusby
Hello,

I am getting odd behaviour when I am trying to write a file in a while loop
with a sleep function call.

I am trying to periodically change the number of jobs on a cluster depending
on the current usage and a dummy version that does not require access to the
cluster usage is attached at the end.

The crux of the matter is in the main loop. When I run it, it writes a blank
file on the first iteration, then sleeps and on the 2nd iteration, it
finally puts in the correct value. I simply can't understand why it
postpones the open, write, close section of the 1st loop until the sleep is
finished. The sleep() is clearly after the write().

Thanks in advance, Gavin

 
___sample_code___sample_code___sample_code___sample_code___sample_code___sample_code___sample_code

while i == 1:
if pid <> -1:
i = pid_exists(pid)
if g.verbose: os.system('clear')
## get data ###
freeload, halfload, fullload =
20,20,40#getnodedata()<--- dummy data
weekend, phase = gettimedata()
userload =
2#getuserdata()<-- dummy
data
if g.verbose: print '...total nodes =',2 * (freeload + halfload +
fullload)
if g.verbose: print '...used nodes =',halfload + 2 * fullload
if g.verbose: print '...free nodes =',2 * freeload + halfload
if g.verbose: print '...user nodes =',userload
# usage logic #
if weekend or phase == 3:
answer = maxnode
if g.verbose:
print '...maxing out the machine with '+str(answer)
else:
answer = max(minnode,2*freeload+halfload-slack)
if g.verbose:
print '...limiting runs to '+str(answer)+' nodes'
 write outfile 
o=open(os.getcwd()+g.dsep+file, 'w')
if g.verbose:
print '...writing to '+os.getcwd()+g.dsep+file
o.write('%over = (njobs => '+str(answer)+');\n')
o.close
### sleep now #
sleep(refresh)
#! /data/gen27/utg/lusbg1/bin/Python-2.5.1/python
import sys, os, getopt, datetime
from time import sleep
from subprocess import Popen, PIPE
from getpass import getuser

class g:
'''
global variables class

'''
if os.name in ('nt','dos','ce'):
dsep = '\\'
else:
dsep = '/'
verbose = False

def main():
'''
main iterative subroutine

'''
if g.verbose: os.system('clear'); print '\n...loadmax.py'
### get opts ##
try:
opts, args = getopt.getopt(sys.argv[1:], "o:p:r:l:u:s:thv", 
["help","verbose"])
except getopt.GetoptError:
usage()
sys.exit()
file = 'mybad'
slack = 20
minnode = 40
maxnode = 100
refresh = 10*60
tracker = 'tracker.dat'
answer = 0
pid = -1
for opt, arg in opts:
if opt == "-v": # verbosity switch
g.verbose = True
if opt == "-o": # output filename switch
file = arg
if opt == "-t": # tracker filename switch
tracker = True
if opt == "-r": # refresh rate converted min->sec
refresh = float(arg)*60.0
if opt == "-u": # maximum permissable nodes
maxnode = int(arg)
if opt == "-l": # minimum permissable nodes
minnode = int(arg)
if opt == "-p": # follow specified pid
pid = int(arg)
if opt == "-s": # no of slack nodes
slack = int(arg)
if opt in ("-h", "--help"): # help!
usage()
sys.exit()
if file == 'mybad':
print '...error: output file not specified'
usage()
sys.exit()
i = 1
while i == 1:
if pid <> -1:
i = pid_exists(pid)
if g.verbose: os.system('clear')
## get data ###
freeload, halfload, fullload = 20,20,40#getnodedata()
weekend, phase = gettimedata()
userload = 2#getuserdata()
if g.verbose: print '...total nodes =',2 * (freeload + halfload + 
fullload)
if g.verbose: print '...used nodes =',h