Re: [Tutor] How to write the __str__ function

2017-05-14 Thread shu latif
Not exactly sure what you're trying to do here but maybe you want to join those 
two things instead? Because of course it's a tuple the way you have it now. 

Shu
Sent from my iTypo (with enhanced embarrassing auto-correcting)

> On May 14, 2017, at 11:03 AM, Sydney Shall  wrote:
> 
> I need some advice that I have been embarrased to ask for, because I think 
> that my error is so elementary.
> 
> I have written, as advised by the tutors, a complex program in a topic that 
> interests me. The program works well and the tests are OK.
> 
> Now I want to add a __str__ function, which I thought would be 
> straightforward. But I cannot get it right.
> 
> The code that I have so far is as folows:
> 
> def __str__(self):
>return("\n"
>   "   Output from __str__ of POCWP. "
>   "\n"
>   "\n After the first turnover, during the "
>   "'Population Of Capitals Init' cycle,"
>   "\n the productivities were raised from 1.0 "
>   "\n to a specific Unit Constant Capital (UCC) "
>   "for each specific capital: "
>   "\n The input value for the mean of UCC "
>   "was %7.5f" % (self.ucc),
>   "\n The fractional sigma (FractionalSTD)"
>   " of UCC that was input was %7.5f " % (self.fractsigma_ucc))
> 
> The error message is:
> 
> TypeError: __str__ returned non-string (type tuple)
> 
> When I omit either or both of the objects; self.ucc or self.fractsigma_ucc, 
> the code works fine.
> 
> So, I guess my code has an error. But I cannot detect the error.
> 
> Guidance would be much appreciated.
> 
> -- 
> Sydney
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to sum weighted matrices

2011-03-07 Thread shu wei
Hello all,

I am new to python and numpy.
My question is how to sum up N weighted matrices.
For example w=[1,2] (N=2 case)
m1=[1 2 3,
   3 4 5]

m2=[3 4 5,
   4 5 6]
I want to get a matrix Y=w[1]*m1+w[2]*m2 by using a loop.

My original problem is like this
X=[1 2 3,
 3 4 5,
 4 5 6]

a1=[1 2 3]  1st row of X
m1=a1'*a1 a matirx
a2=[3 4 5] 2nd row of X
m2=a2'*a2
a3=[ 4 5 6] 3rd row of X
m3=a3'*a3

I want to get Y1=w[1]*m1+w[2]*m2
  Y2=w[1]*m2+w[2]*m3
So basically it is rolling and to sum up the weighted matries
I have a big X, the rolling window is relatively small.

I tried to use

sq=np.array([x[i].reshape(-1,1)*x[i] for i in np.arange(0,len(x)]) #
s=len(x)
m=np.array([sq[i:i+t] for i in np.arange(0,s-t+1)]) # t is the len(w)

then I was stuck, I tried to use a loop somethig like
Y=np.array([np.sum(w[i]*m[j,i],axis=0) for i in np.arange(0,t)] )
Any suggestion is welcome.

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


Re: [Tutor] Pyserial and invalid handle

2010-12-22 Thread Cheeng Shu Chin
Hi all,
Last few days ago,
I facing the same issue with my windows 7 64bit...
it work well with windows XP 32-bit...
FYI, I’m using python 2.6 amd64 bit as my core programming tools
currently, I’m start study/learn some arduino robotic and rewrite 
Python-Arduino API
I just do a quick anatomy to the Pyserial code and diff. of the (Ctypes) Handle 
range...
found both are diff range with same Kernel32 dll function call,
for 64-bit, ctypes.wintypes.HANDLE(-1) ==> c_void_p(18446744073709551615L)
in 32 bit, give c_void_p(4294967295L)
so what I guest it cause by Platform and environments issues. 
below is what I did and modifies on 64-bit server:
  a.. win32.py (C:\Python26\Lib\site-packages\serial) 
a.. use HRESULT to replace HANDLE for kernel32 function handle return
  b.. serialwin32.py (C:\Python26\Lib\site-packages\serial) 
a.. use win32file, pywintypes, win32event to rewrite readfile and writefile 
function
it seem work now for my 64-bit server, somehow I didn’t put some effort on 
self.timeout == 0 for serial as I don’t have enough time for it
that I will leave it back to Pyserial Owner to take the honor for that

hope this will help to all open source member... (that why I’m like python most)

Regards,
Cheeng Shu Chin
#! python
# Python Serial Port Extension for Win32, Linux, BSD, Jython
# serial driver for win32
# see __init__.py
#
# (C) 2001-2009 Chris Liechti 
# this is distributed under a free software license, see license.txt
#
# Initial patch to use ctypes by Giovanni Bajo 

import ctypes
import win32
import win32file
import pywintypes
import win32event
#from ctypes.wintypes import HWND
from serialutil import *
#from pywin32_testutil import str2bytes


def device(portnum):
"""Turn a port number into a device name"""
return 'COM%d' % (portnum+1) # numbers are transformed to a string


class Win32Serial(SerialBase):
"""Serial port implementation for Win32 based on ctypes."""

BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 9600, 19200, 38400, 57600, 115200)

def __init__(self, *args, **kwargs):
self.hComPort = None
SerialBase.__init__(self, *args, **kwargs)

def open(self):
"""Open port with current settings. This may throw a SerialException
   if the port cannot be opened."""
if self._port is None:
raise SerialException("Port must be configured before it can be 
used.")
# the "\\.\COMx" format is required for devices other than COM1-COM8
# not all versions of windows seem to support this properly
# so that the first few ports are used with the DOS device name
port = self.portstr
try:
if port.upper().startswith('COM') and int(port[3:]) > 8:
port = '.\\' + port
except ValueError:
# for like COMnotanumber
pass
self.hComPort = win32.CreateFile(port,
   win32.GENERIC_READ | win32.GENERIC_WRITE,
   0, # exclusive access
   None, # no security
   win32.OPEN_EXISTING,
   win32.FILE_ATTRIBUTE_NORMAL | win32.FILE_FLAG_OVERLAPPED,
   0)
#self.pComPort=pywintypes.HANDLE(self.hComPort)
#self.cComPort=HWND(self.hComPort)
#print ctypes.sizeof(self.pComPort)
#print ctypes.sizeof(self.hComPort)
if self.hComPort == win32.INVALID_HANDLE_VALUE:
self.hComPort = None# 'cause __del__ is called anyway
raise SerialException("could not open port %s: %s" % (self.portstr, 
ctypes.WinError()))

# Setup a 4k buffer
win32.SetupComm(self.hComPort, 4096, 4096)

# Save original timeout values:
self._orgTimeouts = win32.COMMTIMEOUTS()
win32.GetCommTimeouts(self.hComPort, ctypes.byref(self._orgTimeouts))

self._rtsState = win32.RTS_CONTROL_ENABLE
self._dtrState = win32.DTR_CONTROL_ENABLE

self._reconfigurePort()

# Clear buffers:
# Remove anything that was there
win32.PurgeComm(self.hComPort,
win32.PURGE_TXCLEAR | win32.PURGE_TXABORT |
win32.PURGE_RXCLEAR | win32.PURGE_RXABORT)

self._overlappedRead = win32.OVERLAPPED()
self._overlappedRead.hEvent = win32.CreateEvent(None, 1, 0, None)
self._overlappedWrite = win32.OVERLAPPED()
#~ self._overlappedWrite.hEvent = win32.CreateEvent(None, 1, 0, None)
self._overlappedWrite.hEvent = win32.CreateEvent(None, 0, 0, None)
self._isOpen = True

def _reconfigurePort(self):
"""Set communication parameters on opened port."""
if not self.hComPort:
raise SerialException(&qu

[Tutor] Getting traceback info from C-API

2010-04-05 Thread Shu
Hi,

I have a CAPI extension module that is giving me MemoryError
exceptions from once in a while with no other information, so clearly
none of my exception handlers are catching it. Is there any way I can
traceback information for the extension module?

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