Re: licenses

2016-01-10 Thread Thomas 'PointedEars' Lahn
Ben Finney wrote:

> "Martinez, Jorge Alberto (GE Aviation)" […] writes:
>> We develop applications here with Python and I want to know if there's
>> issues by using. We use NumPy, PyDaqMx, Py Visa
> 
> Those are all free software: meaning, every recipient has freedom to
> execute, modify, and/or redistribute the work.

Correct.

> So long as the code base you derive from them is also free software, you 
> will not need to take special care.

No, not all free software licenses (FSLs) are compatible with each other.  
Most notably, not all FSLs are compatible with the GNU General Public 
License (GPL), which also is a FSL.

Software licenses are distinguished whether they are closed source or open 
source licenses, free or unfree, and enforce copyright or copyleft (the GPL 
are/does the latter, respectively).  Also, patent issues have to be 
considered: Some free software licenses, like the Apache License, include 
stipulations that the grated patent license is void if the licensee sues the 
licenser for patent infringement; other licenses do not.

A quick Google search shows:

- NumPy is licensed under the “BSD-new license”.

  

- PyDAQmx can be licensed under either

  ,-
  |
  | (1) The BSD license. 
  | (2) Any other license, as long as it is obtained from the original 
  | author.

- PyVISA is licensed under “The MIT License” (X11 License)

  

See also:




IANAL.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python launcher options

2016-01-10 Thread Tim Golden



On 10/01/2016 05:18, Edward Diener wrote:

On 1/9/2016 11:03 AM, Tim Golden wrote:

On 06/01/2016 00:48, Edward Diener wrote:

The Python launcher in Windows is a neat tool for running multiple
versions of Python 2 and Python 3 at different times. It allows as
options the ability to specify the latest version of either Python 2 or
Python 3 defaulting to the 64-bit version if both exist, or a specific
32-bit or 64-bit version of Python 2 or Python 3. What is missing is the
ability to specify the latest 32-bit version of Python 2 or Python 3.
The equivalent syntax would be '-2-32' or '-3-32'. Is there some reason
why this option has been disallowed ?


As far as I can remember, it's not so much a question of "disallowed" as
just "not thought of by anyone". If you wanted this to go anywhere,
could I suggest you create an issue on the Python issue tracker:

   http://bugs.python.org

and mark it as "Windows" in the [Components] field (that makes sure that
some relevant people get to see it). It's got a much better chance of
achieving traction if you can actually provide a code patch to implement
the behaviour. Failing that, at least make a good case which might
convince one of the developers that it would it be worth their while
implementing the change.


I have tried to register with the link above so I can an issue with the
Python Issue tracker but all attempts fail with:

"Failed issue tracker submission

An unexpected error occurred during the processing
of your message. The tracker administrator is being
notified."


Hmmm. Thanks for making the effort -- and for reporting back. I've just 
successfully registered a (dummy) account there, so it's possible that 
there was a temporary glitch.


If you wouldn't mind trying once more, that would be helpful. If not, I 
can create the issue on your behalf, and inform the tracker admins.


Thanks

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


Fwd: python unit test framework sample code

2016-01-10 Thread Ganesh Pal
Apologies,  looks like I did a reply instead of reply-all. So
forwarding this email , please provide guidance on the same

-- Forwarded message --
From: Ganesh Pal 
Date: Thu, Jan 7, 2016 at 12:26 PM
Subject: Re: python unit test framework sample code
To: Terry Reedy 

> Unless you have a large program already in 2.6 and are just adding tests
> (perhaps so you can more easily upgrade someday), consider upgrading to a
> newer version.

Sure , but for now Iam limited to use 2.6 , hence I cant upgrade need
to work with 2.6

>>
>> class FileSystemTest(unittest2.TestCase):
>>  block_address = {}
>>  report = ""
>>
>>  @classmethod
>>  def setUpClass(cls):
>>  cls.FileSystemSetup()
>
>
> This is senseless.  Put the body of FileSystemSetup here.

I didn't understand  which line is senseless. I was trying to refer
this example on stack overflow
http://stackoverflow.com/questions/5938517/not-able-call-a-local-method-from-setupclass


class TestSystemPromotion(unittest2.TestCase):

  @classmethod
  def setUpClass(cls):
cls.setup_test_data()


  @classmethod
  def setup_test_data(cls):
...

  def test_something(self):
...


class FileSystemTest(unittest2.TestCase):
block_address = {}
report = ""

@classmethod
def setUpClass(cls):
cls.FileSystemSetup()

@classmethod
def FileSystemSetup(cls):
"""
Initial setup before unittest is run
"""
logging.info("SETUP.Started !!!")

>>  def inode_corruption(self):
>>  """ test_01: inode  corruption """
>>  self.assertTrue(corrupt.run_query_tool(self.__class__.report,
>
>
> self.block_address['test_01']))
>
> Assuming that unittest2 is same as unittest, test methods must be called
> test_xyz.  So this is not run, hence no error.

Sorry  I have changed this from inode_corruption' to 'test_inode_corruption

>
>>  @classmethod
>>  def tearDownClass(cls):
>>  cls.tearDown()
>
>
> Ditto.  Put real body here.
>
>>  @classmethod
>>  def tearDown(cls):
>
>

> The above refers to functions you did not post.  For current unittest, it
> looks likes you should be using a setUpModule (possible tearDownModule)
> functions, but I don't know if those are available in the older unittest2.


we have tearDownClass and setUpClass in python 2.6 and under unittest2

>>> help(unittest2.TestCase.tearDownClass)
Help on method tearDownClass in module unittest2.case:

tearDownClass(cls) method of __builtin__.type instance
Hook method for deconstructing the class fixture after running all
tests in the class.

>>> help(unittest2.TestCase.setUpClass)
Help on method setUpClass in module unittest2.case:

setUpClass(cls) method of __builtin__.type instance
Hook method for setting up class fixture before running tests in the class.

Regards,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: When I need classes?

2016-01-10 Thread Michael Torrie
On 01/10/2016 12:29 AM, Arshpreet Singh wrote:
> Hello Friends, I am quite new to OOP(object oriented Programming), I
> did some projects with python which includes Data-Analysis, Flask Web
> Development and some simple scripts.
> 
> I have only one question which is bothering me most of the time, When
> I will get the need to use Classes in Python? Or in other way is
> there any real-life example where you can tell me this
> problem/solution is kind of impossible in Python without Classes?

I can't speak to Flask or any other web development.  But for simple
scripts, I'll start out with no classes.  Just functions as needed, and
some main logic.  I always use the idiom:

if __name__ == '__main__':
# do main logic here

This way I can import functions defined in this script into another
script later if I want.

If I find I need to share state between functions, and if I find that I
might need to have multiple situations of shared state possibly at the
same time, then I will refactor the script into a class.  Despite the
apparent shame of using global variables, if I need to share state
between functions, but I cannot ever foresee a time when I'll need to
have multiple instances of that state, then I'll just use a module-level
global variable and continue to use normal functions, rather than define
a class.  In the parlance of OOP, this use case would be referred to as
a "singleton" and a python module (a script) is a form of singleton
already, even without using classes.

In the end my code often is a mix of classes and non-class -based code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"

2016-01-10 Thread kbtyo
Hello Everyone:

I am curious to know why I receive the aforementioned message. I am using 
Python 3.4.3 and Windows 7. I am running the following script from Windows 
Powershell:


Response = 's.csv'
with open(Response, 'rU', encoding='utf-8') as data:
separated = data.read().split('","')
x = ElementTree.XML(separated[3])
y = ElementTree.XML(separated[4])
print(dict(flatten_dict(x)))
print(dict(flatten_dict(y)))

I am importing ElementTree as follows:

import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser


The input data is as follows:

A,B,C,D,E,F,G,H,I,J
"3","8","1","2312285SChecking10","TrueFalseFalseFalseFalse',0001,0070,","1967-12-25
 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"

Oddly, when I run the same script via WinPython' Jupyter Notebook, it works 
perfectly. The input string is an XML string and I am only interested in the 
4th and 5th columns, (using zero based indexing). 

Thank you, in advance for your feedback and support. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"

2016-01-10 Thread Steven D'Aprano
On Mon, 11 Jan 2016 02:04 am, kbtyo wrote:

> Hello Everyone:
> 
> I am curious to know why I receive the aforementioned message. 


It is impossible to be sure from the information you have given us, since we
do not know which line of code caused the error.

Please copy and paste the ENTIRE traceback, starting from the line:

Traceback (most recent call last)

to the end of the error message. This will (hopefully) show us which line of
your code caused the error.



-- 
Steven

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


Re: When I need classes?

2016-01-10 Thread Steven D'Aprano
On Sun, 10 Jan 2016 06:29 pm, Arshpreet Singh wrote:

> Hello Friends, I am quite new to OOP(object oriented Programming), I did
> some projects with python which includes Data-Analysis, Flask Web
> Development and some simple scripts.
> 
> I have only one question which is bothering me most of the time, When I
> will get the need to use Classes in Python? Or in other way is there any
> real-life example where you can tell me this problem/solution is kind of
> impossible in Python without Classes?

There are *no* problems that are impossible to solve without classes, but
sometimes classes will make problems easier to solve. And sometimes classes
make problems harder to solve. It depends on the problem.

This may help you:

http://kentsjohnson.com/stories/00014.html



-- 
Steven

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


Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"

2016-01-10 Thread Saran Ahluwalia
Hi Steven:

That is the only message (*xml.etree.ElementTree.Element' does not support
the buffer interface"*). There is no traceback. My apologies for not
clarifying previously.

On Sun, Jan 10, 2016 at 10:19 AM, Steven D'Aprano 
wrote:

> On Mon, 11 Jan 2016 02:04 am, kbtyo wrote:
>
> > Hello Everyone:
> >
> > I am curious to know why I receive the aforementioned message.
>
>
> It is impossible to be sure from the information you have given us, since
> we
> do not know which line of code caused the error.
>
> Please copy and paste the ENTIRE traceback, starting from the line:
>
> Traceback (most recent call last)
>
> to the end of the error message. This will (hopefully) show us which line
> of
> your code caused the error.
>
>
>
> --
> Steven
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"

2016-01-10 Thread Steven D'Aprano
On Mon, 11 Jan 2016 02:04 am, kbtyo wrote:

> Hello Everyone:
> 
> I am curious to know why I receive the aforementioned message. I am using
> Python 3.4.3 and Windows 7. I am running the following script from Windows
> Powershell:

I created a file "data" containing the input data you said:

> The input data is as follows:
> 
> A,B,C,D,E,F,G,H,I,J
> "3","8","1"," />2312 />285SChecking10"," TransactionID="2"
>
RequestType="HoldInquiry">TrueFalseFalseFalseFalse',0001,0070,","1967-12-25
> 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"



and then a script containing the code you said you used:

> import xml.etree.cElementTree as ElementTree
> from xml.etree.ElementTree import XMLParser
 
> Response = 's.csv'
> with open(Response, 'rU', encoding='utf-8') as data:
> separated = data.read().split('","')
> x = ElementTree.XML(separated[3])
> y = ElementTree.XML(separated[4])
> print(dict(flatten_dict(x)))
> print(dict(flatten_dict(y)))


I get a completely different error to you, complete with traceback as
expected:

Traceback (most recent call last):
  File "/tmp/testxml.py", line 9, in 
print(dict(flatten_dict(x)))
NameError: name 'flatten_dict' is not defined


This shows me three things:

(1) The calls to ElementTree.XML work fine, and don't raise an exception;

(2) There is no error message referring to xml.etree.ElementTree.Element or
the buffer interface;

(3) The code you posted is clearly not the code you actually ran. At the
very least, it is not *all* the code you ran.

We cannot tell what it wrong with your code if you don't show us the code
that fails. I suggest you read this webpage:

http://www.sscce.org/

and follow the advice given. It's written for Java, but applies to any
programming language. Hopefully you will either solve your problem, or be
able to generate a sufficiently small piece of code that we can work with.


You also suggest that your code works when running in a Jupyter Notebook. It
is unlikely (but not impossible!) that exactly the same code will run
differently when run as a script and when run under Jupyter. More likely,
there is some difference between the code, something you have written in
the Notebook but not included in the script.

If it is exactly the same code, then perhaps it is a difference in the two
environments. Does Jupyter set up the environment differently to what you
get when running a script?

Finally, in another post, you state:

"That is the only message (*xml.etree.ElementTree.Element' does not support
the buffer interface"*). There is no traceback."


That is very unlikely with the code sample you posted. If true, that gives
more evidence that you are running code which is different from what you
have posted here. Perhaps your ACTUAL code (not the pretend code you showed
us) includes a try...except block like this:

try:
some code goes here
except Exception as err:
print(err)
sys.exit()


or similar. If so, TAKE IT OUT. That is destroying useful debugging
information and making it more difficult to solve your problem.





-- 
Steven

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


Re: When I need classes?

2016-01-10 Thread Bernardo Sulzbach
Essentially, classes (as modules) are used mainly for organizational purposes.

Although you can solve any problem you would solve using classes
without classes, solutions to some big problems may be cheaper and
more feasible using classes.

If Python is your everyday scripting tool, you will usually not need
classes, they will add more complexity than you need and passing data
between functions may be done with well-documented tuples and
dictionaries.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding " 'xml.etree.ElementTree.Element' does not support the buffer interface"

2016-01-10 Thread Saran Ahluwalia
Hi Steven:

The previous code was a stand along under the " if __name__ == '__main__':
". The full function suite that I have made (and indeed includes a try and
except block):

import os.path
import sys
import csv
from io import StringIO
import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser
# import xml
# import xml.sax
# from xml.sax import ContentHandler


def flatten_list(self, aList, prefix=''):

for i, element in enumerate(aList, 1):
eprefix = "{}{}".format(prefix, i)
if element:
# treat like dict
if len(element) == 1 or element[0].tag != element[1].tag:
yield from flatten_dict(element, eprefix)
# treat like list
elif element[0].tag == element[1].tag:
yield from flatten_list(element, eprefix)
elif element.text:
text = element.text.strip()
if text:
yield eprefix[:].rstrip('.'), element.text


def flatten_dict(parent_element, prefix=''):

prefix = prefix + parent_element.tag
if parent_element.items():
for k, v in parent_element.items():
yield prefix + k, v
for element in parent_element:
eprefix = element.tag
if element:
# treat like dict - we assume that if the first two tags
# in a series are different, then they are all different.
if len(element) == 1 or element[0].tag != element[1].tag:
yield from flatten_dict(element, prefix=prefix)
# treat like list - we assume that if the first two tags
# in a series are the same, then the rest are the same.
else:
# here, we put the list in dictionary; the key is the
# tag name the list elements all share in common, and
# the value is the list itself
yield from flatten_list(element, prefix=eprefix)
# if the tag has attributes, add those to the dict
if element.items():
for k, v in element.items():
yield eprefix+k
# this assumes that if you've got an attribute in a tag,
# you won't be having any text. This may or may not be a
# good idea -- time will tell. It works for the way we are
# currently doing XML configuration files...
elif element.items():
for k, v in element.items():
yield eprefix+k
# finally, if there are no child tags and no attributes, extract
# the text
else:
yield eprefix, element.text



def just_xml_data(path):
with open(path, 'rU', encoding='UTF-8') as data:
separated = data.read().split('","')
print(separated)
try:
x = ElementTree.XML(separated[3])
print(x)
xml.etree.ElementTree.dump(x)
y = ElementTree.XML(separated[4])
xml.etree.ElementTree.dump(y)
# response = ElementTree.XML(separated[4])  # work on the
Response column
# root = ElementTree.XML(response) #serialize and parse into
XML object
except Exception as e:
print(e)
else:
xml_field = dict(flatten_dict(y))
return xml_field

def read_data(path):
headers= set()
rows = []
with open(path, 'rU', encoding='utf-8') as data:
reader = csv.DictReader(data, dialect=csv.excel,
skipinitialspace=True)
for row in reader:
xml_field = row["CLIENT_RESP_DATA"]
# xml_data = just_xml_data(xml_field) ## function
if xml_data is not None:
row.update(xml_data)
headers.update(row.keys())
rows.append(row)
else:
print("Failure")
pass
with open(os.path.splitext(textFile)[0] + '_' + 'parsed' + '.csv',
"wt", newline='') as output_file:
wr = csv.writer(output_file)
csv_headers = list(headers)
wr.writerow(csv_headers)
for row in rows:
values = []
for field in csv_headers:
value = row.get(field, None)
values.append(value)
wr.writerow(values)
return output_file



if __name__ == '__main__':
Response = "s.csv"
just_xml_data(Response)


Hopefully this will provide you with enough information to emulate
(apologies for any and all indentation errors during the copy and paste).
FYI - I still receive the same error.


On Sun, Jan 10, 2016 at 12:27 PM, Steven D'Aprano 
wrote:

> On Mon, 11 Jan 2016 02:04 am, kbtyo wrote:
>
> > Hello Everyone:
> >
> > I am curious to know why I receive the aforementioned message. I am using
> > Python 3.4.3 and Windows 7. I am running the following script from
> Windows
> > Powershell:
>
> I created a file "data" containing the input data you said:
>
> > The input data is as follows:
> >
> > A,B,C,D,E,F,G,H,I,J
> > "3","8","1"," > />2312 >
> />285SChecking1

What use of these _ prefix members?

2016-01-10 Thread Robert
Hi,

When I read a sample file from hmm learn package, which is on top of 
scikit-learn package, I see there are many member functions (prefix with 
'_') have no caller. That is, I don't see anything uses those functions.
Below is a sample part from class GMMHMM(_BaseHMM):





//
def __init__(self, n_components=1, n_mix=1,
 params="stmcw", init_params="stmcw"):
_BaseHMM.__init__(self, n_components,.
  params=params, init_params=init_params)

# XXX: Hotfit for n_mix that is incompatible with the scikit's
# BaseEstimator API
self.n_mix = n_mix
self.covariance_type = covariance_type
self.covars_prior = covars_prior
self.gmms_ = []
for x in range(self.n_components):
if covariance_type is None:
gmm = GMM(n_mix)
else:
gmm = GMM(n_mix, covariance_type=covariance_type)
self.gmms_.append(gmm)

def _init(self, X, lengths=None):
super(GMMHMM, self)._init(X, lengths=lengths)

for g in self.gmms_:
g.set_params(init_params=self.init_params, n_iter=0)
g.fit(X)

def _compute_log_likelihood(self, X):
return np.array([g.score(X) for g in self.gmms_]).T

def _generate_sample_from_state(self, state, random_state=None):
return self.gmms_[state].sample(1, random_state=random_state).flatten()


Some online tutorials tell me '_' prefix is a kind of convention of private 
members. hmm is not a formal package. Do these '_' member functions are
unfinished? Or, they may be used in some way I don't know yet?

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


Re: What use of these _ prefix members?

2016-01-10 Thread Steven D'Aprano
On Mon, 11 Jan 2016 04:55 am, Robert wrote:

> Some online tutorials tell me '_' prefix is a kind of convention of
> private members. 

Correct. Not just private members, but private *anything*.

Any time you see anything starting with a single underscore, whether it is a
module, class, method, attribute or variable, you can assume that it is
private (unless explicitly documented otherwise) and avoid it.



-- 
Steven

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


Re: What use of these _ prefix members?

2016-01-10 Thread Peter Otten
Robert wrote:


> When I read a sample file from hmm learn package, which is on top of
> scikit-learn package, I see there are many member functions (prefix with
> '_') have no caller. That is, I don't see anything uses those functions.
> Below is a sample part from class GMMHMM(_BaseHMM):

I bet the caller is in the superclass _BaseHMM, like in the following made-
up example:

>>> class Base:
... def __init__(self, x):
... self._init(x)
... def _init(self, x): print("do something with", x)
... 
>>> class Derived(Base):
... def _init(self, x):
... super()._init(x)
... print("do something else with", x)
... 
>>> Derived(42)
do something with 42
do something else with 42
<__main__.Derived object at 0x7f8e6b3e9b70>


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


Re: How to remove item from heap efficiently?

2016-01-10 Thread Sven R. Kunze

Wow. That's an impressive reply.

On 08.01.2016 20:26, srinivas devaki wrote:

So you need a task scheduler with expiration and priority on each task.
Interesting Problem..

as peter said about marking the task in heapB to be deleted. but this
needs searching everytime you pop off an old item [complexity:
O(len(heapB))]. you may as well use python del on it as complexity is
same.
But if you already know the where to look in the heapB then you can
avoid searching and thereby reducing the complexity. you can do this
by saving the references of heapB in heapA and viceversa

and if you marking delete on a number of low priority tasks, then it
can increase your heapB enormously because being low priority items
they can stagnate. to resolve this error you have to perform linear
checking iterations at every critical length (this critical length can
be obtained mathmatically), so that your amortized complexity of push,
pop remains log(number_of_valid_tasks_at_any_time);
the number of valid tasks at any time are those tasks which are not
expired at the time.

My Algorithm:
version_a: https://gist.github.com/9ce7a0e534c6e768239e
this version has simple scheduler which has methods for popping high
priority one and popping oldest task.
But this version has a disadvantage, i.e If lets say you are pushed
some 10**5 tasks with priority 2, and then pushed some 10**5 tasks
with priority 1. and then you decided to pop the oldest 10**5 items.
in this version that 10**5 elements will stagnate in priority heap if
in future all priorities are less than 1.
now this is not a big issue but if you are running a webserver and
over a span of 5 days there could be 10**10 tasks, and even if half of
those tasks stagnate your server is going to crash with out of memory.

version_b: https://gist.github.com/99b4d590753ba234eeed
this version resolved that stagnation. this one will run sweeps
whenever there are more than half of useless items, thereby giving us
an amortized complexity of O(log(n)) for push, pop, etc

but this version doesn't have the feature of expiration.

version_c: https://gist.github.com/9dfd0d291672c0ffa5c3
in this one we simply keep a variable expiration, and relieve the
expired tasks on any operation. i have coded it in such a way that
expiration is specific time, you can change it to delta time if you
want to.

Time Complexity: O(log(n)) insertion, O(log(n)) deletion   [amortized]
Space Complexity: O(n)  [amortized]
here n is number of valid items i.e which are not expired.

I hope this helps with your problem


Indeed. I already do the sweep method as you suggested. ;)

Additionally, you provided me with a reasonable condition when to do the 
sweep in order to achieve O(log n). Thanks much for that. I currently 
used a time-bases approached (sweep each 20 iterations).


PS: Could you add a note on how you got to the condition ( 
2*self.useless_b > len(self.heap_b))?



PS:
sorry for posting links, it's just that the code is large for email.
I'm using minimum number has highest priority convention.


I like Web technology, so no problem here. :)


On Fri, Jan 8, 2016 at 10:15 PM, Sven R. Kunze  wrote:

Thanks for your suggestion.

On 08.01.2016 14:21, srinivas devaki wrote:

You can create a single heap with primary key as timestamp and
secondary key as priority, i.e by creating a tuple
insert the elements into the heap as
(timestamp, priority)

I think I cannot use that because I need the list sorted by both criteria.

If there is any underlying meaning for creating 2 heaps. please mention.


I use two heaps because I need to insert arbitrary items fast and remove the
ones fast which are too old (timestamp) or are next in order (priority).

Basically a task scheduler where tasks can be thrown away once they are too
long in the queue.



On Fri, Jan 8, 2016 at 4:22 AM, Sven R. Kunze  wrote:

Hi everybody,

suppose, I need items sorted by two criteria (say timestamp and
priority).
For that purpose, I use two heaps (heapq module):

heapA # items sorted by timestamp
heapB # items sorted by priority

Now my actual problem. When popping an item of heapA (that's the oldest
item), I need to remove the very same item from heapB, regardlessly where
it
is in heapB. And vice versa.

Is there a datastructure or a simple trick to achieve that in an
efficient
matter?

Best,
Sven
--
https://mail.python.org/mailman/listinfo/python-list




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


Re: How to remove item from heap efficiently?

2016-01-10 Thread Sven R. Kunze

On 09.01.2016 19:32, Paul Rubin wrote:

"Sven R. Kunze"  writes:

Basically a task scheduler where tasks can be thrown away once they
are too long in the queue.

I don't think there's a real nice way to do this with heapq.  The
computer-sciencey way would involve separate balanced tree structures
for the two sorting keys (think of a database table with indexes on two
different columns).


Others suggested using an additional dict where to store the indexes of 
the items. Then, when removing an item, I just need to query the dict.


Best,
Sven
--
https://mail.python.org/mailman/listinfo/python-list


Question on pytest example code

2016-01-10 Thread Robert
Hi,

Below is a code snippet from pytest package. It passes pytest, i.e. there is
no failure report.


# content of test_sysexit.py
import pytest

def f():
 raise SystemExit(1)

def test_mytest():
 with pytest.raises(SystemExit):
  f()


I see that f() will generate 'SystemExit(1)'. Then what does function
test_mytest()?

Is it missing some assert line?
The above code is from page 5 (9 of 93) of 'pytest Documentation' 
Release 2.8.2

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


Re: licenses

2016-01-10 Thread Cody Piersall
On Fri, Jan 8, 2016 at 1:41 PM, Martinez, Jorge Alberto (GE Aviation) <
[email protected]> wrote:
>
> Hello
> We develop applications here with Python and I want to know if there's
issues by using.
> We use NumPy, PyDaqMx, Py Visa
>
> How can we cover this licensing?

I am not a lawyer, and this is not legal advice.

* NumPy is BSD-licensed, which means you can use NumPy in a commercial
product as long as you include its license.
* PyDAQmx is BSD-licensed as well.  (You can find that information in their
GitHub repository's README, https://github.com/clade/PyDAQmx)
* Py Visa is MIT licensed (info on their GitHub:
https://github.com/hgrecco/pyvisa/blob/master/LICENSE), which means you can
also use it in your software as long as you include the license.

For summaries of lots of licenses, you can look at tldrlegal.com.

* BSD license: https://tldrlegal.com/license/bsd-3-clause-license-(revised)
* MIT license: https://tldrlegal.com/license/mit-license

It's never a bad idea to consult a lawyer.  Since you work for GE, I would
imagine there is an army of lawyers happy to answer this question at your
disposal.  Finding out how to talk to them might be the hard part.

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


Re: licenses

2016-01-10 Thread Bernardo Sulzbach
Cody wrote a good and correct answer.

Everyone is going with "lawyers, lawyers, lawyers...". It is not any
lawyer. Most big companies and consulting firms will have lawyers that
are experienced with open source licenses, some that have even fought
against GPL and whatnot. So look for the correct type of lawyer.
Although any lawyer should be able to digest those licenses -- to be
precise, any proficient English reader -- as most of them are quite
simple and short, consulting an experienced professional is safer and
a better investment of your time and money.

Good luck with one of the worst parts of software development.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python launcher options

2016-01-10 Thread Edward Diener

On 1/10/2016 6:38 AM, Tim Golden wrote:



On 10/01/2016 05:18, Edward Diener wrote:

On 1/9/2016 11:03 AM, Tim Golden wrote:

On 06/01/2016 00:48, Edward Diener wrote:

The Python launcher in Windows is a neat tool for running multiple
versions of Python 2 and Python 3 at different times. It allows as
options the ability to specify the latest version of either Python 2 or
Python 3 defaulting to the 64-bit version if both exist, or a specific
32-bit or 64-bit version of Python 2 or Python 3. What is missing is
the
ability to specify the latest 32-bit version of Python 2 or Python 3.
The equivalent syntax would be '-2-32' or '-3-32'. Is there some reason
why this option has been disallowed ?


As far as I can remember, it's not so much a question of "disallowed" as
just "not thought of by anyone". If you wanted this to go anywhere,
could I suggest you create an issue on the Python issue tracker:

   http://bugs.python.org

and mark it as "Windows" in the [Components] field (that makes sure that
some relevant people get to see it). It's got a much better chance of
achieving traction if you can actually provide a code patch to implement
the behaviour. Failing that, at least make a good case which might
convince one of the developers that it would it be worth their while
implementing the change.


I have tried to register with the link above so I can an issue with the
Python Issue tracker but all attempts fail with:

"Failed issue tracker submission

An unexpected error occurred during the processing
of your message. The tracker administrator is being
notified."


Hmmm. Thanks for making the effort -- and for reporting back. I've just
successfully registered a (dummy) account there, so it's possible that
there was a temporary glitch.

If you wouldn't mind trying once more, that would be helpful. If not, I
can create the issue on your behalf, and inform the tracker admins.


I tried again only to run into the exact same problem. I don't know what 
it means that I am not being allowed to register for the Python bug 
tracker. Needless to say the obscure error message is not telling me 
anything. Of course as a programmer who uses Python I would like to find 
out why I am not able to register. But in the meantime if you could 
create the issue on my behalf it would be appreciated. Thanks !


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


Re: Python launcher options

2016-01-10 Thread Edward Diener

On 1/10/2016 6:38 AM, Tim Golden wrote:



On 10/01/2016 05:18, Edward Diener wrote:

On 1/9/2016 11:03 AM, Tim Golden wrote:

On 06/01/2016 00:48, Edward Diener wrote:

The Python launcher in Windows is a neat tool for running multiple
versions of Python 2 and Python 3 at different times. It allows as
options the ability to specify the latest version of either Python 2 or
Python 3 defaulting to the 64-bit version if both exist, or a specific
32-bit or 64-bit version of Python 2 or Python 3. What is missing is
the
ability to specify the latest 32-bit version of Python 2 or Python 3.
The equivalent syntax would be '-2-32' or '-3-32'. Is there some reason
why this option has been disallowed ?


As far as I can remember, it's not so much a question of "disallowed" as
just "not thought of by anyone". If you wanted this to go anywhere,
could I suggest you create an issue on the Python issue tracker:

   http://bugs.python.org

and mark it as "Windows" in the [Components] field (that makes sure that
some relevant people get to see it). It's got a much better chance of
achieving traction if you can actually provide a code patch to implement
the behaviour. Failing that, at least make a good case which might
convince one of the developers that it would it be worth their while
implementing the change.


I have tried to register with the link above so I can an issue with the
Python Issue tracker but all attempts fail with:

"Failed issue tracker submission

An unexpected error occurred during the processing
of your message. The tracker administrator is being
notified."


Hmmm. Thanks for making the effort -- and for reporting back. I've just
successfully registered a (dummy) account there, so it's possible that
there was a temporary glitch.

If you wouldn't mind trying once more, that would be helpful. If not, I
can create the issue on your behalf, and inform the tracker admins.


I was finally able to register so I will report this issue on the Python 
issue tracker myself.

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


Improving code written in order to reduce verbosity and perhaps use a decorator?

2016-01-10 Thread kbtyo
Hello everyone:

A member on the Stack Overflow community advised me to post my question on this 
forum:

http://codereview.stackexchange.com/questions/116395/opening-the-same-csv-file-in-two-different-ways-in-order-to-transform-data-in-on

I appreciate your feedback immensely. 

Sincerely,
Saran
-- 
https://mail.python.org/mailman/listinfo/python-list


Confusing on bool data object or class function?

2016-01-10 Thread Robert
Hi,

When I use an on line code snippet, see below please, I find that 
'converged' in class ConvergenceMonitor is seen as a data.






-
class ConvergenceMonitor(object):
"""Monitors and reports convergence to :data:`sys.stderr`.

Parameters
--
tol : double
Convergence threshold. EM has converged either if the maximum
number of iterations is reached or the log probability
improvement between the two consecutive iterations is less
than threshold.

n_iter : int
Maximum number of iterations to perform.

verbose : bool
If ``True`` then per-iteration convergence reports are printed,
otherwise the monitor is mute.

Attributes
--
history : deque
The log probability of the data for the last two training
iterations. If the values are not strictly increasing, the
model did not converge.

iter : int
Number of iterations performed while training the model.
"""
_template = "{iter:>10d} {logprob:>16.4f} {delta:>+16.4f}"

def __init__(self, tol, n_iter, verbose):
self.tol = tol
self.n_iter = n_iter
self.verbose = verbose
self.history = deque(maxlen=2)
self.iter = 0

def __repr__(self):
class_name = self.__class__.__name__
params = dict(vars(self), history=list(self.history))
return "{0}({1})".format(
class_name, _pprint(params, offset=len(class_name)))

def report(self, logprob):
"""Reports convergence to :data:`sys.stderr`.

The output consists of three columns: iteration number, log
probability of the data at the current iteration and convergence
rate.  At the first iteration convergence rate is unknown and
is thus denoted by NaN.

Parameters
--
logprob : float
The log probability of the data as computed by EM algorithm
in the current iteration.
"""

@property
def converged(self):
"""``True`` if the EM algorithm converged and ``False`` otherwise."""
# XXX we might want to check that ``logprob`` is non-decreasing.
return (self.iter == self.n_iter or
(len(self.history) == 2 and
 self.history[1] - self.history[0] < self.tol))
/

Here is except of the online help content:

 |  Data descriptors defined here:
 |  
 |  __dict__
 |  dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |  list of weak references to the object (if defined)
 |  
 |  converged
 |  ``True`` if the EM algorithm converged and ``False`` otherwise.


The data conclusion is verified by the following test code:


from hmmlearn.base import ConvergenceMonitor

class TestMonitor(object):
def test_converged_by_iterations(self):
print 'self test0'
m = ConvergenceMonitor(tol=1e-3, n_iter=2, verbose=False)
print 'self test1', m.converged
assert not m.converged
print 'self test2', m.converged
m.report(-0.01)
assert not m.converged
print 'self test3', m.converged
m.report(-0.1)
assert m.converged
print 'self test4', m.converged

tmp_obj=TestMonitor()
print 'ttt', tmp_obj.test_converged_by_iterations()
mm = ConvergenceMonitor(tol=1e-3, n_iter=2, verbose=False)
print 'self test mm', mm.converged

That is, I can only use:

mm.converged

If I use it in this way:
mm.converged()

it will have error:
TypeError Traceback (most recent call last)
C:\Users\rj\Documents\PythonDocPrj0\hmmlearn-master\hmmlearn\tests\test_base1.py
 in ()
 27 print None
 28 mm = ConvergenceMonitor(tol=1e-3, n_iter=2, verbose=False)
---> 29 print 'self test mm', mm.converged()
 30 

TypeError: 'bool' object is not callable



On the other hand, 'play' in the following class is seen as a member
function:

class Engine(object):
def __init__(self, scene_map):
pass

def play(self):
print 'play'
return True

a_game = Engine(scene_map=0.1)
a_game.play()


Why can 'converged' only be seen as a data, while 'play' can be seen as
a function?

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


Which Python editor has this feature?

2016-01-10 Thread jfong
It lets you jump between the current cursor position and the line the upper 
level indentation start, something like the bracket matching in C editor. 
Because of Python use indentation as its code block mark, It might be helpful 
if we can jump between different level of it:-)


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


Re: Which Python editor has this feature?

2016-01-10 Thread Chris Angelico
On Mon, Jan 11, 2016 at 12:59 PM,   wrote:
> It lets you jump between the current cursor position and the line the upper 
> level indentation start, something like the bracket matching in C editor. 
> Because of Python use indentation as its code block mark, It might be helpful 
> if we can jump between different level of it:-)

I coded this up as a patch for SciTE/Scintilla at one point, but it
didn't get accepted. It was used for a while at my work, but never
really settled in as being useful. Python code tends not to be as big
and complex as C code often is, so it's not as useful to have a
feature like this.

If you want it, I can probably hunt down the patch file somewhere.

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


Re: When I need classes?

2016-01-10 Thread Cameron Simpson

On 10Jan2016 08:02, Michael Torrie  wrote:

I can't speak to Flask or any other web development.  But for simple
scripts, I'll start out with no classes.  Just functions as needed, and
some main logic.  I always use the idiom:

if __name__ == '__main__':
   # do main logic here

This way I can import functions defined in this script into another
script later if I want.


I always structure this aspect as:

 ... at or near top of script ...

 def main(argv):
   ... do main logic here ...

 ... at bottom ...
 if __name__ == '__main__':
   sys.exit(main(sys.argv))

This has the benefits of (a) putting the main program at the top where it is 
easy to see/find and (b) avoiding accidently introduction of dependence on 
global variables - because verything is inside main() it has the same behaviour 
as any other function.


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


Re: Which Python editor has this feature?

2016-01-10 Thread Tim Chase
On 2016-01-10 17:59, [email protected] wrote:
> It lets you jump between the current cursor position and the line
> the upper level indentation start, something like the bracket
> matching in C editor. Because of Python use indentation as its code
> block mark, It might be helpful if we can jump between different
> level of it:-)

While not quite what you're asking for, vim offers an "indent text
object" plugin[1] that allows you to use a block of indentation
around the cursor as an object.  So you can use vim's grammar to issue
commands like "dai" to delete the current indentation-defined block;
or you can use ">ii" to add a level of indentation to the
indentation-defined block.

If you want to make a vim mapping that will jump up to the top of the
previous level of indentation, the following should do the trick

  :nnoremap  Q '?^'.repeat(' ', (strlen(substitute(getline('.'), '\S.*', 
'', ''))-&sw)).'\S?e'."\"

There might be some edge-cases that I haven't caught there, but, as
long as you edit with spaces rather than tabs, it should work,
including the accommodation of your 'shiftwidth', even if it's not
PEP8 4-spaces-per-indent.

-tkc

[1]
https://github.com/michaeljsmith/vim-indent-object





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


Re: When I need classes?

2016-01-10 Thread Chris Angelico
On Mon, Jan 11, 2016 at 1:57 PM, Cameron Simpson  wrote:
> I always structure this aspect as:
>
>  ... at or near top of script ...
>
>  def main(argv):
>... do main logic here ...
>
>  ... at bottom ...
>  if __name__ == '__main__':
>sys.exit(main(sys.argv))
>
> This has the benefits of (a) putting the main program at the top where it is
> easy to see/find and (b) avoiding accidently introduction of dependence on
> global variables - because verything is inside main() it has the same
> behaviour as any other function.
>

Personally, I like to put 'def main()' at the bottom of the script, on
the principle that, as much as possible, code should refer to stuff
higher up rather than lower down. But otherwise, I agree. Your "if
__name__" block is just the glue between sys.{argv,exit} and your main
function, and that's how it should be.

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


Re: Confusing on bool data object or class function?

2016-01-10 Thread Steven D'Aprano
On Mon, 11 Jan 2016 12:57 pm, Robert wrote:

> Hi,
> 
> When I use an on line code snippet, see below please, I find that
> 'converged' in class ConvergenceMonitor is seen as a data.

That is because converged is a property. That makes it be seen as data.

> @property
> def converged(self):


Properties are computed attributes. Although they have a function deep
inside them, to perform the computation, they are seen from the outside as
data attributes.

See the documentation for "property" for more information, and google
for "descriptor" for the underlying mechanism that allows this to work.
(Warning: descriptors are considered very advanced material.)



-- 
Steven

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


Re: Which Python editor has this feature?

2016-01-10 Thread Rustom Mody
On Monday, January 11, 2016 at 7:30:10 AM UTC+5:30, [email protected] wrote:
> It lets you jump between the current cursor position and the line the upper 
> level indentation start, something like the bracket matching in C editor. 
> Because of Python use indentation as its code block mark, It might be helpful 
> if we can jump between different level of it:-)
> 
> 
> --Jach Fong

Recent emacs' python  mode has all these bunch of python-nav-* functions.
You may have (as usual with emacs!) to choose which you like best, ie not all 
are not bound to keys.

Click on a completion to select it.
In this buffer, type RET to select the completion near point.

Possible completions are:
python-nav--beginning-of-defun  python-nav--forward-defun
python-nav--forward-sexppython-nav--lisp-forward-sexp
python-nav--lisp-forward-sexp-safe  python-nav--syntactically
python-nav--up-list python-nav-backward-block
python-nav-backward-defun   python-nav-backward-sexp
python-nav-backward-sexp-safe   python-nav-backward-statement
python-nav-backward-up-list python-nav-beginning-of-block
python-nav-beginning-of-defun   python-nav-beginning-of-statement
python-nav-end-of-block python-nav-end-of-defun
python-nav-end-of-statement python-nav-forward-block
python-nav-forward-defunpython-nav-forward-sexp
python-nav-forward-sexp-safepython-nav-forward-statement
python-nav-if-name-main python-nav-up-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question on pytest example code

2016-01-10 Thread Terry Reedy

On 1/10/2016 2:38 PM, Robert wrote:

Hi,

Below is a code snippet from pytest package. It passes pytest, i.e. there is
no failure report.


# content of test_sysexit.py
import pytest

def f():
  raise SystemExit(1)

def test_mytest():
  with pytest.raises(SystemExit):
   f()


I see that f() will generate 'SystemExit(1)'. Then what does function
test_mytest()?


What does test_mytest do? It tests that f() raises SystemExit.


Is it missing some assert line?


The unittest version of 'pytest.raises' is 'self.assertRaises'.  The 
latter context manager __exit__ method checks that it is passed the 
exception given to the __enter__ method and fails if not.  I presume the 
pytest version is more or less identical.



The above code is from page 5 (9 of 93) of 'pytest Documentation'
Release 2.8.2


--
Terry Jan Reedy

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


Re: When I need classes?

2016-01-10 Thread Arshpreet Singh
On Sunday, 10 January 2016 23:20:02 UTC+5:30, Bernardo Sulzbach  wrote:
> Essentially, classes (as modules) are used mainly for organizational purposes.
> 
> Although you can solve any problem you would solve using classes
> without classes, solutions to some big problems may be cheaper and
> more feasible using classes.

That seems coming to me as. I am going to start Kivy to build Android 
application and As I see there are lots of Example code with classes and stuff.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: When I need classes?

2016-01-10 Thread Arshpreet Singh
On Sunday, 10 January 2016 21:09:52 UTC+5:30, Steven D'Aprano  wrote:

> There are *no* problems that are impossible to solve without classes, but
> sometimes classes will make problems easier to solve. And sometimes classes
> make problems harder to solve. It depends on the problem.

Is there any particular situation in your experience or in your past like you 
felt this could be more suitable with classes?

Actually a real life story from someone'e past experience could do much greater 
help.

> This may help you:
> 
> http://kentsjohnson.com/stories/00014.html

Thanks That is good read to remember and practice.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: When I need classes?

2016-01-10 Thread Arshpreet Singh
On Sunday, 10 January 2016 20:33:20 UTC+5:30, Michael Torrie  wrote:

> This way I can import functions defined in this script into another
> script later if I want.
> 
> If I find I need to share state between functions, and if I find that I
> might need to have multiple situations of shared state possibly at the
> same time, then I will refactor the script into a class.  Despite the
> apparent shame of using global variables, if I need to share state
> between functions, but I cannot ever foresee a time when I'll need to
> have multiple instances of that state, 

I have a case in Flask-Oauth2 where one function returns Username, Email ID and 
Authorised Token.

So I can make that function Global and access EMail,username and Authorised 
token from any other Script.

Or 
I can make it class?
  
>then I'll just use a module-level
> global variable and continue to use normal functions, rather than define
> a class.  In the parlance of OOP, this use case would be referred to as
> a "singleton" and a python module (a script) is a form of singleton
> already, even without using classes.

Is it also true that Classes(OOP) help to optimise code(uses less memory) 
rather than just doing things with functions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: When I need classes?

2016-01-10 Thread Cameron Simpson

On 10Jan2016 22:45, Arshpreet Singh  wrote:

On Sunday, 10 January 2016 20:33:20 UTC+5:30, Michael Torrie  wrote:

This way I can import functions defined in this script into another
script later if I want.

If I find I need to share state between functions, and if I find that I
might need to have multiple situations of shared state possibly at the
same time, then I will refactor the script into a class.  Despite the
apparent shame of using global variables, if I need to share state
between functions, but I cannot ever foresee a time when I'll need to
have multiple instances of that state,


I have a case in Flask-Oauth2 where one function returns Username, Email ID and 
Authorised Token.

So I can make that function Global and access EMail,username and Authorised 
token from any other Script.


Note: you can access the _function_ from another script or module. When your 
programs do something like:


 from os.path import basename

they are doing exactly this.


Or
I can make it class?


On its own it doesn't mean much. Michael Torrie's criterion said "share state 
between functions"; that state is normally an instance of the class. So you 
have some value and a bunch of standard things you would do with that kind of 
value. That is the situation where a class is a natural thing to use: you make 
a class to represent the value, and each of the standard things you would do 
with one of those values is a class method.


In your situation above I would be inclined to make a class to represent the 
3-tuple you outline above: Username, Email ID and Authorised Token. So:


 from collections import namedtuple
 Authorisation = namedtuple('Authorisation', 'username email authorised_token')

now, where you would have obtained these as a tuple:

 # call your "in Flask-Oauth2 where one function returns Username..."
 authorisation = get_auth_info(...)

and then access authorisation[0] for the username and so forth, you can go:

 # fetch the 3 values and make an "Authorisation" from them
 authorisation = Authorisation(get_auth_info(...))

and the access authorisation.username, authorisation.email etc. This avoids 
knowing special index numbers (0, 1 and 2) which makes your code more readable 
and also makes it easy to pass around the authorisation for use.


Then, if you have things you routinely do with an "Authorisation" you can make 
methods for them. So that your code can say:


 authorisation.do_this(...)

and so forth.


then I'll just use a module-level
global variable and continue to use normal functions, rather than define
a class.  In the parlance of OOP, this use case would be referred to as
a "singleton" and a python module (a script) is a form of singleton
already, even without using classes.


Is it also true that Classes(OOP) help to optimise code(uses less memory) 
rather than just doing things with functions.


Not really? I would not expect using a class to inherently get you less memory 
use, just better and more consistent naming. There are some special situations 
where you can use a class to reduce your memory usage, but they are fairly 
dependent on what you're doing.


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


Re: When I need classes?

2016-01-10 Thread Ulli Horlacher
Cameron Simpson  wrote:

> I always structure this aspect as:
> 
>  ... at or near top of script ...
> 
>  def main(argv):
>... do main logic here ...
> 
>  ... at bottom ...
>  if __name__ == '__main__':
>sys.exit(main(sys.argv))

I, as a Python beginner, came to the same solution!
It seems, it was a good idea :-)


-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: [email protected]
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list