Re: Is Python like VB?

2005-03-17 Thread Matthew
"Mike Cox" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> As you may or may not know, Microsoft is discontinuing Visual Basic in favor
> of VB.NET and that means I need to find a new easy programming language.  I
> heard that Python is an interpreted language similar to VB.  This means that
> it doesn't have all the hard stuff like pointers, classes and templates like
> C, C++ and assembly language.
> 
> Where I work we use  Microsoft Office with a lot of customization using
> Visual Basic.  I would like to switch to Python to do it since VB is being
> discontinued.  Would Python meet our requirements? I need to make lots of
> GUI applications (message boxes, forms, etc.) and do the underlying business
> logic too.
> 
> Thanks in advance.

The macro language used within Microsoft Office products is called VBA
- Visual Basic for Applications. It's similar to but not the same as
VB - Visual Basic.

You message hinted that it might be VBA, and not VB, that you use. If
it really is  VBA, then that MAY be unaffected by the drop of support
for VB.
-- 
http://mail.python.org/mailman/listinfo/python-list


hello

2004-12-17 Thread matthew
testing ...

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


Python / Win32 extensions compatibility with Windows XP

2005-04-12 Thread Matthew
Hi:

I recently installed Python 2.4 and the Win 32 extensions on
Windows XP.  I had some problems with the COM makepy utility for the
Excel COM libraries.  I reported this problem to the sourceforge bug
tracker.

My question is , is python 2.3 and the win32 extensions more stable
than Python 2.4?

Thank You

Matthew Harelick

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


pyOpenGL tom demo segmentation fault

2006-07-26 Thread matthew
Hi,

I have Python 2-4-2,
Numpy 24.2, and  PyOpenGL-2.0.2.01 install, along with glut 3.7.

The 1st 'funny' thing is that I always seem to get two pythons in
/usr/local/bin, ie python and python2.4.  These are exactly the same
timestamp and size.

The other problem is that the demos in ../site-packages/OpenGL/Demo/tom
crash with a segmentation fault, although others (eg in ../NeHe) work
OK.

I would be most grateful for any help ...

Thanks

Matt

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


Testing Python updates

2007-03-21 Thread Matthew
Hello:

What is the methodology for testing the updates to the Python
language?

Thanks

Matthew Harelick

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


Non-identifiers in dictionary keys for **expression syntax

2013-05-23 Thread Matthew Gilson
This is a question regarding the documentation around dictionary 
unpacking.  The documentation for the call syntax 
(http://docs.python.org/3/reference/expressions.html#grammar-token-call) 
says:


"If the syntax **expression appears in the function call, expression 
must evaluate to a mapping, the contents of which are treated as 
additional keyword arguments."


That's fine, but what is a keyword argument?  According to the glossary 
(http://docs.python.org/3.3/glossary.html):


/"keyword argument/: an argument preceded by an identifier (e.g. name=) 
in a function call or passed as a value in a dictionary preceded by **."


As far as I'm concerned, this leads to some ambiguity in whether the 
keys of the mapping need to be valid identifiers or not.


Using Cpython, we can do the following:

 def func(**kwargs):
  print kwargs

 d = {'foo bar baz':3}

So that might lead us to believe that the keys of the mapping do not 
need to be valid identifiers.  However, the previous function does not 
work with the following dictionary:


d = {1:3}

because not all the keys are strings.  Is there a way to petition to get 
this more rigorously defined?


Thanks,
~Matt



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


Re: Non-identifiers in dictionary keys for **expression syntax

2013-05-23 Thread Matthew Gilson


On 05/23/2013 03:20 PM, Neil Cerutti wrote:

On 2013-05-23, Matthew Gilson  wrote:

That's fine, but what is a keyword argument?  According to the glossary
(http://docs.python.org/3.3/glossary.html):

/"keyword argument/: an argument preceded by an identifier (e.g. name=)
in a function call or passed as a value in a dictionary preceded by **."

As far as I'm concerned, this leads to some ambiguity in
whether the keys of the mapping need to be valid identifiers or
not.

I don't see any ambiguity. A keyword argument is an argument
preceded by an identifier according to the definition. Where are
you perceiving wiggle room?

The wiggle room comes from the "or passed as a value in a dictionary" 
clause.  We sort of get caught in a infinite loop there because the 
stuff that can be passed in a dictionary is a keyword which is an 
identifer=expression or something passed as a value in a dictionary ...


Also the fact that:

 func(**{"foo bar baz":1})

works even though `foo bar baz` isn't a valid identifier, but:

 func(**{1:3})

doesn't work.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Non-identifiers in dictionary keys for **expression syntax

2013-05-23 Thread Matthew Gilson


On 05/23/2013 04:52 PM, Terry Jan Reedy wrote:

On 5/23/2013 2:52 PM, Matthew Gilson wrote:

This is a question regarding the documentation around dictionary
unpacking.  The documentation for the call syntax
(http://docs.python.org/3/reference/expressions.html#grammar-token-call)
says:

"If the syntax **expression appears in the function call, expression
must evaluate to a mapping, the contents of which are treated as
additional keyword arguments."

That's fine, but what is a keyword argument?  According to the glossary
(http://docs.python.org/3.3/glossary.html):

/"keyword argument/: an argument preceded by an identifier (e.g. name=)
in a function call or passed as a value in a dictionary preceded by **."


It appears that the requirement has been relaxed (in the previous 
quote), so that 'dictionary' should also be changed to 'mapping'. It 
might not hurt to add 'The key for the value should be an identifier.'


As far as I'm concerned, this leads to some ambiguity in whether the
keys of the mapping need to be valid identifiers or not.


I think you are being too lawyerly. The pretty clear and sensible 
implication is that the key for the value should be a string with a 
valid identifier. If it is anything else, you are on your own and 
deserve any joy or pain that results ;=)



Using Cpython, we can do the following:

  def func(**kwargs):
   print kwargs

  d = {'foo bar baz':3}

So that might lead us to believe that the keys of the mapping do not
need to be valid identifiers.


There are two ways to pass args to func to be gathered into kwargs; 
explicit key=val pairs and **mapping, or both.

func(a=1, b='hi', **{'foo bar baz':3})
#
{'foo bar baz': 3, 'a': 1, 'b': 'hi'}

So func should not expect anything other than identifier strings.

  However, the previous function does not

work with the following dictionary:

 d = {1:3}

because not all the keys are strings.


So CPython checks that keys are strings, because that is cheap, but 
not that the strings are identifiers, because that would be more 
expensive. Just because an implementation allow somethings (omits a 
check) for efficiency does not mean you should do it.


globals()[1] = 1
works, but is not normally very sensible or useful.


 Is there a way to petition to get this more rigorously defined?


bugs.python.org
The problem is that mandating a rigorous check by implementations 
makes Python slower to the detriment of sensible programmers 


To be clear, you're saying that

 func(**{'foo bar baz':3})

is not supported (officially), but it works in CPython because checking 
that every string in the dict is a valid identifier would be costly.  Of 
course that is sensible and I don't think the behaviour should be 
changed "to the detriment of sensible programmers".  However, it would 
be nice if it was documented somewhere that the above function call is 
something that a non-sensible programmer would do.  Perhaps with a 
"CPython implementation detail" type of block.

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


Building a HPC data assimilation system using Python?

2013-05-29 Thread Matthew Francis
I have a prototype data assimilation code ( an ionospheric nowcast/forecast 
model driven by GPS data ) that is written in IDL (interactive data language) 
which is a horrible language choice for scaling the application up to large 
datasets as IDL is serial and slow (interpreted).

I am embarking on a project to convert this prototype into an operational 
parallel HPC code. In the past I've used C++ for this kind of project and am 
comfortable using MPI. On the other hand, I've recently started using python 
and appreciate the flexibility and speed of development using python compared 
with C++. I have read that there is a trend to use python as the high level 
'glue' for these kind of large number crunching projects, so it would seem 
appropriate to go down that path. There are a number of C++ and FORTRAN(!) 
libraries I'd need to incorporate that handle things such as the processing of 
raw GPS data and computing ionospheric models, so I'd need to be able to make 
the appropriate interface for these into python.

If anyone uses python is this way, I'd appreciate any tips, hints, things to be 
careful about and in general any war stories you can relate that you wish you'd 
heard before making some mistake.

Here are the things I have investigated that it looks like I'd probably need to 
use:

* scipy/numpy/matplotlib
* Cython (or pyrex?) for speeding up any bottlenecks that occur in python code 
(as opposed to C++/FORTRAN libraries)
* MPI for Python (mpi4py). Does this play nice with Cython?
* Something to interface python with other language libraries. ctypes, swig, 
boost? Which would be best for this application?
* Profiling. profile/cprofile are straightforward to use, but how do they cope 
with a parallel (mpi4py) code?
* If a C++ library call has its own MPI calls, does that work smoothly with 
mpi4py operating in the python part of the code?

Sorry if some of this is a little basic, I'm trying to get up to speed on this 
a quick as I can.

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


KeyboardInterrupt in Subproccesses

2013-07-19 Thread Matthew Lefavor
All:

I am maintaining a program in Python 2 and need to send it a
KeyboardInterrupt to close it. Unfortunately, the program is used as a
subprocess in a wrapper subprocess, and so I cannot just directly press
CTL-C; I have to use a signal.

When I run the program "bare" (not in a subprocess), I have found that
raising a SIGINT (kill -2) raises a KeyboardInterrupt in the program. But
when I run the program in the wrapper process, kill -2 no longer has any
effect (whether it is raised by the wrapper or externally).

Why is that the case? Does Python's interpretation of SIGINT as a
KeyboardInterrupt only obtain when the program is run in a terminal? Is
there something else I'm missing?

Bonus points: Is this behavior the same for Python 3?

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


Messages to Python-List aren't posting

2013-07-19 Thread Matthew Lefavor
All:

I've been having trouble with all three Python mailing lists (python-list,
python-dev, python-ideas). This has been going on for about a month.
Whenever I post, it takes hours for my messages to be posted on the list. I
first thought it was an issue with my work email server, but moving to
gmail has had no effect.

Is there some obscure setting I am missing? Has anybody else had this
problem?

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


Re: Messages to Python-List aren't posting

2013-07-19 Thread Matthew Lefavor
(Sorry to reply-all, but in this case I think the direct messages will get
to people on this thread 12 hours earlier than the others.)

Well, I have had this problem with both my work email (matthew dot lefavor
_at_ nasa dpt gov) and my Gmail account. Since this doesn't happen to all
users (nor all Gmail users, for that matter), the only thing that's
constant here is that it's my name (well, and my password, but one would
hope that passwords aren't relevant here). Is it possible that the name
"Matthew Lefavor" has been added to a greylist or something?

One relevant piece of information: I have subscribed and unsubscribed to
different combinations of these lists several times over the past year, and
particularly over the past few months (as I was trying to manage the flow
of emails to my filter-less work inbox.) Could that sort of behavior
associate my name with malicious intent?

Matthew Lefavor


On Fri, Jul 19, 2013 at 9:29 AM, Skip Montanaro  wrote:

> > Date: Thu, 18 Jul 2013 16:30:51 -0400 - timestamp of your mail
> > Thu, 18 Jul 2013 13:30:51 -0700 (PDT) - you posted it via HTTP
> > Thu, 18 Jul 2013 22:31:39 +0200 (CEST) - it reached python.org
> > Fri, 19 Jul 2013 09:56:14 +0200 (CEST) - it began delivery to my address
> > Fri, 19 Jul 2013 00:56:15 -0700 (PDT) - it reached gmail and me.
> >
> > So the big delay is somewhere within python.org. Since this does NOT
> > happen to everyone, I would be looking for something like:
> > * Moderation requirements (does your message not comply with spec?)
> > * Spam greylisting
> > * Slow/glitchy DNS when attempting to verify your message
> >
> > Most likely I think is moderation, given the extreme length of time.
> > Something would have to be pretty glitchy to lag your mail out for
> > half a day in spam filtering. It's possible, I guess, though. I've no
> > idea what anti-spam features the list is using.
>
> Python.org runs Mailman 2.1.something.  In addition to all the various
> front-end filters running in the Postfix SMTP daemon (which trap most
> spam), SpamBayes is used as a last check to score messages intended
> for delivery to most mailing lists hosted on mail.python.org. It
> leaves two headers behind as proof of its existence.  Here are those
> headers for Matthew's message that started this thread:
>
> X-Spam-Status: OK 0.017
> X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:Python': 0.06;
> 'problem?': 0.07; 'subject:posting': 0.09; 'python': 0.11;
> 'posted': 0.15; 'effect.': 0.16; 'missing?': 0.16; 'python-dev,':
> 0.16; 'server,': 0.19; 'all:': 0.24; "i've": 0.25; 'message-
> id:@mail.gmail.com': 0.30; 'obscure': 0.31; 'lists': 0.32;
> 'trouble': 0.34; 'anybody': 0.35; 'but': 0.35;
> 'received:google.com': 0.35; 'there': 0.35; 'subject:List': 0.36;
> 'list.': 0.37; 'gmail': 0.38; 'to:addr:python-list': 0.38;
> 'issue': 0.38; 'moving': 0.39; 'to:addr:python.org': 0.39;
> 'mailing': 0.39; 'first': 0.61; 'hours': 0.66
>
> so SpamBayes thought it looked fine.  Had it been marked as "UNSURE"
> or "SPAM" in the status header, Mailman would have held it for
> moderator attention.  It is possible it was held by Mailman for some
> other reason.  Some obvious reasons: sender isn't subscribed, message
> had too many recipients, python-list@python.org was BCC'd (implicit
> destination). If you message was held by Mailman, moderator overload
> is the most likely cause of the delay.  It's also possible Mailman got
> overwhelmed, but I've seen no postmaster messages suggesting there
> were any systemic problems.
>
> BTW, although SpamBayes serves as a last resort for mail arriving via
> SMTP, it is the only line of defense for mail gatewayed from Usenet.
> I'm sure I can dredge up the code I wrote for that if anyone wants it
> for another application.
>
> Skip
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Messages to Python-List aren't posting

2013-07-19 Thread Matthew Lefavor
Well, it seems like the last message I posted did post in time. I'll keep
tabs on the issue and email the postmaster if it persists.

Matthew Lefavor


On Fri, Jul 19, 2013 at 11:00 AM, Chris Angelico  wrote:

> On Sat, Jul 20, 2013 at 12:57 AM, Skip Montanaro  wrote:
> >> One relevant piece of information: I have subscribed and unsubscribed to
> >> different combinations of these lists several times over the past year,
> and
> >> particularly over the past few months (as I was trying to manage the
> flow of
> >> emails to my filter-less work inbox.) Could that sort of behavior
> associate
> >> my name with malicious intent?
> >
> > I wouldn't think so.  More likely, you might have made a mistake in
> > your configuration parameters.  You might want to double-check your
> > python-l...@.python.org subscription information in Mailman to make
> > sure you haven't inadvertently done something.  If the problem
> > persists, send a note to postmas...@python.org describing the problem,
> > and include a message (with all its headers intact) that demonstrates
> > the problem, as Chris did with the timestamps.
>
> I agree with Skip. It's most likely to be a misconfiguration
> somewhere. The postmaster will either know immediately what's wrong,
> or be easily able to find out.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Script Hashplings

2013-07-25 Thread Matthew Lefavor
The answer is "probably not." If you just want to use the latest version of
Python 3 you have installed on your system, use: "#!/usr/bin/python3". When
you use the specific minor version numbers, they point to that specific
minor version.

Actually, the preferred shebang line is of the form: "#!/usr/bin/env
python3". This way the end users can override the interpreter with, say, a
virtualenv, rather than being stuck with the system default.


On Thu, Jul 25, 2013 at 9:54 AM, MRAB  wrote:

> On 25/07/2013 14:42, Devyn Collier Johnson wrote:
>
>> If I execute a Python3 script with this haspling (#!/usr/bin/python3.3)
>> and Python3.3 is not installed, but Python3.2 is installed, would the
>> script still work? Would it fall back to Python3.2?
>>
>>  Why don't you try it?
>
>
>  I hope Dihedral is listening. I would like to see another response from
>> HIM.
>>
>>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it that easy to install Python ?

2013-07-25 Thread Matthew Lefavor
On Thu, Jul 25, 2013 at 11:11 AM,  wrote:

> Hi there,
>
> I never write any Python program but as a system administrator, I'm often
> asked to install python on Debian servers.
>
> I just finished downloading, configuring, making and installing.
>
> The binary is now installed in :
> /usr/local/Python-2.7.5/bin/python2.7
> (the path is a deliberate administrator choice).
>
> Is that it?
>
> What else will my users need?
>
They may need permission to install third-party modules themselves. That
would probably require write permissions to /usr/local/Python-2.7.5/lib. If
you are unwilling to grant those permissions, you can suggest that they
learn how to work with "virtualenvs" (http://www.virtualenv.org/en/latest/),
which would allow them to install third-party modules locally.

And, of course, they need to be saavy enough to put the new python
installation on the PATH.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Script Hashplings

2013-07-26 Thread Matthew Lefavor
>
>
> Thanks Matthew Lefavor! But specifically, why use "#!/usr/bin/env python3"
> instead of "#!/usr/bin/python3"?
>

The "env" program looks up its argument in the current $PATH environment
variable, and then executes that. This means you aren't necessarily tied to
/usr/bin/python3. It makes things more portable.

For example, old Linux distributions don't have Python 3 installed with
them, and the user might not have permissions to install Python 3
system-wide. Instead they have it in some sort of ~/HOME/bin directory, and
then that is placed on the path by their .bashrc file. If your shebang line
was "#!/usr/bin/python3", the program wouldn't work without them changing
that line. If the shebang ling was "#!/usr/bin/env python3", it would find
the Python3 binary no problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [pyxl] xlrd 0.8.0 released!

2012-08-01 Thread Matthew Smith
Thank you guys so much! I am so excited to finally have xlsx so my users
don't have extra steps!

On Wed, Aug 1, 2012 at 11:01 AM, Chris Withers wrote:

> Hi All,
>
> I'm pleased to announce the release of xlrd 0.8.0:
>
> http://pypi.python.org/pypi/**xlrd/0.8.0<http://pypi.python.org/pypi/xlrd/0.8.0>
>
> This release finally lands the support for both .xls and .xlsx files.
> Many thanks to John Machin for all his work on making this happen.
> Opening of .xlsx files is seamless, just use xlrd as you did before and it
> all should "just work".
>
> xlrd 0.8.0 is also the first release that that targets Python 2.6 and 2.7,
> but no Python 3 just yet. Python 2.5 and below may work but are not
> supported. If you need to use Python 2.5 or earlier, please stick to xlrd
> 0.7.x.
>
> Speaking of xlrd 0.7.x, that's now in "requested maintenance only" mode
> ;-) That means, if possible, use 0.8.x. If you have a really good reason
> for sticking with 0.7.x, and you find a bug that you can't work around,
> then please make this clear on the python-ex...@googlegroups.com and
> we'll see what we can do.
>
> If you find any problems, please ask about them on the list, or submit an
> issue on GitHub:
>
> https://github.com/python-**excel/xlrd/issues<https://github.com/python-excel/xlrd/issues>
>
> Full details of all things Python and Excel related can be found here:
>
> http://www.python-excel.org/
>
> cheers,
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
> - http://www.simplistix.co.uk
>
> --
> You received this message because you are subscribed to the Google Groups
> "python-excel" group.
> To post to this group, send an email to python-ex...@googlegroups.com.
> To unsubscribe from this group, send email to python-excel+unsubscribe@**
> googlegroups.com .
> For more options, visit this group at http://groups.google.com/**
> group/python-excel?hl=en-GB<http://groups.google.com/group/python-excel?hl=en-GB>
> .
>
>


-- 
Matthew Smith

Software Engineer at G2, Inc
Follow us on Twitter @G2_inc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting started with IDLE and Python - no highlighting and no execution

2012-08-05 Thread Matthew Barnett

On 06/08/2012 01:58, MRAB wrote:

On 06/08/2012 01:09, Rotwang wrote:

On 06/08/2012 00:46, PeterSo wrote:

I am just starting to learn Python, and I like to use the editor
instead of the interactive shell. So I wrote the following little
program in IDLE

# calculating the mean

data1=[49, 66, 24, 98, 37, 64, 98, 27, 56, 93, 68, 78, 22, 25, 11]

def mean(data):
return sum(data)/len(data)

mean(data1)


There is no syntax highlighting and when I ran it F5, I got the
following in the shell window.


  >>>  RESTART







Any ideas?


I don't know what editor you're using or how it works, but I'm guessing
that pressing f5 runs what you've written as a script, right? In that
case the interpreter doesn't automatically print the result of
expressions in the same way that the interactive interpreter does; you
didn't tell it to print anything, so it didn't.


It looks like it's IDLE.


Actually, he does say that it's IDLE at the start.
[snip]

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


Legal: Introduction to Programming App

2012-08-19 Thread Matthew Zipf
Good evening,

I am considering developing an iOS application that would teach average
people how to program in Python. The app will be sold on the Apple app
store.

May I develop this app? To what extent do I need to receive permission from
the Python Software Foundation? To what extent do I need to recognize the
Python Software Foundation in my app?

Thank you,
Matthew Zipf
-- 
http://mail.python.org/mailman/listinfo/python-list


Error when deleting and reimporting subpackages

2011-08-22 Thread Matthew Brett
Hi,

I recently ran into this behavior:

>>> import sys
>>> import apkg.subpkg
>>> del sys.modules['apkg']
>>> import apkg.subpkg as subpkg
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'subpkg'

where 'apkg' and 'subpkg' comprise empty __init__.py files to simplify the 
example.

It appears then, that importing a subpackage, then deleting the containing 
package from sys.modules, orphans the subpackage in an unfixable state. 

I ran into this because the nose testing framework does exactly this kind of 
thing when loading test modules, causing some very confusing errors and 
failures.

Is this behavior expected?

Best,

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


Re: Error when deleting and reimporting subpackages

2011-08-22 Thread Matthew Brett
On Monday, August 22, 2011 12:06:44 PM UTC-7, Stephen Hansen wrote:
> On 8/22/11 11:51 AM, Matthew Brett wrote:
> > Hi,
> > 
> > I recently ran into this behavior:
> > 
> >>>> import sys
> >>>> import apkg.subpkg
> >>>> del sys.modules['apkg']
> >>>> import apkg.subpkg as subpkg
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > AttributeError: 'module' object has no attribute 'subpkg'
> > 
> > where 'apkg' and 'subpkg' comprise empty __init__.py files to simplify the 
> > example.
> > 
> > It appears then, that importing a subpackage, then deleting the containing 
> > package from sys.modules, orphans the subpackage in an unfixable state. 
> > 
> > I ran into this because the nose testing framework does exactly this kind 
> > of thing when loading test modules, causing some very confusing errors and 
> > failures.
> > 
> > Is this behavior expected?
> 
> Yes. Doing an import of "apkg.subpkg" results in more then just "test1"
> being cached in sys.modules, and you're removing half of that so leaving
> Python in a weird state.
> 
> You also want to del sys.modules["apkg.subpkg"], then you'll be able to
> re-import apkg.subpkg. I.e:
> 
> Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
> [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> import test1.test2
> >>> del sys.modules['test1']
> >>> del sys.modules['test1.test2']
> >>> import test1.test2 as test2
> >>>

Yes, sorry, I should have mentioned that I explored these kind of variations.

I think I see that there isn't an obvious way for del sys.modules['apkg'] to 
know to delete or modify 'apkg.subpkg', because sys.modules is just a dict.

However, I could imagine the import machinery being able to recognize that 
'apkg.subpkg' is broken, and re-import it without error.

Is that reasonable?

Best,

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


Re: Tkinter not working

2022-08-01 Thread Matthew Barnett

On 01/08/2022 13:17, Daniel Lee wrote:

Hello, I my code with tkinter was working before, and now, it has many errors 
in it. I’m not sure what has happened. The results after running are below:

"D:\Python Projects\tes\venv\Scripts\python.exe" "D:/Python 
Projects/tes/main.py"
Traceback (most recent call last):
   File "D:\Python Projects\tes\main.py", line 1, in 
 import tkinter as tk
   File 
"C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py",
 line 3, in 
 import tkinter.messagebox
   File 
"C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\messagebox.py",
 line 25, in 
 from tkinter.commondialog import Dialog
   File 
"C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\commondialog.py",
 line 13, in 
 from tkinter import Frame, _get_temp_root, _destroy_temp_root
ImportError: cannot import name 'Frame' from partially initialized module 
'tkinter' (most likely due to a circular import) 
(C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py)

Process finished with exit code 1


The entry:

  File 
"C:\Users\Daniel.LAPTOP-6U1MQ9CR\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", 
line 3, in 

import tkinter.messagebox

in the traceback does not look right to me. On my PC that file starts 
with a long docstring.

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


Repair Install of 64 bit python

2021-04-15 Thread Dodson, Matthew
Hi,

Having an issue after installing 64 bit python. Can't pip install any packages. 
Get the error "No module named pip".

Thanks,
Matt


Matthew Dodson 
2020 Data Analytics/Video Intern
New York Football Giants
Quest Diagnostics Training Center
1925 Giants Drive
East Rutherford, NJ 07073
o: 201-806-1749
matthew.dod...@giants.nfl.net

This e-mail is intended only for the use of the individual or entity named 
above and contains confidential and privileged information.  If the reader of 
this message is not the intended recipient, or the employee or agent 
responsible to deliver it to the intended recipient, you are hereby notified 
that any dissemination, distribution or copying of this communication is 
strictly prohibited. If you have received this communication in error, please 
immediately notify the sender of the e-mail and destroy the original message.  
UNLESS CLEARLY STATED,  NOTHING IN THIS E-MAIL, IN ANY E-MAIL THREAD OF WHICH 
IT MAY BE A PART, OR IN ANY ATTACHMENTS THERETO, SHALL CONSTITUTE A BINDING 
CONTRACT, OR ANY CONTRACTUAL OBLIGATION, OR ANY INTENT TO ENTER INTO ANY 
BINDING OBLIGATIONS.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python path and append

2016-04-19 Thread Matthew Barnett

On 2016-04-19 23:38, Chris Angelico wrote:

On Wed, Apr 20, 2016 at 8:29 AM, Seymore4Head
 wrote:


handle = open("\\Winmx\New$\q.txt")
for line in handle:
line=line.strip()
print line


Traceback (most recent call last):
  File "\\Winmx\New$\add viewed.py", line 2, in 
handle = open("\\Winmx\New$\q.txt")
IOError: [Errno 2] No such file or directory: '\\Winmx\\New$\\q.txt'

What I would like to do is read a plain text file from a hidden
network drive and append a space and the * character to the end of
each line.


Start with this:

print("\\Winmx\New$\q.txt")

If that doesn't do what you expect, it's possibly because you want to
use a raw string literal to prevent the backslashes from being parsed.

handle = open(r"\\Winmx\New$\q.txt")

That might help you.

(One clue that this is happening is that some of your backslashes got
doubled in the error message.)


when printed out:

>>> print "\\Winmx\New$\q.txt"
\Winmx\New$\q.txt

That's a file 2 directories down on the current drive.

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


Re: The following modules appear to be missing ['_sysconfigdata']

2019-01-08 Thread Matthew Lemon
Hi,

I would start from scratch with this.

1. You have latest Python 2 version.
2. Use virtualenv to create and activate a new virtual environment.
3. pip install wxPython and other dependencies.
4. Get your application running from the command line first and follow up any 
DLL exceptions.
5. Use pyinstaller (https://pyinstaller.readthedocs.io/en/stable/) to create a 
new exe file once you know everything works. 

Matt

On 8 January 2019 10:38:16 GMT, dude.ji...@gmail.com wrote:
>Hello,
>
>first time using python groups (and long time since using any other
>group for that matter). This might not be the right group due to wx
>being involved.
>
>Long story as short as possible. I have an old python script that I did
>~10 years ago, and have forgotten basically everything about Python
>since then (sorry!). Converted it to EXE way back when, and have kept
>using the EXE ever since then (on XP, and on Win7).
>
>I've just gotten a new PC, Windows10 (!), needed to update the script,
>so needed to recompile the EXE, and now it no longer compiles/works. It
>was fine on Windows7 (I'm not specifically stating that the problem is
>with windows10 - I simply don't remember all the hoops I jumped through
>on my previous machine).
>
>Here is the compile error:
>The following modules appear to be missing
>['_sysconfigdata']
>
>And here is the runtime error:
>Traceback (most recent call last):
>  File "AutoArchive.py", line 3, in 
>  File "wx\__init__.pyc", line 45, in 
>  File "wx\_core.pyc", line 4, in 
>  File "wx\_core_.pyc", line 12, in 
>  File "wx\_core_.pyc", line 10, in __load
>ImportError: DLL load failed: %1 is not a valid Win32 application.
>
>Here is how I compile: python setup.py py2exe
>
>And this is my setup.py:
>from distutils.core import setup
>import py2exe
>
>setup(console=['AutoArchive.py'])
>
>Yes, that says 'console' for a Windows EXE, but swapping it to 'window'
>didn't help. Besides which, the setup.py file is straight from the old
>computer, so 'it used to work'.
>
>What I have installed:
>. python 2.7.13 (this in itself may be a problem - I don't even
>remember which version I was using earlier, other than knowing for sure
>it wasn't a 3.x release)
>. wxpython  (since I also got an error that wx module was missing -
>that rang some bells, since the script uses a Windows pop-up window to
>inform me about its progress, and I remember using wx for that, so I
>installed it, but don't know how to get its' version. using "python -c
>"import wx;print wx.__version__" just gives runtime errors).
>
>Any help would be appreciated.
>
>And yes, I googled, but can't really find anything (well, d'Uh!, I
>wouldn’t be here then would I?) - there's ~some~ references, but
>usually it's about similar issues on Linux, with solutions that are not
>applicable at all. Except one I thought: I found some link stating that
>_sysconfigdata is part of sysconfig, and that this needed to be
>installed with pip, so I used the pip that came with my python distro
>and did 'pip install [[_]sys]config[data]' (all possible combo's) but
>pip claims it can't find said module, and TBH, I can't seem to find
>anything myself about said module.
>
>Thx,
>Jimbo
>-- 
>https://mail.python.org/mailman/listinfo/python-list

-- 
Sent from my Android device
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The following modules appear to be missing ['_sysconfigdata']

2019-01-09 Thread Matthew Lemon
If the OP was able to take the time to familiarise himself with the 
technologies, rather than bemoan the difficulty of deploying a ten year old 
code-base without mininal effort, he might have some success. Code rot is an 
issue after weeks sometimes, never mind ten years, and Python deployment is a 
weakness. However the tools do exist if you are prepared to give it a go. I've 
had most successful with pyinstaller, which is why I linked to it. Good luck!

On 9 January 2019 17:57:47 GMT, Chris Angelico  wrote:
>On Thu, Jan 10, 2019 at 2:37 AM Grant Edwards
> wrote:
>>
>> On 2019-01-09, MRAB  wrote:
>>
>> > .py files work on any platform that supports Python: Windows,
>Linux,
>> > MacOs, ...
>>
>> Only after python has been installed along with any other required
>> libraries.
>>
>> > How many platforms support .exe files that were compiled for
>Windows?
>>
>> None.
>>
>> But when your requirement is to support Windows users who are not
>> capable of installing Python, WxWindows, and a half-dozen other
>> libraries, you can't simply hand out .py files, push your fingers
>into
>> your ears, close your eyes, and start yelling "your problem now, not
>> mine, na, na, na, na, na, "
>
>This is true - but on the flip side, it's a bit unfair to say "blah
>blah Python sucks because py2exe is hard". That's not Python's fault.
>You have an additional requirement ("support people who can't install
>Python"), and that's going to have extra hassles. LOTS of them, in
>this case.
>
>ChrisA
>-- 
>https://mail.python.org/mailman/listinfo/python-list

-- 
Sent from my Android device
-- 
https://mail.python.org/mailman/listinfo/python-list


Sub-Org Applications for GSoC 2019 due soon!

2019-01-27 Thread Matthew Lagoe
Hey all, it's Google Summer of Code time again! PSF Sub-org
applications are due
Febuary 4th so if your project could use some student code this year get moving!

If your project is involved with python and you would be willing to
mentor students
this summer email gsoc-admins at python.org
<https://mail.python.org/mailman/listinfo/python-dev> so we can make
sure to get your
project on the list. All projects using python are welcome!

We need a good set of sub-organisations and ideas by Feb 4 for our application,
and then if we're accepted by Google, we can add a few other ideas up until
Feb 28 or so.

For those not familiar with it, Google Summer of Code is a mentoring
program where Google provides stipends to pay students to work with
selected open source organizations. PSF has participated for many
years now, and we're hoping to be selected to participate again.

The PSF GSoC page is http://python-gsoc.org/

That has contact information and answers to questions like "what does it
take to be a mentor?" and please feel free to ask if there's more you
want to know!

Thank you!

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


Complaints on installing packages

2020-01-13 Thread ofomi matthew
Good day Python Team,
I'm Matt by name and I have been having difficulties in installing packages
on my pycharm.it keeps telling me "error installing package". Please how do
I rectify this issues step by step.
Looking forward to get a response as soon as possible. Thank you
-- 
https://mail.python.org/mailman/listinfo/python-list


Errno 8 Exec format error

2005-10-25 Thread Matthew Fowler
Hi there

I have been porting python to the Etrax platform and everything seems to
be working.

However when I run the program in question I get:

"problem with execution of xhpl  on  AxisProduct:  [Errno 8] Exec format
error"

Im looking for any help to try and solve this issue. Am I correct in
thinking that Python is reporting this problem? If so has anybody
experienced this problem before?

Thank You

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


Setup/install issues including site.py

2005-10-25 Thread Matthew Fowler
Hello there

Im trying to port Python 2.4.3 to the Etrax platform, things have been
working but im having some issues I dont get.

if I issue #python I get ...

Could not find platform dependent libraries 
Consider setting $PYTHONHOME to [:]

(i do get the interpreter prompt)

so I set PYTHONHOME
#export PYTHONHOME=/mnt/flash/Python/lib:/mnt/flash/Python/bin

run python again

#python

and then get this error message:

'import site' failed; use -v for traceback

So you see I get one error or the other.

btw site.py is located in the PATH of the above export and also in the
location of the python binary.

Any suggestions greatly appreciated

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


Re: ANN: Circe 0.0.3b1 released

2005-11-07 Thread Matthew Nuzum
Hello,

I'm curious, does Circe use threading? I have been frustrated that
some of the things I've wanted to do required threading, but haven't
found much documentation or good examples of using threading with
wxPy.

I'm eager to disect the source of something that successfully combines the two.

On 6 Nov 2005 20:41:10 -0800, Nick <[EMAIL PROTECTED]> wrote:
> === Whats Circe? ===
> Circe is a multiplatform IRC client written in the Python language that
> utilizes the wxpython library for the graphical interface. Circe
> features Unicode, Scripting, and many other features.

--
Matthew Nuzum
www.bearfruit.org
-- 
http://mail.python.org/mailman/listinfo/python-list


python win32 and COM? for internet monitoring

2005-11-22 Thread Matthew Thorley
Greetings, I have a question I hope some one with more back ground can 
give me a little help with.

I want to write a simple internet monitoring script for windows that 
watches out bound http traffic and keeps a list of all the site visited.

I am thinking that I might be able to use pywin32 and COM to listen on 
open ports (e.g. 80) and watch for http headers. But I'm not sure if 
there is a better way to do it.

I would eventualy like to port this to Mac and Linux, but I figured that 
networking was at a low enough that I would have to do it differently 
for each OS.

Could some one please tell me if I'm way off and point me in the right 
direction?

Thanks very much
--Matthew
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python win32 and COM? for internet monitoring

2005-11-22 Thread Matthew Thorley
Graham Fawcett wrote:
> You might want to look into PCAP (a.k.a libpcap), which is the
> network-sniffing libary used by Ethereal, among other programs.  Much
> more portable than COM, and there are Python wrappers for it, IIRC.
> 
> You could also implement an HTTP proxy server in Python; Google should
> turn up numerous existing implementations.
> 
> Graham
> 

Thanks for the tip, I'll check that out.

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


Re: ANN: Louie-1.0b2 - Signal dispatching mechanism

2005-12-06 Thread Matthew Scott

Thomas Heller wrote:
> "Pat" <[EMAIL PROTECTED]> writes:
> > Thomas Heller wrote:
> >> What is the difference between PyDispatcher and Louie?
> >
> > Not too much at this point, but the general differences are listed on
> > this page:
> >
> > http://louie.berlios.de/changes.html
>
> Ok, so I won't need it (yet).

One difference that may help you out if you ever experiment with PJE's
RuleDispatch, is that the 'louie' top-level package name does not
conflict with RuleDispatch's 'dispatch' top-level package name.

- Matthew

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


formatted xml output from ElementTree inconsistency

2005-06-23 Thread Matthew Thorley
Greetings, perhaps someone can explain this. I get to different styles 
of formatting for xmla and xmlb when I do the following:

from elementtree import ElementTree as et

xmla = et.ElementTree('some_file.xml')
xmlb = et.Element('parent')
et.SubElement(xmlb, 'child1')
et.SubElement(xmlb, 'child2')

root = et.Element('root')
root.append(xmla.getroot())
root.append(xmlb)

print et.tostring(root)

The output I get shows xmla as nicely formatted text, with elements on 
different lines and everything all tabbed and pretty. Inverly, xmlb is 
one long string on one line.

Is that because the some_file.xml is already nicely formatted? I thought 
that the formatting was ignored when creating new elements.

Is their a function to 'pretty print' an element? I looked in api ref 
and didn't see anything that would do it. It would be nice if their was 
a way to create 'standard' formatted output for all elements regardless 
of how they were created.

Comments and suggestions are greatly appreciated.

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


Re: formatted xml output from ElementTree inconsistency

2005-06-23 Thread Matthew Thorley
Jarek Zgoda wrote:
> Matthew Thorley napisał(a):
> 
>> The output I get shows xmla as nicely formatted text, with elements on 
>> different lines and everything all tabbed and pretty. Inverly, xmlb is 
>> one long string on one line.
>>
>> Is that because the some_file.xml is already nicely formatted? I 
>> thought that the formatting was ignored when creating new elements.
> 
> 
> Why want you to read an XML document "by hand"? It's a "machine related" 
> data chunk.
> 
> Document formatting should be done by means of CSS and/or XSL stylesheet.
> 
It is just data to the machine, but people may have to read and 
interpret this data. I don't think there is anything unsual about 
formatting xml with tabs. Most web pages  do that in their html/xhtml. 
Just imagine if you wanted to change a broken link on your web page, and 
the entire page was one long string. That may not matter to Dream 
Weaver, but it sure would be annoying if you were using vi :)

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


problem with tk and pass by refference (I think :)

2005-02-11 Thread Matthew Thorley
Greetings, Maybe someone out there can lend me an eye? I've been 
stumped, banging my head against the wall trying to figure out why my 
script doesn't work. I've tried every thing I could think of, even 
unecessarily complicated mumbo-jumbo. Let me show you a snippet and then 
I'll explain what's happening.

for verse in self.activeSong['verses']:
verseNum = self.activeSong['verses'].index(verse)
activeSong = self.activeSong.copy()
firstLine = split(verse, '\n')[0]
button = Button(self.songWin, text=verse, command=(lambda: 
self.showVerse(verseNum)) )
button.config(bg='grey')
button.pack(expand=YES, fill=BOTH, side=TOP)
self.verseButtons.append(button)

This is as simple app for displaying the words of a song with an 
overhead projector. When you click on a song the program reads it and 
creates a button for each verse. When you click the button it is 
supposed to display that verse. As you can see above I am trying to call 
the showVerse method and pass it the verseNum.

The problem I am having is that every button gets assigned the verseNum 
for the last verse that gets processed. That is to say, if a sone has 4 
verses every button shows verse for, a 6 verse song loads verse 6 for 
every button, etc. I think that the value is getting passed by 
reference, so it gets updated with every iteration. I have seriously 
tried every thing I can think of to fix this.

If any one has any thoughts I would really appreciate it.
Thanks very much!
-Matthew
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with tk and pass by refference (I think :)

2005-02-11 Thread Matthew Thorley
Diez B. Roggisch wrote:
Hi,
button = Button(self.songWin, text=verse, command=(lambda num=verseNum:
self.showVerse(num)) )
should do the trick. The reason is basically that your version kept a
reference to verseNum - and when executed, the value verseNum points to is
the lasts one stored.
Rebinding the argument to a parameter in the lambda will keep the right
value for each iteration.

I tried it but I got a syntax error. The interpreter didn't like the 
equals sign in the lambda. I am using python 2.3.4. Is there another way 
of writing that?

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


Re: problem with tk and pass by refference (I think :)

2005-02-11 Thread Matthew Thorley
Diez B. Roggisch wrote:
I tried it but I got a syntax error. The interpreter didn't like the
equals sign in the lambda. I am using python 2.3.4. Is there another way
of writing that?

Strange. This script works and shows the desired behaviour - python is also
2.3.4:
def foo(x):
print x
fs = [lambda: foo(i) for i in xrange(5)]
for f in fs:
f()
fs = [lambda x=i: foo(x) for i in xrange(5)]
for f in fs:
f()

You're right! It was my fault. I left the colon after the lambda by 
mistake. e.g. lambda: num=verseNum:...

Thanks very much for the help it works great now! I wish I would have 
asked somebody sooner :)
-Matthew
--
http://mail.python.org/mailman/listinfo/python-list


Re: do you master list comprehensions?

2004-12-15 Thread Matthew Moss
Diez B. Roggisch wrote:
> >>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
> >>> [e for l in data for e in l]
> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']


Okay, I tried this in an interactive Python session and it works as
stated. My question is, why?  How is the interpreter parsing that list
expression that it makes sense?

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


trouble building python2.4

2004-12-22 Thread Matthew Thorley
Greetings, I just downloaded the python2.4 source from python.org and 
built it the usual way, i.e. ./configure && make. What I don't 
understand is that the resulting binary, when run, prints this line 
Python 2.3.4 (#1, Nov 15 2004, 10:29:48) at the top of its banner. 
Further more, the poplib modules complains when I try to call the 
poplib.POP3_SSL class, saying that the module has no such class, though 
the online docs say it does.

I read the README and I didn't see anything about having to pass options 
to configure to get it to build version 2.4. I appears to me that the 
tarball I downloaded wasn't really python2.4, even though it was 
'official' and named Python-2.4.tgz.

Can anyone please tell me what might of happened, or if they have had a 
similar experience?

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


Re: trouble building python2.4

2004-12-22 Thread Matthew Thorley
Erik Max Francis wrote:
Matthew Thorley wrote:
Greetings, I just downloaded the python2.4 source from python.org and 
built it the usual way, i.e. ./configure && make. What I don't 
understand is that the resulting binary, when run, prints this line 
Python 2.3.4 (#1, Nov 15 2004, 10:29:48) at the top of its banner. 
Further more, the poplib modules complains when I try to call the 
poplib.POP3_SSL class, saying that the module has no such class, 
though the online docs say it does.

You've got a copy of Python 2.3.4 installed on your system which is in 
your PATH first.

I have got to be the stupidest person on the face of the planet. Thanks 
very much. I was in the Python-2.4 dir, but I didn't call python with 
./python. I can't believe I missed that.

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


Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
This may be a very rudimentary question, but here goes:

If I have a simple dictionary, where the value is a class or function,
is there an interface through which it can discover what its key is?
Similar to index() for list.

For a list, assuming I new what the parent list was I could do something
like this.

>>> class child:
... def get_parent_index(self, parent):
... return parent.index(self)
...
>>> a = child()
>>> l = [a]
>>> b = l[0]
>>> b.get_parent_index(a)
>>> b.get_parent_index(l)
0

Is there a way to do something like that with dicts?


On a similar note, if one object is part of another, is there a way for
the 'child' obj to discover what/who the 'parent' object is? That way
parent does not have to be explicityly passed to get_parent_index?

The idea is like this:

>>> class child:
... def get_parent_index(self):
parent = self.magic_parent_discovery()
... return parent.index(self)
...



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


Re: Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
Can you please elaborate on this?
-Matthew

Diez B. Roggisch wrote:
>>keeping a additional mapping between values and keys.
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
Axel Straschil wrote:
> 
> For unique values, I did something like that couple of weeks ago, the
> thing you would need is the getKey thing, it's fast, but needs much
> memory for big structures becouse I use two dicts.

Thanks for the tip, I may give that a try. I'll be interested to see
what kind of speed penalty I pay. The data I am using has multiples
dictionaries with maybe a thousand entries each. But each entry is an
object containing a history of data with perhaps hundreds or even
thousands more entries. So we're talking about maybe a million+ total
nested key:values. I don't know if that counts as large or not. I can't
even guess how much k memory that is.

I must say I am *very* suprised that python does not have a way to look
up what key is pointing to a given object--without scanning the whole
list that is. Is that what list.index() does under-the-hood? I mean is
list.index(y) just the same as

itemnum = 0
for item in list:
if y == item:
return itemnum
else:
itemnum = itemnum+1


I think I am going to have to reevaluate my system design... grrr.

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


Re: Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
Steve Holden wrote:
while not impossible (using Python's excellent
> introspection facilities) is way beyond what most people would consider
> practical. Obviously the garbage collector has to solve this problem,
> but you *really* don't want to be doing this stuff in Python unless you
> absolutely *must*.
> 
> regards
>  Steve


Thanks very much for the detailed reply. Let me explain in a little more
detail what I am doing, and perhaps you could let me know if I am going
about it the wrong way.


I am creating an object database to store information about network
devices, e.g. switches and routers.

At the top level there are device objects, every device object contains
a mib object--and other various properties--where the actual data is
stored. The mib object is a dicitionary--or at least subclasses
dictionary--where the key is the oid (a unique string) and the value is
a special object called Datapoint. Datapoints come in two types Static
and Dynamic, and every Datapoint is unique.

So a simple structure looks something like this:

Device1
  Mib1
 {'1.3.6.1':Datapoint-x1,
  '1.3.6.2':Datapoint-y1,
  '1.1.5.4':Datapoint-z1,
  '1.2.7.3':Datapoint-q1}

Device2
  Mib2
 {'1.3.6.1':Datapoint-x2,
  '1.3.6.2':Datapoint-y2,
  '1.1.5.4':Datapoint-z2,
  '1.2.7.3':Datapoint-q2}

In the example Datapoint-xx should be read as, some unique data point in
memory.

The key in the Mib object is important because it is used by the
Datapoint is points to to look up its current value on the given device,
which is why I asked the two questions.

In my program I use snmp to look up values on network devices. i.e. 'Go
find the value of 1.3.6.1 from device1.' I want to be able to say
something like Device1.Mib['1.3.6.1].update(), and the correct Datapoint
discovers its key (oid) and parent device, then executes an snmp query
that looks something like this snmpget(keyThatPointsToSelf,
parentObj.ipAddress, parentObj.paramx)

I know of course I could keep all this in a relational database and do a
bunch of queries, but that would really suck! and I would have to track
all the Devices and oids my self. What I really want is smart objects
that think for me--thats what computers are for, right? ;)

I thought my design was a wonderfuly beautiful use of python an oop, but
perhaps I am mistaken and there is a much more pragmatic way to get the
job done. In the end preformance doesn't matter a lot. The front end
will be web based, so if the db can process faster than http/javascript
and user Bob who has to mouse, then everything will be fine.

Let me know what you think
Thanks much
--Matthew
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
Scott David Daniels wrote:
> Matthew Thorley wrote:
> 
>> This may be a very rudimentary question, but here goes:
> 
> From your questions, I believe you are not thinking of values as
> being distinct from the names and data structures that refer to them.
> 
> What is the parent of 23 in the following expression?
> 
> 1 + 11 * 2
> 
> If you know that, what is the parent of 293 in the same expression?
> 
>> If I have a simple dictionary, where the value is a class or function,
>> is there an interface through which it can discover what its key is?
>> Similar to index() for list.
> 
> 
> def keyfor(dictionary, element):
> for key, value in dictionary.iteritems():
> if value == element:  # value is element if identity quest
> return key
> raise ValueError, element
> 
>> On a similar note, if one object is part of another, 
> 
> This is the idea you have wrong.  In C, C++, Java, and Fortran you
> might have objects part of other objects, but in Python objects refer
> to each other.
> 
> How about this:
> 
> class Holder(object): pass
> 
> v = [1 + 11 * 2]
> w = [1, v, 3]
> d = {1: v}
> o = Holder()
> o.x = v
> 
> What is the parent of v?
> 
> Or even worse:
> 
> v = [1]
> v[0] = v
> 
> What is the parent of v now?
> 
> --Scott David Daniels
> [EMAIL PROTECTED]

I see what your saying, but I my situation the values of the dictionary
are unique objects created by a parent object. Sorry :(, I didn't
explain that very well in my first post.

When the 'root object' is 'global' I see what your saying, but when

class A:
  def __init__(self, container):
self.container=container

class B(dict):
  def magice_get_parent(self):
 ...

class special_value():
  def __init__(self, val):
 self.val = val

  def magic_get_key(self):
...

parent = A(B)
parent.container[x] = special_value(1)
parent.container[y] = special_value(2)
parent.container[z] = special_value(1)


In this example, in my mind, the following should happen, and in theory
using introspecition, actualy possible.

child = parent.container
D.magic_get_parent() #returns parent


value1 = parent.container[x]
value2 = parent.container[y]
value3 = parent.container[z]

value1.magic_get_key() #returns x
value2.magic_get_key() #returns y
value3.magic_get_key() #returns z

Let me know if I'm nutz!
--Matthew

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


Re: Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
Steve Holden wrote:

> I think that since each Datapoint appears to be unique, the simplest
> thing to do is to include a reference to the parent object as an
> attribute of the datapoint. Presumably when you create the Datapoint you
> already know which Device and Mib it's going to be stored in, so why
> make work for yourself when you can trade a little storage and make the
> whole thing easy?
>

I'll give that a shot and see how it goes. It makes sense, the parent
objects create the child objects, so they could very easily set the
apropriate parameter.

On the other hand, the introspecitve stuff is cool! When you say "make
more work for yourself" are you talking about 400 lines of code or 50.
Further, is there much processing required to do the magic? When python
do introspective magic, is it memory intensive? by that I mean does it
have to make copies of the objects to do the look-ups?

I don't mind copying the info like you suggest, but if the extra work
won't take more than a day or two, (or maybe even a week if its fun) I'd
like to do the introspection so that 1: I know how, 2: I can say that I
did ;)

Is there any kind of documentaion, (refference, article, blog-post,
tutorial), that explains introspection in useful detail, or at least
enough to get me started?

> You might also be interested to know that the Python Software Foundation
> funded the development of a Python package to support SNMP as a part of
> its first round of grant funding last year, so there's at least one
> other person working on this stuff!
> 
I did see that and I was quite pleased! :) I am currently using the
authors 'old' pysnmp which gets me by, but I am very excited to try out
the new version when it is ready.


Thanks again, for the reply and for the grant to pysnmp!

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


Re: Can dictionary values access their keys?

2005-04-08 Thread Matthew Thorley
Steve Holden wrote:

> Indeed, they will probably just need to pass "self" as an argument to
> the child object's creator (it will become an argument to the __init__()
> method). This will be pretty cheap, since the additional attribute will
> be bound to an already-existing value.
> 
>> On the other hand, the introspecitve stuff is cool! When you say "make
>> more work for yourself" are you talking about 400 lines of code or 50.
>> Further, is there much processing required to do the magic? When python
>> do introspective magic, is it memory intensive? by that I mean does it
>> have to make copies of the objects to do the look-ups?
>>
> Frankly I am not sure I see the advantage. You seem to be saying that
> rather than provide each child object with a reference to its parent you
> would rather provide it with a reference to the collection of parent
> objects and let it work out which one it wants. Where's the benefit?
All right then.

> Happy to help, but I can't take credit for the grant, since all I did as
> a PSF director was vote affirmatively on the recommendations of Martin
> von Lowis' extremely hard-working grants committee.

Thanks to the PSF for the grant!

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


Re: Utah Python Users Group

2005-04-13 Thread Matthew Thorley
lugal wrote:
> Is anyone aware if there's a Utah-based Python User Group? If not, does
> any else from Utah have any interest in forming a Utah-based Python
> User Group?
> 
I'm in Utah, I don't know of any groups but I might be interested.

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


I need help

2016-03-26 Thread matthew thiel
So I downloaded python and made an account but when I run it all it does is 
give me 3 options repair modify and uninstall I have clicked repair and modify 
and let them run but at the end it just exit out so how do I get it to work???


Sent from Mail for Windows 10

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


Re: (test) ? a:b

2014-10-22 Thread Matthew Ruffalo
On 10/22/2014 12:40 PM, Chris Angelico wrote:
> That's true when it's fundamentally arithmetic. But part of that
> readability difference is the redundancy in the second one. What if it
> weren't so redundant?
>
> 'Negative' if x < 0 else 'Low' if x < 10 else 'Mid' if x < 20 else 'High'
>
> You can't easily turn that into a dict lookup, nor indexing. It's
> either a chained if/elif tree or nested if/else expressions, which
> come to the same thing.
No, you can't turn that into a dict lookup, but this is one of the
canonical use cases for the bisect module:

>>> from bisect import bisect
>>> breakpoints = [0, 10, 20]
>>> labels = ['Negative', 'Low', 'Mid', 'High']
>>> values = [-5, 5, 15, 25]
>>> [labels[bisect(breakpoints, value)] for value in values]
['Negative', 'Low', 'Mid', 'High']

It's also worth noting that using bisect is O(log(n)) instead of O(n),
but if you're going to hit a point where the asymptotic behavior matters
I'm sure you will have long since abandoned a manually-written if/elif
chain.

MMR...

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


Re: generating unique variable name via loops

2014-11-04 Thread Matthew Ruffalo
Hi-

Questions like this appear so often in various places (mailing lists,
forums, sites like Stack Overflow) that I think a very blunt/candid
answer is appropriate. This is especially true since there's always
someone who responds to the question as-is with some monstrosity of
exec() and string formatting, instead of addressing the underlying issue
of using the right data structure for what you're trying to accomplish.

On 11/04/2014 06:29 AM, Fatih Güven wrote:
> I want to generate a unique variable name for list using python.
>
> list1=...
> list2=...
> .
> .
> .
> listx=... where x is a number.
*Incorrect.* You do not want to do this. You think you do, but that's
presumably because you aren't familiar with common data structures that
are available in Python. As Peter Otten said, this is *exactly* the
right situation to use a list.

You mentioned having structured and repetitive data, wanting to read a
.txt file and process each line, and wanting to access each employee's
data as appropriate. The general structure of this would be

>>> employees = []
>>> with open('employee_data.txt') as f:
... for line in f:
... # parse_line here isn't anything standard or built-in, it's
... # what you would write to transform a line of the data
... # file into whatever object you're interested in with
... # 'name', 'salary', etc. attributes
... employee = parse_line(line)
... employees.append(employee)
...
>>> employees[0].salary
15

Your line1 is now employees[0] and so on. Now, you can *easily* answer
questions like "what's the total salary of all employees?"

>>> total = 0
>>> for employee in employees:
... total += employee.salary
...
>>> total
7502000

(or 'sum(employee.salary for employee in employees)' of course.)

MMR...

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


Re: [Python-Dev] Python 2.x and 3.x use survey, 2014 edition

2014-12-12 Thread Matthew Ruffalo
On 12/11/2014 09:48 PM, Terry Reedy wrote:
> A possible reason: one is developing an app expected to be released
> fall 2015 after the 3.5 release and the app depends on something new
> in 3.5.  I must admit though that I cannot think of any such thing now
> for 3.5.  For 3.3 there was the new unicode, which was first committed
> about a year before release.  For 3.4, there was asyncio, but it was
> not committed until beta1, and was hardly usable then.
>
> So it was a defensible position.  Anyone who would check 3.5 could
> just as well check 3.4 and have most of the same impact on the summary.
The NumPy and SciPy developers who are implementing support for PEP 465
('@' as a dedicated matrix multiplication operator) will likely want to
test against a development version of 3.5 at some point. Using 3.5 isn't
strictly required, though, since supporting the @ operator is mostly a
matter of implementing the new __matmul__ methods and one can test those
by calling them directly instead of using the new operator.

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


Re: How to "wow" someone new to Python

2015-01-21 Thread Matthew Ruffalo
On 01/21/2015 02:06 PM, Chris Angelico wrote:
> On Thu, Jan 22, 2015 at 5:20 AM, Irmen de Jong  wrote:
>> On 21-1-2015 18:59, Steve Hayes wrote:
>>
>>> 3. When I started to look at it, I found that strings could be any length 
>>> and
>>> were not limited to swomething arbitrary, like 256 characters.
>> Even more fun is that Python's primitive integer type (longs for older 
>> Python versions)
>> has no arbitrary limitation either.
>>
>> That amazed me at the time I discovered python :)
> I hadn't worked with length-limited strings in basically forever
> (technically BASIC has a length limit, but I never ran into it; and I
> never did much with Pascal), but you're right, arbitrary-precision
> integers would have impressed me a lot more if I hadn't first known
> REXX. So... is there a way to show that off efficiently? Normally, any
> calculation that goes beyond 2**32 has already gone way beyond most
> humans' ability to hold the numbers in their heads.
>
> ChrisA
Yes, length-unlimited strings are *extremely* useful in some
applications. I remember bitterly cursing Java's string length limit of
2 ** 31 (maybe - 1) on multiple occasions. Python's strings seem to
behave like integers in that their size is limited only by available memory.

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


Re: How to "wow" someone new to Python

2015-01-21 Thread Matthew Ruffalo
On 01/21/2015 04:26 PM, Chris Angelico wrote:
> On Thu, Jan 22, 2015 at 8:20 AM, Matthew Ruffalo  wrote:
>> Yes, length-unlimited strings are *extremely* useful in some
>> applications. I remember bitterly cursing Java's string length limit of
>> 2 ** 31 (maybe - 1) on multiple occasions. Python's strings seem to
>> behave like integers in that their size is limited only by available memory.
> Hmm, I don't know that you'll get much beyond 2**31 characters (even
> all-ASCII characters in PEP 393) on a 32-bit Python, simply because
> "available memory" is capped at 2**32 bytes minus other stuff. You'd
> need a 64-bit Python to do that, and I would guess a 64-bit Java would
> also raise the limit.
>
> ChrisA
No, Java's String.length returns an int and Strings are limited to ~2 **
31 characters even in 64-bit Java.

I do seem to have encountered some strange behavior, though: creating
very large strings with str.__mul__ seems to enter an allocation loop in
Python 3.4. With a single-character string 's', I can create the
following new strings quickly:

s * 2 ** 33
s * 2 ** 34
s * 2 ** 35
s * 2 ** 36

but s * 2 ** 38 shows some odd memory usage. I'm watching the memory
usage of a Python process steadily increase to 256GB, drop to a few MB,
climb back to 256GB, drop to a few MB, and so on. It takes a half-dozen
cycles of allocation and deallocation before the interactive interpreter
gives me another prompt.

MMR...

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


Re: [OT] fortran lib which provide python like data type

2015-02-01 Thread Matthew Barnett

On 2015-02-02 02:04, Chris Angelico wrote:

On Mon, Feb 2, 2015 at 12:59 PM, Steven D'Aprano
 wrote:

And there are underspecified rules too. What is the plural of
octopus? No fair looking it up in the dictionary.


Standard and well-known piece of trivia, and there are several
options. "Octopodes" is one of the most rigorously formal, but
"octopuses" is perfectly acceptable. "Octopi" is technically
incorrect, as the -us ending does not derive from the Latin.


And the plural of "virus" is "viruses", not "viri" (that's the plural of
"vir") or "virii" (that would be the plural of "virius", if it existed).
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.x stuffing utf-8 into SQLite db

2015-02-09 Thread Matthew Ruffalo
On 02/09/2015 12:30 PM, Skip Montanaro wrote:
> Thanks, Chris. Are you telling me I should have defined the input file
> encoding for my CSV file as CP-1252, or that something got hosed on
> the export from XLSX to CSV? Or something else?
>
> Skip

Hi Skip-

I think it's most likely that the encoding issues happened in the export
from XLSX to CSV (unless the data is malformed in the original XLSX
file, of course). The file you're reading *is* valid UTF-8, and you're
reading it in text mode, so seeing '’' in certain lines implies that
the files might contain '’'.encode('utf-8') ==
b'\xc3\xa2\xe2\x82\xac\xe2\x84\xa2'. You could verify this with a hex
editor, or use something like the following:

"""
#!/usr/bin/env python3
from argparse import ArgumentParser

   

def
dump_lines_with_high_bytes(filename):   
   

with open(filename, 'rb') as
f:  
  

for line in
f:  
   

if any(byte >= 0x80 for byte in
line): 
   
print(line) 
   


if __name__ == '__main__':
p = ArgumentParser()
p.add_argument('filename')
args = p.parse_args()
dump_lines_with_high_bytes(args.filename)
"""

I'm a bit surprised that LibreOffice would mangle the encoding like this
-- the entire point of using a format like XLSX is to avoid encoding
issues. I just created a blank spreadsheet with Excel 2013 on Windows 7,
pasted a U+2019 RIGHT SINGLE QUOTATION MARK into the top-left cell,
saved it to a .xlsx file, and opened it with LibreOffice Calc 4.2.7.2 on
Linux. The quot character displayed correctly, and I was able to save it
to valid UTF-8 CSV that I could read with Python 3.4 without any mojibake.

I wonder whether the encoding problems happened with whatever data was
used to create your .xlsx file -- as Chris mentioned, this would occur
if UTF-8 bytes are incorrectly decoded as Windows cp1252.

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


building c extensions with setuptools that are not tied to python installed on build machine

2015-02-11 Thread Matthew Taylor
Hello pythonistas,

This is my first message to this mailing list, so if my question is
off-topic, please direct me to the correct mailing list.

I manage the NuPIC [1] open source machine intelligence project, which
is a python project with C extensions. We have a build on Travis-CI
[2] that creates binary wheels on release tags (in git). I'm having a
problem with OS X binaries.

The Travis-CI build machine is OS X 10.9 and uses the default Python
2.7.6 that comes with OS X. When binaries are created on this machine,
they seem to be linked somehow to the default Python, because when a
user running a homebrew-installed python (or Anaconda) attempts to
install the binary, there's an awful problem [3], even when the Python
versions are exactly the same (2.7.6). It seems that some connection
is made at compile time when C extensions are built that link the
binary file to the system Python on the OS X machine building the
binary. This means any users working with the homebrew Python or
Anaconda will get nasty thread errors when the program attempts to run
against two Pythons.

Fatal Python error: PyThreadState_Get: no current thread

Does this make sense to anyone? I'm still a little new to Python in
general (especially binary packaging), and it seems like this would be
a common problem for any projects with C extensions that need broad
binary distribution. Does anyone have any suggestions? Please take a
look at our setup.py file [4] and tell me if we're doing something
egregiously wrong.

[1] https://github.com/numenta/nupic
[2] https://travis-ci.org/numenta/nupic
[3] https://github.com/numenta/nupic/issues/1813
[4] https://github.com/numenta/nupic/blob/master/setup.py

Thanks thanks in advance,
-
Matt Taylor
OS Community Flag-Bearer
Numenta
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: building c extensions with setuptools that are not tied to python installed on build machine

2015-02-12 Thread Matthew Taylor
Ned, thank you for your insight on this problem. I will take your
advice and do some more digging. You've been very helpful.

Regards,

-
Matt Taylor
OS Community Flag-Bearer
Numenta


On Wed, Feb 11, 2015 at 4:23 PM, Ned Deily  wrote:
> In article
> ,
>  Matthew Taylor  wrote:
>> Does this make sense to anyone? I'm still a little new to Python in
>> general (especially binary packaging), and it seems like this would be
>> a common problem for any projects with C extensions that need broad
>> binary distribution. Does anyone have any suggestions? Please take a
>> look at our setup.py file [4] and tell me if we're doing something
>> egregiously wrong.
>
> Just taking a quick look at your setup.py there shows a quite
> complicated build, including SWIG.  One suggestion: keep in mind that
> it's normal on OS X for the absolute path of shared libraries and
> frameworks to be embedded in the linked binaries, including the C (or
> C++) extension module bundles (.so files) built for Python packages.  If
> any of the .so files or any other binary artifacts (executables, shared
> libraries) created by your package are linked to the Python
> interpreter's shared library, that may be why you are getting a mixture
> of Python instances.  One way to check for this is to use:
>
> otool -L *.so *.dylib
>
> on all of the directories containing linked binaries in your project and
> check for paths like:
> /System/Library/Frameworks/Python.framework
>
> That would be a link to the Apple-supplied system Python.
>
> A link to /Library/Frameworks/Python.framework or some other path would
> be to a third-party Python like from python.org or Homebrew.
>
> Due to differences in how the various Pythons are built and depending on
> what the C or C++ code is doing, it may not be possible to have one
> binary wheel that works with different Python instances of the same
> version.  For many simple projects, it does work.
>
> You *could* also ask on the PythonMac SIG list.
>
> https://mail.python.org/mailman/listinfo/pythonmac-sig
>
> --
>  Ned Deily,
>  n...@acm.org
>
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-25 Thread Matthew Pounsett

I've run into a threading error in some code when I run it on MacOS that works 
flawlessly on a *BSD system running the same version of python.  I'm running 
the python 2.7.6 for MacOS distribution from python.org's downloads page.

I have tried to reproduce the error with a simple example, but so far haven't 
been able to find the element or my code that triggers the error.  I'm hoping 
someone can suggest some things to try and/or look at.  Googling for "pyton" 
and the error returns exactly two pages, neither of which are any help.

When I run it through the debugger, I'm getting the following from inside 
threading.start().  python fails to provide a stack trace when I step into 
_start_new_thread(), which is a pointer to thread.start_new_thread().  It looks 
like threading.__bootstrap_inner() may be throwing an exception which 
thread.start_new_thread() is unable to handle, and for some reason the stack is 
missing so I get no stack trace explaining the error.

It looks like thread.start_new_thread() is in the binary object, so I can't 
actually step into it and find where the error is occurring.

> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py(745)start()
-> _start_new_thread(self.__bootstrap, ())
(Pdb) s
> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py(750)start()
-> self.__started.wait()
(Pdb) Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from

My test code (which works) follows the exact same structure as the failing 
code, making the same calls to the threading module's objects' methods:


import threading

class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
print "MyThread runs and exits."

def main():
try:
t = MyThread()
t.start()
except Exception as e:
print "Failed with {!r}".format(e)

if __name__ == '__main__':
main()


The actual thread object that's failing looks like this:

class RTF2TXT(threading.Thread):
"""
Takes a directory path and a Queue as arguments.  The directory should be
a collection of RTF files, which will be read one-by-one, converted to
text, and each output line will be appended in order to the Queue.
"""
def __init__(self, path, queue):
threading.Thread.__init__(self)
self.path = path
self.queue = queue

def run(self):
logger = logging.getLogger('RTF2TXT')
if not os.path.isdir(self.path):
raise TypeError, "supplied path must be a directory"
for f in sorted(os.listdir(self.path)):
ff = os.path.join(self.path, f)
args = [ UNRTF_BIN, '-P', '.', '-t', 'unrtf.text',  ff ]
logger.debug("Processing file {} with args {!r}".format(f, args))
p1 = subprocess.Popen( args, stdout=subprocess.PIPE,
universal_newlines=True)
output = p1.communicate()[0]
try:
output = output.decode('utf-8', 'ignore')
except Exception as err:
logger.error("Failed to decode output: {}".format(err))
logger.error("Output was: {!r}".format(output))

for line in output.split("\n"):
line = line.strip()
self.queue.put(line)
self.queue.put("")

Note: I only run one instance of this thread.  The Queue object is used to pass 
work off to another thread for later processing.

If I insert that object into the test code and run it instead of MyThread(), I 
get the error.  I can't see anything in there that should cause problems for 
the threading module though... especially since this runs fine on another 
system with the same version of python.

Any thoughts on what's going on here?



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


Re: Proper deletion of selected items during map iteration in for loop

2014-04-25 Thread Matthew Barnett

On 2014-04-25 18:53, Charles Hixson wrote:

What is the proper way to delete selected items during iteration of a
map?  What I want to do is:

for (k, v) in m.items():
 if f(k):
#  do some processing of v and save result elsewhere
del m[k]

But this gives (as should be expected):
  RuntimeError: dictionary changed size during iteration
In the past I've accumulated the keys to be deleted in a separate list,
but this time there are likely to be a large number of them, so is there
some better way?


The other way is to build a new dictionary.

Actually, there's a third way. Iterate over a snapshot:

for (k, v) in list(m.items()):
if f(k):
#  do some processing of v and save result elsewhere
del m[k]

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


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-27 Thread Matthew Pounsett
On Friday, 25 April 2014 10:05:03 UTC-4, Chris Angelico  wrote:
> First culprit I'd look at is the mixing of subprocess and threading.
> It's entirely possible that something goes messy when you fork from a
> thread.

I liked the theory, but I've run some tests and can't reproduce the error that 
way.  I'm using all the elements in my test code that the real code runs, and I 
can't get the same error.  Even when I deliberately break things I'm getting a 
proper exception with stack trace.

class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
logger = logging.getLogger("thread")
p1 = subprocess.Popen( shlex.split( 'echo "MyThread calls echo."'),
stdout=subprocess.PIPE, universal_newlines=True)
logger.debug( p1.communicate()[0].decode('utf-8', 'ignore' ))
logger.debug( "MyThread runs and exits." )

def main():
console = logging.StreamHandler()
console.setFormatter(
logging.Formatter('%(asctime)s [%(name)-12s] %(message)s', '%T'))
logger = logging.getLogger()
logger.addHandler(console)
logger.setLevel(logging.NOTSET)

try:
t = MyThread()
#t = RTF2TXT("../data/SRD/rtf/", Queue.Queue())
t.start()
except Exception as e:
logger.error( "Failed with {!r}".format(e))

if __name__ == '__main__':
main()


> Separately: You're attempting a very messy charset decode there. You
> attempt to decode as UTF-8, errors ignored, and if that fails, you log
> an error... and continue on with the original bytes. You're risking
> shooting yourself in the foot there; I would recommend you have an
> explicit fall-back (maybe re-decode as Latin-1??), so the next code is
> guaranteed to be working with Unicode. Currently, it might get a
> unicode or a str.

Yeah, that was a logic error on my part that I hadn't got around to noticing, 
since I'd been concentrating on the stuff that was actively breaking.  That 
should have been in an else: block on the end of the try.

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


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-27 Thread Matthew Pounsett
On Friday, 25 April 2014 14:58:56 UTC-4, Ned Deily  wrote:
> FWIW, the Python 2 version of subprocess is known to be thread-unsafe.  
> There is a Py2 backport available on PyPI of the improved Python 3 
> subprocess module:

Since that't the only thread that calls anything in subprocess, and I'm only 
running one instance of the thread, I'm not too concerned about how threadsafe 
subprocess is.  In this case it shouldn't matter.  Thanks for the info though.. 
that might be handy at some future point.

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


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-28 Thread Matthew Pounsett
On Sunday, 27 April 2014 10:33:38 UTC-4, Chris Angelico  wrote:
> In most contexts, "thread unsafe" simply means that you can't use the 
> same facilities simultaneously from two threads (eg a lot of database
> connection libraries are thread unsafe with regard to a single
> connection, as they'll simply write to a pipe or socket and then read
> a response from it). But processes and threads are, on many systems,
> linked. Just the act of spinning off a new thread and then forking can
> potentially cause problems. Those are the exact sorts of issues that
> you'll see when you switch OSes, as it's the underlying thread/process
> model that's significant. (Particularly of note is that Windows is
> *very* different from Unix-based systems, in that subprocess
> management is not done by forking. But not applicable here.)
> 

Thanks, I'll keep all that in mind.  I have to wonder how much of a problem it 
is here though, since I was able to demonstrate a functioning fork inside a new 
thread further up in the discussion.

I have a new development that I find interesting, and I'm wondering if you 
still think it's the same problem.

I have taken that threading object and turned it into a normal function 
definition.  It's still forking the external tool, but it's doing so in the 
main thread, and it is finished execution before any other threads are created. 
  And I'm still getting the same error.

Turns out it's not coming from the threading module, but from the subprocess 
module instead.  Specifically, like 709 of 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py
which is this:

try:
self._execute_child(args, executable, preexec_fn, close_fds,
cwd, env, universal_newlines,
startupinfo, creationflags, shell, to_close,
p2cread, p2cwrite,
c2pread, c2pwrite,
errread, errwrite)
except Exception:

I get the "Warning: No stack to get attribute from" twice when that 
self._execute_child() call is made.  I've tried stepping into it to narrow it 
down further, but I'm getting weird behaviour from the debugger that I've never 
seen before once I do that.  It's making it hard to track down exactly where 
the error is occurring.

Interestingly, it's not actually raising an exception there.  The except block 
is not being run.
-- 
https://mail.python.org/mailman/listinfo/python-list


Is a Metaclass the appropriate way to solve this problem?

2013-08-07 Thread Matthew Lefavor
All:

Like most people, I find the whole metaclass topic pretty obscure, and I
have avoided trying to use one for a while. I am also aware of Tim Peter's
famous advice that if you have to ask whether you need a metaclass, then
you almost certainly don't. But in this case I know I am solving a problem
similar to problems that other high-profile Python modules (e.g., Django's
Model class) have solved using metaclasses.

My company is using a database whose interface is defined in a series of
JSON objects. I am writing some interface code to the database and I am
trying to hide the detail of the JSON implementation from the user.

I want the user to be able to define a class representing a database entry
for any arbitrary table, whose attributes represent database entities, like
fields or tags, with syntax something like the following:

class DataSet:
data_set_id = DatabaseKeyField(int)
description = DatabaseField(str)
creation_date = DatabaseField(datetime.date)
creation_timestamp = DatabaseField(datetime.datetime)

def __init__(self, ds_id, description, timestamp):
self.data_set_id = ds_id
self.description = description
self.creation_timestamp = timestamp
self.creation_date = timestamp.date

I know that to create the DatabaseField objects I should be using a
descriptor. But I also want the DataSet to automatically gain methods that
will convert it into the expected JSON syntax (e.g., a __specifier__ method
that will create a JSON object with only "key" fields, and an __object__
method that will create a JSON object with all the fields and other bells
and whistles.)

My ultimate question then: How do I go about adding those kinds of methods
(which iterate through all the attributes which are Database* descriptors)?
I know that I could eschew metaclasses altogether and use a common
super-class, but this doesn't feel like the right situation for inheritance
to me. Is a metaclass going to be the cleanest and easiest-to-understand
way to solve this problem?

Thank you, all!

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


Re: Lockfile hanling

2015-03-31 Thread Matthew Ruffalo
On 2015-03-31 10:50, Ervin Hegedüs wrote:
> there is an app, written in Python, which stores few bytes of
> datas in a single file. The application uses threads. Every
> thread can modify the file, but only one at a time. I'm using a
> lock file to prevent the multiple access.
>
> ...
>
> How can I prevent or avoid this issue? What's the correct way to
> handle the lockfile in Python?

Hi Ervin-

If only one instance of the app is running at a given time, and you only
need to ensure mutual exclusion between its threads, you should probably
not use a lock *file* for this. I would strongly recommend that you use
a threading.Lock as per
https://docs.python.org/2.5/lib/module-threading.html instead of a lock
file. This will also allow you to avoid a 0.2-second polling loop; a
call to threading.Lock.acquire() will block until it is released by
another thread.

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


Re: Great Math Mystery

2015-04-16 Thread Matthew Barnett
Do you mean Pythonesque or Pythonic?-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing list of dictionaries to CSV

2015-05-05 Thread Matthew Ruffalo
On 2015-05-05 14:25, Skip Montanaro wrote:
> More likely, viewing the CSV file in Excel, Gnumeric, or some other
> spreadsheet which interprets some inputs as dates and formats them
> according to its default rules. Skip 

This is depressingly common, and I've even received CSV and plain text
data files that had some gene names(!) coerced to dates via a trip
through Excel. SEPT9 was one such gene, I believe.

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


Re: No Error; No Output...Nothing

2014-10-21 Thread Matthew Ruffalo
On 10/21/2014 05:44 PM, ryguy7272 wrote:
> Hey everyone, I'm trying to run this code.
>
> ...
>
> I commented out the import pylab as pl because I couldn't get the 
> matplotlib.pylab import working.  So, anyway, I hit F5, and it seems to run, 
> but it doesn't really do anything.  Isn't this either supposed to be 
> downloading data from the web, or throwing an error so I can troubleshoot, 
> and try to figure out what's going on?  It's hard to troubleshoot, when you 
> don't get any error.  Does this work for others?
>
> Thanks.
No, it isn't supposed to be downloading data from the web. You have
defined a few functions but you're not actually calling any of them. The
script terminates successfully with no output, since there's nothing for
it to do after executing your imports and function definitions.

Additionally, your attempted use of 'urllib2' inside
getConstituentsOfAnIndexFromYahoo will fail. Your import of that module
is commented out, since (from your imports) you're presumably using
Python 3 and a module with that name no longer exists. Since you have
'from urllib.request import urlopen', you can use the 'urlopen' module
without any fully-qualified name. The relevant line in your
getConstituentsOfAnIndexFromYahoo function should be 'pageResults =
p.findall(urlopen(...'.

MMR...

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


cause __init__ to return a different class?

2011-09-14 Thread Matthew Pounsett
I'm wondering if there's a way in python to cause __init__ to return a class 
other than the one initially specified.  My use case is that I'd like to have a 
superclass that's capable of generating an instance of a random subclass.  

I've tried both returning the subclass (as I would when overloading an 
operator) but I get the complaint that __init__ wants to return None instead of 
a type.

The other thing I tried was overwriting 'self' while inside __init__ but that 
doesn't seem to work either.

class Parent(object):
def __init__(self, foo):
if foo == True:
self = Child(foo)

class Child(Parent):
def __init__(self, foo):
pass

Is there a way to do this?  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cause __init__ to return a different class?

2011-09-15 Thread Matthew Pounsett
On Sep 15, 1:35 am, Chris Rebert  wrote:
> Override __new__() instead:
> http://docs.python.org/reference/datamodel.html#object.__new__

Aha.. thanks!  The reference book I'm working from neglects to mention
__new__, so I'd assumed __init__ was the constructor.  It hadn't
occurred to me that python would separate the functions (I still don't
see exactly why it would be useful to do that, but perhaps insight
will come with time).

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


Re: cause __init__ to return a different class?

2011-09-15 Thread Matthew Pounsett
On Sep 15, 1:54 am, Ryan Kelly  wrote:
> The above will do exactly what you want, but it's generally bad style
> unless you have a very specific use-case.  Is there a particular reason
> you need to "magically" return a subclass, rather than making this
> explicit in the code?
>
> To be friendlier to others reading your code, I would consider using a
> classmethod to create an alternative constructor:

Yeah, I was considering doing this as well, particularly if I couldn't
have made the other work.   The reason I'm not too concerned about
anyone misinterpreting what's going on is that in this case the base
class is actually named for being a constructor, and any rare case
where I want a specific subclass the subclass will be created
directly.

Thanks very much for your help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cause __init__ to return a different class?

2011-09-18 Thread Matthew Pounsett
On Sep 15, 1:54 am, Ryan Kelly  wrote:
> To be friendlier to others reading your code, I would consider using a
> classmethod to create an alternative constructor:

I finally got back to looking at this today.  As it turns out, un-
overriding __new__ in the child class is more complicated than I first
expected, and isn't worth the extra effort.  So, I ended up using a
constructor class method as you suggested.

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


Re: Non-POSIX parity (mark/space) with Python-Serial on Linux.

2011-11-21 Thread Matthew Lenz
I should also note that I am aware of the following discussion on the newsgroup:

https://groups.google.com/d/msg/comp.lang.python/1HyCqPSOf50/eQINFrrFKwoJ

However, I believe this refers to implementing the solution for 8M1 and 8S1.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Non-POSIX parity (mark/space) with Python-Serial on Linux.

2011-11-21 Thread Matthew Lenz
Using 8N1 under minicom with this device resulted in garbled text when once 
connected.  Connection using 7M1 resulted in the correct text.  So there must 
be something else that needs to be done in my python program correct?

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


Re: Non-POSIX parity (mark/space) with Python-Serial on Linux.

2011-11-21 Thread Matthew Lenz
Ahh. Ok.  So how would I go about doing that with python?  I think in perl 
(sorry for the naughty word) I could use the tr// (translate) but is there a 
quick way to do so with python?  Is it going to be necessary to convert 
commands I SEND to the device or only convert what I receive?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Non-POSIX parity (mark/space) with Python-Serial on Linux.

2011-11-21 Thread Matthew Lenz
Thanks, this will be a great help.

Just wanted to confirm that you meant to use [ .. for x in ord_str] in the 
example conversion?  Got a TypeError using the received_str.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Non-POSIX parity (mark/space) with Python-Serial on Linux.

2011-11-21 Thread Matthew Lenz
Another thing I noticed is that the & and | appear to give the same result as 
adding or subtracting 128 from the ordinal value.  I'm assuming that isn't 
coincidence. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Two questions about logging

2012-01-11 Thread Matthew Pounsett
I'm trying to figure out a couple of things with the logging module,
and I'm hoping someone can provide some pointers.  I've read through
the module docs on python.org, the basic and advanced tutorials, and
the cookbook post, but a couple of things still elude me.

First, I'd like to be able to permit users to do more typical log
rotation, based on their OS's log rotation handler, rather than
rotating logs from inside an application.  This is usually handled by
signalling an application with a HUP, whereupon it closes and then re-
opens all of its logs, getting new file handles (and new inodes).  I
don't see anything in the Handler methods (or anywhere else) that
would let me tell a logger object to refresh the file handles on a log
file.  Is there some standard way to deal with this?

Second, I'm trying to get a handle on how libraries are meant to
integrate with the applications that use them.  The naming advice in
the advanced tutorial is to use __name__ to name loggers, and to allow
log messages to pass back up to the using application's logger for
processing, but these two pieces of advice seem contradictory.. since
log messages only pass back up to the root if the loggers are named
hierarchically.

> cat foo.py
#!/usr/bin/env python

import logging
import bar

logger = logging.getLogger(__name__)
fh = logging.FileHandler('foo.log')
ch = logging.StreamHandler()
logger.addHandler(fh)
logger.addHandler(ch)
logger.warning('from the first')
bar.funct()

> cat bar.py
#!/usr/bin/env python

import logging

def funct():
logger = logging.getLogger(__name__)
logger.warning('from the second')

> ./foo.py
from the first
No handlers could be found for logger "bar"

So, given this state of affairs, how is a library author to use
loggers, given that he or she can't know how authors who use the
library will name their logger objects?  In the above example, what
would the author of bar.py do to hook up bar's logger with foo's,
without knowing in advance what foo's logger will be named?


Thanks very much for any suggestions, or pointers to documentation
that I've missed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two questions about logging

2012-01-11 Thread Matthew Pounsett
On Jan 11, 9:34 pm, Roy Smith  wrote:
> What I would do is log to syslog (logging.handlers.SysLogHandler) and
> let syslog worry about rotating log files.  Why reinvent the wheel?

Syslog is fine for an application run by an administrator, but isn't
an option for a user.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two questions about logging

2012-01-14 Thread Matthew Pounsett
On Jan 12, 8:03 pm, K Richard Pixley  wrote:
> Here's the confusion.  Each log named __name__ is under the root logger.
>   If you want them all, then catch them all with the root logger.

Thanks!  I knew I was missing something obvious.  Between you and Jean-
Michael Pichavant I've figured out what I need to do here.


On Jan 11, 9:34 pm, Roy Smith  wrote:
> What I would do is log to syslog (logging.handlers.SysLogHandler) and
> let syslog worry about rotating log files.  Why reinvent the wheel?

I've also worked out what I need to reset file handles, although it
took a lot of reading in the various logging.Handler subclasses.  What
I needed isn't explicitly documented anywhere, but it turns out that
calling the close() method on a FileHandler instance does what I
need.  There's no method to re-open the handler, but the next call to
emit() will automatically re-open the file if it isn't already open.

The upshot is that this does the expected thing if you rename its log
file and then send the running script a HUP signal.

#!/usr/bin/env python

import logging
import signal
import time

logger = logging.getLogger()
lh = logging.FileHandler('./foo.log')
lh.setFormatter(logging.Formatter('%(asctime)s %(name)s: %(message)s',
'%T'))
logger.addHandler(lh)

def sighup(signum, frame):
lh.close()
logger.error("handled {0}: {1}".format(signum, frame))

def main():
signal.signal(signal.SIGHUP, sighup)
while 1:
time.sleep(1)
logger.error('a message')

if __name__ == '__main__':
main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Feature Request: `operator.not_in`

2013-04-19 Thread Matthew Gilson
I believe that I read somewhere that this is the place to start 
discussions on feature requests, etc.  Please let me know if this isn't 
the appropriate venue (and what the appropriate venue would be if you know).


This request has 2 related parts, but I think they can be considered 
seperately:


1) It seems to me that the operator module should have a `not_in` or 
`not_contains` function.  It seems asymmetric that there exists a 
`is_not` function which implements `x is not y` but there isn't a 
function to represent `x not in y`.


2) I suspect this one might be a little more controversial, but it seems 
to me that there should be a separate magic method bound to the `not in` 
operator.  Currently, when inspecting the bytecode, it appears to me 
that `not x in y` is translated to `x not in y` (this supports item 1 
slightly).  However, I don't believe this should be the case.  In 
python, `x < y` does not imply `not x >= y` because a custom object can 
do whatever it wants with `__ge__` and `__lt__` -- They don't have to 
fit the normal mathematical definitions.  I don't see any reason why 
containment should behave differently.  `x in y` shouldn't necessarily 
imply `not x not in y`.  I'm not sure if `object` could have a default 
`__not_contains__` method (or whatever name seems most appropriate) 
implemented equivalently to:


 def __not_contains__(self,other):
  return not self.__contains__(other)

If not, it could probably be provided by something like 
`functools.total_ordering`.  Anyway, it's food for thought and I'm 
interested to see if anyone else feels the same way that I do.


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


Re: Feature Request: `operator.not_in`

2013-04-19 Thread Matthew Gilson


On 4/19/13 2:27 PM, Terry Jan Reedy wrote:

On 4/19/2013 10:27 AM, Matthew Gilson wrote:
) It seems to me that the operator module should have a `not_in` or

`not_contains` function.  It seems asymmetric that there exists a
`is_not` function which implements `x is not y` but there isn't a
function to represent `x not in y`.


There is also no operator.in.


True.  I'm not arguing that there should be ...

There is operator.contains and operator.__contains__.


Thankfully :-)


There is no operator.not_contains because there is no __not_contains__ 
special method. (Your point two, which I disagree with.)


But there's also no special method `__is_not__`, but there's a 
corresponding `is_not` in the operator module so I don't really see that 
argument.  It's a matter of functionality that I'm thinking about in the 
first part.


 itertools.starmap(operator.not_in,x,y)

vs.

itertools.starmap(lambda a,b: a not in b,x,y)

Pretty much every other operator in python (that I can think of) has an 
analogous function in the operator module.



2) I suspect this one might be a little more controversial, but it seems
to me that there should be a separate magic method bound to the `not in`
operator.


The reference manual disagrees.
"The operator not in is defined to have the inverse true value of in."


I would still leave that as the default behavior.  It's by far the most 
useful and commonly expected.  And I suppose if you *can't* have default 
behavior like that because that is a special case in itself, then that 
makes this second part of the request dead in the water at the outset 
(and I can live with that explanation).



 Currently, when inspecting the bytecode, it appears to me
that `not x in y` is translated to `x not in y` (this supports item 1
slightly).  However, I don't believe this should be the case. In
python, `x < y` does not imply `not x >= y` because a custom object can
do whatever it wants with `__ge__` and `__lt__` -- They don't have to
fit the normal mathematical definitions.


The reason for this is that the rich comparisons do not have to return 
boolean values, and do not for numarray arrays which, I believe, 
implement the operators itemwise.


Yes, you're correct about numpy arrays behaving that way.  It can be 
very useful for indexing them.


It would also be fine for a special method `__not_contains__` to be 
expected to return a boolean value as well.  It could still be very 
useful.  Consider a finite square well from quantum mechanics.  I could 
define `in` for my particle in the square well to return `True` if there 
is a 70% chance that it is located in the well (It's a wave-function, so 
it doesn't have a well defined position -- the particle could be anyway, 
but 7 out of 10 measurements will tell me it's in the well).  It might 
be nice if I could define `not in` to be  `True` if there is only a 30% 
chance that it is in the well.  Of course, this leaves us with a 
no-man's land around the 50% mark.  Is it in the well or not?  There's 
no telling.  I'm sure you could argue that this sort of thing *could* be 
done with rich comparisons, but I would consider that a deflection from 
the point at hand.  It seems it should be up to the user to design the 
API most suited for their task.  Or what about a `Fraternity` class.  
Are the new pledges in the fraternity or not?  Maybe they should be 
considered neither in, nor out until pledge season is over.



> I don't see any reason why containment should behave differently.

'Design by analogy' is tricky because analogies often leave out 
important details. __contains__ *is* expected to return true/false.


" object.__contains__(self, item)
Called to implement membership test operators. Should return true 
if item is in self, false otherwise"


--
Terry Jan Reedy




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


Re: Pythonic way to count sequences

2013-04-25 Thread Matthew Gilson


A Counter is definitely the way to go about this.  Just as a little more 
information.  The below example can be simplified:


from collections import Counter
count = Counter(mylist)

With the other example, you could have achieved the same thing (and been 
backward compatible to python2.5) with


   from collections import defaultdict
   count = defaultdict(int)
   for k in mylist:
count[k] += 1



On 4/25/13 9:16 PM, Modulok wrote:

On 4/25/13, Denis McMahon  wrote:

On Wed, 24 Apr 2013 22:05:52 -0700, CM wrote:


I have to count the number of various two-digit sequences in a list such
as this:

mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)]  # (Here the (2,4) sequence
appears 2 times.)

and tally up the results, assigning each to a variable.

...

Consider using the ``collections`` module::


 from collections import Counter

 mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)]
 count = Counter()
 for k in mylist:
 count[k] += 1

 print(count)

 # Output looks like this:
 # Counter({(2, 4): 2, (4, 5): 1, (3, 4): 1, (2, 1): 1})


You then have access to methods to return the most common items, etc. See more
examples here:

http://docs.python.org/3.3/library/collections.html#collections.Counter


Good luck!
-Modulok-


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


chi-squared tests in python?

2006-01-17 Thread Matthew Vernon
Hi,

I'd like to be able to use the chi-squared test in my code. Currently,
I can output "look up [this value] in a chi-squared table with [x]
degrees of freedom", but that's obviously a little sub-optimal. I
notice that numarray has a chi_square function, but that just gives
you random numbers from a chi-squared distribution with a set number
of degrees of freedom - not really what I want.

Does there exist python code to do this? preferably something vaguely
standard? 

Thanks,

Matthew
ps: given the "batteries included" philosphy, there's a remarkable dearth 
of stats in python...
-- 
Matthew Vernon MA VetMB LGSM MRCVS
Farm Animal Epidemiology and Informatics Unit
Department of Veterinary Medicine, University of Cambridge
http://www.cus.cam.ac.uk/~mcv21/
-- 
http://mail.python.org/mailman/listinfo/python-list


Warning when new attributes are added to classes at run time

2006-07-19 Thread Matthew Wilson

I sometimes inadvertently create a new attribute on an object rather
update a value bound to an existing attribute.  For example:

In [5]: class some_class(object):
   ...:  def __init__(self, a=None):
   ...:  self.a = a
   ...:

In [6]: c = some_class(a=1)

In [7]: c.a
Out[7]: 1

In [8]: c.A = 2

I meant to update c.a but I created a new c.A.  I make this mistake
probably hourly.

I suspect adding attributes at run time can be a beautiful thing, but in
this particular instance, I'm only using this feature to hurt myself.

I wrote a simple class that will warn me when I make this mistake in the
future:

import warnings

class C(object):

warn_on_new_attributes = True

standard_attributes = []

def __setattr__(self, name, value):

if self.warn_on_new_attributes \
and name is not 'warn_on_new_attributes' \
and name not in self.standard_attributes:

warnings.warn("%s has no standard attribute %s."
  % (self.__class__.__name__, name))


self.__dict__[name] = value


class C1(C):

standard_attributes = ['a1', 'a2']


class C2(C):

warn_on_new_attributes = False

# Do some simple testing.
c11 = C1()
c11.a1 = 1
c11.a2 = 2
c11.a3 = 3
c11.a4 = 4

# Disable warnings for this instance.
c12 = C1()
c12.warn_on_new_attributes = False
c12.a1 = 1
c12.a2 = 2
c12.a3 = 3
c12.a4 = 4

c11.a5 = 5

# Use an object that has warnings disabled by default.
c2 = C2()
c2.a1 = 1
c2.a2 = 2
c2.a3 = 3
c2.a4 = 4

# enable warnings for this object.
c2.warn_on_new_attributes = True
c2.a1 = 1
c2.a5 = 5


All comments are welcome.  Is there a better way of implementing the
above class, OR, is this approach generally wrong-headed?  Am I the only
one that makes this mistake?

TIA

-- 
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Warning when new attributes are added to classes at run time

2006-07-20 Thread Matthew Wilson
On Thu 20 Jul 2006 04:32:28 AM EDT, Bruno Desthuilliers wrote:
>> self.__dict__[name] = value
> Make it:  
>   object.__setattr__(self, name, value)
>
> Your approach will lead to strange results if you mix it with properties
> or other descriptors...

Thanks!

>> class C1(C):
>> 
>> standard_attributes = ['a1', 'a2']
>
> DRY violation here. And a potential problem with inheritance (as always
> with class attributes).

Considering I had to look up what DRY meant before replying to this
message, I may be missing your point.  Is the repeat here that each
subclass has to define its own list of standard attributes?   Or, is it
that the standard_attributes list holds strings, but I could build that
list up by looking at my existing attributes?

If you're feeling charitable, can you explain what you mean a little
more?

TIA


-- 
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles
-- 
http://mail.python.org/mailman/listinfo/python-list


ftplib errors/exceptions

2006-07-30 Thread Matthew Little
I'm new to Python and I am writing a simple FTP client.  I am having trouble handling errors like connection refused, invalid username or password, and the like.  I can use a try exception block like thistry:  ftp=FTP('some_server')
  ftp.login()  # more linesexcept:  print "An error has occured.\n"This works fine to supress the errors, but I would like to be able to narrow the errors down so that I can print 'invalid username or password' or 'connection refused' rather than simply 'An error has occured'.  How would I go about narrowing down which errors occur? 
Thanks in advance
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: upgrading python...

2006-08-02 Thread Matthew Miller
On Wed, Aug 02, 2006 at 06:50:11AM -0700, bruce wrote:
> i'min a situation where i might need to upgrade python. i have the current
> version of python for FC3. i might need to have the version for FC4. i built
> the version that's on FC4 from the python source RPM.

Going from Python 2.3 as in FC3 to FC4's Python 2.4 isn't going to be
viable. Too different.

> however, when i simply try to install the resulting RPM, the app gives me
> dependency issues from apps that are dependent on the previous/current
> version of python.

Exactly.


> i'm trying to figure out if there's a 'best' way to proceed.

One option: build a new python RPM that installs Python2.4 into an
alternate path, or skip that complication and just build and install it
into /usr/local, leaving the system python intact.

But I think the *better* solution is to upgrade to Fedora Core 5 or Fedora
Core 6 Test 2. You want new software, go with a collection of new software
tested together.


> do i simply do the install, and force it to overwrite the current version of
> python?

Don't do this. It will break everything. Including yum, which is written in
python.


> is there a way to point 'yum' at my new python RPM, and let yum take care of
> dealing with any dependcy issues? and how would yum handle weird dependency
> issues with RPMs that don't exist.. does yum have the ability to actually
> build required apps from source?

Yum is pretty good, but it's not *that* magical.


-- 
Matthew Miller   [EMAIL PROTECTED]  <http://mattdm.org/>
Boston University Linux  -->  <http://linux.bu.edu/>
-- 
http://mail.python.org/mailman/listinfo/python-list


Need advice on how to improve this function

2006-08-20 Thread Matthew Wilson
I wrote a function that converts a tuple of tuples into html.  For
example:

In [9]: x
Out[9]:
('html',
 ('head', ('title', 'this is the title!')),
 ('body',
  ('h1', 'this is the header!'),
  ('p', 'paragraph one is boring.'),
  ('p',
   'but paragraph 2 ',
   ('a', {'href': 'http://example.com'}, 'has a link'),
   '!')))


In [10]: as_html(x, sys.stdout)




this is the title!





this is the header!

paragraph one is boring.

but paragraph 2 http://example.com";>has a link!






I'd like to know ways to make it better (more efficient, able to deal
with enormous-size arguments, etc).  How would I write this as a
generator?

Here's the definition for as_html:

def as_html(l, s):
"Convert a list or tuple into html and write it to stream s."
if isinstance(l, (tuple, list)):
tagname = l[0]
if isinstance(l[1], dict):
attributes = ' '.join(['%s="%s"' % (k, l[1][k]) for k in l[1]])
s.write('<%s %s>' % (tagname, attributes))
else:
s.write('<%s>' % tagname)
if tagname in ('html', 'head', 'body'):
s.write('\n\n')
for ll in l[1:]:
as_html(ll, s)
s.write('' % tagname)
if tagname not in ('a', 'b', 'ul'):
s.write('\n\n')
elif isinstance(l, str):
s.write(l)


All comments welcome. TIA

-- 
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles
-- 
http://mail.python.org/mailman/listinfo/python-list


Why do this?

2006-10-05 Thread Matthew Warren
Ok, not really python focused, but it feels like the people here could
explain it for me :)

Now, I started programming when I was 8 with BBC Basic.

I never took any formal classes however, and I have never become an
expert programmer. I'm an average/hobbyist programmer with quite a few
languages under my belt but I cant do any really fancy tricks with any
of them. (although Python might be nudging me into more advanced things,
now I'm starting to get what all the __method__ thingies and operators
are all about)

I learned over the years to do things like the following, and I like
doing it like this because of readability, something Python seems to
focus on :-

Print "There are "+number+" ways to skin a "+furryanimal

But nowadays, I see things like this all over the place;

print("There are %s ways to skin a %s" % (number, furryanimal))

Now I understand there can be additional formatting benefits when
dealing with numbers, decimal places etc.. But to me, for strings, the
second case is much harder to read than the first.

I hope I'm not being dense.

The result is that I have pathalogically avoided print "%s" % (thing)
because it seems to just over complicate things.


Ta, :)

Matt.





This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
> 
> | Now, I started programming when I was 8 with BBC Basic.
> 
> Hey, likewise! (Except I was 12 when it came out!)

I think it came out before I was 8, and I started out with print and
input. Not sure if that's 'real' programming - I don't think I graduated
to ifs and thens and gotos and gosubs for a while.. Uhm, Or was it 'def
proc' et al for BBC Basic? Brain holes leaking old stuff out :)

> | Print "There are "+number+" ways to skin a "+furryanimal
> 
> perfectly sound Python code, as far as it goes, altho' obviously
> "Print" is spelt "print" in Python, and if that number is in fact
> a number it'll need to be str ()-ed first.

Blame outlook and AutoCaps. If number were a number I would write

print "There are",number,"ways to skin a "+furryanimal

..something I like about python, 'cause having been pathalogically
avoiding %'s etc.. I have learned to hate going "string "+str(number)+"
string"

> 
> | But nowadays, I see things like this all over the place;
> | 
> | print("There are %s ways to skin a %s" % (number, furryanimal))
> 
> The outermost brackets are (at the moment) unnecessary in python,

Oops :)

> altho' print is slated for replacement by a function in Python 3.0
> at which point they'll be necessary.

? Why do that, point me at a PEP if required and I'll read it :)

> 
> number = 3
> animal = "cat"
> print "There are %d ways to skin a %s" % (number, animal)
> 
> | Now I understand there can be additional formatting benefits when
> | dealing with numbers, decimal places etc.. But to me, for 
> strings, the
> | second case is much harder to read than the first.
> 
> I think what it comes down to is just what's most readable in the
> situation in which you're using it. Imagine a rather longer
> string, and one where  and  are not short names,
> but calls to some function. Sometimes, since %s will call __str__
> on anything it's passed, it's a quick way to get a string 
> representation
> of a more complex object, which you'd otherwise have to str () at
> least.
>
> Also, for me, substitution often reads more naturally because you're 
> not breaking the string up with lots of " +  + "..." + 
> yyy + "...  
> stuff. Again, though, depends on what your code looks like, and how 
> readable you find it. Certainly, I wouldn't advocate *pathologically* 
> avoiding the "%s" % blah style, but it needn't always be the right
> thing.
>

I am a big fan of easily human readable meaningful names for things, to
the extent that I will quite readily use var or function names
ThatCanBeQuiteLongAndExplanatory() For me, it helps retain the
readbility say, when breaking up a string with lots of + xxx + "etc..
Etc.." + lalala

>It may be easier 
> to use the 
> dictionary form of the substitution then, eg:
> 
> print """
> Long string with %(valueA)s and %(valueB)s and %(valueC)s embedded in
> it at some distance from the end...
> ...
> """ % dict (valueA=1, valueB="blah", valueC=datetime.datetime.now ())
> 
> Obviously that dict would likely be defined elsewhere, or could even
> be locals (). Once you get to this stage, though, you might want to 
> start looking at templating toolkits like Cheetah, Genshi or any of
> the many others knocking around.
>

That's something I wasn't aware of, and I think I'll try if I find
myself going

"something"+dict['thingummy']+" red ones and "+dict['colour']+" ones"

The dict substitution does feel a bit easier to read compared to the
concatenation, because of the dict['']  noise.

Matt.




This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
 > Also, having a variable of type str called 'number' seems 
> perverse (and 
> probably error prone), so I suspect I might need something like:
>

And not something I would normally do, but for hastily written contrived
examples I might :)

 
>print "There are "+str(number)+" ways to skin a "+furryanimal
> 
> but the format string does the conversion for free.
> 
> The other main reason for preferring format strings is that 
> they make it 
> easier to refactor the code. If you ever want to move the 
> message away from 
> where the formatting is done then it's a lot easier to 
> extract a single 
> string than it is to clean up the concatenation.
> -- 


This is a benefit of pythons implementation of format strings I hadn't
considered, that __str__() is called to get the representation. And
something I like about python, althgouh at the moment the __xxx__
methods available and their use is something I'm just getting into


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
 > Duncan Booth wrote:
> 
> > print "There are"+number+"ways to skin a"+furryanimal
> > 
> > or at least something equivalent to it. If I try to make 
> the same mistake 
> > with a format string it jumps out to me as wrong:
> > 
> > "There are%sways to skin a%s" % (number, furryanimal)
> 
> Related to this, formatting with sequences is also much more readable 
> when there are complex interpunction and quoting characters present, 
> like this:
> 
> print "'"+var1+"','"+var2'"+","+var3
> 
> the above is much more readable as
> 
> print "'%s', '%s', %s" % (var1, var2, var3)

Hmm, with my hastily written contrived example, I had forgotten about
sequences like that.

Oddly, in the case above I prefer reading the bottom example,

And shouldn't that top one be

print "'"+var1+"','"+var2+"',"+var3

;)

So to conclude, in future I will read it, and see how readable it is
before I decide. But now at least I do see a reason not to
pathalogically avoid such string formatting :)

Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
> [Matthew Warren]
> 
> | Blame outlook and AutoCaps. If number were a number I would write
> | 
> | print "There are",number,"ways to skin a "+furryanimal
> 
> You see now that strikes me as a bit mixed up. Why not simply use?
> 
> print "a", number, "c", string
> 

Habit (not always a good thing..), and it helps keep the distinction as
to what is a number and what is s a string.


> 
> Ultimately it's down to you, but I think you may be doing
> your code a disservice by so assiduously avoiding the %s-style
> of string building. Your last example suggests that you
> see the dict-subtitution flavour simply as an alternative to doing
> "a" + dict[key1] + "b" + dict[key2] etc. I doubt if I've *ever* 
> started with the one and ended with the other; rather I've seen that
> my code would be more readable if I put/had what I wanted into a
> dict (or a dict-like object; just has to support __getitem__).


Not quite. I was meaning that where I had things naturally in a dict and
found myself concatenating the string, I would now probably use the
substitution method.


> 
> An easy example of this is where -- like many, I believe -- I 
> prefer my
> database rows to come in dict-like objects, rather than the 
> tuples which
> the dbapi stipulates. Again, like many, I've used or rolled my own
> wrapper
> which means I can do things like this, where my dbutils.fetch function
> returns some kind of object which traps __getitem__ calls for column
> names and returns the appropriate entry in the underlying tuple:
> 
> 
> import dbutils
> db = db_module.connect (...)
> for row in dbutils.fetch (db, "SELECT * FROM blah"):
>   print "Customer %(name)s (%(id)d) has %(n_orders)d 
> outstanding orders
> since %(last_order_date)s" % row
> 
> 
> 
> TJG

I'm only just really looking into the effects of using things like
__getitem__ etc.., I imagine my approach will become more sophisticated
once I have looked into them.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: dictionary of list from a file

2006-10-05 Thread Matthew Warren
 

> -> > Python 2.5 introduced a dictionary type with automatic 
> > creation of values,
> > ala Perl:
> > 
> > ===
> > from collections import defaultdict
> > 
> > d = defaultdict(list)
> > for line in fl:
> >  k, v = line.strip().split()
> >  d[k].append(v)
> > 
> > for k,v in d.items():
> >  print k, v
> > ===
> > 
> > Notice that Python is always more strongly typed, so you have 
> > to specify a
> > factory function.
> 
> 
> Yay! Python2.5 fixed my approach to this, I tried
> 
> from collections import defaultdict
> f=file('c:\\test.txt')
> lines=f.readlines()
> f.close()
> d=defaultdict(list)
> [ d[l.split()[0]].append(l.split()[1]) for l in lines ]
> 
> But, if I try your (to see if I've actually got it right)
> 
> For k,v in d.items():
>   print k,v
> 
> I get
> 
> AttributeError: 'list' object has no attribute 'items'
> 


Okok, I'm silly.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: dictionary of list from a file

2006-10-05 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Giovanni Bajo
> Sent: 04 October 2006 15:17
> To: python-list@python.org
> Subject: Re: dictionary of list from a file
> 
> [EMAIL PROTECTED] wrote:
> 
> > while(){
> >   @info=split(/ +/,$_);
> >   push (@{$tmp{$info[0]}},$info[1]);
> > }
> >
> > and then
> > foreach $key (keys %tmp){
> >print "$key -> @{$tmp{$key}}\n";
> > }
> 
> Python 2.5 introduced a dictionary type with automatic 
> creation of values,
> ala Perl:
> 
> ===
> from collections import defaultdict
> 
> d = defaultdict(list)
> for line in fl:
>  k, v = line.strip().split()
>  d[k].append(v)
> 
> for k,v in d.items():
>  print k, v
> ===
> 
> Notice that Python is always more strongly typed, so you have 
> to specify a
> factory function.


Yay! Python2.5 fixed my approach to this, I tried

from collections import defaultdict
f=file('c:\\test.txt')
lines=f.readlines()
f.close()
d=defaultdict(list)
[ d[l.split()[0]].append(l.split()[1]) for l in lines ]

But, if I try your (to see if I've actually got it right)

For k,v in d.items():
print k,v

I get

AttributeError: 'list' object has no attribute 'items'




This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: building strings from variables

2006-10-05 Thread Matthew Warren

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Gal Diskin
> Sent: 05 October 2006 16:01
> To: python-list@python.org
> Subject: building strings from variables
> 
> Following a discussion with an associate at work about various ways to
> build strings from variables in python, I'd like to hear your opinions
> and preferred methods. The methods we discussed are:
> 1.  some_string = "cd %s ; %s  %d %s %s" % ( working_dir, ssh_cmd,
> some_count, some_param1, some_param2)
> 
> 2. import string
> template = string.Template("cd $dir ; $cmd $count $param1
> $param2")
> some_string = template.substitute(dict(dir=working_dir,
> 
> cmd=ssh_cmd,
> 
> count=some_count,
> 
> pararm1=some_param1,
> 
> param2=some_param2))
> here you can use a couple of nice tricks by using class.__dict__ and
> globals() \ locals() dictionaries.
> 
> 3. some_string = "cd "+working_dir+" ; "+ssh_cmd+ "
> "+str(some_count)+" "+some_param1+" "+some_param2
> (all these are supposed to produce the same strings)
> 
> Which methods do you know of \ prefer \ think is better because...?
> I will appreciate any opinions about the matter.

:D

I think, it would depend really on what your aims are (readable code,
fast string generation...), and how the vars you want to build the
string from are respresented in your code (is it natural to use a dict
etc..)

I kicked off a conversation similar to this earlier today, and that was
my conclusion after helpful debate & advice.

Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Using twisted, not telnetlib for interactive telnet (WAS: RE: Improving telnetlib)

2006-10-06 Thread Matthew Warren
 

> >The trouble is, I havent got a clue where to start and would 
> appreciate
> >a couple of pointers to get me going...
> >
> 
> I'd suggest taking a look at Twisted, which contains a more complete
> telnet implementation (not as important for being able to launch vi),
> an ssh implementation (which you might want to use instead of telnet),
> a VT102 implementation (which is actually what will help you 
> run programs
> that want to fiddle around with the cursor in fancy ways), as 
> well as a
> fair amount of work towards a simple terminal emulator (to 
> help you keep
> track of what vi has done to your virtual terminal).
> 
> API docs for insults:
> 
> http://twistedmatrix.com/documents/current/api/twisted.conch.i
> nsults.html
> 
> And for the telnet implementation:
> 
> http://twistedmatrix.com/documents/current/api/twisted.conch.t
> elnet.html
>


Looking through those docs quickly leads me into quite a bewildering
maze.

As a kind of way to perhaps get me heading in the right direction and
understanding how I start to hang all that together to get what I want,
I would appreciate it if anyone could look at this little code snippet I
have, and illustrate how I can end up doing the same thing with twisted,
but end up with an interactive connection that can handle vi...  From
there I will happily trundle off by myself.

I think I'm looking for help in getting that AhA! moment :)

Snippet;

C=telnetlib.Telnet(self.TCPAddress)
.
.
((connection / password handling etc..))
.
.
if AliasedSubbedCmd=='__interact':
if system=='UNIX':
retkey='^D'
else:
retkey='^Z'
  print '\nTELNET entity '+self.Name+' entering interactive
mode. Use '+retkey+' to come back\n'
C.mt_interact()


...at this point I am essentially on the remote system command line,
with a very dumb terminal. How could I do this in twisted and end up
with a fairly  clever terminal?


Thanks,

Matt.



This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   >