Re: [Tutor] Python code

2016-11-25 Thread Juan C.
On Thu, Nov 24, 2016 at 9:14 PM, urfa jamil  wrote:
>
> I need help to write a code for this problem.
>
> Please help
>
>
> Ask the user to enter a series of numbers. Stop reading numbers when they
enter a negative number. Calculate the average of the numbers given not
including the final negative number.
>
> Question 9 options:
>
>
>
> With best regards
> urfa jamil
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

It seems to me that you're just passing on to us YOUR exercise, this way
you won't learn a thing. Do you at least have any initial code that you
tried doing yourself?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help on Software Design decisions

2016-11-28 Thread Juan C.
I'm a student and my university uses Moodle as their learning management
system (LMS). They don't have Moodle Web Services enabled and won't be
enabling it anytime soon, at least for students. The university programs
have the following structure, for example:

1. Bachelor's Degree in Computer Science (duration: 8 semesters)

1.1. Unit 01: Mathematics Fundamental (duration: 1 semester)
1.1.1. Algebra I (first 3 months)
1.1.2. Algebra II (first 3 months)
1.1.3. Calculus I (last 3 months)
1.1.4. Calculus II (last 3 months)
1.1.5. Unit Project (throughout the semester)

1.2. Unit 02: Programming (duration: 1 semester)
1.2.1. Programming Logic (first 3 months)
1.2.2. Data Modelling with UML (first 3 months)
1.2.3. Python I (last 3 months)
1.2.4. Python II (last 3 months)
1.2.5. Unit Project (throughout the semester)

Each course/project have a bunch of assignments + one final assignment.
This goes on, totalizing 8 (eight) units, which will make up for a 4-year
program. I have to build my own Moodle API to be consumed by my program,
currently I'm using 'requests' + 'bs4' to do the job. I'm not here for
coding help, instead I have some python software design doubts. Some
information I have in mind:

- I was thinking about having the following classes: Program, Unit, Course,
Assignment, User.
- User would handle auth + session, the rest is pretty straightforward,
program would handle programs, unit would handles units, and so on.
- Inside Program init there would be attributes title, id and units; inside
Unit init there would be attributes title, id and courses; inside Course
init there would be attributes title, id, due_date, submitted, grade.

Should I call the site using requests and do all the parsing for everything
using bs4 as soon as I create a new instance of User? For example:

user = User('john.smith', 'password')  # will get all data needed
user.program  # 

cs = user.program
cs.units  # , , etc.

prog = cs.units[1]
prog.assignments  # , , , , [...], 

as03 = cs.assignments[2]
as03.title  # ''Assignment 03"
as03.id  # 202
as03.due_date  # 2016-11-30T21:21:00+00:00 (ISO 8601)

Is this a good Pythonic design? If not, how could it be better?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on Software Design decisions

2016-11-29 Thread Juan C.
On Mon, Nov 28, 2016 at 11:33 PM, Alan Gauld via Tutor 
wrote:
>
> On 28/11/16 21:53, Juan C. wrote:
> > I'm a student and my university uses Moodle as their learning management
> > system (LMS).
>
> Never heard of it but hopefully that doesn't matter :-)
>
> > 1. Bachelor's Degree in Computer Science (duration: 8 semesters)
> >
> > 1.1. Unit 01: Mathematics Fundamental (duration: 1 semester)
> > 1.1.1. Algebra I (first 3 months)
> > 1.2. Unit 02: Programming (duration: 1 semester)
> > 1.2.1. Programming Logic (first 3 months)
> >
> > Each course/project have a bunch of assignments + one final assignment.
> > This goes on, totalizing 8 (eight) units, which will make up for a
4-year
> > program.
>
> > I have to build my own Moodle API to be consumed by my program,
>
> Do you have any requirements defined?
> APIs exist to do something, what operations does your API enable?
> Until you know that you can't design anything very useful.

Well, basically get program information like title, id and units, within
units get title, id and courses, within courses get title, id, assignments,
and within assignments get title, id, due date and grade. I'm working on a
program to automate the process of getting assignments data and putting
them on Trello and keep them up-to-date. (I'm using a Trello API from PyPi,
but that doesn't matter for my script)

>
> > - I was thinking about having the following classes: Program, Unit,
Course,
> > Assignment, User.
> > - User would handle auth + session, the rest is pretty straightforward,
> > program would handle programs, unit would handles units, and so on.
>
> OK, Have you heard of CRC cards? These might be helpful here in
> helping you understand and express what each of these classes does.
> "pretty straightforward" is not helpful in designing a program, you
> need to give more thought to what the program actually does.
> And that means what each class is responsible for.

No, never heard about it...

>
> > - Inside Program init there would be attributes title, id and units;
inside
> > Unit init there would be attributes title, id and courses; inside Course
> > init there would be attributes title, id, due_date, submitted, grade.
>
> The attributes are there to support the operations. What are the
> operations (ie the responsibilities) of the classes. That will
> tell you what attributes you need to store, which to derive
> and which are references to other (user defined) objects.
> And which turn out not to be needed at all...

Those classes won't have any methods right now, just atributes (title, id,
due date, etc), their job is mainly hold data from Moodle in a convenient
way, so I can use it whenever I like.

>
> > Should I call the site using requests and do all the parsing for
everything
> > using bs4 as soon as I create a new instance of User? For example:
>
> My personal opinion here is that objects should be
> constructed into discrete attributes as quickly as possible so
> I'd vote yes. Get the parsing done early. You can always define
> a method to reconstruct the string if that proves necessary.
>
> BTW you are talking about calling the site but your API (as I understand
> the assignment) is on the server side so have you
> thought about how those requests will be received and
> dispatched? Do you need more classes to deal with that?
> Or is that something Moodle can do for you?
>
> > user = User('john.smith', 'password')  # will get all data needed
> > user.program  # 
>
> I'm not sure what you are trying to show us here.
> The first line is a valid assignment statement but you are passing
> in valid data, no parsing required. The second line does nothing
> useful.

Well, I'm trying to illustrate how the program would work. The parse would
occur inside User, there I would call the site using requests and scrap
data from it using bs4. I would get the username and password provided and
use requests + bs4 inside User to login and keep session. The second line,
and all of them for that matter, are just illustration. I already have a
working version of the script, I'm just fixing some bugs and I will post it
here.

>
> > Is this a good Pythonic design? If not, how could it be better?
>
> Mainly you are asking about general OOP/OOD rather than
> anything Pythonic. That is not a bad thing, just a different
> question.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on Software Design decisions

2016-11-29 Thread Juan C.
On Tue, Nov 29, 2016 at 7:12 AM, Alan Gauld via Tutor 
wrote:
> I just noticed the last bit.
> Is this a client side API or a server side API?
> In other words are you building a set of services on the
> server or are you building a module that makes it easy
> for client side programs to access the server? I had
> assumed the first but your responses make me think it
> is probably the second.

Client side. I'm building a python package to make it easier for my
programs to get data from Moodle.

> OK, In that case you possibly want to call your class
> Users since you seem to intend to fetch all User objects at once?
> Or is the User ID doing double duty as a security token for
> the server and as a key into the data? Or should it look like:

I just need username and password to login, nothing else. I'm passing
'program_id' as an argument to make it easier for me, but I intend to
automatically get it in the future.


Here's my working code:

package moodle/

user.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from .program import Program
import requests


class User:
   _AUTH_URL = 'http://lms.university.edu/moodle/login/index.php'

   def __init__(self, username, password, program_id):
  self.username = username
  self.password = password
  session = requests.session()
  session.post(self._AUTH_URL, {"username": username, "password":
password})
  self.program = Program(program_id=program_id, session=session)

   def __str__(self):
  return self.username + ':' + self.password

   def __repr__(self):
  return '' % self.username

   def __eq__(self, other):
  if isinstance(other, self):
 return self.username == other.username
  else:
 return False

==

program.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from .unit import Unit
from bs4 import BeautifulSoup


class Program:
   _PATH = 'http://lms.university.edu/moodle/course/index.php?categoryid='

   def __init__(self, program_id, session):
  response = session.get(self._PATH + str(program_id))
  soup = BeautifulSoup(response.text, 'html.parser')

  self.name = soup.find('ul',
class_='breadcrumb').find_all('li')[-2].text.replace('/', '').strip()
  self.id = program_id
  self.units = [Unit(int(item['data-categoryid']), session) for item in
soup.find_all('div', {'class': 'category'})]

   def __str__(self):
  return self.name

   def __repr__(self):
  return '' % (self.name, self.id)

   def __eq__(self, other):
  if isinstance(other, self):
 return self.id == other.id
  else:
 return False

==

unit.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from .course import Course
from bs4 import BeautifulSoup


class Unit:
   _PATH = 'http://lms.university.edu/moodle/course/index.php?categoryid='

   def __init__(self, unit_id, session):
  response = session.get(self._PATH + str(unit_id))
  soup = BeautifulSoup(response.text, 'html.parser')

  self.name = soup.find('ul',
class_='breadcrumb').find_all('li')[-1].text.replace('/', '').strip()
  self.id = unit_id
  self.courses = [Course(int(item['data-courseid']), session) for item
in soup.find_all('div', {'class': 'coursebox'})]

   def __str__(self):
  return self.name

   def __repr__(self):
  return '' % (self.name, self.id)

   def __eq__(self, other):
  if isinstance(other, self):
 return self.id == other.id
  else:
 return False

==

course.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from .assignment import Assignment
import re
from bs4 import BeautifulSoup


class Course:
   _PATH = 'http://lms.university.edu/moodle/course/view.php?id='

   def __init__(self, course_id, session):
  response = session.get(self._PATH + str(course_id))
  soup = BeautifulSoup(response.text, 'html.parser')

  self.name = soup.find('h1').text
  self.id = course_id
  self.assignments = [Assignment(int(item['href'].split('id=')[-1]),
session) for item in
 soup.find_all('a', href=re.compile(r'http://lms
\.university\.edu/moodle/mod/assign/view.php\?id=.*'))]

   def __str__(self):
  return self.name

   def __repr__(self):
  return '' % (self.name, self.id)

   def __eq__(self, other):
  if isinstance(other, self):
 return self.id == other.id
  else:
 return False

==

assignment.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup


class Assignment:
   _PATH = 'http://lms.university.edu/moodle/mod/assign/view.php?id='

   def __init__(self, assignment_id, session):
  response = session.get(self._PATH + str(assignment_id))
  soup = BeautifulSoup(response.text, 'html.parser')

  self.name = soup.find('h2').text
  self.id = assignment_id

   def __str__(self):
  return self.name

   def __repr__(self):
  return '' % (self.name, self.id)

   def __eq__(self, other):
  if isinstance(other, self):
 return self.id =

Re: [Tutor] Ran into a problem: Tried many different methods

2016-11-30 Thread Juan C.
On Wed, Nov 30, 2016 at 12:29 AM, Parish Watteau 
wrote:
> A program that will read each player’s name and golf score as
> keyboard input, and then save these as records in a file named golf.txt.
> (Each record will have a field for the player’s name and a field for the
> player’s score.)

I believe you are totally covered by input() and open(). I suggest you to
write data like a csv inside golf.txt to make it easier for write and read,
like that:

name,score
bob donovan,50
mike smith,43
ana lyn,39

> I attempted to solve it but ran into some errors from using some methods
> from a different python version but it didn't turn out the right way. Now
I
> am stuck without a program and no where to start. Any help would be
> appreciated

Well, it would be better if you provided your old script and error
traceback, so we can see what you were thinking. Anyway, a good start would
be to first create the logic to keep getting data from user input until the
user tell you "I'm done", after you got it done you can go and start
thinking about writing to file. To tell you the truth that's a quite easy
script to do, I won't be posting the code here already because it would
make no sense. Hope you got the general idea and can start your code.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2016-12-14 Thread Juan C.
On Dec 10, 2016 12:15 PM, "Tetteh, Isaac - SDSU Student"
> isaac.tet...@jacks.sdstate.edu> wrote:
> >
> > Hello,
> >
> > I am trying to find the number of times a word occurs on a webpage so I
> used bs4 code below
> >
> > Let assume html contains the "html code"
> > soup = BeautifulSoup(html, "html.parser")
> > print(len(soup.fi >nd_all(string=["Engineering","engineering"])))
> > But the result is different from when i use control + f on my keyboard
to
> find
> >
> > Please help me understand why it's different results. Thanks
> > I am using Python 3.5
> >

Well, depending on the word you're looking for it's pretty possible that
when you execute your code it finds matches inside javascript functions,
html/js comments and so on because you're doing a search against the actual
html file. If you execute a simple CRTL+F using a web browser it will just
look for "visual info" and won't be looking into the actual code. For
example, if we go to https://www.python.org/psf/ and do a CRTL+F and search
for "Upgrade to a different browser" we will find zero results, on the
other hand if we do this inside the view-source we will find one result,
because this sentence is inside a commented line.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Created Function, Need Argument to be a String

2016-12-17 Thread Juan C.
On Mon, Dec 12, 2016 at 2:29 PM, Bryon Adams  wrote:
> Is there a way to force my argument to always be a string before entering
> the function?

You could do the following:

1. Use `def ip_checker(ip_address: str):` to make it more clear that
you're expecting a str, but remember, this is just a "hint", it
doesn't enforce anything. (Read more at
https://docs.python.org/3/library/typing.html#module-typing)
2. You could also write something like that:

valid_ip = "192.168.0.1"
invalid_ip = 10


def ip_check(ip_addr: str):
  if type(ip_addr) is not str:
raise TypeError('The IP address should be a string.')  #
https://docs.python.org/3/library/exceptions.html#TypeError

  # continue your function...
  print(ip_addr, 'is valid!')


ip_check(valid_ip)  # 192.168.0.1 is valid!
ip_check(invalid_ip)  # TypeError: The IP address should be a string.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using venv

2017-02-04 Thread Juan C.
On Fri, Jan 27, 2017 at 7:47 PM, Jim  wrote:
>
> [...] This question seems a little dumb and maybe I am being a little dense, 
> but then what?

Imagine that you are working on 5 different Python projects, each
using different packages with different versions. We can break this
down in two situations:

1. No virtualenv
1.a. You only have one Lib/site-packages to hold all your packages
1.b. Project A uses package-abc 1.2 while Project B uses package-abc 1.5
1.c. You go to work on Project B using package-abc 1.5, but later on
you have to do some changes on Project A so you must downgrade
package-abc to 1.2. Too bad you have to fix some bugs on Project B the
next day, need to update package-abc back to 1.5

2. Virtualenv
2.a. You had multiple virtualenvs, each of them having their own
Lib/site-packages
2.b. Project A uses one virtualenv with package-abc 1.2 while Project
B uses another virtualenv with package-abc 1.5
2.c. You go to work on Project B using package-abc 1.5, so you
activate its venv, but later on you have to do some changes on Project
A so you activate the other venv. Too bad you have to fix some bugs on
Project B the next day, activate Project B venv.

It's a simple command to activate/deactivate a venv vs all the work
you have to do when not using venv.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python 3 simple socket issue

2015-02-18 Thread Juan C.
Code:

# !/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket

def main():
target_host = 'www.google.com'
target_port = 80

client = socket.socket()
client.connect((target_host, target_port))
client.send(b"GET HTTP/1.1\r\nHost:google.com\r\n\r\n")

response = client.recv(4096)
print(response)


Output:

C:\Python34\python.exe D:/Documents/PyCharm/Test/__main__.py
b'HTTP/1.0 403 Forbidden\r\nContent-Length: 1180\r\nContent-Type:
text/html; charset=UTF-8\r\nDate: Wed, 18 Feb 2015 15:43:16 GMT\r\nServer:
GFE/2.0\r\nAlternate-Protocol: 80:quic,p=0.08\r\n\r\nSorry... body { font-family: verdana,
arial, sans-serif; background-color: #fff; color: #000;
}GoogleSorry...We\'re sorry.. but your computer or network may be
sending automated queries. To protect our users, we can\'t process your
request right now.See https://support.google.com/websearch/answer/86640";>Google Help for more
information.https://www.google.com";>Google
Home'

Process finished with exit code 0

Why a I getting 403? The code seems fine.

Python 3.4.2
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3 simple socket issue

2015-02-18 Thread Juan C.
The code is fine, the email should have ruined the indentation. Anyway,
indeed, I only saw the 403 error and didn't even read the rest as I thought
it was only HTML/CSS code, my bad.

Tried with other pages and everything is working, thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python OO

2015-03-28 Thread Juan C.
So, I'm trying to create a little script here using OO to learn it, and I
got some questions:

The script uses the Movie DB API, and I have a Series module.

In this module I have 3 classes, 'Serie', 'Season' and 'Episode'. In
__main__.py I will instantiate a serie like so 'some_random_name =
Serie(123)', where 123 is the ID of the serie in the API.

Serie class: title, year, length (X seasons), serieid and a function
get_season(self, number)

Season class: number, year, length (X episodes) and a function
get_episode(self, number)

Episode class: title, number, date and two functions, get_character(self,
index) and crew_size()

What would be the best approach using OO? Should I use class inheritance
and how? What would be the best way to create the whole Serie instance with
the least API calls possible?

Remember that when I create a new Serie(serie_id) I create the whole thing,
every season, every episode.

Many thanks.

PS: If you guys know a good book/course/tutorial about Python OO feel free
to post it here.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-28 Thread Juan C.
On Sat, Mar 28, 2015 at 5:23 PM Alan Gauld 
wrote:

> That looks like a data oriented class, which may not be a bad thing but
you should also be thinking about the methods. What will a series object
do in the context of your application? How will those data attributes help
the methods do their job?

My app doesn't do anything specific. It's just for the sake of doing
something. Maybe, I just create a serie instance and print all the info
about it like title, year of launch, numbers of seasons, go through each
season and post each episode info like title, air date, summary, things
like that.


> Also is date really an attribute of the series? Or is it a derived
feature based on the seasons? And maybe should be a range, or a
tuple of values?

The date thing is a little different in each case, Serie will have only the
year, let's say "Breaking Bad" would be 2008. Season would have year +
month. Episode would have full date, year + month + day.


> The "function" - do you mean method? - get_season() should probably
be a private method and maybe called by __init__()? (I'm assuming
this is the thing that talks to the MDB API?

Yes, I mean method. Yeah, forget about get_season(), I was thinking a
little "Java" there. The best thing to do would be having a self.seasons
just like every else (self.title, self.year and so on) list of 'Season'
instances and then I call it directly.


> Much as before. Is the year an attribute or derived from the
episodes? Can a series be shown more than once? Does it have
multiple dates in that case?

Year is an attribute of season. Not multiple dates, it will have the year +
month of the first episode of the season.

> This is the core data, most of the higher level stuff can be derived
from this. And it has two methods (not functions). Or are they also just
calls to the MDB DB? Or are they methods that your users will want to use?
Itsnot clear.

I don't want to create a bunch of "get" methods, I read in many places that
Python isn't Java, you don't need "getters/setters" you can pretty much
call the 'self.var' directly.

Again, I was thinking 'Java' there, I don't need a 'get_character' method,
I can have a list on my __init__ and call it directly, same goes for
crew_size, I just use len() on characters.


> I don't know the API so I can't answer that.

The API: http://docs.themoviedb.apiary.io/#reference/tv/tvid/get

It returns the data in JSON and I use requests 'json' method to read the
data.


> Do you have to? Why not fetch the seasons and episodes on demand?

Well, let's say I create a new instance, 'bb = Serie(1396)  # Breaking
Bad', won't I need to have my 'self.seasons' list ready, because I can call
it anytime? And the same logic applies to the 'Season' class, I need to
have the 'self.episodes' ready.

To call on demand I will need to change to code and indead create methods
like 'get_season' and 'get_episode'.

Maybe I'm a little confuse and can't really express my logic here.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-28 Thread Juan C.
Ok, so, let me try to express what I think is 'right' here according to
what you said.

My code structure needs to be something like that:

pycinema
- package: pycinema
- - __init__.py
- - api.py
- - actor.py
- - movie.py
- - serie.py
- __main__.py

And why I use it this way?

1. You said that I need to separate the API methods and calls from my code,
so that if I need to move to another API or something like that, all the
API code and logic will be in one place

2. I've read in many places that a basic structure, package with all the
logic and a single __main__.py on the folder root, is good and simple.


Let's start with something more simple, the actor.py module. Currently I
already have this working code (http://pastebin.com/wcCnCwMc) for this
module.

How would be a good way to "convert" this API call to my api.py?

Should I create an API class and have a bunch of methods like "get_actor",
"get_movie", "get_serie", "get_season" and "get_episode"?

In this code I'm only getting the name of the actor, but I can get other
things from the API like birthday, deathday (when applicable),
place_of_birth, and so on.

Should my api.py get only the RAW data, in this case the JSON, and give it
to my classes (Actor, Movie, Serie) and they (the classes) read this JSON
and do what they need?


I'll be finishing this message here so as to not make it so long. But I
still have some "code design" doubts. But let's resolve it in parts.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-28 Thread Juan C.
On Sat, Mar 28, 2015 at 10:26 PM Mark Lawrence 
wrote:
If your classes are small put them in one source file, which is clearly
simpler than your proposed structure. Why over-engineer something if
there is no need to?

Well, my classes won't be that small, and separating them by modules would
be easier to maintain, and after all, it's only 4 modules, not 10-15.

My actor related methods should be set apart from my movie, serie related
ones, don't you agree?

Having all the classes (Actor, Movie, Serie) in one module would make it
200, maybe 300 lines long.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
On Sun, Mar 29, 2015 at 3:56 AM Ben Finney 
wrote:
As a side issue: You apparently intend to choose names that are English
language.

If that's true, you should know that “actor”, “movie”, “series” are all
singular.

My bad, it's series indeed.


On Sun, Mar 29, 2015 at 10:33 AM Dave Angel  wrote:
I'd suggest that you NEVER call a module __main__.py The name
"__main__" is reserved for identifying the script file, and is faked
during the program initialization.

By using that name for an imported file, you could get some very
confusing errors later.

As Peter said, I'm using __main__.py so that I can invoke the the app like
'python pycinema' in the console. A bunch of people recommend this approach.


On Sun, Mar 29, 2015 at 11:31 AM Mark Lawrence 
wrote:
No.

We're talking Python, not Java. Some modules run to 1000s of lines, a
couple of hundred is just peanuts.

Ok, so you recommend me to have only 2 modules? One for the MDB API and one
for the app inside the pycinema package and then my __main__.py on the
folder root?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
Ok, applying what you guys said I have:

- folder: pycinema
- package: pycinema
- - core.py
- - mdb.py
- __main__.py

Current code on core.py:

# -*- coding: utf-8 -*-

from pycinema.mdb import MovieDB

API = MovieDB('API KEY GOES HERE')

class Actor:
def __init__(self, actor_id):
api = API.get_actor(actor_id)
self.name = api['name']
self.biography = api['biography']
self.actorid = api['id']

class Movie:
def __init__(self):
pass

class Series:
def __init__(self):
pass


Current code on mdb.py:

# -*- coding: utf-8 -*-

import requests

class MovieDB:
def __init__(self, api_key):
self.api_key = api_key

def get_actor(self, actor_id):
response = requests.get('http://api.themoviedb.org/3/person/' +
str(actor_id) + '?api_key=' + str(self.api_key)).json()
return {'name': response['name'], 'id': actor_id, 'biography':
response['biography']}


I think the get_actor return is a bit bad. What would be the best way to
return the data?

Should I use classes inside the MovieDB class (Actor, Movie, Series) and
inside those classes have different methods to get each data, like name,
birthday, bio, etc separately?

Only 1 call to the API gives all this data, example (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/person/678). If
I have different methods to get each data separately, I would be calling
this same URL many times, when the best approach would be only 1.

I won't need to get the ID from the API, as the user must provide the ID
when instantiating the Actor.

If you can't read the source code, try here: http://pastebin.com/1DWjxQwH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
On Sun, Mar 29, 2015 at 8:55 PM Alan Gauld 
wrote:
Can you explain how that works? Does the user create their own
random unique values? Do you use a source of unique keys?
Or could the Actor init() maybe generate an ID for the user?

But without the API knowing the ID, how does the correct data get
selected? Surely there must be some kind of unique key for that to happen?


The ID is set by the API, I don't have control over it, I can't modify it,
and I don't really need to modify it. There is an option to search the API
using the name, like so (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/search/person?query=Will%20Smith
).

Of course in the "user side" of the things I will ask for names, let's say.
The person wants to know all the movies that Will Smith played in. Of
course I won't ask the user for his ID (that is 287) because they wouldn't
know, the user will simply type his name.

And my code will call this part of the API to get the ID of the person, and
then create a new Actor using 'actor = Actor(287)'.

The same goes for Movie and Series related stuff, I just modify the
"/person?" to "/movie?" or "/tv?".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
On Sun, Mar 29, 2015 at 9:39 PM Juan C.  wrote:
The ID is set by the API, I don't have control over it, I can't modify it,
and I don't really need to modify it. There is an option to search the API
using the name, like so (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/search/person?query=Will%20Smith
).

Of course in the "user side" of the things I will ask for names, let's say.
The person wants to know all the movies that Will Smith played in. Of
course I won't ask the user for his ID (that is 287) because they wouldn't
know, the user will simply type his name.

And my code will call this part of the API to get the ID of the person, and
then create a new Actor using 'actor = Actor(287)'.

The same goes for Movie and Series related stuff, I just modify the
"/person?" to "/movie?" or "/tv?".


Oh, and yes, my bad, indeed, the user will provide the name only, my script
will get the ID.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to deploy seamless script updates to your "clients"?

2017-05-24 Thread Juan C.
I have some Python 3.6.0 scripts that my co-workers use for some small
and medium tasks. Whenever I have time I fix some bugs and add some
features to said scripts to make their lives (and mine :D) easier, but
there's a problem: I need to send a new script via email/chat/whatever
and they have to replace it wherever they use it, such a hassle.

How would I go to put a "update module" inside my script? I was
thinking about using Git, because those scripts are already in
personal repositories so that I can keep track of changes. Will I have
to setup any special permissions so that the scripts can pull requests
from my private repositories?

I was thinking of something like "check for update before start", if
an update is found the script would replace itself with the newer
version and restart, is that possible? For example, 'cool-tool.py'
v0.2 starts and find that version v0.3 is out, so it downloads and
replace 'cool-tool.py' code with newer code and restart as v0.3.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to deploy seamless script updates to your "clients"?

2017-05-26 Thread Juan C.
On Thu, May 25, 2017 at 1:01 AM, Abdur-Rahmaan Janhangeer
 wrote:
> a basic idea would be to get a webpage and put your code there. This is
> where you edit your codes
>
> Now you make a program which has
> - an updater
> - the script to execute in a separate file
>
> the updater each times pull the program on the webpage and compare it with
> the script file if they are the same, it does nothing. if not, it moves the
> current script to the "previous scripts" folder and create a new script file
>
> it then executes the script file (see how to execute python from python)
>
> the only thing you'll have to do if you want is some auth if your script is
> super secret

What does a webpage have to do with this, I really can't picture that.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor