Splitting a line while keeping quoted items together

2012-11-19 Thread josh
I am working on a cmd.Cmd-based program, and normally could just split the 
string and get the right parts.

Now I have a case where I could have two or three words in the string that need 
to be grouped into the same thing.

Then I realized that I'm not the only person who has had to deal with this, and 
I'm wondering if my solution is the best one out there or if this is as ugly at 
it feels?

Code below
...

#x('Seattle 456') -> ('Seattle', '456')
#x('"Portland Alpha" 123') -> ('Portland Alpha', '123')
#x("'Portland Beta' 789') -> ('Portland Beta', '789')


def x(line):
res = []
append = False
appended = None
quote = None
for item in line.split():
if append:
if item.endswith(quote):
appended.append(item[:-1])
res.append(' '.join(appended))
quote = None
appended = None
append = False
else:
appended.append(item)
else:
if item[0] in ["'",'"']:
append = True
appended = [item[1:]]
quote = item[0]
else:
res.append(item)
return res
..

This seem really ugly. Is there a cleaner way to do this? Is there a keyword I 
could search by to find something nicer?

Thanks,

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


python university search

2005-12-04 Thread josh
[pardon me if this is not the appropriate list]

hello,

i am interested in doing an undergraduate major in computer science
that mainly focuses on python as a programming language..

i am not a very bright student and neither do i have the money to
think about universities like caltech, stanford etc. i am looking for
a university that is easy to get admitted in and yet i can get good
knowledge and education out of it.

also english is not my first language and i feel that acts against me,
but i do have a strong desire to learn.

i have read the tutorials in python.org and understand the python
programming syntax but i feel that only a computer science class is
going to teach me how to program and apply advance concepts.  if any
of you happen to know good video tutorials or self study materials or
tips  that can act as an alternative to going to college, would you
please mind sharing or selling for something reasonable.

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


File Handling Problems Python I/O

2005-01-06 Thread Josh
Hi,

I am having a problem with Python. I am new to Python as a programming
language, but I do have experience in other languages. I am
experiencing strange problems with File handling and wonder if anyone
else has seen this or knows what I am doing wrong. I am simply trying
to open a file for read and iterate through until the end of file. I am
able to do so without a problem, but here's the catch: The open
statement is only working on certain files. I open a simple text file
say file1.txt without any issues, but I change the open statement to
another text file and it error's out stating the file doesn't exist. I
know the code is correct because it worked for the other file. I have
tried both binary and ascii modes to no avail. Any idea why this is
happening? I am running Python 2.3.4 wxPython 2.5.3.1 and SPE as the
IDE on Win2k. Thanks

Josh

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


Re: File Handling Problems Python I/O

2005-01-06 Thread Josh
He is the function where I am making the call. If I change the open
statment to another file, say "c:\test.txt", a file I know exists, it
will error out stating the file does not exist. Thanks

Josh

def GetStartVars(self):
try:
DOWNFILE = open("c:\fixes.txt","r")
except IOError:
print "Failed to open file."
else:
counter = 1
while 1:
theline = DOWNFILE.readline()
if not theline:
break
print theline
counter = counter + 1
print counter
DOWNFILE.close()

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


Re: File Handling Problems Python I/O

2005-01-06 Thread Josh
Peter,

Thank you for the rookie correction. That was my exact problem. I
changed the address to use forward slashes and it works perfect. I did
not know that a backslash had special meaning within a string, but now
I do! Thanks again

Josh

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


Re: File Handling Problems Python I/O

2005-01-06 Thread Josh
Micheal,

Thanks for the advice as the programming I am doing will be run on both
Windows and Linux based PC's, that being the main reason for my venture
into Python. I'm glad to see that people are willing to help out even
the newbie's.  

Josh

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


why not datetime.strptime() ?

2005-01-07 Thread josh
Shouldn't datetime have strptime? It already has strftime, and it'd be really
nice to obviate datetime.fromtimestamp(time.mktime(time.strptime(...)))

thanks in advance

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


Re: why not datetime.strptime() ?

2005-01-11 Thread josh
David M. Cookie writes:
> You don't check for errors: an exception being thrown by
> PyObject_CallMethod will return obj == NULL.

Oops, missed that one. Thanks.

> If there's a module in sys.path called time that overrides the stdlib
> time, things will fail, and you should be able to catch that.

Maybe someone really does want to override the time module, and then we
shouldn't get in their way? For example, if someone adds a new field
descriptor for nanoseconds to time.strptime, then it'd be nice to have
it work with datetime.datetime.strptime as well.

Incidentally, this is the way that the rest of datetime does it.

> Are you positive those PySequence_GetItem calls will succeed? That
> they will return Python integers?

Well, without interfaces, I can't be sure :). Throwing an exception
is cool (especially if we do allow user implemented time.strptime).

--- Modules/datetimemodule.c.orig   2003-10-20 10:34:46.0 -0400
+++ Modules/datetimemodule.c2005-01-11 10:42:23.0 -0500
@@ -3774,6 +3774,47 @@
return result;
 }
 
+/* Return new datetime from time.strptime(). */
+static PyObject *
+datetime_strptime(PyObject *cls, PyObject *args)
+{
+   PyObject *result = NULL, *obj, *module;
+   const char *string, *format;
+
+   if (!PyArg_ParseTuple(args, "ss:strptime", &string, &format))
+   return NULL;
+
+   if ((module = PyImport_ImportModule("time")) == NULL)
+   return NULL;
+   obj = PyObject_CallMethod(module, "strptime", "ss", string, format);
+   Py_DECREF(module);
+
+   if (obj != NULL) {
+   int good_timetuple = 1;
+   if (PySequence_Check(obj) && PySequence_Size(obj) > 6) {
+   int i;
+   for (i=0; i <= 6; i++)
+   if (!PyInt_Check(PySequence_GetItem(obj, i)))
+   good_timetuple = 0;
+   } else
+   good_timetuple = 0;
+   if (good_timetuple)
+   result = PyObject_CallFunction(cls, "iii",
+   PyInt_AsLong(PySequence_GetItem(obj, 0)),
+   PyInt_AsLong(PySequence_GetItem(obj, 1)),
+   PyInt_AsLong(PySequence_GetItem(obj, 2)),
+   PyInt_AsLong(PySequence_GetItem(obj, 3)),
+   PyInt_AsLong(PySequence_GetItem(obj, 4)),
+   PyInt_AsLong(PySequence_GetItem(obj, 5)),
+   PyInt_AsLong(PySequence_GetItem(obj, 6)));
+   else
+   PyErr_SetString(PyExc_ValueError,
+   "unexpected value from time.strptime");
+   Py_DECREF(obj);
+   }
+   return result;
+}
+
 /* Return new datetime from date/datetime and time arguments. */
 static PyObject *
 datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
@@ -4385,6 +4426,11 @@
 PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
   "(like time.time()).")},
 
+   {"strptime", (PyCFunction)datetime_strptime,
+METH_VARARGS | METH_CLASS,
+PyDoc_STR("strptime -> new datetime parsed from a string"
+  "(like time.strptime()).")},
+
{"combine", (PyCFunction)datetime_combine,
 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
 PyDoc_STR("date, time -> datetime with same date and time fields")},
--- Doc/lib/libdatetime.tex.orig2003-09-06 01:36:56.0 -0400
+++ Doc/lib/libdatetime.tex 2005-01-11 11:06:22.699348152 -0500
@@ -624,6 +624,13 @@
   ignored.
   \end{methoddesc}
 
+\begin{methoddesc}{strptime}{date_string, format}
+  Return the date corresponding to date_string, parsed according
+  to format. This is equivalent to \code{datetime(*(time.strptime(
+  date_string, format)[0:7]))}. \exception{ValueError} is raised if
+  \code{time.strptime()} returns a value which isn't a timetuple.
+\end{methoddesc}
+
 Class attributes:
 
 \begin{memberdesc}{min}
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: why not datetime.strptime() ?

2005-01-11 Thread josh
On Tue, Jan 11, 2005 at 08:56:33AM -0600, Skip Montanaro wrote:
> * The seventh item returned from time.strptime() is the day of the week.
>   You're passing it into the microsecond arg of the datetime constructor

Thanks!

>   and ignoring the timezone info (ninth item returned from
>   time.strptime(), eighth arg to the datetime constructor).
> 

This is intentional. time.strptime() doesn't expose the timezone (only
the daylight savings flag), so i'd have to duplicate its logic in order
to distinguish between local time and utc. Since it's easy to explicitly
call somedatetime.replace(tzinfo=sometzinfo), having strptime() always
create "naive" datetimes seemed most sensible.

> * It would be nice if the import happened only once and was cached:
> 
>   static PyObject *time_module;
>   ...
>   if (time_module == NULL &&
>   (time_module = PyImport_ImportModule("time")) == NULL)
> return NULL;
>   obj = PyObject_CallMethod(time_module, ...);

The datetime is full of these calls. Would it make sense to make this a
separate patch? (Or maybe the PyImport_ImportModule could implement such
a cache :) ?)
*** Modules/datetimemodule.c.orig   2003-10-20 10:34:46.0 -0400
--- Modules/datetimemodule.c2005-01-11 12:19:36.839337336 -0500
***
*** 3774,3779 
--- 3774,3819 
return result;
  }
  
+ /* Return new datetime from time.strptime(). */
+ static PyObject *
+ datetime_strptime(PyObject *cls, PyObject *args)
+ {
+   PyObject *result = NULL, *obj, *module;
+   const char *string, *format;
+ 
+   if (!PyArg_ParseTuple(args, "ss:strptime", &string, &format))
+   return NULL;
+ 
+   if ((module = PyImport_ImportModule("time")) == NULL)
+   return NULL;
+   obj = PyObject_CallMethod(module, "strptime", "ss", string, format);
+   Py_DECREF(module);
+ 
+   if (obj != NULL) {
+   int i, good_timetuple = 1;
+   long int ia[6];
+   if (PySequence_Check(obj) && PySequence_Size(obj) >= 6)
+   for (i=0; i < 6; i++) {
+   PyObject *p = PySequence_GetItem(obj, i);
+   if (PyInt_Check(p))
+   ia[i] = PyInt_AsLong(p);
+   else
+   good_timetuple = 0;
+   Py_DECREF(p);
+   }
+   else
+   good_timetuple = 0;
+   if (good_timetuple)
+   result = PyObject_CallFunction(cls, "ii",
+   ia[0], ia[1], ia[2], ia[3], ia[4], ia[5]);
+   else
+   PyErr_SetString(PyExc_ValueError,
+   "unexpected value from time.strptime");
+   Py_DECREF(obj);
+   }
+   return result;
+ }
+ 
  /* Return new datetime from date/datetime and time arguments. */
  static PyObject *
  datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
***
*** 4385,4390 
--- 4425,4435 
 PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
   "(like time.time()).")},
  
+   {"strptime", (PyCFunction)datetime_strptime,
+METH_VARARGS | METH_CLASS,
+PyDoc_STR("strptime -> new datetime parsed from a string"
+  "(like time.strptime()).")},
+ 
{"combine", (PyCFunction)datetime_combine,
 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
 PyDoc_STR("date, time -> datetime with same date and time fields")},
*** Doc/lib/libdatetime.tex.orig2003-09-06 01:36:56.0 -0400
--- Doc/lib/libdatetime.tex 2005-01-11 12:02:30.0 -0500
***
*** 624,629 
--- 624,636 
ignored.
\end{methoddesc}
  
+ \begin{methoddesc}{strptime}{date_string, format}
+   Return the date corresponding to date_string, parsed according
+   to format. This is equivalent to \code{datetime(*(time.strptime(
+   date_string, format)[0:6]))}. \exception{ValueError} is raised if
+   \code{time.strptime()} returns a value which isn't a timetuple.
+ \end{methoddesc}
+ 
  Class attributes:
  
  \begin{memberdesc}{min}
*** Lib/test/test_datetime.py.orig  2005-01-11 11:56:36.0 -0500
--- Lib/test/test_datetime.py   2005-01-11 12:28:30.268243816 -0500
***
*** 1351,1356 
--- 1351,1365 
  # Else try again a few times.
  self.failUnless(abs(from_timestamp - from_now) <= tolerance)
  
+ def test_strptime(self):
+ import time
+ 
+ string = '2004-12-01'
+ format = '%Y-%m-%d'
+ expected = self.theclass(*(time.strptime(string, format)[0:6]))
+ got = self.theclass.strptime(string, format)
+ self.assertEqual(expected, got)
+ 
  def test_more_timetuple(self):
  # This tests fields beyond those teste

Re: why not datetime.strptime() ?

2005-01-13 Thread josh
On Thu, Jan 13, 2005 at 08:06:56AM -0600, Skip Montanaro wrote:
> 
> Skip> I just checked in your changes.  Thanks for the effort.
> 
> Jeez Skip...  That reads poorly.  How about "Thanks for your contribution"?
> In any case, thanks.

My pleasure. Thanks for helping me to help. And I liked the first thanks
just fine (the "checked in" was the important part :) ).
-- 
http://mail.python.org/mailman/listinfo/python-list


why no time() + timedelta() ?

2005-01-20 Thread josh
Why can't timedelta arithmetic be done on time objects?

(e.g. datetime.time(5)-datetime.timedelta(microseconds=3)

Nonzero "days" of the timedelta could either be ignored, or trigger an 
exception.



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


Handling MySQL connection

2005-01-31 Thread Josh
Hi,

First off, I am a newbie to Python and so far I must say that we're
getting along pretty well. My schooling was is C/C++, but have been
writing code in, dare I say it, VB, for the past 4 years.

Here is the problem I have run across. I am using SPE w/WXGlade to
setup the GUI, so it handled the initial formatting of my python code
as two seperate classes, one for the APP and one for the Frame. I'll be
the first to admit I haven't gotten a good grasp on how things should
flow within a python program. Anyway, what I have done is created a few
functions using the DEF statement to handle the action of the widgets.
Part of the program is accessing a MySQL database. Once a button is
pushed, part of it's function is to open the database, check to see if
a value exists, and then close the connection. Running this locally on
my PC, which is also where MySQL resides, returns results instantly.
The flow is
a) Open a connection to the database
b) Execute a simple Select statement to verify the field
c) Close the connection.
All done within a single DEF

This works fine and dandy on my Windows 2000 PC that is also acting as
the MySQL server, but when I transfer the program to a Linux box
running Fedora, there is a very noticable lag when this action takes
place. So my thought is rather than connecting to the database,
fetching the data, then closing out the connection every time, it may
be a better idea to connect to the database upon starting the program,
leaving the connection open, making the transactions as needed, and
then closing the connection upon leaving the program. Doing this,
however, has proved to be a little more difficult than I thought, and
I'm sure it's just because of my lack of knowledge of Python.

So I put the question out, how do I accomplish this?

Do I need some sort of Global(such an evil word) object to work off of?

In the Def __init__ I would like to open the connection
eg: connection = MySQLdb.connect(host="localhost", user="root",
passwd="blah", db="data")

In a Sepeate Function I would like to make the transactions on it.
eg: cursor = connection.cursor()
cursor.execute( "SELECT * FROM tags WHERE tags.tag_no = %s"
%temp_tag )
self.data = cursor.fetchall()
cursor.close()

Finally in another Function I would like to be able to close the
connection
eg: connection.close()

All of these Def's would fall under the same Frame class. Am I going
about this incorrectly? 

Thanks for your help and patience. 

Josh

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


Re: Handling MySQL connection

2005-02-01 Thread Josh
Thanks for the help! I was able to work through it based on you
example. It was lack of knowledge of the object class that was throwing
me off.  

Josh

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


readline: edit-and-execute-command

2005-07-04 Thread josh
anybody know why "edit-and-execute-command" doesn't work in python's readline?  
it doesn't even show up in a dump of readline functions:

$ cat fc_test   
C-p: dump-functions 
C-e: edit-and-execute-command   
$   
$ python
Python 2.4 (#1, Jan 14 2005, 14:29:23)  
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2  
Type "help", "copyright", "credits" or "license" for more information.  
--- import readline 
--- readline.read_init_file('fc_test')  

if you hit control p, and you won't see 'edit-and-execute-command'  
listed at all, though you will when you run "INPUTRC=fc_test bash"

thanks in advance

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


time + timedelta

2005-02-07 Thread josh
datetime.time should support timedelta arithmetic, patch attached.

Times greater than 24 hours should not raise an exception, but always
wrap around. Any other behavior is too surprising. (People expect to be
able to call you up at 11pm, and say "meet me in two hours" (if you're a
night person)).

Clock time is cyclical. Though it's convenient to assign hours numbers,
an unqualified hour 23.5 is not naturally "greater than" hour 0.5.
(Though I'm not suggesting that we break this.)

The special meaning for hour 0 is only relevant for datetimes. When
dates are not involved, it shouldn't get any special treatment.
When dates are involved, it's easy to "assert datetime1.date() ==
datetime2.date()"

If this change is made however, maybe datetime.timedelta(hours=-1)
should no longer normalize to datetime.timedelta(-1, 82800)

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


Re: time + timedelta

2005-02-07 Thread josh
sorry, patch really attached to this message.

On Mon, Feb 07, 2005 at 11:24:20AM -0500, josh wrote:
> datetime.time should support timedelta arithmetic, patch attached.
> 
> Times greater than 24 hours should not raise an exception, but always
> wrap around. Any other behavior is too surprising. (People expect to be
> able to call you up at 11pm, and say "meet me in two hours" (if you're a
> night person)).
> 
> Clock time is cyclical. Though it's convenient to assign hours numbers,
> an unqualified hour 23.5 is not naturally "greater than" hour 0.5.
> (Though I'm not suggesting that we break this.)
> 
> The special meaning for hour 0 is only relevant for datetimes. When
> dates are not involved, it shouldn't get any special treatment.
> When dates are involved, it's easy to "assert datetime1.date() ==
> datetime2.date()"
> 
> If this change is made however, maybe datetime.timedelta(hours=-1)
> should no longer normalize to datetime.timedelta(-1, 82800)
> 
*** Doc/lib/libdatetime.tex12005-02-03 21:53:06.0 -0500
--- Doc/lib/libdatetime.tex 2005-02-03 22:11:52.0 -0500
***
*** 1059,1069 
  
  Supported operations:
  
! \begin{itemize}
!   \item
! comparison of \class{time} to \class{time},
! where \var{a} is considered less than \var{b} when \var{a} precedes
! \var{b} in time.  If one comparand is naive and the other is aware,
  \exception{TypeError} is raised.  If both comparands are aware, and
  have the same \member{tzinfo} member, the common \member{tzinfo}
  member is ignored and the base times are compared.  If both
--- 1059,1122 
  
  Supported operations:
  
! \begin{tableii}{c|l}{code}{Operation}{Result}
!   \lineii{\var{time2} = \var{time1} + \var{timedelta}}{(1)}
! 
!   \lineii{\var{time2} = \var{time1} - \var{timedelta}}{(2)}
! 
!   \lineii{\var{timedelta} = \var{time1} - \var{time2}}{(3)}
! 
!   \lineii{\var{time1} < \var{time2}}
!{Compares \class{time} to \class{time}.
! (4)}
! 
! \end{tableii}
! 
! \begin{description}
! 
! \item[(1)]
! 
! time2 is a duration of timedelta removed from time1, moving
! forward in time if \code{\var{timedelta}.seconds} > 0, or backward if
! \code{\var{timedelta}.seconds} < 0.  The result has the same 
\member{tzinfo} member
! as the input time, and time2 - time1 == timedelta after.
! Note that no time zone adjustments are done even if the input is an
! aware object.
! 
! \item[(2)]
! Computes the time2 such that time2 + timedelta == time1.
! As for addition, the result has the same \member{tzinfo} member
! as the input time, and no time zone adjustments are done even
! if the input is aware.
! This isn't quite equivalent to time1 + (-timedelta), because
! -timedelta in isolation can overflow in cases where
! time1 - timedelta does not.
! 
! \item[(3)]
! Subtraction of a \class{time} from a
! \class{time} is defined only if both
! operands are naive, or if both are aware.  If one is aware and the
! other is naive, \exception{TypeError} is raised.
! 
! If both are naive, or both are aware and have the same \member{tzinfo}
! member, the \member{tzinfo} members are ignored, and the result is
! a \class{timedelta} object \var{t} such that
! \code{\var{time2} + \var{t} == \var{time1}}.  No time zone
! adjustments are done in this case.
! 
! If both are aware and have different \member{tzinfo} members,
! \code{a-b} acts as if \var{a} and \var{b} were first converted to
! naive UTC times first.  The result is
! \code{(\var{a}.replace(tzinfo=None) - \var{a}.utcoffset()) -
!   (\var{b}.replace(tzinfo=None) - \var{b}.utcoffset())}
! except that the implementation never overflows.
! 
! \item[(4)]
! 
! \var{time1} is considered less than \var{time2}
! when \var{time1} precedes \var{time2} in time.
! 
! If one comparand is naive and the other is aware,
  \exception{TypeError} is raised.  If both comparands are aware, and
  have the same \member{tzinfo} member, the common \member{tzinfo}
  member is ignored and the base times are compared.  If both
***
*** 1076,1093 
  raised unless the comparison is \code{==} or \code{!=}.  The latter
  cases return \constant{False} or \constant{True}, respectively.
  
!   \item
! hash, use as dict key
  
!   \item
! efficient pickling
! 
!   \item
! in Boolean contexts, a \class{time} object is considered to be
! true if and only if, after converting it to minutes and
! subtracting \method{utcoffset()} (or \code{0} if that's
! \code{None}), the result is non-zero.
! \end{itemize}
  
  Instance methods:
  
--- 1129,1141 
  raised unless the comparison is \code{==} or \code{!=}.  The latter
  cases return \constant{False} or \constant{True}, respective

Reportlab and Barcodes

2005-02-08 Thread Josh
Hi All,

I need someone to explain to me how to output a Code39 barcode to a
Reportlab PDF. As far as I can tell, there is no means to do this with
the Canvas object, and the demo that is included with the libraries is
using the platypus Frame to place the barcode on the form. I do not
wish to use this method. I'm guessing that it needs to be placed on the
form as some type of graphic. It's obviously not going to work with the
drawText method. Any ideas? 

Thanks

Josh

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


Re: Reportlab and Barcodes

2005-02-09 Thread Josh
Benji,

I have used those very same fonts before and they work great, but I'm
trying to get away with using straight Python to accomplish this,
especially since the program will most likely be used on both Linux and
Windows. 

Josh

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


Re: Reportlab and Barcodes

2005-02-09 Thread Josh
One of the users on the Reportlabs mailing list was kinda enough to
offer me the solution.. A simple call to the drawOn function, e.g.:

bc = code39.Standard39("123",xdim = .015*inch)
x = 6*inch
y = -5*inch
bc.drawOn(canvas,x,y)

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


Re: Reportlab and Barcodes

2005-02-10 Thread Josh
Damjan,

Code39 here refers to part of the Barcode Extensions available to
Reportlabs. It can be imported as such

from reportlab.extensions.barcode import code39

Josh

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


Win32api shellexecute Bshow

2005-02-16 Thread Josh
Hi All,

I am trying to print a pdf file from python using the Win32api
shellexecute method and am having a problem, or perhaps just a
misunderstanding, with the bshow parameter. Am I correct to assume that
if the bshow parm is set to 0, that the program will not show when
launched? e.g.:

win32api.ShellExecute (0, "print", "test.pdf", None, ".", 0)

When this statment is executed, Adobe Acrobat is launched and made
visible to the user, however the actual PDF is hidden. I do not wish
for Acrobat to be seen either. 

Any thoughts? 

Thanks

Josh

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


Re: Win32api shellexecute Bshow

2005-02-16 Thread Josh
Thanks,

I'll take a look at using GhostScript and GSPrint instead. 

Josh

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


Sockets SO_RCVBUF

2005-04-29 Thread Josh
I have written a simple TCP client/server.  I would like to change the TCP 
receive buffer on the server side, to see performance differences.  I am 
trying



err = sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4096)



but that doesn't seem to actual change the buffer size.



The code is:



#Create a socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)



#Ensure that you can restart your server quickly when it terminates

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)



#Set the Recieve Buffer Size

err = sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4096)



print socket.SO_RCVBUF

#Set the client socket's TCP port number

sock.bind(('', portno))



Development is being done a Gentoo Linux box.



Any suggestions would be great!



-Thanks





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


sqlite3 views, if not exists clause

2006-11-14 Thread Josh
Hi,
I'm running into a problem when trying to create a view in my sqlite 
database in python. I think its a bug in the sqlite3 api that comes with 
python 2.5.

This works as expected:
  conn = sqlite3.connect(':memory:')
  conn.execute("create table foo (a int,b int)")
  conn.execute('create view bar as select * from foo')

This fails as expected:
  conn = sqlite3.connect(':memory:')
  conn.execute("create table foo (a int,b int)")
  conn.execute('create view bar as select * from foo')
  conn.execute('create view bar as select * from foo')
with exception "sqlite3.OperationalError: table bar already exists".
Weird that it fails with TABLE bar already exists, but this is the message 
that sqlite actually returns so it is not python's fault.

THIS DOES NOT WORK, but it should!
  conn = sqlite3.connect(':memory:')
  conn.execute("create table foo (a int,b int)")
  conn.execute('create view if not exists bar as select * from foo')
it fails with exception "sqlite3.OperationalError: near "not": syntax error"

Can anyone confirm, or pass this on to the appropriate person?
Thanks!
Josh



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


Re: sqlite3 views, if not exists clause

2006-11-15 Thread Josh
"Tim Golden" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

| Not a bug with the "sqlite3 api that comes with python 2.5." as such,
| since .execute pretty much passes its parameters through to the database
| engine. Rather, the syntax you're using is a relatively late addition
| to the sqlite libs -- at least it fails on my 3.2.1 version of the
| sqlite3 commmand-line tool, but succeeds on the latest download (3.3.8).
|
| I presume therefore that you build of Python is linked against an
| older version of the sqlite libraries / DLL.

Exactly right. Since the sqlite3 interface hasn't changed, the fix was 
simply to copy the newest sqlite3.dll into the python dll directory. Now I 
no longer have to catch and ignore the "already exists" exception thrown by 
python. Thanks! 


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


notify on directory changes

2006-11-17 Thread Josh
Hi,
Does python have a way of registering a callback - or through some other 
mechanism - to notify me when a directory listing has changed, or if a file 
has been modified, deleted, created, etc.? I'm looking for behavior similar 
to the .net class FileSystemWatcher. I would also be interested to know if 
there exists a portable c or c++ solution, too.
thanks!
Josh


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


VB to Python migration

2006-01-27 Thread Josh
We have a program written in VB6 (over 100,000 lines of code and 230 UI 
screens) that we want to get out of VB and into a better language. The 
program is over 10 years old and has already been ported from VB3 to 
VB6, a job which took over two years. We would like to port it to 
Python, but we need to continue to offer upgrades and fixes to the 
current VB6 version. Does anybody know of ways we could go about 
rewriting this, one screen at a time, in Python, and calling the screens 
from the existing program?

We are also looking for a graphics toolkit to use. IronPython with it's 
.NET bindings and ability to integrate with Visual Studio looks good, 
but leaves that bad MS taste in the mouth.

We currently use a MS Access back end and need to migrate to a proper 
SQL server. We need to leave options open for SQL Server (for customers 
who want to use existing infrastructure) and something like MySQL or 
PostgreSQL. But in the mean time, we need to be able to access an 
MSAccess (Jet) database from Python.

Any answers/suggestions/pointers are greatly appreciated.

Thanks,

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


Re: VB to Python migration

2006-01-30 Thread Josh
Nicolas Kassis wrote:
> Josh wrote:
> 
> 
>>We have a program written in VB6 (over 100,000 lines of code and
>>230 UI screens) that we want to get out of VB and into a better
>>language. The program is over 10 years old and has already been
>>ported from VB3 to VB6, a job which took over two years. We would
>>like to port it to Python, but we need to continue to offer
>>upgrades and fixes to the current VB6 version. Does anybody know of
>>ways we could go about rewriting this, one screen at a time, in
>>Python, and calling the screens from the existing program?
>>
>>We are also looking for a graphics toolkit to use. IronPython with
>>it's .NET bindings and ability to integrate with Visual Studio
>>looks good, but leaves that bad MS taste in the mouth.
>>
>>We currently use a MS Access back end and need to migrate to a
>>proper SQL server. We need to leave options open for SQL Server
>>(for customers who want to use existing infrastructure) and
>>something like MySQL or PostgreSQL. But in the mean time, we need
>>to be able to access an MSAccess (Jet) database from Python.
>>
>>Any answers/suggestions/pointers are greatly appreciated.
>>
>>Thanks,
>>
>>Josh Isted
> 
> 
> For the Database, you should use the SQLObject(sqlobject.org) module.
> 
> Nic
> 

Thanks for the info, I took a look at SQLObject, but I'm not sure if 
that's the way we want to go quite yet. We have a lot of embedded SQL in 
the program and it looks like we'd have to convert it all to SQLObject 
object code, which would just add to the initial complexity of the 
project. I'll keep this bookmarked for future reference but I don't 
think it'll work at the moment.

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


Re: VB to Python migration

2006-01-30 Thread Josh
Jon-Pierre Gentil wrote:
> I would look into Qt, PyQt, and the Qt Designer.  It'll provide one of the
> easiest ways of porting your existing GUI dialogs into Python.  Start
> building and componentizing your code.  In VB6, all classes are flat under
> a single branch of a project tree, whereas in Python you'll likely
> implement modules of similar functionality.  Overall porting your
> application will be a combination of rewriting, refactoring, and
> copy-and-translate code.  Good luck.  :-)  It actually sounds like a fun
> project to be on.  I had a lot of VB6 experience back in "the day" and a
> project like that would be a lot of fun to me.

We had looked at QT a while ago, but not in relation to Python. We were 
just looking at it in relation to rewriting in C++. We'll have to 
revisit it, paying attention to PyQt.

It is a rather fun project to be on. It's such a gigantic program 
though, it gets difficult to comprehend how long it's going to take to 
make this transision. Whether it's to Python, or some other direction.

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


Re: VB to Python migration

2006-01-30 Thread Josh
DH wrote:
> see vb2py to help the conversion
> http://vb2py.sourceforge.net/
> or if you want to convert vb6 to vb.net instead, there are tools from 
> microsoft and others to help with that, such as:
> http://www.microsoft.com/downloads/details.aspx?FamilyId=10C491A2-FC67-4509-BC10-60C5C039A272&displaylang=en
>  
> 
> 
> or if you want to start over from scratch, the other recommendations are 
> good, like pyqt and qt designer, or else do it as a web app instead of 
> desktop app if it just involves basic form controls.

I have taken a quick look at vb2py, but I'm not sure if that's the way 
we want to go. If we do move to Python, we want to take advantage of 
features of the Python language, rather than just rewriting a VB app in 
Python. It may be a quicker solution in the short term, but in the long 
term, I think completely embracing Python from the beginning is the best 
way to go.

I tried running the project through the conversion wizard in VB.NET 
Express Edition recently just for the fun of it. I come up with 16498 
messages at the end of the hour and a half conversion. That's Compile 
and Design errors as well as Warnings. It'd take a while to understand 
those errors, possibly correcting them in VB6 before trying to do 
another conversion, and again, we wouldn't be taking advantage of the 
strengths of the new language. Again, a possibility, but not "good" 
solution.

The web app functionality is something we also want to be very 
accomodating of. We are planning on rewriting in such a way that the 
functionality of the screens (input processing, error trapping etc.) is 
done is an extensibly manner. That would mean using the same code for 
both hardcoded windows as well as web forms.

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


Re: VB to Python migration

2006-01-30 Thread Josh
Thomas Ganss wrote:
> You haven't specified where your main pains are.
> Do you have at least rudimentary architecture ?
> How often do you have code reviews / refactored your code ?
> Have you been striving for good code ? Is it a total mess ?

Umm... We have been striving for good code, but being a small place, 
it's rather difficult. The software in question is basically an ERP 
program for the mattress industry. The program started out as a project 
for one company, with a single programmer, with plans to grow.

The program, I am sure, still has much code left from those first days 
of coding. With customer demands for new features, we do our best to 
keep everything extensible, and open, but once a piece of code is 
written, it's very rare that it will be changed unless it is being debugged.

> Guessing only from the number of screens, you probably
> have more than trivial amounts of data.

You have guessed right. One of our largest customers is using an Access 
database that is around 800Mb and they have licenses for about 20 
concurrent users (of our program). I know this may not sound huge for 
someone who works at Walmart head office, but it's been interesting to 
get Jet to work well.

> *Based on that assumption*, I'ld move the data FIRST.
> Get rid of the JET engine! If you are certain you will
> have to support SQL server, make it SQL server and
> MSDE/new express version first. Only then (perhaps)
> think about adding a totally free database engine.
> If not, take your pick but *move the data*.

This is a very interesting thought. That would mean a whole bunch of 
rewriting of the current codebase.

The thought had been to write the port in an extensible manner, allowing 
Jet, but being written well enough to easily use other backends. We'll 
have to see.

> While you are on it, refactor the old code into something
> at least resembling a 3 to 5 layered approach with COM.
> Doesn't need to be perfect 100%, but more than 50%.

This makes sense, but OTOH, it might be more worth our time to properly 
redesign in a more OOP friendly language.

> Then exchange the parts that benefit the most by
> implementation inheritance, since VB's interface inheritance
> is arguably the "best" technical reason for redundant code.
> 
> I'ld guess the business layer[s] would be the next things to convert -
> but if your VB-forms are mostly slightly augmented copies differing
> only slightly, the GUI actually might benefit most from a rewrite,
> if the business rules are written redundance poor.
>
> One of the typical scenarios asking for GUI inheritance
> is insurance - customer data is only specific for few fields
> and even policy field groupings resemble each other across
> similar policies. A "minmally redundant" business layer could
> even in VB be implemented without too much sweat- for instance
> by having methods overwritten in a inheritance based OOP -
> design as name mangled methods on objects responsible across
> similar problem domains, keeping the "common" methods clean.

Unfortunately, there is little duplication across the different screens. 
There definitely is some, like screens for picking a date range of a 
report, with the reports being hardcoded (in our internal report writing 
format) behind the print button on the screen. Here, the report should 
be moved to a different module, or imported from a special format report 
file, but the number of these types of screens are few and far between.

> Check your code with a critical eye -
> even code forced to be totally OOP for
> 
> technical reason=langauge
> 
> can be written across a wide spectrum of quality.
> 
> If there is nothing which is good enogh to be converted last
> or no segregation/layering at all in the current program,
> (even if it came from the DOS dinosaurs, there were
> "good coding practices" known - some of the old "libraries"
> were better decoupled than modern day OOP class libraries.)
> get rid of most of the people responsible and start only then.

I think what needs to happen, is good coding guidelines put into place, 
with a language that supports proper OOP. Inheritance doesn't even work 
in VB, let alone polymorphism, unless you want to count passing Variants 
and figuring out the types and then returning a variant.

> my 0.02 EUR

Thanks for your thoughts. It gives me some things to think over.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: VB to Python migration

2006-01-30 Thread Josh
Magnus Lycka wrote:
  > mxODBC is probably the best way to talk to Jet in a way (DB API)
> that makes it reasonably easy to port the data to another RDBMS.
> Writing multi RDBMS applications is a whole chapter in itself,
> if not a subject for a whole book.
> 
> Whether to use an ORM such as SQLObject on top of that is another
> issue...

mxODBC looks interesting. I'll need to research it some more.

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


Re: VB to Python migration

2006-01-30 Thread Josh
Magnus Lycka wrote:
>> Rewrites are always good and result in smaller code base if features
>> are not added. However, I doubt that will make the screens fewer. Lines
>> of code? Certainly.
> 
> 
> That depends on whether you just refactor the implementation
> of if you look at the problem domain with a different perspective
> than you had the first time around. Obviously, I don't know anything
> about this particular system, but with 230 screen it seems like
> some things could be generalized. I suspect a much smaller number
> of slightly context sensitive screens would suffice... My gut
> tells me that if I looked at the system, I'd find lots of screens
> that were basically the same, just slightly different... Whether
> to use the same actual screen and perhaps let it be modified in runtime
> due to some data driven approach or share most code in base classes
> and have small modules with the specialized behaviour is something
> to argue about in some design meetings. I'll leave that to the guys
> who have to do it.

For a little background, this is an ERP program. It looks after 
accounting, customer orders, purchase orders, inventory, production 
planning/scheduling, and product development. It is an industry specific 
program, designed for the mattress manufacturing industry.

I'm not sure how many screens could be cut out. On the accounting side 
of the program, there are a number that could be cut down I think. Right 
now, Accounts Recievable and Accounts Payable are each separate sets of 
screens. On a rewrite, we could make classes that are generalized for 
accounts and then inherit these and modify them based on whether it's a 
debit or credit account. This is mostly an educated guess since I don't 
have a full understanding of the accounting side.

Other parts of the program I'm not so sure about. With proper OOP, I'm 
sure some more screens could be cut out, or standardized using 
inheritance and such.

>> You are comparing Python with VB 6. That's not what the OP is moving to
>> as an alternative.
> 
> 
> No, but it's what they've used and know. It's the reference for
> their thinking.
> 
>> It's .NET. VB 6 has been out of development since 8
>> years? Its not something to be compared to anymore. .NET on the other
>> hand has all the advantages of Python accessible toolkits for this task
>> and then some.
> 
> 
> But as indicated by the OP, there might be reasons not to
> sit down in that warm and soft lap from Redmond once more...
> 
> I assume they don't consider Python as a chance to avoid having
> to really understand OO, or to be able to stay with the current
> code base. Avoiding vendor lock-in is a very reasonable aspect,
> and making it easier to port to non-windows platforms is also a
> reasonable aspect.

I think in some ways, going with Python rather than just moving to 
VB.NET comes from a desire to change the architecture of the program. 
Going to VB.NET, we would be tempted to run the code through the upgrade 
wizard and just get things working. We wouldn't be forced to change the 
underlying architecture, we could just stay with the status quo.

Moving to Python would make us sit back and take a closer look at how 
things are done and make taking shortcuts harder.

>> I am not saying Python is unfit for 230 screens. Just that I have not
>> seen anyone build such a thing and better write about it.
> 
> 
> Right, and this is interesting. After about 25 years of
> programming I'm still not sure whether I should be impressed
> or worried when I hear people describing systems with
> hundreds of screens and hundreds of database tables.
> 
> I've worked with systems like that, and while I'm sure there
> are situations where such big systems are really warranted,
> the systems of this size that I saw could probably have been
> shrunk to a quarter of their size if proper software development
> practices had been used.

I understand what you are saying, and I'm sure the tasks our program 
does could be made much cleaner. But, implementing an ERP which is 
basically what we have, is a large project and the users need (or maybe 
just want) access to lots of information.

>> People on the
>> other hand have build web sites with such volume. Result? We have a ton
>> of web application frameworks to address such needs.
> 
> 
> I'm not 100% sure that the plethora of Python web kits is entirely
> a benefit.


>>> The really silly thing to do is to apply VB idioms on Python.

I completely agree with this point. We want to get away from the way 
that VB makes us think and therefore program. It's hard to implement OOP 
in a language that won't support inheritance or polymorphism except 
through the outrageous contortions, and then not even do it properly.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: VB to Python migration

2006-01-30 Thread Josh
Steven D'Aprano wrote:
> I suggest you hire a couple of experienced Python developers, pick half a
> dozen screens, say three simple screens and three complex ones, and build
> them in Python. That will give you an idea of how much work is involved.
> Don't get your VB developers to try to port it themselves, that's a recipe
> for disaster: they will be trying to learn Python, and will end up writing
> VB code in Python, which is sure to be slow, inefficient, bulky and buggy.
> 
> You may also like to think about using a web front end. If your GUI app
> only uses simple controls like text fields and push buttons, a web front
> end might simplify things a lot.
> 
> Good luck.

Point taken on VB coders writing Python. I think part of this though, is 
having programmers who are willing to learn new paradigms. I agree that 
there could be many teething problems, but knowing the team, I believe 
that after doing a few modules/classes (2 or 3 months, maybe more) we 
could work out the bugs, and go on to new problems.

Having worked out problems with the first classes, as long as there are 
no major architectural problems, we could continue on with the rest of 
the program. Once good headway on the project, come back to those first 
converts and refactor them, using knowledge accumulated from the last 6 
months in Python.

Maybe I'm just being overly optimistic, but I think the VB programmers 
we have can make the jump to Python. Their first lines of Python will 
just be VB in Python, but, once the power of Python becomes apparent, 
there'll be no going back.

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


Re: VB to Python migration

2006-01-31 Thread Josh
Magnus Lycka wrote:
  > I suppose that you need to present a lot of differnt kinds of data,
> and that you need to provide various search parameters etc for
> different data sets, but this sounds like something that might be
> very few screens that adapt to some kind of meta-data, perhaps XML
> descriptions or small "configuration files" containing som simple
> Python data structures and possibly input validation routines etc.
> Maybe this information should even be in the database?

This all makes sense. Being hamstrung by the VB model has hampered our 
thinking in areas like this I think. Having a smaller number of dynamic, 
data driven screens is good for maintainability anyways.

> The problem arises if there is behavior in the application that
> varies with data, but with Python, it's certainly possible to handle
> that to some extent even if these screens are driven by database
> content.
> 
> BTW, I guess DABO http://dabodev.com/ is intended just for this kind
> of application, but I doubt it was ever used for a project of this
> size, it's fairly new... Never used it myself...

I have looked through the DABO website and it looks very promising. By 
the looks of it, the authors come from the same type of background 
(Windows RAD application) as we are coming from. I'll have to download 
it and give it a run for it's money.

> Whatever you use for your user interface, I suggest that you use
> a multi-tier approach, and a very thin user interface layer. In my
> opinion that makes testing much easier. I'd recommend that you use
> a proper test tool and employ a test-driven approach.

This is what we're planning on. If the user interface is as thin as 
possible, that allows easily porting it to a web app etc.

As for the testing, that's something we'll need to learn about. I've 
read some articles about test driven programming in relation to extreme 
programming. Can you give me links to any good sites explaining how this 
all works? (Off list may be better for these off-topic links)

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


pythonesque constructs for c++

2007-09-12 Thread Josh
One of the best features of python is its ease of use, and the ease of use 
of its modules. Modules like os, os.path, and datetime are invaluable!

Does anyone know of analagous c++ libraries? It seems to me that most of the 
functionality of these modules could easily be replicated in c++. Before I 
go about doing that, I was just wondering if anyone knows if they already 
exist?

thanks,
Josh


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


how to abort on syntax errors

2007-03-26 Thread Josh
I have a lot of except Exception, e statements in my code, which poses some 
problems. One of the biggest is whenever I refactor even the triviallest 
thing in my code.

I would like python to abort, almost as if it were a compile-time error, 
whenever it cannot find a function, or if I introduced a syntax error. But, 
instead, it merrily proceeds on its way.

Is there some idiom that you use in situations like these?
thanks,
josh


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


Re: Weird behavior in search in a list

2007-03-29 Thread Josh

"Su Y" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
On 3ÔÂ29ÈÕ, ÏÂÎç7ʱ51·Ö, "Su Y" <[EMAIL PROTECTED]> wrote:
> hi all,
> I can't understand how this code work, its behavior is really weird
> for me...
>
> I want find the first number in extend[] which is larger than num, soI 
> wrote:
>
> def find(num):
> count=0
> for elem in extend:
> if elem count+=1
  else:
 break
> return count
>
you need to break out of the loop when you first encounter num>elem. The 
reason it works in your sorted list scenario is because elem will be > num, 
always, after some point. It won't be in your unsorted list.

I've added the else: break in your code above
j


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

help with a problem from school??

2008-01-09 Thread Josh
Hello all I did a Google search and found this site and was hoping someone 
could help me with what I am sure is a simple question that I cannot figure 
out. Here goes:
 
Given a simple straight through switch (SPST) with a supply of 14V, 
and the need to simulate its intended load of 14mA, what would you use to 
simulate this load?  Please show your calculations used to support your answer.



I appreciate anyone's help.

Josh Smith

[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Quick Newbie Question

2008-11-28 Thread Josh
Can Python be used on one Linux machine to drive another Linux machine 
through SSH? I am currently running Putty on my XP box to run tests on a 
Linux box. I need to automate these tests and thought it would be fun to 
 do so from a Linux VMWare Image I recently setup. Does this sound 
do-able without too much effort? I already know the Linux commands I 
need to run but just need an interactive shell connection through SSH. 
Again is Python a good choice for this or something else?

Thanks,

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


Emacs vs. Eclipse vs. Vim

2008-11-29 Thread Josh
If you were a beginning programmer and willing to make an investment in 
steep learning curve for best returns down the road, which would you pick?


I know this topic has been smashed around a bit already, but 'learning 
curve' always seems to be an arguement. If you feel that one is easier 
or harder than the others to learn feel free to tell, but let's not make 
that the deciding factor. Which one will be most empowering down the 
road as a development tool?


Thanks in advance,

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


Re: Quick Newbie Question

2008-11-29 Thread Josh

alex23 wrote:

On Nov 29, 5:09 pm, Josh <[EMAIL PROTECTED]> wrote:

Can Python be used on one Linux machine to drive another Linux machine
through SSH? I am currently running Putty on my XP box to run tests on a
Linux box. I need to automate these tests and thought it would be fun to
  do so from a Linux VMWare Image I recently setup. Does this sound
do-able without too much effort? I already know the Linux commands I
need to run but just need an interactive shell connection through SSH.
Again is Python a good choice for this or something else?
Thanks,


For something like this, I'd probably use Pexpect:

http://www.noah.org/wiki/Pexpect#Overview


Cool, thanks I'll check it out.
--
http://mail.python.org/mailman/listinfo/python-list


Different times between Python and System

2008-04-14 Thread Josh
Hi,

Anyone know why python would not show the same time that my system
shows?

[EMAIL PROTECTED]:~$ date
Mon Apr 14 01:27:36 MDT 2008
[EMAIL PROTECTED]:~$ python
Python 2.4.5 (#2, Mar 12 2008, 00:15:51)
[GCC 4.2.3 (Debian 4.2.3-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2008, 4, 14, 1, 27, 50, 472350)

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


Re: Different times between Python and System

2008-04-14 Thread Josh
Hmm...  That didn't work out so well that time.  I feel like an
idiot.  Previously there has been an hour difference between the
system time and the time that python reports.

On Apr 14, 1:29 am, Josh <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Anyone know why python would not show the same time that my system
> shows?
>
> [EMAIL PROTECTED]:~$ date
> Mon Apr 14 01:27:36 MDT 2008
> [EMAIL PROTECTED]:~$ python
> Python 2.4.5 (#2, Mar 12 2008, 00:15:51)
> [GCC 4.2.3 (Debian 4.2.3-2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> 
> import datetime
> >>> datetime.datetime.now()
>
> datetime.datetime(2008, 4, 14, 1, 27, 50, 472350)
>
> Thanks!

-- 
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: 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


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


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 "", line 1, in 
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'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 "", line 1, in 
> > 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


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:


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')


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



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')


This results in:


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 []



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:


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


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


Re: why not?

2013-01-21 Thread Josh Benner
On Mon, Jan 21, 2013 at 7:56 AM, Lie Ryan  wrote:

> On 22/01/13 04:02, kwakukwat...@gmail.com wrote:
>
>> f = open(r'c:\text\somefile.txt')
>> for i in range(3):
>> print str(i) + ': ' + f.readline(),
>> please with the print str(i) + ‘: ‘ + f.readline(), why not print str(i)
>> + f.readline(),
>>
>
> Try running both code. What do you see? What's the difference? When do you
> think you might want one or the other?
> --
> http://mail.python.org/**mailman/listinfo/python-list
>


There is nothing 'wrong' with either print statement.  The why or why not,
depends on the requirements of who or what intends to 'consume' the output.
 In other words, what problem does this code solve?  Think about which
print statement produces the best output for that problem.

It is also always a good idea to close the file object when you are done
with it.  Consider executing file operations inside a 'with' block.

with open(r'c:\text\somefile.txt') as f:
for i in range(3):
print str(i) + ': ' + f.readline()


The above code can be generalized further for use with text files that
contain a variable number of lines.

with open(r'c:\text\somefile.txt') as f:
for index, line in enumerate( f ):
print str( index ) + ': ' + f.readline()
-- 
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


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


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


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):


   This 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.




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: 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


Re: %d not working in re at Python 2.7?

2012-05-14 Thread Josh McGee
%

On Friday, May 11, 2012 11:58:01 AM UTC-7, vacu wrote:
> I am frustrated to see %d not working in my Python 2.7 re.search, like
> this example:
> 
> >>> (re.search('%d', "asdfdsf78asdfdf")).group(0)
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'NoneType' object has no attribute 'group'
> 
> 
> \d works fine:
> 
> >>> (re.search('\d+', "asdfdsf78asdfdf")).group(0)
> '78'
> 
> 
> And google search ignores % in their search, so I failed to find
> answer from Python mailing list or web,
> Do you have any idea what's problem here?
> 
> Thanks a head
> Vacu

%d and %s and such are format strings, not regex.
-- 
http://mail.python.org/mailman/listinfo/python-list


understanding operator overloading

2012-06-01 Thread Josh Benner
Is there a good way to trace what's going on under the hood wrt operator
overloading?

I am trying to understand what is happening in the code and output listed
below.

Why doesn't __getitem__ in mylist return the same result as the builtin
list object?
Does it have something to do with the start and stop arguments to slice?
Is the slice object making the call to __len__?



code___

class mylist():
def __init__(self, data):
self.data = data
def __len__(self):
print('len(self.data) -> self.data = {0}'.format(self.data))
return len(self.data)
def __getitem__(self, index):
print('__getitem__(index) -> index = {0}'.format(index))
return self.data[index]

if __name__ == "__main__":
alist = [1, 2, 4]
blist = mylist(alist)
print('printing alist[-4:]')
print(alist[-4:])
print('printing blist[-4:]')
print(blist[-4:])
print('printing blist[-2]')
print(blist[-2])


output_

printing alist[-4:]
[1, 2, 4]
printing blist[-4:]
len(self.data) -> self.data = [1, 2, 4]
__getitem__(index) -> index = slice(-1, 9223372036854775807, None)
[4]
printing blist[-2]
__getitem__(index) -> index = -2
2


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


Re: text file

2011-06-30 Thread Josh Benner
import os

lst = []
for x in xrange(1, 5001):
lst.append(r"Data\ma{0}.wav Data\ma{0}.mfc".format(x))
lst.insert(x-1, r"Data\ja{0}.wav Data\ja{0}.mfc".format(x))

with open("filename.txt", "w") as fd:
sep = os.linesep
fd.write(sep.join(lst))


On Thu, Jun 30, 2011 at 5:19 PM, Siboniso Shangase
wrote:

> Hi
> i m very new to python and i need hepl plz!!
>
> i want to type this data in a text file it the same the diffrence is
> the number that only increase and i canot write this up myself since
> it up to 5000 samples
>
> Data\ja1.wav Data\ja1.mfc
> Data\ja2.wav Data\ja2.mfc
> Data\ja3.wav Data\ja3.mfc
> Data\ja4.wav Data\ja4.mfc
> .
> .
> .
> .
> Data\ja(n).wav Data\ja(n).mfc
>
> Data\ma1.wav Data\ma1.mfc
> Data\ma2.wav Data\ma2.mfc
> Data\ma3.wav Data\ma3.mfc
> Data\ma4.wav Data\ma4.mfc
> .
> .
> .
> Data\ma(n).wav Data\ma(n).mfc
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
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: 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


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-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


removing nested iffs

2011-07-29 Thread Josh Benner
I'm writing a function to create a string that gets longer iff an argument
is defined.  In there a more elegant way than nesting all those ifs?

def format_rsync_src_string(args, server="RSYNC"):
""" Format an rsync source directory string. """
if args.server is None:
raise CopyNightlyError("No rsync server provided.")
src = "{0}::".format(args.server)
if args.project not None:
src += "{0}/".format(args.project)
if args.version not None:
src += "{0}/".format(args.version)
if args.build not None:
src += "Build {0}".format(args.build)
return src
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removing nested iffs

2011-07-29 Thread Josh Benner
On Fri, Jul 29, 2011 at 1:07 PM, Wolfgang Rohdewald
wrote:

> On Freitag 29 Juli 2011, Josh Benner wrote:
> > if args.build not None:
>
> which python version understands this?
>
> --
> Wolfgang
>

Apparently the version running in my head understands it ;-)

Sorry about that how about this (replacing 'not' with 'is not' where
appropriate):


def format_rsync_src_string(args, server="RSYNC"):
""" Format an rsync source directory string. """
if args.server is None:
raise CopyNightlyError("No rsync server provided.")
src = "{0}::".format(args.server)
if args.project is not None:
src += "{0}/".format(args.project)
if args.version is not None:
src += "{0}/".format(args.version)
if args.build is not None:
src += "Build {0}".format(args.build)
return src
-- 
http://mail.python.org/mailman/listinfo/python-list


import hooks (PEP 302) broken in Python >=2.5?

2011-08-07 Thread Josh Haberman
When reading about import hooks, I came across a blog entry comment
that says:

  One additional thing to note about ihooks is that it's
  somewhat seriously broken on Python 2.5 and newer and there
  seems to be little or no interest in fixing it. It's
  probably worth *always* avoiding when writing new code.

--http://orestis.gr/blog/2008/12/20/python-import-hooks/#c264

Does anyone know what this is referring to?  Should I be wary of
relying on import hooks?

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


Re: Help with regular expression in python

2011-08-18 Thread Josh Benner
On Thu, Aug 18, 2011 at 4:03 PM, Matt Funk  wrote:

> Hi guys,
>
> thanks for the suggestions. I had tried the white space before as well (to
> no
> avail). So here is the expression i am using (based on suggestions), but
> still
> no success:
>
> instance_linetype_pattern_str =\
>r'(([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+))?\s+){32}(.+)'
> instance_linetype_pattern = re.compile(instance_linetype_pattern_str)
> results = instance_linetype_pattern.findall(line)
> print "results: "; print results
>
>
> The match i get is:
> results:
> [('2.199000e+01 ', '2.199000', '.199000', 'e+01', ': (instance: 0)\t:\tsome
> description')]
>
>
> btw: The line to be matched (given below) is ONE line. There are no line
> breaks (even though my email client adds them).
>
>
> matt
>
>
>
If a group matches multiple times, only the last match is accessible.  The
matches returned represent the inner groupings of the last match found.

JB-)
-- 
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


Windows alternative: multiprocessing.connection.wait on Pipe, Tkinter File Handlers

2017-10-23 Thread Josh Jacobson
The two functions in the subject are not fully implementable on Windows,
and so I am looking for an alternative.

Relevant SO postings including full code and description, with bounty:
Freezes
,
Input




I have two scripts:

*Processor_child.py*: Its purpose is to perform a number of data analysis
and cleaning operations. This must perform the same operations when run
alone (without Tkinter_parent.py) as it does when packaged into a GUI with
Tkinter_parent.py.

*Tkinter_parent.py*: Its purpose is to provide a GUI for those who can't
use Processor_child directly.Within Processor_child, there are for loops
that ask the user for input on each iteration. These prompts need to appear
in the Tkinter app, accept the input, and send it back to Processor_child.



The main issue is having the script not move forward on execution until
input has been sent.

I know of three ways to theoretically solve this, but *none of these work
in production on a Windows machine*:

1. while pipe1.poll() != True:

time.sleep(0.1)

This causes constant loading and a "freezing" like user experience.

2. multiprocessing.connection.wait

>From the documentation: ""Windows: ... Note that pipe handles and socket
handles are not waitable handles."

3. Tkinter file handlers

>From the documentation: "This feature is not available on Windows."


*Given that none of the 3 available options works on Windows, what options
are available for Windows machines?*

See Tkinter application 'freezes' while continually polling Pipe for
contents (multiprocessing)

and How can I implement an `input` method in a Tkinter parent script, with
the displayed prompt and return value being sent back to a child script?

for further description and code samples.
-- 
https://mail.python.org/mailman/listinfo/python-list


__hash__ and ordered vs. unordered collections

2017-11-20 Thread Josh B.
Suppose we're implementing an immutable collection type that comes in unordered 
and ordered flavors. Let's call them MyColl and MyOrderedColl.

We implement __eq__ such that MyColl(some_elements) == 
MyOrderedColl(other_elements) iff set(some_elements) == set(other_elements).

But MyOrderedColl(some_elements) == MyOrderedColl(other_elements) iff 
list(some_elements) == list(other_elements).

This works just like dict and collections.OrderedDict, in other words.

Since our collection types are immutable, let's say we want to implement 
__hash__.

We must ensure that our __hash__ results are consistent with __eq__. That is, 
we make sure that if MyColl(some_elements) == MyOrderedColl(other_elements), 
then hash(MyColl(some_elements)) == hash(MyOrderedColl(other_elements)).

Now for the question: Is this useful? I ask because this leads to the following 
behavior:

>>> unordered = MyColl([1, 2, 3])
>>> ordered = MyOrderedColl([3, 2, 1])
>>> s = {ordered, unordered}
>>> len(s)
1
>>> s = {ordered}
>>> unordered in s
True
>>> # etc.

In other words, sets and mappings can't tell unordered and ordered apart; 
they're treated like the same values.

This is a bit reminiscent of:

>>> s = {1.0}
>>> True in s
True
>>> d = {1: int, 1.0: float, True: bool}
>>> len(d)
1
>>> # etc.

The first time I encountered this was a bit of an "aha", but to be clear, I 
think this behavior is totally right.

However, I'm less confident that this kind of behavior is useful for MyColl and 
MyOrderedColl. Could anyone who feels more certain one way or the other please 
explain the rationale and possibly even give some real-world examples?

Thanks!

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


Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread Josh B.
On Monday, November 20, 2017 at 1:55:26 PM UTC-5, Chris Angelico wrote:
> But what you have is the strangeness of non-transitive equality, which
> is likely to cause problems.

But this is exactly how Python's built-in dict and OrderedDict behave:

>>> od = OrderedDict([(1, 0), (2, 0), (3, 0)])
>>> od2 = OrderedDict([(3, 0), (2, 0), (1, 0)])
>>> ud = dict(od)
>>> od == ud
True
>>> od2 == ud
True
>>> od == od2
False


Given that, it would seem wrong for our MyOrderedColl.__eq__ to not behave 
similarly.

Or are you suggesting that OrderedDict.__eq__ should not have been implemented 
this way in the first place?


> So the question is: are you willing to
> accept the bizarre behaviour of non-transitive equality?

Forget what I'm personally willing to do :)
The question here actually is to tease out what Python's existing design is 
telling us to do.

If it helps, substitute "frozenset" for "MyColl" and "FrozenOrderedSet" for 
"MyOrderedColl". How would you implement their __eq__ methods? What would be 
the correct design for our hypothetical frozen(ordered)set library? What would 
be more useful, intuitive, and usable for our users?

Thanks very much for the good examples and for helping me clarify the question!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __hash__ and ordered vs. unordered collections

2017-11-20 Thread Josh B.
On Monday, November 20, 2017 at 2:31:40 PM UTC-5, MRAB wrote:
> What if there are duplicate elements?
> 
> Should that be MyColl(some_elements) == MyOrderedColl(other_elements) 
> iff len(some_elements) == len(other_elements) and set(some_elements) == 
> set(other_elements)?

Yes, that's what I meant. Thanks for catching :)

Please let me know if you have any thoughts on how you would design our 
hypothetical frozen(ordered)set library to behave in these cases.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __hash__ and ordered vs. unordered collections

2017-11-21 Thread Josh B.
On Monday, November 20, 2017 at 3:17:49 PM UTC-5, Chris Angelico wrote:
> Neither is perfect. You have to take your pick between them.

Right on, thanks for weighing in, Chris. Your responses have been very helpful.

I wouldn't feel comfortable claiming the authority to make this call alone. But 
fortunately I reached out to Raymond Hettinger and am delighted to have his 
guidance, pasted below. Great to have this predicament resolved.

In case of interest, I've implemented Raymond's advice in the latest release of 
bidict, the bidirectional map library I authored <http://bidict.rtfd.io>. 
Feedback always welcome.

Thanks,
Josh

-- Forwarded message --
From: Raymond Hettinger 
Date: Mon, Nov 20, 2017 at 4:46 PM
Subject: Re: __hash__ and ordered vs. unordered collections
To: j...@math.brown.edu


If you want to make ordered and unordered collections interoperable, I would 
just let equality be unordered all the time.  You can always provide a separate 
method for an ordered_comparison.

IMO, the design for __eq__ in collections.OrderedDict was a mistake.  It 
violates the Liskov Substitution Principle which would let ordered dicts always 
be substituted whereever regular dicts were expected.  It is simpler to have 
comparisons be unordered everywhere.

But there are no perfect solutions.  A user of an ordered collection may 
rightfully expect an ordered comparison, while a user of both collections may 
rightfully expect them to be mutually substitutable.


Raymond
-- 
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: 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 
>  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


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


Best Practices for Internal Package Structure

2016-04-04 Thread Josh B.
My package, available at https://github.com/jab/bidict, is currently laid out 
like this:

bidict/
├── __init__.py
├── _bidict.py
├── _common.py
├── _frozen.py
├── _loose.py
├── _named.py
├── _ordered.py
├── compat.py
├── util.py


I'd like to get some more feedback on a question about this layout that I 
originally asked here: 
<https://github.com/jab/bidict/pull/33#issuecomment-193877248>:

What do you think of the code layout, specifically the use of the _foo modules? 
It seems well-factored to me, but I haven't seen things laid out this way very 
often in other projects, and I'd like to do this as nicely as possible.

It does kind of bug me that you see the _foo modules in the output when you do 
things like this:

>>> import bidict
>>> bidict.bidict

>>> bidict.KeyExistsError



In https://github.com/jab/bidict/pull/33#issuecomment-205381351 a reviewer 
agrees:

"""
Me too, and it confuses people as to where you should be importing things from 
if you want to catch it, inviting code like

```
import bidict._common

try:
...
except bidict._common.KeyExistsError:
...
```
ie. becoming dependent on the package internal structure.

I would be tempted to monkey-patch .__module__ = __name__ on each imported 
class to get around this. Maybe there are downsides to doing magic of that 
kind, but dependencies on the internals of packages are such a problem for me 
in our very large codebase, that I'd probably do it anyway in order to really 
explicit about what the public API is.
"""

Curious what folks on this list recommend, or if there are best practices about 
this published somewhere.

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


Understanding Python Documentation

2005-11-24 Thread Josh Cronemeyer
Hi,

I have very little experience programming in python but considerable 
experience with java.  One thing that is frustrating me is the differences in 
the documentation style.  Javadocs, at the top level are just a list of 
packages.  Drilling down on a package reveals a list of classes in that 
package, and drilling down on a class reveals a list of methods for that 
class.  Is there something similar for python?  

The closest thing I have found to this for python is 
http://www.python.org/doc/2.4.2/modindex.html  which really isn't the same 
thing at all.

wxpython has their documentation like this http://www.wxpython.org/docs/api/  
is there something like this for the rest of python? 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Understanding Python Documentation

2005-11-24 Thread Josh Cronemeyer
On Thursday 24 November 2005 09:27 am, Simon Brunning wrote:
> On 24/11/05, Josh Cronemeyer <[EMAIL PROTECTED]> wrote:
> > I have very little experience programming in python but considerable
> > experience with java.  One thing that is frustrating me is the
> > differences in the documentation style.  Javadocs, at the top level are
> > just a list of packages.  Drilling down on a package reveals a list of
> > classes in that package, and drilling down on a class reveals a list of
> > methods for that class.  Is there something similar for python?
> >
> > The closest thing I have found to this for python is
> > http://www.python.org/doc/2.4.2/modindex.html  which really isn't the
> > same thing at all.
>
> I think it is, really. Thing is, Python's standard library is broader
> and less nested in structure than Java's, so it stands to reason that
> its documetation will be broader and less nested in structure too.
>
> --
> Cheers,
> Simon B,
> [EMAIL PROTECTED],
> http://www.brunningonline.net/simon/blog/

It is true about the nature of Python's standard library.  But when dealing 
with a large set of methods, for example, the OS module, it is nice to have 
the javadoc API style documentation.  You can see a quick summary of what is 
available, then if you want more detail you drill down on that particular 
method.  Oh well.  I'll get used to it :)

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


Announcement: AsyncSocket module

2005-04-14 Thread Josh Close
I wrote an asynchronous socket module because asyncore and asynchat
didn't quite fit my needs. AsyncSocket has support for connection and
read/write timeouts. It is currently only supported in linux.

It's been stable for me in all my uses so far. I'd like people to give
it a try and let me know what they think, and any bugs that are found.

https://sourceforge.net/projects/asyncsocket/

Thanks.

-Josh
--
http://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


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


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


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


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: Keeping track of things with dictionaries

2014-04-07 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: [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


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


  1   2   3   >