Re: Python Events in 2017, Need your help.

2017-02-08 Thread Stephane Wirtel via Python-list

Thank you, with your help, we have added events on the t-shirt.

Now, Could you just add them on python.org/events ?

Have a nice day,

Stephane

On 9 Jan 2017, at 10:54, Stephane Wirtel via Python-list wrote:


Dear Community,

For the PythonFOSDEM [1] on 4th and 5th February in Belgium, I would 
like to present some slides with the Python events around the World.  
Based on https://python.org/events, I have noted that there are 
missing events, for example:


* PyCon Otto: Italy
* PyCon UK: United Kingdom
* PyCon CA: Canada
* PyCon Ireland: Ireland
* PyCon France: France

Some of these events are not yet announced and I understand they are 
in the second semester, and thus, they don't know the location and the 
dates, excepted for PyCon Otto (April).


In fact, I have noted that we know some big events in the Python 
community (for example: PyCon US and EuroPython) but do you know the 
others events, maybe the local event, PyCon IE, PyCon UK or PyCon IT.


I like to know where there is a PyCon or a Django Conf or a PyData 
Event.


In fact, I think we can help the Python Community if we submit all the 
events in https://python.org/events.


This page has been created by the PSF and is maintained by some 
volunteers.


I know this list of events:
* PyCon Cameroon : 20-23 Jav, Cameroon
* PythonFOSDEM : 4-5 Feb, Belgium
* PyCon Colombia : 10-12 Feb, Colombia
* PyCon Pune : 16-20 Feb, India
* Swiss Python Summit : 17-18 Feb, Switzerland
* IrPyCon : 17-18 Feb, Iran
* PyCon SK : 10-13 Mar, Slovakia
* Django Europe : 3-8 Apr, Italy
* PyCon Otto : 6-9 Apr, Italy
* Python Sudeste : 5-7 Mai, Brazil
* GeoPython : 8-11 May, Switzerland
* PyCon US : 17-26 May, USA
* EuroPython : July, Italy
* PyCon AU : 3-9 Aug, Australia
* PyCon UK : September, United Kingdom
* PyCon CA : November, Canada
* PyCon Ireland : October, Ireland
* PyCon FR : October/November, France

And you ?
Please, could you check on https://www.python.org/events/ , if you are 
an organizer, please add your event.


If you think there is a missing event, please, send me the info via 
[email](mailto:steph...@wirtel.be) or via my [twitter 
account](https://twitter.com/matrixise) and I will add it on my 
slides.


I would like to present your event.

Thank you so much for your help.

Stephane Wirtel

[1] https://www.python-fosdem.org

--
Stéphane Wirtel - http://wirtel.be - @matrixise
--
https://mail.python.org/mailman/listinfo/python-list

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


How to store properties

2017-02-08 Thread Cecil Westerhof
In Java you (can) use a properties file store configuration. What is
the best way to do something like that in Python?
I saw ConfigParser, but have the feeling that it is not really used.
Would a JSON file be a good idea?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to store properties

2017-02-08 Thread Peter Heitzer
Cecil Westerhof  wrote:
>In Java you (can) use a properties file store configuration. What is
>the best way to do something like that in Python?
>I saw ConfigParser, but have the feeling that it is not really used.
>Would a JSON file be a good idea?

If you only want to read the configuration, just use an ordinary 
file you import. For example config.py contains the lines:
username=myuser
server=myserver
password=secret

In your script:

import config

Now you can referenc all the variables via config., e.g. config.username

Another method would be a dictionary for your config. You could pickle and
unpickle it.

-- 
Dipl.-Inform(FH) Peter Heitzer, peter.heit...@rz.uni-regensburg.de
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to store properties

2017-02-08 Thread Cholo Lennon

On 02/08/2017 08:26 AM, Cecil Westerhof wrote:

In Java you (can) use a properties file store configuration. What is
the best way to do something like that in Python?
I saw ConfigParser, but have the feeling that it is not really used.


I use it a lot ;-)


Would a JSON file be a good idea?




--
Cholo Lennon
Bs.As.
ARG
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to store properties

2017-02-08 Thread David Palao
2017-02-08 13:34 GMT+01:00 Cholo Lennon :
> On 02/08/2017 08:26 AM, Cecil Westerhof wrote:
>>
>> In Java you (can) use a properties file store configuration. What is
>> the best way to do something like that in Python?
>> I saw ConfigParser, but have the feeling that it is not really used.
>
>
> I use it a lot ;-)
>
>
>> Would a JSON file be a good idea?
>>
>
>
> --
> Cholo Lennon
> Bs.As.
> ARG
> --
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to store properties

2017-02-08 Thread Cecil Westerhof
On Wednesday  8 Feb 2017 13:11 CET, Peter Heitzer wrote:

> Cecil Westerhof  wrote:
>> In Java you (can) use a properties file store configuration. What
>> is the best way to do something like that in Python? I saw
>> ConfigParser, but have the feeling that it is not really used.
>> Would a JSON file be a good idea?
>
> If you only want to read the configuration, just use an ordinary 
> file you import. For example config.py contains the lines:
> username=myuser
> server=myserver
> password=secret
>
> In your script:
>
> import config
>
> Now you can referenc all the variables via config., e.g.
> config.username

That I know, but it is a security risk.


> Another method would be a dictionary for your config. You could
> pickle and unpickle it.

Is pickle not discouraged? Better to use a JSON file I think.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to store properties

2017-02-08 Thread Neil Cerutti
On 2017-02-08, Cholo Lennon  wrote:
> On 02/08/2017 08:26 AM, Cecil Westerhof wrote:
>> In Java you (can) use a properties file store configuration.
>> What is the best way to do something like that in Python? I
>> saw ConfigParser, but have the feeling that it is not really
>> used.
>
> I use it a lot ;-)

Me too. I wrote a script once to convert all my .cfg files to
JSON at one point while trying out a switch from Python to Go,
but never made the changeover.

-- 
Neil Cerutti

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


Re: How to store properties

2017-02-08 Thread Michael Torrie
On 02/08/2017 04:26 AM, Cecil Westerhof wrote:
> In Java you (can) use a properties file store configuration. What is
> the best way to do something like that in Python?
> I saw ConfigParser, but have the feeling that it is not really used.
> Would a JSON file be a good idea?

I've used ConfigParser before.  I think the relative simpleness of .INI
files.  If I was expecting end users to manipulate a config file, I
would use ConfigParser.

But 90% of the time, I just use another python module.  For my purposes,
there is no security risk to it.  Due to its nature, Django also uses
python modules to define configuration such as URL maps, etc.

When I'm using GTK+, I would use the gsettings facilities of GTK+ to
store and retrieve config variables.  And with Qt, I'd use QSettings. I
think in both cases they use backends appropriate for the operating
system. On Windows, that would be registry settings in the user hive.

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


Re: How to store properties

2017-02-08 Thread Rob Gaddi

On 02/08/2017 03:26 AM, Cecil Westerhof wrote:

In Java you (can) use a properties file store configuration. What is
the best way to do something like that in Python?
I saw ConfigParser, but have the feeling that it is not really used.
Would a JSON file be a good idea?



In the interest of trying to be cross-platform and support fallbacks and 
work natively into the system and play nicely with my Qt ecosystem I 
decided to use PySide.QtCore.QSettings instead of ConfigParser for a 
major application framework I put together.  Bloody mistake that was, 
but now I'm stuck with it.


JSON's cute, but the format doesn't support inline comments.  If you 
really need complex arrays and nested data structures in your 
configuration files, you're probably wrong.


ConfirgParser if you care about security.  Import a raw Python file if 
you don't.  Solved problem and move on; wheel reinvention is for suckers.


--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to add months to a date (datetime object)?

2017-02-08 Thread John_Gordon
From: John_Gordon@f38.n261.z1

From: John_Gordon@f38.n261.z1

From: John Gordon 

In  "Deborah Swanson"
 writes:

> bajimicb...@gmail.com wrote, on February 02, 2017 2:44 AM
> >
> > for start of month to the beginning of next month
> >
> > from datetime import timedelta
> > from dateutil.relativedelta import relativedelta
> >
> > end_date = start_date + relativedelta(months=delta_period) +
> > timedelta(days=-delta_period)

> Where do you define 'delta_period', and what is your question?

There is no question; it is an answer in response to the original post asking
how to add months to a datetime object.

--
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Coding issue with XML for word document

2017-02-08 Thread Deborah_Swanson
From: "Deborah Swanson" 

Cute, whoever you are. Very cute. (I know how to sign up for fake email
addresses too.)

> -Original Message-
> From: Python-list
> [mailto:python-list-bounces+python=deborahswanson.net@python.o
> rg] On Behalf Of Deborah_Swanson@f38.n261.z1
> Sent: Monday, February 06, 2017 9:52 AM
> To: python-list@python.org
> Subject: Re: Coding issue with XML for word document
>
>
> From: "Deborah Swanson" 
>
> I don't see any Python in your code. The python.org list
> isn't generally familiar with Microsoft proprietary code,
> which is often quite different from other versions. Try
> asking this question in Microsoft's Office/Word Document
> forums. Good luck!
>
>
> accessnew...@gmail.com wrote, on February 06, 2017 5:00 PM
> Subject: Coding issue with XML for word document
> >
> >
> > I am constructing a sentence to insert into a word xml template. My
> > code is below
> >
> > 
> > #Create a list
> >
> > if len(myList) > 0:
> > if len(myList) > 1:
> > testText = list_format(myList) + " dealers."
> > else:
> > myText = myList[0] + " dealer."
> >
> > #Contruct sentence
> >
> > #myBlurb = "My
> > Favorite Cars -  My favorite cars are
> available
> > at "  + myText + ""
> > myBlurb = "My Favorite Cars - My favorite cars are
> > available at " + myText + ""
> > else:
> > myBlurb = ""
> >
> >
> > ---
> >
> > If a list exists (not = 0) Then
> >
> > I am trying to get the following output:
> >
> > My Favorite Cars - My favorite cars are available at (list of
> > dealers) dealers.
> >
> > There is a newline above and below this line. The first three words
> > and the dash are bold. All other words are regular.
> >
> > This works
> >
> > myBlurb = "My Favorite Cars - My favorite cars are
> available at
> > " + myText + ""
> >
> > But when I try to bold, it bombs. It does not seem to like
> either of
> > these (+ myText + "") (separately or
> > together) appended on the end.
> >
> > Thia does not work
> >
> > myBlurb = "My Favorite Cars -
> >  My favorite cars are available at
> "
> > + myText + "
> >
> > What am I missing here?
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Re: search for a data in my browser via script

2017-02-08 Thread kelvidpang
From: kelvidpang@f38.n261.z1

From: kelvidpang@f38.n261.z1

From: kelvidp...@gmail.com

On Monday, 6 February 2017 22:43:17 UTC+8, Meeran Rizvi  wrote:
> Hello guys,
> Here i am writing a script which will open my chrome browser and opens the
URL www.google.com.
> But how to search for a data via script.
> for example i need to search for 'Rose' in google.com via script.
> how to do that?
>
> 
> import webbrowser
> url="www.google.com"
> chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
%s'
> webbrowser.get(chrome_path)
> webbrowser.open(url)
> 


You may try to search for selenium for python, which has chrome driver.. it
works like charm.

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


Re: Coding issue with XML for word document

2017-02-08 Thread Deborah_Swanson
From: Deborah_Swanson@f38.n261.z1

From: Deborah_Swanson@f38.n261.z1

From: "Deborah Swanson" 

I don't see any Python in your code. The python.org list isn't generally
familiar with Microsoft proprietary code, which is often quite different from
other versions. Try asking this question in Microsoft's Office/Word Document
forums. Good luck!


accessnew...@gmail.com wrote, on February 06, 2017 5:00 PM Subject: Coding
issue with XML for word document
>
>
> I am constructing a sentence to insert into a word xml
> template. My code is below
>
> 
> #Create a list
>
> if len(myList) > 0:
> if len(myList) > 1:
> testText = list_format(myList) + " dealers."
> else:
> myText = myList[0] + " dealer."
>
> #Contruct sentence
>
> #myBlurb = "My
> Favorite Cars -  My favorite cars are
> available at "  + myText + ""
> myBlurb = "My Favorite Cars - My favorite cars are
> available at " + myText + ""
> else:
> myBlurb = ""
>
>
> ---
>
> If a list exists (not = 0) Then
>
> I am trying to get the following output:
>
> My Favorite Cars - My favorite cars are available at (list of
> dealers) dealers.
>
> There is a newline above and below this line. The first three
> words and the dash are bold. All other words are regular.
>
> This works
>
> myBlurb = "My Favorite Cars - My favorite cars are
> available at " + myText + ""
>
> But when I try to bold, it bombs. It does not seem to like
> either of these (+ myText + "") (separately or
> together) appended on the end.
>
> Thia does not work
>
> myBlurb = "My Favorite
> Cars -  My favorite cars are available
> at "  + myText + "
>
> What am I missing here?
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Re: Coding issue with XML for word document

2017-02-08 Thread Pavol_Lisy
From: Pavol Lisy 

On 2/7/17, Steven D'Aprano  wrote:
> On Mon, 06 Feb 2017 17:00:25 -0800, accessnewbie wrote:
[...]
>> But when I try to bold, it bombs. It does not seem to like either of
>> these (+ myText + "") (separately or together) appended on the
>> end.
>
> How are you trying to bold? If you're programming in a word processor,
> and you apply bold to your code, that won't work.
>
> What do you mean, "bombs"? Do you literally mean you are running a
> classic Macintosh from the 1990s and you are getting a fatal System Error
> in a "Bomb" dialog box?
>
> Otherwise, please be more precise: do you get a Blue Screen of Death? A
> kernel panic? Does the Python interpreter segfault? Or do you get an
> ordinary Python traceback?
>
> If it is an ordinary Python traceback, don't keep it a secret -- tell us
> what it says. COPY AND PASTE the traceback, in full, not just the last
> line.

I am afraid he means that it create mishmash in word document...

Dear accessnewbie I propose divide problem to smaller subproblems.

For example make python code as simple as possible (avoid python part if you
could! As Deborah wrote it is very probably not problem in python)

And try to explicitly try to write example text where is bold part.
#myBlurb = "My Favorite Cars -
 My favorite cars are available at "
 + myText + ""
myBlurb = "My Favorite Cars -
 My favorite cars are available at 
my garage "

if it is still wrong try to make more and more similar text to: myBlurb =
"My Favorite Cars - My favorite cars are available at my garage"

I mean for example remove "" part etc.

Second - This is probably not your problem (unless you have special char in
your example myList) but if you could have xml spec chars in your input then
python could help. See example ->

>>> from xml.sax.saxutils import escape
>>> escape("< & >")
'< & >'

myBlurb = "My Favorite Cars - My favorite cars are available at " +
escape(myText) + ""  # this is safer than your code!

I hope you understand why it is useful. :)

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


Re: How to store properties

2017-02-08 Thread Michael_Torrie
From: Michael Torrie 

On 02/08/2017 04:26 AM, Cecil Westerhof wrote:
> In Java you (can) use a properties file store configuration. What is
> the best way to do something like that in Python?
> I saw ConfigParser, but have the feeling that it is not really used.
> Would a JSON file be a good idea?

I've used ConfigParser before.  I think the relative simpleness of .INI files. 
If I was expecting end users to manipulate a config file, I would use
ConfigParser.

But 90% of the time, I just use another python module.  For my purposes, there
is no security risk to it.  Due to its nature, Django also uses python modules 
to
define configuration such as URL maps, etc.

When I'm using GTK+, I would use the gsettings facilities of GTK+ to store and
retrieve config variables.  And with Qt, I'd use QSettings. I think in both 
cases
they use backends appropriate for the operating system. On Windows, that would 
be
registry settings in the user hive.

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


Re: How to store properties

2017-02-08 Thread Cecil_Westerhof
From: Cecil Westerhof 

On Wednesday  8 Feb 2017 13:11 CET, Peter Heitzer wrote:

> Cecil Westerhof  wrote:
>> In Java you (can) use a properties file store configuration. What
>> is the best way to do something like that in Python? I saw
>> ConfigParser, but have the feeling that it is not really used.
>> Would a JSON file be a good idea?
>
> If you only want to read the configuration, just use an ordinary
> file you import. For example config.py contains the lines:
> username=myuser
> server=myserver
> password=secret
>
> In your script:
>
> import config
>
> Now you can referenc all the variables via config., e.g.
> config.username

That I know, but it is a security risk.


> Another method would be a dictionary for your config. You could
> pickle and unpickle it.

Is pickle not discouraged? Better to use a JSON file I think.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

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


RE: gmail api

2017-02-08 Thread Deborah Swanson
Kelvid Pang wrote, on February 06, 2017 10:22 PM
> 
> hi,
> 
> I am trying to gmail api with reference to this URL: 
> https://developers.google.com/gmail/api/quickstart/python
> 
> 
> But I couldn't find the 
> 'gmail-python-quickstart.json' file. Any one can help? thanks.

You likely won't have a 'gmail-python-quickstart.json' file if you
didn't download one from Google APIs.

Download it in substep 'g' (Download JSON) in the "Step 1: Turn on the
Gmail API" instructions. (But you have to do substeps 'a' thru 'f'
first.)

If you then want to find it and make modifications, and follow the
applicable instructions.

 

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


Re: How to store properties

2017-02-08 Thread Terry Reedy

On 2/8/2017 6:26 AM, Cecil Westerhof wrote:

In Java you (can) use a properties file store configuration. What is
the best way to do something like that in Python?
I saw ConfigParser, but have the feeling that it is not really used.


IDLE uses it for default and user configuration files.


--
Terry Jan Reedy

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


Re: Coding issue with XML for word document

2017-02-08 Thread accessnewbie

> myBlurb = "My Favorite Cars - 
>  My favorite cars are available at "  + 
> myText + "
> 
> What am I missing here?

Sorry about posting in python group. My XML coding was embedded in python 
script. I will do better next time.

The problem was with placement of tags. The following worked:


myBlurb = "My Favorite Cars - 
 My favorite cars are available at 
"  + myText + "

I needed to put the carriage return  within the  wrapper. I also 
needed to add the text elements  and  before and after the variable.
-- 
https://mail.python.org/mailman/listinfo/python-list


subprocess problem

2017-02-08 Thread Andreas Paeffgen
The Problem with the subprocess code is: Using the sourcecode 
functioning as normal.

The frozen app with cx_freeze on every platform just returns an empty result

Here is the code in short:
def get_path_pandoc():




   settings = QSettings('Pandoc', 'PanConvert')

   path_pandoc = settings.value('path_pandoc','')




   if not os.path.isfile(path_pandoc):




   if platform.system() == 'Darwin' or os.name == 'posix':

   args = ['which', 'pandoc']

   p = subprocess.Popen(

   args,

   stdin=subprocess.PIPE,

   stdout=subprocess.PIPE)




   path_pandoc = 
str.rstrip(p.communicate(path_pandoc.encode('utf-8'))[0].decode('utf-8')) 




The whole problematic code can be checked on 
http://github.com/apaeffgen/panconvert

in source/converters/interface_pandoc.py

I debugged it with some QMessage-Boxes. I just know, that the returnd 
result is empty.

I tried some stderr on this code also. But also the error message is empty.

Any hints what could be changed in the code, so it also works in the 
frozen app?


At the moment i use python3.4 to 3.5 depending on the platform (Mac, 
Win, Linux)


P.S. Tried also some code with invoking the bash and afterwords the 
which pandoc code. To know avail also




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


Re: subprocess problem

2017-02-08 Thread Cameron Simpson

On 08Feb2017 18:56, Andreas Paeffgen  wrote:
The Problem with the subprocess code is: Using the sourcecode 
functioning as normal.

The frozen app with cx_freeze on every platform just returns an empty result


I don't know what the sentence above above cx_freeze means.


Here is the code in short:
def get_path_pandoc():
  settings = QSettings('Pandoc', 'PanConvert')
  path_pandoc = settings.value('path_pandoc','')
  if not os.path.isfile(path_pandoc):
  if platform.system() == 'Darwin' or os.name == 'posix':
  args = ['which', 'pandoc']
  p = subprocess.Popen(
  args,
  stdin=subprocess.PIPE,


"which" doesn't read any input. You probably want "subprocess.DEVNULL" instead 
of "subprocess.PIPE" for stdin.



  stdout=subprocess.PIPE)
  path_pandoc = 
  str.rstrip(p.communicate(path_pandoc.encode('utf-8'))[0].decode('utf-8'))


The whole problematic code can be checked on 
http://github.com/apaeffgen/panconvert

in source/converters/interface_pandoc.py

I debugged it with some QMessage-Boxes. I just know, that the returnd 
result is empty.

I tried some stderr on this code also. But also the error message is empty.


What does "which pandoc" say to you at a terminal prompt? The code above relies 
on that returning you the path to the pandoc executable. If it is not in your 
path then you will get an empty result.


Also, the "which" programme should have a zero exit status if it finds and 
prints the path to pandoc, and a nonzero exit status otherwise?  Find out what 
exit status it returns. This is available from the "p" object after 
communicate() returns. See the subprocess doco for details. The easy thing to 
do is probably to call "p.check_returncode()" after communicate() returns.


Any hints what could be changed in the code, so it also works in the 
frozen app?


What's a frozen app?

Maybe that is your problem: when you run the programme by hand it works, but in 
this "forzen" state it doesn't.


The "which" command isn't magic. It searchs for commands by looking for 
executables in the directories listed in the $PATH environment variable. Get 
your program to print or display the value of "os.environ['PATH']" before 
invoking "which". The difference may tell you something useful.


At the moment i use python3.4 to 3.5 depending on the platform (Mac, 
Win, Linux)


Thank you for this information; many people forget to supply it.

P.S. Tried also some code with invoking the bash and afterwords the which 
pandoc code. To know avail also



From a shell prompt (such as bash's prompt) issuing the command:


 which pandoc

should print the executable path, _if_ your $PATH includes the right directory.  
You can look at $PATH from the shell with the command:


 echo $PATH

Do you know where pandoc normally resides on your system?

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


Decorator

2017-02-08 Thread ast

Hi

In python courses I read, it is explained that

@decor
def f():
   pass

is equivalent to:

def f():
   pass

f = decor(f)

But that's not always true. See this code

class Temperature:
   
   def __init__(self):

   self.value = 0

#  @property
   def celsius(self): 
   return self.value
   
   celsius = property(celsius)

#  @celsius.setter   
   def celsius(self, value):<-- overwrites previous celsius

   self.value = value

   celsius = celsius.setter(celsius) <-- error here

When you define the setter function named celsius, it overwrites 
the previous property object also named celsius ...

and it fails:

celsius = celsius.setter(celsius)
AttributeError: 'function' object has no attribute 'setter'

any comment ?

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


Re: Decorator

2017-02-08 Thread Chris Angelico
On Thu, Feb 9, 2017 at 6:03 PM, ast  wrote:
> class Temperature:
>   def __init__(self):
>self.value = 0
>
> #  @property
>def celsius(self):return self.value
>   celsius = property(celsius)
> #  @celsius.setter  def celsius(self, value):<--
> overwrites previous celsius
>self.value = value
>
>celsius = celsius.setter(celsius) <-- error here
>

The difference is that the decorator line is evaluated before the
function is defined. Try this:

class Temperature:
def __init__(self):
self.value = 0

#@property
def celsius(self):
return self.value
celsius = property(celsius)

#@celsius.setter
_tmp = celsius.setter
def celsius(self, value):
   self.value = value
celsius = _tmp(celsius)
del _tmp

Now it'll work.

(Actually, there are some other very minor subtleties; the name isn't
temporarily bound to the undecorated function prior to the decorator
being called. But in this case, it's simply an order of evaluation.)

In CPython (3.7 on Debian Linux, fwiw), the decorated function is
processed like this:

@celsius.setter
#_tmp = celsius.setter
def celsius(self, value):
   self.value = value
#celsius = _tmp(celsius)
#del _tmp


11  28 LOAD_NAME5 (celsius)
 30 LOAD_ATTR6 (setter)
 32 LOAD_CONST   5 ()
 34 LOAD_CONST   4
('Temperature..Temperature.celsius')
 36 MAKE_FUNCTION0
 38 CALL_FUNCTION1
 40 STORE_NAME   5 (celsius)

In other words:
1) Evaluate "celsius.setter" and save that on the stack
2) Fetch up the compiled code for the undecorated function
3) Grab the name of the function (it's a constant)
4) Create a function. Don't save it anywhere yet, just put it on the stack.
5) Call the function from step 1, passing the function from step 4 as
a parameter.
6) Whatever it returns, save that under the name "celsius".

That's very detailed and nitty-gritty, but that's what really happens
when you use "@celsius.setter" in your code.

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