String encoding in Py2.7

2018-05-29 Thread ftg
Hello,
Using Python 2.7 (will switch to Py3 soon but Before I'd like to understand how 
string encoding worked)
Could you please tell me is I understood well what occurs in Python's mind:
in a .py file:
if I write s="héhéhé", if my file is declared as unicode coding, python will 
store in memory s='hx82hx82hx82'
however this is not yet unicode for python interpreter this is just raw bytes. 
Right? 
By the way, why 'h' is not turned into hexa value? Because it is already in the 
ASCII table?
If I want python interpreter to recognize my string as unicode I have to 
declare it as unicode s=u'héhéhé' and magically python will look for those 
hex values 'x82' in the Unicode table. Still OK?
Now: how come when I declare s='héhéhé', print(s) displays well 'héhéhé'? Is it 
because of my shell windows that is dealing well with unicode? Or is it 
because the print function is magic?

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


Project tree and import module

2018-06-08 Thread ftg
Hello,

here is my project hierarchy:
myproject
 |
 |- django-project-name
 | |- django-project-name
 | | |-urls.py
 | | |-views.py
 | |- manage.py
 |- my-package

Is my projet tree correct? Shouldn't I put my-package into my 
django-project-name folder?
Because I am facing some questionning about how to reach my-package from 
views.py and in the current situation, 
I have to insert a sys.path.append line (with full path) in my views.py file, 
and I don't find this very elegant.
What if my package is really in another folder location? Should I need to use 
the sys.path.append function anyway?

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


Design of my program

2018-06-13 Thread ftg
Hello everyone,

Here is a small picture of my project.
I'd like to fetch several datas from a website representing races results. Each 
line of the result contains rank, runner's name and other attributes of the 
runner (like club etc...).
For now I have created 2 classes (Race and Runner).
Methods of Race allow me to fetch datas on the www thanks to beautifulsoup 
module.I instanciate a race at the beginning of the scan and for each line I 
instanciate a Runner.
I want to store all those information in a database (for both Race and Runner), 
but here is the point, I have the feeling, on a programming elegance point of 
view, that I have to separate methods that fetch from method that store and 
also have a kind of superior object that controls those 2 features.
Moreover fetching will interact with storage: if race is already in database, 
no need to fetch again.
How should I "design" this? Should I have only (despite my first impression) 
one class and all the methods in it? Should I create a parent class along with 
2 childrens that manage the two kind of methods?
Is there a design pattern for this?

I precise I am relatively new in "intelligent" python programming ;)

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


Re: Design of my project

2018-06-14 Thread ftg
Hello,

thanks for this nice answer I didn't see at first (I have parallely asked again 
in the list, sorry about the inconvenience).
I will read deeply your proposal I will come bakc with my quesitons if any ;)

Thanks!

June 14 2018 12:37 AM, "Cameron Simpson"  wrote:
> On 13Jun2018 15:23, Fabien LUCE  wrote:
> 
>> Here is a small picture of my project.
>> I'd like to fetch several datas from a website representing races results.
>> Each line of the result contains rank, runner's name and other attributes
>> of the runner (like club etc...).
>> For now I have created 2 classes (Race and Runner).
>> Methods of Race allow me to fetch datas on the www thanks to beautifulsoup
>> module.I instanciate a race at the beginning of the scan and for each line
>> I instanciate a Runner.
>> I want to store all those information in a database (for both Race and
>> Runner), but here is the point, I have the feeling, on a programming
>> elegance point of view, that I have to separate methods that fetch from
>> method that store and also have a kind of superior object that controls
>> those 2 features.
>> Moreover fetching will interact with storage: if race is already in
>> database, no need to fetch again.
>> How should I "design" this? Should I have only (despite my first
>> impression) one class and all the methods in it? Should I create a parent
>> class along with 2 childrens that manage the two kind of methods?
>> Is there a design pattern for this?
> 
> My inclination would be to start by providing a class which "wraps" your Race 
> database table, and
> presents it as a mapping.
> 
> Something along the lines of:
> 
> class RaceTable:
> def __init__(self, database_connection, table_name):
> self.conn = database_connection
> self.table_name = table_name
> def __contains__(self, race_id):
> ... do SQL to see if the race_id is already present, return True or False
> def __getitem__(self, race_id):
> ... do SQL to fetch a Race row from the table and return it ...
> def __setitem__(self, race_id, race_info):
> ... do SQL to store race_info in the table ...
> 
> The special __foo__ methods (called "dunder" methods in the Puython world 
> because of the "double
> underscore") are what make the class look like a Python "dict" from outside: 
> when you perform a
> mapping method like "value in x" or "x[key] = value", Python calls 
> x.__contains__ or x.__setitem__
> for you.
> 
> Look up "Emulating Container Types" here:
> 
> https://docs.python.org/3/reference/datamodel.html#emulating-container-types
> 
> That will show you how to write a class like the above example to implement a 
> "maping" interface.
> You don't need to implement everything, just what you need. The example above 
> only implements 3
> methods.
> 
> Then your outer code can look a bit like:
> 
> race_store = RaceTable(database_connection, 'race_table_name_here')
> ...
> if race_id in race_store:
> ... mention that this race_id is already stored ...
> else:
> race_store[race_id] = race_info
> 
> So the objective here is a simple class that makes it easy to talk about 
> where your information is
> stored in nice Pythonic terms, and which hides the details of database access 
> - that lets you talk
> about the data in a sensbile and clear way outside the storage class, and 
> also leaves scrope for
> changing the storage mechanism later without changing the main code (eg from 
> a conventional SQL
> database to some other service).
> 
> Cheers,
> Cameron Simpson 
> -- https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Design of my program

2018-06-14 Thread ftg
Thanks for your answer.

The term "picture" was more a metaphor ;)
I am not sure I want to dive into ORM, since I am newbie, maybe I want to code 
something more from scratch to understand well program designing.
What would be the best way:
- create 2 classes, one that deals with web scraping, the other one with 
database, and then create child from those 2 parents?
- create one unique class dealing with all those things
- create one first class that deals with DB things and then a child class that 
deals additionnaly with web scraping.

What would be the ideal way?

Thanks  

June 13 2018 6:50 PM, "Dennis Lee Bieber"  wrote:
> On Wed, 13 Jun 2018 13:09:10 +, [email protected] declaimed the following:
> 
>> Hello everyone,
>> 
>> Here is a small picture of my project.
> 
> This is a text-only group. Any binary attachments are removed before
> messages propagate.
> 
> As for the rest of your post... Possibly look at SQLAlchemy
> https://www.sqlalchemy.org or (if it is still in development -- doesn't
> seem to have moved since 2013) DABO https://dabodev.com (
> https://github.com/dabodev/dabo shows some action two years ago)
> 
> --
> Wulfraed Dennis Lee Bieber AF6VN
> [email protected] HTTP://wlfraed.home.netcom.com/
> 
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


My environment doesn't load

2018-10-09 Thread ftg
Hello,

I have rsynced a folder on my distant server. Now I try to source my 
environment: source ./env/bin/activate and nothing happens.
When type > which python, /usr/bin/python is still printed. What could be the 
issue?

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


Re: My environment doesn't load

2018-10-09 Thread ftg
Thx,

initially the env was created with virtualenv yes, and then I copied the folder 
of the project on a remote server. I am using zsh (both locally and remotely).
My PATH env variable does includes well the path of my env bin. In this folder 
there are symbolic links.
And yes there is well an /env/bin/python in there:
drwxrwxr-x 3 ftg ftg4096 Jul  6 21:38 .
drwxrwxr-x 6 ftg ftg4096 May 29 20:33 ..
-rw-rw-r-- 1 ftg ftg2114 May 29 20:30 activate  

   
-rw-rw-r-- 1 ftg ftg1056 May 29 20:30 activate.csh  

   
-rw-rw-r-- 1 ftg ftg2210 May 29 20:30 activate.fish 

   
-rw-rw-r-- 1 ftg ftg1137 May 29 20:30 activate_this.py  

   
-rwxrwxr-x 1 ftg ftg 276 Jul  6 21:38 chardetect
-rwxrwxr-x 1 ftg ftg 318 May 29 20:33 django-admin
-rwxrwxr-x 1 ftg ftg 176 May 29 20:33 django-admin.py
-rwxrwxr-x 1 ftg ftg9267 Jul  6 21:38 dumppdf.py
-rwxrwxr-x 1 ftg ftg 285 May 29 20:30 easy_install
-rwxrwxr-x 1 ftg ftg 285 May 29 20:30 easy_install-3.5
-rwxrwxr-x 1 ftg ftg2438 Jul  6 21:38 latin2ascii.py
-rwxrwxr-x 1 ftg ftg5970 Jul  6 21:38 pdf2txt.py
-rwxrwxr-x 1 ftg ftg 267 May 29 20:30 pip
-rwxrwxr-x 1 ftg ftg 267 May 29 20:30 pip3
-rwxrwxr-x 1 ftg ftg 267 May 29 20:30 pip3.5
drwxrwxr-x 2 ftg ftg4096 Jul  6 21:38 __pycache__
lrwxrwxrwx 1 ftg ftg   7 May 29 20:30 python -> python3
-rwxrwxr-x 1 ftg ftg 4743656 May 29 20:30 python3
lrwxrwxrwx 1 ftg ftg   7 May 29 20:30 python3.5 -> python3
-rwxrwxr-x 1 ftg ftg2373 May 29 20:30 python-config
-rwxrwxr-x 1 ftg ftg 264 May 29 20:30 wheel

So I guess that copying an environment is not "creating" it remotely.
However it works in a certain point of view because apache can find it an use 
it as python-path is set for my WSGI Daemon Process. 
DOn't understand...


October 9 2018 10:18 AM, "Thomas Jollans"  wrote:
> On 09/10/2018 09:20, [email protected] wrote:
> 
>> Hello,
>>> I have rsynced a folder on my distant server. Now I try to source my 
>>> environment: source
>> ./env/bin/activate and nothing happens.
>> When type > which python, /usr/bin/python is still printed. What could be 
>> the issue?
>>> Thanks
> 
> What shell are you using? Was the environment created by virtualenv, venv or 
> something else? After
> sourcing activate, what is your PATH? Is there a ./env/bin/python? Does it 
> work?
> 
> (The hopefully easy fix is obviously to just recreate the environment locally 
> from your
> requirements.txt, assuming you have one)
> -- https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My environment doesn't load

2018-10-09 Thread ftg
Here are the ouput during sourcing:

> [ftg @ localhost] [/var/www/ffablob]
> % source env-p3/bin/activate 
> [ftg @ localhost] [/var/www/ffablob]
> % which python 
> /usr/bin/python

and if I run some of my code that import beautyfulsoup it fails (python 
mycode.py), althoug running
directly ./env-p3/python3.5 mycode.py is working...
> 
> October 9 2018 11:06 AM, "Thomas Jollans"  wrote:
> 
>> On 2018-10-09 10:36, [email protected] wrote:
>> 
>>> Thx,
>>> 
>>> initially the env was created with virtualenv yes, and then I copied the 
>>> folder of the project on a
>>> remote server. I am using zsh (both locally and remotely).
>>> My PATH env variable does includes well the path of my env bin. In this 
>>> folder there are symbolic
>>> links.
>>> And yes there is well an /env/bin/python in there:
>> 
>> But can you run it?
>> 
>> If it exists, and it's at the top of your PATH, then everything should
>> be fine unless your shell's command hash table is getting in the way -
>> which it shouldn't. virtualenv knows about those:
>> 
>> % tail -6 bin/activate
>> # This should detect bash and zsh, which have a hash command that must
>> # be called to get it to forget past commands. Without forgetting
>> # past commands the $PATH changes we made may not be respected
>> if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
>> hash -r 2>/dev/null
>> fi
>> 
>>> drwxrwxr-x 3 ftg ftg 4096 Jul 6 21:38 .
>>> drwxrwxr-x 6 ftg ftg 4096 May 29 20:33 ..
>>> -rw-rw-r-- 1 ftg ftg 2114 May 29 20:30 activate
>>> -rw-rw-r-- 1 ftg ftg 1056 May 29 20:30 activate.csh
>>> -rw-rw-r-- 1 ftg ftg 2210 May 29 20:30 activate.fish
>>> -rw-rw-r-- 1 ftg ftg 1137 May 29 20:30 activate_this.py
>>> -rwxrwxr-x 1 ftg ftg 276 Jul 6 21:38 chardetect
>>> -rwxrwxr-x 1 ftg ftg 318 May 29 20:33 django-admin
>>> -rwxrwxr-x 1 ftg ftg 176 May 29 20:33 django-admin.py
>>> -rwxrwxr-x 1 ftg ftg 9267 Jul 6 21:38 dumppdf.py
>>> -rwxrwxr-x 1 ftg ftg 285 May 29 20:30 easy_install
>>> -rwxrwxr-x 1 ftg ftg 285 May 29 20:30 easy_install-3.5
>>> -rwxrwxr-x 1 ftg ftg 2438 Jul 6 21:38 latin2ascii.py
>>> -rwxrwxr-x 1 ftg ftg 5970 Jul 6 21:38 pdf2txt.py
>>> -rwxrwxr-x 1 ftg ftg 267 May 29 20:30 pip
>>> -rwxrwxr-x 1 ftg ftg 267 May 29 20:30 pip3
>>> -rwxrwxr-x 1 ftg ftg 267 May 29 20:30 pip3.5
>>> drwxrwxr-x 2 ftg ftg 4096 Jul 6 21:38 __pycache__
>>> lrwxrwxrwx 1 ftg ftg 7 May 29 20:30 python -> python3
>>> -rwxrwxr-x 1 ftg ftg 4743656 May 29 20:30 python3
>>> lrwxrwxrwx 1 ftg ftg 7 May 29 20:30 python3.5 -> python3
>>> -rwxrwxr-x 1 ftg ftg 2373 May 29 20:30 python-config
>>> -rwxrwxr-x 1 ftg ftg 264 May 29 20:30 wheel
>>> 
>>> So I guess that copying an environment is not "creating" it remotely.
>>> However it works in a certain point of view because apache can find it an 
>>> use it as python-path is
>>> set for my WSGI Daemon Process.
>>> DOn't understand...
>>> 
>>> October 9 2018 10:18 AM, "Thomas Jollans"  wrote:
>>>> On 09/10/2018 09:20, [email protected] wrote:
>>>> 
>>>>> Hello,
>>>>>> I have rsynced a folder on my distant server. Now I try to source my 
>>>>>> environment: source
>>>>> 
>>>>> ./env/bin/activate and nothing happens.
>>>>> When type > which python, /usr/bin/python is still printed. What could be 
>>>>> the issue?
>>>>>> Thanks
>>>> 
>>>> What shell are you using? Was the environment created by virtualenv, venv 
>>>> or something else? After
>>>> sourcing activate, what is your PATH? Is there a ./env/bin/python? Does it 
>>>> work?
>>>> 
>>>> (The hopefully easy fix is obviously to just recreate the environment 
>>>> locally from your
>>>> requirements.txt, assuming you have one)
>>>> -- https://mail.python.org/mailman/listinfo/python-list
>> 
>> --
>> Thomas Jollans
>> 
>> m ☎ +31 (6) 42630259
>> e ✉ [email protected]
>> --
>> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My environment doesn't load

2018-10-09 Thread ftg
I rsynced the full local project directory (which contains the virtualenv dir) 
to a remote server.
And there I couldn't enter into it.
However, obviously apache2 achieved to...


> October 9 2018 5:21 PM, "Chris Warrick"  wrote:
> 
>> On Tue, 9 Oct 2018 at 11:18,  wrote:
>> 
>>> Here are the ouput during sourcing:
>>> 
>>>> [ftg @ localhost] [/var/www/ffablob]
>>>> % source env-p3/bin/activate
>>>> [ftg @ localhost] [/var/www/ffablob]
>>>> % which python
>>>> /usr/bin/python
>>> 
>>> and if I run some of my code that import beautyfulsoup it fails (python 
>>> mycode.py), althoug running
>>> directly ./env-p3/python3.5 mycode.py is working...
>> 
>> Was the virtualenv copied between machines or directories? If yes: you
>> can’t do that, you must create a new virtualenv in the desired
>> location and install dependencies to it.
>> 
>> --
>> Chris Warrick <https://chriswarrick.com>
>> PGP: 5EAAEA16
>> --
>> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


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