Signals and Slots - Summerfield - what exactly is a signal?

2017-08-05 Thread veek
1. What exactly is a signal. In hardware, an interrupt can be viewed as a 
signal and the voltage on a pin will suddenly jump to +5V as an indicator 
that an interrupt has occurred. With Qt signals - if a widget-c++ code has 
to 'signal' an event - what does it do?

As a consequence of not understanding the above..

2. How do you connect a single signal to multiple slots? Do the 
slots/methods get called one by one? or are they treated as independent 
threads and run on different cores?

3. pg 130 of Summerfield

class ZeroSpinBox(QObject):
   def __init__(self, parent=None):
  super(...blah initialize QObject with parent
  self.connect(self, SIGNAL("valuedChanged(int)"), self.checkzero)

   def checkzero(self):
  if self.value() == 0:
 self.emit(SIGNAL("atzero"), self.zeros)

basically when SpinBox gets set to zero we emit 'atzero' and return a zeros 
counter.

What i don't get is the valueChanged(int) bit.. he clearly defines an (int) 
being passed to checkzero so shouldn't the definition reflect the same? 
Mistake?

A. additionally I did not understand short circuit signals where you can
drop the (type1, type2) in the signature and just do
SIGNAL("atzero")

Is this doable ONLY in emit() or also in connect()??


4. pg 131 'any data can be passed as arguments to the emit method and they 
can be passed as Python objects'

self.emit(SIGNAL("atzero"), 10, 20, 30) or 
self.emit(SIGNAL("atzero"), [10, 20, 30])
does he mean i can pass a SINGLE OBJECT of any python type or does he mean 
multiple independent containers?
(assuming the definition matches)

5. He says 'a signal with one argument is a Qt signal or a non-short-circuit 
Python signal' 

So if I have a Qt widget in C++ and it emits a valueChanged(int) signal.. 
okay he's going to have to convert our asm/x86 int to python-integer and 
then call the python-method that was mapped to 'valueChanged' with the 
python-integer argument OR the signal is being generated from Python code 
itself AND WILL be converted to C++ datatype - huh why??

I'm guessing there's some dispatch code that maintains a mapping table?? SO 
why can't it differentiate between inhouse python widget and Qt-c++ widget??


6. "If we use the SIGNAL() function with a signalSignature (a possibly empty 
parenthesized list of comma-separated PyQt types)..."??

how can it be an empty () list..?? If there's a list it's no longer empty..

7. What exactly is a Python signal vs a Qt signal originating from a C++-
asm-widget?? PyQt is a wrapper around C++Qt so.. where are the python 
widgets??

8. 
class TaxRate(QObject):
  def __init__(self):
super(TaxRate, self).__init__()
self.__rate = 17.5

  def rate(self):
return self.__rate

  def setRate(self, rate):
if rate != self.__rate:
   self.__rate = rate
   self.emit(SIGNAL("rateChanged"), self.__rate)

def rateChanged(value):
print "TaxRate changed to %.2f%%" % value

vat = TaxRate()
vat.connect(vat, SIGNAL("rateChanged"), rateChanged)
vat.setRate(17.5) # No change will occur (new rate is the same)
vat.setRate(8.5) # A change will occur (new rate is different)

Isn't this a mistake? self.connect( should be:
vat.connect(vat, SIGNAL("rateChanged"), rate)




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


Re: Signals and Slots - Summerfield - what exactly is a signal?

2017-08-05 Thread veek
Steve D'Aprano wrote:

> On Sun, 6 Aug 2017 12:28 am, veek wrote:
> 
>> 1. What exactly is a signal. In hardware, an interrupt can be viewed as a
>> signal and the voltage on a pin will suddenly jump to +5V as an indicator
>> that an interrupt has occurred. With Qt signals - if a widget-c++ code
>> has to 'signal' an event - what does it do?
> 
> I don't know what Qt signals are.
> 
> Unix signals are an interrupt mechanism used by Unix and Linux operating
> systems. Windows may support them as well. The "kill" system command sends
> signals to a specific running process, which will then jump to a specific
> piece of code. The command is called "kill" because the main use for this
> is to terminate processes.
> 
> Python supports these signals:
> 
> https://docs.python.org/3/library/signal.html
> 
> https://pymotw.com/3/signal/index.html
> 
> 
>  
>> As a consequence of not understanding the above..
>> 
>> 2. How do you connect a single signal to multiple slots? Do the
>> slots/methods get called one by one? or are they treated as independent
>> threads and run on different cores?
> 
> What are slots?
> 
> 
>> 3. pg 130 of Summerfield
> 
> What is Summerfield?
> 
> 
> 

The book I'm reading is: Mark Summerfield - Rapid GUI Programming with 
Python and Qt.

PyQt is a wrapper around the C++/Qt library and they have a Signal and Slot 
mechanism for doing GUI/event-driven work - Qt doesn't have it.

Basically if you have a widget like QSpinBox it'll be rendered/drawn using a 
C++ Qt library (which is fast). 

Anyway, widgets support 'events' like mouse-over  and click and they are 
constantly generating events which are mostly ignored by the PyQt library 
and discarded. However if you decide you want to handle an event then you 
use PyQt's signal/slot mechanism: slot's a method and signal looks to be 
basically a string.

Eg:

class Form(QWidget):

  def __init__(self, parent=None):
 super(Form, self).__init__(parent) #init QWidget with parent is set
 combo = QComboBox()
 self.connect(combo, SIGNAL("some_event(int)"),self.combo_event_handler)

  def combo_event_handler(self, i):
 pass

combo_event_handler's the slot.. it's just a python OR Qt method.
QComboBox() will result in a combo box being drawn when the app is started 
via 
app = QApplication()
app.exec_()

then if I mouse over the widget it'll generate an 'event'/'signal'
and call the handler to process - event driven programming

"some_event()" is a string being passed to connect() and that's a function 
signature that tells connect what types the handler expects. 
combo_event_handler can be a Qt method in which case conversion occurs to 
C++ data types but it can also be a python method and if you short ckt then 
no conversion to C++ types occurs.
-- 
https://mail.python.org/mailman/listinfo/python-list


PyQt: viewport vs window - how do you translate co-ordinates?

2017-09-23 Thread Veek M
pg 329, Rapid GUI Programming

http://storage4.static.itmages.com/i/17/0923/h_1506165624_2588733_59fdfcd4cc.png

In PyQt terminology the physical coordinate system is called the “viewport”,
and confusingly, the logical coordinate system is called the “window”.

In Figure 11.4, we have a physical widget size of 800 × 600. By calling setWin-
dow(-60, -60, 120, 120) we can create a “window” with a top-left coordinate of
(-60, -60), a width of 120, a height of 120, and centered at point (0, 0). The
window’s coordinate system is a logical coordinate system that QPainter auto-
matically maps to the underlying physical device. After the setWindow() call,
all our painting takes place using the logical (window) coordinate system.
In this case, the widget is rectangular, but our window has the same width and
height. This means that the items we paint will be stretched out horizontally,
since coordinates in the y-axis will be scaled by QPainter in the ratio 120:600
(1:5), whereas those in the x-axis will be scaled in the ratio 120:800 (1:6 2
3
).



1. The physical (coordinate system) widget size is 800x600 = Viewport
2. Logical Coordinates = Window

When he does setWindow(-60, -60, 120, 120) he's moving along a diagonal from 
top-left to bottom-right. From that point he's then drawing a 120x120 rectangle.

A. So how does he get a (150,200) point and a -37.5 point??? 
B. Also, how is it stretched horizontally? If i use the inner 120x120 as a 
frame of reference and draw a circle - it'll still be a circle??? Just draw a 
square on a blackboard.. and then draw a circle in it..

I didn't understand any of that - could someone expand on that para?

C. Why does QPainter have to scale in a different ratio??
QPainter understands two coordinate systems - physical and logical and by 
default the two systems match each other.

Is there a reading resource that explains the Viewport and translations? I am 
not a CS student so I did not study computer graphics.


(sorry for using google groups but my knode is broken and pan is horrible on 
olvwm - my plan is to write myself a News client in PyQt :))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyQt: viewport vs window - how do you translate co-ordinates?

2017-09-24 Thread Veek M
On Saturday, September 23, 2017 at 8:44:25 PM UTC+5:30, Michael Torrie wrote:
> On 09/23/2017 05:38 AM, Veek M wrote:
> > I didn't understand any of that - could someone expand on that para?
> > Is there a reading resource that explains the Viewport and translations? I 
> > am not a CS student so I did not study computer graphics.
> 
> I'm sure there are lots of things that might help. This is primarily
> about OpenGL but the principles may apply:
> https://www.cs.mtsu.edu/~jhankins/files/4250/notes/WinToView/WinToViewMap.html
> 
> Alternatively, you can think it through.  Imagine you're laying out a
> fence around your house. You could use global coordinates for
> everything, or you could just call the exact center of your yard 0,0 and
> measure everything from there, in whatever units you wish. That's the
> idea here. Rather than referring to pixels by their screen coordinates
> (which can change if it moves around the screen), you refer to them by
> an arbitrary coordinate system with it's own scaling relative to the
> screen region.  You could have a screen region that is 100 px by 100 px,
> and the you designate that the upper left corner of that region is
> -500,500 and the lower right corner is 500,-500. Or it could be
> something like -500,200 to 500,-200, which would make things stretched
> out in the x axis compared to the y axis.
> 
> > (sorry for using google groups but my knode is broken and pan is horrible 
> > on olvwm - my plan is to write myself a News client in PyQt :))
> 
> I don't think you need to know that much about graphics coordinate
> systems to get started with Qt. Most GUI layout stuff does not depend on
> pixels or coordinates.

I took a look at the: OpenGL SuperBible - 7e - it's heavy on doing, and not 
much theory; they have a section on Views. Also, read that link. The link has 
some mistakes I suspect..

Let's say we have a teacup-photograph and a table in a 3d-room. The photo has a 
local space - called Model Space or Object Space. The edges of the photo will 
form a coordinate system and using that i could make the teacup handle red.

If i take that photo and place it on top of that table - i get a World Space 
that contains both objects and the walls of the room form a coordinate system. 
(this is where that link is wrong)

Now let's say I want to display the room on a computer screen.

The entire computer screen forms a Screen coordinate system and will include 
the Dolphin file browser, maybe firefox, the kde toolbar)

However, I can't directly take a 3d room and stick it in my 2d screen so, I 
have to capture part of the room from a PERSPECTIVE - this is 2d because the 
camera film is 2d - so, this is a View Space/Clipping Window (link/url).

Now the View Space data that I captured has to be placed on the screen BUT not 
everywhere - this is the Interface Window (link/url).

However I may want to caption the room-image-displayed-on-screen so the 
Interface Window is further shrunk into a Viewport which represents 
pixels/points on the device/paper.

Transformation is a matrix associated with a space that will give you the 
desired pixels in the Viewport.

View Space is how this World Space looks when viewed
---

So how does the above fit into PyQt/book-description.

We have a Widget that is drawn in pixels/picas/whatever that has a 
physical-coordinate system associated with it (800x600 and will prolly fill the 
entire computer-screen). 

We have a window which is at (-60, -60) + 120 length&breadth - so you have a 
small rectangle in which to draw - equivalent to the ViewSpace however since 
this is 2d, it can also be equivalent to the Model/Object Space - with some 
random center (0,0) could be the teacup-handle.

The aspect ratio for widget is 1.
aspect ratio for window is 1

If the aspect ratios were the same there would be no problem - however since 
the aspect ratios are different, x-axis scaling is 120:800 and y-axis is 
120:600. We need to bear this in mind when we draw in our little window because 
what looks proportional/linear will be deformed when placed on screen/viewport.

So if we badly want a square window, we can create a square Screen/Viewport and 
loose some drawing area as a result of the viewport being squared from a 
rectangle.
--

Phew! anyway this is just my longwinded way but I think I got it. Thanks :)





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


PyQt: Parenting a Widget

2017-09-25 Thread Veek M
Summary: Could someone explain widget and dialog parenting - the text book is 
not making sense.
##
I'm trying to understand widget parenting, from the book: Rapid GUI 
Programming, pg 118, and thereabouts - he says:

A. All PyQt classes that derive from QObjectand this includes all the widgets,
since QWidget is a QObject subclasscan have a “parent”.

B. PyQt automatically repar-
ents the widgets that are laid out. So although we did not give our widgets a
parent of self (the Form instance),when we call setLayout() the layout manager
gives ownership of the widgets and of itself to the form,and takes ownership of
any nested layouts itself. This means that none of the widgets that are laid out
is a top-level window, and all of them have parents, which is what we want. So
when the form is deleted, all its child widgets and layouts will be deleted with
-
1. In A, does he mean, you are ALLOWED to set a parent on a widget ONLY because 
its Base Class is QObject? 

With DockWidgets, you have to explicitly parent them - why?

2. If I create two widgets and wdget.show() them, and app.exec_() - which one 
becomes the main-window and which one is the memory leak? I have not used a 
layout manager so, one widget with no parent auto-becomes the main-window (as 
per B), which would result in a leak with the other?

#!/usr/bin/python

import sys, os, re


from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

lbl = QLabel('Hello World')
lbl.setWindowFlags(Qt.SplashScreen)
lbl.show()
txtBrw = QTextBrowser()
txtBrw.show()

QTimer.singleShot(3000, app.quit)
app.exec_()

3. QObject --> QWidget --> QDialog --> Form --> Form_Layout_Manager --> 
Nested_Layout_Manager

B, says that the layout manager parents the widgets under it and makes 'Form' 
the parent. If the Form Layout Manager is taking charge of the Nested Layout 
Manager, who is the parent of the widgets under the Nested Layout Mangaer? 

4. In the Chapter on 'Dialogs', I am trying to create a QMainWindow Style 
application with one label as the central widget and one button to invoke a 
dialog. It doesn't work and I get:

QWidget::setLayout: Attempting to set QLayout "" on Parent "", which already 
has a layout

I tried this link and it made no sense:
https://stackoverflow.com/questions/25450598/qlayout-attempting-to-add-qlayout-to-qwidget-which-already-has-a-layout

How does parenting work in PyQt? What is autoparented, what needs to be 
explicitly parented and who is scrwing whom? Additionally, why is my button, 
hiding?


#!/usr/bin/python

import sys, os, re


from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)

class Dialog(QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)

self.lbl = QLabel('Width: ')
self.spn = QSpinBox()
self.spn.setRange(0, 100)
self.lbl.setBuddy(self.spn)

self.chk = QCheckBox('&Beveled Edges')

self.lbl_styl = QLabel('Style')
self.lst = QComboBox()
self.lst.addItems(['dashed', 'dotted', 'star'])
self.lbl_styl.setBuddy(self.lst)

self.ok_btn = QPushButton('&Ok')
self.cncl_btn = QPushButton('&Cancel')

self.layout([(self.lbl, 0, 0), (self.spn, 0, 1), (self.chk, 0, 2), 
 (self.lbl_styl, 1, 0), (self.lst, 1, 1), 
 (self.ok_btn, 2, 0), (self.cncl_btn, 2, 1)])

def layout(self, wgts = []):
self.lyt = QGridLayout()
for wgt, row, col in wgts:
self.lyt.addWidget(wgt, row, col)

self.setLayout(self.lyt)


class Parent(QMainWindow):
def __init__(self, parent = None):
super(Parent, self).__init__(parent)

lbl = QLabel('HELLO WORLD')
btn = QPushButton('&Start Dialog')
lbl.setBuddy(btn)

lyt = QHBoxLayout()
lyt.addWidget(lbl)
lyt.addWidget(btn)

self.setLayout(lyt)
self.connect(btn, SIGNAL('clicked()'), self.popup_dialog)

def popup_dialog(self):
x = Dialog(self)
if x.exec_():
print(x.spn.value())

p = Parent()
p.show()

app.exec_()


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


Re: PyQt: Parenting a Widget

2017-09-25 Thread Veek M
On Tuesday, September 26, 2017 at 11:18:54 AM UTC+5:30, Veek M wrote:
> Summary: Could someone explain widget and dialog parenting - the text book is 
> not making sense.
> ##
> I'm trying to understand widget parenting, from the book: Rapid GUI 
> Programming, pg 118, and thereabouts - he says:
> 
> A. All PyQt classes that derive from QObjectand this includes all the widgets,
> since QWidget is a QObject subclasscan have a “parent”.
> 
> B. PyQt automatically repar-
> ents the widgets that are laid out. So although we did not give our widgets a
> parent of self (the Form instance),when we call setLayout() the layout manager
> gives ownership of the widgets and of itself to the form,and takes ownership 
> of
> any nested layouts itself. This means that none of the widgets that are laid 
> out
> is a top-level window, and all of them have parents, which is what we want. So
> when the form is deleted, all its child widgets and layouts will be deleted 
> with
> -
> 1. In A, does he mean, you are ALLOWED to set a parent on a widget ONLY 
> because its Base Class is QObject? 
> 
> With DockWidgets, you have to explicitly parent them - why?
> 
> 2. If I create two widgets and wdget.show() them, and app.exec_() - which one 
> becomes the main-window and which one is the memory leak? I have not used a 
> layout manager so, one widget with no parent auto-becomes the main-window (as 
> per B), which would result in a leak with the other?
> 
> #!/usr/bin/python
> 
> import sys, os, re
> 
> 
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> app = QApplication(sys.argv)
> 
> lbl = QLabel('Hello World')
> lbl.setWindowFlags(Qt.SplashScreen)
> lbl.show()
> txtBrw = QTextBrowser()
> txtBrw.show()
> 
> QTimer.singleShot(3000, app.quit)
> app.exec_()
> 
> 3. QObject --> QWidget --> QDialog --> Form --> Form_Layout_Manager --> 
> Nested_Layout_Manager
> 
> B, says that the layout manager parents the widgets under it and makes 'Form' 
> the parent. If the Form Layout Manager is taking charge of the Nested Layout 
> Manager, who is the parent of the widgets under the Nested Layout Mangaer? 
> 
> 4. In the Chapter on 'Dialogs', I am trying to create a QMainWindow Style 
> application with one label as the central widget and one button to invoke a 
> dialog. It doesn't work and I get:
> 
> QWidget::setLayout: Attempting to set QLayout "" on Parent "", which already 
> has a layout
> 
> I tried this link and it made no sense:
> https://stackoverflow.com/questions/25450598/qlayout-attempting-to-add-qlayout-to-qwidget-which-already-has-a-layout
> 
> How does parenting work in PyQt? What is autoparented, what needs to be 
> explicitly parented and who is scrwing whom? Additionally, why is my button, 
> hiding?
> 
> 
> #!/usr/bin/python
> 
> import sys, os, re
> 
> 
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> app = QApplication(sys.argv)
> 
> class Dialog(QDialog):
> def __init__(self, parent = None):
> super(Dialog, self).__init__(parent)
> 
> self.lbl = QLabel('Width: ')
> self.spn = QSpinBox()
> self.spn.setRange(0, 100)
> self.lbl.setBuddy(self.spn)
> 
> self.chk = QCheckBox('&Beveled Edges')
> 
> self.lbl_styl = QLabel('Style')
> self.lst = QComboBox()
> self.lst.addItems(['dashed', 'dotted', 'star'])
> self.lbl_styl.setBuddy(self.lst)
> 
> self.ok_btn = QPushButton('&Ok')
> self.cncl_btn = QPushButton('&Cancel')
> 
> self.layout([(self.lbl, 0, 0), (self.spn, 0, 1), (self.chk, 0, 2), 
>  (self.lbl_styl, 1, 0), (self.lst, 1, 1), 
>  (self.ok_btn, 2, 0), (self.cncl_btn, 2, 1)])
> 
> def layout(self, wgts = []):
> self.lyt = QGridLayout()
> for wgt, row, col in wgts:
> self.lyt.addWidget(wgt, row, col)
> 
> self.setLayout(self.lyt)
> 
> 
> class Parent(QMainWindow):
> def __init__(self, parent = None):
> super(Parent, self).__init__(parent)
> 
> lbl = QLabel('HELLO WORLD')
> btn = QPushButton('&Start Dialog')
> lbl.setBuddy(btn)
> 
> lyt = QHBoxLayout()
> lyt.addWidget(lbl)
> lyt.addWidget(btn)
> 
> self.setLayout(lyt)
> self.connect(btn, SIGNAL('clicked()'

Re: PyQt: Parenting a Widget

2017-09-26 Thread Veek M
On Tuesday, September 26, 2017 at 2:23:22 PM UTC+5:30, Thomas Jollans wrote:
> On 2017-09-26 08:16, Veek M wrote:
> > On Tuesday, September 26, 2017 at 11:18:54 AM UTC+5:30, Veek M wrote:
> >> Summary: Could someone explain widget and dialog parenting - the text book 
> >> is not making sense.
> >> ##
> >> I'm trying to understand widget parenting, from the book: Rapid GUI 
> >> Programming, pg 118, and thereabouts - he says:
> >>
> >> A. All PyQt classes that derive from QObjectand this includes all the 
> >> widgets,
> >> since QWidget is a QObject subclasscan have a “parent”.
> >>
> >> B. PyQt automatically repar-
> >> ents the widgets that are laid out. So although we did not give our 
> >> widgets a
> >> parent of self (the Form instance),when we call setLayout() the layout 
> >> manager
> >> gives ownership of the widgets and of itself to the form,and takes 
> >> ownership of
> >> any nested layouts itself. This means that none of the widgets that are 
> >> laid out
> >> is a top-level window, and all of them have parents, which is what we 
> >> want. So
> >> when the form is deleted, all its child widgets and layouts will be 
> >> deleted with
> >> -
> >> 1. In A, does he mean, you are ALLOWED to set a parent on a widget ONLY 
> >> because its Base Class is QObject? 
> >>
> >> With DockWidgets, you have to explicitly parent them - why?
> >>
> >> 2. If I create two widgets and wdget.show() them, and app.exec_() - which 
> >> one becomes the main-window and which one is the memory leak? I have not 
> >> used a layout manager so, one widget with no parent auto-becomes the 
> >> main-window (as per B), which would result in a leak with the other?
> >>
> >> #!/usr/bin/python
> >>
> >> import sys, os, re
> >>
> >>
> >> from PyQt4.QtCore import *
> >> from PyQt4.QtGui import *
> >>
> >> app = QApplication(sys.argv)
> >>
> >> lbl = QLabel('Hello World')
> >> lbl.setWindowFlags(Qt.SplashScreen)
> >> lbl.show()
> >> txtBrw = QTextBrowser()
> >> txtBrw.show()
> >>
> >> QTimer.singleShot(3000, app.quit)
> >> app.exec_()
> >>
> >> 3. QObject --> QWidget --> QDialog --> Form --> Form_Layout_Manager --> 
> >> Nested_Layout_Manager
> >>
> >> B, says that the layout manager parents the widgets under it and makes 
> >> 'Form' the parent. If the Form Layout Manager is taking charge of the 
> >> Nested Layout Manager, who is the parent of the widgets under the Nested 
> >> Layout Mangaer? 
> >>
> >> 4. In the Chapter on 'Dialogs', I am trying to create a QMainWindow Style 
> >> application with one label as the central widget and one button to invoke 
> >> a dialog. It doesn't work and I get:
> >>
> >> QWidget::setLayout: Attempting to set QLayout "" on Parent "", which 
> >> already has a layout
> >>
> >> I tried this link and it made no sense:
> >> https://stackoverflow.com/questions/25450598/qlayout-attempting-to-add-qlayout-to-qwidget-which-already-has-a-layout
> >>
> >> How does parenting work in PyQt? What is autoparented, what needs to be 
> >> explicitly parented and who is scrwing whom? Additionally, why is my 
> >> button, hiding?
> >>
> >>
> >> #!/usr/bin/python
> >>
> >> import sys, os, re
> >>
> >>
> >> from PyQt4.QtCore import *
> >> from PyQt4.QtGui import *
> >>
> >> app = QApplication(sys.argv)
> >>
> >> class Dialog(QDialog):
> >> def __init__(self, parent = None):
> >> super(Dialog, self).__init__(parent)
> >> 
> >> self.lbl = QLabel('Width: ')
> >> self.spn = QSpinBox()
> >> self.spn.setRange(0, 100)
> >> self.lbl.setBuddy(self.spn)
> >> 
> >> self.chk = QCheckBox('&Beveled Edges')
> >>
> >> self.lbl_styl = QLabel('Style')
> >> self.lst = QComboBox()
> >> self.lst.addItems(['dashed', 'dotted', 'star'])
> >> self.lbl_styl.setBuddy(self.lst)
> >> 
> >> self.ok_btn = QPushButton('

Pandas: How does df.apply(lambda work to create a result

2021-05-26 Thread Veek M
t = pd.DataFrame([[4,9],]*3, columns=['a', 'b'])
   a  b
0  4  9
1  4  9
2  4  9

t.apply(lambda x: [x]) gives
a[[1, 2, 2]]
b[[1, 2, 2]]
How?? When you 't' within console the entire data frame is dumped but how are 
the individual elements passed into .apply()? I can't do lambda x,y: [x,y] 
because only 1 arg is passed so how does [4] generate [[ ]]

Also - this:
 t.apply(lambda x: [x], axis=1)
0[[139835521287488, 139835521287488]]
1[[139835521287488, 139835521287488]]
2[[139835521287488, 139835521287488]]
vey weird - what just happened??

In addition, how do I filter data eg:  t[x] = t[x].apply(lambda x: x*72.72) I'd 
like to remove numeric -1 contained in the column output of t[x]. 'filter' only 
works with labels of indices, i can't do t[ t[x] != -1 ] because that will then 
generate all the rows and I have no idea how that will be translate to 
within a .apply(lambda x... (hence my Q on what's going on internally)

Could someone clarify please.

(could someone also tell me briefly the best way to use NNTP and filter
out the SPAM - 'pan' and 'tin' don't work anymore afaik
[eternal-september]  and I'm using slrn currently - the SLang regex is 
weird within the kill file - couldn't get it to work - wound up killing
everything when I did
Subject: [A-Z][A-Z][A-Z]+
)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Selenium script - stuck - could someone take a look?

2021-05-29 Thread Veek M
On 2021-05-29, Veek M  wrote:

fixed div './/' vs '//'
-- 
https://mail.python.org/mailman/listinfo/python-list


Selenium script - stuck - could someone take a look?

2021-05-29 Thread Veek M


Script: http://paste.debian.net/1199271/

It mostly works but line 78 is supposed to extract 
100 pieces / lot No matter what I
try it's failed and I DON'T KNOW WHY? It's a simple div.classname
match..

Could someone take a look and figure it out - I'm stuck.




import re, sys, time

from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

url = 'https://www.aliexpress.com'

caps = DesiredCapabilities().FIREFOX; caps["pageLoadStrategy"] = 'eager' 
ignored_exceptions=(NoSuchElementException,StaleElementReferenceException,)

fh = open('/tmp/log.html', 'w')
fh.write(' parts\n\n')


def convert(m):
money = m.group()
return str(round(float(money) * 72.4, 3))

import re
def process_fields(txt):
if '$' in txt:
txt = txt.replace('+', '')
txt = txt.replace('$', '')
txt = txt.replace('US', '')
txt = txt.replace('Shipping:', '')
r = re.sub(r'(\s*[0-9]+\.[0-9]+)', convert, txt)
return str(r)

def ali_search(url, txt):
driver.get(url)
assert 'AliExpress' in driver.title

try:
srch_elem = WebDriverWait(driver, 3600, 
ignored_exceptions=ignored_exceptions).until( 
EC.presence_of_element_located((By.XPATH, '//div[@class="search-key-box"]')))
print('search')
x = driver.find_element_by_id('search-key')
if 'input' in x.tag_name:
print 'success'
finally:
for c in list(txt):
time.sleep(1)
x.send_keys(c)
x.send_keys(Keys.RETURN)

try:
element = WebDriverWait(driver, 3600, 
ignored_exceptions=ignored_exceptions).until( 
EC.presence_of_element_located((By.XPATH, '//div[@class="product-container"]')))
finally:
print('product-container')
x = driver.find_element_by_xpath('//body')
x.send_keys(Keys.HOME)

for i in range(1,10):
print('send END')
time.sleep(1)
x.send_keys(Keys.PAGE_DOWN)
time.sleep(1)
#driver.execute_script("window.scrollTo(0, 
document.body.scrollHeight);")


   # EC.presence_of_element_located((By.XPATH, '//div[contains(@class, " 
product-list")]')))
divs = element.find_elements_by_xpath('//li[@class="list-item 
packaging_sale"]')
for c, div in enumerate(divs):
fh.write('')
for param in ['price-current', 'item-price-row packaging-sale', 
'shipping-value', 'store-name']:
try:
if 'store' in param:
fh.write('' + 
div.find_elements_by_class_name(param)[0].text + '')
elif 'sale' in param:
print param
lot = div.find_elements_by_class_name(param)
fh.write('' + str(lot) + '')
else:
fh.write('' + 
process_fields(div.find_elements_by_class_name(param).text) + '')
except Exception as e:
fh.write('' + str(e) + '')
fh.write('\n')
fh.write('\n')
fh.close()


def part_lookup():
global driver
with webdriver.Firefox(executable_path=r'/mnt/sdb1/root/geckodriver', 
firefox_binary='/mnt/sdb1/firefox/firefox-bin', capabilities=caps) as driver:
if len(sys.argv) == 2:
ali_search(url, sys.argv[1])
time.sleep(3600)

part_lookup()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Selenium script - stuck - could someone take a look?

2021-05-29 Thread Veek M
On 2021-05-29, Dennis Lee Bieber  wrote:
> On Sat, 29 May 2021 09:40:35 - (UTC), Veek M  declaimed
> the following:
>

ah, yeah - man that took me a while to do (save to local file and use
file:///). It's working now, basically xpath mistake because I've
forgotten stuff.. but the script is almost complete and cleaned up..
Basically forgot that div.xpath('// doesn't use div as the root window -
thing starts the lookup from the very beginning. You got to .//

My "Next Page" is creating problems - asked here as well:
https://www.reddit.com/r/selenium/comments/nnrxuu/
what_is_overlay_backdrop_how_does_it_block_a/

def page_next():
tmp = driver.find_element_by_xpath('//button[contains(@class, 
" next-next")]')
tmp = driver.find_element_by_xpath('.//div[@class=
"next-overlay-backdrop")]')
#e = WebDriverWait(driver, 200).until(EC.element_to_be_clickable(
#(By.XPATH, "//button[contains(@class, ' next-next']"))).click()
#e.click()

if tmp: tmp.click()
else: raise SystemExit('no next')

 This is the error I get
 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: 
Element  is not clickable at 
point (699,649) because another element  
obscures it

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


Tree library that allows conditions on nodes and will return the node

2021-06-14 Thread Veek M
LibreOffice has a huge class tree and I need to familiarize myself with
it - trouble is, it won't fit on A4 because it has a flat hierarchy with
loads of leaf nodes.

I wanted to shove all the leaf nodes > x into a subgraph and style that
differently using Graphviz.

I tried treelib but while i can build and print a tree, AND figure out
the size for each level of the Tree, it won't give me the parent node
for a particular size.. So I know that level 3 has 7 children too much
but which is the jackass parentID responsible - no idea.

Graphviz has nice plotting and subgraphs and such but same problem - I
checked pydot which is a clone of Graphviz and I'm not sure what exactly
it does different from Graphviz..

Anyway I don't want to spend another two days checking out Plotly or
Pygraphviz.. can someone point me in the right direction?
-- 
https://mail.python.org/mailman/listinfo/python-list


Extend unicodedata with a name/pattern/regex search for character entity references?

2016-09-03 Thread Veek. M
https://mail.python.org/pipermail//python-ideas/2014-October/029630.htm

Wanted to know if the above link idea, had been implemented and if 
there's a module that accepts a pattern like 'cap' and give you all the 
instances of unicode 'CAP' characters.
 ⋂ \bigcap
 ⊓ \sqcap
 ∩ \cap
 ♑ \capricornus
 ⪸ \succapprox
 ⪷ \precapprox

(above's from tex)

I found two useful modules in this regard: unicode_tex, unicodedata
but unicodedata is a builtin which does not do globs, regexs - so it's 
kind of limiting in nature.

Would be nice if you could search html/xml character entity references 
as well.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extend unicodedata with a name/pattern/regex search for character entity references?

2016-09-03 Thread Veek. M
Thomas 'PointedEars' Lahn wrote:

> Veek. M wrote:
> 
>> https://mail.python.org/pipermail//python-ideas/2014-October/029630.htm
>> 
>> Wanted to know if the above link idea,
> 
> … which is 404-compliant; the Internet Archive does not have it either
> …
> 
>> had been implemented
> 
> Probably not.
> 
>> and if there's a module that accepts a pattern like 'cap' and give
>> you all the instances of unicode 'CAP' characters.
> 
> I do not know any.
> 
>>  ⋂ \bigcap
>>  ⊓ \sqcap
>>  ∩ \cap
>>  ♑ \capricornus
>>  ⪸ \succapprox
>>  ⪷ \precapprox
>> 
>> (above's from tex)
>> 
>> I found two useful modules in this regard: unicode_tex, unicodedata
>> but unicodedata is a builtin which does not do globs, regexs - so
>> it's kind of limiting in nature.
> 
> Quick hack:
> 
> #
> from unicode_tex import unicode_to_tex_map
> 
> for key, value \
> in filter(lambda item: "cap" in item[1], unicode_to_tex_map.items()):
> print(key, value)
> #
> 
> (Optimizations are welcome.)
> 
> It is easy to come up with methods that take a globbing or a regular
> expression (globbing expressions can be turned into regular
> expressions easily) and returns, perhaps as a dictionary or list of
> tuples, only the matching entries.
> 
> Other than that I think you will have to turn the Unicode Character
> Database (which is available via HTTP as one huge text file; see the
> Python Tutorial on “Internet Access” for how to get it dynamically)
> into whatever form suits you for querying it.
>  
>> Would be nice if you could search html/xml character entity
>> references as well.
> 
> For what purpose?
> 
> Your posting is lacking a real name in the “From” header field.
> 

Ouch! Sorry for the bad link Thomas. The link is titled '[Python-ideas] 
Extend unicodedata with a name search' and I suspect this updated link 
data (http://code.activestate.com/lists/python-ideas/29504/) may work - 
if it doesn't you could google the title.

I don't want to dump/replicate the existing Unicode data in module 
'unicodedata'.

Regarding purpose, well I need this for hexchat. I IRC a lot and often, 
I want to ask a question involving math symbols. I've written some 
python (included at the bottom) that translates:
 \help filter_word #into a list of symbols and names 
(serves as a memory jog). It also translates stuff like:
 A \cap B \epsilon C to A ∩ B ε C.
but all this works with a subset of tex - it can't do complicated 
formula. I wanted to extend it further.. I don't think I shall be able 
to subscript integrals easily but I could make better use of the 
available unicode, which means making it more accessible (hence the 
pattern matching feature) - html/xml entities provide a new way of 
remembering stuff.

---
Regarding the name (From field), my name *is* Veek.M though I tend to 
shorten it to Vek.M on Google (i think Veek was taken or some such 
thing). Just to be clear, my parents call me something closely related 
to Veek that is NOT Beek or Peek or Squeak or Sneak and my official name 
is something really weird. Identity theft being what it is, I probably 
am lying anyhow about all this, but it sounds funny so :p

import hexchat
import re, unicode_tex, unicodedata

__module_name__ = 'Unicode'
__module_version__ = '0.1'
__module_description__ = 'Substitute \whatever with Unicode char in 
cmdline input'

#re_repl = unicodedata.lookup('N-ARY UNION')

def debug(*args):
hexchat.prnt('#{}#'.format(*args))

def print_help(*args):
hexchat.prnt('{}'.format(*args))

def send_message(word, word_eol, userdata):
if not(word[0] == "65293"):
return

msg = hexchat.get_info('inputbox')
if msg is None:
return

x = re.match(r'(^\\help)\s+(\w+)', msg)
if x:
filter = x.groups()[1]
for key, value in unicode_tex.tex_to_unicode_map.items():
if filter in key:
print_help(value + ' ' + key)
hexchat.command("settext %s" % '')
return

tex_matches = re.findall(r'(\\\w+)', msg)
for tex_word in tex_matches:
repl = unicode_tex.tex_to_unicode_map.get(tex_word)

if repl is None:
repl = 'err'

msg = re.sub(re.escape(tex_word), repl, msg)

hexchat.command("settext %s" % msg)

hexchat.hook_print('Key Press', send_message)


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


Re: Extend unicodedata with a name/pattern/regex search for character entity references?

2016-09-04 Thread Veek. M
Steve D'Aprano wrote:

> On Sun, 4 Sep 2016 06:53 pm, Thomas 'PointedEars' Lahn wrote:
> 
>>> Regarding the name (From field), my name *is* Veek.M […]
>> 
>> Liar.  *plonk*
> 
> You have crossed a line now Thomas.
> 
> That is absolutely uncalled for. You have absolutely no legitimate
> reason to believe that Veek is not his or her real name.
> 
> You owe Veek an apology, and a promise to the entire community that
> you will not act in such a bigoted, racist manner again.
> 
> 
> 

ah umm.. I'm not overly offended - it was more of a startle. I've gotten 
used to USENET, somewhat - nature of the beast. 

Here's a link: 
https://www.eff.org/deeplinks/2010/01/primer-information-theory-and-privacy

Apparently 33 bits of information are enough to identify you. I don't 
bother about the NSA, but plenty of shady scummy types around, so I tend 
to spread misinformation which *would* make me a big time liar. It's not 
a besetting sin.

Here's a link to my Youtube thingy: 
https://www.youtube.com/channel/UCj93gHgUt69R8oGE5hRXd9g

Really, if someone's feeling racist, hire someone of your own kind 
(whatever that is) to kick you and make you study - that way, you are 
more in control of your destiny. There's plenty of energy the sun kick 
out that just goes to waste. (Not the easiest thing to do - finding 
someone reliable enough to zap the toes, anyway..) It's rather 
unreasonable for humans to expect the Universe to spin around them 
indefinitely..

If there are trolls here, then:
https://www.psychologytoday.com/blog/your-online-secrets/201409/internet-trolls-are-narcissists-psychopaths-and-sadists
(scientific study of some sort)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extend unicodedata with a name/pattern/regex search for character entity references?

2016-09-05 Thread Veek. M
Thomas 'PointedEars' Lahn wrote:

> Gregory Ewing wrote:
> 
>> Larry Hudson wrote:
>>> If you continue to read this forum, you will quickly learn to ignore
>>> "Pointy-Ears".  He rarely has anything worth while to post, and his
>>> unique fetish about Real Names shows him to be a hypocrite as well.
>> 
>> To be fair, it's likely that Thomas Lahn is his real
>> name,
> 
> Of course it is my real name.
> 
>> and he's never claimed that one shouldn't also
>> include a nickname in one's posted identifier.
> 
> In fact, I have recommended doing that several times to people who
> only used their nickname in the “From” header field value.
> 
>> So Veek should be able to appease P.E. by calling
>> himself 'Veek "David Smith" M'.
> 
> That would not help.  “Veek” might be (the transcription of) a given
> name or
> a family name, but “Veek M” is not a real name.  [Real name filter
> rules need to be adapted, though, because “O” is the transcription of
> an East Asian family name.]
> 
>> The quotes clearly mark the middle part as an invented addition, so
>> he's not lying,
> 
> He is lying and would be.  That is the main issue.  How can I trust a
> person who does not even have the decency and the courage to stand by
> their statements with their real name?
> 
>> and it looks enough like a Western style real name to avoid
>> triggering P.E.'s "fake name" reflex. :-)
> 
> It is not a reflex, but a request for something so basic that no
> request should be necessary: showing the simple politeness of
> introducing yourself properly when meeting strangers – of not lying to
> them about one’s identity – especially if one asks them for help.
> 

Ah! Now that's the root of his troubles, right there. Just in case 
anyone else is confused, I DON'T feel helped, thankful, grateful when 
someone answers my posts/questions. I expect you to have a whale of a 
good time when you post (I've always found it to be rather pleasant and 
fun to post). However I'm shhing on the subject of Thomas more or less 
permanently. This sort of humor works only when you're in a good mood 
and feeling chirpy, but that's not always the case so.. He's more or 
less Steve's baby anyhow (sorry Steve).




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


Pasting code into the cmdline interpreter

2016-09-21 Thread Veek M
I wanted to test this piece of code which is Kate (editor) on the cmd 
line python >>> prompt:

tex_matches = re.findall(r'(\\\w+{.+?})|(\\\w+)', msg)
for tex_word in tex_matches:
repl = unicode_tex.tex_to_unicode_map.get(tex_word)

if repl is None:
  repl = 'err'

msg = re.sub(re.escape(tex_word), repl, msg)


There are a number of difficulties i encountered.
1. I had to turn on  highlighting to catch mixed indent (which is a 
good thing anyways so this was resolved - not sure how tabs got in 
anyhow)
2. Blank lines in my code within the editor are perfectly acceptable for 
readability but they act as a block termination on cmd line. So if i 
paste:
tex_matches = re.findall(r'(\\\w+{.+?})|(\\\w+)', msg)
for tex_word in tex_matches:
repl = unicode_tex.tex_to_unicode_map.get(tex_word)

if repl is None:

I get:
 IndentationError: unexpected indent

How do i deal with this - what's the best way to achieve what I'm trying 
to do.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pasting code into the cmdline interpreter

2016-09-21 Thread Veek M
Ben Finney wrote:

> Veek M  writes:
> 
>> 1. I had to turn on  highlighting to catch mixed indent (which
>> is a good thing anyways so this was resolved - not sure how tabs got
>> in anyhow)
> 
> The EditorConfig system is a growing consensus for configuring a code
> base to instruct text editors not to mangle it. See the EditorConfig
> site http://editorconfig.org/> for more information.
> 
> Sadly, it seems Kate does not yet recognise EditorConfig instructions
> https://bugs.kde.org/show_bug.cgi?id=330843>. In the meantime,
> manually configure Kate to only ever insert spaces to indent lines.
> 
>> 2. Blank lines in my code within the editor are perfectly acceptable
>> for readability but they act as a block termination on cmd line.
> 
> Yes. That is a deliberate compromise to make it easy to write code
> interactively at the interactive prompt.
> 
>> I get:
>>  IndentationError: unexpected indent
>>
>> How do i deal with this - what's the best way to achieve what I'm
>> trying to do.
> 
> Since you are writing code into a module file, why not just run the
> module from that file with the non-interactive Python interpreter?
> 
It's part of a hexchat plugin/addon.. like below (basically lots of 
hexchat crud interspersed. 

Anyway, i should hive it off into a separate function, and hide the 
import hexchat - still, bit of a pain.

import hexchat

def send_message(word, word_eol, userdata):
if not(word[0] == "65293"):
return

msg = hexchat.get_info('inputbox')
if msg is None:
return

x = re.match(r'(^\\help)\s+(\w+)', msg)
if x:
filter = x.groups()[1]
for key, value in unicode_tex.tex_to_unicode_map.items():
if filter in key:
print_help(value + ' ' + key)
hexchat.command("settext %s" % '')
return

# findall returns: [(,), (,), ...] or []
tex_matches = re.findall(r'((\\\w+){(.+?)})|(\\\w+)', msg)
if len(tex_matches) == 0: 
  return
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pasting code into the cmdline interpreter

2016-09-22 Thread Veek M
eryk sun wrote:

> On Thu, Sep 22, 2016 at 5:12 AM, Veek M  wrote:
>> 2. Blank lines in my code within the editor are perfectly acceptable
>> for readability but they act as a block termination on cmd line.
> 
> You can write a simple paste() function. For example:
> 
> import sys
> paste = lambda: exec(sys.stdin.read(), globals())
> 
> >>> paste()
> class Foo:
> """test class"""
> 
> def spam(self):
> '''test method'''
> return 'eggs'
> 
> >>> Foo().spam()
> 'eggs'
> 
> In a Unix terminal and IDLE, stdin.read() is terminated by typing
> Ctrl+D at the start of an empty line. For the Windows console, it's
> Ctrl+Z, Enter. Actually in a Unix terminal the cursor can also be at
> the end of a line, but a bug in Python requires pressing Ctrl+D twice
> in that case.

ah! very clever and many thanks! so, you use .pythonrc.py to 
store/define your cmd-line shortcuts.

It doesn't work in python2.x, because exec is a stmt, so, def paste(
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pasting code into the cmdline interpreter

2016-09-22 Thread Veek M
Ben Finney wrote:

> Veek M  writes:
> 
>> Ben Finney wrote:
>>
>> > Since you are writing code into a module file, why not just run the
>> > module from that file with the non-interactive Python interpreter?
>> > 
>> It's part of a hexchat plugin/addon..
> 
> Which tells me that it still isn't appropriate to copy-paste the code
> directly into the *interactive* Python interpreter. Instead, it's a
> module that should be imported from file, by Hexchat.
> 
> Unless I misunderstand what you're talking about?
> 
ah nope, you got it.
-- 
https://mail.python.org/mailman/listinfo/python-list


h(re) for help, import re - on NameError

2016-09-22 Thread Veek M
Is there a way to use .pythonrc.py to provide a help function that 
autoloads whatever module name is passed like so:
\>>> h(re)

I tried inheriting site._Helper and overriding __init__ and __call__ but 
that didn't work, also I don't know how to deal/trap/catch the NameError 
(no quotes on h(re)) - is there a way to insert a try/except block 
around the >>> prompt?

I hate having to: import whatever every-time i forget. Actually could I 
ditch the () in h(re) and just do: h re - the joy of that :p
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pasting code into the cmdline interpreter

2016-09-22 Thread Veek M
eryk sun wrote:

> On Thu, Sep 22, 2016 at 12:40 PM, Gregory Ewing
>  wrote:
>> eryk sun wrote:
>>>
>>> Actually in a Unix terminal the cursor can also be at
>>> the end of a line, but a bug in Python requires pressing Ctrl+D
>>> twice in that case.
>>
>> I wouldn't call that a bug, rather it's a consequence of
>> what Ctrl-D does. It doesn't really mean EOF, it means to
>> send whatever the terminal driver has in its input buffer.
>> If the buffer is empty at the time, the process gets a
>> zero-length read which is taken as EOF. But if the buffer
>> is not empty, it just gets whatever is in the buffer.
>>
>> There's nothing Python can do about that, because it
>> never sees the Ctrl-D -- that's handled entirely by the
>> terminal driver.
> 
> Yes, FileIO.readall continues making read() system calls until it sees
> an empty read. But if we know we're reading from a terminal, we should
> be able to assume that a read either consumes an entire line up to a
> newline character or the entire buffer, no? In other words, if we see
> a read that returns less than the buffer size but doesn't end on a
> newline, then for a terminal, and only a terminal, I think we can
> infer Ctrl+D was typed and handle it as EOF.
 
read() changes it behavior intelligently according to the file type 
(pipe, fifo, etc). By default, from a terminal, it reads up to the first 
newline - canonical mode/cooked mode vs non-canonical mode (vi/less). 
Maybe python interpreter is doing the read in non-canonical mode?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: h(re) for help, import re - on NameError

2016-09-22 Thread Veek M
Chris Angelico wrote:

> On Thu, Sep 22, 2016 at 8:10 PM, Veek M  wrote:
>> Is there a way to use .pythonrc.py to provide a help function that
>> autoloads whatever module name is passed like so:
>> \>>> h(re)
>>
>> I tried inheriting site._Helper and overriding __init__ and __call__
>> but that didn't work, also I don't know how to deal/trap/catch the
>> NameError (no quotes on h(re)) - is there a way to insert a
>> try/except block around the >>> prompt?
>>
>> I hate having to: import whatever every-time i forget. Actually could
>> I ditch the () in h(re) and just do: h re - the joy of that :p
> 
> You could use sys.excepthook to catch the NameError. I don't know of a
> way to catch the SyntaxError and look at the original text, but with
> NameError it's pretty easy:
> 
> def excepthook(t,v,tb):
> if t is NameError:
> n = v.args[0].split("'")[1]
> globals()[n] = __import__(n)
> exec(tb.tb_frame.f_code, tb.tb_frame.f_globals)
> else:
> excepthook.old(t,v,tb)
> 
> import sys
> excepthook.old = sys.excepthook
> sys.excepthook = excepthook
> 
> Paste that into interactive Python and give it a try. Be aware that
> it'll attempt to import *any* dud name, so it'll potentially slow
> stuff down any time you typo. Also, it re-executes the current
> traceback frame, which may or may not be appropriate; it's fine for
> help(re), but not otherwise.
> 
> As an alternative, you could inspect tb.tb_frame.f_code to see if it
> matches "help(x)", and if it does, simply re-execute it with the
> string form of the name:
> 
> exec(tb.tb_frame.f_code, tb.tb_frame.f_globals, {n: n})
> 
> This works because help('re') does the same thing as help(re), so by
> effectively setting re="re", you gain that functionality without
> actually importing into the global namespace.
> 
> Adequately recognizing help(re) without false positives is left as an
> exercise for the reader. :)
> 
> ChrisA

works great :) - okay so you save and replace the default exception 
handler for the interpreter. Then when the procedure is called, check to 
see if type is NameError - if yes, then value contains the error string 
with module name, so you split and extract module name, and import the 
module and map it to a name in the global NS and hey presto (run the old 
code with the modified NS - that's a bit funky)!

Regarding fiddling with the code-object, yep, 
tb.tb_frame.f_code.co_names has a tuple ('h', 're') I suppose that's 
clean because it's your input after all vs splitting on something system 
generated (like an error message) 

works brilliantly - thanks :)
-- 
https://mail.python.org/mailman/listinfo/python-list


PyQT - Signals and Slots?

2016-10-10 Thread Veek M
I'm reading Rapid GUI Programming - Mark Summerfield with Python and QT 
pg 131. Basically the mechanism is an event table which maps a 'signal' 
to a 'function/slot' -correct?

  self.connect(dial, SIGNAL("valueChanged(int)"), spinbox.setValue)

Here, dial.valueChanged -> spinbox.setValue

 s.connect(w, SIGNAL("signalSignature"), functionName)
 s.connect(w, SIGNAL("signalSignature"), instance.methodName)
 s.connect(w, SIGNAL("signalSignature"), instance, 
SLOT("slotSignature"))

Here, w.signalSignature -> functionName
-> instance.methodName
-> instance.slotSignature

If signalSignature is a C++ implemented thingy then we got to pass type 
info as part of the mapping so "signalSignature(int, float, const char 
*). PyQT signals are any type and any number of args..


If the Slot-function is implemented in C++ it's better to use the SLOT() 
mechanism:
 self.connect(dial, SIGNAL("valueChanged(int)"), spinbox, 
SLOT("setValue(int)"))
Here, we are mapping dial.valueChanged(int) --> spinbox.setValue(int)


The part i found tricky was this:

class ZeroSpinBox(QSpinBox):
zeros = 0
def __init__(self, parent=None):
super(ZeroSpinBox, self).__init__(parent)
self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero)

def checkzero(self):
if self.value() == 0:
self.zeros += 1
self.emit(SIGNAL("atzero"), self.zeros)

ZeroSpinBox.valueChanged -> ZeroSpinBox.checkzero? Why is he mapping 
back to himself? Shouldn't it be widget-to-widget?

Then he raises a signal 'atzero' with one arg - the lack of a atzero() 
implies it's a 'short-circuit' signal so self.zeroes is a python data 
type. And in the book he says:

###
Here is how we connect to the signal in the form’s __init__() method:
zerospinbox = ZeroSpinBox()
...
self.connect(zerospinbox, SIGNAL("atzero"), self.announce)
Again, we must not use parentheses because it is a short-circuit signal. 
And
for completeness, here is the slot it connects to in the form:
def announce(self, zeros):
print "ZeroSpinBox has been at zero %d times" % zeros
###

Whaaa...t?? Could someone explain what exactly is his grand design 
besides being awfully circuitous? So he has some other Form thingy.. and 
in that he sets up another mapping from ZeroSpinBox.atzero --> 
ZeroSpinBox.announce  where's announce and atzero defined?





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


Re: PyQT - Signals and Slots?

2016-10-10 Thread Veek M
Mark Summerfield wrote:

> 
> The ZeroSpinBox is a tiny example designed to show how the signal/slot
> mechanism works. It is just a QSpinBox with the addition of
> remembering how many times (all the) ZeroSpinBox(es) have had a 0
> value.
> 
> Nowadays the connections would be made with a new improved syntax:
> 
> self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero) # OLD
> self.valueChanged.connect(self.checkzero) # NEW
> # or
> self.valueChanged[int].connect(self.checkzero) # NEW
> 
> Similarly the emit:
> self.emit(SIGNAL("atzero"), self.zeros) # OLD
> self.atzero.emit(self.zeros) # NEW
> 
> What's happening inside ZeroSpinBox? Whenever its value is set to 0 it
> calls its own checkzero() method, and this in turn emits an atzero
> signal with the number of zeros so far. Why does it do this? Just to
> show the mechanism. It doesn't matter whether you connect a widget to
> itself (unusual but done here), or to another widget (the norm).
> 
> See the book's website https://www.qtrac.eu/pyqtbook.html
> for all the source code including some updated with the new syntax.
> (But the book is old, so even the Python 3.1 examples aren't as
> Pythonic as they would be written in Python 3.4+.)

ah - okay, so i was on the right track - thanks :) cool that you hang 
out here :) 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Iteration, while loop, and for loop

2016-10-27 Thread Veek M
Elizabeth Weiss wrote:

> words=["hello", "world", "spam", "eggs"]
> counter=0
> max_index=len(words)-1
> 
> while counter<=max_index:
> word=words[counter]
> print(word + "!")
> counter=counter + 1


while 0 < 10:
  get 0'th element
  do something with element
  increment 0 to 1
(repeat)


words[0] gets the 0'th word (arrays/lists start at 0, not 1)


That example of his is badly presented..

1. use i, x, y, z, count in that order for integers
2. do your dirty work in a function
3. keep loops clean and tidy
4. keep data far away from code
5. avoid " when ' does the trick
6. try to create black box functions - that work no matter what you 
throw at them.. without sacrificing readability - programming is about 
aesthetics and readability - quite artsy..

(I'm not an expert so.. some of this might be outright wrong)

words=['hello', 'world', 'spam', 'eggs']

def display(txt, decoration='!'):
  message = str(txt) + str(decoration)
  print(message)

i = 0
i_max = len(words) - 1

while i <= i_max:
  word = words[i]
  i += 1
  display(word)

languages that don't have 'for' like C, will use 'while' - in python 
'for' is preferred - faster, readable - especially for the example you 
cited.

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


Re: Windows switch between python 2 and 3

2016-10-27 Thread Veek M
Daiyue Weng wrote:

> Hi, I installed Python 2.7 and Python 3.5 64 bit versions on Win 10.
> Under
> 
> C:\Python35
> 
> C:\Python27
> 
> Both have been set in environment variable Path.
> 
> When I type python in cmd, it only gives me python 2.7, I am wondering
> how to switch between 2 and 3 in command prompt.
> 
> cheers

you could try typing the path to the interpreter:
c:\Python35\bin\python.exe foo.py

or changing the path ordering using 'set /?' i think - google
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: problem using pickle

2016-10-27 Thread Veek M
Rustom Mody wrote:

> On Saturday, July 2, 2016 at 9:17:01 AM UTC+5:30, Veek. M wrote:
>> object is a keyword and you're using it as an identifier
> 
> keyword and builtin are different
> In this case though the advice remains the same
> In general maybe not...
> Just sayin'
np - feel free to correct me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: problem using pickle

2016-10-27 Thread Veek M
Ben Finney wrote:

> "Veek. M"  writes:
> 
>> class Foo(object):
>>   pass
>>
>> object is a keyword and you're using it as an identifier
> 
> Python does not have ‘object’ as a keyword. ‘and’ is a keyword.
> 
> Here's the difference::
> 
> >>> object
> 
> >>> object = "Lorem ipsum"
> >>> object
> 'Lorem ipsum'
> 
> >>> and
>   File "", line 1
> and
>   ^
> SyntaxError: invalid syntax
> >>> and = "Lorem ipsum"
>   File "", line 1
> and = "Lorem ipsum"
>   ^
> SyntaxError: invalid syntax
> 
> Here is how you can test whether a word is a Python keyword::
> 
> >>> import keyword
> >>> keyword.iskeyword('object')
> False
> >>> keyword.iskeyword('and')
> True
> 
> The set of keywords in Python is quite small.
> 
> >>> len(keyword.kwlist)
> 33
> 
Oh awesome - thanks - that's a neat module. Yeah, basically keywords are 
part of the language but not punctuation - kind of like how you had to 
do: print 'foo' in 2.x - part of the grammar of the language. Dunno why 
i muffed that.. 'object's a class anyhow so it couldn't be a keyword 
(inherited with its methods etc) - thanks for catching that and sorry 
for the delay.. saw that today.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help in python program

2016-10-29 Thread Veek M
id_1, clk, val = foo_function()
id_2, key, units, delay = bar_function()

if id_1 == id_2:
  print id_1, clk, val, key, units, delay


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


Re: Web Scraping

2016-11-12 Thread Veek M
121sukha wrote:

> I am new to python and I want to use web scraping to download songs
> from website. how do I write code to check if the website has uploaded
> a new song and have that song automatically be downloaded onto my
> computer. I know how to use the requests.get() module but i am more
> interested in knowing how to download and save every new song that the
> website uploads on the site. I would extremely appreciate the help
> from anyone. Thanks!

What you're doing is illegal but Youtube/websites have a lot of stuff up 
that's easily accessible - the moral issue seems ambiguous especially if 
you use adblocker.. 

Check for a RSS feed (orange icon thingy) or maybe hash (md5) the page 
contents(html) and compare against your previous run? Also check out the 
etree module for extracting links and other useful data reliably.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Web Scraping

2016-11-12 Thread Veek M
Steve D'Aprano wrote:

> On Sat, 12 Nov 2016 11:07 pm, Veek M wrote:
> 
>> 121sukha wrote:
>> 
>>> I am new to python and I want to use web scraping to download songs
>>> from website. how do I write code to check if the website has
>>> uploaded a new song and have that song automatically be downloaded
>>> onto my computer. I know how to use the requests.get() module but i
>>> am more interested in knowing how to download and save every new
>>> song that the website uploads on the site. I would extremely
>>> appreciate the help from anyone. Thanks!
>> 
>> What you're doing is illegal
> 
> Really? Everywhere in the world? And you're sure of this because
> you're a lawyer?

'probably illegal' (i did mentally assume he's from India - the nick is 
very indianish and since I am Indian and noticed many badly formatted 
posts I felt obliged to help out)

> Even if the website uses public domain or freely licenced songs?
> 
> Even if the downloads count as fair use, time-shifting or
> format-shifting?

ah true

>> but Youtube/websites have a lot of stuff up
>> that's easily accessible - the moral issue seems ambiguous especially
>> if you use adblocker..
> 
> Doesn't seem ambiguous to me.

> Or are you one of those people who think that you are a thief for
> leaving the room when ads are playing on TV?
> 
> https://yro.slashdot.org/story/02/05/02/0550214/turner-ceo-pvr-users-are-thieves

yep as in.. do the networks have a right to tempt me
 
> 
> Downloading may, or may not, violate the terms of use of the website.
> But you *literally* cannot watch the video without downloading it: in
> order for the video to play in your browser, it must be downloaded.
> That's the end of the story. "Streaming video" is just another way of
> saying "downloading video, where the video is deleted afterwards".
> 
> (Actually, streaming video is just another way of saying "lots of
> pauses, stuttering, dropped frames and a really awful user
> experience".)

also true, but gramophone records were an inconvenience we put up with 
lacking a better solution.. so an inconvenience strictly speaking 
doesn't imply you can break the law.. however, is it fair to tempt us 
with these delights.. hence my moral confusion..

Anyway.. yes I agree that I have no business telling people off (and 
that was not my intention) it was a succinct warning of the dangers.. 
-- 
https://mail.python.org/mailman/listinfo/python-list


__debug__ http://stackoverflow.com/questions/15305688/conditional-debug-statement-not-executed-though-debug-is-true

2016-11-15 Thread Veek M
Trying to make sense of that article. My understanding of debug was 
simple:
1. __debug__ is always True, unless -O or -OO
2. 'if' is optimized out when True and the expr is inlined.

So what does he mean by:

1. 'If you rebind __debug__, it can cause symptoms'
2. 'During module compilation, the same code that handles literals also 
handles the magic constants ..., None, True, False, and __debug__'
3. 'you'll see that if __debug__: statements are either removed 
entirely, or use LOAD_CONST to load the compile-time debug constant, 
while if bool(__debug__): statements use LOAD_GLOBAL to load the value 
of __debug__.'

4. 'Of course these are guaranteed to be the same… unless you rebind 
__debug__'

Basically every line in that answer is new to me..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __debug__ http://stackoverflow.com/questions/15305688/conditional-debug-statement-not-executed-though-debug-is-true

2016-11-15 Thread Veek M
Veek M wrote:

> Trying to make sense of that article. My understanding of debug was
> simple:
> 1. __debug__ is always True, unless -O or -OO
> 2. 'if' is optimized out when True and the expr is inlined.
> 
> So what does he mean by:
> 
> 1. 'If you rebind __debug__, it can cause symptoms'
> 2. 'During module compilation, the same code that handles literals
> also handles the magic constants ..., None, True, False, and
> __debug__' 3. 'you'll see that if __debug__: statements are either
> removed entirely, or use LOAD_CONST to load the compile-time debug
> constant, while if bool(__debug__): statements use LOAD_GLOBAL to load
> the value of __debug__.'
> 
> 4. 'Of course these are guaranteed to be the same… unless you rebind
> __debug__'
> 
> Basically every line in that answer is new to me..
Sorry for the awful title but I was not sure what to title it because I 
have no clue what they are talking about.. barring the fact that it's 
about __debug__ and assigning True/False to it..

Isn't that how you turn it off when you don't want -O ??
-- 
https://mail.python.org/mailman/listinfo/python-list


What exactly is a python variable?

2016-11-16 Thread Veek M
In C:
int x = 10; 
results in storage being allocated and type and location are fixed for 
the life of the program. 

In Python,
x = 10

causes an object '10' to be created but how exactly is 'x' handled? 
Symbol Table lookup at compile time? Is every 'x' being substituted out 
of existence? Because type(x) gives 'int' so..
-- 
https://mail.python.org/mailman/listinfo/python-list


What is the difference between class Foo(): and class Date(object):

2016-11-21 Thread Veek M
>>> class Foo():
...  pass
... 
>>> class Bar(Foo):
...  pass
... 
>>> b = Bar()
>>> type(b)


>>> class Date(object):
...  pass
... 
>>> class EuroDate(Date):
...  pass
... 
>>> x = EuroDate()
>>> type(x)



What is going on here? Shouldn't x = EuroDate();  type(x) give 
'instance'?? Why is 'b' an 'instance' and 'x' EuroDate?
Why isn't 'b' Bar?


Guhh! (I am reading @classmethods from beazley - i know i have two open 
threads but I'll get to that - will require even more reading)


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


Re: What is the difference between class Foo(): and class Date(object):

2016-11-21 Thread Veek M
Steve D'Aprano wrote:

> On Mon, 21 Nov 2016 11:15 pm, Veek M wrote:
> 
>>>>> class Foo():
>> ...  pass
>> ...
>>>>> class Bar(Foo):
>> ...  pass
>> ...
>>>>> b = Bar()
>>>>> type(b)
>> 
> [...]
> 
>> What is going on here? Shouldn't x = EuroDate();  type(x) give
>> 'instance'?? Why is 'b' an 'instance' and 'x' EuroDate?
>> Why isn't 'b' Bar?
> 
> 
> It looks like you are running Python 2, and have stumbled across an
> annoyance from the earliest days of Python: the "classic", or
> "old-style", class.
> 
> Before Python 2.2, custom classes and built-in types like int, float,
> dict and list were different. You have just discovered one of the ways
> they were different: instances of custom classes all had the same
> type, even if the class was different:
> 
> # Python 2
> 
> py> class Dog:
> ... pass
> ...
> py> class Cat:
> ... pass
> ...
> py> lassie = Dog()
> py> garfield = Cat()
> py> type(lassie) is type(garfield)
> True
> py> type(lassie)
> 
> 
> 
> This is just the most obvious difference between "classic classes" and
> types. Some of the other differences:
> 
> - The method resolution order (MRO) is different: the classic class
>   MRO is buggy for diamond-shaped multiple inheritance, and special
>   dunder methods like __eq__ are resolved slightly differently.
> 
> - super, properties, class methods and static methods don't work for
>   classic classes.
> 
> - The metaclass of classic classes is different:
> 
> py> type(Dog)
> 
> py> type(float)
> 
> 
> - Attribute lookup for classic classes is slightly different; in
>   particular, the special __getattribute__ method doesn't work.
> 
> 
> In Python 2.2, the built-in types (list, dict, float etc) were unified
> with the class mechanism, but for backwards compatibility the
> old-style classes had to be left in. So Python had two class
> mechanisms:
> 
> - "New-style classes", or types, inherit from object, or some
>   other built-in type, and support properties, etc.
> 
> - "Old-style classes", don't inherit from object, don't support
>   properties etc.
> 
> 
> So in Python 2, when you write:
> 
> class Foo:
> 
> or
> 
> class Foo():
> 
> 
> you get an old-style class. But when you inherit from object, you get
> a new-style class. Classic classes are an obsolete feature from Python
> 2. They are removed in Python 3, and things are much simpler. In
> Python 3, it doesn't matter whether you write:
> 
> class Foo:
> class Foo():
> class Foo(object):
> 
> the result is the same: a new-style class, or type.
> 
> The best thing to do in Python 2 is to always, without exception,
> write
> 
> class Foo(object):
> 
> to define your base classes. That will ensure that property, super,
> classmethod, staticmethod, __getattribute__, etc. will all work
> correctly, and you will avoid the surprises of classic classes.
> 
> 
> 
Thanks guys, got it!
-- 
https://mail.python.org/mailman/listinfo/python-list


dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Veek M
I was reading this: 
http://stackoverflow.com/questions/4418741/im-able-to-use-a-mutable-object-as-a-dictionary-key-in-python-is-this-not-disa

In a User Defined Type, one can provide __hash__ that returns a integer 
as a key to a dictionary.

so: d = { key : value }

What is the significance of __eq__ in this regard? Why does the article 
state:

"A class that overrides __eq__() and does not define __hash__() will 
have its __hash__() implicitly set to None. '

Is this behavior like __len__ and __bool__ wrt Boolean Expressions?
If one does: if x < y and __bool__ is not defined then __len__ is 
checked. Here, if __hash__ is undefined __eq__ is checked? What does he 
mean by 'overrides __eq__'??


Also didn't follow this 
" For classes you write, this method defaults to returning a value based 
off id(self), and *** if equality is not determined by identity 
for those classes, you may be surprised by using them as keys: 
***"

"The requirement is that the hash of an object doesn't change over time, 
and * that it keeps comparing equal (==) with its original 
value. **"

Also if one can do x.a = 10 or 20 or whatever, and the class instance is 
mutable, then why do books keep stating that keys need to be immutable?
After all, __hash__ is the guy doing all the work and maintaining 
consistency for us. One could do:

class Fruit:
  editable_value = ''
def __hash__(self):
 if 'apple' in self.value: 
   return 10
 elif 'banana' in self.value:
   return 20


 and use 'apple' 'bannana' as keys for whatever mutable data..
Are the books wrong?

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


Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Veek M
Jussi Piitulainen wrote:

> Veek M writes:
> 
> [snip]
> 
>> Also if one can do x.a = 10 or 20 or whatever, and the class instance
>> is mutable, then why do books keep stating that keys need to be
>> immutable?  After all, __hash__ is the guy doing all the work and
>> maintaining consistency for us. One could do:
>>
>> class Fruit:
>>   editable_value = ''
>> def __hash__(self):
>>  if 'apple' in self.value:
>>return 10
>>  elif 'banana' in self.value:
>>return 20
>>
>>
>>  and use 'apple' 'bannana' as keys for whatever mutable data..
>> Are the books wrong?
> 
> The hash does not do all the work, and the underlying implementation
> of a dictionary does not react appropriately to a key changing its
> hash value. You could experiment further to see for yourself.
> 
> Here's a demonstration that Python's dictionary retains both keys
> after they are mutated so that they become equal, yet finds neither
> key (because they are not physically where their new hash value
> indicates).
> 
> I edited your class so that its methods manipulate an attribute that
> it actually has, all hash values are integers, constructor takes an
> initial value, objects are equal if their values are equal, and the
> written representation of an object shows the value (I forgot quotes).
> 
> test = { Fruit('apple') : 'one', Fruit('orange') : 'two' }
> 
> print(test)
> print(test[Fruit('orange')])
> # prints:
> # {Fruit(apple): 'one', Fruit(orange): 'two'}
> # two
> 
> for key in test: key.value = 'banana'
> 
> print(test)
> print(test[Fruit('banana')])
> 
> # prints:
> # {Fruit(banana): 'one', Fruit(banana): 'two'}
> # Traceback (most recent call last):
> #   File "hash.py", line 25, in 
> # print(test[Fruit('banana')])
> # KeyError: Fruit(banana)

ah! not so: that's because you are messing/changing the integer value 
for the key. If apple-object was returning 10, you can't then return 20 
(the text mangling seems to be completely irrelevant except you need it 
to figure out which integer to return but barring that..).

Here's an example of what you're doing (note 'fly' is returning 20 BUT 
the object-instance is 'apple' - that obviously won't work and has 
nothing to do with my Q, err.. (don't mean to be rude):
class Fruit(object):
def __init__(self, text):
self.text = text

def mangle(self,text):
self.text = text

def __hash__(self):
if 'apple' in self.text:
return 10
elif 'orange' in self.text:
return 20
elif 'fly' in self.text:
return 20
else:
pass

apple = Fruit('apple')
orange = Fruit('orange')

d = { apple : 'APPLE_VALUE', orange : 'ORANGE_VALUE' }
print d

apple.mangle('fly')
print d[apple]

The Question is specific.. what I'm saying is that you can change 
attributes and the contents and totally mash the object up, so long as 
__hash__ returns the same integer for the same object. Correct?

Where does __eq__ fit in all this?


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


Re: dictionary mutability, hashability, __eq__, __hash__

2016-11-27 Thread Veek M
Veek M wrote:

> Jussi Piitulainen wrote:
> 
>> Veek M writes:
>> 
>> [snip]
>> 
>>> Also if one can do x.a = 10 or 20 or whatever, and the class
>>> instance is mutable, then why do books keep stating that keys need
>>> to be
>>> immutable?  After all, __hash__ is the guy doing all the work and
>>> maintaining consistency for us. One could do:
>>>
>>> class Fruit:
>>>   editable_value = ''
>>> def __hash__(self):
>>>  if 'apple' in self.value:
>>>return 10
>>>  elif 'banana' in self.value:
>>>return 20
>>>
>>>
>>>  and use 'apple' 'bannana' as keys for whatever mutable data..
>>> Are the books wrong?
>> 
>> The hash does not do all the work, and the underlying implementation
>> of a dictionary does not react appropriately to a key changing its
>> hash value. You could experiment further to see for yourself.
>> 
>> Here's a demonstration that Python's dictionary retains both keys
>> after they are mutated so that they become equal, yet finds neither
>> key (because they are not physically where their new hash value
>> indicates).
>> 
>> I edited your class so that its methods manipulate an attribute that
>> it actually has, all hash values are integers, constructor takes an
>> initial value, objects are equal if their values are equal, and the
>> written representation of an object shows the value (I forgot
>> quotes).
>> 
>> test = { Fruit('apple') : 'one', Fruit('orange') : 'two' }
>> 
>> print(test)
>> print(test[Fruit('orange')])
>> # prints:
>> # {Fruit(apple): 'one', Fruit(orange): 'two'}
>> # two
>> 
>> for key in test: key.value = 'banana'
>> 
>> print(test)
>> print(test[Fruit('banana')])
>> 
>> # prints:
>> # {Fruit(banana): 'one', Fruit(banana): 'two'}
>> # Traceback (most recent call last):
>> #   File "hash.py", line 25, in 
>> # print(test[Fruit('banana')])
>> # KeyError: Fruit(banana)
> 
> ah! not so: that's because you are messing/changing the integer value
> for the key. If apple-object was returning 10, you can't then return
> 20 (the text mangling seems to be completely irrelevant except you
> need it to figure out which integer to return but barring that..).
> 
> Here's an example of what you're doing (note 'fly' is returning 20 BUT
> the object-instance is 'apple' - that obviously won't work and has
> nothing to do with my Q, err.. (don't mean to be rude):
> class Fruit(object):
> def __init__(self, text):
> self.text = text
> 
> def mangle(self,text):
> self.text = text
> 
> def __hash__(self):
> if 'apple' in self.text:
> return 10
> elif 'orange' in self.text:
> return 20
> elif 'fly' in self.text:
> return 20
> else:
> pass
> 
> apple = Fruit('apple')
> orange = Fruit('orange')
> 
> d = { apple : 'APPLE_VALUE', orange : 'ORANGE_VALUE' }
> print d
> 
> apple.mangle('fly')
> print d[apple]
> 
> The Question is specific.. what I'm saying is that you can change
> attributes and the contents and totally mash the object up, so long as
> __hash__ returns the same integer for the same object. Correct?
> 
> Where does __eq__ fit in all this?

How does MUTABILITY play a role in this?? __hash__ *HAS* to return an 
integer which is immutable anyhow.. And as far as UDT's are concerned 
they don't need to be immutable, they just need to return their integer-
key correctly..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable argument unpacking

2016-12-04 Thread Veek M
Peter Otten wrote:

> Mehrzad Irani wrote:
> 
>> Hi All,
>> 
>> Consider the situation
>> [cti@iranim-rhel python_cti]$ cat a.py
>> def a(a = 1, b = 2, c = 3, *d, **e):
>> print(a, b, c)
>> print(d)
>> print(e)
>> 
>> r = {'e': 7, 'f': 8, 'g': 9}
>> 
>> 
>> a(**r)
>> a(3, **r)
>> 
>> r1 = (4,5,6)
>> 
>> a(3,2,1,*r1, **r)
>> a(*r1, **r)
>> 
>> r1 = (4,5,6,7)
>> a(*r1, **r)
>> 
>> [cti@iranim-rhel python_cti]$
>> 
>> The output for this program is as follows:
>> [cti@iranim-rhel python_cti]$ python a.py
>> (1, 2, 3)
>> ()
>> {'e': 7, 'g': 9, 'f': 8}
>> --
>> (3, 2, 3)
>> ()
>> {'e': 7, 'g': 9, 'f': 8}
>> --
>> (3, 2, 1)
>> (4, 5, 6)
>> {'e': 7, 'g': 9, 'f': 8}
>> --
>> (4, 5, 6)
>> ()
>> {'e': 7, 'g': 9, 'f': 8}
>> --
>> (4, 5, 6)
>> (7,)
>> {'e': 7, 'g': 9, 'f': 8}
>> 
>> This program shows, that for d to get assigned, I would need to first
>> assign a, b, c even though their default parameters have been set.
>> 
>> Also, if I would like to unpack a, b, c using e; I would get a
>> multiple assignment TypeError.
>> 
>> Therefore, my question is - is there a way to assign d, without going
>> through the assignments of a, b, c again, since they have already
>> been assigned defaults? (I think I am missing something simple here)
>> 
>> Thanks in advance.
> 
> Python 3 allows
> 
> $ cat b.py
> def a(*d, a=1, b=2, c=3, **e):
> print(a, b, c)
> print(d)
> print(e)
> 
> r = {'e': 7, 'f': 8, 'g': 9}
> 
> 
> a(**r)
> a(3, **r)
> 
> r1 = (4,5,6)
> 
> a(3,2,1,*r1, **r)
> a(*r1, **r)
> 
> r1 = (4,5,6,7)
> a(*r1, **r)
> $ python3 b.py
> 1 2 3
> ()
> {'e': 7, 'f': 8, 'g': 9}
> 1 2 3
> (3,)
> {'e': 7, 'f': 8, 'g': 9}
> 1 2 3
> (3, 2, 1, 4, 5, 6)
> {'e': 7, 'f': 8, 'g': 9}
> 1 2 3
> (4, 5, 6)
> {'e': 7, 'f': 8, 'g': 9}
> 1 2 3
> (4, 5, 6, 7)
> {'e': 7, 'f': 8, 'g': 9}
> 
> Perhaps that is more to your liking? I find the output as unreadable
> as of your a.py, so I won't bother to check...

import inspect, linecache

def a(a = 1, b = 2, c = 3, l = 0, *d, **e):
lineno = inspect.currentframe().f_back.f_lineno
print 'line', lineno, linecache.getline(__file__, lineno),\
linecache.getline(__file__, lineno-1)
print(a, b, c)
print(d)
print(e)
print ''

r = {'e': 7, 'f': 8, 'g': 9}
a(**r)

a(3, **r)

r1 = (4,5,6)
a(3,2,1,*r1, **r)
a(*r1, **r)

r1 = (4,5,6,7)
a(*r1, **r)

(thanks to erica on freenode who linked me to: 
http://code.activestate.com/recipes/145297-grabbing-the-current-line-number-easily/)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working around multiple files in a folder

2016-12-08 Thread Veek M
Emile van Sebille wrote:

> On 11/21/2016 11:27 AM, subhabangal...@gmail.com wrote:
>> I have a python script where I am trying to read from a list of files
>> in a folder and trying to process something. As I try to take out the
>> output I am presently appending to a list.
>>
>> But I am trying to write the result of individual files in individual
>> list or files.
>>
>> The script is as follows:
>>
>> import glob
>> def speed_try():
>>  #OPENING THE DICTIONARY
>>  a4=open("/python27/Dictionaryfile","r").read()
>>  #CONVERTING DICTIONARY INTO WORDS
>>  a5=a4.lower().split()
>>  list1=[]
>>  for filename in glob.glob('/Python27/*.txt'):
>>  a1=open(filename,"r").read()
>>  a2=a1.lower()
>>  a3=a2.split()
>>  for word in a3:
>>  if word in a5:
>>  a6=a5.index(word)
>>  a7=a6+1
>>  a8=a5[a7]
>>  a9=word+"/"+a8
>>  list1.append(a9)
>>  elif word not in a5:
>>  list1.append(word)
>>  else:
>>  print "None"
>>
>>  x1=list1
>>  x2=" ".join(x1)
>>  print x2
>>
>> Till now, I have tried to experiment over the following solutions:
>>
>> a) def speed_try():
>>#OPENING THE DICTIONARY
>>a4=open("/python27/Dictionaryfile","r").read()
>>#CONVERTING DICTIONARY INTO WORDS
>>a5=a4.lower().split()
>>list1=[]
>>for filename in glob.glob('/Python27/*.txt'):
>>   a1=open(filename,"r").read()
>>   a2=a1.lower()
>>   a3=a2.split()
>>list1.append(a3)
>>
>>
>>  x1=list1
>>  print x1
>>
>> Looks very close but I am unable to fit the if...elif...else part.
>>
>> b) import glob
>> def multi_filehandle():
>>  list_of_files = glob.glob('/Python27/*.txt')
>>  for file_name in list_of_files:
>>  FI = open(file_name, 'r')
>>  FI1=FI.read().split()
>>  FO = open(file_name.replace('txt', 'out'), 'w')
>>  for line in FI:
> 
> at this point, there's nothing left to be read from FI having been
> fully drained to populate FI1 -- maybe you want to loop over FI1
> instead?
> 
> Emile
> 
> 
>>  FO.write(line)
>>
>>  FI.close()
>>  FO.close()
>>
>> I could write output but failing to do processing of the files
>> between opening and writing.
>>
>> I am trying to get examples from fileinput.
>>
>> If anyone of the learned members may kindly suggest how may I
>> proceed.
>>
>> I am using Python2.x on MS-Windows.
>>
>> The practices are scripts and not formal codes so I have not followed
>> style guides.
>>
>> Apology for any indentation error.
>>
>> Thanking in advance.
>>
>>
*goggles in shock* that was painful to read! The best I could make of it 
was, he's trying to match words in some.txt against a dictionary.

(OP) should not code like a horror movie. Try to avoid dismembering your 
variable names and numbering the body parts like a serial killer. 
Instead try to pick names that matter. Use functions to hide the gory 
complexity..

Maybe like this (I dunno what you are doing)..

dict_file = '/python27/Dictionaryfile'
txt_file_path = '/Python27/*.txt'


def open_file(fname, mode='r'):
lines = open(fname, mode).read()
words = lines.lower().split()
return words

def get_files(path):
file_list = []
for fname in glob.glob(path):
file_list.append(fname)

return file_list


word_list = open_file(dict_file, mode)
file_list = get_files(txt_file_path)


for fname in file_list:
 do something and stick this in a func when you know what you were doing

Otherwise you'll get minimal help online because your program is 
unreadable.. in-spite of the frivolous comments.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Simple code and suggestion

2016-12-08 Thread Veek M
g thakuri wrote:

> Dear Python friends,
> 
> I have a simple question , need your suggestion the same
> 
> I would want to avoid using multiple split in the below code , what
> options do we have before tokenising the line?, may be validate the
> first line any other ideas
> 
>  cmd = 'utility   %s' % (file)
>  out, err, exitcode = command_runner(cmd)
>  data = stdout.strip().split('\n')[0].split()[5][:-2]
> 
> Love,

import re
? regex/pattern matching module..
-- 
https://mail.python.org/mailman/listinfo/python-list


% string formatting - what special method is used for %d?

2016-12-09 Thread Veek M
When we do:

print '%s %d' % ('hello', 10)

what special method is being invoked internally within the string-
format-specifier? 

format() invokes format__
print invokes __str__

I'm basically trying to make sense of:

raise TypeError('urkle urkle %s' % list(dictionary))
<=> raise TypeError('urkle urkle %s' % [ key1, val1, key2, val2 ]

So, the % operator reads the format specifier and notices %s and 
therefore calls __str__ in the list class to figure out how to represent 
[ key1, val1, key2, val2 ].

However what if I use %d? How do the other format specs work?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Veek M
Steve D'Aprano wrote:

> On Sat, 10 Dec 2016 06:06 pm, Veek M wrote:
> 
>> When we do:
>> 
>> print '%s %d' % ('hello', 10)
>> 
>> what special method is being invoked internally within the string-
>> format-specifier?
> 
> %d requires the argument to be an int, or able to be converted to int
> using the __int__ special method.
> 
> 
> py> class X(object):
> ... def __int__(self):
> ... return 42
> ...
> py> "%d" % X()
> '42'
> 
> 
> 
>> format() invokes format__
>> print invokes __str__
> 
> print actually invokes __str__ or __repr__, whichever is available.
> 
> 
> 
>> I'm basically trying to make sense of:
>> 
>> raise TypeError('urkle urkle %s' % list(dictionary))
>> <=> raise TypeError('urkle urkle %s' % [ key1, val1, key2, val2 ]
> 
> 
> The raise TypeError part of the code is irrelevant to your question.
> You should always simplify your code to only the part that is
> relevant.
> 
> raise TypeError(some_string)
> 
> behaves the same regardless of how some_string is made.
> 
> 
>> So, the % operator reads the format specifier and notices %s and
>> therefore calls __str__ in the list class to figure out how to
>> represent
>> [ key1, val1, key2, val2 ].
>> 
>> However what if I use %d? How do the other format specs work?
> 
> 
> The format specifiers are similar to these:
> 
> %s => str(obj), which ends up calling __str__ or __repr__
> 
> %r => repr(obj), which ends up calling __repr__ or __str__
> 
> %c => chr(obj), or obj must be a string of length 1
> 
> %d %i %u => int(obj), which ends up calling __int__
> 
> %x %X => int(obj), then convert to hexadecimal
> 
> %o => int(obj), then convert to octal
> 
> %e %E %f %g %G => float(obj), which ends up calling __float__
> 
> %% => a literal % sign
> 
> 
> 
> 
> 

Well take a look at this:
###
#!/usr/bin/python

class Foo(int):
def __init__(self, value):
self.value = value

def __str__(self):
print '__str__'
return str(self.value)

def __int__(self):
print '__int__'
return self.value + 1


#'%s' % Foo(10) # %s is mapped to __str__
'%d' % Foo(20)
###

here, '__str__' prints because when you do:
'%s' % x
the __str__ method is invoked. So internally %s invokes __str__ 
independent of print.

However the next line doesn't trigger any similar invocation with 
__int__ or__str__? (but int(Foo(10)) would invoked __int__)

Is there a way to trigger special methods using %d etc OR is this 
restricted to %s and why?








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


Re: % string formatting - what special method is used for %d?

2016-12-11 Thread Veek M
Ian Kelly wrote:

> On Sat, Dec 10, 2016 at 11:40 PM, Veek M  wrote:
>> Well take a look at this:
>> ###
>> #!/usr/bin/python
>>
>> class Foo(int):
>> def __init__(self, value):
>> self.value = value
>>
>> def __str__(self):
>> print '__str__'
>> return str(self.value)
>>
>> def __int__(self):
>> print '__int__'
>> return self.value + 1
>>
>>
>> #'%s' % Foo(10) # %s is mapped to __str__
>> '%d' % Foo(20)
>> ###
>>
>> here, '__str__' prints because when you do:
>> '%s' % x
>> the __str__ method is invoked. So internally %s invokes __str__
>> independent of print.
>>
>> However the next line doesn't trigger any similar invocation with
>> __int__ or__str__? (but int(Foo(10)) would invoked __int__)
> 
> This is probably because Foo inherits from int. Foo(20) is already an
> int so there is no conversion to be done; Python simply uses the int
> value and ignores the __int__ method in this case.
> 
>> Is there a way to trigger special methods using %d etc OR is this
>> restricted to %s and why?
> 
> For an object that is already an int, probably not.
> 
> However you may want to revisit your decision to make Foo inherit from
> int and question whether that is really sensible if you're also
> wanting to override the __int__ method. What does that mean if
> something is an int but also provides a method to convert to int? It's
> a contradiction.

Ah! thanks guys, now it works great. (I still need to ponder all this 
anyhow but yay!)
-- 
https://mail.python.org/mailman/listinfo/python-list


Nested functions, how do they work (stack related)

2016-12-12 Thread Veek M
I was reading the wiki on 'Call stack' because I wanted to understand 
what a traceback object was. My C/C++ isn't good enough to deal with raw 
python source since I have no background in CS. Also, you just can't 
dive into the python src - it takes a good deal of reading and 
background.. (the types will be confusing for a start)

https://en.wikipedia.org/wiki/Call_stack

'Programming languages that support nested subroutines also have a field 
in the call frame that points to the stack frame of the latest 
activation of the procedure that most closely encapsulates the callee, 
i.e. the immediate scope of the callee. This is called an access link or 
static link (as it keeps track of static nesting during dynamic and 
recursive calls) and provides the routine (as well as any other routines 
it may invoke) access to the local data of its encapsulating routines at 
every nesting level. 

Some architectures, compilers, or optimization cases store one link for 
each enclosing level (not just the immediately enclosing), so that 
deeply nested routines that access shallow data do not have to traverse 
several links; this strategy is often called a "display".'

1. What is the difference between a 'call frame' and a 'stack frame' in 
the above context? I know that a stack frame is all the data related to 
one - CALL foo; 

2. He's saying that within the 'call frame' (whatever that is) there's 
an address to one of the previous stack frames of the wrapper function ? 
What does all that mean in terms of nested functions? Access link? How 
are nested function stacks setup..

3. What exactly is a traceback object - we know that an instance object 
is a dictionary and some glue logic that allows you to pretend that 
methods are stored within the instance and call using x.sin(self) etc.
But I was reading: pydoc traceback AND:
http://effbot.org/librarybook/traceback.htm

'Extract the raw traceback from the current stack frame'
A stack frame contains (from wiki) the parameters, local variables, next 
instruction address so.. what's a raw traceback - does the default 
exception handler realize 'okay error' and then walk the stack and 
extract data and prettify it for display and build a magical traceback 
object? Is this documented for dummies what exactly it does?
(i know that's what it's doing but I HAVE NO CLUE so.. are there books 
on this)

How exactly does an exception fit in with tracebacks? How does all this 
fit in with nested functions? 

4. When you call a nested function (decorator), it generally returns a 
wrapper function but I thought he was just returning a reference to a 
function object but obviously since it can see it's environment, how is 
the stack being setup?


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


Re: Nested functions, how do they work (stack related)

2016-12-13 Thread Veek M
Veek M wrote:

> I was reading the wiki on 'Call stack' because I wanted to understand
> what a traceback object was. My C/C++ isn't good enough to deal with
> raw python source since I have no background in CS. Also, you just
> can't dive into the python src - it takes a good deal of reading and
> background.. (the types will be confusing for a start)
> 
> https://en.wikipedia.org/wiki/Call_stack
> 
> 'Programming languages that support nested subroutines also have a
> field in the call frame that points to the stack frame of the latest
> activation of the procedure that most closely encapsulates the callee,
> i.e. the immediate scope of the callee. This is called an access link
> or static link (as it keeps track of static nesting during dynamic and
> recursive calls) and provides the routine (as well as any other
> routines it may invoke) access to the local data of its encapsulating
> routines at every nesting level.
> 
> Some architectures, compilers, or optimization cases store one link
> for each enclosing level (not just the immediately enclosing), so that
> deeply nested routines that access shallow data do not have to
> traverse several links; this strategy is often called a "display".'
> 
> 1. What is the difference between a 'call frame' and a 'stack frame'
> in the above context? I know that a stack frame is all the data
> related to one - CALL foo;
> 
> 2. He's saying that within the 'call frame' (whatever that is) there's
> an address to one of the previous stack frames of the wrapper function
> ? What does all that mean in terms of nested functions? Access link?
> How are nested function stacks setup..
> 
> 3. What exactly is a traceback object - we know that an instance
> object is a dictionary and some glue logic that allows you to pretend
> that methods are stored within the instance and call using x.sin(self)
> etc. But I was reading: pydoc traceback AND:
> http://effbot.org/librarybook/traceback.htm
> 
> 'Extract the raw traceback from the current stack frame'
> A stack frame contains (from wiki) the parameters, local variables,
> next instruction address so.. what's a raw traceback - does the
> default exception handler realize 'okay error' and then walk the stack
> and extract data and prettify it for display and build a magical
> traceback object? Is this documented for dummies what exactly it does?
> (i know that's what it's doing but I HAVE NO CLUE so.. are there books
> on this)
> 
> How exactly does an exception fit in with tracebacks? How does all
> this fit in with nested functions?
> 
> 4. When you call a nested function (decorator), it generally returns a
> wrapper function but I thought he was just returning a reference to a
> function object but obviously since it can see it's environment, how
> is the stack being setup?

found this:
http://www.drdobbs.com/cpp/how-nested-functions-work-part-1/228701476
(still reading it)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested functions, how do they work (stack related)

2016-12-13 Thread Veek M
http://web.archive.org/web/20111030134120/http://www.sidhe.org/~dan/blog/archives/000211.html
(great tail recursion article - best i've seen! SO doesn't really 
explain it unless you already knew it to begin with, but here's the 
link:http://stackoverflow.com/questions/310974/what-is-tail-call-optimization)

I found it useful to read because it deals with the stack. Basically 
when an exception occurs you need to mess with the stack so..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested functions, how do they work (stack related)

2016-12-13 Thread Veek M
Marko Rauhamaa wrote:

> Veek M :
> 
>> https://en.wikipedia.org/wiki/Call_stack
>>
>> 'Programming languages that support nested subroutines also have a
>> field in the call frame that points to the stack frame of the latest
>> activation of the procedure that most closely encapsulates the
>> callee, i.e. the immediate scope of the callee. This is called an
>> access link or static link (as it keeps track of static nesting
>> during dynamic and recursive calls) and provides the routine (as well
>> as any other routines it may invoke) access to the local data of its
>> encapsulating routines at every nesting level.
>>
>> Some architectures, compilers, or optimization cases store one link
>> for each enclosing level (not just the immediately enclosing), so
>> that deeply nested routines that access shallow data do not have to
>> traverse several links; this strategy is often called a "display".'
>>
>> 1. What is the difference between a 'call frame' and a 'stack frame'
>> in the above context?
> 
> There's no difference.
> 
>> 2. He's saying that within the 'call frame' (whatever that is)
>> there's an address to one of the previous stack frames of the wrapper
>> function ? What does all that mean in terms of nested functions?
>> Access link? How are nested function stacks setup..
> 
> The classic C stack frame contains two addresses (in addition to the
> arguments and local variables):
> 
>  * the return address in the calling function
> 
>  * the frame pointer in the calling function
> 
> Some languages (notably Pascal) add a third address:
> 
>  * the frame pointer in the outer function
> 
> Often, the outer function is the same as the calling function.
> However, if the inner functions call each other, the outer function
> may be further up the stack. Since the outer function's local
> variables are seen by the inner functions, the extra pointer is needed
> to access them directly.
> 
> Python has nested functions. Thus, the same technique can be used to
> implement Python's internal call stack.
> 
>> 3. What exactly is a traceback object
> 
> It is an object that reports details of the call stack. It is mostly
> useful for troubleshooting.
> 
>> How exactly does an exception fit in with tracebacks? How does all
>> this fit in with nested functions?
> 
> Well, the traceback object contains also the exception since it is
> essential for troubleshooting.
> 
>> 4. When you call a nested function (decorator), it generally returns
>> a wrapper function but I thought he was just returning a reference to
>> a function object but obviously since it can see it's environment,
>> how is the stack being setup?
> 
> Now I don't exactly understand your question.
> 
> 
> Marko

Umm.. here's an article on windows exception handling.. i was hoping for 
something like that.. (it's very badly written but informative about 
win32 exception handling - i'm still reading it) wanted something 
similar.. I'll quote the highlights.. I KNOW NOTHING about ANY exception 
handling so..

http://www.codeproject.com/KB/cpp/exceptionhandler.aspx

' On the Intel Win32 platform, the FS register always points to the
current TIB. Thus, at FS:[0] you can find a pointer to an 
EXCEPTION_REGISTRATION structure. Now I'm getting somewhere! When an 
exception occurs, the system looks at the TIB of the faulting
thread and retrieves a pointer to an EXCEPTION_REGISTRATION structure. 
In this structure is a pointer to an _except_handler callback function.
The operating system now knows enough to call the _except_handler 
function,'

'When you use a compiler's _try/_except syntax, the compiler also builds 
the EXCEPTION_REGISTRATION struct on the stack. I'm simply showing you a 
simplified version of what a compiler would do if you used 
_try/_except.'




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


Is there a way to insert hooks into a native dictionary type to see when a query arrives and what's looked up?

2016-12-13 Thread Veek M
I know that with user classes one can define getattr, setattr to handle 
dictionary lookup. Is there a way to hook into the native dict() type 
and see in real time what's being queried.

I wanted to check if when one does:

x.sin()

if the x.__dict__ was queried or if the Foo.__dict__ was queried.. I 
know method/attribute lookup starts with the instance but was wondering 
if I could see it in action vs defining __getattr__ __setattr__ in Foo 
which is a bit indirect.. and not what I want.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to insert hooks into a native dictionary type to see when a query arrives and what's looked up?

2016-12-16 Thread Veek M
Steven D'Aprano wrote:

> On Wednesday 14 December 2016 17:11, Veek M wrote:
> 
>> I know that with user classes one can define getattr, setattr to
>> handle dictionary lookup. Is there a way to hook into the native
>> dict() type and see in real time what's being queried.
> 
> Not easily, and maybe not at all.
> 
> There are two obvious ways to do this:
> 
> (1) monkey-patch the object's __dict__, and the class __dict__.
> 
> Unfortunately, Python doesn't support monkey-patching built-ins.
> 
> https://en.wikipedia.org/wiki/Monkey_patch
> 
> Or perhaps I should say, *fortunately* Python doesn't support it.
> 
> http://www.virtuouscode.com/2008/02/23/why-monkeypatching-is-destroying-ruby/
> 
> (2) Alternatively, you could make a dict subclass, and replace the
> class and instance __dict__ with your own.
> 
> Unfortunately, you cannot replace the __dict__ of a class:
> 
> py> class X:  # the class you want to hook into
> ... pass
> ...
> py> class MyDict(dict):  # my custom dict
> ... def __getitem__(self, key):
> ... print(key)
> ... return super().__getitem__(key)
> ...
> py> d = MyDict()
> py> d.update(X.__dict__)
> py> X.__dict__ = d
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: attribute '__dict__' of 'type' objects is not writable
> 
> 
> You can replace the instance dict, but Python won't call your
> __getitem__ method:
> 
> py> instance = X()
> py> instance.__dict__ = MyDict()
> py> instance.a = 999
> py> instance.a
> 999
> 
> So the short answer is, No.
> 
> You might be able to create a completely new metaclass that supports
> this, but it would be a lot of work, and I'm not even sure that it
> would be successful.
> 
> 
> 
>> I wanted to check if when one does:
>> 
>> x.sin()
>> 
>> if the x.__dict__ was queried or if the Foo.__dict__ was queried..
> 
> The easiest way to do that is something like this:
> 
> 
> py> class Test:
> ... def sin(self):
> ... return 999
> ...
> py> x = Test()
> py> x.sin
> >
> py> x.sin()
> 999
> py> x.sin = "surprise!"
> py> x.sin
> 'surprise!'
> 
> 
> 
> So now you know: an instance attribute will shadow the class
> attribute.
> 
> (Actually, that's not *completely* true. It depends on whether x.sin
> is a descriptor or not, and if so, what kind of descriptor.)
> 
> 

heh
If it walks like a duck and talks like a duck, it’s a duck, right? So if 
this duck is not giving you the noise that you want, you’ve got to just 
punch that duck until it returns what you expect. -Patrick Ewing on 
Monkey/Duck patching in RailsConf 2007
-- 
https://mail.python.org/mailman/listinfo/python-list


super and mix-in class: how exactly is the search order altered?

2016-07-01 Thread Veek. M
I had posted this on StackOverflow - it's an excellent example of why SO 
sucks (don't want that happening here so please read carefully):

http://stackoverflow.com/questions/38145818/super-and-mix-in-class-how-exactly-is-the-search-order-altered?noredirect=1#comment63722336_38145818


I'm reading this article: 
https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

He's trying to explain the purpose of a 'mix-in class' and he says

We did not alter the source code for LoggingDict. Instead we built a 
subclass whose only logic is to compose two existing classes and control 
their search order.

class LoggingOD(LoggingDict, collections.OrderedDict):
pass

My question is this: in the above article context, is he talking about 
the LoggingDict's search order that is being manipulated? Or he is 
talking about manipulating the LoggingOD search order?

He says very clearly "not alter the source code for LoggingDict" so 
clearly he means that somehow, magically - the search order for 
super().__setitem__ in

class LoggingDict(dict):
def __setitem__(self, key, value):
logging.info('Settingto %r' % (key, value))
super().__setitem__(key, value)

is being altered/influenced, but how? Could someone clarify what exactly 
is going on here? Far as I can make of it, the tree looks like this: 
http://i.stack.imgur.com/3foOB.jpg

Here's the code:
import collections

class LoggingDict(dict):
def __setitem__(self, key, value):
logging.info('Settingto %r' % (key, value))
super().__setitem__(key, value)

class LoggingOD(LoggingDict, collections.OrderedDict):
pass

x = LoggingDict()
print LoggingDict.__mro__
print LoggingOD.__mro__

as you can see.. 

selfstudy@deathstar:~$ python 29.mro.py 
(, , )
(, , , , )
selfstudy@deathstar:~$ 

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


Re: why x is changed in the following program?

2016-07-01 Thread Veek. M
maurice.char...@telecom-paristech.fr wrote:

> from numpy import random
> x=random.randn(6)
> y=x
> y[0]=12
> print x[0]
> 
> 
> 

random.rand returns a list. x is a label to this list (container).
y=x creates another label to the same container/list.

y[0[ = 12 alters the 0th position of the container.
print x[0], uses a different label 'x' to change the same container.

use: y = list(x) to do a 'shallow copy' of x into a new list container
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess startup error

2016-07-01 Thread Veek. M
Shweta Dinnimani wrote:

> hi
> 
> hello, I'm begineer to python programming.. I had installed python
> 3.5.1 version on my windows 7 system. I was fine earlier and now when
> i was trying the programs on string i'm facing the subprocess startup
> error. IDLE is not connecting. And python shell is also not opening. I
> tried uninstalling and installing the python shell but Im facing the
> problem.Please do help me
> 

You need to post/screen-capture the whole error message/traceback.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: problem using pickle

2016-07-01 Thread Veek. M
Nicky Mac wrote:

> Dear Python team,
> I have studied the excellent documentation, and attempted to make use
> of pickle thus:
> 
> filename = 'my_saved_adventure'
> import  pickle
> class object:
> def __init__(self,i,.t) :
> self.id   = i
>  .
> 
> class world:
> def __init__(self):
> self.object
> 
> class object:
> def __init__(self,i,
> 
> .then   Object instances of object are created 
> 
> myworld = world;
> myworld.Object = Object
> fileobj = open(filename,'wb')
> pickle.dump(myworld,fileobj); fileobj.close()
> result = "saved your game to " + filename
> 
> fileobj = open(filename,'rb')
> myworld = pickle.load(fileobj); fileobj.close()
> Object = myworld.Object
> result = "restored your game from " + filename
> 
> 
> The proecedures execute without error
> but a file of only 21b is created containing " ?c__main__world q .
> altho there are several k of object instance data.
> 

class Foo(object):
  pass

object is a keyword and you're using it as an identifier
-- 
https://mail.python.org/mailman/listinfo/python-list


Descriptor: class name precedence over instance name

2016-07-01 Thread Veek. M
Trying to make sense of this para:

--
Also, the attribute name used by the class to hold a descriptor takes
prece- dence over attributes stored on instances. 

In the previous example,
this is why the descriptor object takes a name parameter and why the
name is changed slightly by inserting a leading underscore. In order
for the descriptor to store a value on the instance, it has to pick a
name that is different than that being used by the descriptor itself
-
Under normal circumstances, when I do an attribute lookup:
x = Foo()
x.a
he will first check 'x' then Foo.

Is he say that Descriptors are a special case where Foo is checked 
first, then what - Base classes..? or does he hop back to look in the 
instance? How is C3 Linearization altered?


Additionally,
class Foo:
   def __init__(self, name, value):
 self.name = name

cannot be done because
x = Foo('bar', 10)

x.bar will..? attribute in class takes precedence.. great, isn't that 
what we want?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Descriptor: class name precedence over instance name

2016-07-02 Thread Veek. M
Ben Finney wrote:

> "Veek. M"  writes:
> 
>> Trying to make sense of this para:
> 
> At the risk of being ruse, I am trying to make sense of some
> paragraphs in the messages you write here. Could you take a little
> more time to write clearly, as a way of communicating in this forum?
> 
>> Is he say that Descriptors are a special case where Foo is checked
>> first, then what - Base classes..? or does he hop back to look in the
>> instance? How is C3 Linearization altered?
> 
> I really have no idea what this paragraph means. Can you please write
> again assuming we don't know already what you are trying to say?
> 

Sorry about that, I found it hard to read too (when I came back to it). 

I was trying to figure out the order in which attributes are looked up. 
Beazley's a great book, but sometimes he kills me and mounts my head on 
a pike - page 127 - Descriptors section, last para.

He says that descriptor-attribute-names in a class, take precedence in a 
attribute lookup wrt instance attributes.

When you do an x.a, python goes on a hunt for 'a' - the whole binding 
idea; typically, that is, Instance Name Space -> Class NS -> BaseClasses
(C3 Linearization algorithm)

Therefore, I was wondering how he could start the search at the 
instance-Class, instead of the instance. When you print __mro__ you get 
a list of classes that are traversed but there's no explicit mention 
within the __mro__ that the instance is searched first. So I wanted to 
know if there was any implications to C3/__mro__

So essentially from what Ian said:
data_descriptor_in_instance -> instance_attribute -> non-
data_descriptor_in_instance -->__mro__

is how the search takes place. Correct?
--

Regarding part2 of the Q, :) Ian hasn't explained it, so I'm not sure 
how to explain it better :) but i'll try given that he has clarified 
part of the answer.

Basically Beazley has a TypedProperty descriptor class, and in class Foo
he instantiates:
 name = TypedProperty
Then he does f = Foo()

Thanks to Ian, we now know that any lookup on 'f' eg: f.name would 
cause.. well.. the f.name(TypedProperty-descriptor) to gain precedence 
thus hiding the f.name attribute! Therefore he needs to name-decorate or 
in this example append an '_' for whatever stupid reason.

I think i've got the jist down pat so :p
Here's the image:
http://storage1.static.itmages.com/i/16/0702/h_1467451175_7972040_b5037f6b46.png

(thanks Ian)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Descriptor: class name precedence over instance name

2016-07-03 Thread Veek. M
Ian Kelly wrote:

> On Sat, Jul 2, 2016 at 3:34 AM, Veek. M  wrote:
>> So essentially from what Ian said:
>> data_descriptor_in_instance -> instance_attribute -> non-
>> data_descriptor_in_instance -->__mro__
>>
>> is how the search takes place. Correct?
> 
> Close, but I would write it as:
> data_descriptor_in_class_including_mro -> instance_attribute ->
> non_data_descriptor_or_class_attribute_including_mro
> 
> In either case the class dict search is in __mro__ order. You can find
> the actual implementation here:
> 
> https://hg.python.org/cpython/file/30099abdb3a4/Objects/object.c#l1326
> 
> Roughly, the algorithm is this:
> 
> tp = type(obj)
> 
> # This call searches the MRO.
> descr = _PyType_Lookup(tp, name)
> 
> if descr != NULL and is_data_descriptor(descr):
> return descr.__get__(obj, tp)
> 
> if name in obj.__dict__:
> return obj.__dict__[name]
> 
> if descr != NULL and is_non_data_descriptor(descr):
> return descr.__get__(obj, tp)
> 
> if descr != NULL:
> return descr
> 
> raise AttributeError(name)
> 
>> --
>>
>> Regarding part2 of the Q, :) Ian hasn't explained it, so I'm not sure
>> how to explain it better :) but i'll try given that he has clarified
>> part of the answer.
>>
>> Basically Beazley has a TypedProperty descriptor class, and in class
>> Foo he instantiates:
>>  name = TypedProperty
>> Then he does f = Foo()
>>
>> Thanks to Ian, we now know that any lookup on 'f' eg: f.name would
> 
> Not *any* lookup, only one for which there is a declared descriptor.
> So in this example f.name or f.num would resolve according to the
> descriptor's __get__ method, but any other attribute lookup would just
> return whatever value is set on the instance (if any).
> 
>> cause.. well.. the f.name(TypedProperty-descriptor) to gain
>> precedence thus hiding the f.name attribute! Therefore he needs to
>> name-decorate or in this example append an '_' for whatever stupid
>> reason.
> 
> Right, if the "name" descriptor tried to store the value in the
> unmangled "name" instance attribute, then its getattr call would just
> recur back to the descriptor and you'd have an infinite loop.
> 
> In my opinion the mangling should be a bit heavier than what's done in
> this example, to avoid collisions between the descriptor and the class
> that's using it. I like to follow the pattern of __foo name mangling,
> so I would have instead used:
> 
> self.name = "_TypedProperty__" + name

Ouch - that's above my skill level (Python types, especially 
PyType_Lookup has no docs but i did find 
https://docs.python.org/3/c-api/index.html) and I got side tracked into reading 
the PCIe spec so 
today went down the drain. I'm putting this on hold as totally not 
understood and requiring much more reading. I'll drag it up later if 
everyones okay with it.

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


Re: super and mix-in class: how exactly is the search order altered?

2016-07-03 Thread Veek. M
dieter wrote:

> "Veek. M"  writes:
>> ...
>> I'm reading this article:
>> https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
>>
>> He's trying to explain the purpose of a 'mix-in class' and he says
>>
>> We did not alter the source code for LoggingDict. Instead we
>> built a
>> subclass whose only logic is to compose two existing classes and
>> control their search order.
>>
>> class LoggingOD(LoggingDict, collections.OrderedDict):
>> pass
>>
>> My question is this: in the above article context, is he talking
>> about the LoggingDict's search order that is being manipulated? Or he
>> is talking about manipulating the LoggingOD search order?
> 
> Likely, his language has been a bit sloppy.
> 
> Likely, his setup is as follows:
> 
>  * He has an existing class ("collections.OrderDict")
>which the base functionality he needs
> 
>  * He has an additional requirement (over that of
>  "collections.OrderDict")
>-- logging modifications
> 
>  * He wants to implement his requirements (the base ones and the
>the additional one) without modifying the existing class in any way
> 
>  * His idea to implement the additional requirement is to define
>a derived class ("LoggingOD") and lets its modifying methods
>perform the logging and then call the corresponding methods of the
>base class.
> 
>  * He recognizes that this logging feature might be interesting
>not only for "collections.OrderDict" but also for other
>dictionary like base classes.
>Therefore, instead of implementing it directly in
>"LoggingOD", he implements it in the mixin class "LoggingDict".
> 
>  * Because "LoggingDict" was explicitely designed to be used
>as mixin class to enhance a base class, it knows that
>some methods ("__setitem__") of the base class need to be called
>in its own implementation of the corresponding method.
> 
>  * The integrator (the one combining "LoggingDict" with the base
>class) must ensure (by an appropriate inheritance order)
>that the combining class ("LoggingOD" in the example)
>calls the "LoggingDict"'s methods (which know about that of the
>base class) rather than the base class's methods (which do not
>know about the mixin class's methods).
> 
>Therefore, he uses the inheritance order "LoggingDict" followed
>by the base class (and not vice versa).
> 
> 
> Python clearly defines in what order attributes of an object
> and of the construct "super(,)" are looked up.
> 
> The essential concept is the so called "MRO" ("method resolution
> order") (in fact, it is an attribute resolution order).
> 
> In simple cases (no common base classes), the MRO of
> a definition "class C(B_1, ..., B_n): ..."
> is defined by a left to right lookup: i.e. first in "C", then "B_1",
> then "B_2", ...
> 
> The rules are a bit more complicated when the "B_i" have a (or more)
> common base classes.

Hey Dieter, I'll need some time to read this and get back on it - hope 
that's okay. But yeah, I think he's explaining it badly and extremely 
misleading (imho).
-- 
https://mail.python.org/mailman/listinfo/python-list


subprocess: xterm -c cat, need to send data to cat and have it displayed in the xterm window

2016-07-11 Thread Veek. M
Script grabs some image data and runs imagemagick on it to extract some 
chinese. Then tesseract OCR to get the actual unicode.

I then need to get it translated which also works and then display in 
XTerm using cat.

I could cat << named_pipe but I was wondering if this was the only way?
Could I juggle fd's instead?


#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, time

import shlex
import codecs, subprocess

import goslate
from textblob.blob import TextBlob


def cmd_exec(cmd, wait=True, shell=True):
print cmd

cmd_line = shlex.split(cmd)
retobj = subprocess.Popen(cmd_line, shell, cwd='/tmp', 
stdin=subprocess.PIPE)

if wait:
retobj.wait()

return retobj

def get_text_and_process(skip=False):
if skip: return
cmd_exec('import /tmp/x.png')
cmd_exec('convert /tmp/x.png -resize 200% /tmp/x.resize.png')
cmd_exec('tesseract /tmp/x.resize.png /tmp/tao -l chi_sim')


get_text_and_process(skip=True)

fh = codecs.open('/tmp/tao.txt', encoding='utf-8')
fd = cmd_exec('xterm -en utf-8 -geometry 50x20+0+0 -e /bin/bash -c "cat 
-"', shell=False, wait=False)
fd.communicate('test\n') #how do i communicate with cat and not xterm
sys.exit() #I EXIT HERE


#This stuff doesn't run (but works) - sys.exit above
for line in fh.readlines():
try:
print >>fd.stdout, line.encode('utf-8')

blob = TextBlob(line)
print >>fd.stdout, blob.detect_language()
print >>fd.stdin, blob.translate(to='en') 
except UnicodeDecodeError, TranslatorError:
pass

fh.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess: xterm -c cat, need to send data to cat and have it displayed in the xterm window

2016-07-12 Thread Veek. M
Steven D'Aprano wrote:

> On Tuesday 12 July 2016 13:20, Veek. M wrote:
> 
>> Script grabs some image data and runs imagemagick on it to extract
>> some chinese. Then tesseract OCR to get the actual unicode.
>> 
>> I then need to get it translated which also works and then display in
>> XTerm using cat.
> 
> Why not just print it? Why do you have to use cat? That sounds like
> "Useless use of cat" to me:
> 
> 
http://porkmail.org/era/unix/award.htmlhttp://porkmail.org/era/unix/award.html
> 

Yeah, i changed that to 'tail -f' - works much better with tempfile.
-- 
https://mail.python.org/mailman/listinfo/python-list


How do you guys tackle a package with poorish documentation?

2016-07-12 Thread Veek. M
I've been messing with QQ (a Chinese chat app) and started receiving a 
lot of shady traffic partly because I was stupid enough to install the 
insecure QQ=international version. 

Anyway, so I decided to write something to provide me with a diff for 
networks. Basically track my current n/w with marginal shady traffic and 
then try to track what happens when I connect.

scapy seems like the internet approved version for python but I 
installed: pypcap https://github.com/dugsong/pypcap

import pcap
for ts, pkt in pcap.pcap():
print ts, `pkt`

which prints a timestamp and a pkt reference. Works great. Unfortunately 
I want what's in the packet and took a look at: pydoc pcap

 class pcap(__builtin__.object)
 |  pcap(name=None, snaplen=65535, promisc=True, timeout_ms=None, 
immediate=False)  -> packet capture object

So, what's a 'packet capture object'? It's not in pydoc and I then 
wasted a lot of time looking for better docs hoping it'll be in a book 
or in an on-line tutorial, and that's when I encountered scapy.
pypcap is and egg with a .so so how do you even do anything with it?

My question is: how do you guys deal with a situation like this? Do you 
look through the source code? Do you switch to scapy? Do you now look 
for books on scapy - there are quite a few, with chapters covering it.

A lot of my time goes to the dogs browsing for stuff so I was wondering 
if that's the norm? 
-- 
https://mail.python.org/mailman/listinfo/python-list


Scapy: sniff(filter='icmp', iface='ppp0', prn=icmp_callback)

2016-07-14 Thread Veek. M
#!/usr/bin/python

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import TCP, IP, ICMP, sniff

def ip_callback(pkt):
print '--- IP--'
pkt.show()
print 'IP', pkt.src, pkt.sport, '--->', pkt.dst, pkt.dport

def icmp_callback(pkt):
print '--- ICMP --'
print pkt.show()

def tcp_callback(pkt):
pkt.show()
print 'TCP', pkt.src, pkt.sport, '--->', pkt.dst, pkt.dport

def udp_callback(pkt):
pkt.show()
print 'UDP', pkt.src, pkt.sport, '--->', pkt.dst, pkt.dport

print 'hello'
sniff(filter='icmp', iface='ppp0', prn=icmp_callback)

I get:
deathstar> python myscapy.py 
hello

and nothing further when ping -c 2 8.8.8.8 runs - why??
Sometimes though it works (once in a blue moon) and i get the show 
output.

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


__instancecheck__ metaclasses, how do they work: why do I get True when I tuple, why doesn't print run?

2019-11-04 Thread Veek M
1. Why do I get True whenever i tuple the 
isinstance(f, (Bar, Foo))
(and why don't the print's run)

The docs say that you can feed it a tuple and that the results are OR'd


The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
-
which implies that the metaclasses are called for each class?


class MyzMeta(type):
def __instancecheck__(cls, other):
print('MyzzMeta', other)
return 0


class MyMeta(MyzMeta, object):
def __instancecheck__(cls, other):
print('MyMeta')
print(cls, other)
return 0


class Foo(list):
__metaclass__ = MyzMeta
pass

class Miaow(object):
pass

class Bar(Foo, Miaow):
__metaclass__ = MyMeta
pass


f = Foo()
b = Bar()

print(isinstance(f, (Bar, Foo)))
raise SystemExit

if isinstance(f, (Bar, Foo)):
print('success')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __instancecheck__ metaclasses, how do they work: why do I get True when I tuple, why doesn't print run?

2019-11-04 Thread Veek M
> 
> Aha.  You're trying to fix up the metaclass after the fact, which is not
> the right way to do it.  If you change the class definitions to:
> 
__metaclass__ = whatever; # is python2.x syntax

> then you get the prints from MyMeta.__instancecheck__().  The
> isinstance() still returns True, though, and I don't know why.  Then
> again, your definition of MyMeta is really weird.

weird how..?
(I'm just trying to figure out what's going on with __instancecheck__ - 
no further higher purpose :p)

example, when we do: isinstance(x, (A, B, C))
you expect from the docs that
A.__instancecheck__(cls, x) is passed but x = []
also, B and C.__instancecheck__ is not called when I return False

Also if I interchange (Bar, Foo), expecting that Foo.__instancecheck__ 
will be called, it's completely bypassed
-- 
https://mail.python.org/mailman/listinfo/python-list


What PEPs are worth reading after you've read a textbook/Beazley but want to understand details/innerworkings

2019-11-04 Thread Veek M
sez it all really, among the Finished PEPs, which ones should I pore 
through to teach Python competently!

What PEPs are considered de rigueur? What PEPs do you guys consider note-
worthy?

https://www.python.org/dev/peps/
-- 
https://mail.python.org/mailman/listinfo/python-list


SSL/TLS in Python using STARTTLS and ssl/ssltelnet and telnetlib

2019-11-06 Thread Veek M
Could someone suggest some introductory reading material that will allow 
me to use 'telnetlib' with 'ssl' or 'ssltelnet'. 
(currently using Pan since Knode is dropped on Debian)

I'm trying to write something that will download the NNTP headers over 
TLS.

The idea is to 
1. telnet to port 119, send 'CAPABILITIES\r\n' using telnetlib
2. then switch to TLS using STARTTLS
3. I tried just connecting to port 119 using a new TLS connection NOT 
OVER telnet and it didn't work. Apparently you need to pass the TLS 
context to telnetlib or vice versa.

Codes crap i've used bl_varname (bytes list) inconsistently - till I 
figure out what to do with TLS.

import ssl, socket
import sys, time
from telnetlib import Telnet
from ssltelnet import SslTelnet

port = 119
server = 'news.eternal-september.org'

user = 'v'
passwd = 'b'


class Response(object):
def __init__(self, tn, cmds = '', wmsg = ''):
self.tn = tn; self.total_data = []; self.wmsg = wmsg

d = { 
'r': self.read_server, 
'w': self.write_server, 
'p': self.bl_print 
}

for c in cmds:
d[c]()


def read_server(self):
tn = self.tn; total_data = []; count = 0
while True:
data = tn.read_very_eager()
if len(data):
count = 0
total_data.append(data)
else:
time.sleep(1)
count += 1
if count > 4:
self.total_data = total_data
return

def write_server(self):
tn = self.tn; txt = self.wmsg
for line in txt.split('\n'):
b_line = bytes(line + '\r\n', encoding='ascii')
tn.write(b_line)

def bl_print(self):
data = self.total_data
if len(data):
for line in data:
print(line.decode())
else:
print('no data')

def tls():
sock = socket.socket(family=AF_INET, type=SOCK_STREAM)
ssl_sock = ssl.wrap_socket(sock)

class Server(object):
def __init__(self, server = server, port = port):
with Telnet(server, port, timeout = 10) as tn:
Response(tn = tn, cmds = 'rp')
Response(tn = tn, cmds = 'wrp', wmsg='CAPABILITIES')
Response(tn = tn, cmds = 'wrp', wmsg='STARTTLS')
s = SslTelnet(force_ssl = False, telnet_tls = True, host = 
server, port = port)
print(s.read_very_eager())
time.sleep(2)
print(s.read_very_eager())



s = Server()

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


What is a backing store in the context of module io https://docs.python.org/3/library/io.html

2019-11-11 Thread Veek M
So i was making some notes and: https://i.imgur.com/UATAKXh.png

I did not understand this 

https://docs.python.org/3/library/io.html
'Text I/O expects and produces str objects. This means that whenever the 
backing store is natively made of bytes (such as in the case of a file), 
encoding and decoding of data is made transparently as well as optional 
translation of platform-specific newline characters.'

1. What is a backing store? 
2. How does it fit in/influence what we pass to the fileObject/stream/
filelikeObject.method()

So I've drawn a small dia - but I cannot see a connection between the 
'str' object or 'bytes' object being passed to .write() and the Backing 
Store?

Google just says the backing store is secondary MEMORY - Harddisk cache 
or for paging.. but how does that relate to Python?

I just concluded the backing store was a buffer?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is a backing store in the context of module io https://docs.python.org/3/library/io.html

2019-11-11 Thread Veek M
On Mon, 11 Nov 2019 16:08:12 +, Veek M wrote:

> So i was making some notes and: https://i.imgur.com/UATAKXh.png
> 
> I did not understand this
> 
> https://docs.python.org/3/library/io.html 'Text I/O expects and produces
> str objects. This means that whenever the backing store is natively made
> of bytes (such as in the case of a file),
> encoding and decoding of data is made transparently as well as optional
> translation of platform-specific newline characters.'
> 
> 1. What is a backing store?
> 2. How does it fit in/influence what we pass to the fileObject/stream/
> filelikeObject.method()
> 

I was reading pydoc io and - how do I decipher the indentation?

_io._BufferedIOBase(_io._IOBase)
_io.BufferedRWPair
_io.BufferedRandom  #are these Derived Classes of BufferedIOBase?
_io.BufferedReader
_io.BufferedWriter
_io.BytesIO
BufferedIOBase(_io._BufferedIOBase, IOBase) #huh??
_io._IOBase(__builtin__.object)
IOBase #huh???
BufferedIOBase(_io._BufferedIOBase, IOBase)
RawIOBase(_io._RawIOBase, IOBase)
TextIOBase(_io._TextIOBase, IOBase)
_io._RawIOBase(_io._IOBase)
_io.FileIO
RawIOBase(_io._RawIOBase, IOBase)
_io._TextIOBase(_io._IOBase)
_io.StringIO
_io.TextIOWrapper
TextIOBase(_io._TextIOBase, IOBase)
-- 
https://mail.python.org/mailman/listinfo/python-list


multiprocessing article on PYMOTW - subclassing with 'def run' and 'logging'

2019-11-16 Thread Veek M
https://pymotw.com/2/multiprocessing/basics.html
https://pymotw.com/2/threading/

I didn't follow this

1.
>The logger can also be configured through the logging configuration file 
>API, using the name multiprocessing.

and 

2.
>it is also possible to use a custom subclass.
> import multiprocessing
> class Worker(multiprocessing.Process):
>def run(self):
>print 'In %s' % self.name
>return


therefore, how is 'def run' going to start a process? 'run' may be 
autocalled same as in 'threading' but in threading, the function is the 
Thread.. so you can do your work in def run: and expect it to be suitably 
threaded

whereas here.. does he somehow magically convert a function to a process?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing article on PYMOTW - subclassing with 'def run' and 'logging'

2019-11-16 Thread Veek M
answered here https://www.reddit.com/r/Python/comments/dxhgec/
how_does_multiprocessing_convert_a_methodrun_in/

basically starts two PVMs - the whole fork, check 'pid' trick.. one 
process continues as the main thread and the other calls 'run'
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there a piece of code ('inspect') that displays all/most of the attributes/methods in a frame, traceback, generator object in a readable fashion

2019-11-19 Thread Veek M
Basically I want to call a method and pretty print the object contents 
for some code I'm playing with.

Instead of manually writing all this crud. Something like a python object 
explorer.



def foo(a, x = 10):
2 + 2

def bar():
pass

class A:
pass

class Foo(A, object):
def __init__(self):
2 + 2

def mymethod(self):
2 + 2

f = Foo()

import inspect
f = inspect.currentframe()
print f.f_locals, '\n\n', f.f_globals

print f.f_lasti, f.f_back, f.f_code, f.f_lineno

print f.f_trace
#print f.f_builtins

print f.f_code.co_name
print f.f_code.co_filename
print f.f_code.co_argcount
print f.f_code.co_varnames
print f.f_code.co_nlocals
-- 
https://mail.python.org/mailman/listinfo/python-list


Extending property using a Subclass - single method - why Super(Baz, Baz).name.__set__ ?

2019-12-03 Thread Veek M
class Foo(object):
@property
def name(self):
if hasattr(self, '_name'):
print('Foo name', self._name)
return self._name
else: 
return 'default'

@name.setter
def name(self, value):
print('Foo', self)
self._name = value
print(self._name)

@name.deleter
def name(self):
print('del')
self._name = None

print('Foo', name)

class Baz(Foo):
@property
def name(self):
print('Baz wrapper around getter')
return super().name

@Foo.name.setter
def name(self, value):
print('Baz wrapper around setter')
print(self)
print(super(Baz,Baz).name, value)
return super(Baz, Baz).name.__set__(self, value)

b = Baz()
print('print', b.name)
b.name = 'v'
print(b.name)

Why do we user super(Baz, Baz) - are we setting a class variable called 
Baz.name which would trigger Baz._name = value?

We are essentially doing:
Foo.name.__set__(Baz, value) ?

How come 'self' is not used.. like in the traditional property way where 
we pass an instance reference instead of a class?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extending property using a Subclass - single method - why Super(Baz, Baz).name.__set__ ?

2019-12-03 Thread Veek M
you've misunderstood my question, let me try again:

So this is a simple descriptor class and as you can see, dunder-set needs 
3 args: the descriptor CONTAINER/Bar-instance is the first arg, then a 
reference to the using instance/Foo-instance

class Bar(object):

def __set__(self, instance, value):
#b-instance of Bar, f-instance of Foo, value
print(self, instance, value)


class Foo(object):
b = Bar()

f = Foo()
print(f)
f.b = 10

1. Now when we create/use @property..
what is the first and second argument to dunder-set

(my guess is, the @property is the first arg and the second arg is 'Foo' 
IF you do

class Foo(object):
   @property
   def whatever..

Am I right? Is there a way to check?


2. The Class Bar/descriptor acts a wrapper/protector for some sekret _var 
and therefore it gets all the data needed to make a judgement call.. that 
is, it's own name/instance-ref and the using class/instance-name-ref

Note that he's receiving instance-references

therefore when I start sub-classing a property why does he then switch to 
class-references/class-variables
-- 
https://mail.python.org/mailman/listinfo/python-list


Decorator as a class and Descriptor __get__ working? - cookbook, 9.9, pg 349

2019-12-08 Thread Veek M
I did not follow the grok bit..
He's creating a Descriptor within class 'Spam' by doing 
@Profiled 
def bar()
because Profiled replaces 'bar' with it's instance that contains __get__

which means I have to do s.grok = 20 to trigger it? Which would imply,
s.__get__(instance, instance, value) NOT whatever he's done..  ? How is 
he passing Spam to 'grok'? It should be (self=decorator-instance, Spam-
instance-s and value) being passed to __get__

(for those who don't have the book, he's switched to 'grok' instead of 
'bar')


>>> s = Spam()
>>> def grok(self, x):
... pass
...
>>> grok.__get__(s, Spam)

class Spam:
@Profiled
def bar(self, x):
print(self, x)

import types
from functools import wraps
class Profiled:
def __init__(self, func):
wraps(func)(self)
self.ncalls = 0
  
def __call__(self, *args, **kwargs):
self.ncalls += 1
return self.__wrapped__(*args, **kwargs)

def __get__(self, instance, cls):
if instance is None:
return self
else:
return types.MethodType(self, instance)
-- 
https://mail.python.org/mailman/listinfo/python-list


Why is a generator expression called a expression?

2020-04-20 Thread Veek M
The docs state that a expression is some combination of value, operator, 
variable and function. Also you cannot add or combine a generator 
expression with a value as you would do with 2 + 3 + 4. For example, 
someone on IRC suggested this
all(a == 'a' for a in 'apple') but

1. all is a function/method
2. so (whatever) in this case is a call but obviously it works so it must 
be a generator object as well.. so.. how does 'all' the function object 
work with the generator object that's being produced? 

I can't for example do min 1,2,3 but i can do min (1,2,3) and the () are 
not integral to a tuple - therefore one could argue that the () are part 
of the call - not so with a generator Expression where the () are 
integral to its existence as an object.

Could someone clarify further.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is a generator expression called a expression?

2020-04-20 Thread Veek M
On Mon, 20 Apr 2020 19:19:31 +1000, Chris Angelico wrote:

> In the case of a genexp, the expression has a value which is a generator
> object. When you pass that to all(), it takes it and then iterates over

but an object is NOT THE SAME as it's value! '2' is an object which 
happens to have a value of 2 under certain contexts.. ergo a generator 
object is returned by ( whatever ) and therefore NOT a value-to-be-used!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is a generator expression called a expression?

2020-04-20 Thread Veek M
Also you will note, one can do:
( 2 if 3 > 2 else 4 ) + 4
so the () is just for precedence but otherwise a Conditional Expression 
works as expected by returning a value to be added to + 4.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is a generator expression called a expression?

2020-04-20 Thread Veek M
but one can do the following
(x for x in 'apple').next() * 2

def foo():
   (yield 2)
foo().next() * 3

(lambda x: 2)()*4

generator expr, yield expr, lambda expression 
all require some modification (insertion of a .next or explicit () so 
it's quite confusing.. 

expression seems to mean anything that gives a value with a little 
massaging..
with statements you can't combine them at all

assert 1 < 2
cannot be mixed at all with anything else unless you use a ; and even 
that won't work within ( )

is this reasonable?

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


x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-30 Thread Veek. M
I'm parsing html and i'm doing:

x = root.find_class(...
y = root.find_class(..
z = root.find_class(..

all 3 are likely to fail so typically i'd have to stick it in a try. This is 
a huge pain for obvious reasons.

try:
 
except something:
 x = 'default_1'
(repeat 3 times)

Is there some other nice way to wrap this stuff up?
I can't do:
try:
 x=
 y=
 z=
except:

because here if x fails, y and z might have succeeded.

Pass the statement as a string to a try function? Any other way?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-30 Thread Veek. M
Chris Angelico wrote:

> On Sun, Jan 31, 2016 at 3:58 PM, Veek. M  wrote:
>> I'm parsing html and i'm doing:
>>
>> x = root.find_class(...
>> y = root.find_class(..
>> z = root.find_class(..
>>
>> all 3 are likely to fail so typically i'd have to stick it in a try. This
>> is a huge pain for obvious reasons.
>>
>> try:
>>  
>> except something:
>>  x = 'default_1'
>> (repeat 3 times)
>>
>> Is there some other nice way to wrap this stuff up?
> 
> I'm not sure what you're using to parse HTML here (there are several
> libraries for doing that), but the first thing I'd look for is an
> option to have it return a default if it doesn't find something - even
> if that default has to be (say) None.
> 
> But failing that, you can always write your own wrapper:
> 
> def find_class(root, ...):
> try:
> return root.find_class(...)
> except something:
> return 'default_1'
> 
> Or have the default as a parameter, if it's different for the different
> ones.
> 
> ChrisA

I'm using lxml.html

def parse_page(self, root):
for li_item in root.xpath('//li[re:test(@id, "^item[a-z0-9]+$")]', 
namespaces={'re': "http://exslt.org/regular-expressions"}):
description = li_item.find_class('vip')[0].text_content()
link = li_item.find_class('vip')[0].get('href')
price_dollar = li_item.find_class('lvprice prc')
[0].xpath('span')[0].text
bids = li_item.find_class('lvformat')[0].xpath('span')[0].text

tme_time = li_item.find_class('tme')[0].xpath('span')
[0].get('timems')
if tme_time:
time_hrs = int(tme_time)/1000 - time.time()
else:
time_hrs = 'No time found'

shipping = li_item.find_class('lvshipping')
[0].xpath('span/span/span')[0].text_content()"

print('{} {} {} {} {}'.format(link, price_dollar, time_hrs, 
shipping, bids))

print('-')

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


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-30 Thread Veek. M
Veek. M wrote:

> Chris Angelico wrote:
> 
>> On Sun, Jan 31, 2016 at 3:58 PM, Veek. M  wrote:
>>> I'm parsing html and i'm doing:
>>>
>>> x = root.find_class(...
>>> y = root.find_class(..
>>> z = root.find_class(..
>>>
>>> all 3 are likely to fail so typically i'd have to stick it in a try.
>>> This is a huge pain for obvious reasons.
>>>
>>> try:
>>>  
>>> except something:
>>>  x = 'default_1'
>>> (repeat 3 times)
>>>
>>> Is there some other nice way to wrap this stuff up?
>> 
>> I'm not sure what you're using to parse HTML here (there are several
>> libraries for doing that), but the first thing I'd look for is an
>> option to have it return a default if it doesn't find something - even
>> if that default has to be (say) None.
>> 
>> But failing that, you can always write your own wrapper:
>> 
>> def find_class(root, ...):
>> try:
>> return root.find_class(...)
>> except something:
>> return 'default_1'
>> 
>> Or have the default as a parameter, if it's different for the different
>> ones.
>> 
>> ChrisA
> 
> I'm using lxml.html
> 
> def parse_page(self, root):
> for li_item in root.xpath('//li[re:test(@id, "^item[a-z0-9]+$")]',
> namespaces={'re': "http://exslt.org/regular-expressions"}):
> description = li_item.find_class('vip')[0].text_content()
> link = li_item.find_class('vip')[0].get('href')
> price_dollar = li_item.find_class('lvprice prc')
> [0].xpath('span')[0].text
> bids = li_item.find_class('lvformat')[0].xpath('span')[0].text
> 
> tme_time = li_item.find_class('tme')[0].xpath('span')
> [0].get('timems')
> if tme_time:
> time_hrs = int(tme_time)/1000 - time.time()
> else:
> time_hrs = 'No time found'
> 
> shipping = li_item.find_class('lvshipping')
> [0].xpath('span/span/span')[0].text_content()"
> 
> print('{} {} {} {} {}'.format(link, price_dollar, time_hrs,
> shipping, bids))
> 
print('-')

Someone suggested i refactor the find_class/xpath into wrapper functions but 
i tried it and it didn't look all that great..

Just give me a general idea of how to deal with messy crud like this..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Veek. M
Thanks guys: you've given me some good ideas - I really need to re-read 
the lxml docs for xpath. (basically trying to scrape ebay and score a 
mobo - ebaysdk doesn't work) Also need to google those principles :)  
thanks! (i knew one shouldn't overly rely on chained attribute lookups - 
didn't figure that had a name :))
-- 
https://mail.python.org/mailman/listinfo/python-list


coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Veek. M

Exceptions can be raised inside a coroutine using the throw(

Exceptions raised in this manner will originate at the currently 
executing yield state-ment in the coroutine.A coroutine can elect to 
catch exceptions and handle them as appropriate. It is not safe to use 
throw() as an asynchronous signal to a coroutine—it should never be 
invoked from a separate execution thread or in a signal handler.


What does Beazley mean by this: 'will originate at the currently 
executing yield state-ment in the coroutine'

If he's throw'ing an exception surely it originates at the throw:

def mycoroutine():
 while len(n) > 2: 
   n = (yield)

 throw('RuntimeError' "die!") 
--
Also: 'not safe to use throw() as an asynchronous signal to a coroutine—
it should never be invoked from a separate execution thread or in a 
signal handler.'

You can use throw within a coroutine to raise an exception.
How would you use it as an async-sig to a coroutine..
eg: you have two threads 
1. coroutine does except FooException:
2. throw(FooException, 'message')

so moment 'throw' runs and an exception is raised.. it'll propagate 
within thread-2 to its parent etc - how is thread-1 affected?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Veek. M
Veek. M wrote:

> 
> Exceptions can be raised inside a coroutine using the throw(
> 
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> 
> 
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
> 
> If he's throw'ing an exception surely it originates at the throw:
> 
> def mycoroutine():
>  while len(n) > 2:
>n = (yield)
> 
>  throw('RuntimeError' "die!")
> --
> Also: 'not safe to use throw() as an asynchronous signal to a
> coroutine— it should never be invoked from a separate execution thread
> or in a signal handler.'
> 
> You can use throw within a coroutine to raise an exception.
> How would you use it as an async-sig to a coroutine..
> eg: you have two threads
> 1. coroutine does except FooException:
> 2. throw(FooException, 'message')
> 
> so moment 'throw' runs and an exception is raised.. it'll propagate
> within thread-2 to its parent etc - how is thread-1 affected?
Veek. M wrote:

> 
> Exceptions can be raised inside a coroutine using the throw(
> 
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> 
> 
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
> 
> If he's throw'ing an exception surely it originates at the throw:
> 
> def mycoroutine():
>  while len(n) > 2:
>n = (yield)
> 
>  throw('RuntimeError' "die!")
> --
> Also: 'not safe to use throw() as an asynchronous signal to a
> coroutine— it should never be invoked from a separate execution thread
> or in a signal handler.'
> 
> You can use throw within a coroutine to raise an exception.
> How would you use it as an async-sig to a coroutine..
> eg: you have two threads
> 1. coroutine does except FooException:
> 2. throw(FooException, 'message')
> 
> so moment 'throw' runs and an exception is raised.. it'll propagate
> within thread-2 to its parent etc - how is thread-1 affected?

Also this bit:
***
If a coroutine returns values, some care is required if exceptions 
raised with throw() are being handled. If you raise an exception in a 
coroutine using throw(), the value passed to the next yield in the 
coroutine will be returned as the result of throw(). If
you need this value and forget to save it, it will be lost.
***

def coroutine():
  while True:
   line = (yield result)

  throw(FooException)

where is the question of a 'yield'? You'll exit the coroutine straight 
away..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: coroutine, throw, yield, call-stack and exception handling

2016-02-09 Thread Veek. M
Ian Kelly wrote:

> On Mon, Feb 8, 2016 at 2:17 AM, Veek. M  wrote:
>> 
>> Exceptions can be raised inside a coroutine using the throw(
>>
>> Exceptions raised in this manner will originate at the currently
>> executing yield state-ment in the coroutine.A coroutine can elect to
>> catch exceptions and handle them as appropriate. It is not safe to
>> use throw() as an asynchronous signal to a coroutine—it should never
>> be invoked from a separate execution thread or in a signal handler.
>> 
>>
>> What does Beazley mean by this: 'will originate at the currently
>> executing yield state-ment in the coroutine'
>>
>> If he's throw'ing an exception surely it originates at the throw:
>>
>> def mycoroutine():
>>  while len(n) > 2:
>>n = (yield)
>>
>>  throw('RuntimeError' "die!")
> 
> The "throw" is not called from inside the coroutine. It's a method of
> the generator object, and it's used by the calling code. It's similar
> to calling the send method, except that instead of passing a value to
> be returned by the yield expression, it passes an exception to be
> raised inside the coroutine at the yield expression.
> 
> Example:
> 
> def mycoroutine():
>   n = 0
>   while True:
> try:
>   n = (yield n)
> except SomeException:
>   n = 42
> 
> coro = mycoroutine()
> coro.next()
> for i in range(100):
>   if i % 6 == 0:
> coro.send(i % 6)
>   else:
> coro.throw(SomeException())
> 
> 
>> Also this bit:
>> ***
>> If a coroutine returns values, some care is required if exceptions
>> raised with throw() are being handled. If you raise an exception in a
>> coroutine using throw(), the value passed to the next yield in the
>> coroutine will be returned as the result of throw(). If
>> you need this value and forget to save it, it will be lost.
>> ***
>>
>> def coroutine():
>>   while True:
>>line = (yield result)
>>
>>   throw(FooException)
>>
>> where is the question of a 'yield'? You'll exit the coroutine
>> straight away..
> 
> Taking my example from above, after SomeException is caught, the next
> value yielded inside the coroutine will be the return value of the
> coro.throw() call. This may be surprising if you're only expecting
> coro.send() and not coro.throw() to return yielded values.

Thanks, that made it abundantly clear :)
-- 
https://mail.python.org/mailman/listinfo/python-list


How do i instantiate a class_name passed via cmd line

2016-02-13 Thread Veek. M
I'm writing a price parser. I need to do the equivalent of perl's
$$var to instantiate a class where $car is the class_name.

I'm passing 'Ebay' or 'Newegg' or 'Amazon' via cmd-line. I have a module 
named ebay.py and a class called Ebay (price parser). I do something 
like:

\> main.py ebay motherboard

and this does: 
 module = __import__(module_name)
but now i need to instantiate the class - right now I do:
 instance = module.Ebay(module_name, product)

how do i replace the 'Ebay' bit with a variable so that I can load any 
class via cmd line.

class Load(object):
def __init__(self, module_name, product):
try:
module = __import__(module_name)
instance = module.Ebay(module_name, product)
except ImportError:
print("Can't find module %s" % module_name)

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


Re: How do i instantiate a class_name passed via cmd line

2016-02-13 Thread Veek. M
Rick Johnson wrote:

> On Saturday, February 13, 2016 at 10:41:20 PM UTC-6, Veek. M wrote:
>> how do i replace the 'Ebay' bit with a variable so that I
>> can load any class via cmd line.
> 
> Is this what you're trying to do?
> 
> (Python2.x code)
>>>> import Tkinter as tk
>>>> classNames = ["Button", "Label"]
>>>> root = tk.Tk()
>>>> for className in classNames:
> classN = getattr(tk, className)
> instanceN = classN(root, text=className)
> instanceN.pack()
> 
> Note: You won't need to call "mainloop" when testing this
> code on the command line, only in a script or in the IDLE
> shell.
> 
>>>> root.mainloop()
Nope - this is what i'm doing:

class Foo():
 pass

x = 'Foo'

How do i use 'x' to create an instance of class Foo?

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


Re: How do i instantiate a class_name passed via cmd line

2016-02-13 Thread Veek. M
Gregory Ewing wrote:

> Veek. M wrote:
>> I'm writing a price parser. I need to do the equivalent of perl's
>> $$var to instantiate a class where $car is the class_name.
>> 
>> I'm passing 'Ebay' or 'Newegg' or 'Amazon' via cmd-line. I have a
>> module named ebay.py and a class called Ebay (price parser). I do
>> something like:
>> 
>> \> main.py ebay motherboard
>> 
>> and this does:
>>  module = __import__(module_name)
>> but now i need to instantiate the class - right now I do:
>>  instance = module.Ebay(module_name, product)
>> 
>> how do i replace the 'Ebay' bit with a variable so that I can load
>> any class via cmd line.
>> 
>> class Load(object):
>> def __init__(self, module_name, product):
>> try:
>> module = __import__(module_name)
>> instance = module.Ebay(module_name, product)
>> except ImportError:
>> print("Can't find module %s" % module_name)
>> 
> 
> Something like this should do it:
> 
>instance = getattr(module, class_name)(module_name, product)
> 
> If the class name is always the same as the module name with the
> first letter capitalized, you could use
> 
>instance = getattr(module, module_name.capitalize())(module_name,
>product)
> 
Ah! i see - clever!

'getattr' returns the class object with class_name=whatever
and we can instantiate now that we have a class object - nice - thanks 
guys - the bell should have rung from Rick's example.
-- 
https://mail.python.org/mailman/listinfo/python-list


repr( open('/etc/motd', 'rt').read() )

2016-02-15 Thread Veek. M
When I do at the interpreter prompt, 
repr( open('/etc/motd', 'rt').read() ) 
i get # 1 #:

"'\\nThe programs included with the Debian GNU/Linux system are free 
software;\\nthe exact distribution terms for each program are described 
in the\\nindividual files in /usr/share/doc/*/copyright.\\n\\nDebian 
GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\\npermitted 
by applicable law.\\n'"

whereas if i just do:
open('/etc/motd', 'rt').read()
i get # 2 #:

'\nThe programs included with the Debian GNU/Linux system are free 
software;\nthe exact distribution terms for each program are described 
in the\nindividual files in /usr/share/doc/*/copyright.\n\nDebian 
GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\npermitted by 
applicable law.\n'
---

With # 2 # read returns a string that the interpreter displays by 
calling __str__ via print so newlines are converted to \n.

What is happening with # 1 # (repr)?
repr calls __repr__ which gives you bytes.. why does this result in \\n




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


threading - Doug Hellman stdlib book, Timer() subclassing etc

2016-02-16 Thread Veek. M
In Doug Hellman's book on the stdlib, he does:

import threading
import logging
logging.basicConfig(level=logging.DEBUG,
format=’(%(threadName)-10s) %(message)s’,
)

class MyThreadWithArgs(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):

threading.Thread.__init__(self, group=group,
target=target,
name=name,
verbose=verbose)

self.args = args
self.kwargs = kwargs
return

def run(self):
logging.debug(’running with %s and %s’,
self.args, self.kwargs)
return

for i in range(5):
t = MyThreadWithArgs(args=(i,), kwargs={’a’:’A’, ’b’:’B’})
t.start()

1. Shouldn't def run() also include a call to the target function?
2. How does a call to a function_target result in a thread being 
created? Normally you'd have to call a function in pthreads (OS call)
One can sort of figure that t.start() hides the actual OS call, but when
we implement run().. somehow, magically there's no OS call? WTH! ??

Then in the Timer example in the next section, how is
the whole delay/canecl bit implemented?
We do t1.start so the 3 second counter starts ticking somewhere
- where? And how does he cancel that?

import threading
import time
import logging

logging.basicConfig(level=logging.DEBUG,
format=’(%(threadName)-10s) %(message)s’,
)

def delayed():
logging.debug(’worker running’)
return

t1 = threading.Timer(3, delayed)
t1.setName(’t1’)

t2 = threading.Timer(3, delayed)
t2.setName(’t2’)

logging.debug(’starting timers’)

t1.start()
t2.start()

logging.debug(’waiting before canceling %s’, t2.getName())
time.sleep(2)

logging.debug(’canceling %s’, t2.getName())
t2.cancel()

logging.debug(’done’)
-- 
https://mail.python.org/mailman/listinfo/python-list


Lookahead while doing: for line in fh.readlines():

2016-02-27 Thread Veek. M
I want to do something like:

#!/usr/bin/env python3

fh = open('/etc/motd')
for line in fh.readlines():
print(fh.tell())

why doesn't this work as expected.. fh.readlines() should return a 
generator object and fh.tell() ought to start at 0 first.

Instead i get the final count repeated for the number of lines.

What i'm trying to do is lookahead:
#!whatever

fh = open(whatever)
for line in fh.readlines():
x = fh.tell()
temp = fh.readline()
fh.seek(x)

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


Re: Lookahead while doing: for line in fh.readlines():

2016-03-04 Thread Veek. M
Terry Reedy wrote:

> On 2/27/2016 4:39 AM, Veek. M wrote:
>> I want to do something like:
>>
>> #!/usr/bin/env python3
>>
>> fh = open('/etc/motd')
>> for line in fh.readlines():
>>  print(fh.tell())
>>
>> why doesn't this work as expected.. fh.readlines() should return a
>> generator object and fh.tell() ought to start at 0 first.
> 
> Not after you have already read some data.  Readlines() reads the
> entire
> file and splits it into lines.  readline reads at least a single
> block.
>   Reading a single byte or character at a time looking for /n would be
> too slow, so even after readline, the file pointer will be somewhere
> past the end of the last line returned.
> 
>> Instead i get the final count repeated for the number of lines.
>>
>> What i'm trying to do is lookahead:
>> #!whatever
>>
>> fh = open(whatever)
>> for line in fh.readlines():
>>  x = fh.tell()
>>  temp = fh.readline()
>>  fh.seek(x)
>>
> 
> 

I get that readlines() would slurp the whole file for efficiency 
reasons. Why doesn't fh.seek() work though. Object 'fh' is a data 
structure for the OS file descriptor similar to FILE in C.


So if seek works in C, how come it doesn't work in python wrt 
readlines() which is just a method. What obviates the functioning of 
seek wrt readlines()?

fh.tell() works at the line level.. and fh.readline() works with 
fh.seek(0)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lookahead while doing: for line in fh.readlines():

2016-03-04 Thread Veek. M
MRAB wrote:

> On 2016-03-04 13:04, Veek. M wrote:
>> Terry Reedy wrote:
>>
>>> On 2/27/2016 4:39 AM, Veek. M wrote:
>>>> I want to do something like:
>>>>
>>>> #!/usr/bin/env python3
>>>>
>>>> fh = open('/etc/motd')
>>>> for line in fh.readlines():
>>>>  print(fh.tell())
>>>>
>>>> why doesn't this work as expected.. fh.readlines() should return a
>>>> generator object and fh.tell() ought to start at 0 first.
>>>
>>> Not after you have already read some data.  Readlines() reads the
>>> entire
>>> file and splits it into lines.  readline reads at least a single
>>> block.
>>>   Reading a single byte or character at a time looking for /n would
>>>   be
>>> too slow, so even after readline, the file pointer will be somewhere
>>> past the end of the last line returned.
>>>
>>>> Instead i get the final count repeated for the number of lines.
>>>>
>>>> What i'm trying to do is lookahead:
>>>> #!whatever
>>>>
>>>> fh = open(whatever)
>>>> for line in fh.readlines():
>>>>  x = fh.tell()
>>>>  temp = fh.readline()
>>>>  fh.seek(x)
>>>>
>>>
>>>
>>
>> I get that readlines() would slurp the whole file for efficiency
>> reasons. Why doesn't fh.seek() work though. Object 'fh' is a data
>> structure for the OS file descriptor similar to FILE in C.
>> 
>>
>  > So if seek works in C, how come it doesn't work in python wrt
>  > readlines() which is just a method. What obviates the functioning
>  > of seek wrt readlines()?
>  >
>  > fh.tell() works at the line level.. and fh.readline() works with
>  > fh.seek(0)
>  >
> 
> fh.readlines() reads the entire file.
> 
> At this point, it's at the end of the file.
> 
> The 'body' of the 'for' loop is then executed.
> 
> fh.tell() returns the the position of the end of the file because it's
> at the end of the file.
> 
> fh.readline() returns an empty string because it's at the end of the
> file.
> 
> fh.seek(x) seeks to the end of the file, which is where it already is.
> 
> Is that clearer?
Ah, right - got it - sorry for being thick. readlines() slurps the whole 
darn thing so the file pointer is at the EOF and within the loop body, 
i'm just saving that EOF position and restoring it back every time.
-- 
https://mail.python.org/mailman/listinfo/python-list


__del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls.

2016-03-06 Thread Veek. M
1. What are the rules for using __del__ besides: 'don't use it'.

2. What happens when I SystemExit? __del__ and gc are not invoked when I 
SystemExit and there's a circular reference - but why? The OS is going 
to reclaim the memory anyways so why be finicky about circular 
references - why can't we go ahead and call __dell_ and run gc?

3.
import foo
def __del__(self, foo=foo):
  foo.bar()

What happens here to prevent a NameError? Apparently without the foo=foo 
a NameError can occur? But why? import foo creates a reference to the 
object anyways so it's refcount will be above 0 anyways till __del__ is 
called.


4. also, are method calls more efficient than function calls?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls.

2016-03-06 Thread Veek. M
Veek. M wrote:

> 1. What are the rules for using __del__ besides: 'don't use it'.
> 
> 2. What happens when I SystemExit? __del__ and gc are not invoked when
> I SystemExit and there's a circular reference - but why? The OS is
> going to reclaim the memory anyways so why be finicky about circular
> references - why can't we go ahead and call __dell_ and run gc?
> 
> 3.
> import foo
> def __del__(self, foo=foo):
>   foo.bar()
> 
> What happens here to prevent a NameError? Apparently without the
> foo=foo a NameError can occur? But why? import foo creates a reference
> to the object anyways so it's refcount will be above 0 anyways till
> __del__ is called.
> 
> 
> 4. also, are method calls more efficient than function calls?

1,2,3 are a result of reading Beazley - pg 179.

I'll post the relevant paras for 3 first because that's still open, then 
i'll paste the paras for 1,2 for completeness but Chris pretty much 
nailed that. ###'s are my comments.


Text from Beazley for 3:

One final peculiarity about program termination is that the __del__ 
method for some objects may try to access global data or methods defined 
in other modules. Because these objects may already have been destroyed, 
a NameError exception occurs in __del__, and you may get an error such 
as the following:
Exception exceptions.NameError: 'c' in  ignored

If this occurs, it means that __del__ has aborted prematurely. It also 
implies that it may have failed in an attempt to perform an important 
operation (such as cleanly shutting down a server connection). If this 
is a concern, it’s probably a good idea to perform an explicit shutdown 
step in your code, rather than rely on the interpreter to destroy
objects cleanly at program termination.

The peculiar NameError exception can also be eliminated by declaring 
default arguments in the declaration of the __del__()
method:

import foo
class Bar(object):
def __del__(self, foo=foo):
foo.bar()# Use something in module foo

### Why the foo=foo? import foo, would increment the ref-count for 
object 'foo' so why go to all the effort of passing it in to every 
instance via the local stack (ref-to-object-foo)?
---

Text for 1,2 from Beazley:
It’s important to note that in some cases the __del__() method might not 
be invoked at program termination.This can occur if circular references 
exist between objects (in which case objects may be allocated but 
accessible from no known name-space).Although Python’s garbage collector 
can reclaim unused circular references dur-ing execution, it isn’t 
normally invoked on program termination.

### which begs the question, why not on program termination? How bad can 
it be since you are terminating anyway. __del__ seems like a total waste 
product method and given how finicky python is about junk i was 
wondering.. (after all print statement became print builtin so..)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __del__: when to use it? What happens when you SystemExit/NameError wrt del? Method vs function calls.

2016-03-07 Thread Veek. M
Steven D'Aprano wrote:

> On Monday 07 March 2016 17:13, Veek. M wrote:
> 
>> import foo
>> class Bar(object):
>> def __del__(self, foo=foo):
>> foo.bar()# Use something in module foo
>> 
>> ### Why the foo=foo? import foo, would increment the ref-count for
>> object 'foo'
> 
> Yes. And then at shutdown, the module globals get deleted so it gets
> decremented again and then garbage collected.
> 
> You cannot tell what order objects will be deleted, so you cannot tell
> whether your instance will be deleted before or after foo. If it
> happens before foo, then __del__ will run and you will be fine. If it
> happens after foo, then foo is gone and your __del__ method will fail.
> 
> 
>> so why go to all the effort of passing it in to every
>> instance via the local stack (ref-to-object-foo)?
> 
> So that every instance has a guaranteed and reliable reference to foo
> that cannot be garbage collected before that instance.
> 
> Here is a sketch of what happens without the foo=foo:
> 
> import foo  # refcount = 1
> x = Bar()  # make an instance
> y = Bar()  # and another instance
> ...
> z = Bar()  # another instance
> ...
> ...
> del z  # z gets garbage collected, everything is fine
> ...
> sys.exit()  # Shutdown starts, x and y still exist
> # Python starts garbage collecting objects
> # in some unknown order...
> ...
> ...
> del x  # x gets garbage collected, foo still exists so everything is
> fine
> del foo  # refcount = 0, so foo gets garbage collected
> del y  # y gets garbage collected, but foo is gone!
> 
> 
> 
> 
> Here is a sketch of what happens with the foo=foo:
> 
> import foo  # refcount = 1
> x = Bar()  # make an instance, foo refcount = 2
> y = Bar()  # and another instance, foo refcount = 3
> ...
> z = Bar()  # another instance, foo refcount = 4
> ...
> ...
> del z  # z gets garbage collected, foo refcount = 3
> ...
> sys.exit()  # Shutdown starts, x and y still exist
> # Python starts garbage collecting objects
> # in some unknown order...
> ...
> ...
> del x  # x gets garbage collected, foo refcount = 2
> del y  # y gets garbage collected, foo refcount = 1
> del foo  # refcount = 0, so foo gets garbage collected
> 
> 
> 
> This guarantees that so long as there are any instances yet to be
> garbage collected, foo cannot be be garbage collected. Only after the
> last of those instances are gone will foo be garbage collected.
> 
> 
> 
>> Text for 1,2 from Beazley:
>> It’s important to note that in some cases the __del__() method might
>> not be invoked at program termination.This can occur if circular
>> references exist between objects (in which case objects may be
>> allocated but accessible from no known name-space).Although Python’s
>> garbage collector can reclaim unused circular references dur-ing
>> execution, it isn’t normally invoked on program termination.
>> 
>> ### which begs the question, why not on program termination?
> 
> I'm afraid I don't know. Perhaps read the source code?
> 
> 
>> How bad can
>> it be since you are terminating anyway. __del__ seems like a total
>> waste product method
> 
> I won't say "total" waste product, but automatic destructor methods
> are only good for very simple use-cases where you don't care that they
> are called in a non-deterministic fashion.
> 
> 
> 
Hi thanks! I kind of posted and your reply came through. Got it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172

2016-03-07 Thread Veek. M
import socket
class Client(object):
 def __init__(self,addr):
  self.server_addr = addr
  self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  self.sock.connect(addr)

 def __getstate__(self):
  return self.server_addr
 
 def __setstate__(self,value):  
  self.server_addr = value
  self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  self.sock.connect(self.server_addr)
-

We'd use it like so:
x = Client(192.168.0.1)
import pickle
pickle.dump(x) #getstate gets called and returns IP for pickling.

#program exits, we restart it
x=Client(None)
x = pickle.load(fh) #Is the IP passed to setstate??
x.__setstate__(self, unpickledIP) ???
Is this correct?

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


exec "x = 3; print x" in a - How does it work?

2016-03-08 Thread Veek. M
What is the return value of `exec`? Would that object be then used to 
iterate the sequence in 'a'? I'm reading this: 
https://www.python.org/download/releases/2.2.3/descrintro/ 
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   >