[Tutor] system()? popen2()? How to execute a command & save its output?

2010-09-29 Thread James Hartley
I'm needing to transfer the following shell construct to Python, plus save
the output of execution:

FTP_SITE='ftp.somesite.com'
ftp -a  $FTP_SITE <___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] system()? popen2()? How to execute a command & save itsoutput?

2010-09-30 Thread James Hartley
On Thu, Sep 30, 2010 at 7:27 AM, R. Alan Monroe wrote:

>
> >> I'm needing to transfer the following shell construct to Python,
> >> plus save
> >> the output of execution:
>
> >> FTP_SITE='ftp.somesite.com'
> >> ftp -a  $FTP_SITE < >> binary
> >> prompt off
> >> cd /some_dir
> >> dir
> >> bye
> >> EOF
>
> NB: If you use the ftp module (which works great), be sure to open all
> files in Binary mode. Voice of experience here.


Thanks all, for the responses thus far.  I was not aware of the ftplib
module.  This looks like it will take care of most of my needs.

However, output from these FTP commands are sent to stdout.  Is there a way
to redirect/capture stdout, as I need to parse the directory listings?

Thanks, again.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regex Ordering

2007-07-23 Thread James Hartley
On 7/23/07, Shidan <[EMAIL PROTECTED]> wrote:
> I'm looking for a Python module that provides methods for ordering
> regexes based on
> how general they are ( how much they match). Do I have to write it

Your question is relative.  Classifying which regular expression is
more general depends upon the other regular expressions used in
comparison along with the specific input.  As you change input &
regular expressions, the ordering will change.

Yes, you will need to write this yourself.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] opening a pipe?

2008-02-12 Thread James Hartley
A Perl script can easily serve as a filter within a pipe as seen in
the following:

use strict;
use warnings;

open(IN, 'cat hello.txt |') or die 'unable to open file';
while () {
print;
}
close(IN);

Can I do the same within Python?  Thanks.

Jim
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sorting dictionary keys?

2008-05-13 Thread James Hartley
I suspect this is a brain-dead question...

Given the following code, output is as expected:

$ cat test.py
d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }

for i in d.keys():
print "%s\t%s" % (i, d[i])
$ python test.py
a   1
c   0
b   3
d   2
$

But if the keys are sorted, I get an error:
$ cat test1.py
d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 }

for i in d.keys().sort():
print "%s\t%s" % (i, d[i])
$ python test1.py
Traceback (most recent call last):
  File "test.py", line 3, in 
for i in d.keys().sort():
TypeError: 'NoneType' object is not iterable
$

What is the correct manner to iterate through sorted dictionary keys?

Thanks.

Jim
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] use of logging module is shared by all?

2011-09-01 Thread James Hartley
I'm just needing to verify some behavior.

Functionality within the logging module is exercised by calling functions
defined within the module itself.  I am using SQLAlchemy for database
access, but it can be configured to dump out intermediate access information
& queries to the logging module -- which is great.  It really helps in
debugging.

However, if I want to write to a log which is separate from what SQLAlchemy
is doing, am I correct stating that I will not be able to do so through the
logging module?

Thanks for any insight which can be shared.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] use of logging module is shared by all?

2011-09-01 Thread James Hartley
Thanks, Peter for taking the time to respond. I need to study the reference
further, & your comments pointed out some of my misconceptions.  Thank you
for clearing up some of my half-researched understanding.

Jim

On Thu, Sep 1, 2011 at 10:53 PM, Peter Otten <__pete...@web.de> wrote:

> James Hartley wrote:
>
> > I'm just needing to verify some behavior.
> >
> > Functionality within the logging module is exercised by calling functions
> > defined within the module itself.  I am using SQLAlchemy for database
> > access, but it can be configured to dump out intermediate access
> > information
> > & queries to the logging module -- which is great.  It really helps in
> > debugging.
> >
> > However, if I want to write to a log which is separate from what
> > SQLAlchemy is doing, am I correct stating that I will not be able to do
> so
> > through the logging module?
> >
> > Thanks for any insight which can be shared.
>
> Loggers are typically organized in a tree; sqlalchemy will probably log to
>
> logging.getLogger("sqlalchemy").warn("whatever")
>
> or something like
>
> logging.getLogger("sqlalchemy.some.sublogger").critical("test")
>
> You can prevent the rootlogger from seeing these messages with
>
> sq = logging.getLogger("sqlalchemy")
> sq.propagate = False
>
> If for example you want to see messages in stdout by default, but write
> SQLAlchemy's messages to a file you'd do (untested)
>
> import logging
> import sys
> logging.basicConfig(stream=sys.stdout)
>
> sq = logging.getLogger("sqalchemy")
> sqhandler = logging.FileHandler("alchemy.log")
> sqformatter = logging.Formatter(logging.BASIC_FORMAT)
> sqhandler.setFormatter(sqformatter)
> sq.addHandler(sqhandler)
> sq.propagate = False
>
> sq.critical("alchemy") # goes to file alchemy.log
> logging.getLogger("mine").critical("mine") # goes to stdout
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] throwing exception across modules?

2011-09-08 Thread James Hartley
This is more a design question.

One lesson C++ programmers might learn is that throwing exceptions from
within library code is fraught with problems because the internals of
exception handling were spelled out in the C++ standard.  This manifests
itself as problems when the library was compiled with one vendor's compiler,
but the user of the library is using another compiler.

While I understand that Python doesn't suffer from this exact problem, are
there other reasons that raising exceptions in a module only be caught by
consumers of the module a bad idea?

Any insight which can be shared would be most appreciated.

Thanks.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] telnetlib's read_very_eager() method?

2011-09-14 Thread James Hartley
I'm trying to programmatically create a telnet session.  Within the
interactive environment, this appears to work very well as server messages
can be saved as strings:

$ python
Python 2.7.1 (r271:86832, Sep  3 2011, 01:32:33)
[GCC 4.2.1 20070719 ] on openbsd5
Type "help", "copyright", "credits" or "license" for more information.
>>> import telnetlib
>>> tn = telnetlib.Telnet('gmail-smtp-in.l.google.com', 25)
>>> s = tn.read_very_eager()
>>> print s
220 mx.google.com ESMTP r70si1582241yhm.54

>>> tn.write("helo\n")
>>> s = tn.read_very_eager()
>>> print s
250 mx.google.com at your service

>>> tn.write("quit\n")
>>> s = tn.read_very_eager()
>>> print s
221 2.0.0 closing connection r70si1582241yhm.54

>>>
$

These server response can then be parsed to determine what commands need to
be provided next.

Yet when I execute the same methods in a script, I am getting nothing in
response:

 $ cat script.py
#!/usr/bin/env python

import telnetlib

if __name__ == '__main__':
print 'begin'
tn = telnetlib.Telnet('gmail-smtp-in.l.google.com', 25)
s = tn.read_very_eager()
print s
tn.write("helo\n")
s = tn.read_very_eager()
print s
tn.write("quit\n")
s = tn.read_very_eager()
print s
print 'end'
$ python script.py
begin



end
$

What do I need to do to emulate the IDE environment?  If I am needing to
capture stdout, it isn't readily apparent.

Any insight you can share would be greatly appreciated.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] telnetlib's read_very_eager() method?

2011-09-14 Thread James Hartley
On Wed, Sep 14, 2011 at 12:54 PM, Walter Prins  wrote:

> Hence, try using read_some() instead of read_very_eager().
>

read_some() captures what I was hoping to catch.  Thanks!


>
> Aside: If you want to interact with a mail server there's probably better
> modules to be using than the telnet module.
>
>
Do you have any suggestions?

Thank you again for your quick reply.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] telnetlib's read_very_eager() method?

2011-09-14 Thread James Hartley
On Wed, Sep 14, 2011 at 1:13 PM, Walter Prins  wrote:

> On 14 September 2011 21:03, James Hartley  wrote:
>
>> On Wed, Sep 14, 2011 at 12:54 PM, Walter Prins  wrote:
>>
> Aside: If you want to interact with a mail server there's probably better
>> modules to be using than the telnet module.
>>
>>>
>>>
>> Do you have any suggestions?
>>
>
> The smtplib module: http://docs.python.org/library/smtplib.html
>

Wow, I wasn't aware this existed.  It appears to have what I was looking
for, too.  My goal was to validate email addresses in bulk, so this reduces
the code I need to write.  I also see that VRFY is disabled on some MX
servers, so I may have to resort to telnetlib's read_some() yet.

:-)

Thanks, again for your comments.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] accessing modules found throughout a package?

2015-10-17 Thread James Hartley
In my current project, I am developing a package.  It makes sense to embed
tests throughout the package's directory structure as they should be part
of the package & its distribution.  It may raise eyebrows that I have tests
sprinkled through various directories, but there are reasons for keeping
some tests in the same proximity as the modules needed to import data into
a database.  There are several directories importing data according to
different constraints.

The problem/annoyance I am facing is that tests need to access modules in
other directories, I have to play games at the beginning of each test file
which looks at os.path.realpath(__file__) to ascertain where the root of
the package can be found before calling os.path.append().  Since the
package is still under development, its structure is still in flux.  As a
result, any structural change requires that I re-adjust each test file
ensuring that the paths are still correct.

Here is the package's general structure:

+/package_directory/
   |
  +/data/
   + base_classes_used_by_some_tests.py
|
   +/import_1/
|+ test_code_requiring_base_classes_above.py
|
   +/import_2/
| + different_test_code_requiring_base_classes_above.py
|
   +/tests/
  |
 + more_test_code_requiring_base_classes_above.py

What is a better solution to minimize any tweaking forced upon the test
code?  It seems that formally installing the package will only remedy some
of the problems I am currently experiencing.

Any suggestions you may have will be very welcomed.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] relative imports within a package?

2015-11-07 Thread James Hartley
The Python 3 tutorial discusses relative imports at:

https://docs.python.org/3/tutorial/modules.html#intra-package-references

I have the following directory structure for a package in development:

+ outer_package/
 + __init__.py
 + inner_package
 | + __init__.py
 | + myclass.py
 + collateral_directory
   + arbitrary_tool.py

arbitrary_tool.py needs to instantiate the class MyClass found in
myclass.py.  I have made MyClass accessible by adding the following to
inner_package/__init__.py:

from myclass import MyClass

Within arbitrary_tool.py, I have attempted the following:

from ..inner_package import MyClass

...but executing with Python 3.4 yields the following stack trace:

Traceback (most recent call last):
  File "./arbitrary_tool.py", line 5, in 
from ..inner_package import MyClass
SystemError: Parent module '' not loaded, cannot perform relative import

Suggestions on how to solve this would be most certainly welcomed!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] enforcing specific types in Python 3.5?

2016-10-13 Thread James Hartley
I have implemented the equivalent of "insert if unique" in Python &
SQLAlchemy to help with data normalization.  However to help minimize the
number of preliminary SELECT statements needed, it helps to check types
through calls to isinstance() before getting to the salient code.
Unfortunately, the code begins to be cluttered with type-checking minutiae.

While researching this problem, I have found potential solutions like the
following:

http://stackoverflow.com/questions/9305751/force-python-class-member-variable-to-be-specific-type

...but given that Python 3.5 has syntactically understands gradual typing,
I have wondered whether addition of this feature offers anything over use
of property() as described above.  Toy examples have not revealed anything
useful on this front:

$ cat example.py
#!/usr/bin/env python

class Foobar():
def __init__(self, value):
self.value = value

def f(s: str) -> int:
print(s)
return 3.14

def main():
i = f('foobar')
print(type(i))
print('i = "{}"'.format(i))
i = f(Foobar(3))
print(type(i))
print('i = "{}"'.format(i))

if __name__ == '__main__':
main()
$ python example.py
foobar

i = "3.14"
<__main__.Foobar object at 0x85b8aaac>

i = "3.14"
$

I understand that gradual typing may be useful with static analysis, but I
don't see that any type enforcement occurs by default at runtime.  Am I
missing something here?   Is there a better solution for type enforcement?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] overriding brackets in lvalue assignment possible?

2016-12-27 Thread James Hartley
I can successfully override __getitem__() for rvalues, but the following
example shows that more is required when used as an lvalue:

===8<-
#!/usr/bin/env python

class Foo():
def __init__(self, n):
self.d = dict.fromkeys([i for i in range(0, n)])

def __getitem__(self, i):
return self.d[i]

def main():
foo = Foo(4)
print(foo[0])
foo[0] = 2   # not as an lvalue?
print(foo[0])

if __name__ == '__main__':
main()
===8<-

Python 3.4 generates the following output when this example is executed:

None
Traceback (most recent call last):
  File "./test.py", line 17, in 
main()
  File "./test.py", line 13, in main
foo[0] = 2
TypeError: 'Foo' object does not support item assignment

I am surprised that the error states that the object itself cannot accept
assignment.  From the C++ perspective, the underlying dictionary should be
exposed.  Does Python overloading allow use of bracket overriding in
lvalues?

Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] directory structure with tests?

2019-03-06 Thread James Hartley
It is preferable to sprinkle tests files throughout the directories of a
project, or coalesce all tests in a test directory?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Lengthy copyright notices?

2019-07-15 Thread James Hartley
help(module_name) will place any text in the *first* module-level docstring
into the description section of the help page in Python 3.4.5.  Subsequent
docstrings found at module level are ignored.

I have been using this factoid for placement of a copyright & licensing
notice.  By placing a rather lengthy copyright & license in the code in a
the second module-level docstring, it is prominent within the code, but not
cluttering up help() output.

Two questions.  Is there a more standardized way of including long license
descriptions in code, & is it documented that any other module-level
docstring will be ignored in help() output?

Thanks!

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] raising exceptions in constructor code?

2019-07-16 Thread James Hartley
I ask this having more C++ knowledge than sense.

There is an adage in the halls of everything Stroustrup that one needs to
think about how resource allocation will be unwound if an exception is
thrown.  This gets watered down to the mantra "Don't throw exceptions from
within constructors."  Does this carry over to Python?  I'm trying to
develop a Pythonistic mindset as opposed to carrying over old baggage...

Thanks!

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Inserting long URL's into comments & docstrings?

2019-07-29 Thread James Hartley
This should be a slow ball pitch.  Unfortunately, I haven't stumbled across
a reasonable answer yet.

On occasion, I put long URL's into comments/docstrings simply to document
where I found specific information.  However, to be a good disciple of
PEP8, anything which can't fit within 72 characters needs to be split
across multiple lines.  Since a number of you seem to be prolific Python
coders, what opinion do you have about splitting URL's in
comments/docstrings?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] class functions/staticmethod?

2019-08-11 Thread James Hartley
I am lacking in understanding of the @staticmethod property.
Explanation(s)/links might be helpful.  I have not found the descriptions
found in the Internet wild to be particularly instructive.  Given the code
below:
=8<--
from collections import namedtuple

class Foo():
Dimensions = namedtuple('Dimensions', ['height', 'width'])
_dimensions = Dimensions(3, 4)

def dimensions():
print('id = {}'.format(id(Foo._dimensions)))
return Foo._dimensions

@staticmethod
def dimensions1():
print('id = {}'.format(id(_dimensions)))
return _dimensions
=8<--
The class method Foo.dimensions() is capable of accessing class members,
but Foo.dimensions1() cannot. What does the @staticmethod decorator really
add?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Is nesting functions only for data hiding overkill?

2019-08-21 Thread James Hartley
Yes, nesting functions is valuable & necessary for closures and wrapping
functions for creating properties.  But is nesting, simply for hiding data,
a preferred solution?  I have a number of member functions which are
prefaced with underscores pointing out that they should not
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Is nesting functions only for data hiding overkill?

2019-08-21 Thread James Hartley
Yes, nesting functions is valuable & necessary for closures and wrapping
functions for creating properties.  But is nesting, simply for hiding data,
a preferred solution?  I have a number of member functions which are
prefaced with underscores pointing out that they should not be called by
client code.  Is nesting considered Pythonic?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor