Re: how to make return(self.res) not to output the content of list ?

2014-09-25 Thread Peter Otten
luofeiyu wrote:

 `sys.displayhook = lambda obj: None ` will block everything to be
 displayed on the interactive python console .
 
 i rewrite my code as the following :
 
 import sqlite3,os,jinja2
 db = r'data.sqlite'
 con = sqlite3.connect(db)
 cur = con.cursor()
 
 class status():
   def __init__():
   import sys
   sys.displayhook=mydisplay
   
   def mydisplay(x):
   if (x is not the result of an method grow of class status ) : x 
   #how to do in python code? else : pass
   
   def grow(self):
   self.res=  a data ananlyse process
   return(self.res)
  
   def display(self):
   #to display it in the html table
 
 
 
 how to fulfill the mydisplay function in python code to make x.grow() in
 the following not to output the list of self.res??
 
 
 import analyse
 x=analyse.status()
 x.grow()

Have grow() return a list subclass with the empty string as its repr():

 class GrowMethodResult(list):
... def __repr__(self): return 
... 
 def grow():
... return GrowMethodResult([1,2,3])
... 
 grow()

 


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


how to make return(self.res) not to output the content of list ?

2014-09-24 Thread luofeiyu

There is a file named analyse.py in the D:\Python34\Lib\site-packages.


import sqlite3,os,jinja2
db = r'F:\workspace\china\data\china.sqlite'
con = sqlite3.connect(db)
cur = con.cursor()

class status():
def grow(self):
self.res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管 
理人员人数 from profile limit 10').fetchall()


def display(self):
template = jinja2.Template('''
table border=1
{% for row in res %}
tr
{% for col in row -%}
td{{ col}}/td
{% endfor %}
/tr
{% endfor %}
/table
''')
with open('f:\\test.html', 'w') as f:
 f.write(template.render(res=self.res))
import webbrowser
webbrowser.open('f:\\test.html')


when i open python3.4  console to input  the code:
import analyse
x=analyse.status()
x.grow()
x.display()

i get


when i add  return(self.res) in grow method to make analyse.py into the 
following:


import sqlite3,os,jinja2
db = r'F:\workspace\china\data\china.sqlite'
con = sqlite3.connect(db)
cur = con.cursor()

class status():
def grow(self):
self.res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管 
理人员人数 from profile limit 10').fetchall()

return(self.res)
def display(self):
template = jinja2.Template('''
table border=1
{% for row in res %}
tr
{% for col in row -%}
td{{ col}}/td
{% endfor %}
/tr
{% endfor %}
/table
''')
with open('f:\\test.html', 'w') as f:
 f.write(template.render(res=self.res))
import webbrowser
webbrowser.open('f:\\test.html')


now again input the code:
import analyse
x=analyse.status()
x.grow()
the x.grow() will output

[('60', '银行', '187亿', 39340.0, 30.0),
('64', '民航机场', '11.5亿', 4499.0, 23.0),
 ('65', '钢铁行业', '101亿', 38857.0, 24.0),
('66', '汽车行业', '20.0亿', 10290.0, 20.0),
('67', '房地产', '10.1亿', 2332.0, 19.0),
 ('68', '公用事业', '22.0亿', 6515.0, 20.0),
('69', '民航机场', '19.3亿', 5472.0, 18.0),
('600010', '钢铁行业', '80.0亿', 31389.0, 19.0),
('600011', '电力行业', '141亿', 37729.0, 29.0),
('600012', '高速公路', '16.6亿', 2106.0, 14.0)]

now what i want to do is :
1.keep  return(self.res)  in grow method.
2.it is my target that when run  the code:

import analyse
x=analyse.status()
x.grow()

there is nothing output in my console ,  to make return(self.res) not to 
output the content of list ,

how can i do ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to make return(self.res) not to output the content of list ?

2014-09-24 Thread Ian Kelly
On Wed, Sep 24, 2014 at 7:55 AM, luofeiyu elearn2...@gmail.com wrote:

 now what i want to do is :
 1.keep  return(self.res)  in grow method.
 2.it is my target that when run  the code:

 import analyse
 x=analyse.status()
 x.grow()

 there is nothing output in my console ,  to make return(self.res) not to
output the content of list ,
 how can i do ?

You could write a separate method that just calls self.grow() and does not
return the result, and call that method instead from the interactive
interpreter.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to make return(self.res) not to output the content of list ?

2014-09-24 Thread Peter Otten
luofeiyu wrote:

 There is a file named analyse.py in the D:\Python34\Lib\site-packages.
 
 
 import sqlite3,os,jinja2
 db = r'F:\workspace\china\data\china.sqlite'
 con = sqlite3.connect(db)
 cur = con.cursor()
 
 class status():
  def grow(self):
  self.res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管
 理人员人数 from profile limit 10').fetchall()
 
  def display(self):
  template = jinja2.Template('''
  table border=1
  {% for row in res %}
  tr
  {% for col in row -%}
  td{{ col}}/td
  {% endfor %}
  /tr
  {% endfor %}
  /table
  ''')
  with open('f:\\test.html', 'w') as f:
   f.write(template.render(res=self.res))
  import webbrowser
  webbrowser.open('f:\\test.html')
 
 
 when i open python3.4  console to input  the code:
 import analyse
 x=analyse.status()
 x.grow()
 x.display()
 
 i get
 
 
 when i add  return(self.res) in grow method to make analyse.py into the
 following:
 
 import sqlite3,os,jinja2
 db = r'F:\workspace\china\data\china.sqlite'
 con = sqlite3.connect(db)
 cur = con.cursor()
 
 class status():
  def grow(self):
  self.res=cur.execute('select 代码,所属行业,注册资本,雇员人数,管
 理人员人数 from profile limit 10').fetchall()
 return(self.res)
  def display(self):
  template = jinja2.Template('''
  table border=1
  {% for row in res %}
  tr
  {% for col in row -%}
  td{{ col}}/td
  {% endfor %}
  /tr
  {% endfor %}
  /table
  ''')
  with open('f:\\test.html', 'w') as f:
   f.write(template.render(res=self.res))
  import webbrowser
  webbrowser.open('f:\\test.html')
 
 
 now again input the code:
 import analyse
 x=analyse.status()
 x.grow()
 the x.grow() will output
 
 [('60', '银行', '187亿', 39340.0, 30.0),
 ('64', '民航机场', '11.5亿', 4499.0, 23.0),
   ('65', '钢铁行业', '101亿', 38857.0, 24.0),
 ('66', '汽车行业', '20.0亿', 10290.0, 20.0),
 ('67', '房地产', '10.1亿', 2332.0, 19.0),
   ('68', '公用事业', '22.0亿', 6515.0, 20.0),
 ('69', '民航机场', '19.3亿', 5472.0, 18.0),
 ('600010', '钢铁行业', '80.0亿', 31389.0, 19.0),
 ('600011', '电力行业', '141亿', 37729.0, 29.0),
 ('600012', '高速公路', '16.6亿', 2106.0, 14.0)]
 
 now what i want to do is :
 1.keep  return(self.res)  in grow method.
 2.it is my target that when run  the code:
 
 import analyse
 x=analyse.status()
 x.grow()
 
 there is nothing output in my console ,  to make return(self.res) not to
 output the content of list ,
 how can i do ?

You can set the display hook:

 [1, 2, 3]
[1, 2, 3]
 import sys
 sys.displayhook = lambda obj: None
 [1, 2, 3]
 



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


Re: how to make return(self.res) not to output the content of list ?

2014-09-24 Thread luofeiyu

`sys.displayhook = lambda obj: None ` will block everything to be displayed on 
the interactive python console .

i rewrite my code as the following :

import sqlite3,os,jinja2
db = r'data.sqlite'
con = sqlite3.connect(db)
cur = con.cursor()

class status():
 def __init__():
 import sys
 sys.displayhook=mydisplay
 
 def mydisplay(x):

 if (x is not the result of an method grow of class status ) : x  #how 
to do in python code?
 else : pass
 
 def grow(self):

 self.res=  a data ananlyse process
 return(self.res)

 def display(self):

 #to display it in the html table



how to fulfill the mydisplay function in python code to make x.grow() in the 
following not to output the list of self.res??


import analyse
x=analyse.status()
x.grow()


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


Re: how to make return(self.res) not to output the content of list ?

2014-09-24 Thread luofeiyu


You could write a separate method that just calls self.grow() and does 
not return the result, and call that method instead from the interactive 
interpreter.


how can i change your meaning into python code?

There is my code structure.

class status():
 def grow(self):
 self.res=  a data ananlyse process
 return(self.res)

def display(self):
 #to display it in the html table

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


Re: how to make return(self.res) not to output the content of list ?

2014-09-24 Thread Cameron Simpson

On 25Sep2014 08:15, luofeiyu elearn2...@gmail.com wrote:

You could write a separate method that just calls self.grow() and does
not return the result, and call that method instead from the
interactive interpreter.


luofeiyu: please take care to include the attribution line. The text above is 
from Ian Kelly, but you do not show that. Attribution is important: it both 
gives credit and also lets people know who said what.


On 25Sep2014 08:15, luofeiyu elearn2...@gmail.com wrote:

how can i change your meaning into python code?


Well, I'd not do it that way. I would just make .grow() not return self.res.

Basicly, if a function returns None (which it does when it does not have an 
explicit return command), the python interpreter prints no output. So I would 
just write .grow() like this:


def grow(self):
self.res=  a data ananlyse process

Let the caller access self.res directly afterwards if they want to.

For Ian's suggestion, write a trivial method like this:

  def grow_but_be_silent(self):
self.grow()

Cumbersome, but it would do what you seem to ask for.

Cheers,
Cameron Simpson c...@zip.com.au

Drive Agressively  Rash Magnificently   - Nankai Leathers
--
https://mail.python.org/mailman/listinfo/python-list