Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2017 Gerry Williams

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# python
### Python

This repo will house python code that can be used freely (sanitized). Also, many of the functions have articles that go with them from my blog at [https://automationadmin.com](https://automationadmin.com).

### DISCLAIMER

Please do not use these scripts in a production environment without reading them over first. Please see the MIT [license](./LICENSE) for more information.
13 changes: 13 additions & 0 deletions learning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
16 changes: 16 additions & 0 deletions learning/base-language/_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3

################################################################
# Text
#
################################################################



def main():
print("called main function")



if __name__ == '__main__':
main()
36 changes: 36 additions & 0 deletions learning/base-language/calling-python-scripts/args-is-unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3

################################################################
# When ran, it will return 1 if all values passed in are unique and 0 if any repeat
# Maybe a good example of testing that each argument passed is unique?

# example: one repeates
# 5, 6, 6, 7
# 0

# Example all unique
# 6
# 1

# example: repeats
# 5,5
# 0

# example: unique
# 6,7
# 1

################################################################

def is_unique(s):
s = list(s)
s.sort()

for i in range(len(s) - 1):
if s[i] == s[i + 1]:
return 0
else:
return 1

if __name__ == "__main__":
print(is_unique(input()))
26 changes: 26 additions & 0 deletions learning/base-language/calling-python-scripts/args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

################################################################
# Script Arguments
# sys.argv is list of arguments passed to the script
# sys.argv[0] is by default the path of script
# and user defined arguments start with index 1
################################################################

import sys

# Print total number of arguments
print('Total number of arguments:', format(len(sys.argv)))

# Print all arguments
print('Argument List:', str(sys.argv))

# Print arguments one by one
print('First argument:', str(sys.argv[0]))
print('Second argument:', str(sys.argv[1]))
print('Third argument:', str(sys.argv[2]))

bob = str(sys.argv[0])
print("magically, the filename for this script is: " + bob)
# save and exit
# now type `chmod +x ./max.py` and `python3 ./max.py 13 23 57`
43 changes: 43 additions & 0 deletions learning/base-language/classes/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Car:
"""A simple attempt to represent a car."""

def __init__(self, make, model, year):
"""Initialize attributes to describe a car."""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()

def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):
"""
Set the odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")

def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles

my_used_car = Car('subaru', 'outback', 2015)
print(my_used_car.get_descriptive_name())

my_used_car.update_odometer(23_500)
my_used_car.read_odometer()

my_used_car.increment_odometer(100)
my_used_car.read_odometer()


26 changes: 26 additions & 0 deletions learning/base-language/classes/dog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Dog:
"""A simple attempt to model a dog."""

def __init__(self, name, age):
"""Initialize name and age attributes."""
self.name = name
self.age = age

def sit(self):
"""Simulate a dog sitting in response to a command."""
print(f"{self.name} is now sitting.")

def roll_over(self):
"""Simulate rolling over in response to a command."""
print(f"{self.name} rolled over!")

my_dog = Dog('Willie', 6)
your_dog = Dog('Lucy', 3)

print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
my_dog.sit()

print(f"\nYour dog's name is {your_dog.name}.")
print(f"Your dog is {your_dog.age} years old.")
your_dog.sit()
43 changes: 43 additions & 0 deletions learning/base-language/classes/electric_car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Car:
"""A simple attempt to represent a car."""

def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()

def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")

def increment_odometer(self, miles):
self.odometer_reading += miles

class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""

def __init__(self, make, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(make, model, year)
self.battery_size = 75

def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")

my_tesla = ElectricCar('tesla', 'model s', 2019)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
65 changes: 65 additions & 0 deletions learning/base-language/classes/electric_car_with_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Car:
"""A simple attempt to represent a car."""

def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()

def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")

def increment_odometer(self, miles):
self.odometer_reading += miles

class Battery:
"""A simple attempt to model a battery for an electric car."""

def __init__(self, battery_size=75):
"""Initialize the battery's attributes."""
self.battery_size = battery_size

def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")

def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 75:
range = 260
elif self.battery_size == 100:
range = 315

print(f"This car can go about {range} miles on a full charge.")


class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""

def __init__(self, make, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(make, model, year)
self.battery = Battery()

def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")

my_tesla = ElectricCar('tesla', 'model s', 2019)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
65 changes: 65 additions & 0 deletions learning/base-language/classes/importing_classes/car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""A class that can be used to represent a car."""

class Car:
"""A simple attempt to represent a car."""

def __init__(self, make, model, year):
"""Initialize attributes to describe a car."""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()

def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):
"""
Set the odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")

def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles

class Battery:
"""A simple attempt to model a battery for an electric car."""

def __init__(self, battery_size=75):
"""Initialize the battery's attributes."""
self.battery_size = battery_size

def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")

def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 75:
range = 260
elif self.battery_size == 100:
range = 315

print(f"This car can go about {range} miles on a full charge.")

class ElectricCar(Car):
"""Models aspects of a car, specific to electric vehicles."""

def __init__(self, make, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(make, model, year)
self.battery = Battery()
7 changes: 7 additions & 0 deletions learning/base-language/classes/importing_classes/my_car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from car import Car

my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())

my_new_car.odometer_reading = 23
my_new_car.read_odometer()
7 changes: 7 additions & 0 deletions learning/base-language/classes/importing_classes/my_cars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import car

my_beetle = car.Car('volkswagen', 'beetle', 2019)
print(my_beetle.get_descriptive_name())

my_tesla = car.ElectricCar('tesla', 'roadster', 2019)
print(my_tesla.get_descriptive_name())
Loading