Re: Mysterious Logging Handlers

2016-09-12 Thread Josh English
On Friday, September 9, 2016 at 11:31:13 AM UTC-7, Peter Otten wrote:
> Josh English wrote:
> 
> > 
> > LOG = logging.getLogger('SHIPPING')
> > FORMAT = '%(asctime)-15s %(name)s %(level)-8s %(message)s'
> 
> That should be either levelname or levelno in the format string.

Yeah, I caught that after I tried running the script in a dedicated console in 
Spyder, and my logging worked as expected. I got the format and whole bunch of 
errors about this.


> > 
> > Even more mysterious, after I run the file (in an IDE so I have a REPL
> > afterwards), I have:
> 
> Don't run your code in an IDE. The interaction between your and their code 
> can make debugging harder than necessary.

I suspect the IDE was the problem, because the dedicated console option solved 
the problem.

Thanks,

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Mysterious Logging Handlers

2016-09-12 Thread Josh English
On Friday, September 9, 2016 at 11:29:32 AM UTC-7, John Gordon wrote:
> In <247db0ab-efe7-484b-a418-dd219f68a...@googlegroups.com> Josh English 
> <j...@gmail.com> writes:
> 
> > When I run the scriptI get logging information from only xlreader, not
> > from the main script:
> 
> > DEBUG:XLREADER:Creating Excel Reader
> 
> > This format isn't defined anywhere.
> 
> That is the default logging format; it's used when you haven't supplied any
> format of your own.  The snippet of xlreader.py does not define any format,
> so it seems like that's where it's coming from.
> 
> (This seems pretty straightforward; am I missing something?)
> 

Strange. I don't see that in the docs anywhere. I figure if basicConfig can set 
the level, it can set the formatting, too, for all my loggers, not just one.

I suspect my IDE was interfering somehow. I'm using Sypder with WinPython 
Portable and when I changed my run settings to run in a new dedicated console, 
logging worked an expected.

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Mysterious Logging Handlers

2016-09-09 Thread Josh English
I have a Python script that imports a utility script. Both scripts use logging, 
but the logs don't work as advertised. I'm getting logging output from the 
utility script but none from the main file. Worse, the format of the utility 
script's logs don't match anything I define.

The utility script is called xlreader.py. It has the following lines:

-- begin snippet --
import logging
XLOGGER = logging.getLogger('XLREADER')
-- end snippet --

And it calls XLOGGER.debug(), XLOGGER.error() and XLOGGER.info() but it never 
defines a handler.


The main script imports xlreader but does not reference XLOGGER. It defines its 
own logger:

-- begin snippet --
import logging
import xlreader

LOG = logging.getLogger('SHIPPING')
FORMAT = '%(asctime)-15s %(name)s %(level)-8s %(message)s'
logging.basicConfig(format=FORMAT,level=logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter(FORMAT)
handler.setFormatter(formatter)
LOG.addHandler(handler)
-- end snippet --

I added the logging.basicConfig line but it didn't have any effect. I created 
the second handler separately.

When I run the scriptI get logging information from only xlreader, not from the 
main script:

DEBUG:XLREADER:Creating Excel Reader

This format isn't defined anywhere. T

Even more mysterious, after I run the file (in an IDE so I have a REPL 
afterwards), I have:

>>> XLOGGER
>>> 

>>> XLOGGER.handlers
>>> []

>>> XLOGGER.debug('test')
>>> DEBUG:XLREADER:test

>>> LOG.handlers
>>> [, ]

>>> [h.formatter._fmt for h in LOG.handlers]
>>> ['%(asctime)-15s %(name)s %(level)-8s %(message)s',
 '%(asctime)-15s %(name)s %(level)-8s %(message)s']

How can I track where this formatting is coming from?

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most pythonic way of rotating a circular list to a canonical point

2015-08-01 Thread Josh English
On Saturday, August 1, 2015 at 3:52:25 PM UTC-7, Lukas Barth wrote:
 On Saturday, August 1, 2015 at 11:37:48 PM UTC+2, Emile van Sebille wrote:
  Well, it looks to me that I don't know what a 'canonical rotation' is -- 
 
 That's because it is not defined. ;)
 
 I need a way to rotate one of these lists in a way so that it will produce 
 the same output every time, regardless of what the input rotation was.
 
 Example:
 
 [0,1,2,3,4] = [0,1,2,3,4]
 [2,3,4,0,1] = [0,1,2,3,4]
 [3,4,0,1,2] = [0,1,2,3,4]
 ...
 
 It doesn't have to be [0,1,2,3,4], it can just as well be [2,3,4,1,0], as 
 long as it's always the same.
 
 Did that make it clearer?
 

Is this canoncal rotation different than sorting. I think you mean it to be, 
but these examples all look like sorting to me.

I think the collections.deque object has a rotate method, and rotating through 
the possibilities looking for matches may work, or take any deque, rotate so 
the minimum value is at the first place in the deque, and then compare.

Or am I not understanding what you mean?

Josh

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


Re: Google Code Shutting Down

2015-03-13 Thread Josh English
Thanks for the discussion. I found my original concern was supposedly about 
sourceforge. PyPi, according to a post over on pypubsub-dev that pip installs 
had anecdotal problems with sourcforge-hosted projects.

I guess I wanted some more anecdotes and opinions before I tried moving 
anything. 

I barely understand the SVN I'm using now, so trying to learn a new VCS is a 
tad daunting for this hobbyist.
-- 
https://mail.python.org/mailman/listinfo/python-list


Google Code Shutting Down

2015-03-12 Thread Josh English
I've been hosting Python projects on Google Code, and they're shutting down.

Damn.

What is the recommended replacement for Code Hosting that works reliably with 
PyPi and pip?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to schedule my script?

2014-12-18 Thread Josh English
On Wednesday, December 17, 2014 11:11:11 AM UTC-8, Juan Christian wrote:
 I know about the schedule modules and such but they work in situations like 
 'run this in a X hours/minutes/seconds interval', I already have my code in a 
 while loop with sleep (it's a bit ugly, I'l change to a scheduler soon).
 
 
 What I really want is, for example:
 
 
 24/7/365
 9:00 AM - Start
 11:59 PM - Stop
 
 
 9:00 AM ~ 11:50 PM - Running
 12:00 AM ~ 8:59 AM - Stopped
 
 
 I want my script to start at a given time and stop at another given time, is 
 that possible?

Windows comes with a Task Scheduler but as I'm playing with it, it only seems 
to allow starting a program, but not actually shutting it down. 

I would consider including a timed shutdown in your program or build another 
app, as is suggested below, to send SHUTDOWN commands to running apps. How you 
would link the running processes, I do not fully understand.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Storing instances using jsonpickle

2014-09-15 Thread Josh English
On Wednesday, September 3, 2014 7:19:07 PM UTC-7, Ned Batchelder wrote:

 Typically, you need to decide explicitly on a serialized representation 
 for your data.  Even if it's JSON, you need to decide what that JSON 
 looks like.  Then you need to write code that converts the JSON-able 
 data structure (dict of lists, whatever) into your object.  Often a 
 version number is a good idea so that you have some chance of using old 
 data as your code changes.
 

Right now my cheap workaround is to define a function that saves the instances 
__dict__ using json, and to recreate the object, I create a new object using 
the __init__ method and cycle through the rest of the json keys and apply them 
to the new object using setattr.

It's a quick and dirty hack, but it seems to be working. I do have to make sure 
that everything that lands in the instance's __dict__ is serializable, but 
that's not so tough.

I need to add a version number, though. Good idea, that.


Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: protocol.py, brine.py, and compat.py causing trouble

2014-09-15 Thread Josh English
On Monday, September 15, 2014 12:12:50 PM UTC-7, Emile van Sebille wrote:

 
 That's your clue -- I'd take a close look at the last changes you made a 
 result of which caused this failure and apparent looping.
 It's easy to lay blame on the (whatever) library and look for a root 
 cause there, but I'd first suspect I did something inappropriate as 
 that's much more likely.
 
 
 Emile

I deleted the original post because I had figured out what I had changed. The 
troubleshooting I had done pointed me to those files, which turn out to be part 
of PyScripter, my IDE.

Oddly enough, once I fixed the actual problem (minutes after posting) it still 
makes no sense... I had a list of things that I processed and returned, but 
some refactoring left out filling the return list with anything. I'm not sure 
what happened, except possibly an endless loop.

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: protocol.py, brine.py, and compat.py causing trouble

2014-09-15 Thread Josh English
On Sunday, September 14, 2014 10:59:07 AM UTC-7, Terry Reedy wrote:
 On 9/14/2014 2:44 AM, Josh English wrote:
 

 To the best of my knowledge, protocol.py, brine.py, compat.py, are not 
 part of the stdlib.  What have you installed other than Python? What 
 editor/IDE are you using?  Check your lib/site-packages directory. From 
 a google search, brine.py is a pickle replacement in the rpyc and 
 dreampie (and other) packages.  The other two names are pretty generic 
 and probably common.
 

They turned out to be part of PyScripter, my IDE.

I think the problem was an enless loop, and eventually a memory error, but I'm 
not sure. 

Thanks, 
Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


protocol.py, brine.py, and compat.py causing trouble

2014-09-14 Thread Josh English
I do not know what these three filesare doing, but suddenly they have caught in 
a loop every time I try to run some code.

I grabbed the trace decorator from the python library and this is the last bit 
of the output:


trollvictims.py(129): if self.current_attack:
trollvictims.py(130): print returning, self.current_attack, 
type(self.current_attack)
string(532):  protocol.py(439):  protocol.py(228):  protocol.py(229):  
protocol.py(244):  brine.py(366):  brine.py(368):  brine.py(369):  
brine.py(369):  brine.py(366):  brine.py(367):  brine.py(369):  brine.py(366):  
brine.py(368):  brine.py(369):  brine.py(369):  brine.py(366):  brine.py(367):  
brine.py(369):  brine.py(369):  brine.py(366):  brine.py(368):  brine.py(369):  
brine.py(369):  brine.py(369):  protocol.py(245):  protocol.py(221):  
brine.py(339):  brine.py(340):  brine.py(203):  brine.py(181):  brine.py(182):  
brine.py(184):  brine.py(186):  brine.py(188):  brine.py(189):  brine.py(196):  
brine.py(197):  brine.py(203):  brine.py(108):  brine.py(109):  brine.py(196):  
brine.py(197):  brine.py(203):  brine.py(108):  brine.py(109):  brine.py(196):  
brine.py(197):  brine.py(203):  brine.py(181):  brine.py(182):  brine.py(184):  
brine.py(186):  brine.py(187):  brine.py(196):  brine.py(197):  brine.py(203):  
brine.py(108):  brine.py(109):  brine.py(196):  brine.py(197):  brin
 e.py(203):  brine.py(181):  brine.py(182):  brine.py(184):  brine.py(186):  
brine.py(187):  brine.py(196):  brine.py(197):  brine.py(203):  brine.py(108):  
brine.py(109):  brine.py(196):  brine.py(197):  brine.py(203):  brine.py(181):  
brine.py(182):  brine.py(184):  brine.py(186):  brine.py(188):  brine.py(189):  
brine.py(196):  brine.py(197):  brine.py(203):  brine.py(108):  brine.py(111):  
compat.py(18):  brine.py(112):  brine.py(113):  brine.py(114):  brine.py(196):  
brine.py(197):  brine.py(203):  brine.py(181):  brine.py(182):  brine.py(184):  
brine.py(185):  brine.py(196):  brine.py(197):  brine.py(203):  brine.py(152):  
brine.py(153):  brine.py(155):  brine.py(157):  brine.py(159):  brine.py(161):  
brine.py(163):  brine.py(164):  brine.py(196):  brine.py(196):  brine.py(197):  
brine.py(203):  brine.py(181):  brine.py(182):  brine.py(183):  brine.py(196):  
brine.py(196):  brine.py(196):  brine.py(196):  brine.py(196):  brine.py(341):  
compat.py(18):  protocol.py(222): 

This is where I managed to send a keybord interrupt. I was working just fine, 
tweaking a line, running the code, tweaking a line, running the code, until 
this point.

I'm on Windows 7 using Python 2.7.5. I should upgrade, and will do so, but what 
are these files and why are they suddenly crashing on me?

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Storing instances using jsonpickle

2014-09-03 Thread Josh English
I am using jsonpickle to store instances of an object into separate data files.

If I make any changes to the original class definition of the object, when I 
recreate my stored instances, they are recreated using the original class 
definition, so any new attributes, methods, or properties, are lost.

I think this is happening because JSON is internalizing the class definition, 
ignoring the updates. Is this true? Is it possible to refresh JSON's knowledge 
of a class?

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


Re: Storing instances using jsonpickle

2014-09-03 Thread Josh English
On Wednesday, September 3, 2014 1:53:23 PM UTC-7, Ned Batchelder wrote:

 Pickle (and it looks like jsonpickle) does not invoke the class' 
 __init__ method when it reconstitutes objects.  Your new __init__ is not 
 being run, so new attributes it defines are not being created.

 This is one of the reasons that people avoid pickle: being completely 
 implicit is very handy, but also fragile.
 

I seem to remember having this exact same frustration when I used pickle and 
shelve 15 years ago. I had hoped to have another way around this. I spent over 
a decade trying to make an XML-based database work, in part because of this 
limitation.

Some days I get so frustrated I think the only data structure I should ever use 
is a dictionary.

I suppose to make this sort of thing work, I should look at creating custom 
json encoders and decoders.

Thanks,

Josh

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


Re: os.startfile hanging onto the launched app, or my IDE?

2014-06-09 Thread Josh English
On Saturday, June 7, 2014 1:24:43 PM UTC-7, Tim Golden wrote:

 I'm not 100% sure what your scenario is, but you can certainly help 
 yourself and us by running the same test on the raw interpreter and then 
 under PyScripter to determine if the behaviour is to do with IDLE or 
 with Python itself.
 
 My half-guess is that PyScripter starts a new process to run your code, 
 possibly killing any pre-existing process first. That's if I've 
 understood the situation you're describing.
 
 
 Could you come back with a little more detail? Specifically: whether 
 what you're seeing happens only from within PyScripter, or only not from 
 within PyScripter, or something else?
 

I think you're right about PyScripter controlling the process. I don't run 
scripts through the command line as a matter of practice. 

But I just tried running my script through the command line, with Excel closed, 
and it opened the Excel file just as I expected. Then I went back to the 
command line and ran it again, and it didn't close Excel. It gave me the error 
I was expecting from zipfile not being able to access the file (because it is 
currently open).

I even left it open and ran another script that also creates and launches an 
Excel workbook, and it again did not close Excel.

So this quirk is coming from PyScripter, which is a shame, because I don't 
think it's under development, so it won't be fixed.

Josh

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


os.startfile hanging onto the launched app, or my IDE?

2014-06-06 Thread Josh English
I have been using os.startfile(filepath) to launch files I've created in 
Python, mostly Excel spreadsheets, text files, or PDFs. 

When I run my script from my IDE, the file opens as I expect. But if I go back 
to my script and re-run it, the external program (either Excel, Notepad, or 
Acrobat Reader) closes all windows and restarts the program. This can, 
unfortunately, close other files I am working on and thus I lose all my changes 
to those files.

This is happening on Windows 7. 

I am not sure if it is Python (2.7.5) or my IDE (PyScripter 2.5.3).

It seems like Python or the IDE is keeping track of things created by the 
os.startfile call, but the docs imply this doesn't happen.

Is this a quirk of os.startfile? Is there a cleaner way to get Windows to open 
files without tying back to my program?

Thanks,

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Yet another simple headscratcher

2014-05-30 Thread Josh English
I am trying to whip up a quick matrix class that can handle multiplication.

Should be no problem, except when it fails.

--- Begin
#!/usr/bin/env python
# _*_ coding: utf-8

from operator import mul

class Matrix(object):
Matrix([data])
Data should be a list of equal sized lists.
Defaults to a 2d identity matrix

def __init__(self, data=None):
if data is None:
data = [[1,0], [0,1]]

self.data = []
if isinstance(data, (list, tuple)):
ncols = len(data[0])
for row in data:
if len(row) != ncols:
raise ValueError(Rows are unequal lengths)
self.data.append(row)
self.size = (len(self.data), len(self.data[0]))

def get_row(self, idx):
if (0 = idx  self.size[0]):
return self.data[idx]
else:
raise ValueError(Bad row)

def get_col(self, idx):
if (0 = idx  self.size[1]):
return list(d[idx] for d in self.data)
else:
raise ValueError(Bad column index)

def __mul__(self, other):
if not isinstance(other, (Matrix,int, float)):
raise ValueError(Cannot multiply by given value)
if isinstance(other, (int, float)):
res = []
for row in self.data:
res.append([d*other for d in row])
return Matrix(res)
# left with a matrix
res = zero_matrix(self.size[0], other.size[1])
for i in range(res.size[0]):
for j in range(res.size[1]):
print i, j, self.get_row(i), other.get_col(j),
temp = map(mul, self.get_row(i), other.get_col(j))

print temp,
t = sum(temp)
print t
res.data[i][j] = t
print res.data
return res

def as_string(self):
# return a list of lines that look pretty
stringed =[]
for row in self.data:
stringed.append(map(str, row))
widths = []
for col in range(self.size[1]):
column = [s[col] for s in stringed]
widths.append(max(map(len, column)))
item_format = {:%s}
format_items = [item_format % w for w in widths]
format_string =   .join(format_items)

formatted = [format_string.format(*s) for s in stringed]
return formatted

def zero_matrix(rows, cols):
row = [0] * cols
data = []
for r in range(rows):
data.append(row)

return Matrix(data)

M = Matrix(([1, 0], [0, -1]))

N = M*4


print '\n'.join(M.as_string())
print '-'
print '\n'.join(N.as_string())


print '-'
S = N * M
print '\n'.join(S.as_string())
--- END

For some reason, my output from this is:

1   0
0  -1
-
4   0
0  -4
-
0 0 [4, 0] [1, 0] [4, 0] 4
[[4, 0], [4, 0]]
0 1 [4, 0] [0, -1] [0, 0] 0
[[4, 0], [4, 0]]
1 0 [0, -4] [1, 0] [0, 0] 0
[[0, 0], [0, 0]]
1 1 [0, -4] [0, -1] [0, 4] 4
[[0, 4], [0, 4]]
0  4
0  4


The print lines prove to me that the logic is working, but for some reason, 
assigning the sum to a particular item in a particular row is assigning the 
same row values to every row.

This should be one of those really simple Python things, but for the life of me 
I don't see it.

The first [[4, 0], [4, 0]] is clearly wrong. In each step, this algorithm is 
repeating the row.

Any ideas as to why this is happening?

Python 2.7.5, Windows 7, so nothing exotic.

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Yet another simple headscratcher

2014-05-30 Thread Josh English
Mea culpa, gang.

I found it.

It had absolutely nothing to do with the multiplication.

It was in zero_matrix.

I feel like a fool.

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [ANNC] pybotwar-0.9 : now using pybox2d-2.3b0

2014-05-06 Thread Josh English
I loved RoboWar on my Mac, many ages ago.

I wrote a Stack based language very similar to the one RoboWar used. If you're 
interested, let me know.

Josh English

On Saturday, May 3, 2014 3:28:34 PM UTC-7, Lee Harr wrote:
 pybotwar is a fun and educational game where players
 write computer programs to control simulated robots.
 
 http://pybotwar.googlecode.com/
 


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


Re: Keeping track of things with dictionaries

2014-04-08 Thread Josh English
On Monday, April 7, 2014 9:08:23 PM UTC-7, Chris Angelico wrote:

 That depends on whether calling Brand() unnecessarily is a problem.
 Using setdefault() is handy when you're working with a simple list or
 something, but if calling Brand() is costly, or (worse) if it has side
 effects that you don't want, then you need to use a defaultdict.
 

 I think this is a textbook example of why defaultdict exists, though,
 so I'd be inclined to just use it, rather than going for setdefault :)

Thanks for the clarification.

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


Re: Keeping track of things with dictionaries

2014-04-07 Thread Josh English
On Sunday, April 6, 2014 12:44:13 AM UTC-7, Giuliano Bertoletti wrote:


 obj = brands_seen.get(brandname)
 
 if obj is None:
 obj = Brand()
 brands_seen[brandname] = obj
 
 

Would dict.setdefault() solve this problem? Is there any advantage to 
defaultdict over setdefault()

Josh

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


Re: Switching between cmd.CMD instances

2014-04-03 Thread Josh English
On Wednesday, April 2, 2014 4:33:07 PM UTC-7, Jason Swails wrote:

 From there, you can implement a method interface in which the child Cmd 
 subclasses can call to indicate to BossCmd that do_exit has been called and 
 it should quit after the child's cmdloop returns.  So something like this:

 

Hey, yeah. My previous responses didn't show up, or are delayed, or something.

Thank you, yes. This advice helped and rooting around the source code I 
realized that the Cmd.cmdqueue attribute is the easy way out:

import cmd

class BossCmd(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = 'Boss'
self.exit_called_from_minion = False
self.minions = {}

def add_minion(self, name, cmder):
self.minions[name] = cmder

def do_exit(self, line):
return True

def postloop(self):
print I mean it. I'm done

def postcmd(self, stop, line):
# check if minion called for exit
if self.exit_called_from_minion:
stop = True
return stop

def do_submission(self, line):
if line:
self.minions['submission'].onecmd(line)
else:
self.minions['submission'].cmdloop()

def do_story(self, line):
if line:
self.minions['story'].onecmd(line)
else:
self.minions['story'].cmdloop()

class SubmissionCmd(cmd.Cmd):
def __init__(self, master = None):
cmd.Cmd.__init__(self)
self.prompt = 'Submission'
self.master = master
if self.master:
self.master.add_minion('submission', self)

def do_done(self, line):
return True

def do_exit(self, line):
self.master.exit_called_from_minion = True
return True

def do_story(self, line):
if line:
self.master.minions['story'].onecmd(line)
else:
self.master.cmdqueue.append('story {}'.format(line))
return True


class StoryCmd(cmd.Cmd):
def __init__(self, master=None):
cmd.Cmd.__init__(self)
self.prompt = 'Story'
self.master=master
if self.master:
self.master.add_minion('story', self)

def do_done(self, line):
return True

def do_exit(self, line):
elf.master.exit_called_from_minion = True
return True

def do_submission(self, line):
if line:
self.master.minions['submission'].onecmd(line)
else:
self.master.cmdqueue.append('submission {}'.format(line))
return True

Boss = BossCmd()
Sub = SubmissionCmd(Boss)
Story = StoryCmd(Boss)

Boss.cmdloop()


This gives me a flexible framework to bounce between Cmd instances at will, and 
quit the program from anywhere.

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Switching between cmd.CMD instances

2014-04-01 Thread Josh English
I have a program with several cmd.Cmd instances. I am trying to figure out what 
the best way to organize them should be.

I've got my BossCmd, SubmissionCmd, and StoryCmd objects.

The BossCmd object can start either of the other two, and this module allows 
the user switch back and forth between them. Exiting either of the sub-command 
objects returns back to the BossCmd.

I have defined both a do_done and do_exit method on the sub-commands.

Is it possible to flag BossCmd so when either of the other two process do_exit, 
the BossCmd will also exit?

Josh





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


Oddity using sorted with key

2014-03-11 Thread Josh English
I am running into a strange behavior using the sorted function in Python 2.7. 
The key parameter is not behaving as the docs say it does:

Here is a snippet of code, simplified from my full program:

#begin code
class Thing(object):
def __init__(self, name):
self.name = name

def __repr__(self):
return Thing %s % self.name


stuff = [Thing('a'), Thing('C'), Thing('b'), Thing('2')]
more_stuff = [Thing('d'), Thing('f')]

all_the_stuff = stuff + more_stuff

print list(sorted(all_the_stuff, key=lambda x: x.name.lower))
print list(sorted(all_the_stuff, key=lambda x: x.name.lower()))

# END

The output is:

[Thing d, Thing f, Thing 2, Thing a, Thing b, Thing C]
[Thing 2, Thing a, Thing b, Thing C, Thing d, Thing f]

The second call to sorted works as expected. Just using the method doesn't sort 
properly.

Any ideas why I'm seeing two different results? Especially as the correct form 
is giving me the wrong results?

Josh English
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Oddity using sorted with key

2014-03-11 Thread Josh English
A comprehensive and educational answer, Peter. Thank you.

Josh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Oddity using sorted with key

2014-03-11 Thread Josh English
On Tuesday, March 11, 2014 9:25:29 AM UTC-7, John Gordon wrote:
 
 
 
 
 Why do you say that 'key=lambda x: x.name.lower' is the correct form? That
 
 returns the str.lower() function object, which is a silly thing to sort
 
 on.  Surely you want to sort on the *result* of that function, which is
 
 what your second print does.
 


From http://docs.python.org/2/library/functions.html:

key specifies a function of one argument that is used to extract a comparison 
key from each list element: key=str.lower. The default value is None (compare 
the elements directly).

And from https://wiki.python.org/moin/HowTo/Sorting/:

The value of the key parameter should be a function that takes a single 
argument and returns a key to use for sorting purposes. This technique is fast 
because the key function is called exactly once for each input record.

...

Of course, now that I re-read that, I was mistaking the lambda function with 
the definition of the lambda function. Stupid me.

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


Re: Understanding how is a function evaluated using recursion

2013-09-25 Thread Josh English
On Wednesday, September 25, 2013 4:24:22 PM UTC-7, Arturo B wrote:
 Hi, I'm doing Python exercises and I need to write a function to flat nested 
 lists


 So I know what recursion is, but I don't know how is 

flatten(i)
  
 evaluated, what value does it returns?
 

In this case, flatten always returns a list. When it hits the recursion, it 
calls itself to get another list, that it uses to extend the current list.

Josh

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


Re: Is there a function that applies list of functions to a value?

2013-08-28 Thread Josh English
Reduce tricks are nice, but I prefer clarity sometimes:

def double(x):
return x*2

def add3(x):
return x+3


def compose(*funcs):
for func in funcs:
if not callable(func):
raise ValueError('Must pass callable functions')

def inner(value):
for func in funcs:
value = func(value)
return value

return inner


add_then_double = compose(add3, double)
double_then_add = compose(double, add3)

print add_then_double(1) # prints 8
print double_then_add(1) # prints 5
-- 
http://mail.python.org/mailman/listinfo/python-list


How many times does unittest run each test?

2013-08-10 Thread Josh English
I am working on a library, and adding one feature broke a seemingly unrelated 
feature. As I already had Test Cases written, I decided to try to incorporate 
the logging module into my class, and turn on debugging at the logger before 
the newly-broken test.

Here is an example script:
# -
#!/usr/bin/env python

import logging

def get_vals():
return ['a','b','c']

class SimpleChecker(object):
def __init__(self, callback=None):
self.callback = callback
self.logger = logging.getLogger(self.__class__.__name__)
h = logging.StreamHandler()
f = logging.Formatter(%(name)s - %(levelname)s - %(message)s)
h.setFormatter(f)
self.logger.addHandler(h)

def __call__(self, thing):
self.logger.debug('calling %s' % thing)
vals = self.callback()
return thing in vals

import unittest

class LoaderTC(unittest.TestCase):
def setUp(self):
self.checker = SimpleChecker(get_vals)

def tearDown(self):
del self.checker

def test_callable(self):
self.assertTrue(callable(self.checker),
'loader did not create callable object')
self.assertTrue(callable(self.checker.callback),
'loader did not create callable callback')

self.checker.logger.setLevel(logging.DEBUG)
self.assertTrue(self.checker('q') is False, checker accepted bad 
input)

class NameSpaceTC(unittest.TestCase):
def setUp(self):
self.checker = SimpleChecker(get_vals)

def tearDown(self):
del self.checker

def test_callable(self):
self.assertTrue(callable(self.checker),
'loader did not create callable object')
self.assertTrue(callable(self.checker.callback),
'loader did not create callable callback')

self.checker.logger.setLevel(logging.DEBUG)
self.assertTrue(self.checker('a'), checker did not accept good value)
self.assertFalse(self.checker('f'), checker accepted bad value)

if __name__=='__main__':
unittest.main(verbosity=0)

# ---

When I run this, I get:
SimpleChecker - DEBUG - calling q
SimpleChecker - DEBUG - calling a
SimpleChecker - DEBUG - calling a
SimpleChecker - DEBUG - calling f
SimpleChecker - DEBUG - calling f
--
Ran 2 tests in 0.013s

OK
Exit code:  False

Why am I seeing those extra debugging lines? In the script I'm really trying to 
debug, I see 12-13 debug messages repeated, making actual debugging difficult.

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


Re: How many times does unittest run each test?

2013-08-10 Thread Josh English
On Saturday, August 10, 2013 1:40:43 PM UTC-7, Roy Smith wrote:
 In article f7b24010-f3f4-4e86-b6c4-9ddb503d0...@googlegroups.com,
 
  Josh English  wrote:
 The first thing to do is get this down to some minimal amount of code 
 that demonstrates the problem.
 
 
 
 For example, you drag in the logging module, and do some semi-complex 
 configuration.  Are you SURE your tests are getting run multiple times, 
 or maybe it's just that they're getting LOGGED multiple times.  Tear out 
 all the logging stuff.  Just use a plain print statement.
 
 You've got two different TestCases here.  Does the problem happen with 
 just LoaderTC, or with just NameSpaceTC?
 


Ok, then why would things get logged multiple times? The two test cases 
actually test a loader function that I could strip out, because it wasn't 
relevant. In these cases, the loader was being called with two different 
configurations in the individual setUp methods.

I left them there to show that in LoaderTC, there is one debug log coming from 
SimpleChecker, but in the NameSpaceTC, each debug message is printed twice. If 
I print statements on each test_ method, they are called once. 

As far as stripping down the code, I suppose 15 lines can be culled:

#-
import logging

class SimpleChecker(object):
def __init__(self,):
self.logger = logging.getLogger(self.__class__.__name__)
h = logging.StreamHandler()
f = logging.Formatter(%(name)s - %(levelname)s - %(message)s)
h.setFormatter(f)
self.logger.addHandler(h)

def __call__(self, thing):
self.logger.debug('calling %s' % thing)
return thing in ['a','b','c']

import unittest

class LoaderTC(unittest.TestCase):
def setUp(self):
self.checker = SimpleChecker()

def tearDown(self):
del self.checker

def test_callable(self):
self.checker.logger.setLevel(logging.DEBUG)
self.assertTrue(self.checker('q') is False, checker accepted bad 
input)

class NameSpaceTC(unittest.TestCase):
def setUp(self):
self.checker = SimpleChecker()

def tearDown(self):
del self.checker

def test_callable(self):
print testing callable

self.checker.logger.setLevel(logging.DEBUG)
self.assertTrue(self.checker('a'), checker did not accept good value)
self.assertFalse(self.checker('f'), checker accepted bad value)

if __name__=='__main__':
unittest.main(verbosity=0)

#---
The output:

SimpleChecker - DEBUG - calling q
setting up NameSpace
testing callable
SimpleChecker - DEBUG - calling a
SimpleChecker - DEBUG - calling a
SimpleChecker - DEBUG - calling f
SimpleChecker - DEBUG - calling f
--
Ran 2 tests in 0.014s

OK
Exit code:  False

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


Re: How many times does unittest run each test?

2013-08-10 Thread Josh English
Aha. Thanks, Ned. This is the answer I was looking for.

I use logging in the real classes, and thought that turning setting
the level to logging.DEBUG once was easier than hunting down four
score of print statements.

Josh

On Sat, Aug 10, 2013 at 3:52 PM, Ned Batchelder n...@nedbatchelder.com wrote:
 On 8/10/13 4:40 PM, Roy Smith wrote:

 In article f7b24010-f3f4-4e86-b6c4-9ddb503d0...@googlegroups.com,
   Josh English joshua.r.engl...@gmail.com wrote:

 I am working on a library, and adding one feature broke a seemingly
 unrelated
 feature. As I already had Test Cases written, I decided to try to
 incorporate
 the logging module into my class, and turn on debugging at the logger
 before
 the newly-broken test.

 Here is an example script:

 [followed by 60 lines of code]

 The first thing to do is get this down to some minimal amount of code
 that demonstrates the problem.

 For example, you drag in the logging module, and do some semi-complex
 configuration.  Are you SURE your tests are getting run multiple times,
 or maybe it's just that they're getting LOGGED multiple times.  Tear out
 all the logging stuff.  Just use a plain print statement.

 Roy is right: the problem isn't the tests, it's the logging.  You are
 calling .addHandler in the SimpleChecker.__init__, then you are constructing
 two SimpleCheckers, each of which adds a handler.  In the LoaderTC test,
 you've only constructed one, adding only one handler, so the calling q
 line only appears once.  Then the NameSpaceTC tests runs, constructs another
 SimplerChecker, which adds another handler, so now there are two.  That's
 why the calling a and calling f lines appear twice.

 Move your logging configuration to a place that executes only once.

 Also, btw, you don't need the del self.checker in your tearDown methods:
 the test object is destroyed after each test, so any objects it holds will
 be released after each test with no special action needed on your part.

 --Ned.



-- 
Josh English
joshua.r.engl...@gmail.com
http://www.joshuarenglish.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How many times does unittest run each test?

2013-08-10 Thread Josh English
On Saturday, August 10, 2013 4:14:09 PM UTC-7, Roy Smith wrote:

 
 
 I don't understand the whole SimpleChecker class.  You've created a 
 class, and defined your own __call__(), just so you can check if a 
 string is in a list?  Couldn't this be done much simpler with a plain 
 old function:
 

 def checker(thing):
 print calling %s % thing
 return thing in ['a','b','c']

SimpleCheck is an extremely watered down version of my XML checker 
(https://pypi.python.org/pypi/XMLCheck/0.6.7). I'm working a feature that 
allows the checker to call a function to get acceptable values, instead of 
defining them at the start of the program. I included all of that because it's 
the shape of the script I'm working with.

The real problem was setting additional handlers where they shouldn't be.

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


Re: How many times does unittest run each test?

2013-08-10 Thread Josh English
On Saturday, August 10, 2013 4:21:35 PM UTC-7, Chris Angelico wrote:
 On Sun, Aug 11, 2013 at 12:14 AM, Roy Smith  wrote:
 
  Maybe you've got two different handlers which are both getting the same
  loggingvents and somehow they both end up in your stderr stream.
  Likely?  Maybe not, but if you don't have any logging code in the test
  at all, it becomes impossible.  You can't have a bug in a line of code
  that doesn't exist (yeah, I know, that's a bit of a handwave).
 
 Likely? Very much so, to the extent that it is, if not a FAQ,
 certainly a Not All That Uncommonly Asked Question. So many times
 someone places logging code in something that gets called twice, and
 ends up with two handlers. Personally, I much prefer to debug with
 straight-up 'print' - much less hassle. I'd turn to the logging module
 only if I actually need its functionality (logging to some place other
 than the console, or leaving the log statements in and {en|dis}abling
 them at run-time).

Yes, I definitely need the NUATAQ sheet for Python.

I'm using logging for debugging, because it is pretty straightforward and can 
be activated for a small section of the module. My modules run long (3,000 
lines or so) and finding all those dastardly print statements is a pain, and 
littering my code with if debug: print message clauses. Logging just makes it 
simple.

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


Help with Singleton SafeConfigParser

2012-12-08 Thread Josh English
I am trying to create a Singleton SafeConfigParser object to use across all the 
various scripts in this application. 

I tried a Singleton pattern found on the web:

pre
class Singleton(object):
def __new__(cls):
if not hasattr(cls, '_inst'):
print Creating Singleton Object
cls._inst = super(Singleton, cls).__new__(cls)
else:
print Returning established Singleton

return cls._inst

import ConfigParser

class Options(ConfigParser.SafeConfigParser, Singleton):
def __init__(self):
print Initialing Options
ConfigParser.SafeConfigParser.__init__(self)
self.add_section('test')
/pre

And this doesn't work because it calls the __init__ method every time I create 
the Options object:

pre

O = Options()
print O
O.set('test','start','True')
print from O, O.options('test')


P = Options()
print P
print from P, P.options('test')
/pre

This results in:

pre
Creating Singleton Object
Initialing Options
__main__.Options object at 0x02BF4C50
from O ['start']
Returning established Singleton
Initialing Options
__main__.Options object at 0x02BF4C50
from P []
/pre


I have seen older posts in this group that talk about using modules as 
singletons, but this, unless I misunderstand, requires me to code the entire 
API for SafeConfigParser in the module:

pre
import ConfigParser


class Options(ConfigParser.SafeConfigParser):
def __init__(self):
ConfigParser.SafeConfigParser.__init__(self)
self.readfp(open('defaults.cfg'))
self.read(['local.txt', 'local.cfg'])

def save(self):
with open('local.txt','w') as f:
self.write(f)

__options = Options()

def set(section, name, value):
return self.__options.set(section, name, value)

def options(section):
return self.__options.options

# And so on
/pre

This seems incredibly wasteful, and to introspect my options I get a module, 
not a SafeConfigParser object, so I'm wondering if there is a different way to 
handle this?

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


Re: Help with Singleton SafeConfigParser

2012-12-08 Thread Josh English
On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote:
 
 
 
 Two underscores trigger name mangling only in a class, not in a module. 
 
 Don't try to hide the Options instance:
 
 
 
 # module config.py
 
 import ConfigParser
 
 
 
 class Options(ConfigParser.SafeConfigParser):
 
  ... # as above
 
 
 
 options = Options()
 
 
 
 Then use it elsewhere:
 
 from config import options
 
 options.set(mysection, myoption, myvalue)
 
 
 
 All but the first import will find the module in the cache (sys.modules) and 
 
 therefore the same Options instance will be used. Voilà your no-nonsense 
 
 singleton.


Ah. I was over-thinking again. I couldn't find an example of this anywhere, and 
when I saw the tirades against Singletons they mentioned use modules but, 
well, I haven't had my morning coffee yet. I shouldn't even be trying this sort 
of thing until then.

Thank you for the simple answer.

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


format() not behaving as expected

2012-06-29 Thread Josh English
I have a list of tuples, and usually print them using:

print c,  .join(map(str, list_of_tuples))

This is beginning to feel clunky (but gives me essentially what I want), and I 
thought there was a better, more concise, way to achieve this, so I explored 
the new string format and format() function:

 c = (1,3)
 s = {0[0]}
 print s.format(c)
'1'
 print format(c,s)
Traceback (most recent call last):
  File interactive input, line 1, in module
ValueError: Invalid conversion specification

I'm running *** Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 
bit (Intel)] on win32. ***
(This is actually a PortablePython run on a Windows 7 machine)

Any idea why one form works and the other doesn't? 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: format() not behaving as expected

2012-06-29 Thread Josh English
On Friday, June 29, 2012 10:02:45 AM UTC-7, MRAB wrote:
 
 The .format method accepts multiple arguments, so the placeholders in
 the format string need to specify which argument to format as well as
 how to format it (the format specification after the :).
 
 The format function, on the other hand, accepts only a single
 argument to format, so it needs only the format specification, and
 therefore can't accept subscripting or attributes.
 
   c = foo
   print {0:s}.format(c)
 foo
   format(c, s)
 'foo'

Thank you. That's beginning to make sense to me. If I understand this, 
everything between the braces is the format specification, and the format 
specification doesn't include the braces, right? 

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


Re: format() not behaving as expected

2012-06-29 Thread Josh English
On Friday, June 29, 2012 10:08:20 AM UTC-7, Steven D#39;Aprano wrote:
  c = (1,3)
  s = {0[0]}
  print s.format(c)
  '1'
 
 That's not actually the output copied and pasted. You have quotes around 
 the string, which you don't get if you pass it to the print command.
 

Mea culpa. I typed it in manually because the direct copy and paste was rather 
ugly full of errors because of many haplographies.



  print format(c,s)
  Traceback (most recent call last):
File interactive input, line 1, in module
  ValueError: Invalid conversion specification
 [...]
  Any idea why one form works and the other doesn't?
 
 Because the format built-in function is not the same as the format string 
 method.
...
 
 (Personally, I find the documentation about format to be less than 
 helpful.)
 

Thanks. I think it's coming together.

Either way, this format seems uglier than what I had before, and it's longer to 
type out.  I suppose that's just programmers preference.

Josh

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


Re: Getting lazy with decorators

2012-06-27 Thread Josh English
On Monday, June 25, 2012 11:57:39 PM UTC-7, Peter Otten wrote:
  
  There is nothing in the documentation (that I have found) that points to
  this solution. 
 
 That's because I invented it.
 

Oh bother. The lines I completely overlooked were in your __getattr__ override.

Boy is my face red.

On further experimentation, adding a do_xxx command without the decorator still 
works...ish. The undecorated do_xxx is still considered to have a help 
function, and it prints the raw docstring (instead of using the show_help 
method to clean it up).

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


Re: Getting lazy with decorators

2012-06-25 Thread Josh English
On Sunday, June 24, 2012 1:07:45 AM UTC-7, Peter Otten wrote:
 
 You cannot access a class instance because even the class itself doesn't 
 exist yet. You could get hold of the class namespace with sys._getframe(),
 
 def add_help(f):
 exec \
 def help_%s(self):
 f = getattr(self, %r)
 self.show_help(f)
  % (f.__name__[3:], f.__name__) in sys._getframe(1).f_locals
 return f
 
 but here's a simpler approach:
 
 import cmd
 
 def add_help(f):
 def help(self):
 self.show_help(f)
 f.help = help
 return f
 
 
 class BaseCmd(cmd.Cmd):
 def __init__(self, *args, **kwargs):
 cmd.Cmd.__init__(self, *args, **kwargs)
 
 def show_help(self, func):
 print \n.join((line.strip() for line in 
 func.__doc__.splitlines()))
 
 def __getattr__(self, name):
 if name.startswith(help_):
 helpfunc = getattr(self, do_ + name[5:]).help
 setattr(self.__class__, name, helpfunc)
 return getattr(self, name)
 raise AttributeError
 
 @add_help
 def do_done(self, line):
 done
 Quits this and goes to higher level or quits the application.
 I mean, what else do you expect?
 
 return True
 
 if __name__=='__main__':
 c = BaseCmd()
 c.cmdloop()


Okay. If I understand this, you are adding a help attribute to the class 
method. The help attribute is itself a function. 

There is nothing in the documentation (that I have found) that points to this 
solution. Even after reading the do_help method in the cmd.Cmd source, I don't 
see this as working.

Yet it works.

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


Getting lazy with decorators

2012-06-23 Thread Josh English
I'm creating a cmd.Cmd class, and I have developed a helper method to easily 
handle help_xxx methods.

I'm trying to figure out if there is an even lazier way I could do this with 
decorators.

Here is the code:
*
import cmd


def add_help(func):
if not hasattr(func, 'im_class'):
return func #probably should raise an error
cls = func.im_class
setattr(cls, func.im_func.__name__.replace(do,help), None)

return func


class BaseCmd(cmd.Cmd):
def __init__(self, *args, **kwargs):
cmd.Cmd.__init__(self, *args, **kwargs)

def show_help(self, func):
print \n.join((line.strip() for line in func.__doc__.splitlines()))

@add_help
def do_done(self, line):
done
Quits this and goes to higher level or quits the application.
I mean, what else do you expect?

return True

if __name__=='__main__':
c = BaseCmd()

print c.help_done


* 

This generates AttributeError: BaseCmd instance has no attribute 'help_done'

The show_help method is the shortcut I want to use (I'm pretty sure it's from 
Doug Hellman's site). I'm wondering if it's possible to use a decorator such as 
add_help to automatically create the appropriate help_xxx function.

In the decorator, I can get the function and the name of the class, but I can't 
find the instance of  the class that the method is attached to. Maybe this is 
just one step of lazy too far.


Am I right in thinking that I can't do this? There is no way to access the 
class instance from the method?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyTextile Question

2012-05-14 Thread Josh English
On Monday, May 7, 2012 6:13:28 AM UTC-7, dinkyp...@gmail.com wrote:
 
 Below is a test script that shows one way I've dealt with this issue in the 
 past by reformatting paragraphs to remove embedded line breaks.  YMMV.
 
 import re, textile
 ...

Wow. Thank you. This works. I'm trying to figure what that regular expression 
does, but I swear it's copying the lines twice. (Clearly, I don't speak re.)

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


PyTextile Question

2012-05-03 Thread Josh English
I am working with an XML database and have large chunks of text in certain 
child and grandchildren nodes.

Because I consider well-formed XML to wrap at 70 characters and indent 
children, I end up with a lot of extra white space in the node.text string. (I 
parse with ElementTree.)

I thought about using pytextile to convert this text to HTML for a nicer 
display option, using a wx.HTMLWindow (I don't need much in the way of fancy 
HTML for this application.)

However, when I convert my multiple-paragraph text object with textile, my 
original line breaks are preserved. Since I'm going to HTML, I d'nt want my 
line breaks preserved.

Example (may be munged, formatting-wise):
pre
action
   descriptionThis is a long multi-line description
with several paragraphs and hopefully, eventually,
proper HTML P-tags.

This is a new paragraph. It should be surrounded
by its own P-tag.

Hopefully (again), I won't have a bunch of unwanted
BR tags thrown in.
/description
/action
/pre

I've tried several ways of pre-processing the text in the node, but pytextile 
still gives me line breaks.

Any suggestions? Is there a good tutorial for PyTextile that I haven't found?

Thanks.

Josh

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


Re: xlrd 0.7.4 released!

2012-04-03 Thread Josh English
Maybe it's just me, but I tried to upgrade my previous versions of xlrd, 
xlwt, an xlutils and now some of the modules aren't loading properly.

I am currently using Portable Python 2.7 at this workstation.

I ran easy_install --upgrade xlrd and the result said it had updated. If 
I try to update again, it says it's already there.

When I try to import xlrd, I get an error

IOError: [Errno 2] No such file or directory: 
'C:\\Users\\josh\\Desktop\\Portable 
Python\\App\\lib\\site-packages\\xlrd-0.7.5-py2.7.egg\\xlrd\\version.txt'

I also noticed that in my Site Packages folder, I 
have xlrd-0.7.1-py2.7-win32.egg as an egg file, but xlrd-0.7.5-py2.7.egg as 
a folder.

This in on a Windows 7 machine.

I didn't think EGG files could be folders. Is there a change in the way the 
EGG file was meant to be distributed?

Josh English
Confused Data Geek

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


Re: Udacity CS 101

2012-03-02 Thread Josh English
On Monday, February 27, 2012 6:37:25 PM UTC-8, Ray Clark wrote:
 
 You have to remember that this course assumes no prior computer
 programming knowledge.
 
 I agree, it is starting out very basic.  Give it some more time.
 These guys have PhDs from MIT and/or have taught at Stanford.  They
 know what they are doing.

It seems to me that they're not starting with good Python practices, really, or 
even the terminology I'd expect.
-- 
http://mail.python.org/mailman/listinfo/python-list


Udacity CS 101

2012-02-25 Thread Josh English
Has anyone here looked at Udacity's open CS101 course 
(http://www.udacity.com/overview/Course/cs101) that started this week? The goal 
of the seven week course is to build a web crawler.

So far, I'm not impressed with the speed or content of the course. I was 
wondering what anyone here may think of it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Deleting files on a shared server

2011-10-06 Thread Josh English
This is a follow-up to some questions I posted a month or two ago. I have two 
programs running on various Windows XP boxes, sharing several resource files on 
a Windows 2003 server. It's a mapped drive on the workstations to a shared 
folder.

I am using a locking utility that works by creating .lock files in the shared 
folder and deleting those files when the program is done with them.

To delete the files, I am using os.unlink.

One lock file refuses to disappear, even though I have code at both application 
startup and shutdown (on the OnInit and OnExit methods to the wxPython 
Application object) that hunts down .lock files and deletes them.

Is there a better command than os.unlink to delete a file on Windows 2003 
server?

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


Re: Deleting files on a shared server

2011-10-06 Thread Josh English
The problem shows up when the application starts. It tries to read the file but 
the lock mechanism times out because the file is still around after the last 
time the application ran.

It's a wxPython program. The code to unlink the .lock files is run in the 
wxApp.OnInit method (before any code to open these resources) and in the 
wxApp.OnExit method. I know both of these methods are being called.

The locking mechanism I am using can be found at 
http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/

The clearing code is:

import os
import fnmatch
files = fnmatch.filter(os.listdir(self.Options.DataDir), *.lock)
for f in files:
os.unlink(os.path.abspath(os.path.join(self.Options.DataDir, f)))

The Options object has a property called DataDir.

MMM...

Now that I sit down to test abso-frikkin'-lutely that this code does what I 
want it to do, it appears not to do this at all. The files list I build doesn't 
work and returns an empty list. I may have found a workaround using glob.

Now my face is red. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Understanding .pth files

2011-08-27 Thread Josh English
I am developing a library for Python 2.7. I'm on Windows XP. I am also learning 
the proper way to do this (per PyPi) but not in a linear fashion: I've built  
a prototype for the library, created my setup script, and run the install to 
make sure I had that bit working properly.

Now I'm continuing to develop the library alongside my examples and 
applications that use this library.

The source is at c:\Dev\XmlDB.
The installed package in in c:\Python27\lib\site-packages\xmldb\

According to the docs, I should be able to put a file in the site-packages 
directory called xmldb.pth pointing anywhere else on my drive to include the 
package. I'd like to use this to direct Python to include the version in the 
dev folder and not the site-packages folder.

(Otherwise I have my dev folder, but end up doing actual library development in 
the site-packages folder)

So my C:\Python27\lib\site-packages\xmldb.pth file has one line:

c:\dev\XmlDB\xmldb

(I've tried the slashes the other way, too, but it doesn't seem to work).

Is the only solution to delete the installed library and add the dev folder to 
my site.py file?

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


Understanding .pth in site-packages

2011-08-27 Thread Josh English
(This may be a shortened double post)

I have a development version of a library in c:\dev\XmlDB\xmldb

After testing the setup script I also have c:\python27\lib\site-packages\xmldb

Now I'm continuing to develop it and simultaneously building an application 
with it.

I thought I could plug into my site-packages directory a file called xmldb.pth 
with:

c:\dev\XmlDB\xmldb

which should redirect import statements to the development version of the 
library.

This doesn't seem to work.

Is there a better way to redirect import statements without messing with the 
system path or the PYTHONPATH variable?

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


Re: Understanding .pth in site-packages

2011-08-27 Thread Josh English
Philip,

Yes, the proper path should be c:\dev\XmlDB, which has the setup.py, xmldb 
subfolder, the docs subfolder, and example subfolder, and the other text files 
proscribed by the package development folder.

I could only get it to work, though, by renaming the xmldb folder in the 
site-packages directory, and deleting the egg file created in the site-packages 
directory. 

Why the egg file, which doesn't list any paths, would interfere I do not know.

But with those changes, the xmldb.pth file is being read.

So I think the preferred search order is:

1. a folder in the site-packages directory
2. an Egg file (still unsure why)
3. A .pth file

It's a strange juju that I haven't figured out yet. 

Thanks for the hint.

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


Re: Understanding .pth in site-packages

2011-08-27 Thread Josh English
I have .egg files in my system path. The Egg file created by my setup script 
doesn't include anything but the introductory text. If I open other eggs I see 
the zipped data, but not for my own files.

Is having a zipped egg file any faster than a regular package? or does it just 
prevent people from seeing the code?

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


Re: Understanding .pth in site-packages

2011-08-27 Thread Josh English
When I run: os.listdir('c:\Python27\lib\site-packages') I get the contents in 
order, so the folders come before .pth files (as nothing comes before 
something.) I would guess Python is using os.listdir. Why wouldn't it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Understanding .pth in site-packages

2011-08-27 Thread Josh English
OKB,

The setup.py script created the egg, but not the .pth file. I created that 
myself.

Thank you for clarifying about how .pth works. I know redirect imports was 
the wrong phrase, but it worked in my head at the time. It appears, at least on 
my system, that Python will find site-packages/foo before it finds and reads 
site-packages/foo.pth.

At least this solution gives me a way to develop my libraries outside of 
site-packages.

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


Re: Looking for general advice on complex program

2011-07-18 Thread Josh English
That would be one of mine, probably.

http://code.google.com/p/pyxmlcheck/

It's an old version. I haven't updated it in a while.

And while my program worked fine at home, my test environment gave me some 
grief. Apparently the lock files are being deleted properly. I have a few ideas 
about that, but I'm away from my code right now.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for general advice on complex program

2011-07-17 Thread Josh English
Chris,

I got my solution working, at least on my local machine. I'm trying to bundle 
it for testing on location.

I've thought about the server-client model and one day I may have the guts to 
tackle that, but I don't think it's this project. 

Sadly, I'm the type of guy who almost has to re-invent the wheel. When I 
started XML processing, it was on an old computer and I couldn't get things 
like lxml to work, or understand the ones I did manage to install. To fully 
understand XML processing and validating, I had to write my own XML validation 
utility.

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


Re: Looking for general advice on complex program

2011-07-16 Thread Josh English
Cameron,

You use a directory as lock mechanism. I think I get how that works.
When you're done manipulating the file, do you just remove the director?

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


Re: Looking for general advice on complex program

2011-07-16 Thread Josh English
I found a FileLock (lost the link and it's not in the code) that uses context 
managers to create a .lock file in the same directory of the file. It uses 
os.unlink to delete the .lock file but I don't know if this deletes the file or 
just removes it from the directory and leaves the memory filled. If it does, I 
don't know if the OS will overwrite that memory. I'm afraid of taking up lots 
of space that I don't need with this program.

The docs indicate that Unix recovers the memory, but I'm not sure about Windows.

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


Re: Looking for general advice on complex program

2011-07-16 Thread Josh English
Chris,

Thank you for spelling this out. I thought about this as a solution but I don't 
have the skills to create this server application, and I don't know if the 
target network can handle this request. They can see files on a shared drive. 
They can't see each other's computers on the network, and I don't know if I can 
make a socket work.

I do know they put an internal wiki in the system, and if I could figure out 
how to do these requests over HTTP, I may try that, but that seems to be the 
wrong solution.

(Yes, I am a programming neophyte way over my head here.)


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


Looking for general advice on complex program

2011-07-15 Thread Josh English
Maybe not to the gurus here, but for me, this is a complex problem and I want 
to make sure I understand the real problem.

All of this is in Python 2.7 and wxPython

I have several XML files on a shared drive.
I have applications on other machines that need to access this XML file.
These applications need to read and write to this file.
These applications need to a) be alerted to a file change, or b) monitor the 
file for changes and regular intervals.

In my code, I have XManager classes (using a Singleton pattern) that reads each 
XML file into a tree (using ElementTree). The XManager class can read the file, 
make changes to the tree, and write the file as needed.

Now I'm expanding to the multiple application level, and I think I understand 
what I need to do, and I'm unsure about the exact processes.

I've been trying to have the XManagers check periodically if the XML file they 
monitor has changed. Since I don't want to mess up the GUI with constant 
hanging, I think I can use the thread or threading modules to create a 
recurring timed check, and then I need a separate check to see if the file is 
in use before reading or writing. 

I also need, I think, to have a way to check if the GUI is editing a node 
before the XManager reads the file, so the thread needs to be interrupted, or 
paused, because I don't know if threads would stop if a wxDialog is being show 
modally or not.

So, am I on the right track here? 

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


Re: frequency of values in a field

2011-02-09 Thread Josh English
On Wednesday, February 9, 2011 9:52:27 AM UTC-8, noydb wrote:

 
 So it seems the idea is to add all the records in the particular field
 of interest into a list (record).  How does one do this in pure
 Python?
 Normally in my work with gis/arcgis sw, I would do a search cursor on
 the DBF file and add each value in the particular field into a list
 (to populate records above).  Something like:
 

I'm not sure I understand what you mean by pure Python. Decimal and 
collections are part of the standard library. 

If you mean use no imports whatsoever, the code would look like:

counts = {}
for thing in long_list:
  key = make_key(thing)
  if key in counts:
counts[key] += 1
  else:
counts[key] = 1

But using a few imports is cleaner and just as easy to troubleshoot.


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


Re: frequency of values in a field

2011-02-09 Thread Josh English
On Wednesday, February 9, 2011 10:34:12 AM UTC-8, noydb wrote:

 
 How do you add all the records in the particular field of interest
 into long_list?

Sorry to be unclear. In both cases I was tossing out pseudo-code, as I am not 
familiar with the arggisscripting module. long_list is a list with the records 
you want, or an iterator that goes through the records. The get_key function 
does whatever you need it to do to get the key by which you are counting.

 Andreas offers a nice pythonic solution in this thread.

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


Re: frequency of values in a field

2011-02-08 Thread Josh English
You could try a collections.defaultdict object with an integer as the startup 
value:

counts = collections.defaultdict(int)
for thing in long_list:
  counts[get_last_two_digits(thing)] += 1

This assumes get_last_two_digits is the function that provides the key you want 
to count by. I'm not sure what you want get_last_two_digits to look like from 
your post, or how you would get the long_list, which is just an iterator 
through your records.

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


Re: Remove whitespaces and line breaks in a XML file

2011-02-07 Thread Josh English
I found the code posted at 

http://infix.se/2007/02/06/gentlemen-indent-your-xml

quite helpful in turning my xml into human-readable structures. It works
best for XML-Data.

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


Re: Which coding style is better? public API or private method inside class definition

2011-01-04 Thread Josh English
I think it depends on where you're willing to deal with changes. As it stands, 
if you make changes to get_all that will effect the way get_even works. If 
there's a logical chain that your code needs to follow and such changes would 
be acceptable, use them, because it's easier to change one thing once instead 
of one thing twice.


In general, I try to code in a way that will reduce the number of places I'll 
have to change or refactor.

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


Re: Problem inserting an element where I want it using lxml

2011-01-04 Thread Josh English
Here's a trimmed down version of how I did this (using ElementTree standalone, 
but the API should be the same)
This is from a class definition and the _elem attribute is a link to an 
ElementTree.Element object.

...
def _add_elem(self, tagName, text, attrib ={}):
_add_elem(tagName, text, attrib={})
Adds a child element in the appropriate place in the tree.
Raises an IndexError if the checker does not allow an addition child of 
tagName.

last_child = None
for child in self._elem.findall('.//%s' % tagName):
last_child = child
 
if last_child is None:
new_child = ET.SubElement(self._elem, tagName, attrib)
else:
new_child = ET.Element(tagName, attrib)
self._elem.insert(self._elem._children.index(last_child)+1, 
new_child)
new_child.text=str(text)

return new_child

I don't think you need to count the instances of the bingo node (unless you 
want to prevent too many from being added in).

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


Re: Problem inserting an element where I want it using lxml

2011-01-04 Thread Josh English
Oh, and I usually use a separate function to indent my xml for readability when 
I need to actually look at the xml. It's easier than trying to change the 
element in situ and make it look nice.

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


Re: Multiple instances and wrong parental links

2011-01-02 Thread Josh English
Chas,

Thanks. The self in the setattr statement works sometimes, but what I'm 
adding most of the time is a property, and if I don't use the class when 
setting a property, nothing works. The property is returned instead of the 
value of the function the property returns. (yeah, it's complicated).

This is the same project I asked about at 
https://groups.google.com/d/topic/comp.lang.python/K9PinAbuCJk/discussion.

That's why this simple sample looks so incredibly complicated. I'm coding on 
the far edge of my learning curve.

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


Re: Multiple instances and wrong parental links

2011-01-02 Thread Josh English
Steven, 

This was simplified. The complete story is, well, tedious:

I have created a set of XML Validation and Normalization classes. With them I 
can define my data structures and make sure my data is good. These 
complications have come in wanting to take this tool and create a GUI (in 
wxPython) around these objects. Okay, really these complication come from me 
generalizing the problem, probably one step too far. I want a tool to take my 
structural definition and create the GUI. To use some of the easy tools like 
Validators that transfer data to the GUI and back, I need an interface.

I tried creating validators that went straight to the element, but that got 
complicated. 

Then I created a map from a dictionary to an element (as defined by the XML 
validation tool) and back again. This also bogged down and fixing one problem 
undid all the other progress I had made.

So this is my third attempt: create a Python object around the XML definition.

I've looked at pyxb. It doesn't make any sense to me. I looked at pysxer, that 
made even less sense. Hence, I try it on my own.

The Element List isn't a list, it has the same interface as a list, but always 
goes back to the original XML element (an elementree.Element object in this 
case.) 

Insanely complicated and just beyond my comprehension, I fear. I haven't found 
an easier way to wrap an object around these XML validators of mine.

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


Multiple instances and wrong parental links

2011-01-01 Thread Josh English
I have hit yet another wall. I am dynamically creating a class and then 
creating instances of that class. The class relies on a second class to store a 
list of objects. (This is simplified from the the original by a factor of about 
20. The real program is trying to create a Python object around an XML 
definition object.)

Here's the code:

## OPTION ONE for class: ElementList
### Not really a list, but a wrapper that behaves like a list
class ElementList(object):
def __init__(self, parent, name):
self._parent = parent
self._name = name

def MakeWrapper(checker, _addNameAsAttribute = False ):

## OPTION TWO for class: ElementList
class Wrap(object):

## OPTION THREE for class: Elementlist

def __init__(self, name):
self._name = name
setattr(Wrap, 'stuff', ElementList(self, 'test'))

Wrap.__name__= checker.title()

return Wrap

if __name__ == '__main__':

Dude = MakeWrapper('Dude')
print Dude
d1 = Dude('Josh')
print d1, d1.stuff

# creating the second instance changes the behavior of the subclass
d2 = Dude('Ben')
print d2, d2.stuff
print d1.stuff._parent
print d2.stuff._parent

#creating a third instance changes the behavior of all the subclasses
d3 = Dude('David')
print d3, d3.stuff
print d1.stuff._parent, d2.stuff._parent, d3.stuff._parent

## END CODE

And here is the output:

 
class '__main__.Dude'
__main__.Dude object at 0x00DFB930 __main__.ElementList object at 0x00DFB950
__main__.Dude object at 0x00DFB730 __main__.ElementList object at 0x00DFB770
__main__.Dude object at 0x00DFB730
__main__.Dude object at 0x00DFB730
__main__.Dude object at 0x00DFB870 __main__.ElementList object at 0x00DFB9B0
__main__.Dude object at 0x00DFB870 __main__.Dude object at 0x00DFB870 
__main__.Dude object at 0x00DFB870

The 'stuff' attribute is an ElementList object linked back to the parent 
instance, but every time I create an instance, every instance's 'stuff' links 
back to the last instance created.

I'm not sure why this is happening, or how to prevent it.

Any suggestions?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-29 Thread Josh English
This whole conversation got interesting, so I thought I'd run some
speed tests:

The code:
from timeit import Timer

def is_palindrome_recursive(s):
if len(s) = 1:
return True
if s[0] != s[-1]:
return False
else:
return is_palindrome(s[1:-1])

def is_palindrome_slice(s):
return s == s[::-1]

def is_palindrome_list(s):
l = list(s)
l.reverse()
return s == ''.join(l)

def is_palindrome_reversed(s):
return s == ''.join(reversed(s))

t = Timer(is_palindrome_recursive('madamimadam'), from __main__
import is_palindrome_recursive)
print is_palindrome_recursive, min(t.repeat())

t = Timer(is_palindrome_slice('madamimadam'), from __main__ import
is_palindrome_slice)
print is_palindrome_slice, min(t.repeat())

t = Timer(is_palindrome_list('madamimadam'), from __main__ import
is_palindrome_list)
print is_palindrome_list, min(t.repeat())

t = Timer(is_palindrome_reversed('madamimadam'), from __main__
import is_palindrome_reversed)
print is_palindrome_reversed, min(t.repeat())

The results:
is_palindrome_recursive 6.32680866827
is_palindrome_slice 1.23618350114
is_palindrome_list 4.60104846653
is_palindrome_reversed 5.99355296513

The slice method is uglier, I have to admit, but it's the fastest of
these four on my machine.

Josh

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


Re: palindrome iteration

2010-08-29 Thread Josh English
I have no idea. That's a lower level of programming than I'm used to
dealing with.

Josh

(I also only tried the one value. Had I tried with other strings that
would fail the test, some
functions may have performed better.)

On Aug 29, 2:19 am, Matteo Landi landima...@gmail.com wrote:
 Well, I tried the also the solution posted above (recursive w/o
 slicing and iterative), and I discovered they were the slowest..

 is_palindrome_recursive 2.68151649808
 is_palindrome_slice 0.44510699381
 is_palindrome_list 1.93861944217
 is_palindrome_reversed 3.28969831976
 is_palindrome_recursive_no_slicing 6.78929775328
 is_palindrome_iterative 4.88826141315

 Nothing to say about the iterative function, but the benchmark of the
 recursive was unexpected, at least for my point of view: do you think
 it is due to the try/except overhead?

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


Re: pyxser-1.5r --- Python Object to XML serializer/deserializer

2010-08-28 Thread Josh English
On Aug 26, 10:02 pm, Stefan Behnel stefan...@behnel.de wrote:
 Josh English, 27.08.2010 01:30:

  solve a lot of the problems I'm running into in my own attempt to
  build a python Class implementation of an XML Validation object.

 How would object serialisation help here?



I'm running into the same problem in a lot of projects I'm working on.
I can't decide
one the best way to serialize instances of classes. I want to be able
to store these
instances in a human-readable and editable format that I can reload
back into their
objects.

The XML validation tool requires the rules of the XML to be written
out in Python
as classes and instances. I would love to have a consistent way to
write the definition
file and load it. For example, Relax NG can be written out, the rules
loaded into a parser,
and actual XML data can be validated against it.

Since I started in Python, I want that bridge back to serialization.

In this project, and others, subclassing plays a big role in using the
code, so I need
a reliable way of writing a definition, loading it into the proper
subclass, and running
with those subclasses.

I am familiar with XML, but I've played with YAML and may try that.
I'm hesitant to use YAML
because it requires that I add the YAML.object.

I even wrote a program to store data similar to GEDCOM format, which
seemed the simplest from
a user's perspective.

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


Re: Tag parsing in python

2010-08-28 Thread Josh English
On Aug 28, 9:14 am, agnibhu dee...@gmail.com wrote:
 Hi all,

 I'm a newbie in python. I'm trying to create a library for parsing
 certain keywords.
 For example say I've key words like abc: bcd: cde: like that... So the
 user may use like
 abc: How are you bcd: I'm fine cde: ok

 So I've to extract the How are you and I'm fine and ok..and
 assign them to abc:, bcd: and cde: respectively.. There may be
 combination of keyowords introduced in future. like abc: xy: How are
 you
 So new keywords qualifying the other keywords so on..
 So I would like to know the python way of doing this. Is there any
 library already existing for making my work easier. ?

 ~
 Agnibhu

Have you looked at pyparsing? (http://pyparsing.wikispaces.com/) It
may
be possible to use that library to do this.

Josh



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


Re: pyxser-1.5r --- Python Object to XML serializer/deserializer

2010-08-26 Thread Josh English


It looks nice, but it's a shame it doesn't work on Windows. This
could
solve a lot of the problems I'm running into in my own attempt to
build a python Class implementation of an XML Validation object.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Namespace problem?

2010-07-02 Thread Josh English
On Jul 1, 3:30 pm, Rhodri James rho...@wildebst.demon.co.uk wrote:


 If this is a version of your code that actually fails when you run it  
 (rather than being another artistic interpretation of a photograph of your  
 code :-), then I'd go with Matt's analysis.  This will give you a  
 NameError for fws_last_col if tracker.hasFWS() happens to return False for  
 all students.


Actually, tracker.hasFWS() was returning False for an entirely
different reason that I didn't expect until I had the script dump all
the information it was processing. Thanks for the clue. I added one
parameter to one function elsewhere and the thing seems to be working
again.

Thanks for the help.

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


Namespace problem?

2010-07-01 Thread Josh English
I have a script that generates a report from a bunch of data I've been
collecting for the past year. I ran the script, successfully, for
several weeks on test runs and creating more detailed reports.

Today (back from vacation) and the script doesn't work. It's giving me
a name error.

I'm running python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.
1310 32 bit (Intel)]. (Upgrading is not a possibility right now.
Eventually we'll be upgraded top Windows 7.)

Here is the reduced code:

fws_first_col = 6

for student in range(32):

if True:
idx = fws_first_col

for _month in range(12):
idx += 2
fws_last_col = idx


print fws_last_col

I get:

NameError: name 'fws_last_col' is not defined

This snippet of code works on it's own, but not in the main script. I
know it looks funny, but this should preserve the appropriate nesting
of things.

Where else could I look to find this problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Namespace problem?

2010-07-01 Thread Josh English
On Jul 1, 2:50 pm, Matt McCredie mccre...@gmail.com wrote:

 That doesn't give me enough information to help you with the issue. In general
 you need to provide enough code to reproduce the failure, not some modified
 version that doesn't fail. My guess is that the if True is actually 
 something
 else, and it isn't being interpreted as True. As such, fws_last_col never
 gets assigned, and thus never gets created. You can fix that by assigning
 fws_last_col to an appropriate default value before the for loop. But what do 
 I
 know, that is just a guess.

 Matt

This is the code snippet with more context, but it isn't runnable
without creating a bunch of dummy objects:
# data is a dictionary. The keys are student names, the values are
Tracker object.
# The tracker class defines the method hasFWS() which returns a
boolean.
# _monthnumbers is a list: [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
I just cut out the code that doesn't have anything to do the variable
fws_last_col

fws_first_col = 6

for student in sorted(data.keys() ):
#~ print student

tracker = data[student]

if tracker.hasFWS():

idx = fws_first_col


for _month in iter(_monthnumbers):
idx += 2
fws_last_col = idx

fws_month_count_col = idx + 4
fwsrow += 1

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


Re: Python Line Intersection

2010-04-09 Thread Josh English
On Apr 9, 8:36 am, Emile van Sebille em...@fenx.com wrote:
 On 4/9/2010 8:04 AM Peyman Askari said...

  Hello

  This is partly Python related, although it might end up being more math 
  related.

  I am using PyGTK (GUI builder for Python) and I need to find the 
  intersection point for two lines. It is easy to do, even if you only have 
  the four points describing line segments 
  (http://www.maths.abdn.ac.uk/~igc/tch/eg1006/notes/node23.html). However, 
  it requires that you solve for two equations. How can I do this in Python, 
  either solve equations, or calculating intersection points some other way?

 I needed this a couple years back and used parts of pycad without much
 difficulty.

 Emile

You can also do this by creating a Python representation of a line. I
did it by creating a vector class (using named tuple) and a line class
that stored a point and a direction vector. From there, you can find
the intersection of two lines (or a line with a circle, triangle, etc.
through some mathematical jiggery pokery using dot products.

If anyone want to see it I can post the code when I get home
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamic Class Creation

2010-03-17 Thread Josh English
Chris,

Thanks. This worked for the attributes, but I think the tactic is
still misleading. There are child elements I can't quite determine how
to deal with:

market code='anlg' tier='ProMarket' mail='True'
title field=prefAnalog Science Fiction and Fact/title
nicknameAnalog/nickname
keywordScience Fiction/keyword
keywordFirst Contact/keyword
keywordHard Science Fiction/keyword
address
attnlineStanley Schmidt, Editor/attnline
address1267 Broadway, 4th Floor/address1
address2New York, NY 10007-2352/address2
/address
websitehttp://www.analogsf.com/website
/market

A child element with text and an attribute or two, for example, pose a
problem. I can call Market.title but should I try Market.title.field
or Market.title_field.

Multiple elements, such as keywords, are allowed in xml but harder to
map to the object. I don't know if I want to go create a list and
methods for accessing those keywords as a list, or redefine the rules
of my XML to not allow multiple child elements with the same tag. I
can't decide.

Then the address is a bit of a bear.

In short, I have to figure out the whole object interface to the XML
and how I want that to work.

Thanks for the suggestion. It is undeniably clever.

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


Dynamic Class Creation

2010-03-16 Thread Josh English
I have a large program with lots of data stored in XML. I'm upgrading
my GUI to use ObjectListView, but with my data in XML, I don't have
regular objects to interact with the OLV. I do have an XML validator
that defines the structure of the XML elements, and I'm trying to
dynamically create a class to wrap the XML element.

So, an element like:

market code=WotF
titleWriters of the Future/title
/market

I want to create a class like:

class Market(object):
def __init__(self, elem):
self._elem = elem

def get_code(self):
return self._elem.get('code')

def set_code(self, value):
self._elem.set('code', value)

def get_title(self):
return self._elem.find('title').text

def set_title(self, value):
node = self._elem.find('title')
node.text = value

Naturally, I don't want to hand code this for every interface but
would like to create them dynamically. (The laziness of programming, I
guess.)

I have tried several solutions that I found on various forums, but
they are all several years old.

Questions:

What's the best way to create these helper methods?

How can I attach them to the class and have it run?

I have had a few solutions work where I had a class with three methods
(get_code, get_tier, get_mail) but they all return the same value, not
the individual values.


Josh English




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


Re: datetime string conversion error

2010-03-16 Thread Josh English
On Mar 16, 11:56 am, Jordan Apgar twistedphr...@gmail.com wrote:

 here's what I'm doing:
 date = 2010-03-16 14:46:38.409137
  olddate = datetime.strptime(date,%Y-%m-%j %H:%M:%S.%f)


Due to circumstances, I'm using Python 2.5.4 on one machine (2.6 on
the other).

When I have a script as simple as this:

import datetime

datetime.datetime.strptime('2010-09-14', %Y-%m-%d)


Running this script brings up a calendar, believe it or not. The
calendar displays March 2010, and shows the 22nd as a holiday. When I
dismiss the dialog box I get:
Traceback (most recent call last):
  File strptimetest.py, line 3, in module
datetime.datetime.strptime('2010-09-14', %Y-%m-%d)
  File C:\Python25\lib\_strptime.py, line 272, in module
_TimeRE_cache = TimeRE()
  File C:\Python25\lib\_strptime.py, line 191, in __init__
self.locale_time = LocaleTime()
  File C:\Python25\lib\_strptime.py, line 74, in __init__
self.__calc_weekday()
  File C:\Python25\lib\_strptime.py, line 94, in __calc_weekday
a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
AttributeError: 'module' object has no attribute 'day_abbr'


err... what? Is this an old weirdness I don't remember from the 2.5
series?

I can select dates in the calendar, but nothing dismisses it but the
close box.

Josh English
Incredibly Confused
-- 
http://mail.python.org/mailman/listinfo/python-list


MediaWiki to RTF/Word/PDF

2010-02-17 Thread Josh English
I have several pages exported from a private MediaWiki that I need to
convert to a PDF document, or an RTF document, or even a Word
document.

So far, the only Python module I have found that parses MediaWiki
files is mwlib, which only runs on Unix, as far as I can tell. I'm
working on Windows here.

Has anyone heard of a module that parses wiki markup and transforms
it? Or am I looking at XSLT?

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


Collections.py -- any help?

2009-04-24 Thread Josh English
In my quest to learn more, I've been trying to get the collections.py
module to do anything but look cool. So far, it's only a cool idea.
How do they work? I can't find a tutorial on the web anywhere
explaining how I would use them in my code.

Any clues?

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


Re: Collections.py -- any help?

2009-04-24 Thread Josh English
Sorry, I was referring to the abstract base classes defined there...

mea culpa. I didn't get out of my head while posting.

Josh

On Apr 24, 4:15 pm, Jerry Hill malaclyp...@gmail.com wrote:
 On Fri, Apr 24, 2009 at 6:56 PM, Josh English


 Doug Hellmann wrote an article on the collections module for his
 Python Module of the Week (PyMOTW) series 
 here:http://www.doughellmann.com/PyMOTW/collections/index.html

 Does that help?

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


Re: negative numbers are not equal...

2008-08-14 Thread Josh English
On Aug 14, 1:18 pm, ariel ledesma [EMAIL PROTECTED] wrote:
 hello guys

 i just ran into this when comparing negative numbers, they start
 returning False from -6 down, but only when comparing with 'is'

   m = -5
   a = -5
   m is a
 True
   m = -6
   a = -6
   m is a
 False
   m == a
 True

 i read that 'is' compares if they are really the same object, but i
 don't that's it because then why does -5 return True?
 of course i could only use == to compare, but still, what am i missing here?
 thanks in advance

 ariel

strange, I get the same thing.

From the docs
The operators is and is not test for object identity: x is y is true
if and only if x and y are the same object. x is not y yields the
inverse truth value.

so:
a = b = -6
a is b
True

It is odd behaviour, but I would stick to '==' when comparing numeric
values

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


Re: learning unit testing in python

2008-06-23 Thread Josh English
A good starting point is Mark Pilgrim's Dive Into Python.

http://www.diveintopython.org/unit_testing/index.html

Josh
On Jun 23, 7:55 am, Alex [EMAIL PROTECTED] wrote:
 Hi all.

 I'd like learn some basic unit testing with python.
 I red some articles about different testing framework like unittest or
 nose, but I'm a bit confused: what is the best choice? I'm not a
 professional developer (I'm a SEO) but I belive that unit testing is a
 good and pragmatic way to produce working software, so I'd like to
 find something really simple ad straightforward because I don't have
 to manage big programming projects.

 Thanks in advance,

 Alex

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


Re: Developing a Package with Sub Packages

2008-02-18 Thread Josh English
When testing the package in idle, this results in
C:\Python25\Lib\idlelib
instead of the file.
The Data folder is created in this folder now.

On 2/18/08, Gabriel Genellina [EMAIL PROTECTED] wrote:
 En Sun, 17 Feb 2008 22:34:27 -0200, Josh English
 [EMAIL PROTECTED] escribi�:

  Here's what I think is happening: IMS/__init__.py uses os.getcwd() to
  establish the path to the data folder and the files inside of it. When
  I run StoryCreator, os.getcwd() returns the story folder.
  If I'm right, how can I get the IMS/__init__.py module to use relative
  paths to its own module, and not the current working directory the os
  module provides?

 Use the module's own __file__ attribute:
 my_path = os.path.abspath(os.path.dirname(__file__))

 --
 Gabriel Genellina

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


-- 
Josh English
[EMAIL PROTECTED]
http://joshenglish.livejournal.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Developing a Package with Sub Packages

2008-02-17 Thread Josh English
I have created a group of scripts to manage an XML-based database. I'd
like to make it into a proper package that will let me keep track of
the code. I have a lot of files that are similar in name and they just
get crowded in one folder.

Here's a sample of the file structure:

IMS/
IMS/__init__.py
IMS/Config.py
IMS/imsdefaults.cfg
IMS/local.txt
IMS/Data/
IMS/Data/stories.xml
IMS/Story/
IMS/Story/__init__.py
IMS/Story/StoryCreator.py

When the IMS/__init__.py file is loaded, it creates the IMS/Data/
folder and if there are no xml files, it creates them and fills them
in with some default values.
The IMS/Config.py has a subclass of the ConfigParser, and reads the
imsdefaults.cfg and local.txt files.
When I run StoryCreator, buried in it's own package (that imports IMS)
the data folder is created inside the Story folder, and I get an error
message stating the ConfigParser object could not find the
imsdefaults.cfg file.
Here's what I think is happening: IMS/__init__.py uses os.getcwd() to
establish the path to the data folder and the files inside of it. When
I run StoryCreator, os.getcwd() returns the story folder.
If I'm right, how can I get the IMS/__init__.py module to use relative
paths to its own module, and not the current working directory the os
module provides?
This could also solve the problem with Config.py, I think.

Thanks

Josh English
http://joshenglish.livejournal.com
-- 
http://mail.python.org/mailman/listinfo/python-list