Function factory?

2018-10-24 Thread ftg
Hello everyone,

I have 2 functions whose aim is to read a pdf file, the first one manages an 
uploaded file, the another one fecth a remote one (via an url).
They are quite the same:

def handle_uploaded_file(path,file):
#if os.path.isfile(path + '/' + file.name):
#file.name = '_' + file.name

destination = open(path + '/' + file.name, 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
if check_file(path,file.name):
return True
else:
return False

def handle_remote_file(url,path,file_name=''):
if not file_name:
file_name = url.split('/')[-1]

with urllib.request.urlopen(url) as response:
with open(path + '/' + file_name, 'wb+') as out_file:
shutil.copyfileobj(response, out_file)
if check_file(path,file_name):
return True
else:
return False


I am wondering about the way I could rationalize those 2 functions.
I have read about function factory and maybe it crosses my need.

Do you have some advices?

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


Re: Function factory?

2018-10-24 Thread Thomas Jollans
On 2018-10-24 10:58, [email protected] wrote:
> if check_file(path,file_name):
> return True
> else:
> return False

Replace this with a simple

return check_file(path, file_name)

and now your functions share one line, and one that simply calls another
function at that.

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


Re: Function factory?

2018-10-24 Thread MRAB

On 2018-10-24 09:58, [email protected] wrote:

Hello everyone,

I have 2 functions whose aim is to read a pdf file, the first one manages an 
uploaded file, the another one fecth a remote one (via an url).
They are quite the same:

def handle_uploaded_file(path,file):
 #if os.path.isfile(path + '/' + file.name):
 #file.name = '_' + file.name

 destination = open(path + '/' + file.name, 'wb+')
 for chunk in file.chunks():
 destination.write(chunk)
 destination.close()
 if check_file(path,file.name):
 return True
 else:
 return False

def handle_remote_file(url,path,file_name=''):
 if not file_name:
 file_name = url.split('/')[-1]

 with urllib.request.urlopen(url) as response:
 with open(path + '/' + file_name, 'wb+') as out_file:
 shutil.copyfileobj(response, out_file)
 if check_file(path,file_name):
 return True
 else:
 return False


I am wondering about the way I could rationalize those 2 functions.
I have read about function factory and maybe it crosses my need.

Do you have some advices?

Thanks, best regards


Replace:

destination = open(path + '/' + file.name, 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()

with:

with open(path + '/' + file.name, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function factory?

2018-10-24 Thread Peter Otten
[email protected] wrote:

> Hello everyone,
> 
> I have 2 functions whose aim is to read a pdf file, the first one manages
> an uploaded file, the another one fecth a remote one (via an url). They
> are quite the same:
> 
> def handle_uploaded_file(path,file):
> #if os.path.isfile(path + '/' + file.name):
> #file.name = '_' + file.name
> 
> destination = open(path + '/' + file.name, 'wb+')
> for chunk in file.chunks():
> destination.write(chunk)
> destination.close()
> if check_file(path,file.name):
> return True
> else:
> return False
> 
> def handle_remote_file(url,path,file_name=''):
> if not file_name:
> file_name = url.split('/')[-1]
> 
> with urllib.request.urlopen(url) as response:
> with open(path + '/' + file_name, 'wb+') as out_file:
> shutil.copyfileobj(response, out_file)
> if check_file(path,file_name):
> return True
> else:
> return False
> 
> 
> I am wondering about the way I could rationalize those 2 functions.
> I have read about function factory and maybe it crosses my need.
> 
> Do you have some advices?

I don't see how a factory can help here; maybe you can instead factor out 
common code into a helper function? Example:

def handle_uploaded_file(fileobj, destfolder):
return _handle_file(fileobj, destfolder, fileobj.name)

def handle_remote_file(url, destfolder):
with urllib.request.urlopen(url) as response:
return _handle_file(response, destfolder, url)

def _handle_file(instream, destfolder, filename):
destfile = os.path.join(destfolder, posixpath.basename(filename))
with open(destfile, "wb") as outstream:
shutil.copyfileobj(instream, outstream)
return check_file(destfile)


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


Building on windows and installing to prefix

2018-10-24 Thread Patrick Stinson
Hello!

I am having trouble finding out how to build python from source and then 
install it to a path prefix, as you can on unix. I have looked at the options 
in “PCBuild\build.bat -h” and in readme.txt, and on google, but no dice.

I have VS 2017.

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


recommends of redesign OO feature of python !!!

2018-10-24 Thread iamybj--- via Python-list
I am an engineer of java and c#, I want to some personal projects in free time, 
and I choose python.

After try python, I hava some suggestion.

The first thing is that python’s class is not well designed than other 
programming languages. 
Using dictionary as data model is the 20th century style, but now is 21t 
century.We usually use strong typed class to express a data model.
For example,  the code presentation of a person in Java/c++/c# may be:
public class Person {
public String name;
public String email;
public int age;
}
The corresponding Python code:
class Person:
def __init__(self):
self.name = None
self.email = None
self.gage = None
It is very strange to define instance members of a class in constructor.Even 
the OOP feature of PHP is very like Java. Python's dynamic feature has lost 
control.

Second, python is too complex. 
Python is an old programming language.At that time, enterprise programming 
style is very popular, witch like making simple things become complex, so force 
the costumer to pay more money.But now is WWW and Internet and Linux time, 
people like simple production and simple programming styles. Every new 
programming language wants to keep simple.

Third, python is too slow.
At the old enterprise programming time, performance is not a critical feature. 
If software runs too slow, customer have  to pay more money to enterprise 
software company to buy new hardware.
Time changed, performance is very important now. Because the complex of python, 
the pypy project process very slow, and not widely used by people.

Totally speaking, simple and performance are mostly required by this times. I 
suggest the python team should stop any new work, and start to rebuild a new 
python with simple grammar and better performance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: recommends of redesign OO feature of python !!!

2018-10-24 Thread Chris Angelico
On Thu, Oct 25, 2018 at 4:16 PM iamybj--- via Python-list
 wrote:
>
> I am an engineer of java and c#, I want to some personal projects in free 
> time, and I choose python.
>
> After try python, I hava some suggestion.
>
> The first thing is that python’s class is not well designed than other 
> programming languages.
> Using dictionary as data model is the 20th century style, but now is 21t 
> century.We usually use strong typed class to express a data model.
> For example,  the code presentation of a person in Java/c++/c# may be:
> public class Person {
> public String name;
> public String email;
> public int age;
> }
> The corresponding Python code:
> class Person:
> def __init__(self):
> self.name = None
> self.email = None
> self.gage = None
> It is very strange to define instance members of a class in constructor.Even 
> the OOP feature of PHP is very like Java. Python's dynamic feature has lost 
> control.
>
> Second, python is too complex.
> Python is an old programming language.At that time, enterprise programming 
> style is very popular, witch like making simple things become complex, so 
> force the costumer to pay more money.But now is WWW and Internet and Linux 
> time, people like simple production and simple programming styles. Every new 
> programming language wants to keep simple.
>
> Third, python is too slow.
> At the old enterprise programming time, performance is not a critical 
> feature. If software runs too slow, customer have  to pay more money to 
> enterprise software company to buy new hardware.
> Time changed, performance is very important now. Because the complex of 
> python, the pypy project process very slow, and not widely used by people.
>
> Totally speaking, simple and performance are mostly required by this times. I 
> suggest the python team should stop any new work, and start to rebuild a new 
> python with simple grammar and better performance.
>

There's actually a version of Python that's less dynamic, more
restrictive in its class syntax, and (often) higher performance.

It's called C.

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


Re: recommends of redesign OO feature of python !!!

2018-10-24 Thread dieter
iamybj--- via Python-list  writes:
> I am an engineer of java and c#, I want to some personal projects in free 
> time, and I choose python.
>
> After try python, I hava some suggestion.
>
> The first thing is that python’s class is not well designed than other 
> programming languages. 
> Using dictionary as data model is the 20th century style, but now is 21t 
> century.We usually use strong typed class to express a data model.
> For example,  the code presentation of a person in Java/c++/c# may be:
> public class Person {
>   public String name;
>   public String email;
>   public int age;
> }
> The corresponding Python code:
> class Person:
>   def __init__(self):
>   self.name = None
>   self.email = None
>   self.gage = None
> It is very strange to define instance members of a class in constructor.

Nothing prevents you from using

  class Person:
name = None
email = None
gage = None


You can also have typed instance members (with help of some
other packages), e.g.:

  from zope.schema import String, Int
  from zope.schema.fieldproperty import FieldProperty

  class Person:
name = FieldProperty(String(u'Name'))
email = FieldProperty(String(u'Email'))
gage = FieldProperty(Int(u'Gage'))

This gives you even more control than the simple types
you know from other languages. E.g. "gage" could be defined as

gage = FieldProperty(Int(u'Gage', min=0))

To force "gage" to be an integer >= 0.

> ...
> Second, python is too complex. 
I have implemented large projects in assembler, Algol, C, C++, Java and Python.
I achieved the highest productivity as a developper with Python.

In addition, Python is far less complex than e.g. C++. The strong
typing of C++ forces it to define the incredible complex concept "template"
to build general purpose data structure libraries.
Due to its dynamic typing, this is trivial in Python.

My projects typically gain much from the use of multiple inheritance.
This allows to implement single features in so called mixin classes
which can easily be integrated via inheritance in any context
that needs the feature. Something, I dearly miss in Java.

> Python is an old programming language.At that time, enterprise programming 
> style is very popular, witch like making simple things become complex, so 
> force the costumer to pay more money.But now is WWW and Internet and Linux 
> time, people like simple production and simple programming styles. Every new 
> programming language wants to keep simple.

In fact, I am developing internet applications - and I much prefer
Python over Java in this domain.


> Third, python is too slow.

Python can be slow at runtime.
Mostly, it does not matter. If it does, you can implement
performance critical portions in C/C++. The Python C API is
far easier to use than Java's JNI for such tasks. In addition,
"cython" (a Python++ to C compiler) can avoid the direct use
of Python's C API, thus making optimizations even easier.

You could argue that Java's JIT compilers often do quite
a good job in optimizing performance critical code, thus often
avoiding the need to optimize manually. One point for Java.

> ...
> Totally speaking, simple and performance are mostly required by this times. I 
> suggest the python team should stop any new work, and start to rebuild a new 
> python with simple grammar and better performance.

Obviously, you do not like Python. Thus, stay with Java/C++/C#.

I, on the other side, do like Python (far better than C++ and Java)
and I hope your suggestion remains unheard.

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


Re : recommends of redesign OO feature of python !!!

2018-10-24 Thread [email protected]
   If performance is at stake, then Python might not be the best choice.
   When you pick Python, it's for its other qualities.
   This being said, unless you're writing software for a plane, you're not
   very likely to need that much performance... as long as you code well.

   As for your statement that Python is too complex, I'm curious to read what
   you mean exactly.
   Python programs are way shorter than those written in those industrials
   OOP languages, so I'm assuming you're not talking about that.
   One can even compare the formal grammars of Python and say, Java.
   I'll leave it to you, but believe me, Python is way simpler than Java.
   So what complexity are you referring to?
   Maybe you'd prefer Haskell's purity?

   Finallyc regarding your first point, I invite you to check out Python
   3.7's dataclasses.

   -
   Armand FOUCAULT
   Ing**nieur G**nie Logiciel
   [email protected]

    Message original 
   Objet**: recommends of redesign OO feature of python !!!
   De**: iamybj--- via Python-list 
   : [email protected]
   Cc**:

 I am an engineer of java and c#, I want to some personal projects in
 free time, and I choose python.

 After try python, I hava some suggestion.

 The first thing is that python***s class is not well designed than other
 programming languages.
 Using dictionary as data model is the 20th century style, but now is 21t
 century.We usually use strong typed class to express a data model.
 For example, the code presentation of a person in Java/c++/c# may be:
 public class Person {
 public String name;
 public String email;
 public int age;
 }
 The corresponding Python code:
 class Person:
 def __init__(self):
 self.name = None
 self.email = None
 self.gage = None
 It is very strange to define instance members of a class in
 constructor.Even the OOP feature of PHP is very like Java. Python's
 dynamic feature has lost control.

 Second, python is too complex.
 Python is an old programming language.At that time, enterprise
 programming style is very popular, witch like making simple things
 become complex, so force the costumer to pay more money.But now is WWW
 and Internet and Linux time, people like simple production and simple
 programming styles. Every new programming language wants to keep simple.

 Third, python is too slow.
 At the old enterprise programming time, performance is not a critical
 feature. If software runs too slow, customer have to pay more money to
 enterprise software company to buy new hardware.
 Time changed, performance is very important now. Because the complex of
 python, the pypy project process very slow, and not widely used by
 people.

 Totally speaking, simple and performance are mostly required by this
 times. I suggest the python team should stop any new work, and start to
 rebuild a new python with simple grammar and better performance.
 --
 https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list