c code generator from python

2018-01-17 Thread bhattacharya . kushal4
Hi,
Is there any python framework or any tool as  which can generate C code from 
python code as it is .

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


python to C code generator

2018-01-17 Thread kushal bhattacharya
Hi,
Is there any python framework or any tool as  which can generate C code from 
python code as it is .

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


Re: python to C code generator

2018-01-17 Thread David Palao
Hi,
Have a look at Cython.

Best

2018-01-17 12:04 GMT+01:00 kushal bhattacharya :
> Hi,
> Is there any python framework or any tool as  which can generate C code from 
> python code as it is .
>
> Thanks,
> Kushal
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-17 Thread bartc

On 17/01/2018 11:04, kushal bhattacharya wrote:

Hi,
Is there any python framework or any tool as  which can generate C code from 
python code as it is .


What C code would you expect to see from this line of Python:

   a = b + c

?

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


Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread leutrim . kaleci

Hello everyone, 

I am implementing a time-dependent Recommender System which applies BPR 
(Bayesian Personalized Ranking), where Stochastic Gradient Ascent is used to 
learn the parameters of the model. Such that, one iteration involves sampling 
randomly the quadruple (i.e. userID, positive_item, negative_item, epoch_index) 
for n times, where n is the total number of positive feedbacks (i.e. the number 
of ratings given to all items). But, as my implementation takes too much time 
for learning the parameters (since it requires 100 iterations to learn the 
parameters), I was wondering if there is a way to improve my code and speed up 
the learning process of the parameters.

Please find the code of my implementation (the update function named as 
updateFactors is the one that learns the parameters, and such that I guess is 
the one that should be improved in order to speed up the process of learning 
the parameter values) in the following link: 
https://codereview.stackexchange.com/questions/183707/speeding-up-the-implementation-of-stochastic-gradient-ascent-in-python
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread breamoreboy
On Wednesday, January 17, 2018 at 2:30:13 PM UTC, Leo wrote:
> Hello everyone, 
> 
> I am implementing a time-dependent Recommender System which applies BPR 
> (Bayesian Personalized Ranking), where Stochastic Gradient Ascent is used to 
> learn the parameters of the model. Such that, one iteration involves sampling 
> randomly the quadruple (i.e. userID, positive_item, negative_item, 
> epoch_index) for n times, where n is the total number of positive feedbacks 
> (i.e. the number of ratings given to all items). But, as my implementation 
> takes too much time for learning the parameters (since it requires 100 
> iterations to learn the parameters), I was wondering if there is a way to 
> improve my code and speed up the learning process of the parameters.
> 
> Please find the code of my implementation (the update function named as 
> updateFactors is the one that learns the parameters, and such that I guess is 
> the one that should be improved in order to speed up the process of learning 
> the parameter values) in the following link: 
> https://codereview.stackexchange.com/questions/183707/speeding-up-the-implementation-of-stochastic-gradient-ascent-in-python

You have a fair chunk of code so I've only taken a quick glance so this could 
just be the tip of the iceberg.

You're clearly using Python 2 as you have print statements, not functions.  If 
you can upgrade to Python 3 is it is superior, see e.g. 
https://www.youtube.com/watch?v=f_6vDi7ywuA

for i in range(1, len(self.userItems[userID])):

is a code smell, consider using

for userItem in self.userItems[userID]:

if len(pos_per_user[userID]) == 0:

is written

if pos_per_user[userID]:

if userID == None:

should be

if userID is None:

Profile your code with https://docs.python.org/3/library/profile.html to find 
where the hot spots are.

Read https://wiki.python.org/moin/PythonSpeed/PerformanceTips for tips on how 
to up your code performance.

--
Kindest regards.

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


Re: Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread Ned Batchelder

On 1/17/18 9:29 AM, leutrim.kal...@gmail.com wrote:

Hello everyone,

I am implementing a time-dependent Recommender System which applies BPR 
(Bayesian Personalized Ranking), where Stochastic Gradient Ascent is used to 
learn the parameters of the model. Such that, one iteration involves sampling 
randomly the quadruple (i.e. userID, positive_item, negative_item, epoch_index) 
for n times, where n is the total number of positive feedbacks (i.e. the number 
of ratings given to all items). But, as my implementation takes too much time 
for learning the parameters (since it requires 100 iterations to learn the 
parameters), I was wondering if there is a way to improve my code and speed up 
the learning process of the parameters.

Please find the code of my implementation (the update function named as 
updateFactors is the one that learns the parameters, and such that I guess is 
the one that should be improved in order to speed up the process of learning 
the parameter values) in the following link:
https://codereview.stackexchange.com/questions/183707/speeding-up-the-implementation-of-stochastic-gradient-ascent-in-python


It looks like you have lists that you are doing "in" and "remove" 
operations on.  I don't know how long these lists are, but sets will be 
much faster in general.  You'll have to replace random.choice() with 
random.choice(list(...)), since you can't random.choice from a set.  
That might negate the benefits, but it's worth a try.


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


Re: Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread Chris Angelico
On Thu, Jan 18, 2018 at 6:28 AM, Ned Batchelder  wrote:
> You'll have to replace random.choice() with
> random.choice(list(...)), since you can't random.choice from a set.
>

Side point: why can't you? You can random.sample from a set, but
random.choice requires a sequence. It seems perfectly sane to ask for
a random element from a set.

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


Re: Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread Ned Batchelder

On 1/17/18 2:45 PM, Chris Angelico wrote:

On Thu, Jan 18, 2018 at 6:28 AM, Ned Batchelder  wrote:

You'll have to replace random.choice() with
random.choice(list(...)), since you can't random.choice from a set.


Side point: why can't you? You can random.sample from a set, but
random.choice requires a sequence. It seems perfectly sane to ask for
a random element from a set.




You'd have to ask Raymond Hettinger I guess.  random.sample works on a 
set because it starts with:


    if isinstance(population, _Set):
    population = tuple(population)

So sample() is willing to listify (tuplify?) your set for you, but 
choice() is not.


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


Re: Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread Chris Angelico
On Thu, Jan 18, 2018 at 7:19 AM, Ned Batchelder  wrote:
> On 1/17/18 2:45 PM, Chris Angelico wrote:
>>
>> On Thu, Jan 18, 2018 at 6:28 AM, Ned Batchelder 
>> wrote:
>>>
>>> You'll have to replace random.choice() with
>>> random.choice(list(...)), since you can't random.choice from a set.
>>>
>> Side point: why can't you? You can random.sample from a set, but
>> random.choice requires a sequence. It seems perfectly sane to ask for
>> a random element from a set.
>>
>>
>
> You'd have to ask Raymond Hettinger I guess.  random.sample works on a set
> because it starts with:
>
> if isinstance(population, _Set):
> population = tuple(population)
>
> So sample() is willing to listify (tuplify?) your set for you, but choice()
> is not.
>

Ah. So it's just as inefficient. (I should have looked.)

So I guess what we really need is some lower-level support. There's no
way in Python to pick a random element from a set in O(1) time. It
could be done in O(n) time and O(1) space by iterating through the set
until you hit the chosen random index, but you can't just say "grab me
one of those".

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


Why does random.choice() reject sets? was Re: Speeding up the implementation of Stochastic Gradient Ascent in Python

2018-01-17 Thread Peter Otten
Chris Angelico wrote:

> On Thu, Jan 18, 2018 at 6:28 AM, Ned Batchelder 
> wrote:
>> You'll have to replace random.choice() with
>> random.choice(list(...)), since you can't random.choice from a set.
>>
> 
> Side point: why can't you? You can random.sample from a set, 

I'm not sure this was a good idea.

> but
> random.choice requires a sequence. It seems perfectly sane to ask for
> a random element from a set.

$ python3 -m timeit -s 'from random import sample; items = 
list(range(10))' 'sample(items, 5)'
10 loops, best of 3: 18 usec per loop
$ python3 -m timeit -s 'from random import sample; items = 
set(range(10))' 'sample(items, 5)'
100 loops, best of 3: 7.27 msec per loop

You would need access to the set implementation to get reasonable 
performance.

As random.choice() is more likely to be called repeatedly with the same 
argument I'd rather not hide something like

if isinstance(seq, set):
seq = tuple(set)

in its function body.


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


Re: documentation on read.encode

2018-01-17 Thread Larry Martell
On Tue, Jan 16, 2018 at 3:58 PM, MRAB  wrote:
> On 2018-01-16 19:52, Larry Martell wrote:
>>
>> On Tue, Jan 16, 2018 at 2:35 PM, Gene Heskett 
>> wrote:
>>>
>>> On Tuesday 16 January 2018 14:19:38 Larry Martell wrote:
>>>
 On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell
>>>
>>>  wrote:

 > Looking for 2.7 docs on read.encode - googling did not turn up
 > anything.
 >
 > Specifically, looking for the supported options for base64, and how
 > to specify them, e.g. Base64.NO_WRAP

 So I just realized that encode() is not a method of read() it's a
 string method. But I still have the same question - can I pass in any
 flags?

 My issue is that I am base64 encoding PNG images on linux and it's
 putting a LF at the end of each line. If I do the same on Windows it's
 putting CR/LF. I want the files to be encoded with no platform
 dependences. Googling I found mention of Base64.NO_WRAP and I want to
 pass that into encode() - can I do that?
>>>
>>>
>>> Di you not have the manpages installed?
>>>
>>> In my copy of the manpage:
>>> base64 [OPTION]... [FILE]
>>> where option is:
>>>  -w, --wrap=COLS
>>>   wrap encoded lines after COLS character (default 76).  Use
>>> 0 to disable line wrapping.
>>>
>>> Seems pretty simple.
>>
>>
>> But how do I use that in read().encode('base64')?
>>
> Use the base64 module instead, which is also how you would do it in Python
> 3.
>
> If you're getting CR/LF on Windows, that's because you're opening the file
> in text mode. In both Python 2 and Python 3 the base64 string will be a
> bytestring, which you'd write out to a file opened in binary mode.
>
> That's an extra bit of future-proofing! :-)

Thanks. That's what it ended up being. The code that was receiving the
PNG was not reading and writing the file as binary. Strangely that
worked on Linux but not on Windows.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: documentation on read.encode

2018-01-17 Thread Steven D'Aprano
On Wed, 17 Jan 2018 16:54:37 -0500, Larry Martell wrote:

> The code that was receiving the
> PNG was not reading and writing the file as binary. Strangely that
> worked on Linux but not on Windows.

Nothing strange about it -- on Unix and Linux systems (with the possible 
exception of Mac OS?) in Python 2 there's no difference between text and 
binary mode for ASCII-only files. In Python 2, strings are byte strings, 
not Unicode, and reading from files returns such sequences of bytes.

On Windows, reading from files in text mode treats \r\n as the end of 
line, and converts[1] such \r\n pairs to \n; it also treats ^Z byte as 
the end of file[2], or at least it used to back in the 2.5 or so days. I 
haven't tested it in more recent versions.


[1] Technically this is a build-time option, but as far as I know it is 
not just the default but pretty much universal.

[2] https://blogs.msdn.microsoft.com/oldnewthing/20040316-00/?p=40233/




-- 
Steve

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