An array of a "list" of names.

2005-03-13 Thread spencer
Hi,
I'm using NumPy to build an array of a
list of names that will be of multiple
dimensions.I did do a google on this 
subject, but couldn't find what I was looking
for. The problem I'm having is there are 
space between each character in each name.
To fix this
I used the attribute 'tostring'. This did
remove the spaces but now I got to walk
the string to rebuild the array with
each name being a list.Is there an
easier way? If so, how?
TIA
Wayne


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


why this error?

2005-03-15 Thread spencer
Hi,
I'm not sure why I can't concatenate dirname() with basename().

Traceback (most recent call last):
  File "showDir.py", line 50, in ?
print 'somthing new...', os.path.join(os.path.dirname(os.getcwd)) +
os.path.basename(os.getcwd())
  File "/usr/lib/python2.3/posixpath.py", line 119, in dirname
return split(p)[0]
  File "/usr/lib/python2.3/posixpath.py", line 77, in split
i = p.rfind('/') + 1
AttributeError: 'builtin_function_or_method' object has no attribute
'rfind'
Thanks,
Wayne


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


A Q. on pop().

2005-03-17 Thread spencer
Hi,
The code.

def buildStackMajor():
for node in dirStackMinor:
#print 's is the node...', node
dirStackMajor.append(node)
dirStackMinor.pop()
print 'POP the stack...', len(dirStackMinor)
print 'after pop...', dirStackMinor

When I start the "for" loop I have 11 members in 
the stack- a list. The pop() is poping member
from the back, as though it's deal with a LIFO
stack instead of LIFO. I never get pass a length
of 5.
first Q.. Why is pop() starting from the back
  back of the stack?
second Q.. Why can't I never empty the stack?
Thanks. 

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


How to run python without python

2010-04-01 Thread Spencer
Is there a way to developing a script on linux and give it
to someone on microsoft, so that they could run it on microsoft
without installing python?

Wayne

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


Instances' __setitem__ methods

2011-06-20 Thread Spencer Pearson
I was recently trying to implement a dict-like object which would do
some fancy stuff when it was modified, and found that overriding the
__setitem__ method of an instance did not act the way I expected. The
help documentation (from help(dict.__setitem__)) claims that
"d.__setitem__(k,v)" is equivalent to "d[k]=v", but I've produced this
code that, on Python 2.6, acts differently in the two cases.

def print_args( key, value ):
print "print_args called: key = %s, value = %s" %(key,value)

class MyDict( dict ):
def __init__( self ):
dict.__init__( self )
self.__setitem__ = print_args

def __setitem__( self, key, value ):
print "ModelDict.__setitem__ called"
dict.__setitem__( self, key, value )

d = MyDict()

print "d.__setitem__(0,1):",
d.__setitem__(0,1)

print "d[0]=1:",
d[0]=1


I would expect the two setitems to both call print_args, but that's
not what happens. In the first case, it calls print_args, but in the
second case, the __setitem__ declared in MyDict is called instead.

The documentation at 
http://docs.python.org/reference/datamodel.html#specialnames
says that for new-style classes, "x[i]" is equivalent to
"type(x).__getitem__(x, i)". I assume that "x[i]=y" has similarly been
changed to be equivalent to "type(x).__setitem__(x, i, y)", since that
would produce the results that I'm getting. Is the help documentation
for dict.__setitem__ just outdated, or am I missing some subtlety
here?

Also: when I say "d.f(*args)", am I correct in thinking that d checks
to see if it has an instance attribute called "f", and if it does,
calls f(*args); and if it doesn't, checks whether its parent class
(and then its grandparent, and so on) has a class attribute called
"f", and if it does, calls f(x, *args)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Add a method to list the current named logging levels

2021-09-02 Thread Edward Spencer
Sometimes I like to pass the logging level up to the command line params so my 
user can specific what level of logging they want. However there is no easy 
method for pulling the named logging level names.

Looking into the code, it would actually be incredibly easy to implement;

in `logging.__init__.py`;

def listLevelNames():
return _nameToLevel.keys()

You could obviously add some other features, like listing only the defaults, 
sorted by numerical level or alphabetically, etc. But really this basic 
implementation would be enough to expose the internal variables which shouldn't 
be accessed because they change (and in fact, between python 2 and 3, they did).

Any thoughts?

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


Re: Add a method to list the current named logging levels

2022-03-30 Thread Edward Spencer
在 2021年9月3日星期五 UTC+1 18:50:51, 写道:
> > On 2 Sep 2021, at 23:38, Dieter Maurer  wrote:
> > 
> > Edward Spencer wrote at 2021-9-2 10:02 -0700: 
> >> Sometimes I like to pass the logging level up to the command line params 
> >> so my user can specific what level of logging they want. However there is 
> >> no easy method for pulling the named logging level names. 
> >> 
> >> Looking into the code, it would actually be incredibly easy to implement; 
> >> 
> >> in `logging.__init__.py`; 
> >> 
> >> def listLevelNames(): 
> >> return _nameToLevel.keys() 
> >> 
> >> You could obviously add some other features, like listing only the 
> >> defaults, sorted by numerical level or alphabetically, etc. But really 
> >> this basic implementation would be enough to expose the internal variables 
> >> which shouldn't be accessed because they change (and in fact, between 
> >> python 2 and 3, they did). 
> >> 
> >> Any thoughts? 
> > 
> > Usually, you use 5 well known log levels: "DEBUG", "INFO", "WARNING", 
> > "ERROR" and "CRITICAL". 
> > No need to provide a special function listing those levels.
> I add my own levels, but then I know I did it. 
> 
> Barry 
> 
> > 
> > 
> > 
> > -- 
> > Dieter 
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list 
> >

Yes, the names are already well defined. But every software project anyone has 
that needs to use logging then has to define that list, which is a waste of 
time since they're already defined inside the logging repo. But no-one can 
access them unless they use protected variables. If it's a case of not wanting 
users to be able to modify the defaults, then just define that list of log 
levels as a tuple. Hiding it is unnecessary.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Add a method to list the current named logging levels

2022-03-30 Thread Edward Spencer
在 2022年3月30日星期三 UTC+1 16:38:26, 写道:
> > On 30 Mar 2022, at 16:11, Edward Spencer  wrote:
> > 
> > 在 2021年9月3日星期五 UTC+1 18:50:51, 写道: 
> >>>> On 2 Sep 2021, at 23:38, Dieter Maurer  wrote: 
> >>> 
> >>> Edward Spencer wrote at 2021-9-2 10:02 -0700: 
> >>>> Sometimes I like to pass the logging level up to the command line params 
> >>>> so my user can specific what level of logging they want. However there 
> >>>> is no easy method for pulling the named logging level names. 
> >>>> 
> >>>> Looking into the code, it would actually be incredibly easy to 
> >>>> implement; 
> >>>> 
> >>>> in `logging.__init__.py`; 
> >>>> 
> >>>> def listLevelNames(): 
> >>>> return _nameToLevel.keys() 
> >>>> 
> >>>> You could obviously add some other features, like listing only the 
> >>>> defaults, sorted by numerical level or alphabetically, etc. But really 
> >>>> this basic implementation would be enough to expose the internal 
> >>>> variables which shouldn't be accessed because they change (and in fact, 
> >>>> between python 2 and 3, they did). 
> >>>> 
> >>>> Any thoughts? 
> >>> 
> >>> Usually, you use 5 well known log levels: "DEBUG", "INFO", "WARNING", 
> >>> "ERROR" and "CRITICAL". 
> >>> No need to provide a special function listing those levels. 
> >> I add my own levels, but then I know I did it. 
> >> 
> >> Barry 
> >> 
> >>> 
> >>> 
> >>> 
> >>> -- 
> >>> Dieter 
> >>> -- 
> >>> https://mail.python.org/mailman/listinfo/python-list 
> >>> 
> > 
> > Yes, the names are already well defined. But every software project anyone 
> > has that needs to use logging then has to define that list, which is a 
> > waste of time since they're already defined inside the logging repo. But 
> > no-one can access them unless they use protected variables. If it's a case 
> > of not wanting users to be able to modify the defaults, then just define 
> > that list of log levels as a tuple. Hiding it is unnecessary.
> Is logging.getLevelNamesMapping() what you are looking for? 
> 
> Barry 
> 
> 
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list

Hi Barry,

What version for python / logging are you seeing that method in? I don't appear 
to be able to find it.
I vaguely remember seeing something similar to it though, did it return a dict 
of {: } only or did it also include the reverse of int to 
str?

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


Multiple problems with Python 3.7 under Windows 7 Home Premium

2018-09-21 Thread Spencer Graves

Hello:


  I'm having a series of problems getting Python 3.7 to work on a 
machine running Windows 7 Home Premium with SP1.



WEBINSTALL.EXE:


  "python-3.7.0-amd64-webinstall.exe" stopped seemingly before it 
started.  I can try it again and give you a more precise error message 
if you want, but I want to describe some of the other problems I've had 
first.



  "python-3.7.0-amd64.exe" acted like it installed properly, until 
I started using it.  IDLE seemed to work for me on some simple tasks, 
but I wanted to try PyAudio, and I've so far not been able to make that 
work.



PYTHON - M PIP INSTALL PYAUDIO


  "python -m pip install pyaudio" stopped with 'error: Microsoft 
visual C++14.0 is required.  Get it with "Microsoft Visual C++ Build 
Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools";.



  That web address "http://landinghub..."; gave "404 NOT FOUND". 
However, I found elsewhere Microsoft Visual C++ Build Tools" and seemed 
to get them installed.



  Then "python -m pip install pyaudio" stopped with "error: command 
'C:\\Program Files (x86)\\Microsoft Visual 
Studio\\BuildTools\\VC\\Tools\\MSVC\\14.15.26726\\bin\\HostX86\\x64\\c1.exe' 
failed with exit status 2".



GCCWINBINARIES


  Searching for "... exit status 2" led me to a suggestion to use 
"https://github.com/develersrl/gccwinbinaries".  That failed with "No 
Python installation found in the registried.  It won't be possible to 
install the bindings."



WINDOWS APP CERTIFICATION KIT


  Somehow, I got a notice to try "Windows App Certification Kit 
10.0.17134.12".  I tried that with "program installer: 
python-3.7.0-amd64.exe" and "command:  python".  This moved along, then 
generated a popup saying "Modify setup", then stopped.  After waiting 
about 12 minutes, I clicked the "x" in the upper right. Then it moved 
again for a time until what looked like the same popup appeared during 
"Installed programs / In progress ...".  After clicking "x" in the upper 
right, I got, "Testing cannot continue unless an application is 
successfully installed, including a discoverable shortcut and an entry 
in add / remove programs."



  I repeated "Windows App Certification Kit", except that this time 
when the "Modify setup" window appeared, I clicked "repair". It came to 
that same point and gave the same error.



  Suggestion?
  Thanks,
  Spencer


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


Re: Multiple problems with Python 3.7 under Windows 7 Home Premium

2018-09-22 Thread Spencer Graves
  Thanks very much to all who replied.  This problems was solved as 
follows:  First I downloaded "PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl" 
from "https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio"; as suggested 
by MRAB and Terry Reedy.  Then I ran "python -m pip install 
PyAudio‑0.2.11‑cp37‑cp37m‑win_amd64.whl".  Then I ran the script at 
"https://stackoverflow.com/questions/35344649/reading-input-sound-signal-using-python#35390981";. 
That produced "output.wav" containing 5 seconds of sound recorded from a 
radio connected to "audio in" on my Windows 7 machine.



  Thanks again.
  Spencer Graves


On 2018-09-21 23:33, Terry Reedy wrote:

On 9/21/2018 8:57 PM, MRAB wrote:

On 2018-09-22 01:02, Michael Torrie wrote:

On 09/21/2018 07:22 AM, Spencer Graves wrote:

PYTHON - M PIP INSTALL PYAUDIO


   "python -m pip install pyaudio" stopped with 'error: 
Microsoft visual C++14.0 is required.  Get it with "Microsoft 
Visual C++ Build Tools": 
http://landinghub.visualstudio.com/visual-cpp-build-tools";.


You're going to want to wait until binary wheels of pyaudio for Python
3.7 and Windows 64-bit land in the PyPI repository.  Unless you're
already a visual studio user and have the where with all to build it
from source.  Python 3.7 is a recent release, so I'm not surprised that
some packages don't yet have binary wheels in the repository.


Christoph Gohlke's site appears to have a binary release for Python 3.7:

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio


As I remember, he was building binaries for the 300 packages on the 
site well before the 3.7.0 release.




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


Problems installing RStudio from Anaconda Navigator

2018-10-11 Thread Spencer Graves
  I clicked "Install" for "RStudio 1.1.456" in Anaconda Navigator 
1.9.2 under Windows 7 Home Premium SP1.  It said "Installing application 
RStudio" for over 30 minutes.  It looked stuck.



  Then I read a GitHub post that said, "On the environments tab 
create a new R/Python3.5 environment", then install RStudio into that 
new environment.[1]



  So I killed the RStudio install and looked at the "Environments 
tab".  I'm new to Anaconda Navigator, and I didn't know what I was 
looking at so decided to ask here.



  Suggestions?


  Thanks,
  Spencer Graves


p.s.  I do not currently have RStudio installed on that machine.  On a 
macOS 10.13.6 machine, I have have RStudio 1.2.792 on a macOS 10.13.6.  
Anaconda Navigator 1.9.2 on my Mac fails to recognize the existing and 
more recent installation of RStudio 1.2.792 there. Should I, e.g., try 
to "Import" that existing RStudio installation?



[1] https://github.com/conda/conda/issues/4204
--
https://mail.python.org/mailman/listinfo/python-list


Re: Overwhelmed by the Simplicity of Python. Any Recommendation?

2018-10-12 Thread Spencer Graves



On 2018-10-12 11:44, Rhodri James wrote:

On 12/10/18 17:12, Rob Gaddi wrote:

On 10/11/2018 11:29 PM, Kaan Taze wrote:

Hi everyone,

Since this is my first post to mail-list I'm kind of hesitant to ask 
this
question here but as many of you spend years working with Python 
maybe some

of you can guide me.

What I trouble with is not a logical error that exist on a program I 
wrote.
It's the Python itself. Well, I'm 22 years old CS student -from 
Turkey- and
what they showed us at university was C Language and Java but I 
mainly use

C in school projects etc. So it's been few months that I started to use
Python for my personal side-projects. There are lots of resources to 
learn
language. I do what I need to do with Python too but I was kinda 
shocked
when I solve Python questions at Hackerrank. Even with list 
comprehensions

you can implement in very smart way to get things done and easy.
Iterations, string operations. The codes I see on the Internet using 
basics
in a very clever way which I couldn't come up with the same solution 
if I
tried to for some time. I do understand this ways but coming from 
ANSI C

makes it hard to see this flexibility. I probably do things in a both
inefficient and hard way in my projects.

How do I get used to this? Is this just another "practice, practice,
practice" situation? Anything you can recommend?


All the best.

Kaan.



A) Yes, it's practice practice practice.

B) Don't get hung up on finding the clever solution. Comprehensions 
and generators and lots of other things are great under some 
circumstances for making the code clearer and easier to read, but 
they too can become the hammer that makes everything look like a 
nail.  The most important thing is that your code is logical, clean, 
and easy to understand.  If it doesn't take full advantage of the 
language features, or if the performance isn't optimized to within an 
inch of its life, well so be it.


I completely agree.  I too have come from a background in C, and still 
do most of my day job in C or assembler.  It took a while before I was 
writing idiomatic Python, never mind efficient Python (arguably I 
still don't, but as Rob says, who cares?).  Don't worry about it; at 
some point you will discover that the "obvious" Python you are writing 
looks a lot like the code you are looking at now and thinking "that's 
really clever, I'll never be able to to that."



I suggest two things:


  1.  Document your work as you do it in something like Jupyter 
Notebooks that were discussed in another recent thread.  I use "R 
Markdown Documents" in RStudio.  This allows me to mix Python code with 
text and code for other languages (including R, C, SQL, and others).  I 
tried installing Jupyter using Ananconda Navigator and failed -- under 
both Windows 7 and macOS 10.14.[1]  One consulting gig I had involved 
spending roughly a week creating an "R Markdown Document" mixing text 
with code and results analyzing a client's data, followed by months 
replying to questions by asking, "Did you look at p. ___ in the R 
Markdown Document I gave you" -- plus a few extensions to that 
document.  An article in The Atlantic last April claimed, "The 
scientific research paper is obsolete" and is being replaced by Jupyter 
Notebooks.[2]  I'd like to see a serious comparison of "R Markdown 
Documents" with "Jupyter Notebooks":  The latter may be better, but I 
was unable to even started with them after two days of effort.



  2.  Find a reasonable "Introduction to Python" on the web. Others 
on this list should be able to suggest several.  I just found 
"https://docs.python.org/2/tutorial/introduction.html".  A web search 
for "an introduction to Python" identified several others. I'd also be 
interested in reference(s) others might suggest for "creating python 
packages".



  Hope this helps.
  Spencer Graves


[1] RStudio offers a free "Desktop" version, which I have used routinely 
for the past three years.  It's available at 
"www.rstudio.com/products/rstudio/download".  Creating "R Markdown 
Documents" (File > "New File" > "R Markdown..." in RStudio) made a 
dramatic improvement in my productivity in many ways similar to those 
described by Paul Romer in 
"https://paulromer.net/jupyter-mathematica-and-the-future-of-the-research-paper";. 
(I also experimented with File > "New File" > "R Notebook" in RStudio 
and encountered bazaar errors I could not understand -- and no benefits 
that I could see that would push me to spend more time trying to get 
past the problems I encountered.  I've used "R Markdown Documents" 
extensively for three years -- with R -- and I found it easy to use with 
Python once I learned I could do that. See 
"https://bookdown.org/yihui/rmarkdown";.



[2] 
https://www.theatlantic.com/science/archive/2018/04/the-scientific-paper-is-obsolete/556676/


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


Package creation documentation?

2018-10-16 Thread Spencer Graves
  Where can I find a reasonable tutorial on how to create a Python 
package?



  I've not created a Python package before, and I want to. Sadly, 
I'm having trouble finding reasonable documentation on how to do so.



  According to the Python 3 Glossary, "a package is a Python module 
with an __path__ attribute."[1]



  I found "packaging.python.org", which recommends "Packaging 
Python Projects"[2] and "An Overview of Packaging for Python".[3] I 
failed to find "__path__" in either.



  I've started a project on GitHub for this, which includes what I 
have so far toward building a Python package I want along with RStudio 
Rmarkdown Documents summarizing what I've tried so far.[4]



  What would you suggest I do to understand what I should do to 
create a "__path__ attribute" for this, and what I should do after that?



  I have decades of coding experience, but only a small portion of 
that was with Python, and that was roughly 7 years ago.



  Thanks,
  Spencer Graves


[1] https://docs.python.org/3/glossary.html#term-package


[2] https://packaging.python.org/tutorials/packaging-projects/


[3] https://packaging.python.org/overview/


[4] https://github.com/sbgraves237/radioMonitor

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


Wikipedia on Python

2018-10-16 Thread Spencer Graves
  Thanks to Léo El Amri and Thomas Jollans for their quick and 
helpful replies to my question about "Package creation documentation".



  Beyond that, I'd like to encourage people on this list to review 
the Wikipedia article on "Python (programming language)",[1] especially 
the claim that "a package is a Python module with an __path__ 
attribute", which I added on 2018-09-24 to help me understand the 
distinction.



  That Wikipedia article has averaged over 6,000 views per day over 
the past 3 years.  Therefore, any improvements will benefit lots of people.



  If you have suggestions for how the article might be improved, 
you can post them to the "Talk" page associated with that article or 
send them to me.  If you are "autoconfirmed" with the Wikimedia system, 
you can make the changes yourself.



  Thanks,
  Spencer Graves


[1] https://en.wikipedia.org/wiki/Python_(programming_language)


On 2018-10-16 11:14, Léo El Amri wrote:

Hello Spencer,

On 16/10/2018 17:15, Spencer Graves wrote:

   Where can I find a reasonable tutorial on how to create a Python
package?

IMO, the best documentation about this is the tutorial:
https://docs.python.org/3/tutorial/modules.html#packages


   According to the Python 3 Glossary, "a package is a Python module
with an __path__ attribute."[1]

What you are looking at are the technical details of what a package is.
Incidentally, if you follow the tutorial, everything will get in-place.


   I found "packaging.python.org", which recommends "Packaging Python
Projects"[2] and "An Overview of Packaging for Python".[3]

packaging.python.org is centered on "How to install and distribute
Python packages (Or modules)"

- Léo



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


Mixing R and Python in the same Jupyter Notebook and finding Python code within an RMarkdown document

2018-11-02 Thread Spencer Graves

Hello, All:


  Two questions:


        1.  Is it feasible to mix R and Python code in the same 
Jupyter notebook?  If yes, can you please point me to an example?



        2.  How can one find Python code from within and R Markdown 
document?



          ** "https://github.com/sbgraves237/radioMonitor"; 
includes "radioMonitor-init2018-10-11.Rmd" that shows it's possible to 
mix R and Python snippets in the same R Markdown (*.Rmd) document.  
However, "radioMonitor0_1.Rmd" calls 'system("py idle0_1.py")' in an R 
snippet, which does what I want (namely recording 5 seconds of whatever 
is connected to "audio in" or something similar on your computer and 
writing it to "KKFI2018-10-12t13_16-5sec.wav".



  Thanks,
  Spencer Graves

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


Re: Good editor for python

2018-11-11 Thread Spencer Graves
  People rave about Jupyter Notebooks, which reportedly allow you 
to mix narrative with code describing what you are doing and why.



  I primarily program in R, and RMarkdown Documents in RStudio 
allow me to mix narrative with R and Python code.  I explain what I'm 
doing and why, then write "```{python}" ... "```" to encapsulate a 
Python code snippet and "```{r}" ... "```" for an R code snippet. Or I 
just use the Idle editor that comes with Python.



  Someone suggested that Apache Zeppelin  and / or BeakerX might be 
able to do this also, but I've not tried or verified them.



  Spencer Graves


On 2018-11-11 08:11, Andrew Z wrote:

If you do scripts - emacs/vi is the way to go.
If you need something more (like creating libraries,  classes) go with
pycharm. It is a professionally made IDE.

Over past 2 years ive been trying to "downgrade" myself to something with
less belts and whistles,  but come back to it all the time.

On the other hand , if you already use emacs - u should not need anything
else.

On Sun, Nov 11, 2018, 04:15 Olive 
I am not a professional programmer but I use Python regularly for custom
scripts (and plot with matplotlib). I have just learned VBA for Excel: what
I found amazing was their editor: it is able to suggest on the spot all the
methods an object support and there is a well-integrated debugger. I wonder
if something similar exists for Python. For now I just use emacs with the
command line pdb. What do people use here? Ideally I would like to have
something that is cross platform Windows/Linux.

Olivier

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



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


Re: sampling from frequency distribution / histogram without replacement

2019-01-14 Thread Spencer Graves



On 2019-01-14 18:40, duncan smith wrote:

On 14/01/2019 22:59, Gregory Ewing wrote:

duncan smith wrote:

Hello,
   Just checking to see if anyone has attacked this problem before
for cases where the population size is unfeasibly large.

The fastest way I know of is to create a list of cumulative
frequencies, then generate uniformly distributed numbers and
use a binary search to find where they fall in the list.
That's O(log n) per sample in the size of the list once it's
been set up.


That's the sort of thing I've been thinking about. But once I'd found
the relevant category I'd need to reduce its frequency by 1 and
correspondingly update the cumulative frequencies. Alternatively, I
could add an extra step where I selected a unit from the relevant
category with probability equal to the proportion of non-sampled units
from the category. I could maybe set up an alias table and do something
similar.

The other thing I was thinking about was iterating through the
categories (ideally from largest frequency to smallest frequency),
generating the numbers to be sampled from the current category and the
remaining categories (using numpy.random.hypergeometric). With a few
large frequencies and lots of small frequencies that could be quite
quick (on average). Alternatively I could partition the categories into
two sets, generate the number to be sampled from each partition, then
partition the partitions etc. binary search style.

I suppose I'll try the both the alias table + rejection step and the
recursive partitioning approach and see how they turn out. Cheers.



  R has functions "sample" and "sample.int";  see 
"https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/sample";. 
You can call R from Python, 
"https://sites.google.com/site/aslugsguidetopython/data-analysis/pandas/calling-r-from-python";. 




  These are in the "base" package.  I believe they have been an 
important part of the base R language almost since its inception and 
have been used extensively.  You'd have to work really hard to do 
better, in my judgment.



      Spencer Graves


DISCLAIMER:  I'm primarily an R guy and only use Python when I can't 
find a sensible way to do what I want in R.


Duncan


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


Re: 3 random numbers

2019-01-14 Thread Spencer Graves



On 2019-01-14 23:29, caig...@gmail.com wrote:

So I was given this question to be solved in Python 3 : Pick any 3 random 
ascending numbers and write out a loop function that prints out all 3 numbers. 
This was the code and solution presented to me. Can anyone understand it and 
explain it to me please ? I have looked at it but cannot see how it was derived 
and why the correct solution was printed. Thanks alot !

# any 3 ascending numbers , counter must start at 0.
# 400 467 851
i = 0
x = 400
while x < 852:
print(x)
if i > 0:
x = x + ((i + 4) * 67) + (i * 49)
else:
x = x + 67
i = i + 1



  This sounds like a homework problem for a class.  I don't know 
how this list treats such questions, but I suspect answering such 
questions may be discouraged.



  Hint:  Read the documentation on "while" and then trace the 
iterations.



      Spencer



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


Re: Your IDE's?

2019-03-25 Thread Spencer Graves



On 2019-03-25 18:55, Gene Heskett wrote:

On Monday 25 March 2019 18:20:29 DL Neil wrote:


On 26/03/19 10:38 AM, John Doe wrote:

What is your favorite Python IDE?

In case you are tempted to reply, neither of "John"'s supposed domains
resolves (to a web site)/has been registered.

--
Regards =dn

your email agent is inventing links? There were none in the single msg I
got from a john doe. Unless they were buried in the headers that kmail
doesn't show me..



  The original email was "From John Doe ", with a 
"Reply to John Doe ".  "Doe.com" is a URL being 
advertised for sale for $150,000.  "ping something.com" returns, "cannot 
resolve something.com:  Unknown host".



  Clearly the original poster is playing games with us.


      Spencer


Cheers, Gene Heskett


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


How to create a list and append to list inside a mqtt and GUI program?

2019-09-01 Thread Spencer Du
Hi 

I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a 
GUI file if the file exists in current directory. I want to add the file name 
to a list when a file is imported and for each subsequent file that is imported 
I want the file name to be imported to the same list and print the list or 
create a new list but with the imported file named added to list which has the 
existing file names that have already been imported. I was wondering how I do 
this. By the way run GUI.py to test this and test1.py and test2.py are the 
files which can be used to import GUI . 

GUI.py 

import logging 
from datetime import timedelta 
import time 
from thespian.actors import * 
from transitions import Machine 
import paho.mqtt.client as mqtt 
import importlib 
import os.path 
import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5 import QtWidgets, uic 
from mqtt import * 
import json 

class MainWindow(QtWidgets.QMainWindow): 
def __init__(self,parent = None): 
QMainWindow.__init__(self) 
super(MainWindow, self).__init__(parent) 
self.mdi = QMdiArea() 
self.setCentralWidget(self.mdi) 

self.setMinimumSize(QSize(800, 600)) 
self.setWindowTitle("PyQt button example - 
pythonprogramminglanguage.com") 

pybutton = QPushButton('Add device', self) 

pybutton.clicked.connect(self.importbutton) 

pybutton.move(100, 400) 
pybutton.resize(150, 32) 

self.textbox = QLineEdit(self) 
self.textbox.move(100,350) 
self.textbox.resize(100, 32) 

self.fileName_UI = "" 

def importbutton(self): 
self.fileName_UI = self.textbox.text() 
self.loadGUI() 

def getGUIFilename(self): 
return self.fileName_UI 

def loadGUI(self): 
print("Searching file", self.fileName_UI)   
try: 
module = __import__(self.fileName_UI) 
my_class = getattr(module, "SubWindow") 

sub = QMdiSubWindow() 

sub.setWidget(my_class()) 
sub.setWindowTitle("New GUI:  " + self.fileName_UI) 
self.mdi.addSubWindow(sub) 
sub.show() 

print("creating new instance " + self.fileName_UI) 
client = device("Device") 
client.run() 

client.loop_start()  # start the loop 
device_message = self.fileName_UI 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI") 
client.subscribe("microscope/light_sheet_microscope/UI") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI") 
client.publish("microscope/light_sheet_microscope/UI", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": 
"adding device"}}, indent=2)) 
time.sleep(1)  # wait 
client.loop_stop()  # stop the loop 
print("Device added" + "\n") 
listofdevice = [] 
listofdevice.append(self.fileName_UI) 
print(listofdevice) 
except: 
print("creating new instance " + self.fileName_UI) 
client = device("Device") 
client.run() 

client.loop_start()  # start the loop 
device_message = self.fileName_UI 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI") 
client.subscribe("microscope/light_sheet_microscope/UI") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI") 
client.publish("microscope/light_sheet_microscope/UI", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) 
time.sleep(2)  # wait 
client.loop_stop()  # stop the loop 
print(device_message + ".py " + "file doesn't exist") 
print("Device not added") 
if __name__ == "__main__": 
app = QApplication(sys.argv) 
mainWin = MainWindow() 
mainWin.show() 
publishedMessage = mainWin.getGUIFilename() 
sys.exit(app.exec_()) 

MQTT.py 
import logging 
from datetime import timedelta 
import time 
from thespian.actors import * 
from transitions import Machine 
import paho.mqtt.client as mqtt 
import importlib 
import os.path 
import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5 import QtWidgets, uic 

class device(mqtt.Client): 
def on_connect(self, mqttc, obj, flags, rc): 
if rc == 0: 
print("Connected to broker") 
else: 
print("Connection failed") 

# mqttc.subscribe("microscope/light_sheet_microscope/UI") 

def on_message(self, mqttc, userdata, message): 
msg = str(message.payload.decode("utf-8")) 
print("message recieved= " + msg) 
# print("File which you want to import(with .py extension)") 
print("message topic=", message.

Hi how do I import files inside a txt file?

2019-09-02 Thread Spencer Du
Hi

How do i import files inside a txt file if they exist in the current directory?

Here is the current code but I dont know how to do it correctly.

import paho.mqtt.client as mqtt
from mqtt import *
import importlib
import os
import os.path
# from stateMachine import *

with open("list_of_devices.txt", "r") as reader:
for item in reader:
try:
os.getcwd()
print("hi")
except:
print("error")

This is "list_of_devices.txt":
test1,test2

Each name refers to a python file.

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


Re: Hi how do I import files inside a txt file?

2019-09-02 Thread Spencer Du
On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid  wrote:
> Spencer Du  writes:
> 
> > How do i import files inside a txt file if they exist in the current 
> > directory?
> >
> > Here is the current code but I dont know how to do it correctly.
> >
> > import paho.mqtt.client as mqtt
> > from mqtt import *
> > import importlib
> > import os
> > import os.path
> > # from stateMachine import *
> >
> > with open("list_of_devices.txt", "r") as reader:
> > for item in reader:
> > try:
> > os.getcwd()
> > print("hi")
> > except:
> > print("error")
> >
> > This is "list_of_devices.txt":
> > test1,test2
> >
> > Each name refers to a python file.
> >
> My interpretation is that you want to read a file (list_of_devices.txt)
> and this file contains names of other files and you want to read those
> files as well and do something with them (read or print or whatever).
> 
> You can approach it like this: write a function to read a file and work
> on it. Like this,
> 
> def fn(fname):
> with open(fname, "r") as f:
>  try:
> # work with f
>  except:
> print("error")
> 
> Then use this function in your code that you have writen. Like this
> 
> with open("list_of_devices.txt", "r") as reader:
>  for item in reader:
>  try:
> fn(item)
>  except:
> print("error")
> 
> In the example that you gave, you have written contents of
> "list_of_devices.txt" as
> 
> test1,test2
> 
> Take care to read them as comma separated. Or if you have control then
> write them on separate lines.
> 
> Regards.
> -- 
> Pankaj Jangid

Hi Pankaj

I dont understand so what is complete code then?

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


Re: Hi how do I import files inside a txt file?

2019-09-02 Thread Spencer Du
On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid  wrote:
> Spencer Du  writes:
> 
> > How do i import files inside a txt file if they exist in the current 
> > directory?
> >
> > Here is the current code but I dont know how to do it correctly.
> >
> > import paho.mqtt.client as mqtt
> > from mqtt import *
> > import importlib
> > import os
> > import os.path
> > # from stateMachine import *
> >
> > with open("list_of_devices.txt", "r") as reader:
> > for item in reader:
> > try:
> > os.getcwd()
> > print("hi")
> > except:
> > print("error")
> >
> > This is "list_of_devices.txt":
> > test1,test2
> >
> > Each name refers to a python file.
> >
> My interpretation is that you want to read a file (list_of_devices.txt)
> and this file contains names of other files and you want to read those
> files as well and do something with them (read or print or whatever).
> 
> You can approach it like this: write a function to read a file and work
> on it. Like this,
> 
> def fn(fname):
> with open(fname, "r") as f:
>  try:
> # work with f
>  except:
> print("error")
> 
> Then use this function in your code that you have writen. Like this
> 
> with open("list_of_devices.txt", "r") as reader:
>  for item in reader:
>  try:
> fn(item)
>  except:
> print("error")
> 
> In the example that you gave, you have written contents of
> "list_of_devices.txt" as
> 
> test1,test2
> 
> Take care to read them as comma separated. Or if you have control then
> write them on separate lines.
> 
> Regards.
> -- 
> Pankaj Jangid

Hi I dont really understand this. So what would be the complete code? Also I 
want to import files if it exists based on what is in the txt file.

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


Re: Hi how do I import files inside a txt file?

2019-09-02 Thread Spencer Du
On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick  wrote:
> On Mon, Sep 2, 2019 at 8:46 AM Spencer Du  wrote:
> >
> > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid  wrote:
> > > Spencer Du  writes:
> > >
> > > > How do i import files inside a txt file if they exist in the current 
> > > > directory?
> > > >
> > > > Here is the current code but I dont know how to do it correctly.
> > > >
> > > > import paho.mqtt.client as mqtt
> > > > from mqtt import *
> > > > import importlib
> > > > import os
> > > > import os.path
> > > > # from stateMachine import *
> > > >
> > > > with open("list_of_devices.txt", "r") as reader:
> > > > for item in reader:
> > > > try:
> > > > os.getcwd()
> > > > print("hi")
> > > > except:
> > > > print("error")
> > > >
> > > > This is "list_of_devices.txt":
> > > > test1,test2
> > > >
> > > > Each name refers to a python file.
> > > >
> > > My interpretation is that you want to read a file (list_of_devices.txt)
> > > and this file contains names of other files and you want to read those
> > > files as well and do something with them (read or print or whatever).
> > >
> > > You can approach it like this: write a function to read a file and work
> > > on it. Like this,
> > >
> > > def fn(fname):
> > > with open(fname, "r") as f:
> > >  try:
> > > # work with f
> > >  except:
> > > print("error")
> > >
> > > Then use this function in your code that you have writen. Like this
> > >
> > > with open("list_of_devices.txt", "r") as reader:
> > >  for item in reader:
> > >  try:
> > > fn(item)
> > >  except:
> > > print("error")
> > >
> > > In the example that you gave, you have written contents of
> > > "list_of_devices.txt" as
> > >
> > > test1,test2
> > >
> > > Take care to read them as comma separated. Or if you have control then
> > > write them on separate lines.
> > >
> > > Regards.
> > > --
> > > Pankaj Jangid
> >
> > Hi Pankaj
> >
> > I dont understand so what is complete code then?
> >
> > Thanks
> > Spencer
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> 
> Pardon me for guessing, but your question seems to imply that you know
> how you want to do something .. but I'm not sure you have tackled your
> problem correctly.
> 
> My guess is:  Depending upon the names listed in a text file, you want
> to do different imports into your program.   You don't yet know how to
> read a file with python.
> 
> First, when you run your program, python compiles it in order.  Since
> you don't know what you want to import until after you run your
> program, you can't import those modules.  You may be able to run a
> program to read the module list, then have it output to a new file the
> code you eventually want to run based on the modules you discovered.
> That sounds cute in a way, but probably not in a good way.  You could
> also surround import statements with try/except code that will import
> what it can, and alert you when it can't
> 
> Can you give us the bigger picture of what you want to accomplish?
> This might lead to a better solution than the one you are thinking of
> now
> 
> -- 
> Joel Goldstick
> http://joelgoldstick.com/blog
> http://cc-baseballstats.info/stats/birthdays

Hi

I have a txt file which contains the names of files. They are .py files. I want 
to import them into a python file if they exists in current directory and if 
the name of file does not exist then print out error and not import. How do I 
do this?

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


Re: Hi how do I import files inside a txt file?

2019-09-02 Thread Spencer Du
On Monday, 2 September 2019 15:29:07 UTC+2, Joel Goldstick  wrote:
> On Mon, Sep 2, 2019 at 9:21 AM Spencer Du  wrote:
> >
> > On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick  wrote:
> > > On Mon, Sep 2, 2019 at 8:46 AM Spencer Du  wrote:
> > > >
> > > > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid  wrote:
> > > > > Spencer Du  writes:
> > > > >
> > > > > > How do i import files inside a txt file if they exist in the 
> > > > > > current directory?
> > > > > >
> > > > > > Here is the current code but I dont know how to do it correctly.
> > > > > >
> > > > > > import paho.mqtt.client as mqtt
> > > > > > from mqtt import *
> > > > > > import importlib
> > > > > > import os
> > > > > > import os.path
> > > > > > # from stateMachine import *
> > > > > >
> > > > > > with open("list_of_devices.txt", "r") as reader:
> > > > > > for item in reader:
> > > > > > try:
> > > > > > os.getcwd()
> > > > > > print("hi")
> > > > > > except:
> > > > > > print("error")
> > > > > >
> > > > > > This is "list_of_devices.txt":
> > > > > > test1,test2
> > > > > >
> > > > > > Each name refers to a python file.
> > > > > >
> > > > > My interpretation is that you want to read a file 
> > > > > (list_of_devices.txt)
> > > > > and this file contains names of other files and you want to read those
> > > > > files as well and do something with them (read or print or whatever).
> > > > >
> > > > > You can approach it like this: write a function to read a file and 
> > > > > work
> > > > > on it. Like this,
> > > > >
> > > > > def fn(fname):
> > > > > with open(fname, "r") as f:
> > > > >  try:
> > > > > # work with f
> > > > >  except:
> > > > >         print("error")
> > > > >
> > > > > Then use this function in your code that you have writen. Like this
> > > > >
> > > > > with open("list_of_devices.txt", "r") as reader:
> > > > >  for item in reader:
> > > > >  try:
> > > > > fn(item)
> > > > >  except:
> > > > > print("error")
> > > > >
> > > > > In the example that you gave, you have written contents of
> > > > > "list_of_devices.txt" as
> > > > >
> > > > > test1,test2
> > > > >
> > > > > Take care to read them as comma separated. Or if you have control then
> > > > > write them on separate lines.
> > > > >
> > > > > Regards.
> > > > > --
> > > > > Pankaj Jangid
> > > >
> > > > Hi Pankaj
> > > >
> > > > I dont understand so what is complete code then?
> > > >
> > > > Thanks
> > > > Spencer
> > > > --
> > > > https://mail.python.org/mailman/listinfo/python-list
> > >
> > > Pardon me for guessing, but your question seems to imply that you know
> > > how you want to do something .. but I'm not sure you have tackled your
> > > problem correctly.
> > >
> > > My guess is:  Depending upon the names listed in a text file, you want
> > > to do different imports into your program.   You don't yet know how to
> > > read a file with python.
> > >
> > > First, when you run your program, python compiles it in order.  Since
> > > you don't know what you want to import until after you run your
> > > program, you can't import those modules.  You may be able to run a
> > > program to read the module list, then have it output to a new file the
> > > code you eventually want to run based on the modules you discovered.
> > > That sounds cute in a way, but probably not in a good way.  You could
> > > also surround import s

Help needed urgently for running some code!!!!

2019-09-02 Thread Spencer Du
Hi

I want to execute 

"from devicesEmbedded import *": in GUI.py after all code in GUI.py is run. 
Also how do I make the devicesEmbedded.py reload and run when a txt file is 
created in the name of "list_of_devices.txt" in the GUI.py python program.


GUI.py

import logging
from datetime import timedelta
import time
from thespian.actors import *
from transitions import Machine
import paho.mqtt.client as mqtt
import importlib
import os
import os.path
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from mqtt import *
# from devicesEmbedded import *
import json

class MainWindow(QtWidgets.QMainWindow):
def __init__(self,parent = None):
QMainWindow.__init__(self)
super(MainWindow, self).__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)

self.setMinimumSize(QSize(800, 600))
self.setWindowTitle("PyQt button example - 
pythonprogramminglanguage.com")

pybutton = QPushButton('Add device', self)

pybutton.clicked.connect(self.importbutton)

pybutton.move(100, 400)
pybutton.resize(150, 32)

self.textbox = QLineEdit(self)
self.textbox.move(100,350)
self.textbox.resize(100, 32)

self.fileName_UI = ""

def importbutton(self):
self.fileName_UI = self.textbox.text()
self.loadGUI()

def getGUIFilename(self):
return self.fileName_UI

def loadGUI(self):
print("Searching file", self.fileName_UI)   
try:
module = __import__(self.fileName_UI)
my_class = getattr(module, "SubWindow")

sub = QMdiSubWindow()

sub.setWidget(my_class())
sub.setWindowTitle("New GUI:  " + self.fileName_UI)
self.mdi.addSubWindow(sub)
sub.show()

print("creating new instance " + self.fileName_UI)
client = device("Device")
client.run()

client.loop_start()  # start the loop
# device_message = self.fileName_UI
time.sleep(2)
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI")
client.subscribe("microscope/light_sheet_microscope/UI")
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI/list_of_devices")

client.publish("microscope/light_sheet_microscope/UI/list_of_devices", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": 
"adding device"}}, indent=2))
time.sleep(1)  # wait
client.loop_stop()  # stop the loop
print("Device added" + "\n")

client.run()
client.loop_start()
time.sleep(2)
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI/list_of_devices")

client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices")
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI/list_of_devices")

client.publish("microscope/light_sheet_microscope/UI/list_of_devices", 
self.fileName_UI + " added to device list")
time.sleep(1)
client.loop_stop()

listofdevices = []
listofdevices.append(self.fileName_UI)
with open("list_of_devices.txt", "a+") as myfile:
for item in listofdevices:
myfile.write(item + ",")


print(item)
print(listofdevices)
print("Device added to list")
except:
print("creating new instance " + self.fileName_UI)
client = device("Device")
client.run()

client.loop_start()  # start the loop
device_message = self.fileName_UI
time.sleep(2)
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI")
client.subscribe("microscope/light_sheet_microscope/UI")
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI")
client.publish("microscope/light_sheet_microscope/UI", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2))
time.sleep(2)  # wait
client.loop_stop()  # stop the loop
print(device_message + ".py " + "file doesn't exist")
print("Device not added")
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
a = mainWin.show()
try:
mainWin.show()
os.remove("list_of_devices.txt")
print("Awaiting devices to be launched")
except:
print("Awaiting devices to be launched")
publishedMessage = mainWin.getGUIFilename()
sys.exit(app.exec_())

devicesEmbedded.py:

import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference
from mqtt import *
# from GUI im

Help needed to run some code!!!!

2019-09-02 Thread Spencer Du
Hi

How can I execute "from devicesEmbedded import *" after this: "print("Device 
added to list")" in GUI.py because currently if I have the import added at the 
top of GUI.py file it always executes first before the GUI.py file is executed. 
I want the devicesEmbedded.py to execute after GUI.py has executed first.

Thanks
Spencer

GUI.py 

import logging 
from datetime import timedelta 
import time 
from thespian.actors import * 
from transitions import Machine 
import paho.mqtt.client as mqtt 
import importlib 
import os 
import os.path 
import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5 import QtWidgets, uic 
from mqtt import * 
# from devicesEmbedded import * 
import json 

class MainWindow(QtWidgets.QMainWindow): 
def __init__(self,parent = None): 
QMainWindow.__init__(self) 
super(MainWindow, self).__init__(parent) 
self.mdi = QMdiArea() 
self.setCentralWidget(self.mdi) 

self.setMinimumSize(QSize(800, 600)) 
self.setWindowTitle("PyQt button example - 
pythonprogramminglanguage.com") 

pybutton = QPushButton('Add device', self) 

pybutton.clicked.connect(self.importbutton) 

pybutton.move(100, 400) 
pybutton.resize(150, 32) 

self.textbox = QLineEdit(self) 
self.textbox.move(100,350) 
self.textbox.resize(100, 32) 

self.fileName_UI = "" 

def importbutton(self): 
self.fileName_UI = self.textbox.text() 
self.loadGUI() 

def getGUIFilename(self): 
return self.fileName_UI 

def loadGUI(self): 
print("Searching file", self.fileName_UI)   
try: 
module = __import__(self.fileName_UI) 
my_class = getattr(module, "SubWindow") 

sub = QMdiSubWindow() 

sub.setWidget(my_class()) 
sub.setWindowTitle("New GUI:  " + self.fileName_UI) 
self.mdi.addSubWindow(sub) 
sub.show() 

print("creating new instance " + self.fileName_UI) 
client = device("Device") 
client.run() 

client.loop_start()  # start the loop 
# device_message = self.fileName_UI 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI") 
client.subscribe("microscope/light_sheet_microscope/UI") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI/list_of_devices") 

client.publish("microscope/light_sheet_microscope/UI/list_of_devices", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": 
"adding device"}}, indent=2)) 
time.sleep(1)  # wait 
client.loop_stop()  # stop the loop 
print("Device added" + "\n") 

client.run() 
client.loop_start() 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI/list_of_devices") 

client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI/list_of_devices") 

client.publish("microscope/light_sheet_microscope/UI/list_of_devices", 
self.fileName_UI + " added to device list") 
time.sleep(1) 
client.loop_stop() 

listofdevices = [] 
listofdevices.append(self.fileName_UI) 
with open("list_of_devices.txt", "a+") as myfile: 
for item in listofdevices: 
myfile.write(item + ",") 


print(item) 
print(listofdevices) 
print("Device added to list") 
except: 
print("creating new instance " + self.fileName_UI) 
client = device("Device") 
client.run() 

client.loop_start()  # start the loop 
device_message = self.fileName_UI 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI") 
client.subscribe("microscope/light_sheet_microscope/UI") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI") 
client.publish("microscope/light_sheet_microscope/UI", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) 
time.sleep(2)  # wait 
client.loop

How to remove a string from a txt file?

2019-09-04 Thread Spencer Du
Hi

I want to remove a string from a txt file and then print out what I have 
removed. How do I do this.

The txt file is in this format and should be kept in this format.

txt.txt:
laser,cameras,

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


Help me fix a problem

2019-09-06 Thread Spencer Du
Hi 

I want to print yes in gui.py but it does not get printed because of the json. 
How do I fix this. Execute embedded.py and then gui.py to test.

embedded.py

import paho.mqtt.client as mqtt
from mqtt import *

client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)

client.on_connect = on_connect
client.subscribe("topic/test")
client.on_subscribe = on_subscribe
print("Subscribing to topic", "topic/test")
client.on_message = on_message

client.loop_forever()

gui.py

import paho.mqtt.client as mqtt
from mqtt import *
import json

# This is the Publisher

client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
print("Publishing message (name: Hello world!) to topic", "topic/test")
client.publish("topic/test",json.dumps({"name": "Hello world!"}));
client.loop_forever();

mqtt.py

import logging
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
print("Connecting to broker")
# client.subscribe("topic/test")

def on_subscribe(client, userdata, mid, granted_qos):
print("I've subscribed to topic")

def on_message(client, userdata, msg):
print("message recieved= " + msg.payload.decode())
# print("File which you want to import(with .py extension)")
print("message topic=", msg.topic)
print("message qos=", msg.qos)
print("message retain flag=", msg.retain)

if msg.payload[name] == "Hello world!":
print("Yes!")

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


Re: Help me fix a problem

2019-09-06 Thread Spencer Du
On Friday, 6 September 2019 20:15:40 UTC+2, MRAB  wrote:
> On 2019-09-06 18:11, Spencer Du wrote:
> > Hi
> > 
> > I want to print yes in gui.py but it does not get printed because of the 
> > json. How do I fix this. Execute embedded.py and then gui.py to test.
> > 
> > def on_message(client, userdata, msg):
> > print("message recieved= " + msg.payload.decode())
> > # print("File which you want to import(with .py extension)")
> > print("message topic=", msg.topic)
> > print("message qos=", msg.qos)
> > print("message retain flag=", msg.retain)
> > 
>  >if msg.payload[name] == "Hello world!":
>  >print("Yes!")
>  >
> What is the value of the variable called 'name'? Or did you intend that 
> to be a string?
> 
>   if msg.payload["name"] == "Hello world!":
>   print("Yes!")
"name" is part of {"name": "Hello world!"} which is a key value pair dictionary 
json.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me fix a problem

2019-09-06 Thread Spencer Du
On Friday, 6 September 2019 20:15:40 UTC+2, MRAB  wrote:
> On 2019-09-06 18:11, Spencer Du wrote:
> > Hi
> > 
> > I want to print yes in gui.py but it does not get printed because of the 
> > json. How do I fix this. Execute embedded.py and then gui.py to test.
> > 
> > def on_message(client, userdata, msg):
> > print("message recieved= " + msg.payload.decode())
> > # print("File which you want to import(with .py extension)")
> > print("message topic=", msg.topic)
> > print("message qos=", msg.qos)
> > print("message retain flag=", msg.retain)
> > 
>  >if msg.payload[name] == "Hello world!":
>  >print("Yes!")
>  >
> What is the value of the variable called 'name'? Or did you intend that 
> to be a string?
> 
>   if msg.payload["name"] == "Hello world!":
>   print("Yes!")


{"name": "Hello world!"} is a key value pair.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me fix a problem

2019-09-06 Thread Spencer Du
On Friday, 6 September 2019 20:15:40 UTC+2, MRAB  wrote:
> On 2019-09-06 18:11, Spencer Du wrote:
> > Hi
> > 
> > I want to print yes in gui.py but it does not get printed because of the 
> > json. How do I fix this. Execute embedded.py and then gui.py to test.
> > 
> > def on_message(client, userdata, msg):
> > print("message recieved= " + msg.payload.decode())
> > # print("File which you want to import(with .py extension)")
> > print("message topic=", msg.topic)
> > print("message qos=", msg.qos)
> > print("message retain flag=", msg.retain)
> > 
>  >if msg.payload[name] == "Hello world!":
>  >print("Yes!")
>  >
> What is the value of the variable called 'name'? Or did you intend that 
> to be a string?
> 
>   if msg.payload["name"] == "Hello world!":
>   print("Yes!")

"name" is part of {"name": "Hello world!"} which is a key value pair dictionary 
and json.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why is "Subscribing to topic topic/test" printed in console log before connect message?

2019-09-10 Thread Spencer Du
Hi 

I have code for publish and subscribe over mqtt. In the console log I have 
"Subscribing to topic topic/test" printed before connect message why is this? I 
want to this to be printed after the connect message. How do I fix this 
problem. Please run gui.py to test.

gui.py

import paho.mqtt.client as mqtt
from mqtt import *
import json
import time

client = mqtt.Client()
client.connect("broker.hivemq.com",1883,60)
client.on_connect = on_connect
client.on_message = on_message

client.loop_start()
print("Subscribing to topic", "topic/test")
client.subscribe("topic/test")
client.publish("topic/test", "Hello world!")
time.sleep(1)
client.loop_stop()

mqtt.py

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))

def on_message(client, userdata, msg):
if msg.payload.decode() == "Hello world!":
print("Yes!")

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


Re: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time

2019-09-15 Thread Spencer Graves



On 2019-09-14 07:30, Gene Heskett wrote:

On Saturday 14 September 2019 04:37:14 Larry Martell wrote:


On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro


wrote:

https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has
-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-t
ime/

I doubt this is unusual, and presume JP Morgan is big enough to
handle the change of status, either by managing security releases
in-house or relying on third-party releases (say, Anaconda). When I
retired from Citadel recently, most Python was still 2.7 (though the
group I worked in was well on the way to converting to 3.x, and no
new applications were written against 2.7). Bank of America has an
enterprise-wide system called Quartz. I wouldn't be surprised if it
was still running Python 2.7 (though I don't know for sure).

Yes Quartz is 2.7. As I’ve said before here, I know a lot of companies
running large apps in 2.7 and they have no intention of moving to 3.

And I, Larry, have little doubt that the hackers have a hole into a 2.7
install, all squirreled away, and waiting until 2.7 security support
goes away. It's the nature of the thing.

They will get hacked.  Its like asking if concrete will crack as you are
watching it being poured, will is the wrong question, when is far more
correct.

And it will cost them trillions in the long haul. The courts,
adjudicating damages, will not be kind to the foot dragger's who think
they are saving money.  History sure seems to be pointing in that
direction recently.

Its a puzzle to me, why so-called sane MBA's cannot understand that the
best defense is spending money on the offense by updateing their
in-house operating code. Or the OS under it.



  Is anyone interested in contacting these companies -- or the 
companies from which they buy cybersecurity insurance -- and inviting 
them to provide paid staff to maintain 2.7 and to offer further offer 
consulting services to help these clients inventory what they have and 
how much it would cost to migrate?



  For example, how much would it cost to write and maintain an 
emulator for 2.7.16 in 3.7.4?



  The Python Software Foundation does not want to maintain 2.7 for 
free anymore, but if there is sufficient demand, they should be thrilled 
to make a handsome profit off of it -- while providing high quality, 
good paying jobs for smart Pythonistas.



  As I'm thinking about it, the companies that provide 
cybersecurity insurance could be the best points of leverage for this, 
because they think about these kinds of things all the time. Insurance 
companies for decades and probably well over 100 years have required 
their commercial clients to employ night watch crews, who make the 
rounds of a facility collecting time stamps from different points in the 
facility, which they provide to insurer(s) in exchange for reduced rates 
-- on as a condition of getting insurance in the first place.  This is 
conceptually and practically the same kind of thing.



  Spencer Graves


Cheers, Gene Heskett


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


How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value.

2019-09-26 Thread Spencer Du
Hi

How do I publish a message of the qspinbox value when the qspinbox sets the 
slider with corresponding value.

Thanks

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'laser.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from pyqtconfig import ConfigManager

class Ui_Laser(object):
def setupUi(self, Laser):
self.config = ConfigManager()
Laser.setObjectName("Laser")
Laser.resize(379, 274)
Laser.setMinimumSize(QtCore.QSize(379, 268))
self.config.set_defaults({
'number': 13,
'number2': 0,
'number3': 0,
'number4': 0,
'number5': 0,
'number6': 0,
'on': True,
})
self.centralwidget = QtWidgets.QWidget(Laser)
self.centralwidget.setObjectName("centralwidget")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_6.setMaximum(100)
self.spinBox_6.setObjectName("spinBox_6")
self.gridLayout.addWidget(self.spinBox_6, 1, 5, 1, 1)
self.config.add_handler('number', self.spinBox_6)
self.verticalSlider_2 = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider_2.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_2.setObjectName("verticalSlider_2")
self.gridLayout.addWidget(self.verticalSlider_2, 0, 1, 1, 1)
self.verticalSlider_5 = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider_5.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_5.setObjectName("verticalSlider_5")
self.gridLayout.addWidget(self.verticalSlider_5, 0, 4, 1, 1)
self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox.setMaximum(100)
self.spinBox.setObjectName("spinBox")
self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1)
self.verticalSlider = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.gridLayout.addWidget(self.verticalSlider, 0, 0, 1, 1)
self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_2.setMaximum(100)
self.spinBox_2.setObjectName("spinBox_2")
self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1)
self.verticalSlider_3 = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider_3.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_3.setObjectName("verticalSlider_3")
self.gridLayout.addWidget(self.verticalSlider_3, 0, 2, 1, 1)
self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_5.setMaximum(100)
self.spinBox_5.setObjectName("spinBox_5")
self.gridLayout.addWidget(self.spinBox_5, 1, 4, 1, 1)
self.verticalSlider_6 = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider_6.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_6.setObjectName("verticalSlider_6")
self.gridLayout.addWidget(self.verticalSlider_6, 0, 5, 1, 1)
self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_4.setMaximum(100)
self.spinBox_4.setObjectName("spinBox_4")
self.gridLayout.addWidget(self.spinBox_4, 1, 3, 1, 1)
self.verticalSlider_4 = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider_4.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_4.setObjectName("verticalSlider_4")
self.gridLayout.addWidget(self.verticalSlider_4, 0, 3, 1, 1)
self.verticalSlider_7 = QtWidgets.QSlider(self.gridLayoutWidget)
self.verticalSlider_7.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_7.setObjectName("verticalSlider_7")
self.gridLayout.addWidget(self.verticalSlider_7, 0, 6, 1, 1)
self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget)
self.spinBox_3.setMaximum(100)
self.spinBox_3.setObjectName("spinBox_3")
self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 50, 41, 19))
self.pushButton.setCheckable(True)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(50, 50, 41, 19))
self.pushButton_2.setCheckable(True)
sel

How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value.

2019-09-27 Thread Spencer Du
'].connect(self.label_13.setNum)
self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum)
QtCore.QMetaObject.connectSlotsByName(Laser)

def hi(self):
self.spinBox.valueChanged['int'].connect(self.hi)

def retranslateUi(self, Laser):
_translate = QtCore.QCoreApplication.translate
Laser.setWindowTitle(_translate("Laser", "MainWindow"))
self.label_13.setText(_translate("Laser", " %"))
self.label_11.setText(_translate("Laser", " %"))
self.label_12.setText(_translate("Laser", "%"))
self.label_14.setText(_translate("Laser", " %"))
self.label_10.setText(_translate("Laser", "%"))
self.label_9.setText(_translate("Laser", " %"))
self.label_8.setText(_translate("Laser", "   %"))
self.pushButton.setText(_translate("Laser", "ON"))
self.pushButton_2.setText(_translate("Laser", "ON"))
self.pushButton_3.setText(_translate("Laser", "ON"))
self.pushButton_4.setText(_translate("Laser", "ON"))
self.pushButton_5.setText(_translate("Laser", "ON"))
self.pushButton_6.setText(_translate("Laser", "ON"))
self.pushButton_7.setText(_translate("Laser", "ON"))
self.label.setText(_translate("Laser", "445nm"))
self.label_2.setText(_translate("Laser", "488nm"))
self.label_3.setText(_translate("Laser", "515nm"))
self.label_4.setText(_translate("Laser", "561nm"))
self.label_5.setText(_translate("Laser", "594nm"))
self.label_6.setText(_translate("Laser", "638nm"))
self.label_7.setText(_translate("Laser", "LED"))
self.pushButton_8.setText(_translate("Laser", "ON"))


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Laser = QtWidgets.QMainWindow()
ui = Ui_Laser()
ui.setupUi(Laser)
Laser.show()
sys.exit(app.exec_())

laserEmbedded.py

import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class Laser(Actor):
async def handle_message(self, message: Message):
print("Laser")
await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a laser!" 
+"\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it. 
async with Laser() as laser:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(laser, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

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


How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value.

2019-09-27 Thread Spencer Du
bel_12") 
self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) 
self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_14.setObjectName("label_14") 
self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) 
self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) 
self.spinBox_6.setMaximum(100) 
self.spinBox_6.setObjectName("spinBox_6") 
self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) 
self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_10.setObjectName("label_10") 
self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) 
self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) 
self.spinBox_3.setMaximum(100) 
self.spinBox_3.setObjectName("spinBox_3") 
self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) 
self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_9.setObjectName("label_9") 
self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) 
self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_8.setObjectName("label_8") 
self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) 
- show quoted text -
self.spinBox.valueChanged['int'].connect(self.hi) 
self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) 
self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) 
self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) 
self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) 
self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) 
self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) 
QtCore.QMetaObject.connectSlotsByName(Laser) 

def hi(self): 
self.spinBox.valueChanged['int'].connect(self.hi) 

def retranslateUi(self, Laser): 
_translate = QtCore.QCoreApplication.translate 
Laser.setWindowTitle(_translate("Laser", "MainWindow")) 
self.label_13.setText(_translate("Laser", " %")) 
self.label_11.setText(_translate("Laser", " %")) 
self.label_12.setText(_translate("Laser", "%")) 
self.label_14.setText(_translate("Laser", " %")) 
self.label_10.setText(_translate("Laser", "%")) 
self.label_9.setText(_translate("Laser", " %")) 
self.label_8.setText(_translate("Laser", "   %")) 
self.pushButton.setText(_translate("Laser", "ON")) 
self.pushButton_2.setText(_translate("Laser", "ON")) 
self.pushButton_3.setText(_translate("Laser", "ON")) 
self.pushButton_4.setText(_translate("Laser", "ON")) 
self.pushButton_5.setText(_translate("Laser", "ON")) 
self.pushButton_6.setText(_translate("Laser", "ON")) 
self.pushButton_7.setText(_translate("Laser", "ON")) 
self.label.setText(_translate("Laser", "445nm")) 
self.label_2.setText(_translate("Laser", "488nm")) 
self.label_3.setText(_translate("Laser", "515nm")) 
self.label_4.setText(_translate("Laser", "561nm")) 
self.label_5.setText(_translate("Laser", "594nm")) 
self.label_6.setText(_translate("Laser", "638nm")) 
self.label_7.setText(_translate("Laser", "LED")) 
self.pushButton_8.setText(_translate("Laser", "ON")) 


if __name__ == "__main__": 
import sys 
app = QtWidgets.QApplication(sys.argv) 
Laser = QtWidgets.QMainWindow() 
ui = Ui_Laser() 
ui.setupUi(Laser) 
Laser.show() 
sys.exit(app.exec_()) 

laserEmbedded.py 

import random 
import asyncio 
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference 

class Laser(Actor): 
async def handle_message(self, message: Message): 
print("Laser") 
await asyncio.sleep(2) 
print("Unitialised") 
await asyncio.sleep(2) 
print("Initialising") 
await asyncio.sleep(2) 
print("Initialised") 
await asyncio.sleep(2) 
print("Configuring") 
await asyncio.sleep(2) 
print("Configured") 
await asyncio.sleep(2) 
await message.sender.tell(DataMessage(data="Hello World Im a laser!" 
+"\n", sender=self)) 
async def main(): 
# Let's create an instance of a Greeter actor and start it. 
async with Laser() as laser: 
# Then we'll just send it an empty message and wait for a response 
reply : DataMessage = await ask(laser, Message()) 
print(reply.data) 
asyncio.get_event_loop().run_until_complete(main()) 

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


How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value.

2019-09-27 Thread Spencer Du
bel_12") 
self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) 
self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_14.setObjectName("label_14") 
self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) 
self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) 
self.spinBox_6.setMaximum(100) 
self.spinBox_6.setObjectName("spinBox_6") 
self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) 
self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_10.setObjectName("label_10") 
self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) 
self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) 
self.spinBox_3.setMaximum(100) 
self.spinBox_3.setObjectName("spinBox_3") 
self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) 
self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_9.setObjectName("label_9") 
self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) 
self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_8.setObjectName("label_8") 
self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) 
- show quoted text -
self.spinBox.valueChanged['int'].connect(self.hi) 
self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) 
self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) 
self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) 
self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) 
self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) 
self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) 
QtCore.QMetaObject.connectSlotsByName(Laser) 

def hi(self): 
self.spinBox.valueChanged['int'].connect(self.hi) 

def retranslateUi(self, Laser): 
_translate = QtCore.QCoreApplication.translate 
Laser.setWindowTitle(_translate("Laser", "MainWindow")) 
self.label_13.setText(_translate("Laser", " %")) 
self.label_11.setText(_translate("Laser", " %")) 
self.label_12.setText(_translate("Laser", "%")) 
self.label_14.setText(_translate("Laser", " %")) 
self.label_10.setText(_translate("Laser", "%")) 
self.label_9.setText(_translate("Laser", " %")) 
self.label_8.setText(_translate("Laser", "   %")) 
self.pushButton.setText(_translate("Laser", "ON")) 
self.pushButton_2.setText(_translate("Laser", "ON")) 
self.pushButton_3.setText(_translate("Laser", "ON")) 
self.pushButton_4.setText(_translate("Laser", "ON")) 
self.pushButton_5.setText(_translate("Laser", "ON")) 
self.pushButton_6.setText(_translate("Laser", "ON")) 
self.pushButton_7.setText(_translate("Laser", "ON")) 
self.label.setText(_translate("Laser", "445nm")) 
self.label_2.setText(_translate("Laser", "488nm")) 
self.label_3.setText(_translate("Laser", "515nm")) 
self.label_4.setText(_translate("Laser", "561nm")) 
self.label_5.setText(_translate("Laser", "594nm")) 
self.label_6.setText(_translate("Laser", "638nm")) 
self.label_7.setText(_translate("Laser", "LED")) 
self.pushButton_8.setText(_translate("Laser", "ON")) 


if __name__ == "__main__": 
import sys 
app = QtWidgets.QApplication(sys.argv) 
Laser = QtWidgets.QMainWindow() 
ui = Ui_Laser() 
ui.setupUi(Laser) 
Laser.show() 
sys.exit(app.exec_()) 

laserEmbedded.py 

import random 
import asyncio 
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference 

class Laser(Actor): 
async def handle_message(self, message: Message): 
print("Laser") 
await asyncio.sleep(2) 
print("Unitialised") 
await asyncio.sleep(2) 
print("Initialising") 
await asyncio.sleep(2) 
print("Initialised") 
await asyncio.sleep(2) 
print("Configuring") 
await asyncio.sleep(2) 
print("Configured") 
await asyncio.sleep(2) 
await message.sender.tell(DataMessage(data="Hello World Im a laser!" 
+"\n", sender=self)) 
async def main(): 
# Let's create an instance of a Greeter actor and start it. 
async with Laser() as laser: 
# Then we'll just send it an empty message and wait for a response 
reply : DataMessage = await ask(laser, Message()) 
print(reply.data) 
asyncio.get_event_loop().run_until_complete(main()) 

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


How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value.

2019-09-27 Thread Spencer Du
bel_12") 
self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) 
self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_14.setObjectName("label_14") 
self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) 
self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) 
self.spinBox_6.setMaximum(100) 
self.spinBox_6.setObjectName("spinBox_6") 
self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) 
self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_10.setObjectName("label_10") 
self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) 
self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) 
self.spinBox_3.setMaximum(100) 
self.spinBox_3.setObjectName("spinBox_3") 
self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) 
self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_9.setObjectName("label_9") 
self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) 
self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) 
self.label_8.setObjectName("label_8") 
self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) 
- show quoted text -
self.spinBox.valueChanged['int'].connect(self.hi) 
self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) 
self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) 
self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) 
self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) 
self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) 
self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) 
QtCore.QMetaObject.connectSlotsByName(Laser) 

def hi(self): 
self.spinBox.valueChanged['int'].connect(self.hi) 

def retranslateUi(self, Laser): 
_translate = QtCore.QCoreApplication.translate 
Laser.setWindowTitle(_translate("Laser", "MainWindow")) 
self.label_13.setText(_translate("Laser", " %")) 
self.label_11.setText(_translate("Laser", " %")) 
self.label_12.setText(_translate("Laser", "%")) 
self.label_14.setText(_translate("Laser", " %")) 
self.label_10.setText(_translate("Laser", "%")) 
self.label_9.setText(_translate("Laser", " %")) 
self.label_8.setText(_translate("Laser", "   %")) 
self.pushButton.setText(_translate("Laser", "ON")) 
self.pushButton_2.setText(_translate("Laser", "ON")) 
self.pushButton_3.setText(_translate("Laser", "ON")) 
self.pushButton_4.setText(_translate("Laser", "ON")) 
self.pushButton_5.setText(_translate("Laser", "ON")) 
self.pushButton_6.setText(_translate("Laser", "ON")) 
self.pushButton_7.setText(_translate("Laser", "ON")) 
self.label.setText(_translate("Laser", "445nm")) 
self.label_2.setText(_translate("Laser", "488nm")) 
self.label_3.setText(_translate("Laser", "515nm")) 
self.label_4.setText(_translate("Laser", "561nm")) 
self.label_5.setText(_translate("Laser", "594nm")) 
self.label_6.setText(_translate("Laser", "638nm")) 
self.label_7.setText(_translate("Laser", "LED")) 
self.pushButton_8.setText(_translate("Laser", "ON")) 


if __name__ == "__main__": 
import sys 
app = QtWidgets.QApplication(sys.argv) 
Laser = QtWidgets.QMainWindow() 
ui = Ui_Laser() 
ui.setupUi(Laser) 
Laser.show() 
sys.exit(app.exec_()) 

laserEmbedded.py 

import random 
import asyncio 
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference 

class Laser(Actor): 
async def handle_message(self, message: Message): 
print("Laser") 
await asyncio.sleep(2) 
print("Unitialised") 
await asyncio.sleep(2) 
print("Initialising") 
await asyncio.sleep(2) 
print("Initialised") 
await asyncio.sleep(2) 
print("Configuring") 
await asyncio.sleep(2) 
print("Configured") 
await asyncio.sleep(2) 
await message.sender.tell(DataMessage(data="Hello World Im a laser!" 
+"\n", sender=self)) 
async def main(): 
# Let's create an instance of a Greeter actor and start it. 
async with Laser() as laser: 
# Then we'll just send it an empty message and wait for a response 
reply : DataMessage = await ask(laser, Message()) 
print(reply.data) 
asyncio.get_event_loop().run_until_complete(main()) 

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


How can I set the value of the textedit box and slider of ui with the value from a config file when it has been created?

2019-10-01 Thread Spencer Du
Hi
How can I set the value of the textedit box and slider of ui with the value 
from a config file when it has been created meaning if a configuration file 
exists then set the UI with value from the config file otherwise load ui with 
nothing set to any value.
ui.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QSlider, QLineEdit, 
QPushButton
from PyQt5.QtCore import Qt
import myconfig
config=myconfig.Config()
import os
import configparser

ui.py

class Example(QMainWindow):

def __init__(self):
super().__init__()

self.textbox = QLineEdit(self)
self.textbox.move(20, 25)
self.textbox.resize(60,30)

button = QPushButton('Button', self)
button.setToolTip('This is an example button')
button.clicked.connect(self.printValue)
button.move(100,25)

self.mySlider = QSlider(Qt.Horizontal, self)
self.mySlider.setGeometry(30, 140, 200, 30)
self.mySlider.setMinimum(0)
self.mySlider.setMaximum(100)

# self.textbox.setText(str(config.getminValue()))
# textboxValue = self.textbox.text()
# self.mySlider.setValue(int(textboxValue) + 30)
   
self.setGeometry(50,50,320,200)
self.setWindowTitle("Checkbox Example")
self.show()

#def changeValue(self, value):
#print(value)

def printValue(self):
if not os.path.exists('445nm.ini'):
textboxValue = self.textbox.text()
print(textboxValue) 
f = open('445nm.ini', 'w+')
config = configparser.RawConfigParser()
config.add_section('445nm')
config.set('445nm', 'intensity', textboxValue)
config.write(f)
if self.textbox.text() == "":
self.mySlider.setValue(0)
else:
self.mySlider.setValue(int(textboxValue))
else:
self.mySlider.setValue(config.get('445nm', 'intensity'))

# if self.textbox.text() == "":
# self.mySlider.setValue(0)
# else:
# self.mySlider.setValue(config.get('445nm', 'intensity'))

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

config.py

[445nm]
intensity = 4

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


How execute at least two python files at once when imported?

2019-11-05 Thread Spencer Du
ort asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class FW(Actor):
async def handle_message(self, message: Message):
print("Filter wheel")
await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a filter 
wheel!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it. 
async with FW() as fw:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(fw, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

motorized_galvo_wheelEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class Motorized_galvo_wheel(Actor):
async def handle_message(self, message: Message):
print("Motorized galvo wheel")
await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a motorized 
galvo wheel!" + "\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it. 
async with Motorized_galvo_wheel() as motorized_galvo_wheel:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(motorized_galvo_wheel, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main())

stagesEmbedded.py
import random
import asyncio
from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference

class Stages(Actor):
async def handle_message(self, message: Message):
print("Stages")
await asyncio.sleep(2)
print("Unitialised")
await asyncio.sleep(2)
print("Initialising")
await asyncio.sleep(2)
print("Initialised")
await asyncio.sleep(2)
print("Configuring")
await asyncio.sleep(2)
print("Configured")
await asyncio.sleep(2)
await message.sender.tell(DataMessage(data="Hello World Im a stage!" + 
"\n", sender=self))
async def main():
# Let's create an instance of a Greeter actor and start it. 
async with Stages() as stages:
# Then we'll just send it an empty message and wait for a response
reply : DataMessage = await ask(stages, Message())
print(reply.data)
asyncio.get_event_loop().run_until_complete(main()) 

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


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
On Wednesday, 6 November 2019 09:05:42 UTC+1, Christian Gollwitzer  wrote:
> Am 06.11.19 um 03:59 schrieb Dennis Lee Bieber:
> > On Tue, 5 Nov 2019 10:33:20 -0800 (PST), Spencer Du
> >  declaimed the following:
> > 
> >> Hi
> >>
> >> I want to execute at least two python files at once when imported but I 
> >> dont know how to do this. Currently I can only import each file one after 
> >> another but what i want is each file to be imported at the same time. Can 
> >> you help me write the code for this? embedded.py is the main file to 
> >> execute.
> > 
> > 
> > Short answer: you don't.
> > 
> > When you import a module, the code for that module is parsed and
> > anything that is module level executable statement is done (note: "def" is
> > an executable statement -- it creates a function object contained the
> > parsed body and binds it to the provided name). When the parser gets to the
> > end of the module, it returns to the parent level and the next statement is
> > executed.
> > 
> > Unless you use 1) threads; 2) subprocesses; or 3) multiprocess a Python
> > program only has one line of control, and that control is sequential.
> 
> Since some of these example programs use asyncio, there is a 4th method. 
> You convert all the programs to use asyncio, remove the event loop from 
> these programs, i.e. remove the
> 
> asyncio.get_event_loop().run_until_complete(main())
> 
> from the individual programs, and then you run a single event loop in 
> your main program. Something like
> 
>loop = asyncio.get_event_loop()
>loop.run_until_complete(asyncio.gather(laserembeded.main(),
> camerasembedded.main()))
> 
> 
>   Christian

Ok I am interested in knowing how i can do it via either 1) threads; 2) 
subprocesses; or 3) multiprocess; depending on what you think is the best 
method.

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


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
On Tuesday, 5 November 2019 19:37:32 UTC+1, Karsten Hilbert  wrote:
> > I want to execute at least two python files at once when imported but I 
> > dont know how to do this.
> > Currently I can only import each file one after another but what i want is 
> > each file to be imported
> > at the same time.
> 
> Can you explain why that seems necessary ?
> 
> Karsten

These are modules/files which are independent of each other and have no 
dependencies with one another. Because of this I want to run modules all at 
once.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
On Tuesday, 5 November 2019 21:05:02 UTC+1, Terry Reedy  wrote:
> On 11/5/2019 1:33 PM, Spencer Du wrote:
> 
> > I want to execute at least two python files at once when imported but I 
> > dont know how to do this. Currently I can only import each file one after 
> > another but what i want is each file to be imported at the same time. Can 
> > you help me write the code for this? embedded.py is the main file to 
> > execute.
> 
> [snip about 150 lines of example code]
> 
> We don't understand what you mean other than something impossible.  Your 
> example code iswa   ytoo long.  It should be the minimum 
> needed to illustrate the idea.
> 
> -- 
> Terry Jan Reedy

These are modules/files which are independent of each other and have no 
dependencies with one another. Because of this I want to run modules all at 
once.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
On Tuesday, 5 November 2019 19:41:59 UTC+1, Bob Gailer  wrote:
> On Nov 5, 2019 1:35 PM, "Spencer Du"  wrote:
> >
> > Hi
> >
> > I want to execute at least two python files at once when imported but I
> dont know how to do this. Currently I can only import each file one after
> another but what i want is each file to be imported at the same time. Can
> you help me write the code for this?
> 
> Please explain what you mean by "imported at the same time". As far as I
> know that is physically impossible.

These are modules/files which are independent of each other and have no 
dependencies with one another. Because of this I want to run modules all at 
once.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
On Tuesday, 5 November 2019 19:44:46 UTC+1, Rhodri James  wrote:
> On 05/11/2019 18:33, Spencer Du wrote:
> > I want to execute at least two python files at once when imported but
> > I dont know how to do this. Currently I can only import each file one
> > after another but what i want is each file to be imported at the same
> > time.
> 
> That is a very odd requirement.  Why would you want to do this?  What 
> exactly does "at the same time" mean here?
> 
> -- 
> Rhodri James *-* Kynesim Ltd

These are modules/files which are independent of each other and have no 
dependencies with one another. Because of this I want to run modules all at 
once.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
Why is importing modules in parallel bad?

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


Re: How execute at least two python files at once when imported?

2019-11-06 Thread Spencer Du
Hi

Sorry if I haven't stated my requirements clearly.

I just wanted a way to import at least two python files in parallel and I 
wanted to know how this can be done or a reason why its bad as stated in 
another post.

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


Re: What is "self"?

2005-09-23 Thread Michael Spencer
Ron Adam wrote:
> Erik Max Francis wrote:
> 
>>Ron Adam wrote:
>>
>>
>>>When you call a method of an instance, Python translates it to...
>>>
>>> leader.set_name(leader, "John")
>>
>>
>>It actually translates it to
>>
>>Person.set_name(leader, "John")
>>
> 
> 
> I thought that I might have missed something there.
> 
> Is there a paper on how python accesses and stores instance data and 
> methods?  I googled but couldn't find anything that addressed this 
> particular question.
> 
>  >>> class a(object):
> ...def x(self):
> ...   print 'x'
> ...
>  >>> b = a()
>  >>> b
> <__main__.a object at 0x009D1890>
>  >>> b.x
> >
> 
> So what exactly is a bound method object?  Does it possibly translates 
> to something like the following?
> 
>  def x(*args, **kwds):
>  self = ?
>  return __class__.self(self, *args, **kwds)
> 
> Cheers,
> Ron
> 
> 
> 
> 
> 

All is explained at:
http://users.rcn.com/python/download/Descriptor.htm#functions-and-methods
and further at:
http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf

"For objects, the machinery is in object.__getattribute__ which transforms b.x 
into type(b).__dict__['x'].__get__(b, type(b))."

What follows is my interpretation - hope it's correct:

# what exactly is a bound method object?
# Illustrate b.f => type(b).__dict__['x'].__get__(b, type(b))

  >>> class B(object):
  ... def f(self, x):
  ... return x or 42
  ...
  >>> b = B()
  >>> type(b).__dict__['f']
# a plain old function
  >>> _.__get__(b, type(b))   # invoke the descriptor protocol
  # to make a bound method
  >
  >>>

You don't have to use object.__getattribute__ to get a bound method.  Nor does 
the function have to be in the class dictionary.  You can just call any 
function 
descriptor yourself:

  >>> def g(self, y):
  ... return self.f(y)
  ...
  >>> boundg = g.__get__(b)  # bind to B instance
  >>> boundg
  >
  >>> boundg(0)
  42
  >>>

Looked at this way, function.__get__ just does partial function application 
(aka 
currying).

  >>> def f(x, y):
  ... return x+y
  ...
  >>> add42 = f.__get__(42)
  >>> add42
  
  >>> add42(1)
  43


Michael

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


Re: Wrapping classes

2005-09-23 Thread Michael Spencer
Jeremy Sanders wrote:
> Colin J. Williams wrote:
> 
> 
>>Could you not have functions a and b each of which returns a NumArray
>>instance?
>>
>>Your expression would then be something like a(..)+2*b(..).
> 
> 
> The user enters the expression (yes - I'm aware of the possible security
> issues), as it is a scientific application. I don't think they'd like to
> put () after each variable name.
> 
> I could always munge the expression after the user enters it, of course.
> 
> Jeremy
> 
Alternatively, you could build your own expression calculator, and initialize 
the objects if necessary as they are evaluated.  If you are happy with Python 
syntax for your expressiones then the stdlib compiler package is helpful.  The 
example below is not tested beyond what you see.  It's a bit verbose, but most 
of the code is boilerplate.

  >>> a = 3
  >>> b = 4
  >>> calc('a * b')
  using a
  using b
  12
  >>> calc('a * b ** (b - a) * "a"')
  using a
  using b
  using b
  using a
  ''
   >>> calc("0 and a or b")
  using b
  4
  >>> calc("1 and a or b")
  using a
  3
  >>> calc("1 and a or c")
  using a
  3
  >>> calc("0 and a or c")
  Undefined symbol: c
  >>>


HTH, Michael

-

import compiler


class CalcError(Exception):
 def __init__(self,error,descr = None,node = None):
 self.error = error
 self.descr = descr
 self.node = node

 def __repr__(self):
 return "%s: %s" % (self.error, self.descr)
 __str__ = __repr__


class LazyCalc(object):

 def __init__(self, namespace):
 self._cache = {} # dispatch table
 self.context = namespace

 def visit(self, node,**kw):
 cls = node.__class__
 meth = self._cache.setdefault(cls,
 getattr(self,'visit'+cls.__name__,self.default))
 return meth(node, **kw)

 def visitExpression(self, node, **kw):
 return self.visit(node.node)


 # Binary Ops
 def visitAdd(self,node,**kw):
 return self.visit(node.left) + self.visit(node.right)
 def visitDiv(self,node,**kw):
 return self.visit(node.left) / self.visit(node.right)
 def visitFloorDiv(self,node,**kw):
 return self.visit(node.left) // self.visit(node.right)
 def visitLeftShift(self,node,**kw):
 return self.visit(node.left) << self.visit(node.right)
 def visitMod(self,node,**kw):
 return self.visit(node.left) % self.visit(node.right)
 def visitMul(self,node,**kw):
 return self.visit(node.left) * self.visit(node.right)
 def visitPower(self,node,**kw):
 return self.visit(node.left) ** self.visit(node.right)
 def visitRightShift(self,node,**kw):
 return self.visit(node.left) >> self.visit(node.right)
 def visitSub(self,node,**kw):
 return self.visit(node.left) - self.visit(node.right)

 # Unary ops
 def visitNot(self,node,*kw):
 return not self.visit(node.expr)
 def visitUnarySub(self,node,*kw):
 return -self.visit(node.expr)
 def visitInvert(self,node,*kw):
 return ~self.visit(node.expr)
 def visitUnaryAdd(self,node,*kw):
 return +self.visit(node.expr)

 # Flow Control
 def visitAnd(self,node,**kw):
 for arg in node.nodes:
 val = self.visit(arg)
 if not val:
 return val
 return val
 def visitOr(self,node,**kw):
 for arg in node.nodes:
 val = self.visit(arg)
 if val:
 return val
 return val

 # Logical Ops
 def visitBitand(self,node,**kw):
 return reduce(lambda a,b: a & b,[self.visit(arg) for arg in 
node.nodes])
 def visitBitor(self,node,**kw):
 return reduce(lambda a,b: a | b,[self.visit(arg) for arg in 
node.nodes])
 def visitBitxor(self,node,**kw):
 return reduce(lambda a,b: a ^ b,[self.visit(arg) for arg in 
node.nodes])
 def visitCompare(self,node,**kw):
 comparisons = {
 "<": operator.lt, # strictly less than
 "<=": operator.le,# less than or equal
 ">": operator.gt, # strictly greater than
 ">=": operator.ge, # greater than or equal
 "==": operator.eq, # equal
 "!=": operator.ne, # not equal
 "<>": operator.ne, # not equal
 "is": operator.is_, # object identity
 "is not": operator.is_not # negated object identity
 }
 obj = self.visit(node.expr)
 for op, compnode in node.ops:
 compobj = self.visit(compnode)
 if not comparisons[op](obj, compobj):
 return False
 obj  = compobj
 return True


 # Values
 def visitCallFunc(self,node,**kw):
 raise CalcError("Functions not supported", node.node)

 def visitName(self, node, **kw):
 """LazyEvaluation"""
 name = node.name
 try:
 val = eval(name, self.context)
 except

Re: What is "self"?

2005-09-27 Thread Michael Spencer
Ron Adam wrote:
> What I've noticed is you can block the visibility of a class attribute, 
> which include methods, by inserting an object in the instance with the 
> same name.
> 
[snip example of this behavior]

Yes, that's true for "non-data descriptors" (see last two bullets below)

Raymond Hettinger [http://users.rcn.com/python/download/Descriptor.htm]
 >
 > The important points to remember are:
 >
 > * descriptors are invoked by the __getattribute__ method
 > * overriding __getattribute__ prevents automatic descriptor calls
 > * __getattribute__ is only available with new style classes and objects
 > * object.__getattribute__ and type.__getattribute__ make different calls 
to __get__.
 > * data descriptors always override instance dictionaries.
 > * non-data descriptors may be overridden by instance dictionaries.

Michael

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


Re: Silly function call lookup stuff?

2005-09-27 Thread Michael Spencer
Lucas Lemmens wrote:
> Dear pythonians,
> 
> I've been reading/thinking about the famous function call speedup 
> trick where you use a function in the local context to represent 
> a "remoter" function to speed up the 'function lookup'.
> 
> "This is especially usefull in a loop where you call the function a 
> zillion time" they say.
> 
> I think this is very odd behavior. 
> 
> Why isn't the result of the first function-lookup cached so that following
> function calls don't need to do the function-lookup at all?
> 
I guess because the function name may be re-bound between loop iterations.  Are 
there good applications of this?  I don't know.

> And if the context changes (an import-statement say) reset the
> cached 'function-lookups'.

In general an object doesn't know what names are bound to it and there are many 
ways besides an import statement of binding/re-binding, so "if the context 
changes" is easier said than done.

> 
> This way any function would only need to be looked up once.
> 
> L.
> 
Would you apply this optimization to all lookups in outer scopes, or just 
callables?  Why? ;-)

Michael

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


Re: grouping array

2005-09-29 Thread Michael Spencer
[EMAIL PROTECTED] wrote:
> hi if I have an array
> 
> say x = [[2,2,0,0,1,1],
>  [1,1,0,0,1,1],
>  [1,1,0,0,1,1]]
> I basically want to group regions that are non zero like I want to get
> the coordinates of non zero regions..as (x1,y1,x2,y2)
> [(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
> right(x2,y2) corners of each group.hope i am clear.
> 
> Thanks
> 
How about this:


def getregions(grid):
 """Yield lists of adjancent points, not necessarily rectangular"""
 adj = [(-1,0),(+1,0),(0,-1),(0,+1)] # horizontal and vertical adjacencies
 # could add diagonals

 points = set((y,x) for y, row in enumerate(grid)
for x, cell in enumerate(row)
if cell)

 while points:# set of (y,x) non-zero points
 region = [points.pop()]  # start a new region with any remaining point
 ptr = 0
 while ptr < len(region):
 y, x = region[ptr]
 adjpoints = set((y + j, x + i) for j, i in adj)
 adjpoints &= points # keep only the non-zero, unseen points
 points -= adjpoints # remove these adjancencies from points
 region.extend(adjpoints) # add them to the region
 ptr += 1
 yield region

def getregioncoords(grid):
 """Get top left and bottom right of *rectangular* regions"""
 regions = getregions(grid)
 return [(reg[0], reg[-1]) for reg in regions if reg.sort() or True]


  >>> x = [[2,2,0,0,1,1],
  ...  [1,1,0,0,1,1],
  ...  [1,1,0,0,1,1]]
  ...
  ...
  >>> getregioncoords(x)
  [((0, 0), (2, 1)), ((0, 4), (2, 5))]
   >>> x = [[1,0,1,0,1]]
  >>> getregioncoords(x)
  [((0, 0), (0, 0)), ((0, 2), (0, 2)), ((0, 4), (0, 4))]
  >>> x = [[random.choice([0,1,2]) for x in range(6)] for y in range(6)]
  >>> pprint.pprint(x)
  [[1, 1, 2, 1, 2, 0],
   [2, 0, 0, 2, 0, 1],
   [1, 2, 2, 0, 2, 0],
   [0, 1, 0, 0, 0, 0],
   [2, 0, 0, 1, 1, 0],
   [2, 2, 2, 0, 1, 0]]
  >>> print "\n".join(str(reg) for reg in getregions(x))
  [(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (2, 0), (1, 3), (0, 4), (2, 1), (3, 
1), (2, 2)]
  [(5, 4), (4, 4), (4, 3)]
  [(5, 0), (5, 1), (4, 0), (5, 2)]
  [(1, 5)]
  [(2, 4)]
  >>>

Unfortunately, it's rather slow.  This one is much faster, using just one data 
structure

def getregions2(grid):
 """Yield lists of adjancent points, not necessarily rectangular"""
 adj = [(-1,0),(+1,0),(0,-1),(0,+1)] # horizontal and vertical adjacencies
 # could add diagonals
 rows = len(grid)
 cols = len(grid[0])
 griddata = []
 for row in grid:
 griddata.extend(row)
 for y in xrange(rows):
 ybase = y * cols
 for x in xrange(cols):
 if griddata[ybase + x]:
 griddata[ybase + x] = 0
 region = [(y, x)]
 append = region.append
 ptr = 0
 while ptr < len(region):
 y1, x1 = region[ptr]
 for j, i in adj:
 y2, x2 = y1 + j, x1 + i
 if y2 < 0 or y2 == rows: continue
 if x2 < 0 or x2 == cols: continue
 if griddata[y2 * cols + x2]:
 append((y2, x2))
 griddata[y2 * cols + x2] = 0
 ptr += 1
 yield region



Michael

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


Re: grouping array

2005-09-30 Thread Michael Spencer
[EMAIL PROTECTED] wrote:
> fredrick's solutions seems to be more closer to what I was looking
> for.But I am still not sure if that could be done without the use of
> Image module.

What do you mean by "closer to what I was looking
for"?  For the single test case you provided:

 > say x = [[2,2,0,0,1,1],
 >  [1,1,0,0,1,1],
 >  [1,1,0,0,1,1]]
 > I basically want to group regions that are non zero like I want to get
 > the coordinates of non zero regions..as (x1,y1,x2,y2)
 > [(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
 > right(x2,y2) corners of each group.hope i am clear.
 >


my solution provides the correct output:

   >>> x = [[2,2,0,0,1,1],
   ...  [1,1,0,0,1,1],
   ...  [1,1,0,0,1,1]]
   ...
   ...
   >>> getregioncoords(x)
   [((0, 0), (2, 1)), ((0, 4), (2, 5))]

* except that the points aren't flattened.  If that's important to you, rewrite 
getregioncoords as follows:

def getregioncoords(grid):
 """Get top left and bottom right of *rectangular* regions"""
 regions = getregions(grid)
 return [reg[0]+reg[-1] for reg in regions if reg.sort() or True]

  >>> getregioncoords(x)
  [(0, 0, 2, 1), (0, 4, 2, 5)]
  >>>


> Also in your solution I cannot follow this

I broke the solution into two parts:

1) the getregions generator yields a list of all the contiguous regions.  The 
output below is the lists of coordinates that are contiguous non-zero cells in 
the grid.

 > [[1, 1, 2, 1, 2, 0],
 >[2, 0, 0, 2, 0, 1],
 >[1, 2, 2, 0, 2, 0],
 >[0, 1, 0, 0, 0, 0],
 >[2, 0, 0, 1, 1, 0],
 >[2, 2, 2, 0, 1, 0]]
 >   >>> print "\n".join(str(reg) for reg in getregions(x))
 >   [(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (2, 0), (1, 3), (0, 4), (2,
 > 1), (3,
 > 1), (2, 2)]
 >   [(5, 4), (4, 4), (4, 3)]
 >   [(5, 0), (5, 1), (4, 0), (5, 2)]
 >   [(1, 5)]
 >   [(2, 4)]


2) If the regions are rectangular, the getregioncoords functions returns the 
coordinates of the top-left and bottom-right points.  You did not answer the 
previous post which asked what to do if the regions were not rectangular.



HTH

Michael



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


Re: Feature Proposal: Sequence .join method

2005-09-30 Thread Michael Spencer
Terry Reedy wrote:
> "David Murmann" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> 
>>>def join(sep, seq):
>>>return reduce(lambda x, y: x + sep + y, seq, type(sep)())
>>
>>damn, i wanted too much. Proper implementation:
>>
>>def join(sep, seq):
>>if len(seq):
>>return reduce(lambda x, y: x + sep + y, seq)
>>return type(sep)()
>>
>>but still short enough
> 
> 
> For general use, this is both too general and not general enough.
> 
> If len(seq) exists then seq is probably reiterable, in which case it may be 
> possible to determine the output length and preallocate to make the process 
> O(n) instead of O(n**2).  I believe str.join does this.  A user written 
> join for lists could also.  A tuple function could make a list first and 
> then tuple(it) at the end.
> 
> If seq is a general (non-empty) iterable, len(seq) may raise an exception 
> even though the reduce would work fine.
> 
> Terry J. Reedy
> 
> 
> 
For the general iterable case, you could have something like this:

  >>> def interleave(sep, iterable):
  ... it = iter(iterable)
  ... next = it.next()
  ... try:
  ... while 1:
  ... item = next
  ... next = it.next()
  ... yield item
  ... yield sep
  ... except StopIteration:
  ... yield item
  ...
  >>> list(interleave(100,range(10)))
  [0, 100, 1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 6, 100, 7, 100, 8, 100, 9]
  >>>

but I can't think of a use for it ;-)

Michael

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


Re: SPE IDE for Python

2005-11-08 Thread Chris Spencer
py wrote:
> Anyone here use SPE (http://www.stani.be/python/spe/blog/). ...the IDE?
> 
> Also, anyone know if it supports CVS or has a plugin for CVS?  If not,
> what do you use to get your code into CVS (via an IDE preferably)?

I used to use SPE quite frequently, until it went nearly unmaintained 
for most of late last year. I switched to the PyDev plugin for Eclipse, 
which comes with standard support for CVS (there's also an SVN plugin). 
SPE development looks to be back in full swing, which in good, since 
it's one of the best pure Python IDEs around.

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


Re: Recompile AST?

2005-11-10 Thread Chris Spencer
Leif K-Brooks wrote:
> [EMAIL PROTECTED] wrote:
> 
>>Is it possible to recompile the AST generated by compiler.parse, back
>>into code or an executable code object?
> 
> 
> Into a bytecode object:
> 
>  >>> from compiler.pycodegen import ModuleCodeGenerator
>  >>> from compiler.misc import set_filename
>  >>> from compiler import parse
>  >>>
>  >>> tree = parse('foo = 42')
>  >>> set_filename('', tree)
>  >>> code = ModuleCodeGenerator(tree).getCode()
>  >>> exec code
>  >>> foo
>  42
> 
> Into Python source code: .

Thanks, that's exactly what I was looking for. I had almost figured that 
out, but I had overlooked the need for set_filename.

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


Re: Dynamically Update Class Definitions?

2005-11-11 Thread Chris Spencer
Alex Martelli wrote:
> <[EMAIL PROTECTED]> wrote:
> 
> 
>>Is there a way to loop through all instantiated objects and update
>>their classes when a source file changes? I know about Michael Hudson's
>>method
>>(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164), but
>>you have to modify all your classes to subclass AutoReloader. Is there
>>something less intrusive (visitor pattern?) that you can use like
>>update(old_class, new_class) to automagically do the work?
> 
> 
> If you're in no hurry, you COULD loop over all of gc.get_objects(),
> identify all those which are instances of old_class and "somehow" change
> their classes to new_class -- of course, x.__class__ = new_class may
> well not be sufficient, in which case you'll have to pass to update a
> callable to do the instance-per-instance job.

Couldn't I just loop over gc.get_referrers(cls), checking for instances 
of the class object? Since class instances refer to their class, the gc 
seems to be doing the exact same thing as Hudson's fancy metaclass. Or 
am I missing something?

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


Re: Dynamically Update Class Definitions?

2005-11-11 Thread Chris Spencer
Chris Spencer wrote:
> Alex Martelli wrote:

>> If you're in no hurry, you COULD loop over all of gc.get_objects(),
>> identify all those which are instances of old_class and "somehow" change
>> their classes to new_class -- of course, x.__class__ = new_class may
>> well not be sufficient, in which case you'll have to pass to update a
>> callable to do the instance-per-instance job.
> 
> 
> Couldn't I just loop over gc.get_referrers(cls), checking for instances 
> of the class object? Since class instances refer to their class, the gc 
> seems to be doing the exact same thing as Hudson's fancy metaclass. Or 
> am I missing something?
> 
> Chris

In fact, the following code seems to work, and doesn't require any 
modification to new-style class based code:

import os
import time
import threading
import inspect
import gc
import copy

class ModuleUpdater(object):
 '''
 This will constantly check a module's source file for updates, reload
 if any are detected, and update all class instances.
 Only works for new-style classes.

 Use like:
 checker = ModuleUpdater(module=mymod)
 checker.start()
 '''
 def __init__(self, module):
 self.module = module
 self.lastloaded = time.time()
 self.running = 0
 self.t = None
 def __call__(self):
 self.running = 1
 while self.running:
 self.check()
 time.sleep(1)
 def check(self):
 lastmodified = os.stat(inspect.getsourcefile(self.module))[8]
 if lastmodified > self.lastloaded:
 print 'update detected for',self.module.__name__
 oldmod = copy.copy(self.module.__dict__)
 newmod = reload(self.module)
 try:
 for name,obj in oldmod.items():
 if isinstance(obj,type) and name in newmod.__dict__:
 newobj = newmod.__dict__[name]
 referrers = gc.get_referrers(obj)
 for referrer in referrers:
 if isinstance(referrer,obj):
 # update old class instances to use new 
class
 referrer.__class__ = newobj
 print 'update loaded for',self.module.__name__
 except Exception, e:
 print 'unable to load update for %s: %s' % 
(self.module.__name__, str(e))
 self.lastloaded = lastmodified
 return 1
 return 0
 def start(self):
 t = threading.Thread(target=self)
 t.setDaemon(1)
 t.start()
 self.t = t
 return t
 def stop(self, blocking=0):
 self.running = 0
 if blocking:
 self.t.join()

if __name__ == '__main__':

 import testmod # any module containing class Bar with method meth
 uc = ModuleUpdater(testmod)

 uc.start()

 b=testmod.Bar(1)
 while 1: # meanwhile, modify the source to testmod.py
 time.sleep(1)
 print b.meth()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically Update Class Definitions?

2005-11-12 Thread Chris Spencer
Jean-Paul Calderone wrote:

> There are lots of cases where you cannot rebind the __class__ 
> attribute.  For a comprehensive treatment of this idea (but still not a 
> completely functionality implementation), take a look at 
> .
>   
> On another note, the usage of threads in this code is totally insane and 
> unsafe.  Even for strictly development purposes, I would expect it to 
> introduce so many non-deterministic and undebuggable failures as to make 
> it cost more time than it saves.  You really want to stop the rest of 
> the program, then update things, then let everything get going again.

I used a thread to quickly detect changes in the source code, but you're 
absolutely right. In any non-toy application you'll definitely need 
control over when the upgrade process occurs. Thanks for the help.

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


Re: Need advice on subclassing code

2005-11-15 Thread Michael Spencer
Kent Johnson wrote:
> Rusty Shackleford wrote:
>> ...
>> C_1_1 and C_1_2 share a common C ancestor, and in practice may be
>> identical, but theoretically, could have the same function name with two
>> different implementations underneath.
>>
>> ...
> 
> How are you instantiating the correct class? You should be able to provide a 
> default behaviour. For example if the classes are all defined in module C you 
> could have a factory like this:
> 
> import C
> def makeC(x, y):
>   subtype = 'C_%d_%d' % (x, y)
>   cls = getattr(C, subtype, C.C)
>   return cls(x, y)
> 
> Then in module C just define the subtypes you need to specialize; all other 
> values of x and y will get the base class C.C.
> 
> Kent

Or, if you actually want different classes for each set of parameters (say for 
debugging or introspection), you could compose the default ones on the fly:


import C
def makeC(x, y):
   subtype = 'C_%d_%d' % (x, y)
   cls = getattr(C, subtype, None)
   if not cls:
 # No specialized class found, so compose a default
 # This requires C.C to be a new-style class
 cls = type(subtype, (C.C,), {"__autogenerated__": True})
   return cls(x, y)

Michael



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


Re: best cumulative sum

2005-11-22 Thread Michael Spencer
David Isaac wrote:
  for a solution when these are available.
> Something like:
> def cumreduce(func, seq, init = None):
> """Return list of cumulative reductions.
> 
> 
This can be written more concisely as a generator:

  >>> import operator
  >>> def ireduce(func, iterable, init):
  ... for i in iterable:
  ... init = func(init, i)
  ... yield init
  ...
  >>> list(ireduce(operator.mul, range(1,5),init=1))
  [1, 2, 6, 24]
  >>>

Michael


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


Re: aligning a set of word substrings to sentence

2005-12-01 Thread Michael Spencer
Steven Bethard wrote:
> I've got a list of word substrings (the "tokens") which I need to align 
> to a string of text (the "sentence").  The sentence is basically the 
> concatenation of the token list, with spaces sometimes inserted beetween 
> tokens.  I need to determine the start and end offsets of each token in 
> the sentence.  For example::
> 
> py> tokens = ['She', "'s", 'gon', 'na', 'write', 'a', 'book', '?']
> py> text = '''\
> ... She's gonna write
> ... a book?'''
> py> list(offsets(tokens, text))
> [(0, 3), (3, 5), (6, 9), (9, 11), (12, 17), (18, 19), (20, 24), (24, 25)]
> 
> Here's my current definition of the offsets function::
> 
> py> def offsets(tokens, text):
> ... start = 0
> ... for token in tokens:
> ... while text[start].isspace():
> ... start += 1
> ... text_token = text[start:start+len(token)]
> ... assert text_token == token, (text_token, token)
> ... yield start, start + len(token)
> ... start += len(token)
> ...
> 
> I feel like there should be a simpler solution (maybe with the re 
> module?) but I can't figure one out.  Any suggestions?
> 
> STeVe

Hi Steve:

Any reason you can't simply use str.find in your offsets function?

  >>> def offsets(tokens, text):
  ... ptr = 0
  ... for token in tokens:
  ... fpos = text.find(token, ptr)
  ... if fpos != -1:
  ... end = fpos + len(token)
  ... yield (fpos, end)
  ... ptr = end
  ...
  >>> list(offsets(tokens, text))
  [(0, 3), (3, 5), (6, 9), (9, 11), (12, 17), (18, 19), (20, 24), (24, 25)]
  >>>

and then, for an entry in the wacky category, a difflib solution:

  >>> def offsets(tokens, text):
  ... from difflib import SequenceMatcher
  ... s = SequenceMatcher(None, text, "\t".join(tokens))
  ... for start, _, length in s.get_matching_blocks():
  ... if length:
  ... yield start, start + length
  ...
  >>> list(offsets(tokens, text))
  [(0, 3), (3, 5), (6, 9), (9, 11), (12, 17), (18, 19), (20, 24), (24, 25)]
  >>>

cheers
Michael

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


Re: Checking length of each argument - seems like I'm fighting Python

2005-12-03 Thread Michael Spencer
Brendan wrote:
...
> 
> class Things(Object):
> def __init__(self, x, y, z):
> #assert that x, y, and z have the same length
> 
> But I can't figure out a _simple_ way to check the arguments have the
> same length, since len(scalar) throws an exception.  The only ways
> around this I've found so far are
> 
...
> 
> b) use a separate 'Thing' object, and make the 'Things' initializer
> work only with Thing objects.  This seems like way too much structure
> to me.
> 

Yes, but depending on what you want to do with Things, it might indeed make 
sense to convert its arguments to a common sequence type, say a list.  safelist 
is barely more complex than sLen, and may simplify downstream steps.

def safelist(obj):
 """Construct a list from any object."""
 if obj is None:
 return []
 if isinstance(obj, (basestring, int)):
 return [obj]
 if isinstance(obj, list):
 return obj
 try:
 return list(obj)
 except TypeError:
 return [obj]

class Things(object):
 def __init__(self, *args):
 self.args = map(safelist, args)
 assert len(set(len(obj) for obj in self.args)) == 1
 def __repr__(self):
 return "Things%s" % self.args

  >>> Things(0,1,2)
  Things[[0], [1], [2]]
  >>> Things(range(2),xrange(2),(0,1))
  Things[[0, 1], [0, 1], [0, 1]]
  >>> Things(None, 0,1)
  Traceback (most recent call last):
File "", line 1, in ?
File "C:\Documents and Settings\Michael\My 
Documents\PyDev\Junk\safelist.py", line 32, in __init__
  assert len(set(len(obj) for obj in self.args)) == 1
  AssertionError


Michael



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


Re: Documentation suggestions

2005-12-06 Thread Michael Spencer
A.M. Kuchling wrote:
> Here are some thoughts on reorganizing Python's documentation, with
> one big suggestion.
> 

Thanks for raising this topic, and for your on-going efforts in this field.

I use the compiled html help file provided by PythonWin, which includes all the 
core documentation.  I usually use the index interface, not the table of 
contents (the main exception is the LibRef, see below).  In this form, the 
structure of the documentation is less important than how good the index is. 
Unfortunately, the "additional documentation', including, in particular, your 
re 
HowTo is linked, but not indexed and is therefore less accessible.

> The tutorial seems to be in pretty good shape because Raymond
...
Agreed, but as you say below, there may be friendlier forms available for the 
first-timer.

...
> There's another struggle within the LibRef: is it a reference or a
> tutorial?

I want it to help answer questions of the form "What's in the the library that 
might help me do x?"  For this case, some of the current section structure is 
not that helpful.  "Miscellaneous Services", in particular, gives no clue to 
treasures it contains.  I would prefer, for example, to see the data structure 
modules: collections, heapq, array etc... given their own section. 
Documentation/testing, cmd/options might be other candidates to draw together 
currently related material more meaningfully.

   Does it list methods in alphabetical order so you can look
> them up, or does it list them in a pedagogically useful order?  I
> think it has to be a reference;

A reference, yes, but not necessarily alphabetical if another organization is 
more communicative.  itertools is a good example where alphabetic presentation 
makes perfect sense, since the functions are more-or-less peers; the math 
functions are usefully classified by topic; textwrap presents most 
commonly-used 
functions first; several modules document classes before convenience functions. 
   Each of these has its merits, and I don't see a lot of mileage in trying to 
standardize them, given how varied modules are.  However, whatever the 
reference 
structure, examples add significantly to the value to me.

...

> I suspect the Achilles' heel of the docs is the Language Reference.
> Put aside the fact that it's not up to date with new-style classes and
> other stuff; that would be fixable with some effort.
> 

> To some degree, the guide is trying to be very formal; it's written
> like a specification for an implementor, not a document that people
> would read through.  But there's no other way for people to learn
> about all the special object methods like __add__; the tutorial can't
> cover them all, and the LibRef doesn't describe them.  So the newbie
> is stuck.

I find very little of value to me in the Language Ref.  Special methods are the 
crucial exception.  Perhaps they, together with a description of class 
semantics 
(including metaclasses and descriptors) could be moved to the Built-in types 
section of the LibRef, where some related material is already.

I don't know whether the rest of the Language reference is of use to 
implementers, but given the proliferation of implementations beyond Cpython 
(Jython, IronPython, pypy) I would speculate that a formal specification is now 
more important rather than less.  However, perhaps it would be possible to 
express the specification more succinctly via tests instead of a manual.

...
> 
> Perhaps we need a friendlier counterpart to the RefGuide, something
> like the 20-page introduction to Python at the beginning of Beazley's 
> Essential Reference:

I did't know this source, but I just skimmed it at 
http://www.amazon.com/gp/reader/0735709017/ref=sib_dp_pt/103-1276064-0751851#reader-page
(not sure if this is a session link), and I agree it's a very clear 
introduction.   Probably better first reading than the existing tutorial.

...


Michael

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


Re: i=2; lst=[i**=2 while i<1000]

2005-12-06 Thread Michael Spencer
Daniel Schüle wrote:
> Hello NG,
> 
> I am wondering if there were proposals or previous disscussions in this 
> NG considering using 'while' in comprehension lists
> 
> # pseudo code
> i=2
> lst=[i**=2 while i<1000]
> 

You are actually describing two features that list comps don't natively support 
- while-based termination, and calculating based on prior values of output.  Of 
course there are work-arounds for both, which others have shown.  Here's 
another 
  approach:

The while-based termination can be easily achieved using itertools.takewhile, 
e.g.,:

  >>> list(itertools.takewhile(lambda x: x < 10, range(100)))
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>>

the harder piece is to access the prior value.  One way is like this:

def chasetail(start, func):
 from itertools import tee
 def mygen():
 yield start
 for i in (func(i) for i in iterators[0]):
 yield i
 iterators = tee(mygen())
 return iterators[1]

the trick is to create two independent iterators, using itertools.tee, one of 
which is consumed internally in the func(i) for i in iterators[0] generator 
expression, the other is returned to use code.

  >>> it = chasetail(2, lambda x: x*x) #careful - this won't terminate
  >>> it.next()
  2
  >>> it.next()
  4
  >>> it.next()
  16
  >>> it.next()
  256
  >>> it.next()
  65536
  >>>

Then you can combine these two approaches to get something semantically like 
what you wanted in the first place (although not as pretty ;-)

  >>> list(itertools.takewhile(lambda x: x < 1000, chasetail(2, lambda x: x*x)))
  [2, 4, 16, 256]
  >>>



If you like this sort of thing, you might want to generalize the concept with a 
Stream class.  Here's minimal implementation:

import itertools as it

class Stream(object):
 """An extendable stream, that provides a separate iterator
 (using itertools.tee) on every iteration request"""

 def __init__(self, *iterables):
 self.queue = list(iterables)
 self.itertee = it.tee(self._chain(self.queue))[0]

 def _chain(self, queue):
 while queue:
 for i in self.queue.pop(0):
 self.head = i
 yield i

 def extend(self,other):
 self.queue.append(other)

 def __iter__(self):
 """Normal iteration over the iterables in self.queue in turn"""
 return self.itertee.__copy__()


then, you can write your squaring algorithm as:

  >>> s= Stream([2])
  >>> s.extend(it.takewhile(lambda x: x < 1000, (i**2 for i in s)))
  >>> list(s)
  [2, 4, 16, 256]


Michael




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


Re: Documentation suggestions

2005-12-07 Thread Michael Spencer
A.M. Kuchling wrote:
> On Tue, 06 Dec 2005 10:29:33 -0800, 
>   Michael Spencer <[EMAIL PROTECTED]> wrote:
>> not that helpful.  "Miscellaneous Services", in particular, gives no clue to 
>> treasures it contains.  I would prefer, for example, to see the data 
>> structure modules: collections, heapq, array etc... given their own section. 
>> Documentation/testing, cmd/options might be other candidates to draw together
>> currently related material more meaningfully.
> 
> You're right; "Miscellaneous Services" is a grab-bag of stuff, and so
> are 'Generic OS Services' and 'Optional OS Services'.  These chapters
> should be rearranged into more, smaller chapters.  
> 
> A patch for a draft reorganization is at http://www.python.org/sf/1375417
> 
> --amk
Thanks!  That looks like a good start.

I experimented with some more re-organization, but I don't see away to attach 
the resulting file in the SF comments, so I'll post it here instead.

Michael



% experimental re-organization of lib.tex,
% from http://www.python.org/sf/1375417

\tableofcontents

 % Chapter title:

\input{libintro}% Introduction


% =
% BUILT-INs
% =

\input{libobjs} % Built-in Types, Exceptions and Functions
\input{libfuncs}
\input{libstdtypes}
\input{libexcs}
\input{libconsts}



% =
% BASIC/GENERAL-PURPOSE OBJECTS
% =

% General object services
\input{libtypes}
\input{libnew}
\input{libweakref}
\input{libcopy}
\input{libpprint}
\input{librepr}

% Strings
\input{libstrings}  % String Services
\input{libstring}
\input{libre}
\input{libreconvert}
\input{libstruct}   % also/better in File Formats?
\input{libdifflib}
\input{libfpformat}
\input{libstringio}
\input{libtextwrap}
\input{libcodecs}
\input{libunicodedata}
\input{libstringprep}

% Data types and structures
%\input{libdata}% Data types and structures
\input{libdatetime}
\input{libcalendar}
\input{libcollections}
\input{libheapq}
\input{libarray}
\input{libsets}
\input{libsched}
\input{libmutex}
\input{libqueue}
\input{libuserdict}   % From runtime.  What happened to UserList and UserString?

% Numeric/Mathematical modules
\input{libdecimal}
\input{libmath}
\input{libcmath}
\input{librandom}
\input{libbisect} % is this needed here - more useful in Data types, like heapq?

% Functions, Functional, Generators and Iterators
\input{libitertools}
\input{libfunctional}
\input{liboperator}   % from runtime - better with itertools and functional


%\input{libmisc} % Miscellaneous Services


% =
% DATA FORMATS
% =

%% File formats
\input{libcfgparser}
\input{libnetrc}
\input{librobotparser}
\input{libcsv}
\input{libstruct}   % and in string?

% Big move - include all the markup and internet formats here

% MIME & email stuff
\input{email}
\input{libmailcap}
\input{libmailbox}
\input{libmhlib}
\input{libmimetools}
\input{libmimetypes}
\input{libmimewriter}
\input{libmimify}
\input{libmultifile}
\input{librfc822}

% encoding stuff
\input{libbase64}
\input{libbinascii}
\input{libbinhex}
\input{libquopri}
\input{libuu}
\input{libxdrlib}

\input{markup}  % Structured Markup Processing Tools
\input{libhtmlparser}
\input{libsgmllib}
\input{libhtmllib}
\input{libpyexpat}
\input{xmldom}
\input{xmldomminidom}
\input{xmldompulldom}
\input{xmlsax}
\input{xmlsaxhandler}
\input{xmlsaxutils}
\input{xmlsaxreader}
% \input{libxmllib}

\input{libcrypto}   % Cryptographic Services
\input{libhmac}
\input{libhashlib}
\input{libmd5}
\input{libsha}

% =
% FILE & DATABASE STORAGE
% =

\input{liballos}% File-system services (XXX change header)
\input{libos}
\input{libposixpath}% os.path
\input{libfileinput}
\input{libstat}
\input{libstatvfs}
\input{libfilecmp}
\input{libtempfile}
\input{libglob}
\input{libfnmatch}
\input{liblinecache}
\input{libshutil}
\input{libdircache}

%% Data compression and archiving
\input{libzlib}
\input{libgzip}
\input{libbz2}
\input{libzipfile}
\input{libtarfile}

%\input{libpersistence}  % Persistent storage
\input{libpickle}
\input{libcopyreg}  % really copy_reg % from runtime...
\input{libshelve}
\input{libmarshal}
\input{libanydbm}
\input{libwhichdb}
\input{libdbm}
\input{libgdbm}
\input{libdbhash}
\input{libbsddb}
\input{libdumbdbm}


% =
% OS
% =


\input{liballos}% Generic Operating System Services
\input{libtime}
\input{libgetpass}
\input{libcurses}
\input{libascii}% curses.ascii
\input{libcursespanel}
\input{libplatform}
\input{liberrno}

%% Interprocess communication/networking
\input{libsubprocess}
\input{l

Re: Bitching about the documentation...

2005-12-07 Thread Michael Spencer
Fredrik Lundh wrote:
> Rocco Moretti wrote:
> 
>> Insert punctuation & capitalization to make the following a correct and
>> coherent (if not a little tourtured).
>>
>> fred where guido had had had had had had had had had had had a better
>> effect on the reader
> 
> punctuation, including quote marks, I presume?
> 
> it's not time to bring out "d'ä ä e å, å i åa ä e ö" yet, I hope?
> 
> 
> 
> 
> 
Allowing quotation, almost anything is possible, e.g.,


Fred! Where Guido had had "had", Had had had "had had".  "Had had" had a better 
effect on the reader

or simply

"fred", where Guido had "had had had had had had had had had", had a better
effect on the reader

M

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


Re: newby question: Splitting a string - separator

2005-12-08 Thread Michael Spencer
Thomas Liesner wrote:
> Hi all,
> 
> i am having a textfile which contains a single string with names.
> I want to split this string into its records an put them into a list.
> In "normal" cases i would do something like:
> 
>> #!/usr/bin/python
>> inp = open("file")
>> data = inp.read()
>> names = data.split()
>> inp.close()
> 
> The problem is, that the names contain spaces an the records are also
> just seprarated by spaces. The only thing i can rely on, ist that the
> recordseparator is always more than a single whitespace.
> 
> I thought of something like defining the separator for split() by using
>  a regex for "more than one whitespace". RegEx for whitespace is \s, but
> what would i use for "more than one"? \s+?
> 
> TIA,
> Tom
\s+ gives one or more, you need \s{2,} for two or more:

  >>> import re
  >>> re.split("\s{2,}","Guido van Rossum  Tim Peters Thomas Liesner")
  ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']
  >>>

Michael

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


Re: Dynamically add Class to Modules

2005-12-08 Thread Michael Spencer
[EMAIL PROTECTED] wrote:
> I'm trying to add a class to a module at runtime.  I've seen examples
> of adding a method to a class, but I haven't been able to suit it to my
> needs.
> 
> As part of a testsuite, I have a main process X that searches
> recursively for python test files.  Those files typically have a global
> "isSupported" method, in which the file tells the test searcher "do or
> do not run me", as well as the typical TestName_TestCase class, with a
> testMyTest method.
> 
> For numerous and reasonable reasons, the TestName_TestCase class must
> be generated at runtime (i cannot use any pre-processing scripts to
> generate the testcase files).  So the external runner has to look into
> each testcase file, determine if it is supported, expand out the
> test-class code, and add that new class to that testcase in memory.
> 
> I hope this picture helps:
> 
> 
> # atestcase.py 
> def isSupported():
> """ do a real check"""
> return True
> 
> 
> ThisTestName = "foo"
> TestCode = \
> """
> class %s_TestCase:
> def __init__( self ):
> """ do some stuff"""
> 
> def test_%s( self ):
>   """ run the test """
> """
> #
> 
> 
> #--- The external runner 
> 
> (essentially)
> import atestcase.py
> if atestcase.isSupported():
> # Run this test
> 
> (here's what i'm trying to figure out)
> #--> expand atestcase.TestCode out to include "foo"
> #--> make the testcode a class
> #--> add the new foo_TestCase class to
> #the atestcase module
> 
> #-
> 
> 
> So:  Does anyone know how dynamically generate a class, and add it to a
> "module" that is already in memory?
> 
> Thanks so much in advance.  My flu is heating up my brain pretty badly,
> so please ask me if I have to clarify anything above.
> 
Bill,
I think this should do it:

import atestcase as T
exec T.TestCode % T.ThisTestName in T.__dict__

If you want to substitute ThisTestName more than once, you might be better off 
using the %(name)s form, supplied with a dictionary {name: "foo"}, or you could 
look at the new string.Template class for easier string subsitution.

Michael

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


Re: Dynamically add Class to Modules

2005-12-08 Thread Michael Spencer
[EMAIL PROTECTED] wrote:
...
> exec testModule.TheTestCode %(testModule.TheTestName, testModule.TheTestName )

...

Try changing that to exec ~ in testModule.__dict__

otherwise, your class statement gets executed in the current scope

Michael

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


Re: getting host and path from url

2005-12-09 Thread Michael Spencer
Steve Young wrote:
> Hi, this is probably an easy question but is there a way to get the host and 
> path seperatly out of an url? 
>   
>   Example:
>   
>   url = http://news.yahoo.com/fc/world/iraq
>   
>   and i want some way of getting:
>   
>   host = http://news.yahoo.com
>   and
>   path = /fc/world/iraq
>   
>   thanks.
>   
>   -Steve
>
>   
> 
>   
> -
> Yahoo! Shopping
>  Find Great Deals on Holiday Gifts at Yahoo! Shopping 
> 
check out urlparse in the stdlib

Michael

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


Re: newby question: Splitting a string - separator

2005-12-09 Thread Michael Spencer
[EMAIL PROTECTED] wrote:
> Thomas Liesner wrote:
>> Hi all,
>>
>> i am having a textfile which contains a single string with names.
>> I want to split this string into its records an put them into a list.
>> In "normal" cases i would do something like:
>>
>>> #!/usr/bin/python
>>> inp = open("file")
>>> data = inp.read()
>>> names = data.split()
>>> inp.close()
>> The problem is, that the names contain spaces an the records are also
>> just seprarated by spaces. The only thing i can rely on, ist that the
>> recordseparator is always more than a single whitespace.
>>
>> I thought of something like defining the separator for split() by using
>>  a regex for "more than one whitespace". RegEx for whitespace is \s, but
>> what would i use for "more than one"? \s+?
>>
> Can I just use "two space" as the seperator ?
> 
> [ x.strip() for x in data.split("  ") ]
> 
If you like, but it will create dummy entries if there are more than two spaces:

  >>> data = "Guido van Rossum  Tim PetersThomas Liesner"
  >>> [ x.strip() for x in data.split("  ") ]
  ['Guido van Rossum', 'Tim Peters', '', 'Thomas Liesner']

You could add a condition to the listcomp:

  >>> [name.strip() for name in data.split("  ") if name]
  ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']

but what if there is some other whitespace character?

  >>> data = "Guido van Rossum  Tim Peters  \t  Thomas Liesner"
  >>> [name.strip() for name in data.split("  ") if name]
  ['Guido van Rossum', 'Tim Peters', '', 'Thomas Liesner']
  >>>

perhaps a smarter condition?

  >>> [name.strip() for name in data.split("  ") if name.strip(" \t")]
  ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']

but this is beginning to feel like hard work.


I think this is a case where it's not worth the effort to try to avoid the 
regexp

  >>> import re
  >>> re.split("\s{2,}",data)
  ['Guido van Rossum', 'Tim Peters', 'Thomas Liesner']
  >>>

Michael


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


args (was Re: Lambda as declarative idiom (was RE: what is lambda used for in real code?))

2005-01-04 Thread Michael Spencer
Roman Suzi wrote:
Maybe this is too outlandish, but I see lambdas as a "quote" mechanism,
which presents a possibility to postpone (precisely control, delegate)
evaluation. That is, an ovehead for lambda must be much lower but at the
same time visible to the programmer:
 d = a + (lambda x, y: x+ y)(3, 4)
[...]
I believe that this "possibility to postpone" divides into two related but 
separate concepts: controlling the moment of evaluation, and assembling the 
arguments required at that moment.  They are both species of 'eval', but 
managing arguments is more specialized, because it includes possibly renaming 
parameters, assigning default values, processing positional and keyword 
arguments, and, perhaps in the future dealing with argument types.

Meanwhile, GvR wrote (about defining Interfaces in the context of Optional 
Static Type Checking)
Method declarations can be inspected to find out their signature. I propose a
__signature__ attribute (also for methods defined in classes!) which might be an
object whose attributes make the signature easily inspectable. This might take 
the form of a list of argument declaration objects giving the name, type and default
(if any) for each argument, and a separate argument for the return type. For 
signatures that include *args and/or **kwds, the type of the additional arguments 
should also be given (so you can write for example a varargs method whose arguments
are all strings).
GvR's method.__signature__ object might be related to the args object I proposed 
 as part of the syntax for anonymous functions without 'lambda'. i.e.,

args(a,*b,**kw) --> an object that specifies but does not evaluate its 
parameters until it is supplied to a callable, possibly with calling parameters

This object would contain the default values, and could contain type 
annotations, explicit, or inferred, as well as more complex assertions used in 
several contexts.

* Current function syntax:
def func(a,*b,**c) : pass
creates func with func.__signature__ = args(a,*b,**c)
and when func is called, the args are evaluated using a mechanism in
args.__call__
so, roughly, eval(func.__signature__) --> func.locals
 * Anonymous functions
	Syntax alternatives at http://www.python.org/moin/AlternateLambdaSyntax
	e.g., (f(a) + o(b) - o(c) for args(a, b, c))
	
	args would evaluated with the calling parameters and made available in 			the 
local scope defined by ()
	
 * A stricter alternative to keyword arguments:
 	argspec = args(arg1, arg2, arg3)
	def func(**argspec): pass
	
	is equivalent to def func(arg1, arg2, arg3): pass


args["arg1"]

(i.e., only args defined in argspec are accepted)
 * Useful infrastructure for user-supplied type-based dispatch/lightweight 
multimethods:
	
	argspec = args([(a:int, b:int),(a:str,b:str)])
	
	then a framework can provide a custom args.__call__ method that does
	conformance-checking, adaptation or whatever


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


Re: pure python code to do modular-arithmetic unit conversions?

2005-01-21 Thread Michael Spencer
Dan Stromberg wrote:
Is there already a pure python module that can do modular-arithmetic unit
conversions, like converting a huge number of seconds into months,
weeks... or a bandwidth measure into megabits/s or gigabits/s or
megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?
Thanks!
Take a look at:
http://home.tiscali.be/be052320/Unum_tutorial.html
From the intro:
"Unum stands for 'unit-numbers'. It is a Python module that allows to define and 
manipulate true quantities, i.e. numbers with units such as 60 seconds, 500 
watts, 42 miles-per-hour, 100 kg per square meter, 14400 bits per second, 30 
dollars etc. The module validates unit consistency in arithmetic expressions; it 
provides also automatic conversion and output formatting."

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


Re: Reload Tricks

2005-01-21 Thread Michael Spencer
Kamilche wrote:
I want my program to be able to reload its code dynamically. I have a
large hierarchy of objects in memory. The inheritance hierarchy of
these objects are scattered over several files.
I find that after reloading the appropriate files, and overwriting the
__class__ of object instances, one more thing is necessary: reloading
the __bases__ of each reloaded class. If I don't do this, the modules
reloaded first point to old versions of the classes from later modules,
and when the later module is reloaded, it doesn't update the
inheritance hierarchy of classes already loaded.
This appears to be working... but now I'm wondering, what else did it
not change? Can I expect more toes to be blown off?
--Kamilche
There are some cases when re-assigning __class__ isn't possible, for 
example:
 >>> class A(object):
 ... pass
 ...
 >>> class B(dict):
 ... pass
 ...
 >>> class C:
 ... pass
 ...
 >>> a = A()
 >>> a.__class__ = B
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: __class__ assignment: 'A' object layout differs from 'B'
 >>> a.__class__ = C
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: __class__ must be set to new-style class, not 'classobj' object
 >>>
An alternative approach (with some pros and cons) is to modify the class in 
place, using something like:

 >>> def reclass(cls, to_cls):
 ... """Updates attributes of cls to match those of to_cls"""
 ...
 ... DONOTCOPY = ("__name__","__bases__","__base__",
 ... "__dict__", "__doc__","__weakref__")
 ...
 ... fromdict = cls.__dict__
 ... todict = to_cls.__dict__
 ...
 ... # Delete any attribute present in the new class
 ... [delattr(cls,attr) for attr in fromdict.keys()
 ... if not((attr in todict) or (attr in DONOTCOPY)) ]
 ...
 ... for to_attr, to_obj in todict.iteritems():
 ...
 ... if to_attr in DONOTCOPY:
 ... continue
 ...
 ... # This overwrites all functions, even if they haven't changed.
 ... if type(to_obj) is types.MethodType:
 ... func = to_obj.im_func
 ... to_obj = types.MethodType(func,None, cls)
 ...
 ... setattr(cls, to_attr,to_obj)
 ...
 >>> class A(object):
 ... attr = "A"
 ...
 >>> class B(object):
 ... attr = "B"
 ...
 >>> a = A()
 >>> reclass(A,B)
 >>> a.attr
'B'
 >>>
This copies attributes of old and new-style classes (in fact anything with a 
__dict__ so probably a module would work too)

You still run into problems trying to re-assigning __bases__ to incompatible 
objects, but this one-attribute-at-a-time approach gives you the potential to 
intercept problem cases.  In the example above, problems are avoided by not 
copying __bases__.

An additional advantage of this aprpoach is that you don't need to keep track of 
class instances, in order to change their __class__.  Instances automatically 
acquire the new behavior

One wart is that class docstrings are not writeable, so cannot be copied.  Why?
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reload Tricks

2005-01-22 Thread Michael Spencer
Kamilche wrote:
I want my program to be able to reload its code dynamically. I have a
large hierarchy of objects in memory. The inheritance hierarchy of
these objects are scattered over several files.
Michael Spencer wrote:
An alternative approach (with some pros and cons) is to modify the class in 
place, using something like:
 >>> def reclass(cls, to_cls):
 ... """Updates attributes of cls to match those of to_cls"""
 ...
 ... DONOTCOPY = ("__name__","__bases__","__base__",
 ... "__dict__", "__doc__","__weakref__") 
etc...
Kamilche wrote:
Would it be possible to just not copy any attribute that starts and
ends with '__'? Or are there some important attributes being copied?

Possible?  of course, it's Python ;-)
But there are many 'magic' attributes for behavior that you probably do want to 
copy:

e.g., __getitem__, __setitem__ etc...
See: http://docs.python.org/ref/specialnames.html
Michael Hudson's recipe: 	 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164
does auto-reloading "automatically", at the price of changing the type of the 
classes you want to manage.  It's a very convenient approach for interactive 
development (which is the recipe's stated purpose).  It works by tracking 
instances and automatically updating their class.  If your program relies on 
class identity, you may run into problems.

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


Re: default value in a list

2005-01-22 Thread Michael Spencer
Alex Martelli wrote:
[explanation and the following code:]
 >>> a, b, c = it.islice(
 ...   it.chain(
 ...   line.split(':'), 
 ...   it.repeat(some_default),
 ...   ), 
 ...   3)
 ... 
 ...   
 >>> def pad_with_default(N, iterable, default=None):
 ... it = iter(iterable)
 ... for x in it:
 ... if N<=0: break
 ... yield x
 ... N -= 1
 ... while N>0:
 ... yield default
 ... N -= 1
Why not put these together and put it in itertools, since the requirement seems 
to crop up every other week?

 >>> line = "A:B:C".split(":")
 ...
 >>> def ipad(N,iterable, default = None):
 ... return it.islice(it.chain(iterable, it.repeat(default)), N)
 ...
 >>> a,b,c,d = ipad(4,line)
 >>> a,b,c,d
('A', 'B', 'C', None)
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: What YAML engine do you use?

2005-01-22 Thread Michael Spencer
Paul Rubin wrote:
YAML looks to me to be completely insane, even compared to Python
lists.  I think it would be great if the Python library exposed an
interface for parsing constant list and dict expressions, e.g.:
   [1, 2, 'Joe Smith', 8237972883334L,   # comment
  {'Favorite fruits': ['apple', 'banana', 'pear']},  # another comment
  'xyzzy', [3, 5, [3.14159, 2.71828, [
I don't see what YAML accomplishes that something like the above wouldn't.
Note that all the values in the above have to be constant literals.
Don't suggest using eval.  That would be a huge security hole.
Not hard at all, thanks to compiler.ast:
>>> import compiler
 ...
 >>> class AbstractVisitor(object):
 ... def __init__(self):
 ... self._cache = {} # dispatch table
 ...
 ... def visit(self, node,**kw):
 ... cls = node.__class__
 ... meth = self._cache.setdefault(cls,
 ... getattr(self,'visit'+cls.__name__,self.default))
 ... return meth(node, **kw)
 ...
 ... def default(self, node, **kw):
 ... for child in node.getChildNodes():
 ... return self.visit(child, **kw)
 ...
 >>> class ConstEval(AbstractVisitor):
 ... def visitConst(self, node, **kw):
 ... return node.value
 ...
 ... def visitName(self,node, **kw):
 ... raise NameError, "Names are not resolved"
 ...
 ... def visitDict(self,node,**kw):
 ... return dict([(self.visit(k),self.visit(v)) for k,v in node.items])
 ...
 ... def visitTuple(self,node, **kw):
 ... return tuple(self.visit(i) for i in node.nodes)
 ...
 ... def visitList(self,node, **kw):
 ... return [self.visit(i) for i in node.nodes]
 ...
 >>> ast = compiler.parse(source,"eval")
 >>> walker = ConstEval()
 >>> walker.visit(ast)
[1, 2, 'Joe Smith', 8237972883334L, {'Favorite fruits': ['apple', 'banana', 
'pear']}, 'xyzzy', [3, 5, [3.14158999, 2.71828, [

Add sugar to taste
Regards
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: What YAML engine do you use?

2005-01-24 Thread Michael Spencer
Fredrik Lundh wrote:
Sion Arrowsmith wrote:
I'm probably not thinking deviously enough here, but how are you
going to exploit an eval() which has very tightly controlled
globals and locals (eg. eval(x, {"__builtins__": None}, {}) ?
try this:
eval("'*'*100*2*2*2*2*2*2*2*2*2")
I updated the safe eval recipe I posted yesterday to add the option of reporting 
unsafe source, rather than silently ignoring it.  Is this completely safe?  I'm 
interested in feedback.

Michael
Some source to try:
 >>> goodsource =  """[1, 2, 'Joe Smith', 8237972883334L,   # comment
 ...   {'Favorite fruits': ['apple', 'banana', 'pear']},  # another comment
 ...   'xyzzy', [3, 5, [3.14159, 2.71828, ["""
 ...
Unquoted string literal
 >>> badsource = """[1, 2, JoeSmith, 8237972883334L,   # comment
 ...   {'Favorite fruits': ['apple', 'banana', 'pear']},  # another comment
 ...   'xyzzy', [3, 5, [3.14159, 2.71828, ["""
 ...
Non-constant expression
 >>> effbot = "'*'*100*2*2*2*2*2*2*2*2*2"
 >>> safe_eval(good_source)
[1, 2, 'Joe Smith', 8237972883334L, {'Favorite fruits': ['apple', 'banana', 
'pear']}, 'xyzzy', [3, 5, [3.14158999, 2.71828, [
 >>> assert _ == eval(good_source)

 >>> safe_eval(bad_source)
Traceback (most recent call last):
  [...]
Unsafe_Source_Error: Line 1.  Strings must be quoted: JoeSmith
 >>> safe_eval(bad_source, fail_on_error = False)
[1, 2, None, 8237972883334L, {'Favorite fruits': ['apple', 'banana', 'pear']}, 
'xyzzy', [3, 5, [3.14158999, 2.71828, [

 >>> safe_eval(effbot)
Traceback (most recent call last):
  [...]
Unsafe_Source_Error: Line 1.  Unsupported source construct: compiler.ast.Mul
 >>> safe_eval(effbot, fail_on_error = False)
 ...
'*'
 >>>
Source:
import compiler
class Unsafe_Source_Error(Exception):
def __init__(self,error,descr = None,node = None):
self.error = error
self.descr = descr
self.node = node
self.lineno = getattr(node,"lineno",None)
def __repr__(self):
return "Line %d.  %s: %s" % (self.lineno, self.error, self.descr)
__str__ = __repr__
class AbstractVisitor(object):
def __init__(self):
self._cache = {} # dispatch table
def visit(self, node,**kw):
cls = node.__class__
meth = self._cache.setdefault(cls,
getattr(self,'visit'+cls.__name__,self.default))
return meth(node, **kw)
def default(self, node, **kw):
for child in node.getChildNodes():
return self.visit(child, **kw)
visitExpression = default
class SafeEval(AbstractVisitor):
def visitConst(self, node, **kw):
return node.value
def visitDict(self,node,**kw):
return dict([(self.visit(k),self.visit(v)) for k,v in node.items])
def visitTuple(self,node, **kw):
return tuple(self.visit(i) for i in node.nodes)
def visitList(self,node, **kw):
return [self.visit(i) for i in node.nodes]
class SafeEvalWithErrors(SafeEval):
def default(self, node, **kw):
raise Unsafe_Source_Error("Unsupported source construct",
node.__class__,node)
def visitName(self,node, **kw):
raise Unsafe_Source_Error("Strings must be quoted",
 node.name, node)
# Add more specific errors if desired
def safe_eval(source, fail_on_error = True):
walker = fail_on_error and SafeEvalWithErrors() or SafeEval()
try:
ast = compiler.parse(source,"eval")
except SyntaxError, err:
raise
try:
return walker.visit(ast)
except Unsafe_Source_Error, err:
raise
--
http://mail.python.org/mailman/listinfo/python-list


Re: Classical FP problem in python : Hamming problem

2005-01-25 Thread Michael Spencer
Francis Girard wrote:
The following implementation is even more speaking as it makes self-evident 
and almost mechanical how to translate algorithms that run after their tail 
from recursion to "tee" usage :

Thanks, Francis and Jeff for raising a fascinating topic.  I've enjoyed trying 
to get my head around both the algorithm and your non-recursive implementation.

Here's a version of your implementation that uses a helper class to make the 
algorithm itself prettier.

from itertools import tee, imap
def hamming():
def _hamming():
yield 1
for n in imerge(2 * hamming, imerge(3 * hamming, 5 * hamming)):
yield n
hamming = Tee(_hamming())
return iter(hamming)
class Tee(object):
"""Provides an indepent iterator (using tee) on every iteration request
Also implements lazy iterator arithmetic"""
def __init__(self, iterator):
self.iter = tee(iterator,1)[0]
def __iter__(self):
return self.iter.__copy__()
def __mul__(self, number):
return imap(lambda x: x * number,self.__iter__())
def imerge(xs, ys):
  x = xs.next()
  y = ys.next()
  while True:
if x == y:
  yield x
  x = xs.next()
  y = ys.next()
elif x < y:
  yield x
  x = xs.next()
else: # if y < x:
  yield y
  y = ys.next()
>>> hg = hamming()
>>> for i in range(1):
... n = hg.next()
... if i % 1000 == 0: print i, n
...
0 1
1000 5184
2000 81
3000 27993600
4000 4707158941350
5000 5096079360
6000 4096000
7000 2638827906662400
8000 143327232
9000 680244480
Regards
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: Classical FP problem in python : Hamming problem

2005-01-25 Thread Michael Spencer
Nick Craig-Wood wrote:
Steven Bethard <[EMAIL PROTECTED]> wrote:
Nick Craig-Wood wrote:
Thinking about this some more leads me to believe a general purpose
imerge taking any number of arguments will look neater, eg
def imerge(*generators):
   values = [ g.next() for g in generators ]
   while True:
   x = min(values)
   yield x
   for i in range(len(values)):
   if values[i] == x:
   values[i] = generators[i].next()
This kinda looks like it dies after the first generator is exhausted, 
but I'm not certain.

Yes it will stop iterating then (rather like zip() on lists of unequal
size). Not sure what the specification should be!  It works for the
hamming problem though.

list(imerge(iter([1, 2]), iter([1, 2, 3]), iter([1, 2, 3, 4])))
[1, 2]

An alternate version that doesn't search for 'i':
py> def imerge(*iterables):
... iters = [iter(i) for i in iterables]
... values = [i.next() for i in iters]
... while iters:
... x, i = min((val, i) for i, val in enumerate(values))
... yield x
... try:
... values[i] = iters[i].next()
... except StopIteration:
... del iters[i]
... del values[i]
... 
py> list(imerge([1, 4, 7], [2, 5, 8], [3, 6, 9]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
py> list(imerge([3, 6, 9], [1, 4, 7], [2, 5, 8]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
py> list(imerge([1, 4, 7], [3, 6, 9], [2, 5, 8]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

This isn't quite right...

list(imerge([1, 2, 3], [1, 2, 3], [1, 2, 3]))
[1, 1, 1, 2, 2, 2, 3, 3, 3]
This should produce
[1, 2, 3]
So I'm afraid the searching *is* necessary - you've got to find all
the generators with the min value and move them on.
Here's a dict-based implementation: cute, but slow, at least for a small number 
of iterators

 >>> def imerge(*iterables):
 ... cache = {}
 ... iterators = map(iter,iterables)
 ... number = len(iterables)
 ... exhausted = 0
 ... while 1:
 ... for it in iterators:
 ... try:
 ... cache.setdefault(it.next(),[]).append(it)
 ... except StopIteration:
 ... exhausted += 1
 ... if exhausted == number:
 ... raise StopIteration
 ... val = min(cache)
 ... iterators = cache.pop(val)
 ... yield val
 >>> list(imerge([1, 2, 3, 6], [1, 2, 3, 7], [1, 2, 3, 4, 5]))
[1, 2, 3, 4, 5, 6, 7]
 >>>
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: limited python virtual machine (WAS: Another scripting language implemented into Python itself?)

2005-01-25 Thread Michael Spencer
Steven Bethard wrote:

> 
> I wish there was a way to, say, exec something with no builtins and
with 
> import disabled, so you would have to specify all the available 
> bindings, e.g.:
> 
> exec user_code in dict(ClassA=ClassA, ClassB=ClassB)
> 
> but I suspect that even this wouldn't really solve the problem,
because 
> you can do things like:
> 
> py> class ClassA(object):
> ... pass
> ...
> py> object, = ClassA.__bases__
> py> object
> 
> py> int = object.__subclasses__()[2]
> py> int
> 
> 
> so you can retrieve a lot of the builtins.  I don't know how to
retrieve 
>  __import__ this way, but as soon as you figure that out, you can then

> do pretty much anything you want to.
> 
> Steve

Steve

Safe eval recipe posted to cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469

Couldn't safe exec be programmed similarly?

'import' and 'from' are syntax, so trivially avoided

Likewise, function calls are easily intercepted

As you say, attribute access to core functions appears to present the
challenge. It is easy to intercept attribute access, harder to know
what's safe.  If there were a known set of 'dangerous' objects e.g.,
sys, file, os etc... then these could be checked by identity against any
attribute returned

Of course, execution would be painfully slow, due to double -
interpretation.  

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


Re: limited python virtual machine (WAS: Another scripting language implemented into Python itself?)

2005-01-25 Thread Michael Spencer
Steven Bethard wrote:
>
> I wish there was a way to, say, exec something with no builtins and
> with import disabled, so you would have to specify all the available
> bindings, e.g.:
>
> exec user_code in dict(ClassA=ClassA, ClassB=ClassB)
>
> but I suspect that even this wouldn't really solve the problem,
> because you can do things like:
>
> py> class ClassA(object):
> ... pass
> ...
> py> object, = ClassA.__bases__
> py> object
> 
> py> int = object.__subclasses__()[2]
> py> int
> 
>
> so you can retrieve a lot of the builtins.  I don't know how to
> retrieve  __import__ this way, but as soon as you figure that out, you
> can then do pretty much anything you want to.
>
> Steve
Steve
Safe eval recipe posted to cookbook: 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469

Couldn't safe exec be programmed similarly?
'import' and 'from' are syntax, so trivially avoided
Likewise, function calls are easily intercepted
As you say, attribute access to core functions appears to present the challenge. 
It is easy to intercept attribute access, harder to know what's safe.  If there 
were a known set of 'dangerous' objects e.g., sys, file, os etc... then these 
could be checked by identity against any attribute returned

Of course, execution would be painfully slow, due to double - interpretation.
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: limited python virtual machine (WAS: Another scripting language implemented into Python itself?)

2005-01-25 Thread Michael Spencer
Steven Bethard wrote:
Michael Spencer wrote:
Safe eval recipe posted to cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469

This recipe only evaluates constant expressions:
"Description:
Evaluate constant expressions, including list, dict and tuple using the 
abstract syntax tree created by compiler.parse"

It means you can't eval arbitrary Python code -- it's basically just a 
data parser.  Handy in some situations, but not the equivalent of a 
limited Python virtual machine.
Indeed.  But it's easy to extend this to arbitrary constructs.  You just need to 
decide what code to emit for the other 50 or so ast node types.  Many of those 
are boiler-plate binops.

Likewise, function calls are easily intercepted
I'm not sure I follow this...  How do you intend to intercept all 
function calls?
Sorry, should have been more precise.  In the AST, Function calls have their own 
 node type, so it is easy to 'intercept' them and execute them conditionally

[snip]
It sounds like you're suggesting overriding the global attribute access 
mechanism.  Is that right?  So that every time Python encountered an 
attribute access, you would verify that the attribute being accessed is 
not on the 'dangerous' list?
Just in the context of the AST-walker, yes
  I don't know how to do that without
basically rewriting some of Python's C code, though certainly I'm no 
expert in the area...
Not messing with the CPython interpreter
Also, I'm not sure identity is sufficient:
py> import sys
py> import new
py> new.module('newsys')
py> newsys = new.module('newsys')
py> newsys.__dict__.update(sys.__dict__)
py> newsys is sys
False
py> newsys == sys
False
Right - the crux of the problem is how to identify dangerous objects.  My point 
is that if such as test is possible, then safe exec is very easily implemented 
within current Python. If it is not, then it is essentially impossible.

Let's assume that it is indeed not possible to know in general whether an object 
is safe, either by inspecting its attributes, or by matching its identity 
against a black list.

It might still be possible to have a reliable test within a problem-specific 
domain i.e., white-listing.  This, I think, is what you meant when you said:

I wish there was a way to, say, exec something with no builtins and with import 
disabled, so you would have to specify all the available bindings, e.g.:
exec user_code in dict(ClassA=ClassA, ClassB=ClassB) 
I believe that if you can come up with a white-list, then the rest of the 
problem is easy.

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


Re: limited python virtual machine (WAS: Another scripting language implemented into Python itself?)

2005-01-25 Thread Michael Spencer
Cameron Laird wrote:
In article <[EMAIL PROTECTED]>,
Michael Spencer  <[EMAIL PROTECTED]> wrote:
.
.
.
Right - the crux of the problem is how to identify dangerous objects.  My point 
is that if such as test is possible, then safe exec is very easily implemented 
within current Python. If it is not, then it is essentially impossible.


I'll suggest yet another perspective:  add another indirection.
As the virtual machine becomes more available to introspection,
it might become natural to define a *very* restricted interpreter
which we can all agree is safe, PLUS a means to extend that 
specific instance of the VM with, say, new definitions of bindings
for particular AST nodes.  Then the developer has the means to
"build out" his own VM in a way he can judge useful and safe for
his own situation.  Rather than the Java there-is-one-"safe"-for-
all approach, Pythoneers would have the tools to create safety.
That does sound good.  And evolutionary, because the very restricted VM could be 
implemented today (in Python), and subsequently PyPy (or whatever) could 
optimize it.

The safe eval recipe I referred to earlier in the thread is IMO a trivial 
example of of this approach. Of course, its restrictions are extreme - only 
constant expressions, but it is straightforwardly extensible to any subset of 
the language.

The limitation that I see with this approach is that it is not, in general, 
syntax that is safe or unsafe (with the notable exception of 'import' and its 
relatives).  Rather, it the library objects, especially the built-ins, that 
present the main source of risk.

So, if I understand your suggestion, it would require assessing the safety of 
the built-in objects, as well as providing an interpreter that could control 
access to them, possibly with fine-grain control at the attribute level.

M

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


Re: python without OO

2005-01-25 Thread Michael Spencer
Davor wrote:
Thanks,
I do not hate OO - I just do not need it for the project size I'm
dealing with - and the project will eventually become open-source and
have additional developers - so I would prefer that we all stick to
"simple procedural" stuff rather than having to deal with a developer
that will be convincing me that his 50 layers inheritance hierarchy is
good since it exists in some weird pattern that he saw somewhere on
some Java design patterns discussion board :-) and other "proper" OO
design issues...  Once I opted for C++ in a very small project and
believed everyone will stick with C subset + better type checking
offered through C++ - but I simply could not manage to keep them off
using OO stuff which was just making program more complicated than it
should have been. (note, I am not an experienced developer, nor the
others I'll be working with (even though some think they are:-)), so I
prefer preemptively dealing with issue of everyone showing off their OO
design skills)
Davor
Perhaps pylint (http://www.logilab.org/projects/pylint) or its ilk can help you 
enforce a coding style

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


Re: Classical FP problem in python : Hamming problem

2005-01-27 Thread Michael Spencer
Paul Rubin wrote:
Francis Girard <[EMAIL PROTECTED]> writes:
Thank you Nick and Steven for the idea of a more generic imerge.

If you want to get fancy, the merge should use a priority queue (like
in the heapsort algorithm) instead of a linear scan through the
incoming iters, to find the next item to output.  That lowers the
running time to O(n log k) instead of O(n*k), where k is the number of
iterators and n is the length.
I looked at a heapq solution but didn't see any clean way of dealing with 
multiple iterators having equal values.  The dict solution below deals cleanly 
with that, since one key can be shared by any number of iterators.  Extracting 
the minimum, and the associated iterables is fast, but the overall solution is 
still slower than the brute force approach for the 3 hamming iterators.

 >>> def imerge(*iterables):
 ... cache = {}
 ... iterators = map(iter,iterables)
 ... number = len(iterables)
 ... exhausted = 0
 ... while 1:
 # First time through, looked at all of them
 # Subsequently, update only the minimum iterators
 ... for it in iterators:
 ... try:
 # Key each iterator by its next() value
 # Multiple iterators may share the same key
 ... cache.setdefault(it.next(),[]).append(it)
 ... except StopIteration:
 ... exhausted += 1
 ... if exhausted == number:
 ... raise StopIteration
 # Get the lowest value
 ... val = min(cache)
 # and all the iterators that have that value
 ... iterators = cache.pop(val) 
 ... yield val
 >>> list(imerge([1, 2, 3, 6], [1, 2, 3, 7], [1, 2, 3, 4, 5]))
[1, 2, 3, 4, 5, 6, 7]
 >>>
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: remove duplicates from list *preserving order*

2005-02-03 Thread Michael Spencer
Steven Bethard wrote:
I'm sorry, I assume this has been discussed somewhere already, but I 
found only a few hits in Google Groups...  If you know where there's a 
good summary, please feel free to direct me there.

I have a list[1] of objects from which I need to remove duplicates.  I 
have to maintain the list order though, so solutions like set(lst), etc. 
will not work for me.  What are my options?  So far, I can see:

def filterdups(iterable):
result = []
for item in iterable:
if item not in result:
result.append(item)
return result
def filterdups(iterable):
result = []
seen = set()
for item in iterable:
if item not in seen:
result.append(item)
seen.add(item)
return result
def filterdups(iterable):
seen = set()
for item in iterable:
if item not in seen:
seen.add(item)
yield item
Does anyone have a better[2] solution?
STeve
[1] Well, actually it's an iterable of objects, but I can convert it to 
a list if that's helpful.

[2] Yes I know, "better" is ambiguous.  If it helps any, for my 
particular situation, speed is probably more important than memory, so 
I'm leaning towards the second or third implementation.
How about:
>>> def filterdups3(iterable):
... seen = set()
... def _seen(item):
... return item in seen or seen.add(item)
... return itertools.ifilterfalse(_seen,iterable)
...
>>> list(filterdups3([1,2,2,3,3,3,4,4,4,2,2,5]))
[1, 2, 3, 4, 5]
>>>
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: List mapping question

2005-02-03 Thread Michael Spencer
Marc Huffnagle wrote:
I have a number of variables that I want to modify (a bunch of strings 
that I need to convert into ints).  Is there an easy way to do that 
other than saying:

 > a = int(a)
 > b = int(b)
 > c = int(c)
It may not matter to you, at the moment, but a = int(a) is not strictly 
'modifying a variable'.  Instead int(a) creates a new int object, if possible, 
from the object that a is currently bound to.  Then a is rebound to the new object.

I tried
 > [i = int(i) for i in [a, b, c]]
You can't make an assignment in a list comprehension.  If your 'variables' are 
object attributes, you could do: [setattr(obj,name,int(getattr(obj,name)) for 
name in [list of attribute names]]


but that didn't work because it was creating a list with the values of 
a, b and c instead of the actual variables themselves, then trying to 
set a string equal to an integer, which it really didn't like.

 Marc
For your problem as stated:
>>> a=b=c="1"
>>> for var in ["a","b","c"]:
... exec "%s = int(%s)" % (var,var)
...
>>> a,b,c
(1, 1, 1)
>>>
But don't do this, except as a "one-off" data crunching exercise
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to generate SQL SELECT pivot table string

2005-02-03 Thread Michael Spencer
McBooCzech wrote:
Hallo all,
I am trying to generate SQL SELECT command which will return pivot
table. The number of column in the pivot table depends on the data
stored in the database. It means I do not know in advance how many
columns the pivot table will have.
For example I will test the database as following:
SELECT DISTINCT T1.YEAR FROM T1
The SELECT command will return:
2002
2003
2004
2005
So I would like to construct following select:
select T1.WEEK,
SUM (case T1.YEAR when '2002' then T1.PRICE else 0 END) Y_02,
SUM (case T1.YEAR when '2003' then T1.PRICE else 0 END) Y_03,
SUM (case T1.YEAR when '2004' then T1.PRICE else 0 END) Y_04,
SUM (case T1.YEAR when '2005' then T1.PRICE else 0 END) Y_05
from T1
group by T1.week
which will return pivot table with 5 columns:
WEEK, Y_02, Y_03, Y_04, Y_05,
but if the command "SELECT DISTINCT T1.YEAR FROM T1" returns:
2003
2004
I have to construct only following string:
select T1.WEEK,
SUM (case T1.YEAR when '2003' then T1.PRICE else 0 END) Y_03,
SUM (case T1.YEAR when '2004' then T1.PRICE else 0 END) Y_04,
from T1
group by T1.week
which will return pivot table with 3 columns:
WEEK, Y_03, Y_04
Can anyone help and give me a hand or just direct me, how to write a
code which will generate SELECT string depending on the data stored in
the database as I described?
Thanks
Petr McBooCzech
>>> step1result = """2000
... 2001
... 2002
... 2003""".splitlines()
>>> step1result
['2000', '2001', '2002', '2003']
>>> step2query = "Prefix " + ",".join(["Case %s" % year for year in 
step1result]) + " Postfix"
>>> step2query
'Prefix Case 2000,Case 2001,Case 2002,Case 2003 Postfix'

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


Re: Popularizing SimpleHTTPServer and CGIHTTPServer

2005-02-04 Thread Michael Spencer
Jorey Bump wrote:
> ... Is there a NotSoSimpleHTTPServer? ...
Steve Holden wrote:
> ... You want ExtremelyBloodyComplicatedHTTPServer  :-)
Lee Harr wrote:
... I think I would point to twisted for that.

Michael :-)

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


Re: returning True, False or None

2005-02-04 Thread Michael Spencer
Steven Bethard wrote:
I have lists containing values that are all either True, False or None, 
e.g.:

[True,  None,  None,  False]
[None,  False, False, None ]
[False, True,  True,  True ]
etc.
For a given list:
* If all values are None, the function should return None.
* If at least one value is True, the function should return True.
* Otherwise, the function should return False.
Right now, my code looks like:
if True in lst:
return True
elif False in lst:
return False
else:
return None
This has a light code smell for me though -- can anyone see a simpler 
way of writing this?

STeVe
max(lst)  ;-)
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Michael Spencer
Bo Peng wrote:
Dear list,
I have many dictionaries with the same set of keys and I would like to 
write a function to calculate something based on these values. For 
example, I have

a = {'x':1, 'y':2}
b = {'x':3, 'y':3}
def fun(dict):
  dict['z'] = dict['x'] + dict['y']
fun(a) and fun(b) will set z in each dictionary as the sum of x and y.
My function and dictionaries are a lot more complicated than these so I 
would like to set dict as the default namespace of fun. Is this 
possible? The ideal code would be:

def fun(dict):
  # set dict as local namespace
  # locals() = dict?
  z = x + y
As you no doubt have discovered from the docs and this group, that isn't doable 
with CPython.

If you must write your functions as real functions, then you might do something 
like this:

 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 ...
 >>> def funa(x,y, **kw):
 ... del kw #Careful of unwanted names in locals with this approach
 ... z = x + y
 ... return locals()
 ...
 >>> a.update(funa(**a))
 >>> b.update(funa(**b))
 >>> a
{'y': 2, 'x': 1, 'z': 3}
 >>> b
{'y': 3, 'x': 3, 'z': 6}
 >>>
Alternatively, you could use exec:
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>> exec "z = x + y" in globals(), a
 >>> a
{'y': 2, 'x': 1, 'z': 3}
 >>> exec "z = x + y" in globals(), b
 >>> b
{'y': 3, 'x': 3, 'z': 6}
 >>>
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Michael Spencer
Bo Peng wrote:
Michael Spencer wrote:
 >
There are hundreds of items in the dictionary (that will be needed in 
the calculation) so passing the whole dictionary is a lot better than 
passing individual items.
...
def fun(d):
  exec 'z = x + y' in globals(), d
seems to be more readable than
def fun(d):
  d['z'] = d['x'] + d['y']
But how severe will the performance penalty be?
Try it and see.
Bo
Compare it with Jeff Shannon's suggestion, and with a lazy dict-wrapper 
like this:
 >>> class wrapbigdict(object):
 ... """Lazy attribute access to dictionary keys.  Will not access
 ... keys that are not valid attribute names!"""
 ... def __init__(self, mydict):
 ... object.__setattr__(self, "mydict",mydict)
 ... def __getattr__(self, attrname):
 ... return self.mydict[attrname]
 ... def __setattr__(self, attrname, value):
 ... self.mydict[attrname] = value
 ...
 ...
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 ...
 >>> w_a = wrapbigdict(a)
 >>> w_b = wrapbigdict(b)
 ...
 >>> def fun(d):
 ... d.z = d.x + d.y
 ...
 >>> fun(w_a)
 >>> fun(w_b)
 ...
 >>> w_a.mydict
{'y': 2, 'x': 1, 'z': 3}
 >>> w_b.mydict
{'y': 3, 'x': 3, 'z': 6}
 >>>
--
http://mail.python.org/mailman/listinfo/python-list


Re: returning True, False or None

2005-02-04 Thread Michael Spencer
Fahri Basegmez wrote:
reduce(lambda x, y: x or y, lst)
works but when I tried
import operator
reduce(operator.or_, lst)
this did not work.  It pukes
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'
Any comments?
Fahri

TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'
operator.or_ is "|" i.e., bitwise, not logical or
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Michael Spencer
Nick Coghlan wrote:
Michael Spencer wrote:
def fun(dict):
  # set dict as local namespace
  # locals() = dict?
  z = x + y

As you no doubt have discovered from the docs and this group, that 
isn't doable with CPython.

Not entirely impossible:
Py> def f(d):
...   exec "locals().update(d)"
...   return x + y
...
Py> f(dict(x=1, y=2))
3
Due to the way 'exec' is implemented, modifications to locals() inside 
an exec statement actually take effect (basically, they're freeloading 
on the code which allows 'exec "x = 1"' to work properly).

This is an evil, evil hack and almost certainly not what anyone should 
be doing. Also, variables created this way will be slower than normal 
variables due to the way the associated code works.

Cheers,
Nick.
Oooh - evil indeed, but thanks for the pointer.
I debated including a link to one of the 'writable locals' threads, when I 
settled on not 'doable', but gambled on being probably useful rather than 
certainly accurate.  Just goes to show you can't get away with anything in this 
NG ;-)

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


Re: returning True, False or None

2005-02-04 Thread Michael Spencer
Fahri Basegmez wrote:
"Michael Spencer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

Fahri Basegmez wrote:
reduce(lambda x, y: x or y, lst)
works but when I tried
import operator
reduce(operator.or_, lst)
this did not work.  It pukes
Traceback (most recent call last):
 File "", line 1, in ?
TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'
Any comments?
Fahri

TypeError: unsupported operand type(s) for |: 'NoneType' and 'bool'
operator.or_ is "|" i.e., bitwise, not logical or
Michael

That explains it.  Is there a logical or we can use with reduce?
Fahri 


Yes, but it's not quite the same as the 'or' operator
 >>> bool.__or__(True, False)
True
 >>> bool.__or__(False, False)
False
 >>> bool.__or__(False, None)
NotImplemented
 >>>
this may not be intentional...
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Michael Spencer
Alex Martelli wrote:
Hmmm, you do realize that wrapdict uses a lot of indirection while my
equivalent approach, just posted, is very direct, right?  To reiterate
the latter, and dress it up nicely too, it's
class wrapwell(object):
def __init__(self, somedict):
self.__dict__ = somedict
Bad mistake on my part, sorry!
Nick Coghlan wrote:
... a class that combined property access with the above...
 
In a similar vein to Nick's solution:
class AutoProperty(object):
def __init__(self, meth):
   self.meth = meth
   self.name = meth.__name__
   self.__doc__ = meth.__doc__
def __get__(self, obj, cls):
if isinstance(obj, cls):
return obj.__dict__.setdefault(self.name, self.meth(obj))
else:
return self.__doc__
# You could define __set__ and __del__ but they don't seem
# necessary based on what you've said so far
class DataManipulator(object):
def __init__(self, data):
self.__dict__ = data
class Model(DataManipulator):
def z(self):
"""x+y"""
return self.x+self.y
z = AutoProperty(z)
def z1(self):
"""Or any other useful information"""
return self.z + self.x
z1 = AutoProperty(z1)
# You could automate these calls to AutoProperty in a metaclass
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>> d = Model(a)
 >>> d.z
3
 >>> d.z1
4
 >>> a
{'y': 2, 'x': 1, 'z': 3, 'z1': 4}
 >>> d=  Model(b)
 >>> d.z1
9
 >>> b
{'y': 3, 'x': 3, 'z': 6, 'z1': 9}
 >>>
Michael
--
http://mail.python.org/mailman/listinfo/python-list


Re: empty classes as c structs?

2005-02-05 Thread Michael Spencer
Alex Martelli wrote:
Nick Coghlan <[EMAIL PROTECTED]> wrote:
   ...
Michael Spencer also posted ...
Wasted indirection, IMHO.  A better implementation:
class attr_view(object):
def __init__(self, data):
self.__dict__ = data
Alex
Indeed!  A complete brain-blip
Michael
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   >