diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/LICENSE b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/LICENSE new file mode 100644 index 0000000..2c7cba7 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/LICENSE @@ -0,0 +1,8 @@ +"THE BEER-WARE LICENSE" (Revision 42): + + wrote this file. As long as you retain this +notice you can do whatever you want with this stuff. If we meet some +day, and you think this stuff is worth it, you can buy me a beer in +return. + +Miguel Angel Astor. \ No newline at end of file diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/README.org b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/README.org new file mode 100644 index 0000000..f7bbeec --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/README.org @@ -0,0 +1,7 @@ +* RSP: Rock Scissors Paper + +RSP is a Python 3 clone of the classic MacOS software toy from 1996 +[[https://macintoshgarden.org/games/rock-scissors-paper][Rock Scissors Paper]] by shareware publisher Argus IG. + +#+ATTR_HTML: :alt Playing rock paper scissors against a robot. :align center +[[file:rsp2.png]] diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.1.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.1.png new file mode 100644 index 0000000..d5410ca Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.1.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.2.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.2.png new file mode 100644 index 0000000..c31e68d Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.2.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.3.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.3.png new file mode 100644 index 0000000..44138e6 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/btn.3.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.1.1.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.1.1.png new file mode 100644 index 0000000..c1fdd94 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.1.1.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.1.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.1.png new file mode 100644 index 0000000..b357689 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.1.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.2.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.2.png new file mode 100644 index 0000000..55023da Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.2.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.3.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.3.png new file mode 100644 index 0000000..38ffe5d Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.2.3.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.1.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.1.png new file mode 100644 index 0000000..664335e Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.1.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.2.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.2.png new file mode 100644 index 0000000..a4a2cd7 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.2.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.3.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.3.png new file mode 100644 index 0000000..7e691ca Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.3.3.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.1.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.1.png new file mode 100644 index 0000000..70c88b0 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.1.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.2.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.2.png new file mode 100644 index 0000000..89e35c7 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.2.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.3.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.3.png new file mode 100644 index 0000000..c7c1018 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/assets/gfx/eyeguy.4.3.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/rsp2.png b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/rsp2.png new file mode 100644 index 0000000..ca07b01 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/rsp2.png differ diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/rsvp.py b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/rsvp.py new file mode 100644 index 0000000..b1011ca --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/RSP/rsvp.py @@ -0,0 +1,176 @@ +#! /usr/bin/env python3 + +import random as r +import threading as th +import tkinter as tk + +# Gampelay constants +ROCK = 0 +SCISSORS = 1 +PAPER = 2 + +# Score variables +PLAYER_SCORE = 0 +EYEGUY_SCORE = 0 + +# Threading timer object +TIMER = None + +def main(): + """ Main method of the game. """ + + def reset(): + """ Restarts the game. """ + global PLAYER_SCORE, EYEGUY_SCORE + PLAYER_SCORE = 0 + EYEGUY_SCORE = 0 + player_label["text"] = "Humanoid> " + str(PLAYER_SCORE) + eyeguy_label["text"] = str(EYEGUY_SCORE) + " 80: + print(a) + a -= 1 + +######################## +# Function definitions # +######################## + +# No arguments + +def fun(): + return 89 + +fun() + +# Functions without a return statement return None + +def non(): + pass + +print(non()) + +# Positional arguments + +def sum(a, b): + return a + b + +print(sum(1, 2)) + +# Keyword arguments + +def divide(dividend = 1, divisor = 1): + return dividend / divisor # Unsafe!! + +print(divide()) +print(divide(divisor = 2)) +print(divide(dividend = 4, divisor = 2)) +print(divide(divisor = 4, dividend = 2)) + +# Both argument types + +def xnp(x, n, p = 1): + # Keyword args MUST appear AFTER positional args! + return (x * n) + p + +# Variable length arguments + +def varargs(*args): + for a in args: + print("Argument " + str(args.index(a)) + " is " + str(a)) + +varargs(1) +varargs(2, 3) +varargs(4, 5, 6) + +# Variable keyword arguments + +def varkwargs(**kwargs): + for k in kwargs.keys(): + print('Argument "' + str(k) + '" is "' + str(kwargs[k])) + +varkwargs(a = 1, b = 2, c = 3) + +# Everything! + +def allargs(a, b, c = None, *args, **kwargs): + print("a is " + str(a)) + + print("b is " + str(b)) + + if c is None: + print("c is None") + else: + print("c is Some") + + for a in args: + print("Argument " + str(args.index(a)) + " is " + str(a)) + + for k in kwargs.keys(): + print('Argument "' + str(k) + '" is "' + str(kwargs[k])) + +allargs('a', 1, None, 2, 3, 4, q = "Hail", w = "Caesar!") +allargs(1, 2, c = 89) + +# Nested functions + +def outer(x): + def inner(y): + return x + y + + return inner(9) + +print(outer(1)) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/classes.py b/Ecoanova/Introduccion a Python 3/Ejemplos/classes.py new file mode 100644 index 0000000..2699eaa --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/classes.py @@ -0,0 +1,33 @@ +################ +# Simple class # +################ + +class Class(object): + def __init__(self, a): + self.a = a + + def method(self): + return self.a + +o = Class(1) +print(o.method()) +print(o.a) # ! + +#################### +# With inheritance # +#################### + +class Subclass(Class): + def __init__(self, a, b): + super(Subclass, self).__init__(a) + self.b = b + + def method(self): + return self.b + + def sub_method(self): + return self.a + +s = Subclass(1, 2) +print(s.method()) +print(s.sub_method()) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/db.py b/Ecoanova/Introduccion a Python 3/Ejemplos/db.py new file mode 100644 index 0000000..26e24c9 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/db.py @@ -0,0 +1,36 @@ +import sqlite3 + +# "Connect" to a database +conn = sqlite3.connect('example.db') + +# Get a cursor to operate on the database +c = conn.cursor() + +# Create table +c.execute('''CREATE TABLE stocks + (date text, trans text, symbol text, qty real, price real)''') + +# Insert a row of data +c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") + +# Save (commit) the changes +conn.commit() + +# Select +t = ('RHAT',) +c.execute('SELECT * FROM stocks WHERE symbol=?', t) +print(c.fetchone()) + +# Larger example that inserts many records at a time +purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), + ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00), + ('2006-04-06', 'SELL', 'IBM', 500, 53.00), + ] +c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases) + +for row in c.execute('SELECT * FROM stocks ORDER BY price'): + print(row) + +# We can also close the connection if we are done with it. +# Just be sure any changes have been committed or they will be lost. +conn.close() diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/example.json b/Ecoanova/Introduccion a Python 3/Ejemplos/example.json new file mode 100644 index 0000000..04288b3 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/example.json @@ -0,0 +1,28 @@ +{ + "firstName": "John", + "lastName": "Smith", + "isAlive": true, + "age": 25, + "address": { + "streetAddress": "21 2nd Street", + "city": "New York", + "state": "NY", + "postalCode": "10021-3100" + }, + "phoneNumbers": [ + { + "type": "home", + "number": "212 555-1234" + }, + { + "type": "office", + "number": "646 555-4567" + }, + { + "type": "mobile", + "number": "123 456-7890" + } + ], + "children": [], + "spouse": null +} diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/functional.py b/Ecoanova/Introduccion a Python 3/Ejemplos/functional.py new file mode 100644 index 0000000..f1d3ec5 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/functional.py @@ -0,0 +1,31 @@ +################################ +# Lambda (anonymous) functions # +################################ + +f = lambda x, y: x + y +print(f(1, 2)) +print() + +########################## +# Higher order functions # +########################## + +# Map: Applies a function to many lists. Returns a generator. + +for i in map(lambda x: x*x, [1, 2, 3, 4, 5, 6]): + print(i) +print() + +for i in map(f, [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7]): + print(i) +print() + +# Custom higher order function + +def hof(function = lambda x: x, *args): + for a in args: + print(function(a)) + +hof(lambda s: s.upper(), "a", "b", "c") +print() +hof(lambda x: x is None, 1, ["a", "b"], None, {}) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/generators.py b/Ecoanova/Introduccion a Python 3/Ejemplos/generators.py new file mode 100644 index 0000000..2c435d8 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/generators.py @@ -0,0 +1,46 @@ +#################### +# Generator object # +#################### + +def simple_generator(): + i = 0 + while(i < 10): + yield i + i += 1 + raise StopIteration + +g = simple_generator() + +for i in g: + print(i) + +try: + print(next(g)) +except StopIteration: + print("Generator exhausted!") +else: + print("Generator success!") +finally: + print("At the end!") + +#################### +# Collection class # +#################### + +class Collection(object): + def __init__(self): + self.c = [] + + def add(self, x): + self.c.append(x) + + def __iter__(self): + for i in self.c: + yield i + +c = Collection() +c.add(1) +c.add(2) +c.add(3) +for i in c: + print(i) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/http_client.py b/Ecoanova/Introduccion a Python 3/Ejemplos/http_client.py new file mode 100644 index 0000000..52c37c2 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/http_client.py @@ -0,0 +1,7 @@ +import urllib.request + +# Create an HTTP request and get the response. +response = urllib.request.urlopen("http://www.concisa.net.ve/2016/evi-2016-tutoriales/") +# Get the response data. +document = response.read() +print(document) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/http_server.py b/Ecoanova/Introduccion a Python 3/Ejemplos/http_server.py new file mode 100644 index 0000000..2ba8df8 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/http_server.py @@ -0,0 +1,8 @@ +from http.server import HTTPServer, SimpleHTTPRequestHandler + +def run(server_class = HTTPServer, handler_class = SimpleHTTPRequestHandler): + server_address = ('127.0.0.1', 8000) + httpd = server_class(server_address, handler_class) + httpd.serve_forever() + +run() diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/jsoning.py b/Ecoanova/Introduccion a Python 3/Ejemplos/jsoning.py new file mode 100644 index 0000000..f4cb7d9 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/jsoning.py @@ -0,0 +1,16 @@ +import json + +data = {"Name": "Miguel", + "Age": 27, + "ID": 18810993, + "info" : {"Height": 1.72, + "Weight": 80 + } + } + +with open("data.json", "w") as f: + f.write(json.dumps(data)) + +with open("example.json") as f: + j = json.loads(f.read()) + print(j["isAlive"]) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/lists.py b/Ecoanova/Introduccion a Python 3/Ejemplos/lists.py new file mode 100644 index 0000000..abb7995 --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/lists.py @@ -0,0 +1,60 @@ +a = [1, 2, 3, 4, 5, 6, 7, 8, 9] + +################# +# List indexing # +################# + +# Classical position indexing + +i = 0 +while i < len(a): + print(a[i]) + i += 1 + +# Negative indices + +print(a[-1]) +print(a[-2]) +print(a[-3]) + +################ +# List slicing # +################ + +# Elements between indices 3 and 7 + +print(a[3:7]) + +# Elements from index 5 onwards + +print(a[5:]) + +# Elements from the start up to index 8 + +print(a[:8]) + +####################### +# List comprehensions # +####################### + +# The first 10 square natural numbers + +l = [x * x for x in range(1, 10)] +print(l) + +# The first even square natural numbers + +l = [x * x for x in range(1, 10) if (x * x) % 2 == 0] +print(l) + +# Some numbers from the first list + +l = [x for x in a if x > 2 and x < 7] +print(l) + +####### +# zip # +####### + +l = [x for x in zip([1, 2, 3, 4], ["a", "b", "c"], ["Hello", ",", "World", "!"])] +print(l) diff --git a/Ecoanova/Introduccion a Python 3/Ejemplos/qsort.py b/Ecoanova/Introduccion a Python 3/Ejemplos/qsort.py new file mode 100644 index 0000000..25b81bf --- /dev/null +++ b/Ecoanova/Introduccion a Python 3/Ejemplos/qsort.py @@ -0,0 +1,16 @@ +import random as r + +def qsort(l): + if len(l) == 0: + return [] + elif len(l) == 1: + return l + else: + less = qsort([x for x in l[1:] if x <= l[0]]) + more = qsort([x for x in l[1:] if x > l[0]]) + return less + [l[0]] + more + +a = [r.randint(0, 100) for x in range(100)] +b = qsort(a) +print(a) +print(b) diff --git a/Ecoanova/Introduccion a Python 3/python.pdf b/Ecoanova/Introduccion a Python 3/python.pdf new file mode 100644 index 0000000..bd3c190 Binary files /dev/null and b/Ecoanova/Introduccion a Python 3/python.pdf differ