What's the best forum to get help with Pandas?

2020-02-20 Thread Luca



subject has it all. Thanks
--
https://mail.python.org/mailman/listinfo/python-list


confused by matplotlib and subplots

2020-04-01 Thread Luca



Hello Covid fighters and dodgers,

I'm sort of confused by what I am seeing in a Pandas book.

This works:

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax3.plot(np.random.randn(50).cumsum(), 'k--');

but also this works!

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
plt.plot(np.random.randn(50).cumsum(), 'k--');

(the second one is actually the example in the book).

Why does it work? Isn't axX referring to one of the subplots and plt to 
the plot as a whole?


Thanks

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


print small DataFrame to STDOUT and read it back into dataframe

2020-04-04 Thread Luca



possibly a stupid question. Let's say I have a (small) dataframe:

import pandas as pd
dframe = pd.DataFrame({'A': ['a0','a1','a2','a3'],
'B': ['b0','b1','b2','b3'],
'C': ['c0','c1','c2','c3'],
'D': ['d0','d1','d2','d3']}, index=[0,1,2,3])

Is there a way that I can ask this dataframe to "print itself" in a way 
that I can copy that output and easily rebuild the original dataframe 
with index, columns and all?


dframe.to_string

gives:



Can I evaluate this string to obtain a new dataframe like the one that 
generated it?


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


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Luca

On 4/6/2020 4:08 AM, Reto wrote:

Does this help?


Thank you, but not really. What I am trying to achieve is to have a way 
to copy and paste small yet complete dataframes (which may be the result 
of previous calculations) between a document (TXT, Word, GoogleDoc) and 
Jupiter/IPython.


Did I make sense?

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


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Luca

On 4/6/2020 3:03 PM, Christian Gollwitzer wrote:





CSV is the most sensible option here. It is widely supported by 
spreadsheets etc. and easily copy/pasteable.


Thank you Christian.

so, given a dataframe, how do I make it print itself out as CSV?

And given CSV data in my clipboard, how do I paste it into a Jupiter 
cell (possibly along with a line or two of code) that will create a 
dataframe out of it?



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


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Luca

On 4/6/2020 8:51 PM, Reto wrote:

out = df.to_csv(None)
new = pd.read_csv(io.StringIO(out), index_col=0)


Thank you, brother. It works

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


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-07 Thread Luca

On 4/6/2020 11:05 PM, Luca wrote:

On 4/6/2020 8:51 PM, Reto wrote:

out = df.to_csv(None)
new = pd.read_csv(io.StringIO(out), index_col=0)


Thank you, brother. It works



BTW, a little gotcha (I write this in case someone gets here in the 
future through Google or something)


"""
import pandas as pd
import numpy as np
import io
df = pd.DataFrame(10*np.random.randn(3,4))
df = df.astype(int)
out = df.to_csv(None)

# out == ',0,1,2,3\n0,9,4,-5,-2\n1,16,12,-1,-5\n2,-2,8,0,6\n'

new = pd.read_csv(io.StringIO(out), index_col=0)

#gotcha
type(df.iloc[1,1]) # numpy.int32
type(new.iloc[1,1]) # numpy.int64
"""

new == out will return a dataframe of False

0   1   2   3
0   False   False   False   False
1   False   False   False   False
2   False   False   False   False

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


Using sh library with grep command

2013-11-23 Thread Luca
I'm trying to use sh (https://pypi.python.org/pypi/sh) for calling
system grep command but it's now working as expected.

An example:

import sh
sh.grep('abc', os.getcwd(), '-r')

But I get the ErrorReturnCode_1: exception, that I learned is the
normal exit code for grep command when it not found any match.

The error instance object reported that the command run is:

*** ErrorReturnCode_1:
  RAN: '/usr/bin/grep abc /Users/keul/test_sh'

Obviously manually running the command I get some output and exit code 0.

Where I'm wrong?

-- 
-- luca

twitter: http://twitter.com/keul
linkedin: http://linkedin.com/in/lucafbb
blog: http://blog.keul.it/
-- 
https://mail.python.org/mailman/listinfo/python-list


vb dll

2006-03-06 Thread Luca
Hello at all.
I have to use a vb dll called ndxComm.dll

I have do this but i get the following error:

 from win32com import client
 from win32com.client import Dispatch

 avatar = Dispatch("NdxComm")
 avatar.Reset(1, 20, True)

pywintypes.com_error:(-2147221005, "stringa dell'interfaccia non
valida.", None, None)
frame4.py:avatar = Dispatch(NdxComm")
_init_.py:dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
dynamic.py : return(_GetGoodDispatch(IDispatch, clsctx), userName)
dynamic.py: IDspatch = pythoncom.CoCreateInstance(IDspatch, None,
clsctx,pythoncom.IID_IDispatch)

Can you help me to make it in the right way.

Thanks for your help

Regars

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


Re: vb dll

2006-03-06 Thread Luca
I have solve

Thanks

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


Re: Pyserial again

2006-03-06 Thread Luca
> and than i store the read data in a textctrl.

That's got nothing to do with pyserial.

Ok i know but for this reason i put :

 Another point is the textctrl: it store only the first byte and not
the
other why?, the byte are like 22 and all are readed
---
No clue.  I don't know what "textctrl" is or what it's supposed
to do.

a = ser.readline()
a = self.textCtrl3.SetValue(a + ' ')

For my bad understanding is clear

---
What makes you think the port closed?

 Sniffing the data with serial port monitor i see that the
> answer is ok but after the answer the port close, why?

The serial port monitor write com port close.

--


Dear Grant

As you know i'm not a programmer, but i try to do this in my free time.
Probably you have reason to write:

The code you post is irrelevant.

But i try to do my best as in my possibility.

I think  that in any case you can't run my code if you haven't the same
device that i have.

So i don't want that you loose time with my stupid question, i'm sure
you have a lot of better things to do that help a beginner like me.

In any case thanks for your help, and you cordility.

Best Regards

Luca

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


python i2c ioctl

2007-06-14 Thread luca

Hi,
 I was trying to make to work directly l i2c with python with the
calls ioctl. But I have of the problems and I do not succeed to go
ahead.

this and l error

[EMAIL PROTECTED] /usr/local/wrap]129# python pcf8591_ioctl.py
Reading from 4 ch 8 bit A/D converter PCF8591
Traceback (most recent call last):
  File "pcf8591_ioctl.py", line 269, in ?
main()
  File "pcf8591_ioctl.py", line 229, in main
if i2c_open () < 0:
  File "pcf8591_ioctl.py", line 168, in i2c_open
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), 0,
iomask)
TypeError: ioctl requires a file or file descriptor, an integer and
optionally a integer or buffer argument

the code

import fcntl
import os
###
#etrax.h
ETRAXGPIO_IOCTYPE = 43
GPIO_MINOR_A = 0
GPIO_MINOR_B = 1
GPIO_MINOR_LEDS = 2
GPIO_MINOR_G = 3
GPIO_MINOR_LAST = 3
IO_READBITS = 0x1
IO_SETBITS =  0x2
IO_CLRBITS =  0x3
IO_HIGHALARM = 0x4
IO_LOWALARM = 0x5
IO_CLRALARM = 0x6
IO_LEDACTIVE_SET = 0x7
IO_READDIR  =  0x8
IO_SETINPUT =  0x9
IO_SETOUTPUT = 0xA
IO_LED_SETBIT = 0xB
IO_LED_CLRBIT = 0xC
IO_SHUTDOWN  = 0xD
IO_GET_PWR_BT = 0xE
IO_CFG_WRITE_MODE = 0xF

def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )
def IO_CFG_WRITE_MODE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )

IO_READ_INBITS =  0x10
IO_READ_OUTBITS = 0x11
IO_SETGET_INPUT = 0x12
IO_SETGET_OUTPUT = 0x13
###
###
#ioctl.h

_IOC_NRBITS = 8
_IOC_TYPEBITS=  8
_IOC_SIZEBITS   =14
_IOC_DIRBITS = 2
_IOC_NRMASK = ((1 << _IOC_NRBITS)-1)
_IOC_TYPEMASK = ((1 << _IOC_TYPEBITS)-1)
_IOC_SIZEMASK   =((1 << _IOC_SIZEBITS)-1)
_IOC_DIRMASK=((1 << _IOC_DIRBITS)-1)
_IOC_NRSHIFT=0
_IOC_TYPESHIFT  =(_IOC_NRSHIFT+_IOC_NRBITS)
_IOC_SIZESHIFT  =(_IOC_TYPESHIFT+_IOC_TYPEBITS)
_IOC_DIRSHIFT   =(_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NONE=0
_IOC_WRITE=1
_IOC_READ=2
def _IOC(dir,type,nr,size):
(((dir)  << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr)   << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT))

def _IO(type,nr):
_IOC(_IOC_NONE,(type),(nr),0)

def _IOR(type,nr,size):
_IOC(_IOC_READ,(type),(nr),sizeof(size))

def _IOW(type,nr,size):
_IOC(_IOC_WRITE,(type),(nr),sizeof(size))

def _IOWR(type,nr,size):
_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))

def _IOC_DIR(nr):
(((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
def _IOC_TYPE(nr):
(((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
def _IOC_NR(nr):
(((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
def _IOC_SIZE(nr):
(((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
def IOC_IN():
(_IOC_WRITE << _IOC_DIRSHIFT)

def IOC_OUT():
(_IOC_READ << _IOC_DIRSHIFT)
def IOC_INOUT():
((_IOC_WRITE|_IOC_READ) << _IOC_DIRSHIFT)
def IOCSIZE_MASK():
(_IOC_SIZEMASK << _IOC_SIZESHIFT)
def IOCSIZE_SHIFT():
(_IOC_SIZESHIFT)
##
##
##



I2C_DATA_LINE  = 1<<24
I2C_CLOCK_LINE = 1<<25


#Get the SDA line state
def i2c_getbit():
value=fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_READBITS))
if ((value&(I2C_DATA_LINE))==0):
return 0
else:
return 1

#Set the SDA line as output
def i2c_dir_out():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_OUTPUT), iomask)

#Set the SDA line as input
def i2c_dir_in():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETGET_INPUT), iomask)

#Set the SDA line state
def i2c_data(state):
if (state==1):
i2c_dir_in()
else:
i2c_dir_out()
fcntl.ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS),
I2C_DATA_LINE)
#Set the SCL line state
def i2c_clk(state):
if (state==1):
ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS), 
I2C_CLOCK_LINE)
else:
ioctl(i2c_fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS), 
I2C_CLOCK_LINE)

#Read a byte from I2C bus and send the ack sequence
#Put islast = 1 is this is the last byte to receive from the slave

def i2c_inbyte(islast):
value = 0

#Read data byte

i2c_clk(0)
i2c_dir_in()
for i in range (0,5):
i2c_clk(1)
bitvalue = i2c_getbit()
value |= bitvalue
if (i<7):
value = 1
i2c_clk(0)

if (islast==0):
#Send Ack if is not the last byte to read
i2c_dir_out()
i2c_data(0)
i2c_clk(1)
i2c_clk(0)
i2c_dir_in()
else:
#Doesn't send Ack if is the last byte to read
i2c_dir_in()
   

zip function for python

2008-01-17 Thread Luca
Hi, Ive written this easy and fast function cause there it was
impossible for me to find one that zip a directory without external
libraries:

import zipfile
import sys
import os

def zipdir(zipPath,directory="./"):
 """Store the cdontent of directory to a zipPath file, if directory is
not given stores the content of the current dir
( [EMAIL PROTECTED] )"""
 directory=os.path.realpath(directory)
 zipObject = zipfile.ZipFile(zipPath, 'w')
 for root, dirs, files in os.walk(directory):
  for file in files:
   arcfile=root[len(os.path.commonprefix((directory, root)))
+1:]+"/"+file #retrieves the inner relative file path
   filepath=os.path.join(root,file)
   zipObject.write(filepath,arcfile)
 zipObject.close()
 return zipObject #for optional further elaborations
-- 
http://mail.python.org/mailman/listinfo/python-list


Read a content file from a P7M

2009-03-11 Thread Luca
Hi all.

There is standard or sugested way in python to read the content of a P7M file?

I don't need no feature like verify sign, or sign using a certificate.
I only need to extract the content file of the p7m (a doc, a pdf, ...)

Thanks!

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


Re: Read a content file from a P7M

2009-03-12 Thread Luca
On Wed, Mar 11, 2009 at 5:05 PM, Luca  wrote:
> There is standard or sugested way in python to read the content of a P7M file?
>
> I don't need no feature like verify sign, or sign using a certificate.
> I only need to extract the content file of the p7m (a doc, a pdf, ...)

I'm there again!

I found a document and some exaples related to the M2Crypto library
http://eckhart.stderr.org/doc/python-m2crypto-doc/doc/howto.smime.html

I tryed to use this:

>>> from M2Crypto import BIO, SMIME, X509
>>> p7, data = SMIME.smime_load_pkcs7('/Users/luca/Desktop/testsigned.pdf.p7m')
Traceback (most recent call last):
  File "", line 1, in ?
  File "build/bdist.macosx-10.5-i386/egg/M2Crypto/SMIME.py", line 91,
in smime_load_pkcs7
M2Crypto.SMIME.SMIME_Error: no content type

May be this is the wrong library... but even Google can't help me a
lot with this problem :-(


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


Wiki syntax available when publishing on pypi

2009-03-24 Thread Luca
A non python, but pypi only related question:

Where I can find a reference guide about all what I can do with the
text infos about a pypi egg published?
Looking at others guys code I lear how to make links, bold text,
italic, lists...
For example one time I found how to insert an image in the text, but I
don't remember how.

There is a official refence about this?

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


Re: Wiki syntax available when publishing on pypi

2009-03-25 Thread Luca
On Tue, Mar 24, 2009 at 3:06 PM, andrew cooke  wrote:
>
> it's some form of restructured text, which is described at
> http://docutils.sourceforge.net/rst.html
>
> however, there seem to be various implementations; i don't know if pypi
> exactly follows what is described there (i know i had a small problem with
> some detail being inconsistent with that convention used in sphinx).

It works! Thanks you!


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


Newbie question about import

2008-04-25 Thread Luca
Hi all. I'm trying to do something with python import but isn't working for me.

Using python 2,5 I've a program structured like this:

* a main module called (for example) "mommy" with an __init__.py and a
file called "mommy.py"
* a __version__ var defined inside the main __init__.py

>From the mommy.py file I need to import the __version__ var, but I'm
really not able to do this! I fear this is a very stupid task to do...
my problem is that the file is called like the module.

Anyone can point me to the solution?

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


Re: Newbie question about import

2008-04-27 Thread Luca
On Sat, Apr 26, 2008 at 4:14 AM, Gabriel Genellina
<[EMAIL PROTECTED]> wrote:
>  The short answer is: don't do that! __init__.py may import any module, but
> other modules in the package should not import anything from __init__.py
>  The same rule applies to the main module in an application: it can import
> any other required module, but no one should import main.
>  If you don't follow those rules you may encounter some surprises.
>  You *can* break the rules and actually do what you want, but I would not
> reccomend it.
>  In this case, can't you switch the place where __version__ is defined? It
> looks like a constant, so you could have it actually defined in mommy.py,
> and inside __init__.py just import the value.
>

Ok, thanks all for helping.

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


Know if a object member is a method

2008-09-01 Thread Luca
Hi all.

I think this is a newbie question... what is the best method to know
if a property of an object is a function?

I'm thinking something as

if type(obj.methodName)==???

Can someone help me?

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


Re: Know if a object member is a method

2008-09-01 Thread Luca
On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> That's relatively fragile, since such names aren't reserved in any way.
> It's easy to fool a name comparison check with an accidental name
> collision:
>
>>>> class function(object):  # not a reserved name
> ... pass
> ...
>>>> x = function()
>>>> type(x).__name__
> 'function'
>>>> x()  # must be a function then...
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: 'function' object is not callable
>
>
> But not so easy to fool a type check:
>
>>>> type(x) == new.function
> False
>
> Of course that's not bullet-proof either. I leave it as an exercise to
> discover how you might break that piece of code.
>

Ok, so...

What is the best way to do this? The "most pythonic"?


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


Re: Know if a object member is a method

2008-09-01 Thread Luca
On Mon, Sep 1, 2008 at 2:18 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
>
> If your aim is to write something like a debugger, profiler, or some
> other application that needs to inspect arbitrary objects and work out
> what they do, then you probably should be using:
>
> isinstance(obj.methodName, new.instancemethod)
>
> But remember that not all callable attributes are instancemethods!
>
> There is no one right way of doing this.
>
> Hope this helps.

Yes, this helps a lot. In facts I need to do something like a language parser.

Thanks all.

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


Clustering technique

2009-12-22 Thread Luca
Dear all, excuse me if i post a simple question.. I am trying to find
a software/algorythm that can "cluster" simple data on an excel sheet

Example:
Variable a   Variable b   Variable c
Case 11   0  0
Case 20   1  1
Case 31   0  0
Case 41   1  0
Case 50   1  1


The systems recognizes that there are 3 possible clusters:

the first with cases that has Variable a as true,
the second has Variables b and c
the third is "all the rest"

Variabile aVariabile b   Variabile c

Case 1 1   00
Case 3 1   00

Case 2 0   11
Case 5 0   11

Case 4 1   10


Thank you in advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Pool module -- does map pre-assign tasks to processes?

2009-09-28 Thread Luca
I would like to use the Pool module, but my tasks take sometimes
unpredictably different time to complete.  The simplest way to write
the code would be to put all task descriptions in an array, then call

p = Pool(8)
p.map(f, a)

But I don't want to preassign process 0 with elements a[0], a[8], a
[16],  process 1 with elements a[1], a[9], a[17], and so forth.
Rather, I would like all tasks to be put into a queue, and I would
like the processes to each time grab the next task to be done, and do
it.  This would ensure fairly equal loading.

My question is: does the map() method of Pool pre-assign which task
gets done by each process, or is this done at runtime, on a get-first-
task-to-be-done basis?

Many thanks,

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


Re: How to know if a file is a text file

2009-11-15 Thread Luca
On Sat, Nov 14, 2009 at 6:51 PM, Philip Semanchuk  wrote:
> Hi Luca,
> You have to define what you mean by "text" file. It might seem obvious, but
> it's not.
>
> Do you mean just ASCII text? Or will you accept Unicode too? Unicode text
> can be more difficult to detect because you have to guess the file's
> encoding (unless it has a BOM; most don't).
>
> And do you need to verify that every single byte in the file is "text"? What
> if the file is 1GB, do you still want to examine every single byte?
>
> If you give us your own (specific!) definition of what "text" means, or
> perhaps a description of the problem you're trying to solve, then maybe we
> can help you better.
>

Thanks all.

I was quite sure that this is not a very simple task. Right now search
only inside ASCII encode is not enough for me (my native language is
outside this encode :-)
Checking every single byte can be a good solution...

I can start using the mimetype module and, if the file has no
extension, check byte one by one (commonly) as "file" command does.
Better: I can check use the "file" command if available.

Again: thanks all!

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


Renaming identifiers & debugging

2010-02-25 Thread Luca
Hello, i am trying to develop an application to teach programming to 
young kids in a similar way as Logo did in the past. I would like to use 
an embedded Python as underlying language but this raises a problem.


The target of my app are very young kids that might be unfamiliar with 
english, so i am wondering if there is a way to rename/redefine 
identifiers and messages in the language of the kid.


In UCB-Logo this is very easy with the command
  COPYDEF "newidentifier "oldidentifier
so all you have to do is setup a startup script to redefine all the 
identifiers to the language of the user.


Is there anything similar for python? Since python would be embedded it 
would not be a problem for me to do it through some API.



Also, i would need a way to debug the program, so set up breakpoints, 
execute line by line, inspect variables, is there any API for this in 
embedded python?


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


Re: Renaming identifiers & debugging

2010-02-25 Thread Luca

Chris Rebert wrote:

On Thu, Feb 25, 2010 at 7:26 AM, Luca  wrote:

Hello, i am trying to develop an application to teach programming to young
kids in a similar way as Logo did in the past. I would like to use an
embedded Python as underlying language but this raises a problem.

The target of my app are very young kids that might be unfamiliar with
english, so i am wondering if there is a way to rename/redefine identifiers
and messages in the language of the kid.

In UCB-Logo this is very easy with the command
 COPYDEF "newidentifier "oldidentifier
so all you have to do is setup a startup script to redefine all the
identifiers to the language of the user.

Is there anything similar for python? Since python would be embedded it
would not be a problem for me to do it through some API.


It can certainly be done (c.f. ChinesePython -
http://www.chinesepython.org/cgi_bin/cgb.cgi/english/english.html),
but I know of no framework that simplifies the task. Essentially, you
just have to manually modify Python's parser by swapping out the
english for the other language (and if you want to mess with the basic
types' names, their name definitions somewhere else too). There also
might be encoding issues to deal with.

Cheers,
Chris
--
http://blog.rebertia.com


Yes, i am playing with Python source code, i have changed some keywords 
but the compile-process fails when it tries to compile some of python 
libraries which, of course, use english keywords... so i guess this is 
not a very clean/viable solution (without having to rewrite several 
parts of the libraries). A better solution could be to "duplicate" 
keywords adding a translated version but leaving the original version in 
place so that every module keeps working. In few words, if i was going 
to translate the keyword "if" in, say, italian "se", then i would have 
both "if" and also "se" working at the same time, in the same manner.
I think that the best way to do this is to insert a "filter" somewhere 
that converts every "se" keyword into a "if" keyword so that python 
doesn't even see the change.


What i would like to do is add a new keyword (lets call it "copydef" 
like in UCBLogo or whatever) that does this work at runtime by keeping a 
substitution table in RAM. If i could manage to add this new keyword to 
python then it would be easy to write a startup script that translates 
the keywords once python is "up and running" and without breaking 
existing python programs/libraries (unless the new keyword conflicts 
with functions defined inside these programs/libraries).


Thanx,
  Luca
--
http://mail.python.org/mailman/listinfo/python-list


Re: Renaming identifiers & debugging

2010-02-25 Thread Luca

Diez B. Roggisch wrote:
You could use import-hooks for importing your code. There was a 
python-magazine article a while ago that showed how to use that + a 
parser to import seamlessly a DSL.


I will look into this, right now i don't know what import-hooks are nor 
if i can use them from embedded python.


Using pyparsing to write a python-grammar on the fly that simply 
exchanges the keywords before passing it to the interpreter is also easy.


This could be an easy solution yes... unfortunately this makes other 
problems arise... for instance... if i have an IDE that shows the 
translated language and i want to debug it step by step... would it be 
easy for me to map the english version (the one that python sees) with 
the translated version? I still have to look at this and how to run the 
debugger from the application that is "hosting" python so for now the 
answer is "i don't know".


Right now, by simply changing the file Python-3.1.1/Grammar/Grammar i 
could obtain this (with "if" translated in "se"):


Python 3.1.1 (r311:74480, Feb 25 2010, 22:44:50)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Vero = True
>>> stampa = print
>>>
>>> se Vero:
...   stampa("Ciao")
...
Ciao
>>>

Which seems to work, but requires to manually change the source code and 
it is way too complex for someone that has zero knowledge about the 
internals of python (like myself). Besides... it is a very ugly hack IMO.


But to be honest: I doubt it's worth the effort. Really. Teaching people 
how to code, but in something that is *not* the "real" language is of 
little, if any, benefit.


And also I don't think that your concerns are valid in general. Keywords 
are like brandnames or other things - the stand for a concept, and 
people immediatly accept them when they want them.


Maybe you are right, but being italian myself i can remember when i was 
a middle schooler (no computer before that) and the hours spent on my 
MSX figuring out how the thing worked. I learned all the commands as 
"brandnames" without really understanding them. I had _no_ idea that 
"if" meant... if, or that "print" meant to print. I had zero knowledge 
of english and for this reason most of these commands were meaningless 
to me, i didn't even suspect they had a meaning in an existing language, 
i just thought that was the way computers talked. When several years 
later, in High School, i learned English it was a real surprise for me 
to find out that the commands i had learned by heart as "magical words" 
actually had a precise meaning in "human language" and i think this 
would have helped me to figure out several things that instead took 
hours or days to understand.
Now kids have internet, they study english from the kindergarten, they 
have a huge quantity of manuals and books freely available, so maybe you 
are right and i am just over-worrying. But still, i think it would be 
nice to use a language familiar to the kid (especially very young ones), 
it would make the learning process faster (no need to learn new words) 
and it would make the computer feel almost like a virtual "friend".


Much, much, much more important would - if anything - be a 
standard-library, or a wrapper for that, e.g. for turtle-graphics, that 
was written in terms of your desired language.


I already made some tests with turtle-graphics and it seems to work. My 
next problem was to translate the language.


http://img192.imageshack.us/img192/3093/zzz5.png

[Btw... i took too much time to write this post so the ladybug fell 
asleep...]


Thanx,
  Luca
--
http://mail.python.org/mailman/listinfo/python-list


Re: Renaming identifiers & debugging

2010-02-25 Thread Luca

MRAB wrote:

Perhaps you could use a different extension, eg ".pyn", so existing
".py" files are handled as-is but ".pyn" files are read through a
translator.


This could be a good idea... especially since i could make my own 
extension since we are talking of a special-purpose application that 
only incorporates python as a library.


Thanx,
  Luca
--
http://mail.python.org/mailman/listinfo/python-list


Re: Renaming identifiers & debugging

2010-02-25 Thread Luca

News123 wrote:

a pragmatic approach might be to preprocess the source code
and keep the original version as comments above the translated line.
(for debugging)


Not very nice to see though. This change should be transparent to the 
kid, he/she should not be aware of this "translation".



or to preprecess the input line before the 'exec'.


This could be a solution, yes, but then i would also need to figure out 
a way to show the original line when debugging (still, i know nothing 
about pdb, maybe it is possible in a easy way).



It might be, that kids are flexible enough to use English words without
understanding them.

My younger brother could write Pascal before he learnt any English and
he never cared, what 'if' 'then' 'else' 'for', etc. meant.


I had a similar experience. When i was a middle schooler i knew nothing 
about English and so i learned Basic and then Pascal without actually 
knowing that those words had a meaning at all. For me they were just 
kind of "magical words" that i could use to tell the computer what to 
do. So i know for sure that this is possible. But... imagine the use of 
this language in a elementary school. Some kids, like me or you or your 
brother would be interested enough to find it easy to learn the new 
words. For others it would only be a pain. Another lesson among others 
that to them makes no sense at all. If we could make it easier for them 
to understand the words used in the language the teacher would have an 
easier time to explain some concepts and maybe even these kids could 
have fun and begin to love it.


I understand that this "breaks" the language, that it would be better if 
they would just learn the "original" language... but most teachers would 
ask "then why not use Logo instead? it is already in our language..."


I have nothing against logo of course, i have learned it myself even 
before Pascal and i had lots of fun with it. But python is much more 
widespread than logo and i think in some ways it makes "more sense" than 
logo and it is not much harder. The only problem that i see is that Logo 
does have its commands translated while python doesn't nor, it seems, 
there is an easy way to add this functionality.



It might be useful though to internationalize the python errror messages
if that's not something already done.


Yes, this would be very important... but still, it should be easy for 
the teacher to translate the messages to make them more understandable 
to the kids.


For instance...

* Python:
>>> draw
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'draw' is not defined

* Logo:
? draw
I don't know how  to draw

Logo's message, while maybe too simple for a professional developer, is 
certainly much more easy for a kid. So you see, it is not just a matter 
of internationalization... it is also a matter of users-target. If i am 
talking to a young kid i should use words and concepts that are easy for 
him/her to understand... so, for my target, it would be very important 
not only a correct translation in my language, but also the use of words 
and concepts that are familiar to a kid.


Thanx,
  Luca
--
http://mail.python.org/mailman/listinfo/python-list


Re: Renaming identifiers & debugging

2010-02-26 Thread Luca

Roald de Vries wrote:
I would suggest to do choose the same strategy as 'from __future__ 
import ...' takes, which does similar things and limits them to the 
module it is used in. I would be curious to hear about your results.


Kind regards, Roald


Hi,
well, i have thought on the issue and i think that i have found a 
solution that does not imply renaming the keywords. It mixes a bit the 
suggestions i got here and this is how.


My application will have a text editor (with, hopefully, code 
highlighting) a text-box to insert "instant" commands and a text-box for 
the output and a window for the "turtle".


With the "instant commands" you can insert little pieces of python code 
and see instantly their result both as a turtle movement or textual 
output. With the text-editor you can load/save/write whole python 
programs and execute or debug them step by step.


The way to track the code could use this technique 
http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html 
which gives me exactly the number of line of code under execution and 
also stops the code until the function "treaceit" returns. This will 
allow me to insert breakpoints, execute the code line by line, and so on 
(or at least this is the theory).


Now. Since i get the number of the line i don't need anymore to have the 
language deal with translated keywords. The kid can write the code in 
his/her own language and the program will "translate it back to english" 
before passing them to the embedded python. When i save a source file i 
can save the english version and translate it "on the fly" when i load 
it. I can also intercept error codes and translate them as well, showing 
the kid only what i want him/her to see. So it would be completely 
transparent to the kid and it would notice it only by opening the source 
code via a different text-editor. In this way the files produced are 
100% python files and the kid can disable this feature if he doesn't 
like it. This will also allow me to translate the same piece of code in 
any language i want, with 0 effort and this way will allow kids of 
different nations exchange their code and have it working on their computer.


This seems a good idea to me, it reaches my points without breaking 
compatibility. The kid will see the language in his own language but the 
computer will actually work with the standard-python file.


Once i have this "ready" i can proceed to other steps and try to 
implement other ideas that i have (remember c-robots and p-robots?).


Bye,
  Luca
--
http://mail.python.org/mailman/listinfo/python-list


raw_input with a pre-compiled data

2009-06-20 Thread Luca
Hi all.

I need to use a function like the raw_input to read data from user
command line, but I really like to pre-compile the choice and I'm not
able to do this. There is some other function/module I can use?
I wanna to pre-compile the raw_input input line with the current working path.

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


Re: raw_input with a pre-compiled data

2009-06-21 Thread Luca
On Sat, Jun 20, 2009 at 6:38 PM, Chris Rebert wrote:
> On Sat, Jun 20, 2009 at 7:17 AM, Luca wrote:
>> Hi all.
>>
>> I need to use a function like the raw_input to read data from user
>> command line, but I really like to pre-compile the choice and I'm not
>> able to do this. There is some other function/module I can use?
>> I wanna to pre-compile the raw_input input line with the current working 
>> path.
>
> What does "pre-compile" mean in this context? It's not clear at all,
> so I can't even understand your question.

I'm really sorry to all that replied... In fact I literally traduced a
concept from italian and the term precompiled was not so clear...

What I mean is this: I wanna that raw_input (or other modules) ask to
the user the data, but some data inserted is already present, to the
user can cancel it with the BACKSPACE key.

Examples below (the "_" is the initial cursor position).

 >>> raw_input("Insert the directory to work: ")
 Insert the directory to work: _

What I need is:
 >>> XXX_raw_input("Insert the directory to work: ", default="/home/john/")
 Insert the directory to work: /home/john_

In the second example a user can cancel the text "/home/john".

I hope this time is more clear, sorry again.

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


Re: raw_input with a pre-compiled data

2009-06-22 Thread Luca
On Sun, Jun 21, 2009 at 12:51 PM, Peter Otten<__pete...@web.de> wrote:
> With "traduced" you stumbled upon another false friend ;)
>
> http://it.wikipedia.org/wiki/Falso_amico

D'oh!!!   x-)

> import readline
>
> def input_default(prompt, default):
>    def startup_hook():
>        readline.insert_text(default)
>    readline.set_startup_hook(startup_hook)
>    try:
>        return raw_input(prompt)
>    finally:
>        readline.set_startup_hook(None)
>
> print input_default("directory? ", default="/home/john")
>

Thanks! It works! This is working on Linux and MacOS too.

> The readline module is specific to Unix implementations.
> I don't know what OS the OP was using.

Any one knows is this working also on Windows? I've no Win system
right no to test this...

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


Re: Read a content file from a P7M

2009-08-28 Thread Luca
On Fri, Mar 20, 2009 at 4:12 PM, Emanuele Rocca wrote:
> On 11/03/09 - 05:05, Luca wrote:
>> There is standard or sugested way in python to read the content of a P7M 
>> file?
>>
>> I don't need no feature like verify sign, or sign using a certificate.
>> I only need to extract the content file of the p7m (a doc, a pdf, ...)
>
> For PDF files you can just remove the P7M content before %PDF and after
> %%EOF.
>
> The following snippet converts /tmp/test.p7m into PDF, saving the
> resulting document into /tmp/test.pdf:
>
> import re
> from gzip import GzipFile
>
> contents = GzipFile('/tmp/test.p7m').read()
>
> contents_re = re.compile('%PDF-.*%%EOF', re.MULTILINE | re.DOTALL)
> contents = contents_re.search(contents).group()
>
> open('/tmp/test.pdf', 'w').write(contents)
>

After all those days... only to say THANKS!
The example of the PDF file is perfect, I only needed to not execute
the GzipFile call (it seems that our PDF are not GZipped).

Unluckily now seems that we need the same feature for non-pdf files...

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


Re: Control webbrowser from Python script

2010-10-10 Thread luca
On Oct 9, 11:39 am, Johny  wrote:
> Is it possible to control any webbrowser from Python ? For example to
> issue http POST and GET  command
> Thanks
> Johny

I'm using funkload and it is awesome!

http://funkload.nuxeo.org/

FunkLoad is a functional and load web tester, written in Python, whose
main use cases are:

Functional testing of web projects, and thus regression testing as
well.
Performance testing: by loading the web application and monitoring
your servers it helps you to pinpoint bottlenecks, giving a detailed
report of performance measurement.
Load testing tool to expose bugs that do not surface in cursory
testing, like volume testing or longevity testing.
Stress testing tool to overwhelm the web application resources and
test the application recoverability.
Writing web agents by scripting any web repetitive task, like checking
if a site is alive.
-- 
http://mail.python.org/mailman/listinfo/python-list


Piping processes works with 'shell = True' but not otherwise.

2013-05-24 Thread Luca Cerone
Hi everybody, 
I am new to the group (and relatively new to Python)
so I am sorry if this issues has been discussed (although searching for topics 
in the group I couldn't find a solution to my problem).

I am using Python 2.7.3 to analyse the output of two 3rd parties programs that 
can be launched in a linux shell as:

 program1 | program2

To do this I have written a function that pipes program1 and program2 (using 
subprocess.Popen) and the stdout of the subprocess, and a function that parses 
the output:

A basic example:

from subprocess import Popen, STDOUT, PIPE
def run():
  p1 = Popen(['program1'], stdout = PIPE, stderr = STDOUT)
  p2 = Popen(['program2'], stdin = p1.stdout, stdout = PIPE, stderr = STDOUT)
  p1.stdout.close()
  return p2.stdout


def parse(out):
  for row in out:
print row
#do something else with each line
  out.close()
  return parsed_output
   

# main block here

pout = run()

parsed = parse(pout)

#--- END OF PROGRAM #

I want to parse the output of 'program1 | program2' line by line because the 
output is very large.

When running the code above, occasionally some error occurs (IOERROR: [Errno 
0]). However this error doesn't occur if I code the run() function as:

def run():
  p = Popen('program1 | program2', shell = True, stderr = STDOUT, stdout = PIPE)
  return p.stdout

I really can't understand why the first version causes errors, while the second 
one doesn't.

Can you please help me understanding what's the difference between the two 
cases? 

Thanks a lot in advance for the help,
Cheers, Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-26 Thread Luca Cerone
> 
> Can you please help me understanding what's the difference between the two 
> cases? 
> 

Hi guys has some of you ideas on what is causing my issue?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-26 Thread Luca Cerone
> Could you provide the *actual* commands you're using, rather than the generic 
> "program1" and "program2" placeholders? It's *very* common for people to get 
> the tokenization of a command line wrong (see the Note box in 
> http://docs.python.org/2/library/subprocess.html#subprocess.Popen for some 
> relevant advice).
> 
Hi Chris, first of all thanks for the help. Unfortunately I can't provide the 
actual commands because are tools that are not publicly available.
I think I get the tokenization right, though.. the problem is not that the 
programs don't run.. it is just that sometimes I get that error..

Just to be clear I run the process like:

p = subprocess.Popen(['program1','--opt1','val1',...'--optn','valn'], ... the 
rest)

which I think is the right way to pass arguments (it works fine for other 
commands)..

> 
> Could you provide the full & complete error message and exception traceback?
> 
yes, as soon as I get to my work laptop..

> 
> One obvious difference between the 2 approaches is that the shell doesn't 
> redirect the stderr streams of the programs, whereas you /are/ redirecting 
> the stderrs to stdout in the non-shell version of your code. But this is 
> unlikely to be causing the error you're currently seeing.
> 
> 
> You may also want to provide /dev/null as p1's stdin, out of an abundance of 
> caution.
>

I tried to redirect the output to /dev/null using the Popen argument:
'stdin = os.path.devnull' (having imported os of course)..
But this seemed to cause even more troubles...
 
> Lastly, you may want to consider using a wrapper library such as 
> http://plumbum.readthedocs.org/en/latest/ , which makes it easier to do 
> pipelining and other such "fancy" things with subprocesses, while still 
> avoiding the many perils of the shell.
> 
> 
Thanks, I didn't know this library, I'll give it a try.
Though I forgot to mention that I was using the subprocess module, because I 
want the code to be portable (even though for now if it works in Unix platform 
is OK).

Thanks a lot for your help,
Cheers,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-27 Thread Luca Cerone
> 
> 
> Will it violate privacy / NDA to post the command line? Even if we
> 
> can't actually replicate your system, we may be able to see something
> 
> from the commands given.
> 
> 

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


Re: Create a file in /etc/ as a non-root user

2013-05-31 Thread Luca Cerone
> fd = open('/etc/file','w')
> 
> fd.write('jpdas')
> 
> fd.close()
> 
> 
Hi Bibhu, that is not a Python problem, but a permission one.
You should configure the permissions so that you have write access to the 
folder.
However unless you know what you are doing it is discouraged to save your
file in the /etc/ folder.

I don't know if on Mac the commands are the same, but in Unix systems (that I 
guess Mac is) you can manage permissions with chmod.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-31 Thread Luca Cerone
> 
> That's because stdin/stdout/stderr take file descriptors or file
> 
> objects, not path strings.
> 

Thanks Chris, how do I set the file descriptor to /dev/null then?
-- 
http://mail.python.org/mailman/listinfo/python-list


error bluetooth

2012-10-05 Thread Luca Sanna
the code is output the error of the ubuntu

from bluetooth import *

target_name = "My Phone"
target_address = None

nearby_devices = discover_devices()

for address in nearby_devices:
if target_name == lookup_name( address ):
target_address = address
break

if target_address is not None:
print "found target bluetooth device with address", target_address
else:
print "could not find target bluetooth device nearby"

the error

luca@luca-XPS-M1330:~/py-temperature/py-temperature$ python bluetooth.py
Traceback (most recent call last):
  File "bluetooth.py", line 14, in 
    from bluetooth import *
  File "/home/luca/py-temperature/py-temperature/bluetooth.py", line 19, in 

nearby_devices = discover_devices()
NameError: name 'discover_devices' is not defined
luca@luca-XPS-M1330:~/py-temperature/py-temperature$ 

it's a bug of the module? thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error bluetooth

2012-10-05 Thread Luca Sanna
Il giorno venerdì 5 ottobre 2012 13:33:14 UTC+2, Hans Mulder ha scritto:
> On 5/10/12 10:51:42, Luca Sanna wrote:
> 
> 
> 
> > from bluetooth import *
> 
> 
> 
> [..]
> 
> 
> 
> > luca@luca-XPS-M1330:~/py-temperature/py-temperature$ python bluetooth.py
> 
> 
> 
> When you say "from bluetooth import *", Python will find a file
> 
> name "bluetooth.py" and import stuff from that file.  Since your
> 
> script happens to be named "bluetooth.py", Python will import
> 
> your script, thinking it is a module.
> 
> 
> 
> > it's a bug of the module?
> 
> 
> 
> You've chosen the wrong file name.  Rename your script.
> 
> 
> 
> 
> 
> Hope this helps,
> 
> 
> 
> -- HansM



i'm sorry, it's ok the rename file in bt.py

how do I send a ping in bluetooth?
because android phones are not always visible.
I can not find the ping command
thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


calendar from python to html

2012-10-05 Thread Luca Sanna
hi,

I enter a calendar in an html page
in each calendar day, I enter a time that is used by the program to perform 
actions with python

What can I use to do this?

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


ping in bluetooth

2012-11-02 Thread Luca Sanna
hi,
how do I send a ping in bluetooth? 
because android phones are not always visible. 
I can not find the ping command 
thanks 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Selection sort

2021-12-24 Thread Luca Anzilli
Hello

try this code

def selectionsort(arr):
#le=len(arr)
for b in range(0,len(arr)-1):
   # pos=b
for a in range(b+1,len(arr)):
if arr[b]>arr[a]:
arr[b],arr[a]=arr[a],arr[b]
return arr

arr=[3,5,9,8,2,6]
print(selectionsort(arr))

Il giorno ven 24 dic 2021 alle ore 15:50 Mats Wichmann 
ha scritto:

> On 12/24/21 07:22, vani arul wrote:
> > Hello,
> > I am trying write a code.Can some help me find the error in my code.
> > Thanks!
> >
> >
> > def selectionsort(arr):
> ># le=len(arr)
> > for b in range(0,len(arr)-1):
> > pos=b
> > for a in range(b+1,len(arr)-1):
> > if arr[b]>arr[a+1]:
> > arr[b],arr[a+1]=arr[a+1],arr[b]
> > return arr
> >
> > arr=[3,5,9,8,2,6]
> > print(selectionsort(arr))
> >
>
> Hint: what are you using 'pos' for?  A placeholder for (something) has
> an actual purpose in the typical selection-sort, but you only initialize
> it and never use or update it.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


variable exchange

2018-10-09 Thread Luca Bertolotti
Hello i'm using python with pyqt but i have a problem on varable:
I have two class:

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

from Ui_form import Ui_Form
from form_cli import Form_cli


class Form(QWidget, Ui_Form):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor

@param parent reference to the parent widget
@type QWidget
"""
super(Form, self).__init__(parent)
self.setupUi(self)
self.ftc = Form_cli()etc


from PyQt5.QtCore import pyqtSlot, QModelIndex
from PyQt5.QtWidgets import QWidget, QTableWidgetItem, QLineEdit
from PyQt5.QtSql import QSqlTableModel
from PyQt5.QtSql import QSqlDatabase
from PyQt5.QtCore import Qt




from Ui_form_cli import Ui_Form



class Form_cli(QWidget, Ui_Form):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor

@param parent reference to the parent widget
@type QWidget
"""
super(Form_cli, self).__init__(parent)
self.setupUi(self)
self.db = QSqlDatabase()
self.tableWidget.setRowCount(1)
self.tableWidget.setColumnCount(10)


>From the class Form_cli how i can modify a textedit that is in the class Form?

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


Re: variable exchange

2018-10-09 Thread Luca Bertolotti
Thanks Cameron
but my problem is that i can't access to a line edit of the class Form from the 
class Form_cli i get this error:
'Form_cli' object has no attribute 'lineEdit_29'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using sh library with grep command

2013-11-23 Thread Luca Fabbri
On Sat, Nov 23, 2013 at 5:29 PM, Peter Otten <__pete...@web.de> wrote:
> Luca wrote:
>
>> I'm trying to use sh (https://pypi.python.org/pypi/sh) for calling
>> system grep command but it's now working as expected.
>>
>> An example:
>>
>> import sh
>> sh.grep('abc', os.getcwd(), '-r')
>>
>> But I get the ErrorReturnCode_1: exception, that I learned is the
>> normal exit code for grep command when it not found any match.
>>
>> The error instance object reported that the command run is:
>>
>> *** ErrorReturnCode_1:
>>   RAN: '/usr/bin/grep abc /Users/keul/test_sh'
>>
>> Obviously manually running the command I get some output and exit code 0.
>>
>> Where I'm wrong?
>
> Did you run grep with or without the -r option?
>
> The code sample and the error message don't match. Maybe you accidentally
> left out the -r in your actual code.
>

Sorry all, it was a stupid error and I provided a bad example.

I was running...
   sh.grep('"abc"', os.getcwd(), '-r')

...and the output of the command inside the exception was exactly...
RAN: '/usr/bin/grep "abc" /Users/keul/test_sh -r'

So, at first glance it was ok (copying/pasting it in in the terminal
return exactly was I expected).

The error? The doublequote inclusion.

Using this...
   sh.grep('abc', os.getcwd(), '-r')

...I  get this output...
RAN: '/usr/bin/grep abc /Users/keul/test_sh -r'

But this time I get the expected result (both in terminal and python
env). So seems that when quoting you get a false positive right
command output but a wrong execution.


-- 
-- luca

twitter: http://twitter.com/keul
linkedin: http://linkedin.com/in/lucafbb
blog: http://blog.keul.it/
-- 
https://mail.python.org/mailman/listinfo/python-list


Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
Hi everybody,
I am trying to understand how to use named pipes in python to launch external 
processes (in a Linux environment).

As an example I am trying to "imitate" the behaviour of the following sets of 
commands is bash:

> mkfifo named_pipe
> ls -lah > named_pipe &
> cat < named_pipe

In Python I have tried the following commands:

import os
import subprocess as sp

os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash..
fw = open("named_pipe",'w')
#at this point the system hangs...

My idea it was to use subprocess.Popen and redirect stdout to fw...
next open named_pipe for reading and giving it as input to cat (still using 
Popen).

I know it is a simple (and rather stupid) example, but I can't manage to make 
it work..


How would you implement such simple scenario?

Thanks a lot in advance for the help!!!

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


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
Hi Paul, first of all thanks for the help.

I am aware of the first solutions, just now I would like to experiment a bit 
with  using named pipes (I also know that the example is trivial, but it just 
to grasp the main concepts)

> 
> You can also pass a file object to p1's stdout and p2's stdin if you want to 
> >pipe via a file.
 
> 
> with open("named_pipe", "rw") as named_pipe:
>     p1 = subprocess.Popen(["ls", "-lah"], stdout=named_pipe)
>     p2 = subprocess.Popen(["cat"], stdin=named_pipe)
> 
>     p1.wait()
>     p2.wait()
>  

Your second example doesn't work for me.. if named_file is not a file in the 
folder I get an error saying that there is not such a file.

If I create named_pipe as a named pipe using os.mkfifo("named_file",0777) than 
the code hangs.. I think it is because there is not process that reads the
content of the pipe, so the system waits for the pipe to be emptied.

Thanks a lot in advance for the help in any case.
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
Hi MRAB, thanks for the reply!
> 
> Opening the pipe for reading will block until it's also opened for
> 
> writing, and vice versa.
> 

OK.

> 
> 
> In your bash code, 'ls' blocked until you ran 'cat', but because you
> 
> ran 'ls' in the background you didn't notice it!
> 
> 
Right.
> 
> In your Python code, the Python thread blocked on opening the pipe for
> 
> writing. It was waiting for another thread or process to open the pipe
> 
> for reading.

OK. What you have written makes sense to me. So how do I overcome the block?
As you said in bash I run the ls process in background. How do I do that in 
Python?

Thanks again for the help,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
Hi Alister,
> Are you sure you are using the correct tool for the task?

Yes. For two reasons: 1. I want to learn how to do this in Python :) 2. for an 
application I have in mind I will need to run external tools (not developed by 
me) and process the output using some tools that I have written in Python.

For technical reasons I can't use the subprocess.communicate() (the output to 
process is very large) method, and due to a bug in the interactive shell I am 
using (https://github.com/ipython/ipython/issues/3884) I cannot pipe processes 
just using the standard subprocess.Popen() approach.

> 
> I tend to find that in most cases if you are trying to execute bash 
> 
> commands from Python you are doing it wrong.

As I said, the example in my question is just for learning purposes, I don't 
want to reproduce ls and cat in Python...

I just would like to learn how to handle named pipes in Python, which I find it 
easier to do by using a simple example that I am comfortable to use :)

Thanks in any case for your answer,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
Thanks MRAB,
> 
> You need to ensure that the pipe is already open at the other end.

So I need to open the process that reads the pipe before writing in it?

> 
> 
> 
> Why are you using a named pipe anyway?

For some bug in ipython (see my previous email) I can't use subprocess.Popen 
and pipe in the standard way.
One of Ipython developers has suggested me to use named pipes as a temporary 
workaround. So I am taking the occasion to learn :)

 
> 
> If you're talking to another program, then that needs to be running
> 
> already, waiting for the connection, at the point that you open the
> 
> named pipe from this end.

I am not entirely sure I got this: ideally I would like to have a function that 
runs an external tool (the equivalent of ls in my example) redirecting its 
output in a named pipe.

A second function (the cat command in my example) would read the named_pipe, 
parse it and extract some features from the output.

I also would like that the named_pipe is deleted when the whole communication 
is ended.


> 
> If you're using a pipe _within_ a program (a queue would be better),
> 
> then you should opening for writing in one thread and for reading in
> 
> another.

Let's stick with the pipe :) I will ask about the queue when I manage to use 
pipes ;)

I should have explained better that I have no idea how to run threads in Python 
:): how do I open a thread that executes "ls -lah" in background and writes 
into a named pipe? And how do I open a thread that reads from the named pipe?

Can you please post a small example, so that I have something to work on?

Thanks a lot in advance for your help!

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


Re: Piping processes works with 'shell = True' but not otherwise.

2013-08-05 Thread Luca Cerone
thanks and what about python 2.7?
> 
> 
> In Python 3.3 and above:
> 
> 
> 
> p = subprocess.Popen(..., stderr=subprocess.DEVNULL)

P.s. sorry for the late reply, I discovered I don't receive notifications from 
google groups..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
Thanks this works (if you add shell=True in Popen).
If I don't want to use shell = True, how can I redirect the stdout to 
named_pipe? Popen accepts an open file handle for stdout, which I can't open 
for writing because that blocks the process...

> 
> 
> os.mkfifo("named_pipe", 0777)
> 
> ls_process = subprocess.Popen("ls -lah > named_pipe")
> 
> pipe = open("named_pipe", "r")
> 
> # Read the output of the subprocess from the pipe.
> 
> 
> 
> When the subprocess terminates (look at the docs for Popen objects),
> 
> close and delete the fifo.

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


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-05 Thread Luca Cerone
> You're back to using separate threads for the reader and the writer.
>
And how do I create separate threads in Python? I was trying to use the 
threading library without not too success..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simulate `bash` behaviour using Python and named pipes.

2013-08-06 Thread Luca Cerone
> my_thread.join()

Thanks! I managed to make it work using the threading library :)

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


Using Pool map with a method of a class and a list

2013-08-06 Thread Luca Cerone
Hi guys,
I would like to apply the Pool.map method to a member of a class.

Here is a small example that shows what I would like to do:

from multiprocessing import Pool

class A(object):
   def __init__(self,x):
   self.value = x
   def fun(self,x):
   return self.value**x


l = range(10)

p = Pool(4)

op = p.map(A.fun,l)

#using this with the normal map doesn't cause any problem

This fails because it says that the methods can't be pickled.
(I assume it has something to do with the note in the documentation: 
"functionality within this package requires that the __main__ module be 
importable by the children.", which is obscure to me).

I would like to understand two things: why my code fails and when I can expect 
it to fail? what is a possible workaround?

Thanks a lot in advance to everybody for the help!

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


Re: Using Pool map with a method of a class and a list

2013-08-06 Thread Luca Cerone
On Tuesday, 6 August 2013 18:12:26 UTC+1, Luca Cerone  wrote:
> Hi guys,
> 
> I would like to apply the Pool.map method to a member of a class.
> 
> 
> 
> Here is a small example that shows what I would like to do:
> 
> 
> 
> from multiprocessing import Pool
> 
> 
> 
> class A(object):
> 
>def __init__(self,x):
> 
>self.value = x
> 
>def fun(self,x):
> 
>return self.value**x
> 
> 
> 
> 
> 
> l = range(10)
> 
> 
> 
> p = Pool(4)
> 
> 
> 
> op = p.map(A.fun,l)
> 
> 
> 
> #using this with the normal map doesn't cause any problem
> 
> 
> 
> This fails because it says that the methods can't be pickled.
> 
> (I assume it has something to do with the note in the documentation: 
> "functionality within this package requires that the __main__ module be 
> importable by the children.", which is obscure to me).
> 
> 
> 
> I would like to understand two things: why my code fails and when I can 
> expect it to fail? what is a possible workaround?
> 
> 
> 
> Thanks a lot in advance to everybody for the help!
> 
> 
> 
> Cheers,
> 
> Luca



On Tuesday, 6 August 2013 18:12:26 UTC+1, Luca Cerone  wrote:
> Hi guys,
> 
> I would like to apply the Pool.map method to a member of a class.
> 
> 
> 
> Here is a small example that shows what I would like to do:
> 
> 
> 
> from multiprocessing import Pool
> 
> 
> 
> class A(object):
> 
>def __init__(self,x):
> 
>self.value = x
> 
>def fun(self,x):
> 
>return self.value**x
> 
> 
> 
> 
> 
> l = range(10)
> 
> 
> 
> p = Pool(4)
> 
> 
> 
> op = p.map(A.fun,l)
> 
> 
> 
> #using this with the normal map doesn't cause any problem
> 
> 
> 
> This fails because it says that the methods can't be pickled.
> 
> (I assume it has something to do with the note in the documentation: 
> "functionality within this package requires that the __main__ module be 
> importable by the children.", which is obscure to me).
> 
> 
> 
> I would like to understand two things: why my code fails and when I can 
> expect it to fail? what is a possible workaround?
> 
> 
> 
> Thanks a lot in advance to everybody for the help!
> 
> 
> 
> Cheers,
> 
> Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-06 Thread Luca Cerone
Hi Chris, thanks

> Do you ever instantiate any A() objects? You're attempting to call an
> 
> unbound method without passing it a 'self'.

I have tried a lot of variations, instantiating the object, creating lambda 
functions that use the unbound version of fun (A.fun.__func__) etc etc..
I have played around it quite a bit before posting.

As far as I have understood the problem is due to the fact that Pool pickle the 
function and copy it in the various pools.. 
But since the methods cannot be pickled this fails..

The same example I posted won't run in Python 3.2 neither (I am mostly 
interested in a solution for Python 2.7, sorry I forgot to mention that).

Thanks in any case for the help, hopefully there will be some other advice in 
the ML :)

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


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
Hi Joshua thanks!

> I think you might not understand what Chris said.
> Currently this does *not* work with Python 2.7 as you suggested it would.
> >>> op = map(A.fun,l)

Yeah actually that wouldn't work even in Python 3, since value attribute used 
by fun has not been set.
It was my mistake in the example, but it is not the source of the problem..

> This, however, does:
> >>> op = map(A(3).fun,l)
> 
> >>> op
> 
> [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683]
> 
> 

This works fine (and I knew that).. but is not what I want...

You are using the map() function that comes with Python. I want
to use the map() method of the Pool class (available in the multiprocessing 
module).

And there are differences between map() and Pool.map() apparently, so that if 
something works fine with map() it may not work with Pool.map() (as in my case).

To correct my example:

from multiprocessing import Pool

class A(object):
def __init__(self,x):
self.value = x
def fun(self,x):
return self.value**x

l = range(100)
p = Pool(4)
op = p.map(A(3).fun, l)

doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 
for my application).

> You will find that 
> http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-> 
> > when-using-pythons-multiprocessing-pool-ma 
> explains the problem in more detail than I understand. I suggest 
> reading it and relaying further questions back to us. Or use Python 3 

:) Thanks, but of course I googled and found this link before posting. I don't 
understand much of the details as well, that's why I posted here.

Anyway, thanks for the attempt :)

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


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
> > doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 
> > 3 for my application).
>  
> Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though.

No I am using Ubuntu (12.04, 64 bit).. maybe things changed from 3.2 to 3.3?
 
> from multiprocessing import Pool
> 
> from functools import partial
> 
> 
> 
> class A(object):
> 
> def __init__(self,x):
> 
> self.value = x
> 
> def fun(self,x):
> 
> return self.value**x
> 
> 
> 
> def _getattr_proxy_partialable(instance, name, arg):
> 
> return getattr(instance, name)(arg)
> 
> 
> 
> def getattr_proxy(instance, name):
> 
> """
> 
> A version of getattr that returns a proxy function that can
> 
> be pickled. Only function calls will work on the proxy.
> 
> """
> 
> return partial(_getattr_proxy_partialable, instance, name)
> 
> 
> 
> l = range(100)
> 
> p = Pool(4)
> 
> op = p.map(getattr_proxy(A(3), "fun"), l)
> 
> print(op)

I can't try it now, I'll let you know later if it works!
(Though just by reading I can't really understand what the code does).

Thanks for the help,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
Thanks for the post.
I actually don't know exactly what can and can't be pickles..
not what partialing a function means..
Maybe can you link me to some resources?
 
I still can't understand all the details in your code :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
Thanks for the help Peter!

> 
> 
> 
> >> def make_instancemethod(inst, methodname):
> 
> >> return getattr(inst, methodname)
> 
> > 
> 
> > This is just getattr -- you can replace the two uses of
> 
> > make_instancemethod with getattr and delete this ;).
> 
> 
> 
> D'oh ;)

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


"Nested" virtual environments

2013-08-09 Thread Luca Cerone
Dear all, is there a way to "nest" virtual environments?

I work on several different projects that involve Python programming.

For a lot of this projects I have to use the same packages (e.g. numpy, scipy, 
matplotlib and so on), while having to install packages that are specific
for each project.

For each of this project I created a virtual environment (using virtualenv 
--no-site-packages) and I had to reinstall the shared packages in each of them.

I was wondering if there is a way to nest a virtual environment into another,
so that I can create a "common" virtual environment  that contains all the
shared packages and then "specialize" the virtual environments installing the 
packages specific for each project.

In a way this is not conceptually different to using virtualenv 
--system-site-packages, just instead of getting access to the system packages a 
virtual environment should be able to access the packages of an other one.

Thanks a lot in advance for the help,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Nested" virtual environments

2013-08-16 Thread Luca Cerone
Thanks Marcel,
I will give it a try during the weekend and let you know if it worked for me :)
> 
> If you have a recent version of pip, you can use wheels [1] to save built 
> packages locally. First create a new virtualenv and install the common 
> packages. Then put these packages in a wheel directory. Then, for any other 
> virtualenv that need the common packages, you can easily install then from 
> the wheel directory (this is fast even for numpy & friends, because nothing 
> will be compiled again) [2].
> 
> 
> 
> # Create a new virtualenv
> 
> virtualenv myenv
> 
> source myenv/bin/activate
> # Install the wheel package
> pip install wheel
> # Install your common packages
> 
> pip install numpy scipy matplotlib
> # Create a requirements file
> pip freeze > /local/requirements.txt
> # Create wheel for the common packages
> pip wheel --wheel-dir=/local/wheels -r /local/requirements.txt
> 
> 
> Now you have all the built packages saved to /local/wheels, ready to install 
> on any other environment. You can safely delete myenv. Test it with the 
> following:
> 
> # Create a virtualenv for a new project
> 
> virtualenv myproj
> source myproj/bin/activate
> # Install common packages from wheel
> pip install --use-wheel --no-index --find-links=/local/wheels -r 
> /local/requirements.txt
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> [1] https://wheel.readthedocs.org
> [2] 
> http://www.pip-installer.org/en/latest/cookbook.html#building-and-installing-wheels
> 
> 
> 
> 
> 
> 
> 2013/8/9 Luca Cerone 
> 
> Dear all, is there a way to "nest" virtual environments?
> 
> 
> 
> I work on several different projects that involve Python programming.
> 
> 
> 
> For a lot of this projects I have to use the same packages (e.g. numpy, 
> scipy, matplotlib and so on), while having to install packages that are 
> specific
> 
> for each project.
> 
> 
> 
> For each of this project I created a virtual environment (using virtualenv 
> --no-site-packages) and I had to reinstall the shared packages in each of 
> them.
> 
> 
> 
> I was wondering if there is a way to nest a virtual environment into another,
> 
> so that I can create a "common" virtual environment  that contains all the
> 
> shared packages and then "specialize" the virtual environments installing the 
> packages specific for each project.
> 
> 
> 
> In a way this is not conceptually different to using virtualenv 
> --system-site-packages, just instead of getting access to the system packages 
> a virtual environment should be able to access the packages of an other one.
> 
> 
> 
> 
> Thanks a lot in advance for the help,
> 
> Luca
> 
> --
> 
> http://mail.python.org/mailman/listinfo/python-list

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


How to keep cookies when making http requests (Python 2.7)

2013-08-20 Thread Luca Cerone
Hi everybody,
I am trying to write a simple Python script to solve the "riddle" at:
http://quiz.gambitresearch.com/

The quiz is quite easy to solve, one needs to evaluate the expression between 
the curly brackets (say that the expression has value )
and go to the web page:

http://quiz.gambitresearch/job/

You have to be fast enough, because with the page there is an associated cookie 
that expires 1 sec after the first request, therefore you need to be quick to 
access the /job/ page.

[I know that this is the correct solution because with a friend we wrote a 
small script in JavaScript and could access the page with the email address]

As an exercise I have decided to try doing the same with Python.

First I have tried with the following code:

#START SCRIPT

import re
import urllib2

regex = re.compile(r'\{(.*)\}')
base_address = "http://quiz.gambitresearch.com/";
base_h = urllib2.urlopen(base_address)
base_page = base_h.read()

val = str(eval(regex.findall(base_page)[0]))

job_address = base_address + "job/" + val
job_h = urllib2.urlopen(job_address)
job_page = job_h.read()

print job_page
#END SCRIPT

job_page has the following content now: "WRONG! (Have you enabled cookies?)"

Trying to solve the issues with the cookies I found the "requests" module that 
in theory should work.
I therefore rewrote the above script to use request:

#START SCRIPT:
import re
import requests

regex = re.compile(r'\{(.*)\}')

base_address = "http://quiz.gambitresearch.com/";

s = requests.Session()
 
base_h = s.get('http://quiz.gambitresearch.com/')
base_page = base_h.text

val = eval( regex.findall( base_page )[0] )

job_address = base_address + "job/" + str(val)
job_h = s.get( job_address )
job_page = job_h.text

print job_page
#END SCRIPT
# print job_page produces "Wrong!".

According to the manual using Session() the cookies should be enabled and 
persistent for all the session. In fact the cookies in base_h.cookies and in 
job_h.cookies seem to be the same:

base_h.cookies == job_h.cookies
#returns True

So, why does this script fail to access the job page?
How can I change it so that I it works as intended and job_page prints
the content of the page that displays the email address to use for the job 
applications?

Thanks a lot in advance for the help!

Best Wishes,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to keep cookies when making http requests (Python 2.7)

2013-08-21 Thread Luca Cerone
> 
> I have used "cookielib" externally to "urllib2". It looks
> 
> like this:
> 
> from urllib2 import urlopen, Request
> 
> from cookielib import CookieJar
> cookies = CookieJar()
> 
> 
> 
> r = Request(...)
> 
> cookies.add_cookie_header(r) # set the cookies
> 
> R = urlopen(r, ...) # make the request
> 
> cookies.extract_cookies(R, r) # remember the new cookies

Hi Dieter,
thanks a lot for the help.
I am sorry but your code is not very clear to me.
It seems that you are setting some cookies,
but I can't understand how you use the ones that the site
sends to you when you perform the initial request.

Have you tried this code to check if this work?
If it works as intended can you explain a bit better
what it does exactly?

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


Re: How to keep cookies when making http requests (Python 2.7)

2013-08-27 Thread Luca Cerone
Dear all, 
first of all thanks for the help.
As for your remark, you are right, and I usually tend to post questions in a 
way that is detached from the particular problem I have to solve.
In this case since I only have a limited knowledge of the cookies mechanism (in 
general, not only in Python), I preferred to ask for the specific case.
I am sorry if I gave you the impression I didn't appreciate your answer,
it was absolutely not my intention.

Cheers,
Luca
> Let me make an additional remark however: you should
> 
> not expect to get complete details in a list like this - but only
> 
> hints towards a solution for your problem (i.e.
> 
> there remains some work for you).
> 
> Thus, I expect you to read the "cookielib/cookiejar" documentation
> 
> (part of Python's standard documentation) in order to understand
> 
> my example code - before I would be ready to provide further details.

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


Re: How to keep cookies when making http requests (Python 2.7)

2013-08-27 Thread Luca Cerone
> 
> > Let me make an additional remark however: you should
> > not expect to get complete details in a list like this - but only
> > hints towards a solution for your problem (i.e. 
> > there remains some work for you).
> > Thus, I expect you to read the "cookielib/cookiejar" documentation
> > (part of Python's standard documentation) in order to understand
> > my example code - before I would be ready to provide further details.

Ok so after reading the documentation for urllib2 and cookielib I came up with 
the following code:

#START
from urllib2 import urlopen , Request
from cookielib import CookieJar
import re
regex = re.compile(r'\{(.*)\}')

base_url = "http://quiz.gambitresearch.com";
job_url  = base_url + "/job/"

cookies = CookieJar()
r = Request(base_url) #prepare the request object
cookies.add_cookie_header(r) #allow to have cookies
R = urlopen(r) #read the url
cookies.extract_cookies(R,r) #take the cookies from the response R and adds 
#them to the request object 

#build the new url
t = R.read()
v = str(eval(regex.findall(t)[0]))
job_url = job_url + v


# Here I create a new request to the url containing the email address
r2 = Request(job_url)
cookies.add_cookie_header(r2) #I prepare the request for cookies adding the 
cookies that I extracted before.

#perform the request and print the page
R2 = urlopen(r2)
t2 = R2.read()
print job_url
print t2
#END

This still doesn't work, but I really can't understand why.
As far as I have understood first I have to instantiate a Request object
and allow it to receive and set cookies (I do this with r = Request() and 
cookies.add_cookie_header(r))
Next I perform the request (urlopen),  save the cookies in the CookieJar 
(cookies.extract_cookies(R,r)).

I evaluate the new address and I create a new Request for it (r2 = Request)
I add the cookies stored in the cookiejar in my new request 
(cookies.add_cookie_header(r2))
Then I perform the request (R2 = urlopen(r2)) and read the page (t2 = R2.read())

What am I doing wrong? Do I misunderstand something in the process?

Thanks again in advance for the help,
Cheers,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to keep cookies when making http requests (Python 2.7)

2013-08-30 Thread Luca Cerone
Thanks Dieter,
 
> With respect to cookie handling, you do everything right.
> 
> 
> 
> There may be other problems with the (wider) process.
> 
> Analysing the responses of your requests (reading the status codes,
> 
> the response headers and the response bodies) may provide hints
> 
> towards the problem.
> 

I will try to do that and try to see if I can figure out why.

> 
> 
> >Do I misunderstand something in the process?
> 
> 
> 
> Not with respect to cookie handling.

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


Sphinx Doctest: test the code without comparing the output.

2013-09-21 Thread Luca Cerone
Dear all,
I am writing the documentation for a Python package using Sphinx.

I have a problem when using doctest blocks in the documentation:
I couldn't manage to get doctest to run a command but completely ignoring
the output.

For example, how can I get a doctest like the following to run correctly?

.. doctest:: example_1

   >>> import random
   >>> x = random.uniform(0,100)
   >>> print str(x)
   #some directive here to completely ignore the output

Since I don't know the value of `x`, ideally in this doctest I only want
to test that the various commands are correct, regardless of
the output produced.

I have tried using the ELLIPSIS directive, but the problem is that the `...` 
are interpreted as line continuation rather than `any text`:

.. doctest:: example_2

   >>> import random
   >>> x = random.uniform(0,100)
   >>> print str(x) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
   ...

I don't know if there is a way to make Sphinx understand that I want to ignore 
the whole output. I think the easiest way to solve this, would be 
differentiating between the ellipsis sequence and the line continuation 
sequence, but I don't know how to do that.

I know that I could skip the execution of print(str(x)) but this is not what I 
want; I really would like the command to be executed the output ignored.
Can you point me to any solution for this issue?

Thanks a lot in advance for your help,
Cheers,
Luca
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-21 Thread Luca Cerone
Dear Steven,
thanks for the help.

I am aware that I might have used the SKIP directive (as I hinted in my mail).
Even if the fine manual suggests to do so I don't agree with it, though.
The reason is simple: SKIP as the name suggests causes the code not to be run 
at all, it doesn't ignore the output. If you use a SKIP directive on code that 
contains a typo, or maybe you changed
the name of a keyword to make it more meaningful and forgot to update your 
docstring, then the error won't be caught.

For example:

.. doctest:: example

   >>> printt "Hello, World!" # doctest: +SKIP
   "Hello, World!"

would pass the test. Since I am writing a tutorial for people that have even 
less experience than me with Python, I want be sure that the code in my 
examples runs just fine.

> 
> (There's no need to convert things to str before printing them.)
>

You are right, I modified an example that uses x in one of my functions that 
requires a string in input, and didn't change that.

Thanks again for the help anyway,

Cheers,
Luca
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-21 Thread Luca Cerone
> And if you ignore the output, the error won't be caught either. What's 
> 
> the difference?
> 
> >>> 1 + 1  #doctest:+IGNORE_OUTPUT  (not a real directive)
> 
> 1000
> 
> 

The difference is that in that case you want to check whether the result is 
correct or not, because you expect a certain result.

In my case, I don't know what the output is, nor care for the purpose of the 
tutorial. What I care is being sure that the command in the tutorial is 
correct, and up to date with the code.

If you try the following, the test will fail (because there is a typo in the 
code)

.. doctest:: example

   >>> printt "hello, world"

and not because the output doesn't match what you expected.

Even if the command is correct:

.. doctest:: example_2

   >>> print "hello, world"

this text will fail because doctest expects an output. I want to be able to 
test that the syntax is correct, the command can be run, and ignore whatever 
the output is.

Don't think about the specific print example, I use this just to show what the 
problem is, which is not what I am writing a tutorial about!

> 
> 
> So you simply can't do what you want. You can't both ignore the output of 
> 
> a doctest and have doctest report if the test fails.
> 

OK, maybe it is not possible using doctest. Is there any other way to do what I 
want? For example using an other Sphinx extension??
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-21 Thread Luca Cerone
> but if you're using this for a
> 
> tutorial, you risk creating a breed of novice programmers who believe
> 
> their first priority is to stop the program crashing. Smoke testing is

Hi Chris,
actually my priority is to check that the code is correct. I changed the syntax 
during the development, and I want to be sure that my tutorial is up to date.

The user will only see the examples that, after testing with doctest, will
run. They won't know that I used doctests for the documentation..

How can I do what you call smoke tests in my Sphinx documentation?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-21 Thread Luca Cerone
> 
> That is not how doctest works. That test fails because its output is:

ok.. is there a tool by which I can test if my code runs regardless the output?

> 
> The only wild-card output that doctest recognises is ellipsis, and like 
> 
> all wild-cards, can match too much if you aren't careful. If ellipsis is 
>

actually I want to match the whole output.. and you can't because ellipsis is 
the same as line continuation...
> 
> 
> 
> will work. But a better solution, I think, would be to pick a 

I think you are sticking too much to the examples I posted, where I used 
functions that are part of Python, so that everybody could run the code and 
test the issues.

I don't use random numbers, so I can't apply what you said.
Really, I am looking for a way to test the code while ignoring the output.

I don't know if usually it is a bad choice, but in my case is what I want/need.

Thanks for the help,
Luca
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-22 Thread Luca Cerone
> This makes no sense. If you ignore the output, the code could do ANYTHING 
> 
> and the test would still pass. Raise an exception? Pass. SyntaxError? 
> 
> Pass. Print "99 bottles of beer"? Pass.
> 

if you try the commands, you can see that the tests fail..
for example

.. doctest::

   >>> raise Exception("test")

will fail with this message: 

File "utils.rst", line 5, in default
Failed example:
raise Exception("test")
Exception raised:
Traceback (most recent call last):
  File "/usr/lib/python2.7/doctest.py", line 1289, in __run
compileflags, 1) in test.globs
  File "", line 1, in 
raise Exception("test")
Exception: test

So to me this seems OK.. "Print" will fail as well...

> 
> 
> I have sometimes written unit tests that just check whether a function 
> 
> actually is callable:
> 
> 
> 
> ignore = function(a, b, c)
> 
> 
> 
> but I've come to the conclusion that is just a waste of time, since there 
> 
> are dozens of other tests that will fail if function isn't callable. But 
> 
> if you insist, you could always use that technique in your doctests:
> 
> 
> 
> >>> ignore = function(a, b, c)
> 
> 
> If the function call raises, your doctest will fail, but if it returns 
> 
> something, anything, it will pass.
> 
> 

I understand your point, but now I am not writing unit tests to check the 
correctness of the code. I am only writing a tutorial and assuming that
the code is correct. What I have to be sure is that the code in the tutorial
can be executed correctly, and some commands print verbose output which can 
change.

It is not enough to write >>> ignore = function(a,b,c) won't work because the 
function still prints messages on screen and this causes the failure of the 
test...

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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-22 Thread Luca Cerone
On Sunday, 22 September 2013 14:39:07 UTC+1, Ned Batchelder  wrote:
> On 9/22/13 12:09 AM, Luca Cerone wrote:
> 
> > Hi Chris,
> 
> > actually my priority is to check that the code is correct. I changed the 
> > syntax
> 
> > during the development, and I want to be sure that my tutorial is up to 
> > date.
> 
> >
> 
> 
> 
> If you do manage to ignore the output, how will you know that the syntax 
> 
> is correct?  The output for an incorrect syntax line will be an 
> 
> exception, which you'll ignore.

if the function raises an exception, the test fails, regardless of the output

>  Maybe I don't know enough about the 
> 
> details of doctest.  It's always seemed incredibly limited to me.  

I agree that has some limitations

> 
> Essentially, it's as if you used unittest but the only assertion you're 
> 
> allowed to make is self.assertEqual(str(X), "")
> 
> 

I don't know unittest, is it possible to use it within Sphinx?

> 
> --Ned.

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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Luca Cerone
> It won't be very good documenation any more but nothing stops you
> 
> from examining the result in the next doctest and making yourself
> 
> happy about it.
> 
> 
> 
>   >>> x = input("indeterminate:")
> 
>   >>> result = "'{}'".format(x))
> 
>   >>> result.startswith("'") and result.endswith("'")
> 
>   True
> 

Hi Neil, thanks for the hint, but this won't work.

The problem is that the function displays some output informing you of what 
steps are being performed (some of which are displayed by a 3rd party function 
that I don't control).

This output "interferes" with the output that should be checked by doctest.

For example, you can check that the following doctest would fail:

.. doctest:: example_fake

   >>> def myfun(x,verbose):
   ...print "random output"
   ...return x
   >>> myfun(10)
   10

When you run make doctest the test fails with this message:

File "tutorial.rst", line 11, in example_fake
Failed example:
myfun(10)
Expected:
10
Got:
random output
10

In this case (imagine that "random output" is really random, therefore I can 
not easily filter it, if not ignoring several lines. This would be quite easy 
if ellipsis and line continuation wouldn't have the same sequence of 
characters, but unfortunately this is not the case.

The method you proposed still is not applicable, because I have no way to use 
startswith() and endswith()...

The following code could do what I want if I could ignore the output...

   >>> def myfun(x,verbose):
   ...print "random output"
   ...return x
   >>> result = myfun(10) #should ignore the output here!
   >>> print result
   10

fails with this message:

File "tutorial.rst", line 11, in example_fake
Failed example:
result = myfun(10)
Expected nothing
Got:
random output

(line 11 contains: >>> result = myfun(10))

A SKIP directive is not feasible either:

.. doctest:: example_fake

   >>> def myfun(x):
   ...print "random output"
   ...return x
   >>> result = myfun(10) # doctest: +SKIP 
   >>> result
   10

fails with this error message:
File "tutorial.rst", line 12, in example_fake
Failed example:
result
Exception raised:
Traceback (most recent call last):
  File "/usr/lib/python2.7/doctest.py", line 1289, in __run
compileflags, 1) in test.globs
  File "", line 1, in 
result
NameError: name 'result' is not defined

As you can see is not that I want something too weird, is just that sometimes 
you can't control what the function display and ignoring the output is a 
reasonable way to implement a doctest.

Hope these examples helped to understand better what my problem is.

Thanks all of you guys for the hints, suggestions and best practices :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Luca Cerone
I don't know why but it seems that google groups stripped the indentation from 
the code. I just wanted to ensure you that in the examples that I have run
the definition of myfunc contained correctly indented code!

On Monday, 23 September 2013 15:45:43 UTC+1, Luca Cerone  wrote:
> .. doctest:: example_fake
> 
> 
> 
>>>> def myfun(x,verbose):
> 
>...print "random output"
> 
>...return x
> 
>>>> myfun(10)
> 
>10
> 
> 
> 
> When you run make doctest the test fails with this message:
> 
> 
> 
> File "tutorial.rst", line 11, in example_fake
> 
> Failed example:
> 
> myfun(10)
> 
> Expected:
> 
> 10
> 
> Got:
> 
> random output
> 
> 10
> 
> 
> 
> In this case (imagine that "random output" is really random, therefore I can 
> not easily filter it, if not ignoring several lines. This would be quite easy 
> if ellipsis and line continuation wouldn't have the same sequence of 
> characters, but unfortunately this is not the case.
> 
> 
> 
> The method you proposed still is not applicable, because I have no way to use 
> startswith() and endswith()...
> 
> 
> 
> The following code could do what I want if I could ignore the output...
> 
> 
> 
>>>> def myfun(x,verbose):
> 
>...print "random output"
> 
>...return x
> 
>>>> result = myfun(10) #should ignore the output here!
> 
>>>> print result
> 
>10
> 
> 
> 
> fails with this message:
> 
> 
> 
> File "tutorial.rst", line 11, in example_fake
> 
> Failed example:
> 
> result = myfun(10)
> 
> Expected nothing
> 
> Got:
> 
> random output
> 
> 
> 
> (line 11 contains: >>> result = myfun(10))
> 
> 
> 
> A SKIP directive is not feasible either:
> 
> 
> 
> .. doctest:: example_fake
> 
> 
> 
>>>> def myfun(x):
> 
>...print "random output"
> 
>...return x
> 
>>>> result = myfun(10) # doctest: +SKIP 
> 
>>>> result
> 
>10
> 
> 
> 
> fails with this error message:
> 
> File "tutorial.rst", line 12, in example_fake
> 
> Failed example:
> 
> result
> 
> Exception raised:
> 
> Traceback (most recent call last):
> 
>   File "/usr/lib/python2.7/doctest.py", line 1289, in __run
> 
> compileflags, 1) in test.globs
> 
>   File "", line 1, in 
> 
> result
> 
> NameError: name 'result' is not defined
> 
> 
> 
> As you can see is not that I want something too weird, is just that sometimes 
> you can't control what the function display and ignoring the output is a 
> reasonable way to implement a doctest.
> 
> 
> 
> Hope these examples helped to understand better what my problem is.
> 
> 
> 
> Thanks all of you guys for the hints, suggestions and best practices :)

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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Luca Cerone
> 
> The docstring for doctest.DocTestRunner contains the example code
> 
> I was looking for.
> 
> 
 Thanks, I will give it a try!
> 
> -- 
> 
> Neil Cerutti

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


Re: What's the best way to extract 2 values from a CSV file from each row systematically?

2013-09-28 Thread Luca Cerone
> I'd really appreciate any suggestions or help, thanks in advance!

Hi Alex if you know that you want only columns 3 and 5, you could also use list 
comprehension to fetch the values:

import csv

with open('yourfile.csv','rU') as fo: 
 #the rU means read using Universal newlines
 cr = csv.reader(fo)
 values_list = [(r[2],r[4]) for r in cr] #you have a list of tuples containing 
the values you need

Cheers,
Luca
-- 
https://mail.python.org/mailman/listinfo/python-list


Let me introduce myself.

2015-04-30 Thread Luca Menegotto

Hello everybody.

One of the common rules i like most is: when you enter in a community, 
introduce yourself!


So here I am! Luca, old developer (50 and still running!), Python (and 
not only) developer, from Marostica, a lovely small town in the 
north-eastern part of Italy.


It's a pleasure for me to notice that an NNTP newsgroup regarding Python 
is alive and kicking!


--
Ciao!
Luca Menegotto.
--
https://mail.python.org/mailman/listinfo/python-list


python paho mqtt thread

2015-05-21 Thread Luca Sanna
hi ,
I try to run this code MQTT but I can not read the messages in the topic , can 
you help me ?
thanks

class MyMQTTClass(Thread):
#def __init__(self, clientid=None):
clientid=None
_mqttc = mqtt.Client(clientid)
#_mqttc.on_message = mqtt_on_message
#_mqttc.on_connect = mqtt_on_connect
#_mqttc.on_publish = mqtt_on_publish
#_mqttc.on_subscribe = mqtt_on_subscribe
db=Mydata()

def mqtt_on_connect(self, mqttc, obj, flags, rc):
#print("rc: "+str(rc))
pass

def mqtt_on_message(self, mqttc, obj, msg):
#print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
if msg.topic=="home/soggiorno/luce":
if msg.paylod=="ON":
self.db.update_configure('power',1,1)
else:
self.db.update_configure('power',0,1)

def mqtt_on_publish(self, mqttc, obj, mid):
#print("mid: "+str(mid))
pass

def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
#print("Subscribed: "+str(mid)+" "+str(granted_qos))
pass

def mqtt_on_log(self, mqttc, obj, level, string):
#print(string)
pass

def run(self):
logCritical("run")
self._mqttc.on_message = self.mqtt_on_message
rc=0
while rc==0:

self._mqttc.connect("192.168.1.60", 1883, 60)
self._mqttc.subscribe("home/soggiorno/temperatura")
self._mqttc.subscribe("home/soggiorno/umidita")
self._mqttc.subscribe("home/soggiorno/luce")
self.mqtt_on_message
rc=self._mqttc.loop()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to show time to execute another function

2015-06-07 Thread Luca Menegotto

Il 07/06/2015 10:22, Cecil Westerhof ha scritto:
> That only times the function. I explicitly mentioned I want both the
> needed time AND the output.
>
> Sadly the quality of the answers on this list is going down

First of all, thank God it's a newsgroup, not a list.

Second, often the quality of an answer is deeply connected to the 
quality of a question. I've red your question, and I still have not 
clear what you want.


If you want a function that returns a result and a time elapsed, why 
don't you pack the time and the result in a tuple, a list or a 
dictionary? it takes 20 seconds, if you are slow.


Else if you want a function that prints time and result, why don't you 
print result more or less in the same manner you do with the time 
elapsed? it takes 20 seconds, if you are slow.


Else, can you explain exactly what do you want?


--
Ciao!
Luca

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


Re: Function to show time to execute another function

2015-06-07 Thread Luca Menegotto

Il 07/06/2015 11:28, Steven D'Aprano ha scritto:



But if your function takes less than, say, 1 millisecond, then your timing
results are probably just meaningless random numbers, affected more by the
other ten thousand processes running on your computer than by the Python
code itself.


That's a good point. IMHO test the time execution of a function makes 
sense only if all these assetions are true:


- the function takes a long time to be executed;
- the execution is iterated a certain (large) number of times.

--
Ciao!
Luca

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


Re: Is it a newsgroup or a list?

2015-06-07 Thread Luca Menegotto

Il 07/06/2015 13:45, Steven D'Aprano ha scritto:


As far as I know, python-list a.k.a. comp.lang.python is the only one of the
Python mailing lists with an official newsgroup mirror.


OK. So let me rephrase: Thank God the list is mirrired to a newsgroup...

--
Ciao!
Luca


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


Re: Flan definition collision

2015-08-11 Thread Luca Menegotto

Il 11/08/2015 08:28, smahab...@google.com ha scritto:

I am importing two modules, each of which is defining flags

> (command line arguments) with the same name. This makes
> it impossible to import both the modules at once, because of flag
> name definition conflict.




If you use 'import', and not 'from xyz import', you avoid any conflict.

Ah: 'from ... import' has caused me a lot of terrible headaches. I don't 
use this statement if not strictly necessary.


--
Bye.
Luca

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


Re: Why Python is not both an interpreter and a compiler?

2015-08-31 Thread Luca Menegotto

Il 31/08/2015 19:48, Mahan Marwat ha scritto:


If it hasn't been considered all that useful, then why

> the tools like cx_freeze, pytoexe are doing very hard!

Well, I consider those tools useless at all!
I appreciate Python because, taken one or two precautions, I can easily 
port my code from one OS to another with no pain.

So, why should I loose this wonderful freedom?

--
Ciao!
Luca

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


Re: Porting Python Application to a new linux machine

2015-09-03 Thread Luca Menegotto

Il 03/09/2015 16:32, Heli Nix ha scritto:


How can I do this in Linux ?


As far as I know, in general a Linux distro comes with Python already 
installed.

All you have to do is check if the installed version matches your needs.
Tipically, you'll find Python 2.7; however, I know there are distros 
with Python3.x as default (Fedora?)


--
Ciao!
Luca


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


Re: Porting Python Application to a new linux machine

2015-09-03 Thread Luca Menegotto

Il 03/09/2015 17:53, Nick Sarbicki ha scritto:

Is 3.x the default on ubuntu now? My 14.10 is still 2.7. Although it
does have python3 installed.


I've checked my Ubuntu 15.04, and the default is 2.7.9.
There is also Python3 (3.4.3), but sorry, I can't remember if I've 
manually installed it or not.


--
Ciao!
Luca

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


Re: continue vs. pass in this IO reading and writing

2015-09-03 Thread Luca Menegotto

Il 03/09/2015 17:05, kbtyo ha scritto:


I am experimenting with many exception handling and utilizing

> continue vs pass. After pouring over a lot of material on SO
> and other forums I am still unclear as to the difference when
> setting variables and applying functions within multiple "for"
> loops.

'pass' and 'continue' have two very different meanings.

'pass' means 'don't do anything'; it's useful when you _have_ to put a 
statement but you _don't_need_ to put a statement.
You can use it everywhere you want, with no other damage then adding a 
little weight to your code.


A stupid example:

if i == 0:
   pass
else:
   do_something()


'continue', to be used in a loop (for or while) means 'ignore the rest 
of the code and go immediatly to the next iteration'. The statement 
refers to the nearest loop; so, if you have two nested loops, it refers 
to the inner one; another stupid example:


for i in range(10):
for j in range(10):
if j < 5: continue
do_something(i, j) # called only if j >= 5

--
Ciao!
Luca

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


Re: Porting Python Application to a new linux machine

2015-09-03 Thread Luca Menegotto

Il 03/09/2015 18:49, Chris Angelico ha scritto:


If you mean that typing "python" runs 2.7, then that's PEP 394 at
work. For compatibility reasons, 'python' doesn't ever run Python 3.


Please forgive me, Il make it clearer.
I'm pretty shure that Ubuntu 15.04 comes with Python 2.7.
I don't remember if Python 3 was preinstalled or if I had to install it 
manually.


--
Ciao!
Luca


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


Re: Check if a given value is out of certain range

2015-09-29 Thread Luca Menegotto

Il 29/09/2015 23:04, Random832 ha scritto:


How about x not in range(11)?



Remember: simpler is better.

--
Ciao!
Luca

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


Re: variable scope of class objects

2015-10-19 Thread Luca Menegotto

Il 19/10/2015 20:39, JonRob ha scritto:


I (think) I understand that in the below case, the word self could be
replaced with "BME280" to explicitly call out a variable.

But even still I don't know how explicit call out effects the scope of
a variable.


These two statements make me think you come from C++ or something similar.

In Python you can declare variables at class level, but this declaration 
must NOT be interpreted in the same manner of a similar declaration in 
C++: they remain at the abstract level of a class, and they have nothing 
to do with an instance of a class (in fact, to be correctly invoked, 
they must be preceeded by the class name).


'self' (or a similar representation, you could use 'this' without 
problem) gives you access to the instance of the class, even in the 
constructor; it is important, because the constructor is the place where 
instance variables should be defined. Something like this:


class foo:
# invoke with foo._imAtClassLevel
_imAtClassLevel = 10

def __init__(self):
#  need to say how this must be invoked?
self._imAtInstanceLevel = 0

no confusion is possible, because:

class foo2:
_variable = 1000

def __init__(self):
# let's initialize an instance variable with
# a class variable
self._variable = foo2._variable

Please, note that declaring a variable in the constructor is only a 
convention: in Python you can add a variable to an object of a class 
wherever you want in your code (even if it is very dangerous and 
discouraged).


--
Ciao!
Luca

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


Re: variable scope of class objects

2015-10-20 Thread Luca Menegotto

Il 20/10/2015 08:38, Nagy László Zsolt ha scritto:


When you say "they have nothing to do", it is almost true but not 100%.


I know it, but when it comes to eradicate an idea that comes directly 
from C++-like languages, you must be drastic.

Nuances come after...

--
Ciao!
Luca
--
https://mail.python.org/mailman/listinfo/python-list


Re: variable scope of class objects

2015-10-21 Thread Luca Menegotto

Il 20/10/2015 23:33, JonRob ha scritto:



Hello Luca,

I very much appreciated your comments.  And I understand the
importance of "doing something right"  (i.e. convention).

This leads me to another question.

Because I am interfacing with an I2C sensor I have many register
definations to include (30 register addresses and  26 Variables to be
red from some of those registers.
In your comment you mentioned that convention is to declare variables
(and constants?)  in the construction (__ini__).
I am concerned that the sheer number of varialbe / constants would
make it difficult to read.

In your opinion, what would be the best method to structure such code?

Regards
JonRob


Let's start from constants. Constants, in Python, simply don't exist 
(and IMHO this is one of the few lacks of Python). All you can do is to 
declare a variable and treat it as a constant: you never change it!


It doesn't make sense to put a constant declaration at instance level, 
declaring it in the __init__ part of a class. After all, a constant is 
an information you want to share. The choice is up to you as the project 
manager: if you think that your constant is deeply related to the class 
you're designing, declare it as a class variable; otherwise, declare it 
at global level (in this case, often I use a separate file dedicated to 
constant declaration).


--
Ciao!
Luca


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


Re: variable scope of class objects

2015-10-22 Thread Luca Menegotto

Maybe I've been too cryptic. I apologize.

Il 22/10/2015 01:35, JonRob ha scritto:

@Dennis,


Thanks for your example.  My structure is very similar.


And that's ok. But you can also 'attach' the constants to a class, if it 
makes sense. For example, the same code of Dennis can be written as:


class SensorA():
GYROXREG = 0x0010
GYROYREG = 0x0011
GYROZREG = 0x0001
_registers = [GYROXREG, GYROYREG, GYROZREG]

And then you can invoke those constants as:

SensorA.GYROXREG

to emphasize that they are significant to this class, and to this class 
only.



 Luca wrote...

Please, note that declaring a variable in the constructor is only a
convention: in Python you can add a variable to an object of a class
wherever you want in your code (even if it is very dangerous and
discouraged).


This is the cryptic part.
I mean: you can do, and it's perfectly legal:

class A():
def __init__(self):
self.a = 10

if __name__ == '__main__':
o = A()
print(o.a)
# this is a new member, added on the fly
o.b = 20
print(o.b)

but, for God's sake, use it only if you have a gun at your head!

--
Ciao!
Luca

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


  1   2   >