Re: command line micro wiki written in Python

2017-02-02 Thread paul . wolf
On Tuesday, 31 January 2017 23:39:41 UTC, Ben Finney  wrote:

> The Python community has a stronger (?) preference for reStructuredText
> format. Can that be the default?
> 
> That is, I want my text files to be named ‘foo’ (no suffix) or ‘foo.txt’
> (because they're primarily text), and have the default be to parse them
> as reStructuredText.

Good point. It should at least be possible to provide an arbitrary default so 
you can have rst as default for new files. And perhaps as you say also start 
with rst as the main default. 

Regarding no suffix, whatever the default is gets used for newly created files, 
but not for the 'take' command which I use mainly to link to (with --symlink) 
config files, like:

yd take ~/.emacs

for instance. 

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


Is there a lint tool that can spot unused classes/methods/attributes/functions/etc?

2017-02-02 Thread Mark Summerfield
Suppose I have a project with an application .py file and various module .py 
files all in the same directory, and after lots of refactoring and other 
changes the modules contain lots of unused stuff. Is there a lint tool that 
will spot the unused things so that I can get rid of them?

I've tried flake and pylint but neither seem to help.

Here's the smallest example I can think of:

# app.py
import Mod
a = Mod.A()
print(a.quack())

# Mod.py
class A: # Used in app.py
def __init__(self):
self.x = "hello" # Used in quack() which is used in app.py
self.y = 7 # Unused
@property
def z(self): # Unused
return 4.5
def quack(self): # Used in app.py
return self.x
class B: # Unused
pass
CONST = 999 # Unused
def func(): # Unused
pass

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


Re: How to know what to install (Ubuntu/Debian) for a given import?

2017-02-02 Thread Chris Green
Michael Torrie  wrote:
> On 02/01/2017 02:29 PM, Chris Green wrote:
> > OK, thank you, what a strange way to do it.
> 
> Why is it strange?  Essentially, python bindings for any GObject-based
> library are now fully automatic via this gi module.  No longer do we

Which is thus different from every other python library!  So we have
to somehow 'know' that the components of GObject are imported by this
special mechanism.  Everything else works the old/normal way.

> 
> > OK, no problem, but isn't it very non-portable?
> 
> Not inherently, no.  Should work on OS X and Windows.
> 
OK, good, I thought from the original reply that it was Ubuntu
specific, but it's not.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to know what to install (Ubuntu/Debian) for a given import?

2017-02-02 Thread Chris Green
Michael Torrie  wrote:
> On 02/01/2017 01:03 PM, Wildman via Python-list wrote:
> > 
> > It is the proper way.  This page helps explain it.
> > 
> > http://askubuntu.com/questions/784068/what-is-gi-repository-in-python
> > 
> >> ... and doesn't it need an internet connection?
> > 
> > No.
> 
> However the gi module provides access to GTK+3, and it's quite likely
> Chris's project requires GTK+2, and would probably take some work to
> port from GTK+2 to GTK+3, though in the long run it's worth the effort.
> 
> Anyway the GTK+2 module for python is usually called pygtk in the repos.
> 
It would appear that there isn't a GTK+2 for Python 3 in Ubuntu, or at
least I can't find one.  I've reverted to staying with Python 2 fr the
moment.  I can't manage changing the code to work with a database
instead of LDAP *and* migrating from GTK+2 to GTK+3. :-)

When I have it working as I want in python 2 I'll consider the GTK+2
to GTK+3 migration and moving to python 3.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a lint tool that can spot unused classes/methods/attributes/functions/etc?

2017-02-02 Thread Peter Otten
Mark Summerfield wrote:

> Suppose I have a project with an application .py file and various module
> .py files all in the same directory, and after lots of refactoring and
> other changes the modules contain lots of unused stuff. Is there a lint
> tool that will spot the unused things so that I can get rid of them?
> 
> I've tried flake and pylint but neither seem to help.

I often "find" unused code with coverage.py. However, it cannot detect 
statements that are executed but have no effect, and thus will report only
A.z() and func() in the example below.
 
> Here's the smallest example I can think of:
> 
> # app.py
> import Mod
> a = Mod.A()
> print(a.quack())
> 
> # Mod.py
> class A: # Used in app.py
> def __init__(self):
> self.x = "hello" # Used in quack() which is used in app.py
> self.y = 7 # Unused
> @property
> def z(self): # Unused
> return 4.5
> def quack(self): # Used in app.py
> return self.x
> class B: # Unused
> pass
> CONST = 999 # Unused
> def func(): # Unused
> pass
> 
> Thanks.



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


Re: Why doesn't module finalization delete names as expected?

2017-02-02 Thread dieter
Philippe Proulx  writes:
> ...
> It feels like `bread` is never deleted in the module initialization
> situation, but I don't know why: the only reference to the Bread Python
> object is this `bread` name in the module... what could prevent this
> object's __del__() method to be called? It works when I call `del bread`
> manually: I would expect that the module finalization does the exact
> same thing?

I expect, you are speaking about "module finalization" as part
of "process finalization", i.e. when the whole Python process terminates:
a module is not "finalized" at the end of its code execution.

Python's behaviour during process finalization is complex (and has changed
over time). It may still be prone to surprises.
Somewhere, there is a document describing in details how Python performs
finalization. When I last read it (may have changed meanwhile), it told
that first "_*" variables are unbound, then the module dict is cleared.

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


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

2017-02-02 Thread bajimicbiga
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)
-- 
https://mail.python.org/mailman/listinfo/python-list


how i can write command to run...........This is code to convert .json file to .csv......Input file name is sample.json

2017-02-02 Thread muetced124
import sys
import json
import csv

##
# Convert to string keeping encoding in mind...
##
def to_string(s):
try:
return str(s)
except:
#Change the encoding type if needed
return s.encode('utf-8')


##
# This function converts an item like 
# {
#   "item_1":"value_11",
#   "item_2":"value_12",
#   "item_3":"value_13",
#   "item_4":["sub_value_14", "sub_value_15"],
#   "item_5":{
#   "sub_item_1":"sub_item_value_11",
#   "sub_item_2":["sub_item_value_12", "sub_item_value_13"]
#   }
# }
# To
# {
#   "node_item_1":"value_11",
#   "node_item_2":"value_12",
#   "node_item_3":"value_13",
#   "node_item_4_0":"sub_value_14", 
#   "node_item_4_1":"sub_value_15",
#   "node_item_5_sub_item_1":"sub_item_value_11",
#   "node_item_5_sub_item_2_0":"sub_item_value_12",
#   "node_item_5_sub_item_2_0":"sub_item_value_13"
# }
##
def reduce_item(key, value):
global reduced_item

#Reduction Condition 1
if type(value) is list:
i=0
for sub_item in value:
reduce_item(key+'_'+to_string(i), sub_item)
i=i+1

#Reduction Condition 2
elif type(value) is dict:
sub_keys = value.keys()
for sub_key in sub_keys:
reduce_item(key+'_'+to_string(sub_key), value[sub_key])

#Base Condition
else:
reduced_item[to_string(key)] = to_string(value)


if __name__ == "__main__":
if len(sys.argv) != 4:
print "\nUsage: python json_to_csv.py   
\n"
else:
#Reading arguments
node = sys.argv[1]
json_file_path = sys.argv[2]
csv_file_path = sys.argv[3]

fp = open(json_file_path, 'r')
json_value = fp.read()
raw_data = json.loads(json_value)

try:
data_to_be_processed = raw_data[node]
except:
data_to_be_processed = raw_data

processed_data = []
header = []
for item in data_to_be_processed:
reduced_item = {}
reduce_item(node, item)

header += reduced_item.keys()

processed_data.append(reduced_item)

header = list(set(header))
header.sort()

with open(csv_file_path, 'wb+') as f:
writer = csv.DictWriter(f, header, quoting=csv.QUOTE_ALL)
writer.writeheader()
for row in processed_data:
writer.writerow(row)

print "Just completed writing csv file with %d columns" % len(header)
-- 
https://mail.python.org/mailman/listinfo/python-list


user+sys time bigger than real time (with threads)

2017-02-02 Thread Marco Buttu

Let's have the following example:

$ cat foo.py
from threading import Thread, Lock

result = 0
lock = Lock()

def task():
global result
for i in range(10**6):
lock.acquire()
result += 1
lock.release()

if __name__ == '__main__':
t1, t2 = Thread(target=task), Thread(target=task)
t1.start()
t2.start()
t1.join()
t2.join()
print('result:', result)

When I execute it (Python 3.6), I get a sys+user time bigger than the 
real time:


$ time python foo.py
result: 200

real   0m7.088s
user   0m6.597s
sys0m5.043s

I can expect this result when I run some processes in parallel on 
different CPUs, but this code uses threads, so the GIL prevents the two 
task() functions to be executed in parallel. What am I missing?


--
Marco Buttu

INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbu...@oa-cagliari.inaf.it

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


Re: What is meant by "Duty Cycle" when referring to laser printers?

2017-02-02 Thread Chris Green
Where is GDK_BUTTON_PRESS defined in gtk2, i.e. how can I use it when
testing for button presses?

I have tried gtk.GDK_BUTTON_PRESS and gtk.gdk.GDK_BUTTON_PRESS but
both result in "object has no attribute 'GDK_BUTTON_PRESS'" errors.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Is requests[security] required for python 3.5+ ?

2017-02-02 Thread Thomas Nyberg

Hello,

I'm trying to understand whether requests[security] or just plain 
requests should be installed for python 3.5. I.e. do the following 
packages need to be installed: pyOpenSSL, cryptography,	idna.


The reason I'm asking is because I'm moving an application to python 3 
and I am testing out which requirements continue to be required in the 
version.


Thanks for any info.

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


Re: Python3 using requests to grab HTTP Auth Data

2017-02-02 Thread Ian Kelly
On Wed, Feb 1, 2017 at 5:22 PM, Νίκος Βέργος  wrote:
> 
> # Give user the file requested
>
> print(''' content="5;url=http://superhost.gr/data/files/%s";>''' % realfile)
>
> authuser = os.environ.get( 'REMOTE_USER', 'Άγνωστος' )
> print( authuser )
> 
>
> Trying this, feels liek i'm almost there except that when printing the value 
> of authuser variable it default to "Άγνωστος" meaning not there.
>
> is there any other way i can grab what the user gave a auth login info?

Hold on, are those consecutive lines within the same script?

I think you need to better understand the HTTP request cycle. The
browser sends a request to your server, the server runs your CGI which
builds a response, and then the server sends the response back to the
browser. At that point the CGI is done with this request.

The  tag that you're printing is part of that response. The
browser can't do anything with it until it sees it. When it does, it
will perform the refresh which creates a second request to the server
at the new URL. If the server's response to the second request is a
401 Unauthorized, then the browser shows the username/password dialog
and after the user enters those it will make a /third/ request
containing that info, also to the new URL.

Your script which ran on the first request is trying to get the
REMOTE_USER from the authentication data that was passed to that first
request, but there wasn't any. The user didn't enter any until the
third request, at which point your script was long since finished
running.

If you want the user to authenticate to your script and not just
whatever file you're redirecting them to, then you need to configure
the server to require authorization for the script and not just the
redirect target. Most likely you would do this with an .htaccess
directive as Michael Torrie already suggested. Once that's done, then
as soon as your script is invoked you'll be able to get the
REMOTE_USER. The  tag has nothing to do with requesting auth and
you only need it if you want the browser to perform a delayed redirect
to that file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is requests[security] required for python 3.5+ ?

2017-02-02 Thread Chris Warrick
On 2 February 2017 at 20:41, Thomas Nyberg  wrote:
> Hello,
>
> I'm trying to understand whether requests[security] or just plain requests
> should be installed for python 3.5. I.e. do the following packages need to
> be installed: pyOpenSSL, cryptography,idna.
>
> The reason I'm asking is because I'm moving an application to python 3 and I
> am testing out which requirements continue to be required in the version.

No, requests[security] is only needed for old Python 2.7 versions
(before 2.7.9).

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is meant by "Duty Cycle" when referring to laser printers?

2017-02-02 Thread MRAB

On 2017-02-02 19:30, Chris Green wrote:

Where is GDK_BUTTON_PRESS defined in gtk2, i.e. how can I use it when
testing for button presses?

I have tried gtk.GDK_BUTTON_PRESS and gtk.gdk.GDK_BUTTON_PRESS but
both result in "object has no attribute 'GDK_BUTTON_PRESS'" errors.


Does this help?

http://faq.pygtk.org/index.py?req=edit&file=faq02.007.htp

BTW, why is the subject line what it is?

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


Re: Python3 using requests to grab HTTP Auth Data

2017-02-02 Thread Νίκος Βέργος
Τη Πέμπτη, 2 Φεβρουαρίου 2017 - 9:38:47 μ.μ. UTC+2, ο χρήστης Ian έγραψε:

> If you want the user to authenticate to your script and not just
> whatever file you're redirecting them to, then you need to configure
> the server to require authorization for the script and not just the
> redirect target. Most likely you would do this with an .htaccess
> directive as Michael Torrie already suggested. Once that's done, then
> as soon as your script is invoked you'll be able to get the
> REMOTE_USER. The  tag has nothing to do with requesting auth and
> you only need it if you want the browser to perform a delayed redirect
> to that file.

I have also came up with this idea but the problem with that approach is that 
in order for 'files.py' script to run an authentice dialog must come first.

As i have the script coded the 'files.py' need to run first normally and only 
after a form submit bases on user selection of a a listed file, onlty then the 
authentication process to be initiated , not earlier.

To see what i eman just visit my website at http://superhost.gr, scroll down on 
the middle of the page, hit download now image link.

A list of files will be presented to you and when you click someone then the 
 tage initiate the authentication.

Can this also be done in such a way with .htaccess & .htpasswd ?!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: psutil 5.1.0 with hardware sensors released

2017-02-02 Thread Hans-Peter Jansen
On Mittwoch, 1. Februar 2017 21:54:06 Giampaolo Rodola' wrote:
> Hello all,
> I'm glad to announce the release of psutil 5.1.1:
 ^

Guess, you meant to say 5.1.0 here, or probably your time machine broke ;)

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


Re: What is meant by "Duty Cycle" when referring to laser printers?

2017-02-02 Thread Chris Green
MRAB  wrote:
> On 2017-02-02 19:30, Chris Green wrote:
> > Where is GDK_BUTTON_PRESS defined in gtk2, i.e. how can I use it when
> > testing for button presses?
> >
> > I have tried gtk.GDK_BUTTON_PRESS and gtk.gdk.GDK_BUTTON_PRESS but
> > both result in "object has no attribute 'GDK_BUTTON_PRESS'" errors.
> >
> Does this help?
> 
> http://faq.pygtk.org/index.py?req=edit&file=faq02.007.htp
> 
Yes, thank you, just what I need!  :-)


> BTW, why is the subject line what it is?
> 
Er?  I suspect I didn't change it!  :-) 

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


PyDictObject to NSDictionary

2017-02-02 Thread Charles Heizer
Hello,
I'm embedding python in to my Objective-C app and I need to know how do I 
convert a PyDictObject (PyObject) to a NSDictionary?

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


Downloading and Sending the file to the client's browser

2017-02-02 Thread Νίκος Βέργος
==
# Give user the file requested
url = "http://superhost.gr/data/files/%s"; % realfile
response = requests.get( url, stream=True )

with open( realfile, 'wb' ) as f:
f.write( response.content )
==

The above code will download and save the specific file locally to the server 
where the python script is running.

What alternation does this need to be able to send it to the client's browser?
-- 
https://mail.python.org/mailman/listinfo/python-list