About Modifying Globals

2014-12-04 Thread LJ
Hi All,

I have a quick question regarding the modification of global variables within 
functions. To illustrate, consider the following toy example:

a={"1": set()}
b=9

def gt(l):
   a["1"] = a["1"] | set([l])

When calling this last function and checking the a dictionary, I get:

>>> gt(5)
>>> a
{"1": set([5])}


The set in the dictionary was modified. The question is, why isn't it necessary 
to declare a as global within the gt function, as apposed to a case like  

def gt2(l):
   b=b+l

where I need to declare b as global within the function to avoid:

UnboundLocalError: local variable 'b' referenced before assignment.


I apologize if this question has been answered before.

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


id() and is operator

2015-02-22 Thread LJ
Hi everyone. Quick question here. Lets suppose if have the following numpy 
array:

b=np.array([[0]*2]*3)

and then:

>>> id(b[0])
4582
>>> id(b[1])
45857512
>>> id(b[2])
4582

Please correct me if I am wrong, but according to this b[2] and b[0] are the 
same object. Now,

>>> b[0] is b[2]
False


Any clarification is much appreciated.

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


Re: Numpy Array of Sets

2014-05-25 Thread LJ
Wolfgang, thank you very much for your reply.

Following the example in the link, the problem appears:

>>> A = [[0]*2]*3
>>> A
[[0, 0], [0, 0], [0, 0]]
>>> A[0][0] = 5
>>> A
[[5, 0], [5, 0], [5, 0]]

Now, if I use a numpy array:

>>> d=array([[0]*2]*3)
>>> d
array([[0, 0],
   [0, 0],
   [0, 0]])
>>> d[0][0]=5
>>> d
array([[5, 0],
   [0, 0],
   [0, 0]])


What is the difference here?

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


Re: Numpy Array of Sets

2014-05-25 Thread LJ
Thank you for the reply.

So, as long as I access and modify the elements of, for example, 

A=array([[set([])]*4]*3)


as (for example):

a[0][1] = a[0][1] | set([1,2])

or:

a[0][1]=set([1,2])

then I should have no problems?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Numpy Array of Sets

2014-05-25 Thread LJ
Thank you very much!
-- 
https://mail.python.org/mailman/listinfo/python-list


pypy - Gurobi solver library

2015-09-29 Thread LJ
Hi All,

I use gurobipy to model a large scale optimization problem. Is there a way to 
use pypy with the gurobipy library? Has anyone done this?

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


PyPy subprocess

2015-11-01 Thread LJ
Hi All.

I hope you're having a good weekend. Im working on a large scale optimization 
problem which invokes the Gurobi solver from python. Before invoking the solver 
I use pure python to solve five subproblems in parallel using the 
multiprocessing module. These subproblems are the bottleneck of my procedure. 
Im wondering if there is a way in which I can use PyPy to solve the just 
subproblems in parallel, and return to CPython for the overall routines. The 
reason behind this, is that Gurobipy (python interface for the Gurobi 
optimization solver) is not compatible with PyPy, and Im seeing savings of 50% 
in time when using PyPy to somve the subproblems.

Any help would be highly appreciated.

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


PyPy subprocess

2015-11-01 Thread LJ
By the way, Im using python 2.7.

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


Multiprocessing PyPy-CPython

2015-11-24 Thread LJ
Hi everyone.

Im trying to solve some problems in parallel using the multiprocessing module 
in Python 2.7. A general model is built using CPython and then the subproblems 
are solved in parallel returning results in a queue. This is currently working 
fine. I would like to solve the subproblems using PyPy to increase speed.
I found http://project-trains.tumblr.com/post/102076598295/multiprocessing-pypy 
, but there it says that the procedure only works with CPython 3.4.

I wonder is there is any clean direct way to do this.

Appreciate any help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Iterating through set

2014-07-14 Thread LJ
Hi All.

I'm coding a Dynamic Programming algorithm to solve a network flow problem. At 
some point in the algorithm I have to iterate through a set of nodes, while 
adding and/or removing elements, until the set is empty. I know a regular set() 
object does not work in a case like this, so I wonder if anyone knows of an 
efficient pythonic way to handle this.

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


Comparison

2014-09-22 Thread LJ
Hi All,

Quick question here. In the code I am working on I am apparently doing a lot of 
dictionary lookups and those are taking a lot of time. 
I looked at the possibility of changing the structure and I found about the 
numpy structured arrays.
The concrete question is: what would be the difference between using one or the 
other in terms of performance? meaning looping and performing checks an 
operations on them?

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


Re: Comparison

2014-09-22 Thread LJ
On Monday, September 22, 2014 1:12:23 PM UTC-4, Chris Angelico wrote:
> On Tue, Sep 23, 2014 at 2:57 AM, LJ  wrote:
> 
> > Quick question here. In the code I am working on I am apparently doing a 
> > lot of dictionary lookups and those are taking a lot of time.
> 
> > I looked at the possibility of changing the structure and I found about the 
> > numpy structured arrays.
> 
> > The concrete question is: what would be the difference between using one or 
> > the other in terms of performance? meaning looping and performing checks an 
> > operations on them?
> 
> 
> 
> The lookups themselves almost certainly aren't actually taking the
> 
> time, unless you have some kind of crazy hash collision happening,
> 
> which is so statistically unlikely that it would have to have come
> 
> from malice. What's the code doing? What are you trying to accomplish?
> 
> It's much more likely that you have an algorithmic flaw than a data
> 
> type inefficiency.
> 
> 
> 
> ChrisA



Thank you for your reply.

Basically what is happening is the following:

I have a network in which the nodes are defined as dictionaries using the 
NetworkX package. Inside each node (each dictionary) I defined a dictionary of 
dictionaries holding attributes corresponding to different ways in which the 
node can be reached (this dictionaries I refer to as labels). 
At some point in my algorithm I am looping through some subset of nodes and 
through the labels in each node and I perform some "joining" checks with the 
labels of each node in another subset of nodes. To clarify I check for a 
feasibility condition in a pairwise manner for every label in one node and 
every label of another. This nested loop is taking time.
I wonder if the fact of defining the labels using numpy instead of dictionaries 
could imply a faster way to perform this loop.

I hope I was clear.

Thanks.

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


gmail/poplib: quickly detecting new mail

2006-07-01 Thread LJ
Hello,

I'm trying to monitor my gmail account to know when I have obtained a
new email.  It seems that once I have logged in, I should be able to
call the stat() function repeatedly to see how many messages are in my
inbox.  The problem is that this number does not seem to update until I
have logged out, and logged back in.  In other words, I run the code
below, send myself an email, and observe that the count does not
change.  If I kill the program and restart (hence logging back in),
then the total count is now updated.  The other function calls seem to
work the same way (eg "list" just shows the same list, even when I know
new mail has arrived).

Questions:
1. is this a standard behavior of pop protocol? (to give me the same
results for any API call on a single login?)
2. OR is this a peculiarity of gmail
3. is there a more efficient and correct way to see know when I have
received a new mail?  Currently my "working" method is to log out and
log back in.  With this method, I can get about 17 refreshes per minute
but anything faster and Gmail blocks me for a few minutes. (yes it is
important to my application that I have very frequent refreshes).

(see code sample below)

Thanks,
LJ

---

import poplib
import time
from email.Parser import Parser

parser = Parser()
server = poplib.POP3_SSL("pop.gmail.com", 995)
print server.user("XXX-MY_EMAIL")
print server.pass_("XXX-MY_PW")
server.set_debuglevel(0)

def getMsgCount():
  # check message count by stat() and list() functions
  numMsgs = server.stat()[0]
  print "Num msg by stat():", numMsgs
  print "Num msg by list():", len(server.list()[1])
  print "Most recent:", numMsgs, getSubj(numMsgs)
  return

def getSubj(which):
  # return subject of message with id 'which'
  msg = "\n".join(server.top(which, 1)[1])
  email = parser.parsestr(msg)
  return email.get("Subject")

while True:
  print "--"
  getMsgCount()
  time.sleep(2)

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


Re: A little test for you Guys😜

2015-09-22 Thread Lj Fc via Python-list
On Tuesday, September 22, 2015 at 11:19:00 PM UTC+2, [email protected] wrote:
> On Tuesday, September 22, 2015 at 11:45:00 AM UTC-7, Lj Fc wrote:
> > you have 10 minutes😂 Good luck!!
> > 
> > 
> > 1. What is PEP8 ?
> > 
> > 2. What are the different ways to distribute some python source code ?
> > 
> > 2 Lists
> > 
> > Let's define the function plural :
> > 
> > def plural(words):
> > plurals = []
> > for word in words:
> >plurals.append(word + 's')
> > return plurals
> > 
> > for word in plural(['cabagge','owl','toy']):
> > print word
> > 
> > Question : How could the code of the function plural be optimised?
> > 
> > 3 Dictionaries
> > 
> > Here are two dictionnaries :
> > 
> > input = {
> > 'foo1': 'bar1',
> > 'chose': 'truc',
> > 'foo2': 'bar2',
> > }
> > output = {
> > 'bar1': 'foo1',
> > 'truc': 'chose',
> > 'bar2': 'foo2'
> > }
> > 
> > Question : Propose a function that returns output when you provide input ?
> > 
> > 4 Iterators
> > 
> > Let's consider this program :
> > 
> > def program_1():
> > yield 1
> > yield 2
> > yield 3
> > 
> > g = program_1()
> > a = list(g)
> > b = list(g)
> > c = g()
> > 
> > Question : At the end of the program,
> > 
> > 1. What is the type of g ?
> > 2. What is the value of a ?
> > 3. What is the value of b ?
> > 4. What is the value of c ?
> > 
> > 5 Decorators
> > 
> > Let's consider now :
> > 
> > def str2print(f):
> > def str2print_wrap(*args, **kwargs):
> > """wrapper"""
> > s = f(*args, **kwargs)
> > print s
> >return str2print_wrap
> > 
> > def hello(s):
> > """ Return "Hello $s" """
> > return "%s %s" % ("Hello", s)
> > 
> > Questions :
> > 
> > 1. Decorate the method 'hello' with 'str2printf' and write the 
> > corresponding code.
> > 2. What is the effect of the decorator on a call to the new method 'hello' ?
> > 3. What is the return value of hello.__doc__
> 
> Pretty sure this guy is asking us to do his homework.  :-P

See Not that Easy Dude...Simple Questions are sometimes the Toughest!! KISS😜
-- 
https://mail.python.org/mailman/listinfo/python-list